*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  min-height: 100dvh;
  background: var(--surface-0);
  color: var(--text-primary);
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}

body { min-height: 100vh; min-height: 100dvh; }

a { color: var(--accent); }

.skip {
  position: absolute;
  inset-inline-start: -999px;
  top: 8px;
  background: var(--accent);
  color: var(--text-inverse);
  padding: 8px 16px;
  z-index: 100;
  border-radius: var(--radius-8);
}
.skip:focus { inset-inline-start: 8px; }

.num {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  direction: ltr;
  unicode-bidi: isolate;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}

.hidden { display: none !important; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.app {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* ---------- topbar ---------- */

.topbar {
  min-height: var(--topbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-12);
  flex-wrap: wrap;
  padding: var(--space-12) var(--space-24);
  padding-top: calc(var(--space-12) + var(--safe-top));
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
}
.topbar-end {
  display: flex;
  align-items: center;
  gap: var(--space-8);
  flex-wrap: wrap;
}
.brand { text-align: start; }
.brand h1 { margin: 0; font-size: 1.25rem; font-weight: 700; }
.brand p { margin: 0; font-size: 0.75rem; color: var(--text-muted); }

.status, .link-pill {
  display: flex;
  align-items: center;
  gap: var(--space-8);
  font-weight: 600;
  font-size: 0.85rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 12px;
  white-space: nowrap;
}
.status-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--warning);
  flex: none;
}
.status[data-phase="ready"] .status-dot,
.status[data-phase="scanning"] .status-dot { background: var(--success); }
.status[data-phase="done"] .status-dot { background: var(--accent); }
.status[data-phase="need_account"] .status-dot,
.status[data-phase="error"] .status-dot { background: var(--danger); }

.link-pill[data-live="true"] .status-dot { background: var(--success); }
.link-pill[data-live="false"] { border-color: var(--danger); color: var(--danger); }
.link-pill[data-live="false"] .status-dot { background: var(--danger); }

.btn-icon { min-height: 40px; padding-inline: 12px; }

/* ---------- share bar ---------- */

.share-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  padding: 10px var(--space-24);
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  font-size: 0.8rem;
}
.share-label {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-muted);
}
.share-chip {
  flex: none;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.68rem;
  font-weight: 700;
  border: 1px solid var(--border);
  background: var(--surface-3);
  color: var(--text-secondary);
}
.share-bar[data-kind="permanent"] .share-chip {
  border-color: rgba(46, 204, 113, 0.45);
  background: rgba(46, 204, 113, 0.14);
  color: #7ee2a8;
}
.share-bar[data-kind="temporary"] .share-chip {
  border-color: rgba(241, 196, 15, 0.45);
  background: rgba(241, 196, 15, 0.14);
  color: #f4d47c;
}
.share-bar[data-kind="problem"] .share-chip {
  border-color: rgba(231, 76, 60, 0.5);
  background: rgba(231, 76, 60, 0.14);
  color: #ff9d92;
}
.share-bar .url {
  flex: 1;
  min-width: 0;
  font-family: var(--font-mono);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text-secondary);
}
.share-bar .btn { min-height: 40px; }

/* ---------- layout ---------- */

.layout {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--rail-w);
  gap: var(--space-12);
  padding: var(--space-12);
  max-width: 1400px;
  width: 100%;
  margin-inline: auto;
}
.main, .rail { min-width: 0; }

.rail {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-16);
  padding: var(--space-16);
  display: flex;
  flex-direction: column;
  gap: var(--space-16);
  align-content: start;
}
.rail-group { display: flex; flex-direction: column; gap: var(--space-8); }

.section-label {
  margin: 0;
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--text-muted);
  letter-spacing: 0.04em;
  text-transform: none;
}
.hint { margin: 0; font-size: 0.78rem; color: var(--text-muted); line-height: 1.45; }
.file-name {
  margin: 0;
  color: var(--accent);
  font-size: 0.9rem;
  word-break: break-word;
}

/* ---------- buttons ---------- */

.btn {
  min-height: var(--touch);
  border: 0;
  border-radius: var(--radius-8);
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  padding: 0 var(--space-16);
  transition: background var(--t-fast) var(--ease), opacity var(--t-fast) var(--ease);
}
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn[data-blocked="true"] { opacity: 0.55; }
.btn-primary { background: #1b5e20; color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--success); color: var(--text-inverse); }
.btn-danger { background: #b71c1c; color: #fff; }
.btn-danger:hover:not(:disabled) { background: var(--danger); }
.btn-secondary {
  background: var(--surface-3);
  color: var(--text-primary);
  border: 1px solid var(--border-strong);
}
.btn-secondary:hover:not(:disabled) { background: #2a4664; }
.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  min-height: 42px;
  border: 1px solid var(--border);
}
.btn-ghost:hover:not(:disabled) { background: var(--surface-2); color: var(--text-primary); }
.btn-accent { background: var(--accent); color: var(--text-inverse); }
.btn-accent:hover:not(:disabled) { background: var(--accent-hover); }
.btn-block { width: 100%; }

/* ---------- stepper ---------- */

.stepper-wrap { margin-bottom: var(--space-12); }
.stepper {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--space-8);
}
.step {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-12);
  padding: 10px 12px;
  font-size: 0.82rem;
  color: var(--text-muted);
  min-width: 0;
}
.step-num {
  width: 24px;
  height: 24px;
  flex: none;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--surface-3);
  color: var(--text-secondary);
  font-family: var(--font-mono);
  font-size: 0.78rem;
  font-weight: 600;
}
.step-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.step[data-state="done"] { border-color: rgba(61, 203, 122, 0.4); color: var(--text-secondary); }
.step[data-state="done"] .step-num { background: var(--success); color: var(--text-inverse); }
.step[data-state="current"] {
  border-color: var(--accent);
  background: var(--info-bg);
  color: var(--text-primary);
  font-weight: 600;
}
.step[data-state="current"] .step-num { background: var(--accent); color: var(--text-inverse); }

/* ---------- guide card ---------- */

.guide {
  background: var(--surface-1);
  border: 1px solid var(--border-strong);
  border-inline-start: 4px solid var(--accent);
  border-radius: var(--radius-12);
  padding: var(--space-16);
  margin-bottom: var(--space-12);
}
.guide[data-tone="success"] { border-inline-start-color: var(--success); }
.guide[data-tone="warn"] { border-inline-start-color: var(--warning); }
.guide[data-tone="error"] { border-inline-start-color: var(--danger); background: var(--danger-bg); }
.guide-head {
  display: flex;
  align-items: center;
  gap: var(--space-8);
  flex-wrap: wrap;
  margin-bottom: 6px;
}
.guide-head h2 { margin: 0; font-size: 1.05rem; }
.guide-badge {
  font-size: 0.7rem;
  font-weight: 700;
  background: var(--surface-3);
  color: var(--text-secondary);
  border-radius: 999px;
  padding: 3px 10px;
}
.guide-text { margin: 0 0 10px; color: var(--text-secondary); }
.guide-how {
  margin: 0 0 12px;
  padding-inline-start: 20px;
  color: var(--text-secondary);
  font-size: 0.9rem;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.guide-how:empty { display: none; }
.guide-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- cards ---------- */

.cards {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--space-8);
  margin-bottom: var(--space-12);
}
.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-12);
  padding: var(--space-16);
  box-shadow: var(--shadow-card);
  min-width: 0;
}
.card .label { margin: 0; color: var(--text-muted); font-size: 0.78rem; }
.card .value { margin: 4px 0 0; font-size: 1.75rem; font-weight: 700; }
.card .pct { margin: 0; font-size: 0.78rem; color: var(--text-muted); min-height: 1.2em; }
.card.found .value { color: var(--success); }
.card.nf .value { color: var(--warning); }
.card.err .value { color: var(--danger); }
.card.total .value { color: var(--accent); }

/* ---------- panels ---------- */

.panel {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-12);
  padding: var(--space-16);
  margin-bottom: var(--space-12);
}
.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: var(--space-8);
}
.panel-head h2 { margin: 0; font-size: 0.95rem; }
.panel-note { margin: 10px 0 0; font-size: 0.78rem; color: var(--text-muted); }

.progress {
  height: 14px;
  background: var(--surface-3);
  border-radius: 999px;
  overflow: hidden;
}
.progress > span {
  display: block;
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width var(--t-fast) var(--ease);
}
.meta-row {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-top: var(--space-8);
  color: var(--text-secondary);
  font-size: 0.85rem;
}

.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.found-table { width: 100%; border-collapse: collapse; }
.found-table th, .found-table td {
  text-align: start;
  padding: 9px 6px;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}
.found-table th { color: var(--text-muted); font-weight: 500; font-size: 0.75rem; }
.empty { text-align: center; color: var(--text-muted); padding: var(--space-24); }

.log {
  height: 150px;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--surface-2);
  border-radius: var(--radius-8);
  padding: var(--space-8) var(--space-12);
  font-family: var(--font-mono);
  font-size: 0.76rem;
  line-height: 1.6;
  color: var(--text-secondary);
}
.log .warn { color: var(--warning); }
.log .error { color: var(--danger); }
.log .success, .log .found { color: var(--success); }
.log .info { color: var(--accent); }

/* ---------- accounts ---------- */

.accounts {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 240px;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}
.account {
  background: var(--surface-2);
  border-radius: var(--radius-8);
  border: 1px solid transparent;
  padding: 10px 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}
.account.bad { background: var(--danger-bg); border-color: rgba(239, 90, 90, 0.4); }
.account.pending { border-color: rgba(232, 184, 74, 0.45); }
.account .meta { font-size: 0.78rem; color: var(--text-muted); }
.account button { min-height: 36px; padding-inline: 10px; font-size: 0.8rem; }

.more {
  border-top: 1px solid var(--border);
  padding-top: var(--space-12);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
#moreBody { display: flex; flex-direction: column; gap: 8px; }

/* ---------- dialogs ---------- */

.dialog-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(4, 8, 15, 0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 60;
  padding: var(--space-16);
}
.dialog {
  width: min(460px, 100%);
  max-height: 88dvh;
  overflow: auto;
  background: var(--surface-1);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-16);
  padding: var(--space-24);
  box-shadow: var(--shadow-card);
}
.dialog-wide { width: min(640px, 100%); }
.dialog h2 { margin: 0 0 8px; font-size: 1.15rem; }
.dialog > p { margin: 0 0 16px; color: var(--text-secondary); }
.dialog-kicker {
  margin: 0 0 4px;
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--danger);
}
.dialog-problem { border-color: rgba(239, 90, 90, 0.45); }

.how-box {
  background: var(--surface-2);
  border-radius: var(--radius-8);
  padding: var(--space-12) var(--space-16);
  margin-bottom: var(--space-16);
}
.how-title {
  margin: 0 0 6px;
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--text-muted);
}
.how-box .guide-how { margin: 0; font-size: 0.88rem; }

.explain {
  background: var(--surface-2);
  border-radius: var(--radius-8);
  padding: 10px 14px;
  margin-bottom: var(--space-16);
  font-size: 0.85rem;
  color: var(--text-secondary);
}
.explain summary { cursor: pointer; font-weight: 600; color: var(--accent); }
.explain ol { margin: 10px 0 0; padding-inline-start: 20px; display: flex; flex-direction: column; gap: 6px; }

.field { margin-bottom: var(--space-12); }
.field label { display: block; margin-bottom: 6px; color: var(--text-muted); font-size: 0.85rem; }
.field input {
  width: 100%;
  min-height: 48px;
  border-radius: var(--radius-8);
  border: 1px solid var(--border-strong);
  background: var(--surface-2);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 1rem;
  padding-inline: 12px;
}
.field input:focus { border-color: var(--accent); }
.field input[aria-invalid="true"] { border-color: var(--danger); }
.field .hint { margin-top: 5px; }

.stat-list {
  margin: 0 0 16px;
  padding-inline-start: 20px;
  color: var(--text-secondary);
  font-size: 0.88rem;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.stat-list:empty { display: none; }

.dialog-actions { display: flex; gap: 10px; margin-top: 8px; }
.dialog-actions .btn { flex: 1; }
.err { color: var(--danger); font-size: 0.85rem; min-height: 1.2em; margin: 0 0 8px; }

.help-body { color: var(--text-secondary); font-size: 0.9rem; }
.help-body h3 {
  margin: 20px 0 6px;
  font-size: 0.95rem;
  color: var(--text-primary);
}
.help-body h3:first-child { margin-top: 0; }
.help-body p { margin: 0 0 8px; }
.help-body ul, .help-body ol {
  margin: 0 0 8px;
  padding-inline-start: 20px;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

/* ---------- toasts ---------- */

.toasts {
  position: fixed;
  inset-inline: 0;
  bottom: 0;
  z-index: 55;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 0 var(--space-16) calc(var(--space-16) + var(--safe-bottom));
  pointer-events: none;
}
.toast {
  pointer-events: auto;
  width: min(440px, 100%);
  background: var(--surface-3);
  border: 1px solid var(--border-strong);
  border-inline-start: 4px solid var(--accent);
  border-radius: var(--radius-8);
  padding: 12px 14px;
  box-shadow: var(--shadow-card);
  font-size: 0.88rem;
  display: flex;
  align-items: flex-start;
  gap: 10px;
  animation: toast-in 180ms var(--ease);
}
.toast[data-tone="success"] { border-inline-start-color: var(--success); }
.toast[data-tone="warn"] { border-inline-start-color: var(--warning); }
.toast[data-tone="error"] { border-inline-start-color: var(--danger); }
.toast p { margin: 0; flex: 1; }
.toast .toast-title { font-weight: 700; margin-bottom: 2px; }
.toast button {
  background: none;
  border: 0;
  color: var(--text-muted);
  font-size: 1.1rem;
  cursor: pointer;
  line-height: 1;
  padding: 2px 4px;
}

@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.dock { display: none; }

/* ---------- mobile / iPhone ---------- */

@media (max-width: 900px) {
  .topbar {
    padding-inline: 14px;
    gap: 8px;
  }
  .brand h1 { font-size: 1.02rem; }
  .topbar-end { gap: 6px; }
  .status, .link-pill { font-size: 0.75rem; padding: 5px 10px; }

  .share-bar { padding: 10px 14px; }
  .share-bar .btn { flex: 1; min-width: 140px; }

  .layout {
    display: flex;
    flex-direction: column;
    padding: 10px 10px calc(var(--dock-h) + var(--safe-bottom) + 10px);
  }
  .main { order: 1; }
  .rail { order: 2; }
  .rail-scan { display: none; }

  .stepper { grid-template-columns: repeat(4, 1fr); gap: 6px; }
  .step { flex-direction: column; gap: 4px; padding: 8px 4px; text-align: center; font-size: 0.68rem; }
  .step-title { max-width: 100%; }

  .cards { grid-template-columns: 1fr 1fr; }
  .card { padding: var(--space-12); }
  .card .value { font-size: 1.5rem; }

  .guide { padding: var(--space-12); }
  .guide-actions .btn { flex: 1; min-width: 140px; }

  .found-table th, .found-table td { font-size: 0.82rem; padding: 10px 4px; }
  .log { height: 130px; }
  .accounts { max-height: 200px; }

  .dialog-backdrop { align-items: flex-end; padding: 0; }
  .dialog {
    width: 100%;
    max-width: none;
    border-radius: var(--radius-16) var(--radius-16) 0 0;
    padding-bottom: calc(var(--space-24) + var(--safe-bottom));
    max-height: 92dvh;
  }
  .field input { font-size: 16px; }

  .toasts { padding-bottom: calc(var(--dock-h) + var(--safe-bottom) + 10px); }

  .dock {
    display: flex;
    position: fixed;
    bottom: 0;
    inset-inline: 0;
    gap: 8px;
    padding: 10px 12px calc(10px + var(--safe-bottom));
    background: var(--surface-1);
    border-top: 1px solid var(--border);
    z-index: 40;
  }
  .dock .btn { flex: 1; min-height: 52px; }
}

@media (max-width: 380px) {
  .step-title { display: none; }
  .step { padding: 8px 2px; }
}
