/* ── Tokens ────────────────────────────────────────────── */
:root {
  --brand: #2563eb;
  --brand-dark: #1d4ed8;
  --brand-soft: #eff6ff;
  --ink: #0f2747;
  --text: #334155;
  --muted: #64748b;
  --line: #e2e8f0;
  --surface: #fff;
  --page: #f4f7fb;
  --ok: #15803d;
  --ok-soft: #dcfce7;
  --bad: #b91c1c;
  --bad-soft: #fee2e2;
  --warn: #b45309;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(15, 39, 71, .05), 0 8px 24px rgba(15, 39, 71, .06);
}

/* ── Reset & base ──────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
               "Helvetica Neue", Arial, "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  background: var(--page);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
}
.icon-sprite { position: absolute; }
.icon {
  width: 18px; height: 18px;
  flex-shrink: 0;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.icon-lg { width: 32px; height: 32px; color: var(--brand); }
:focus-visible { outline: 3px solid #93c5fd; outline-offset: 2px; }
h2 { font-size: 1.15rem; font-weight: 700; color: var(--ink); margin-bottom: 1rem; }

/* ── Layout shell ──────────────────────────────────────── */
.page-wrap {
  max-width: 1040px;
  margin: 0 auto;
  padding: 1.25rem 1.25rem 2rem;
}
.split {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 380px);
  gap: 1.25rem;
  align-items: start;
}
.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.5rem;
}
.preview-card {
  position: sticky;
  top: 1rem;
  display: flex;
  flex-direction: column;
  gap: .9rem;
}

/* ── Header ────────────────────────────────────────────── */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: .75rem;
  margin-bottom: 1.25rem;
}
.brand { display: flex; align-items: center; gap: .75rem; }
.brand img { border-radius: 12px; }
.brand h1 { font-size: 1.35rem; font-weight: 800; line-height: 1.2; color: var(--ink); letter-spacing: -.3px; }
.brand p { font-size: .84rem; color: var(--muted); }
.header-actions { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
#pwa-status { font-size: .78rem; color: var(--ok); }

/* ── Tabs ──────────────────────────────────────────────── */
.tabs {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: .25rem;
  margin-bottom: 1.25rem;
  padding: .3rem;
  background: #e8eef6;
  border-radius: 14px;
}
.tabs button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: .45rem;
  min-height: 44px;
  padding: .45rem .5rem;
  border: none;
  border-radius: 11px;
  background: none;
  font: inherit;
  font-size: .92rem;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  transition: background .15s, color .15s;
}
.tabs button:hover { color: var(--ink); }
.tabs button[aria-selected="true"] {
  background: var(--surface);
  color: var(--brand);
  box-shadow: 0 1px 3px rgba(15, 39, 71, .14);
}

/* ── Buttons ───────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .45rem;
  min-height: 40px;
  padding: .5rem 1.1rem;
  border: none;
  border-radius: 10px;
  font: inherit;
  font-size: .9rem;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  transition: background .15s, opacity .15s;
}
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary { background: var(--brand); color: #fff; }
.btn-primary:not(:disabled):hover { background: var(--brand-dark); }
.btn-secondary { background: #e8eef6; color: var(--ink); }
.btn-secondary:not(:disabled):hover { background: #d9e2ee; }
.btn-ghost { background: none; color: var(--muted); border: 1px solid var(--line); }
.btn-ghost:hover { color: var(--ink); background: var(--surface); }
.btn-sm { min-height: 34px; padding: .3rem .8rem; font-size: .82rem; border-radius: 8px; }
.btn-lg { min-height: 50px; width: 100%; font-size: 1rem; }

.share-row { display: flex; flex-wrap: wrap; gap: .5rem; }
.share-row .btn { flex: 1 1 auto; }
#qp-share-status:empty, #bc-share-status:empty { display: none; }
#bc-downloads .share-row, #bc-share-status { margin-top: .6rem; }

/* ── Form fields ───────────────────────────────────────── */
.field { display: flex; flex-direction: column; gap: .35rem; margin-top: 1rem; }
.field label, .field-label { font-size: .85rem; font-weight: 600; color: var(--text); }
.optional { font-weight: 400; color: var(--muted); }
.hint { font-size: .82rem; color: var(--muted); word-break: break-word; }
input[type="text"], input[type="number"], textarea, select {
  width: 100%;
  padding: .6rem .75rem;
  border: 1px solid #cbd5e1;
  border-radius: 10px;
  background: #fff;
  font: inherit;
  font-size: .95rem;
  color: var(--ink);
  transition: border-color .15s, box-shadow .15s;
}
input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, .18);
}
textarea { resize: vertical; }
.error-text { font-size: .85rem; color: var(--bad); }
.error-text:empty { display: none; }
.check-row { display: flex; align-items: center; gap: .45rem; margin-top: .75rem; font-size: .88rem; cursor: pointer; }

/* ── Segmented control ─────────────────────────────────── */
.segmented {
  display: flex;
  gap: .25rem;
  padding: .25rem;
  background: #eef2f7;
  border-radius: 12px;
}
.segmented label { flex: 1; cursor: pointer; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span {
  display: block;
  padding: .5rem .5rem;
  border-radius: 9px;
  text-align: center;
  font-size: .92rem;
  font-weight: 600;
  color: var(--muted);
  transition: background .15s, color .15s;
}
.segmented input:checked + span {
  background: var(--surface);
  color: var(--brand);
  box-shadow: 0 1px 3px rgba(15, 39, 71, .14);
}
.segmented input:focus-visible + span { outline: 3px solid #93c5fd; }
.segmented-sm span { padding: .32rem .4rem; font-size: .82rem; }
.format-row { display: flex; align-items: center; gap: .75rem; }
.format-row .field-label { white-space: nowrap; }
.format-row .segmented { flex: 1; }
#format-hint { margin-top: .4rem; }
#settings-slot-batch:not(:empty) { margin-top: 1.25rem; padding-top: 1.25rem; border-top: 1px solid var(--line); max-width: 460px; }

/* ── Collapsible sections ──────────────────────────────── */
.more { margin-top: .9rem; }
.more summary {
  font-size: .85rem;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  width: fit-content;
}
.more summary:hover { color: var(--ink); }
.more[open] summary { margin-bottom: .25rem; }
.more-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 0 1rem; }

/* ── Live preview ──────────────────────────────────────── */
.qr-stage, .bc-stage {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 300px;
  padding: 1rem;
  border-radius: 12px;
  background: #f8fafc;
  overflow: hidden;
}
.qr-stage canvas, .qr-stage img {
  width: 100%;
  max-width: 280px;
  height: auto;
  border-radius: 6px;
  image-rendering: pixelated;
}
.stage-empty { max-width: 15rem; text-align: center; font-size: .9rem; color: var(--muted); }
.bc-stage { min-height: 200px; background: #fff; border: 1px solid var(--line); }
#bc-preview { max-width: 100%; height: auto; }
.bc-primary { display: flex; gap: .5rem; }
.bc-primary select { width: auto; flex-shrink: 0; }
#bc-dpi-note { margin-top: .4rem; }
#bc-dpi-note:empty { display: none; }
.bc-formats { display: flex; flex-wrap: wrap; align-items: center; gap: .4rem; margin-top: .75rem; }
.bc-formats .field-label { width: 100%; font-size: .8rem; color: var(--muted); }
.bc-formats select { font-size: .82rem; padding: .4rem .5rem; }
.scan-badge { padding: .55rem .8rem; border-radius: 10px; font-size: .86rem; font-weight: 600; word-break: break-all; }
.scan-badge:empty { display: none; }
.scan-badge.pass { background: var(--ok-soft); color: var(--ok); }
.scan-badge.fail { background: var(--bad-soft); color: var(--bad); }

/* ── Drop zones and big choices ────────────────────────── */
.choice-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 1rem; margin-top: 1rem; }
.drop-zone, .choice-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: .35rem;
  min-height: 170px;
  padding: 1.25rem 1rem;
  border: 2px dashed #bfdbfe;
  border-radius: 14px;
  background: #fbfdff;
  font: inherit;
  color: var(--ink);
  text-align: center;
  cursor: pointer;
  transition: border-color .15s, background .15s;
}
.choice-btn { border-style: solid; }
.drop-zone:hover, .drop-zone.drag-over, .choice-btn:hover { border-color: var(--brand); background: var(--brand-soft); }
#detect-drop { margin-top: 1rem; }
.dz-sample { margin-top: .4rem; font-size: .8rem; color: var(--muted); }
.dz-sample a { margin-left: .35rem; color: var(--brand); font-weight: 600; text-decoration: none; }
.dz-sample a:hover { text-decoration: underline; }
.paste-box { display: flex; flex-direction: column; gap: .5rem; }
.paste-box label { font-size: .9rem; color: var(--ink); }
.paste-box textarea { flex: 1; }
.paste-box .btn { align-self: flex-start; }

/* ── Batch results ─────────────────────────────────────── */
#spinner {
  display: none;
  align-items: center;
  justify-content: center;
  gap: .75rem;
  margin: 1.25rem 0;
  font-size: .95rem;
  font-weight: 600;
  color: var(--brand);
}
#spinner.visible { display: flex; }
.spin-ring {
  width: 22px; height: 22px;
  border: 3px solid #bfdbfe;
  border-top-color: var(--brand);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
#toolbar {
  display: none;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: .75rem;
  margin: 1.5rem 0 1rem;
}
#toolbar.visible { display: flex; }
.count-label { font-size: 1rem; font-weight: 700; color: var(--ink); }
.toolbar-actions { display: flex; flex-wrap: wrap; align-items: flex-start; gap: .5rem; }
.menu { position: relative; }
.menu summary { list-style: none; }
.menu summary::-webkit-details-marker { display: none; }
.menu-body {
  position: absolute;
  right: 0;
  z-index: 5;
  display: flex;
  flex-direction: column;
  gap: .5rem;
  width: 290px;
  margin-top: .4rem;
  padding: 1rem;
  border-radius: 12px;
  background: var(--surface);
  box-shadow: 0 8px 28px rgba(15, 39, 71, .2);
  font-size: .85rem;
}
#log-wrap { margin-bottom: 1rem; font-size: .85rem; }
#log-wrap summary { font-weight: 600; color: var(--muted); cursor: pointer; }
#log-area {
  margin-top: .5rem;
  max-height: 200px;
  overflow-y: auto;
  padding: .75rem 1rem;
  border-radius: 10px;
  background: var(--surface);
  font-size: .8rem;
  line-height: 1.7;
}
.log-ok { color: var(--ok); }
.log-err { color: var(--bad); }
.log-info { color: var(--muted); }
.log-warn { color: var(--warn); }
#results-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 1rem; }
.qr-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: .6rem;
  padding: .9rem;
  border-radius: 14px;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.qr-canvas-wrap { display: flex; align-items: center; justify-content: center; width: 140px; height: 140px; }
.qr-canvas-wrap canvas, .qr-canvas-wrap img { max-width: 140px; max-height: 140px; }
.qr-filename { max-width: 150px; font-size: .78rem; text-align: center; word-break: break-all; color: var(--muted); }

/* ── Scan ──────────────────────────────────────────────── */
.status-text { margin-top: 1rem; font-size: .9rem; color: var(--text); }
.status-text:empty { display: none; }
#scan-to-detect { margin-top: .5rem; }
.scan-row { margin-top: 1rem; padding: 1rem; border-radius: 12px; background: #f8fafc; }
.scan-kind { display: inline-block; padding: .05rem .55rem; border-radius: 999px; background: #dbeafe; color: var(--brand-dark); font-size: .72rem; font-weight: 700; }
.scan-value { margin-top: .4rem; font-size: .95rem; color: var(--ink); white-space: pre-wrap; word-break: break-all; }
.scan-detail { margin-top: .25rem; font-size: .82rem; color: var(--muted); }
.scan-actions { display: flex; flex-wrap: wrap; gap: .5rem; margin-top: .7rem; }

body.camera-open { overflow: hidden; }
#scan-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  flex-direction: column;
  background: #000;
}
.scan-view {
  position: relative;
  flex: 0 0 80%;
  min-height: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: max(1rem, env(safe-area-inset-top)) 1rem 1rem;
}
#scan-live {
  flex: 0 0 20%;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: .5rem .75rem max(.5rem, env(safe-area-inset-bottom));
  background: var(--surface);
}
.scan-live-empty { padding-top: .5rem; color: var(--muted); font-size: .9rem; text-align: center; }
#scan-results:not(:empty) + .scan-live-empty { display: none; }
#scan-live .scan-row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: .35rem .5rem;
  margin-top: .4rem;
  padding: .45rem .6rem;
}
#scan-live .scan-row:first-child { margin-top: 0; }
#scan-live .scan-value { margin-top: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
#scan-live .scan-detail { display: none; }
#scan-live .scan-actions { grid-column: 1 / -1; margin-top: 0; }
.scan-row.just-read { animation: scan-read 1.2s ease-out; }
@keyframes scan-read { from { background: var(--ok-soft); } }
#scan-video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.scan-frame {
  position: absolute;
  top: 50%;
  left: 50%;
  width: min(70vw, 45vh, 360px);
  aspect-ratio: 1;
  transform: translate(-50%, -50%);
  border: 3px solid #fff;
  border-radius: 20px;
  box-shadow: 0 0 0 100vmax rgba(0, 0, 0, .45);
  pointer-events: none;
}
.scan-overlay-hint {
  position: relative;
  padding: .4rem .9rem;
  border-radius: 999px;
  background: rgba(0, 0, 0, .55);
  color: #fff;
  font-size: .95rem;
  text-align: center;
}
.scan-close {
  position: relative;
  min-width: 11rem;
  min-height: 50px;
  background: #fff;
  color: var(--ink);
  font-size: 1rem;
}
.scan-close:hover { background: #e8eef6; }
/* Show the whole picture on wide screens, where cropping would cut off a lot. */
@media (min-width: 821px) {
  #scan-video { object-fit: contain; }
}

/* ── Check ─────────────────────────────────────────────── */
#detect-report { margin-top: 1.25rem; font-size: .9rem; }
.detect-verdict { display: flex; align-items: center; gap: 1rem; padding: 1rem; border-radius: 12px; background: #f8fafc; }
.detect-verdict strong { display: block; font-size: 1.1rem; color: var(--ink); }
.detect-grade {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 3.25rem; height: 3.25rem;
  border-radius: 12px;
  font-size: 1.6rem;
  font-weight: 800;
}
.grade-A { background: #dcfce7; color: #166534; }
.grade-B { background: #ecfccb; color: #3f6212; }
.grade-C { background: #fef9c3; color: #854d0e; }
.grade-D { background: #ffedd5; color: #9a3412; }
.grade-F { background: #fee2e2; color: #991b1b; }
.detect-tips { margin: .9rem 0 0 1.25rem; line-height: 1.7; }
.detect-layout { display: flex; flex-wrap: wrap; align-items: flex-start; gap: 1.5rem; margin-top: 1.25rem; }
.detect-side { flex: 1 1 300px; min-width: 0; }
.detect-table { width: 100%; border-collapse: collapse; font-size: .86rem; }
.detect-table th, .detect-table td { padding: .5rem .4rem; border-bottom: 1px solid #eef2f7; text-align: left; vertical-align: top; }
.detect-table th { font-weight: 600; white-space: nowrap; }
.detect-table td { word-break: break-all; }
.detect-table td.grade-chip { width: 2rem; border-radius: 6px; text-align: center; font-weight: 700; }
.detect-figure { flex: 0 1 auto; }
.detect-map { display: block; max-width: 100%; border: 1px solid var(--line); border-radius: 8px; }
.detect-figure figcaption { margin-top: .5rem; font-size: .76rem; color: var(--muted); }
.key { display: inline-block; width: .7rem; height: .7rem; margin-right: .25rem; border-radius: 2px; vertical-align: -1px; }
.key-data { background: #dc2626; }
.key-function { background: #f59e0b; margin-left: .5rem; }
.detect-note { margin-top: 1rem; font-size: .78rem; color: var(--muted); }

/* ── Footer ────────────────────────────────────────────── */
.app-footer {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: .4rem 1rem;
  margin-top: 2rem;
  font-size: .8rem;
  color: var(--muted);
}
.app-footer a { color: var(--muted); }

/* ── Responsive ────────────────────────────────────────── */
@media (max-width: 820px) {
  .split { grid-template-columns: minmax(0, 1fr); }
  .preview-card { position: static; }
  .qr-stage { min-height: 240px; }
}
@media (max-width: 520px) {
  .page-wrap { padding: 1rem .75rem 1.5rem; }
  .card { padding: 1.1rem; }
  .brand p { display: none; }
  .tabs button { flex-direction: column; gap: .1rem; min-height: 52px; font-size: .74rem; padding: .3rem .1rem; }
  .format-row { flex-direction: column; align-items: stretch; gap: .35rem; }
  .menu-body { right: auto; left: 0; width: min(290px, 80vw); }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
  .spin-ring { animation-duration: 2s; }
}
