*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg: #080f18; --bg2: #0b1622; --bg3: #0f1e2e; --bg4: #152a3f;
  --border: rgba(255,255,255,0.09); --border2: rgba(255,255,255,0.16);
  --accent: #e86100; --accent2: #ff7a30; --accent-soft: rgba(232,97,0,0.14);
  --navy: #1a3a5c;
  --text: #dde8f0; --text2: #8aa6be; --text3: #56738c;
  --danger: #ff6b5b; --ok: #3ecf8e;
  --font: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --font2: 'Space Grotesk', 'Inter', sans-serif;
  --r: 10px; --r-sm: 6px;
  --safe-t: env(safe-area-inset-top, 0px); --safe-b: env(safe-area-inset-bottom, 0px);
}
html, body { height: 100%; }
body {
  font-family: var(--font); background: var(--bg); color: var(--text);
  line-height: 1.5; -webkit-font-smoothing: antialiased; overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
a { color: var(--accent2); text-decoration: none; }
.muted { color: var(--text2); font-size: 13px; }
:focus-visible { outline: 2px solid var(--accent2); outline-offset: 2px; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 10px;
  min-height: 44px; padding: 0 18px; border-radius: var(--r-sm);
  font-weight: 600; font-size: 15px; transition: background .18s, border-color .18s, transform .1s;
  white-space: nowrap;
}
.btn:active { transform: scale(0.98); }
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent2); }
.btn.ghost { border: 1px solid var(--border2); background: var(--bg2); }
.btn.ghost:hover { border-color: var(--accent); }
.btn.text { color: var(--text2); padding: 0 8px; }
.btn.big { min-height: 56px; font-size: 16px; width: 100%; }
.btn.sm { min-height: 38px; padding: 0 14px; font-size: 14px; }
.btn:disabled { opacity: .5; pointer-events: none; }
.icon-btn {
  width: 44px; height: 44px; display: grid; place-items: center; border-radius: 50%;
  color: var(--text);
}
.icon-btn:hover { background: rgba(255,255,255,0.06); }
.chip {
  min-height: 38px; padding: 0 16px; border-radius: 999px; border: 1px solid var(--border2);
  background: var(--bg3); font-size: 14px; font-weight: 500;
}
.chip:hover { border-color: var(--accent); }
.link-btn { color: var(--accent2); font-size: 13px; font-weight: 500; padding: 6px 0; }

/* segmented control */
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 6px; padding: 5px; background: var(--bg2); border: 1px solid var(--border); border-radius: var(--r); }
.seg button { padding: 10px 12px; border-radius: var(--r-sm); text-align: left; color: var(--text2); transition: background .18s, color .18s; }
.seg button b { display: block; font-size: 15px; color: var(--text); font-weight: 600; }
.seg button small { font-size: 12px; }
.seg button[aria-checked="true"] { background: var(--accent-soft); box-shadow: inset 0 0 0 1px var(--accent); color: var(--text); }
.seg button[aria-checked="true"] b { color: var(--accent2); }
.seg.mini { padding: 3px; gap: 3px; border-radius: 999px; }
.seg.mini button { padding: 7px 14px; border-radius: 999px; text-align: center; font-size: 13px; font-weight: 600; }
.seg.mini button[aria-checked="true"] { background: var(--accent); color: #fff; box-shadow: none; }
.seg.wide { width: 100%; }

/* ---------- views ---------- */
.view.full { position: fixed; inset: 0; z-index: 10; display: flex; flex-direction: column; background: #000; }

/* ---------- home ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(12px + var(--safe-t)) 16px 12px; max-width: 1080px; margin: 0 auto;
}
.brand { display: flex; align-items: center; gap: 10px; color: var(--text); }
.brand img { border-radius: 8px; }
.brand-text { display: flex; flex-direction: column; line-height: 1.1; }
.brand-text strong { font-family: var(--font2); font-size: 19px; letter-spacing: -0.01em; }
.brand-text small { font-size: 11px; color: var(--text3); letter-spacing: .06em; text-transform: uppercase; }
.badge { font-size: 12px; font-weight: 600; color: var(--ok); border: 1px solid rgba(62,207,142,.35); padding: 4px 10px; border-radius: 999px; }

.home { max-width: 1080px; margin: 0 auto; padding: 0 16px calc(28px + var(--safe-b)); }
.hero {
  position: relative; padding: 28px 0 8px; display: grid; gap: 18px;
}
.hero h1 { font-family: var(--font2); font-size: clamp(30px, 8vw, 50px); line-height: 1.05; letter-spacing: -0.025em; }
.hero h1 span { color: var(--accent); }
.lede { color: var(--text2); font-size: 16px; max-width: 560px; }
.hero-actions { display: grid; gap: 10px; }
@media (min-width: 720px) {
  .hero { grid-template-columns: 1fr 1fr; column-gap: 40px; align-items: end; padding-top: 48px; }
  .hero h1, .lede { grid-column: 1; }
  .seg#modeSeg, .hero-actions { grid-column: 2; }
  .seg#modeSeg { grid-row: 1; align-self: end; }
  .hero-actions { grid-row: 2; }
}

.library { margin-top: 34px; }
.lib-head { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 12px; }
.lib-head h2 { font-family: var(--font2); font-size: 20px; }
.lib-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.doc-card {
  position: relative; background: var(--bg2); border: 1px solid var(--border); border-radius: var(--r);
  overflow: hidden; text-align: left; transition: border-color .18s;
}
.doc-card:hover { border-color: var(--border2); }
.doc-card .thumb { aspect-ratio: 3 / 4; background: var(--bg3); display: grid; place-items: center; overflow: hidden; }
.doc-card .thumb img { width: 100%; height: 100%; object-fit: contain; padding: 8px; }
.doc-card .meta { padding: 9px 10px 10px; }
.doc-card .meta b { display: block; font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-card .meta span { font-size: 12px; color: var(--text3); }
.doc-card .open { position: absolute; inset: 0; }
.doc-card .del {
  position: absolute; top: 6px; right: 6px; width: 34px; height: 34px; border-radius: 50%;
  background: rgba(8,15,24,.78); display: grid; place-items: center; color: var(--text2);
}
.doc-card .del svg { width: 17px; height: 17px; }
.doc-card .del:hover { color: var(--danger); }
.empty { color: var(--text3); font-size: 14px; padding: 26px 16px; border: 1px dashed var(--border2); border-radius: var(--r); text-align: center; }

.facts { list-style: none; display: grid; gap: 10px; margin-top: 34px; }
@media (min-width: 720px) { .facts { grid-template-columns: repeat(3, 1fr); } }
.facts li { background: var(--bg2); border: 1px solid var(--border); border-left: 3px solid var(--accent); border-radius: var(--r-sm); padding: 14px 16px; }
.facts b { display: block; font-size: 14px; margin-bottom: 2px; }
.facts span { font-size: 13px; color: var(--text2); }
.foot { display: flex; gap: 8px; justify-content: center; margin-top: 30px; font-size: 13px; color: var(--text3); }
.foot a { color: var(--text2); }

/* ---------- camera ---------- */
.camera video, .camera #camOverlay { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.camera .flash { position: absolute; inset: 0; background: #fff; opacity: 0; pointer-events: none; transition: opacity .25s; }
.camera .flash.on { opacity: .75; transition: none; }
.cam-top {
  position: relative; display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: calc(10px + var(--safe-t)) 12px 10px; background: linear-gradient(rgba(0,0,0,.6), transparent);
}
.cam-top .icon-btn { background: rgba(0,0,0,.35); }
.cam-top .icon-btn.on { color: #fff; background: var(--accent); }
.cam-top .seg { background: rgba(0,0,0,.45); border-color: rgba(255,255,255,.15); }
.cam-hint {
  position: relative; align-self: center; margin-top: 8px; padding: 6px 14px; border-radius: 999px;
  background: rgba(0,0,0,.5); font-size: 13px; font-weight: 500;
}
.cam-hint.good { color: #fff; background: rgba(232,97,0,.85); }
.cam-error {
  position: relative; margin: auto 24px; padding: 22px; border-radius: var(--r); background: var(--bg2);
  border: 1px solid var(--border2); display: grid; gap: 10px; text-align: center; max-width: 380px; align-self: center;
}
.cam-error p { color: var(--text2); font-size: 14px; margin-bottom: 4px; }
.cam-bottom {
  position: relative; margin-top: auto; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  padding: 18px 20px calc(22px + var(--safe-b)); background: linear-gradient(transparent, rgba(0,0,0,.7));
}
.cam-bottom > :first-child { justify-self: start; background: rgba(0,0,0,.35); }
.shutter { width: 76px; height: 76px; border-radius: 50%; border: 4px solid #fff; display: grid; place-items: center; }
.shutter span { width: 60px; height: 60px; border-radius: 50%; background: var(--accent); transition: transform .12s; }
.shutter:active span { transform: scale(.88); }
.shutter:disabled { opacity: .6; }
.cam-done, .cam-spacer { justify-self: end; }
.cam-done {
  position: relative; display: flex; align-items: center; gap: 8px; padding: 4px 14px 4px 4px;
  border-radius: 999px; background: rgba(0,0,0,.5); border: 1px solid rgba(255,255,255,.2); font-weight: 600; font-size: 14px;
}
.cam-done img { width: 42px; height: 42px; border-radius: 50%; object-fit: cover; background: var(--bg3); }
.cam-done .count {
  position: absolute; top: -4px; left: 30px; min-width: 22px; height: 22px; padding: 0 6px; border-radius: 999px;
  background: var(--accent); font-size: 12px; display: grid; place-items: center;
}

/* ---------- bars ---------- */
.bar {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: calc(8px + var(--safe-t)) 10px 8px; background: var(--bg); border-bottom: 1px solid var(--border);
}
.bar.bottom { padding: 12px 10px calc(12px + var(--safe-b)); border-top: 1px solid var(--border); border-bottom: 0; justify-content: center; }
.bar-title { font-size: 14px; font-weight: 600; color: var(--text2); }

/* ---------- crop ---------- */
.crop { background: var(--bg); }
.crop-stage { position: relative; flex: 1; min-height: 0; display: grid; place-items: center; padding: 26px; background: #05090f; touch-action: none; }
.crop-box { position: relative; }
.crop-box img { display: block; width: 100%; height: 100%; user-select: none; -webkit-user-drag: none; }
.crop-box svg { position: absolute; inset: -30px; width: calc(100% + 60px); height: calc(100% + 60px); overflow: visible; fill: none; stroke: none; }
.crop-box .shade { fill: rgba(0,0,0,.55); fill-rule: evenodd; }
.crop-box .edge { fill: rgba(232,97,0,.08); stroke: var(--accent); stroke-width: 2.5; }
.crop-box .handle { fill: rgba(232,97,0,.25); stroke: #fff; stroke-width: 2.5; cursor: grab; }
.crop-box .hit { fill: transparent; cursor: grab; }
.loupe { position: absolute; top: 12px; width: 110px; height: 110px; border-radius: 50%; border: 3px solid var(--accent); box-shadow: 0 6px 20px rgba(0,0,0,.6); pointer-events: none; background: #000; }

/* ---------- editor ---------- */
.edit { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }
.doc-name {
  flex: 1; min-width: 0; height: 40px; padding: 0 10px; border-radius: var(--r-sm);
  background: transparent; border: 1px solid transparent; color: var(--text); font: 600 16px var(--font);
}
.doc-name:hover, .doc-name:focus { border-color: var(--border2); background: var(--bg2); outline: none; }
.edit-body { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.preview { position: relative; flex: 1; min-height: 160px; background: #05090f; overflow: hidden; }
.preview img { position: absolute; inset: 14px; width: calc(100% - 28px); height: calc(100% - 28px); object-fit: contain; filter: drop-shadow(0 8px 22px rgba(0,0,0,.65)); }
.page-no { position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%); font-size: 12px; background: rgba(0,0,0,.6); padding: 3px 10px; border-radius: 999px; }
.busy { position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: 10px; background: rgba(5,9,15,.55); font-size: 13px; color: var(--text2); }
.spinner { width: 28px; height: 28px; border-radius: 50%; border: 3px solid rgba(255,255,255,.15); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.panel { padding: 10px 12px 4px; border-top: 1px solid var(--border); background: var(--bg); }
.filters { display: grid; grid-template-columns: repeat(5, 1fr); gap: 5px; }
.filters button { min-height: 38px; padding: 0 2px; border-radius: var(--r-sm); border: 1px solid var(--border2); background: var(--bg2); font-size: 13px; font-weight: 600; color: var(--text2); }
.filters button[aria-checked="true"] { border-color: var(--accent); color: var(--accent2); background: var(--accent-soft); }
.panel .link-btn { display: block; margin: 2px auto 0; }
.tools { display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px; margin-top: 2px; }
.tool { display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 8px 2px; border-radius: var(--r-sm); font-size: 12px; color: var(--text2); }
.tool:hover { background: var(--bg2); color: var(--text); }
.tool.danger:hover { color: var(--danger); }
.tool:disabled { opacity: .35; pointer-events: none; }

.strip-wrap { border-top: 1px solid var(--border); background: var(--bg2); padding: 8px 0 calc(8px + var(--safe-b)); }
.strip { display: flex; gap: 8px; overflow-x: auto; padding: 4px 12px 6px; scrollbar-width: thin; }
.strip button { position: relative; flex: none; width: 54px; height: 70px; border-radius: 4px; overflow: hidden; border: 2px solid transparent; background: var(--bg3); }
.strip button img { width: 100%; height: 100%; object-fit: cover; }
.strip button.active { border-color: var(--accent); }
.strip button span { position: absolute; bottom: 2px; right: 3px; font-size: 10px; font-weight: 700; background: rgba(0,0,0,.65); padding: 0 4px; border-radius: 3px; }
.strip button .mini-spin { position: absolute; inset: 0; margin: auto; width: 18px; height: 18px; border-width: 2px; }
.add-row { display: flex; gap: 8px; padding: 2px 12px 0; }
.add-row .btn { flex: 1; }

@media (min-width: 900px) {
  .edit-body { flex-direction: row; }
  .panel { width: 320px; border-top: 0; border-left: 1px solid var(--border); padding: 18px; display: flex; flex-direction: column; gap: 14px; }
  .filters { grid-template-columns: repeat(2, 1fr); }
  .filters button:first-child { grid-column: 1 / -1; }
  .filters button { min-height: 46px; }
  .tools { grid-template-columns: repeat(3, 1fr); }
  .add-row { justify-content: center; }
  .add-row .btn { flex: none; min-width: 160px; }
}

/* ---------- export sheet ---------- */
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 40; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 41; max-width: 520px; margin: 0 auto;
  background: var(--bg2); border: 1px solid var(--border2); border-bottom: 0; border-radius: 16px 16px 0 0;
  padding: 10px 18px calc(18px + var(--safe-b)); display: grid; gap: 8px;
  animation: up .22s cubic-bezier(.2,.8,.2,1);
}
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
.sheet-grip { width: 40px; height: 4px; border-radius: 2px; background: var(--border2); margin: 0 auto 6px; }
.sheet h3 { font-family: var(--font2); font-size: 20px; }
.field-label { font-size: 12px; font-weight: 600; color: var(--text3); text-transform: uppercase; letter-spacing: .06em; margin-top: 8px; }
.sheet .seg.mini { border-radius: var(--r-sm); }
.sheet .seg.mini button { border-radius: 4px; padding: 9px 6px; }
.hint { font-size: 13px; color: var(--text2); min-height: 20px; }
.sheet-actions { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 10px; margin-top: 6px; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(24px + var(--safe-b)); z-index: 60; transform: translate(-50%, 20px);
  max-width: calc(100vw - 32px); padding: 11px 16px; border-radius: var(--r-sm); background: #eef3f7; color: #0d1a2a;
  font-size: 14px; font-weight: 500; opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
  box-shadow: 0 8px 30px rgba(0,0,0,.45);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { background: var(--danger); color: #fff; }

.diag {
  position: fixed; left: 8px; right: 8px; top: calc(64px + var(--safe-t)); z-index: 55; pointer-events: none;
  margin: 0; padding: 6px 8px; border-radius: 6px; background: rgba(0,0,0,.72); color: #9ef0b8;
  font: 11px/1.4 ui-monospace, Consolas, monospace; white-space: pre-wrap;
}

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