/* ui.css — QuickScan. iOS-style, mobile-first (iPhone 8 / iOS 16 Safari).
   Light by default, follows the system dark mode. The crop screen is always
   dark so the photo stands out. */

:root {
  --bg: #f2f2f7;
  --surface: #ffffff;
  --surface-2: #f7f7fa;
  --line: rgba(60, 60, 67, 0.14);
  --text: #111318;
  --muted: #6b6f7a;
  --primary: #0a7cff;
  --primary-press: #0066d6;
  --primary-soft: rgba(10, 124, 255, 0.12);
  --on-primary: #ffffff;
  --success: #1fa463;
  --danger: #e5383b;
  --danger-soft: rgba(229, 56, 59, 0.1);
  --warn-bg: #fff4d6;
  --warn-text: #7a5300;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.06), 0 6px 20px rgba(0, 0, 0, 0.06);
  --radius: 14px;
  --radius-sm: 10px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --dark-bg: #0b0f17;
  --dark-surface: #161b26;
  --dark-text: #f1f3f7;
  --dark-muted: #a1a8b8;
  --handle: #0a7cff;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0f17;
    --surface: #161b26;
    --surface-2: #1d2331;
    --line: rgba(255, 255, 255, 0.1);
    --text: #f1f3f7;
    --muted: #a1a8b8;
    --primary: #3d95ff;
    --primary-press: #2b82ec;
    --primary-soft: rgba(61, 149, 255, 0.18);
    --danger: #ff5a5f;
    --danger-soft: rgba(255, 90, 95, 0.14);
    --warn-bg: #3a2e10;
    --warn-text: #ffd479;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

/* The UA rule [hidden]{display:none} loses to any class that sets `display`.
   Force it to win (also inlined in index.html for stale-cache safety). */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.4;
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
  -webkit-user-select: none;
  user-select: none;
}
input, textarea { -webkit-user-select: text; user-select: text; }
button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { cursor: default; }

/* ---------------- Icons ---------------- */
.ic {
  width: 22px; height: 22px; flex: none;
  fill: none; stroke: currentColor; stroke-width: 2;
  stroke-linecap: round; stroke-linejoin: round;
}
.ic-art {
  width: 96px; height: 96px;
  fill: none; stroke: var(--primary); stroke-width: 2.5;
  stroke-linecap: round; stroke-linejoin: round;
}
.ic-art .scanline { stroke: var(--success); animation: scan 2.4s ease-in-out infinite; }
@keyframes scan { 0%, 100% { transform: translateY(-22px); } 50% { transform: translateY(10px); } }

/* ---------------- Screens ---------------- */
.screen { display: none; min-height: 100vh; min-height: 100dvh; flex-direction: column; }
.screen.active { display: flex; animation: fade-in .18s ease-out; }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

.topbar {
  display: flex; align-items: center; gap: 8px;
  min-height: 52px;
  padding: calc(6px + var(--safe-top)) 8px 6px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(16px);
  backdrop-filter: saturate(180%) blur(16px);
  border-bottom: 0.5px solid var(--line);
  position: sticky; top: 0; z-index: 10;
}
.topbar .title {
  position: absolute; left: 50%; transform: translateX(-50%);
  margin: 0; font-size: 17px; font-weight: 600; white-space: nowrap;
  pointer-events: none;
}
.topbar-spacer { margin-left: auto; }

.topbar-home { padding-left: 16px; border-bottom: none; }
.home-title { margin-right: auto; }
.home-title h1 { margin: 0; font-size: 28px; font-weight: 800; letter-spacing: -0.4px; }
.home-title .sub { margin: 0; font-size: 14px; color: var(--muted); }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 44px; height: 44px; border: none; border-radius: 22px;
  background: transparent; color: var(--primary);
}
.icon-btn:active { background: var(--primary-soft); }

.text-btn {
  display: inline-flex; align-items: center; gap: 2px;
  min-height: 44px; padding: 0 10px; border: none; background: transparent;
  color: var(--primary); font-size: 17px; border-radius: var(--radius-sm);
}
.text-btn.strong { font-weight: 600; }
.text-btn.with-icon { padding-left: 2px; }
.text-btn:active { opacity: .55; }

.content {
  flex: 1; padding: 12px 16px calc(24px + var(--safe-bottom));
  overflow-y: auto; -webkit-overflow-scrolling: touch;
}

/* ---------------- Buttons ---------------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  appearance: none; border: none; border-radius: var(--radius);
  min-height: 48px; padding: 0 18px;
  font-size: 16px; font-weight: 600;
  background: var(--surface); color: var(--text);
  transition: transform .08s ease, background-color .15s ease, opacity .15s ease;
}
.btn:active:not(:disabled) { transform: scale(.98); }
.btn:disabled { opacity: .4; }
.btn.primary { background: var(--primary); color: var(--on-primary); }
.btn.primary:active:not(:disabled) { background: var(--primary-press); }
.btn.ghost { background: var(--primary-soft); color: var(--primary); }
.btn.danger-ghost { background: var(--danger-soft); color: var(--danger); }
.btn.large { min-height: 56px; font-size: 17px; }
.btn.small { min-height: 34px; padding: 0 12px; font-size: 14px; border-radius: 17px; }
.btn.pill { border-radius: 28px; min-height: 52px; padding: 0 22px; }
.btn.block { display: flex; width: 100%; }
.btn + .btn { margin-top: 10px; }

/* ---------------- Empty state ---------------- */
.empty {
  display: flex; flex-direction: column; align-items: center; text-align: center;
  max-width: 420px; margin: 4vh auto 0; padding: 0 8px;
}
.empty-art {
  width: 148px; height: 148px; border-radius: 40px;
  display: flex; align-items: center; justify-content: center;
  background: var(--surface); box-shadow: var(--shadow); margin-bottom: 22px;
}
.empty h2 { margin: 0 0 8px; font-size: 22px; font-weight: 700; }
.empty > p { margin: 0 0 24px; color: var(--muted); font-size: 15px; }
.steps {
  display: flex; gap: 18px; list-style: none; padding: 0; margin: 26px 0 0;
  color: var(--muted); font-size: 13px;
}
.steps li { display: flex; align-items: center; gap: 6px; }
.steps b {
  display: inline-flex; align-items: center; justify-content: center;
  width: 20px; height: 20px; border-radius: 10px;
  background: var(--primary-soft); color: var(--primary); font-size: 12px;
}

.inline-hint {
  margin: 0 0 12px; padding: 10px 12px; border-radius: var(--radius-sm);
  background: var(--primary-soft); color: var(--text); font-size: 14px;
}

/* ---------------- Pages grid ---------------- */
.pages-grid {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 14px;
  padding-bottom: 130px; /* room for the floating capture bar */
}
@media (min-width: 560px) { .pages-grid { grid-template-columns: repeat(3, 1fr); } }

.page-card {
  position: relative; border: none; padding: 0; text-align: left;
  background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: hidden;
  transition: transform .12s ease;
}
.page-card:active { transform: scale(.97); }
.page-thumb {
  width: 100%; aspect-ratio: 210 / 297; object-fit: cover; display: block;
  background: var(--surface-2);
}
.page-foot {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 10px; font-size: 13px; color: var(--muted);
}
.page-foot b { color: var(--text); font-weight: 600; }
.filter-tag {
  font-size: 11px; padding: 2px 7px; border-radius: 8px;
  background: var(--surface-2); color: var(--muted);
}

.reorder-ctrls {
  position: absolute; left: 0; right: 0; bottom: 38px;
  display: flex; justify-content: space-between; padding: 0 6px;
}
.reorder-ctrls button {
  width: 44px; height: 44px; border: none; border-radius: 22px;
  background: rgba(0, 0, 0, .62); color: #fff;
  display: inline-flex; align-items: center; justify-content: center;
}
.reorder-ctrls button:disabled { opacity: .25; }
.reordering .page-card { outline: 2px dashed var(--primary); outline-offset: -2px; }

/* ---------------- Floating capture bar ---------------- */
.capture-bar {
  position: fixed; left: 12px; right: 12px; bottom: calc(12px + var(--safe-bottom));
  z-index: 20;
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  padding: 10px 14px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(18px);
  backdrop-filter: saturate(180%) blur(18px);
  border-radius: 26px; box-shadow: 0 8px 30px rgba(0, 0, 0, .16);
}
.bar-btn {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  min-height: 56px; border: none; background: transparent; color: var(--text);
  font-size: 12px; font-weight: 600; border-radius: var(--radius);
}
.bar-btn:active { background: var(--surface-2); }
.bar-btn.accent { color: var(--primary); }
.badge-wrap { position: relative; display: inline-flex; }
.badge {
  position: absolute; top: -6px; right: -12px;
  min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px;
  background: var(--danger); color: #fff; font-size: 11px; line-height: 18px; text-align: center;
}

.shutter {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  border: none; background: transparent; padding: 0 10px;
}
.shutter-ring {
  width: 70px; height: 70px; border-radius: 35px; padding: 4px;
  border: 3px solid var(--primary);
  display: flex; align-items: center; justify-content: center;
  transition: transform .1s ease;
}
.shutter:active .shutter-ring { transform: scale(.92); }
.shutter-core {
  width: 100%; height: 100%; border-radius: 50%;
  background: var(--primary); color: #fff;
  display: flex; align-items: center; justify-content: center;
}
.shutter-core .ic { width: 26px; height: 26px; }
.shutter-label { font-size: 12px; font-weight: 600; color: var(--muted); }

/* ---------------- Crop (always dark) ---------------- */
.screen-dark { background: var(--dark-bg); color: var(--dark-text); }
.screen-dark .topbar {
  background: rgba(11, 15, 23, .85); border-bottom-color: rgba(255, 255, 255, .08);
}
.screen-dark .text-btn { color: #fff; }
.banner {
  margin: 10px 16px 0; padding: 10px 12px; border-radius: var(--radius-sm); font-size: 14px;
}
.banner.warn { background: #3a2e10; color: #ffd479; }
.crop-tip { margin: 10px 16px 0; text-align: center; font-size: 14px; color: var(--dark-muted); }
.crop-stage {
  position: relative; flex: 1; min-height: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 16px; overflow: hidden;
}
.crop-canvas, .crop-overlay {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  max-width: 100%;
}
.crop-canvas { border-radius: 4px; box-shadow: 0 10px 40px rgba(0, 0, 0, .5); }
.crop-overlay { touch-action: none; }
.crop-loupe {
  position: absolute; width: 120px; height: 120px; border-radius: 50%;
  pointer-events: none; z-index: 10; box-shadow: 0 4px 18px rgba(0, 0, 0, .6);
}

.tool-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 10px 16px calc(14px + var(--safe-bottom));
}
.screen-dark .tool-bar { background: var(--dark-bg); }
.tool-bar.flat { padding: 4px 0 0; justify-content: space-around; }
.tool-btn {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  min-width: 64px; min-height: 56px; padding: 4px 6px;
  border: none; background: transparent; border-radius: var(--radius-sm);
  font-size: 12px; font-weight: 500; color: inherit;
}
.tool-btn:active { background: rgba(127, 127, 127, .15); }
.tool-btn.danger { color: var(--danger); }
.screen-dark .tool-btn { color: var(--dark-text); }

/* ---------------- Page detail ---------------- */
.preview-wrap {
  position: relative; flex: 1; min-height: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 16px 52px;
}
.preview-img {
  max-width: 100%; max-height: calc(100dvh - 330px);
  border-radius: 4px; background: #fff; box-shadow: var(--shadow);
}
.nav-btn {
  position: absolute; top: 50%; transform: translateY(-50%);
  width: 40px; height: 40px; border: none; border-radius: 20px;
  background: var(--surface); color: var(--text); box-shadow: var(--shadow);
  display: inline-flex; align-items: center; justify-content: center;
}
.nav-btn.left { left: 8px; }
.nav-btn.right { right: 8px; }
.nav-btn:disabled { opacity: 0; pointer-events: none; }

.detail-panel {
  background: var(--surface); border-radius: 20px 20px 0 0;
  padding: 14px 16px calc(10px + var(--safe-bottom));
  box-shadow: 0 -4px 20px rgba(0, 0, 0, .06);
}

.segmented {
  display: flex; padding: 3px; border-radius: 12px; background: var(--surface-2);
  border: 0.5px solid var(--line);
}
.seg {
  flex: 1; position: relative; display: flex; align-items: center; justify-content: center;
  min-height: 40px; padding: 0 6px; border: none; border-radius: 9px;
  background: transparent; color: var(--text); font-size: 14px; font-weight: 600;
  text-align: center;
}
.seg.active, .seg:has(input:checked) {
  background: var(--surface); color: var(--primary); box-shadow: 0 1px 4px rgba(0, 0, 0, .12);
}
@media (prefers-color-scheme: dark) {
  .seg.active, .seg:has(input:checked) { background: #2a3142; }
}
.seg input { position: absolute; opacity: 0; pointer-events: none; }

.switch-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  min-height: 48px; font-size: 15px;
}
.switch-row small { display: block; font-size: 12px; color: var(--muted); }
.switch {
  appearance: none; -webkit-appearance: none; flex: none;
  width: 51px; height: 31px; border-radius: 16px; margin: 0;
  background: rgba(120, 120, 128, .32); position: relative;
  transition: background-color .2s ease;
}
.switch::after {
  content: ""; position: absolute; top: 2px; left: 2px;
  width: 27px; height: 27px; border-radius: 50%; background: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .2); transition: transform .2s ease;
}
.switch:checked { background: var(--success); }
.switch:checked::after { transform: translateX(20px); }

/* ---------------- Export ---------------- */
.file-card {
  display: flex; gap: 14px; align-items: center;
  padding: 14px; margin-bottom: 18px;
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
}
.file-thumb {
  width: 64px; aspect-ratio: 210 / 297; flex: none; border-radius: 4px; overflow: hidden;
  background: var(--surface-2); border: 0.5px solid var(--line);
}
.file-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.file-info { min-width: 0; }
.file-name { margin: 0; font-weight: 600; font-size: 15px; word-break: break-all; }
.file-meta { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.file-progress { margin: 4px 0 0; color: var(--primary); font-size: 13px; font-weight: 600; }
.file-progress.ok { color: var(--success); }

.tip { margin: 8px 4px 14px; color: var(--muted); font-size: 13px; }

.group { margin-top: 22px; }
.group-title {
  margin: 0 4px 8px; font-size: 13px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .4px; color: var(--muted);
}
.field { display: block; }
.field-label { display: block; margin: 0 4px 6px; font-size: 13px; color: var(--muted); }
.input {
  width: 100%; min-height: 48px; padding: 0 14px;
  border: 0.5px solid var(--line); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text); font-size: 16px; /* 16px stops iOS zoom */
}
.input:focus { outline: 2px solid var(--primary); outline-offset: -1px; }
.input.invalid { outline: 2px solid var(--danger); }
.field-error { margin: 6px 4px 0; color: var(--danger); font-size: 13px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0 12px; }
#btn-mail { margin-top: 12px; }
.chips:not([hidden]) + #btn-mail { margin-top: 0; }
.chip {
  min-height: 34px; padding: 0 12px; border-radius: 17px;
  border: 0.5px solid var(--line); background: var(--surface);
  font-size: 13px; color: var(--text); max-width: 100%;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chip.active { background: var(--primary-soft); color: var(--primary); border-color: transparent; }

/* ---------------- Settings sheet ---------------- */
.sheet { position: fixed; inset: 0; z-index: 40; visibility: hidden; }
.sheet.open { visibility: visible; }
.sheet-backdrop {
  position: absolute; inset: 0; background: rgba(0, 0, 0, .4);
  opacity: 0; transition: opacity .2s ease;
}
.sheet.open .sheet-backdrop { opacity: 1; }
.sheet-panel {
  position: absolute; left: 0; right: 0; bottom: 0;
  max-height: 90vh; max-height: 90dvh; overflow-y: auto;
  background: var(--bg); border-radius: 18px 18px 0 0;
  padding: 6px 16px calc(24px + var(--safe-bottom));
  transform: translateY(100%); transition: transform .25s cubic-bezier(.2, .8, .2, 1);
}
.sheet.open .sheet-panel { transform: translateY(0); }
.sheet-grabber { width: 36px; height: 5px; border-radius: 3px; background: var(--line); margin: 4px auto 6px; }
.sheet-head {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  position: sticky; top: -6px; background: var(--bg); z-index: 1; margin: 0 -8px;
}
.sheet-head h3 { margin: 0; font-size: 17px; font-weight: 600; }
.sheet-head .text-btn:last-child { justify-self: end; }
.sheet-panel .group { margin-top: 18px; }
.sheet-panel .segmented .seg { font-size: 13px; }

.range-row {
  display: grid; grid-template-columns: 1fr 1.4fr 36px; align-items: center; gap: 10px;
  min-height: 44px; font-size: 15px;
}
.range-row output { text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }
.range-row input { width: 100%; accent-color: var(--primary); }

/* ---------------- Busy / toast / update ---------------- */
.busy {
  position: fixed; inset: 0; z-index: 60;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0, 0, 0, .28);
}
.busy-card {
  display: flex; flex-direction: column; align-items: center; gap: 12px;
  min-width: 160px; padding: 22px 26px; border-radius: 18px;
  background: var(--surface); color: var(--text); box-shadow: 0 12px 40px rgba(0, 0, 0, .25);
}
.busy-card p { margin: 0; font-size: 15px; font-weight: 500; }
.spinner {
  width: 34px; height: 34px; border-radius: 50%;
  border: 3px solid var(--primary-soft); border-top-color: var(--primary);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed; left: 50%; bottom: calc(16px + var(--safe-bottom));
  transform: translateX(-50%) translateY(16px);
  display: flex; align-items: center; gap: 12px;
  max-width: calc(100vw - 32px); padding: 12px 16px; border-radius: 14px;
  background: rgba(28, 28, 32, .94); color: #fff; font-size: 14px;
  opacity: 0; pointer-events: none; transition: opacity .2s ease, transform .2s ease;
  z-index: 70; box-shadow: 0 8px 24px rgba(0, 0, 0, .25);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); pointer-events: auto; }
#toast-msg, .toast-action { white-space: nowrap; }
.toast { width: max-content; }
/* Sit above the floating capture bar only when that bar is on screen. */
body:has(#screen-pages.active #pages-bar:not([hidden])) .toast { bottom: calc(128px + var(--safe-bottom)); }
/* Above the bottom tool bars on Crop / detail screens. */
body:has(#screen-crop.active) .toast,
body:has(#screen-preview.active) .toast { bottom: calc(96px + var(--safe-bottom)); }
.toast-action {
  border: none; background: transparent; color: #6cb4ff;
  font-weight: 700; font-size: 14px; min-height: 32px; padding: 0 4px;
}

.update-banner {
  position: fixed; left: 12px; right: 12px; top: calc(10px + var(--safe-top));
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 10px 14px; border-radius: 14px;
  background: var(--surface); color: var(--text); box-shadow: var(--shadow);
  z-index: 55;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
