:root {
  --ink: #14213d;
  --ink-2: #22335a;
  --paper: #f5f7fa;
  --line: #d9dee6;
  --graphite: #5b6472;
  --marker: #ffe14d;
  --danger: #c8412d;
  --radius-s: 8px;
  --radius-l: 18px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.45;
  background: var(--ink);
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
[hidden] { display: none !important; }
button, input, a.btn { font: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 3px solid var(--marker); outline-offset: 2px; }
svg { fill: currentColor; }

.app {
  height: 100%;
  display: grid;
  grid-template-rows: auto 1fr auto;
  padding-top: env(safe-area-inset-top, 0px);
}

/* Header */
.top {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; min-height: 52px; color: #fff;
}
.top h1 { margin: 0; font-size: 20px; font-weight: 650; letter-spacing: 0.01em; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%; border: 0;
  background: var(--ink-2); color: #fff; display: grid; place-items: center;
}
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn[aria-pressed="true"] { background: var(--marker); color: var(--ink); }

main { position: relative; overflow: hidden; background: #000; }
.view { position: absolute; inset: 0; }

/* Camera */
.cam video, .cam canvas#overlay {
  position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover;
}
.cam canvas#overlay { pointer-events: none; }
.reticle {
  position: absolute; left: 50%; top: 42%;
  width: min(78vw, 360px); aspect-ratio: 16 / 10;
  transform: translate(-50%, -50%);
  border-radius: 14px;
  box-shadow: 0 0 0 100vmax rgba(10, 16, 32, 0.45);
  outline: 3px solid rgba(255, 255, 255, 0.9);
  transition: outline-color 0.15s;
}
.reticle.hit { outline-color: var(--marker); }
.cam-hint {
  position: absolute; left: 0; right: 0; top: calc(42% + min(24vw, 112px) + 18px);
  margin: 0; text-align: center; color: #fff; font-size: 15px;
  text-shadow: 0 1px 3px rgba(0,0,0,0.6);
}
.mode-doc .reticle { display: none; }
.mode-doc .cam-hint { top: auto; bottom: 120px; }
.cam-error {
  position: absolute; inset: 0; display: grid; place-content: center; gap: 16px;
  padding: 24px; text-align: center; color: #fff; background: var(--ink);
}
.cam-error p { margin: 0; max-width: 32ch; }

.cam-bar {
  position: absolute; left: 0; right: 0; bottom: 0;
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  padding: 16px 24px 20px;
  background: linear-gradient(to top, rgba(10,16,32,0.75), rgba(10,16,32,0));
}
.round-btn {
  justify-self: start; width: 52px; height: 52px; border-radius: 50%;
  background: rgba(255,255,255,0.16); color: #fff; display: grid; place-items: center;
  cursor: pointer;
}
.round-btn svg { width: 26px; height: 26px; }
.shutter {
  width: 74px; height: 74px; border-radius: 50%;
  border: 5px solid #fff; background: var(--marker); padding: 0;
  box-shadow: 0 0 0 3px rgba(0,0,0,0.25);
  transition: transform 0.1s;
}
.shutter:active { transform: scale(0.92); }
.pages-btn {
  justify-self: end; min-width: 52px; height: 52px; border-radius: var(--radius-s);
  border: 2px solid #fff; background: rgba(255,255,255,0.12); color: #fff;
  font-weight: 700; font-size: 18px; padding: 0 12px;
}
.pages-btn.has { background: #fff; color: var(--ink); }
.mode-barcode .shutter, .mode-barcode .pages-btn { visibility: hidden; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 46px; padding: 0 18px; border-radius: var(--radius-s);
  border: 1.5px solid var(--line); background: #fff; color: var(--ink);
  font-weight: 600; text-decoration: none; white-space: nowrap;
}
.btn.primary { background: var(--ink); border-color: var(--ink); color: #fff; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); }
.btn.wide { width: 100%; }
.btn:disabled { opacity: 0.45; cursor: default; }
.row { display: flex; gap: 10px; }
.row.wrap { flex-wrap: wrap; }
.row > .btn { flex: 1; }

/* Edit */
.edit { background: #0b1224; display: grid; grid-template-rows: 1fr auto; }
.edit-stage { position: relative; overflow: hidden; touch-action: none; }
.edit-stage canvas, .edit-svg { position: absolute; }
.edit-svg { overflow: visible; }
.edit-svg polygon { fill: rgba(255, 225, 77, 0.12); stroke: var(--marker); stroke-width: 2.5; }
.edit-svg circle.h { fill: rgba(255, 225, 77, 0.25); stroke: var(--marker); stroke-width: 3; cursor: grab; }
.edit-bar {
  background: var(--paper); padding: 14px 16px calc(14px + 4px);
  display: grid; gap: 12px; border-radius: var(--radius-l) var(--radius-l) 0 0;
}
.seg { display: grid; grid-template-columns: repeat(3, 1fr); background: #e7ebf1; border-radius: var(--radius-s); padding: 3px; }
.seg button { border: 0; background: transparent; min-height: 40px; border-radius: 6px; color: var(--graphite); font-weight: 600; }
.seg button[aria-checked="true"] { background: #fff; color: var(--ink); box-shadow: 0 1px 2px rgba(20,33,61,0.15); }

/* Pages */
.pages, .history { background: var(--paper); overflow-y: auto; display: flex; flex-direction: column; }
.pages-head { padding: 16px 16px 4px; }
.field { display: grid; gap: 6px; font-size: 14px; color: var(--graphite); }
.field input {
  min-height: 46px; padding: 0 12px; border-radius: var(--radius-s);
  border: 1.5px solid var(--line); background: #fff; color: var(--ink); font-size: 16px;
}
.page-list {
  list-style: none; margin: 0; padding: 12px 16px;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 14px;
  flex: 1; align-content: start;
}
.page-item { position: relative; background: #fff; border: 1px solid var(--line); border-radius: var(--radius-s); padding: 8px; }
.page-item canvas { width: 100%; height: auto; display: block; border-radius: 3px; background: #fff; }
.page-no { position: absolute; top: 12px; left: 12px; background: var(--ink); color: #fff; font-size: 13px; font-weight: 700; border-radius: 4px; padding: 1px 7px; }
.page-tools { display: flex; gap: 6px; margin-top: 8px; }
.page-tools button { flex: 1; min-height: 38px; border-radius: 6px; border: 1px solid var(--line); background: var(--paper); color: var(--ink); font-size: 14px; }
.page-tools button.del { color: var(--danger); }
.pages-bar, .history-bar {
  position: sticky; bottom: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 10px;
  padding: 12px 16px 16px; background: var(--paper); border-top: 1px solid var(--line);
}
.pages-bar .btn.primary { grid-column: 1 / -1; }

/* History */
.history-list { list-style: none; margin: 0; padding: 8px 0; flex: 1; }
.history-list li { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.h-text { font-size: 17px; font-weight: 600; word-break: break-all; background: none; border: 0; padding: 0; text-align: left; color: var(--ink); }
.h-meta { font-size: 13px; color: var(--graphite); }
.h-del { grid-row: 1 / span 2; grid-column: 2; align-self: center; border: 0; background: none; color: var(--graphite); width: 40px; height: 40px; border-radius: 50%; }
.h-del svg { width: 20px; height: 20px; }
.privacy { margin: 0; padding: 4px 16px 20px; font-size: 13px; color: var(--graphite); background: var(--paper); }
.empty { margin: 0; padding: 40px 24px; text-align: center; color: var(--graphite); }

/* Tabs */
.tabs {
  display: grid; grid-template-columns: repeat(3, 1fr);
  background: var(--ink); padding-bottom: env(safe-area-inset-bottom, 0px);
}
.tabs button {
  border: 0; background: none; color: #9aa6bf; min-height: 62px;
  display: grid; place-items: center; gap: 2px; font-size: 12px; font-weight: 600; padding: 6px 0;
}
.tabs svg { width: 24px; height: 24px; }
.tabs button[aria-current="page"] { color: #fff; }
.tabs button[aria-current="page"] svg { color: var(--marker); }

/* Sheet */
.sheet { position: fixed; inset: 0; background: rgba(10,16,32,0.35); display: flex; align-items: flex-end; z-index: 10; }
.sheet-card {
  width: 100%; background: #fff; border-radius: var(--radius-l) var(--radius-l) 0 0;
  padding: 18px 16px calc(16px + env(safe-area-inset-bottom, 0px)); display: grid; gap: 14px;
  max-height: 80%; overflow-y: auto;
}
.sheet-format { margin: 0; font-size: 14px; color: var(--graphite); }
.sheet-text {
  margin: 0; font-size: 22px; font-weight: 650; word-break: break-all;
  border-left: 5px solid var(--marker); padding-left: 12px;
}

/* Busy + toast */
.busy { position: fixed; inset: 0; background: rgba(10,16,32,0.6); display: grid; place-content: center; justify-items: center; gap: 14px; color: #fff; z-index: 20; }
.busy p { margin: 0; }
.spinner { width: 44px; height: 44px; border-radius: 50%; border: 4px solid rgba(255,255,255,0.25); border-top-color: var(--marker); animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.toast {
  position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom, 0px)); transform: translate(-50%, 20px);
  background: var(--ink); color: #fff; padding: 10px 16px; border-radius: var(--radius-s);
  font-size: 15px; opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s; z-index: 30;
  max-width: calc(100% - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation-duration: 0.01ms !important; }
}
@media (min-width: 720px) {
  .edit-bar, .sheet-card { max-width: 560px; margin: 0 auto; width: 100%; }
  .sheet { justify-content: center; }
}
