/* Margin. Layout: library screen with a notebook margin rule; editor is a toolbar, a scrolling page tray and a floating tool dock. */
:root {
  --bg: #eceef2;
  --surface: #ffffff;
  --surface-2: #f2f4f7;
  --ink: #16181d;
  --muted: #596170;
  --line: #d8dce3;
  --accent: #0b6b5c;
  --accent-ink: #ffffff;
  --accent-soft: #d6ece6;
  --danger: #b3261e;
  --rule: #e08c8c;
  --shadow: 0 1px 2px rgba(20, 24, 32, .08), 0 8px 24px rgba(20, 24, 32, .12);
  --sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --serif: ui-serif, "New York", "Iowan Old Style", Georgia, serif;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --rule-x: 22px;
}
@media (min-width: 700px) { :root { --rule-x: 56px; } }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14161a; --surface: #1d2025; --surface-2: #262a31; --ink: #eceff4; --muted: #9aa3b2;
    --line: #2e333c; --accent: #46d1b3; --accent-ink: #062a23; --accent-soft: #17352f; --danger: #ff8a80;
    --rule: #7d4a4a;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #14161a; --surface: #1d2025; --surface-2: #262a31; --ink: #eceff4; --muted: #9aa3b2;
  --line: #2e333c; --accent: #46d1b3; --accent-ink: #062a23; --accent-soft: #17352f; --danger: #ff8a80;
  --rule: #7d4a4a;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--ink); font: 15px/1.4 var(--sans);
  overflow: hidden; overscroll-behavior: none; -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
#app { position: relative; height: 100%; }
.i { display: inline-grid; place-items: center; }
svg.i { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* ---------- library ---------- */
#library { position: absolute; inset: 0; overflow: auto; padding: calc(var(--safe-t) + 44px) 0 calc(var(--safe-b) + 56px); }
#library::before {
  content: ""; position: fixed; top: 0; bottom: 0; left: calc(var(--safe-l) + var(--rule-x)); width: 1px;
  background: var(--rule); opacity: .7; pointer-events: none;
}
.lib-inner { max-width: 960px; padding: 0 20px 0 calc(var(--safe-l) + var(--rule-x) + 26px); }
.wordmark { margin: 0; font: 600 clamp(44px, 8vw, 68px)/1 var(--serif); letter-spacing: -.025em; }
.tagline { margin: 12px 0 0; max-width: 38ch; color: var(--muted); font-size: 16px; }
.lib-actions { display: flex; flex-wrap: wrap; gap: 12px; margin: 28px 0 40px; }
.big {
  display: inline-flex; align-items: center; justify-content: center; gap: 10px; min-height: 52px; padding: 0 20px;
  border-radius: 12px; border: 1px solid var(--line); background: var(--surface); font-weight: 600;
}
.big.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.lib-sub { margin: 0 0 14px; font-size: 12px; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.recent { display: grid; grid-template-columns: repeat(auto-fill, minmax(148px, 1fr)); gap: 22px 18px; }
.card { position: relative; }
.card .open { display: block; width: 100%; padding: 0; border: 0; background: none; text-align: left; }
.thumb { aspect-ratio: 1 / 1.414; background: #fff; border-radius: 6px; box-shadow: var(--shadow); overflow: hidden; display: grid; place-items: center; }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.card .name { margin-top: 10px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .when { color: var(--muted); font-size: 13px; }
.card .rm {
  position: absolute; top: 6px; right: 6px; min-width: 32px; height: 32px; padding: 0 8px; border-radius: 16px;
  border: 1px solid var(--line); background: var(--surface); color: var(--muted); display: grid; place-items: center; font-size: 13px; font-weight: 600;
}
.card .rm.arm { background: var(--danger); color: #fff; border-color: transparent; }
.empty { max-width: 44ch; color: var(--muted); }

/* ---------- editor chrome ---------- */
#editor { position: absolute; inset: 0; display: flex; flex-direction: column; }
.bar {
  flex: none; display: flex; align-items: center; gap: 6px; z-index: 5;
  padding: calc(var(--safe-t) + 6px) max(10px, var(--safe-r)) 6px max(10px, var(--safe-l));
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.spacer { flex: 1 1 0; min-width: 0; }
.grp { display: flex; align-items: center; gap: 2px; }
.icon { width: 40px; height: 40px; display: grid; place-items: center; border: 0; border-radius: 10px; background: none; padding: 0; }
.icon:hover:not(:disabled) { background: var(--surface-2); }
.icon:disabled { opacity: .35; cursor: default; }
.docname {
  min-width: 0; flex: 0 1 300px; font: 600 15px var(--sans); color: inherit; background: transparent;
  border: 1px solid transparent; border-radius: 8px; padding: 7px 9px; text-overflow: ellipsis;
}
.docname:hover { border-color: var(--line); }
.pageind { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; padding: 0 8px; white-space: nowrap; }
.zoomlbl { min-width: 54px; height: 36px; border: 0; background: none; border-radius: 8px; font-size: 13px; font-variant-numeric: tabular-nums; color: var(--muted); }
.zoomlbl:hover { background: var(--surface-2); }
.export {
  display: inline-flex; align-items: center; gap: 8px; height: 40px; padding: 0 14px; margin-left: 4px;
  border: 0; border-radius: 10px; background: var(--accent); color: var(--accent-ink); font-weight: 600;
}
@media (max-width: 640px) { .zoomgrp { display: none; } .export span { display: none; } .export { padding: 0 11px; } }

/* ---------- page tray ---------- */
#viewer {
  flex: 1; min-height: 0; overflow: auto; position: relative; background: var(--bg);
  touch-action: pan-x pan-y; -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}
body.finger-draw #viewer .ink { touch-action: none; }
#pages { position: relative; display: flex; flex-direction: column; width: max-content; min-width: 100%; padding: 16px 16px calc(var(--safe-b) + 150px); }
.page { position: relative; flex: none; margin: 0 auto 14px; background: #fff; box-shadow: 0 1px 2px rgba(0, 0, 0, .18), 0 6px 22px rgba(0, 0, 0, .14); isolation: isolate; }
.page canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.page .pdf, .page .hl { pointer-events: none; }
.page .hl { opacity: .5; mix-blend-mode: multiply; }
.page .ink { cursor: crosshair; }
body[data-tool="text"] .page .ink { cursor: text; }
.annots { position: absolute; inset: 0; pointer-events: none; }
.addpage {
  align-self: center; display: inline-flex; align-items: center; gap: 8px; margin-top: 4px; padding: 12px 18px;
  border: 1.5px dashed var(--line); border-radius: 12px; background: none; color: var(--muted); font-weight: 600;
}
.addpage:hover { border-color: var(--accent); color: var(--accent); }

/* ---------- text boxes ---------- */
.tb { position: absolute; display: inline-grid; pointer-events: none; font-family: Helvetica, Arial, "Liberation Sans", sans-serif; line-height: 1.25; }
body[data-tool="text"] .tb { pointer-events: auto; }
.tb::before { content: ""; position: absolute; inset: -6px; }
.tb::after { content: attr(data-v) " "; grid-area: 1 / 1; white-space: pre; visibility: hidden; font: inherit; line-height: inherit; min-width: 3ch; }
.tb textarea {
  grid-area: 1 / 1; margin: 0; padding: 0; border: 0; background: transparent; color: inherit; caret-color: currentColor;
  font: inherit; line-height: inherit; white-space: pre; overflow: hidden; resize: none; outline: none; width: 100%; height: 100%; min-width: 3ch;
  -webkit-user-select: text; user-select: text;
}
.tb:focus-within { outline: 1.5px dashed var(--accent); outline-offset: 4px; }
.tb .tbbar { position: absolute; left: -2px; top: -44px; display: none; gap: 4px; font-size: 14px; }
body[data-tool="text"] .tb:focus-within .tbbar, body[data-tool="text"] .tb:hover .tbbar { display: flex; }
.tb .tbbar button {
  width: 34px; height: 34px; border-radius: 9px; border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  display: grid; place-items: center; padding: 0; box-shadow: var(--shadow);
}
.tb .tbbar .grip { touch-action: none; cursor: grab; }

/* ---------- dock ---------- */
.dock {
  position: absolute; left: 50%; transform: translateX(-50%); width: max-content; bottom: calc(var(--safe-b) + 14px); z-index: 6;
  display: flex; align-items: center; justify-content: center; flex-wrap: wrap; gap: 4px 6px; padding: 6px 8px;
  max-width: calc(100% - 24px); background: var(--surface); border: 1px solid var(--line); border-radius: 18px; box-shadow: var(--shadow);
}
.sep { width: 1px; height: 26px; background: var(--line); margin: 0 4px; }
.tool { width: 44px; height: 44px; border: 0; border-radius: 12px; background: none; display: grid; place-items: center; padding: 0; }
.tool:hover { background: var(--surface-2); }
.tool[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }
.tool.toggle { margin-left: 4px; }
.sw { width: 36px; height: 36px; border: 0; background: none; display: grid; place-items: center; padding: 0; }
.sw::before { content: ""; width: 24px; height: 24px; border-radius: 50%; background: var(--c); box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .22); }
.sw[aria-pressed="true"]::before { outline: 2px solid var(--accent); outline-offset: 3px; }
.sz { width: 38px; height: 38px; border: 0; border-radius: 10px; background: none; display: grid; place-items: center; padding: 0; color: var(--ink); }
.sz::before { content: ""; width: var(--d); height: var(--d); border-radius: 50%; background: currentColor; }
.sz[aria-pressed="true"] { background: var(--accent-soft); }

/* ---------- sheets, busy, toast ---------- */
.scrim { position: fixed; inset: 0; z-index: 20; display: grid; place-items: center; padding: 16px; background: rgba(10, 12, 16, .45); }
.sheet { width: min(460px, 100%); background: var(--surface); border-radius: 18px; padding: 22px; box-shadow: var(--shadow); }
.sheet h2 { margin: 0 0 6px; font: 600 22px/1.2 var(--serif); }
.sheet p { margin: 0 0 18px; color: var(--muted); overflow-wrap: anywhere; }
.sheet .row { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 8px; }
.ghost { margin-top: 10px; width: 100%; height: 44px; border: 0; background: none; color: var(--muted); font-weight: 600; border-radius: 10px; }
.ghost:hover { background: var(--surface-2); }
.papers { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.papers button { border: 1px solid var(--line); background: var(--surface-2); border-radius: 12px; padding: 10px 6px 8px; display: grid; justify-items: center; gap: 8px; font-weight: 600; font-size: 13px; }
.papers button:hover { border-color: var(--accent); }
.paper { display: block; width: 100%; max-width: 64px; aspect-ratio: 1 / 1.414; background-color: #fff; border-radius: 3px; box-shadow: 0 1px 3px rgba(0, 0, 0, .25); }
.paper.lined { background-image: repeating-linear-gradient(to bottom, transparent 0 7px, #b8c5db 7px 8px); background-position: 0 6px; }
.paper.grid { background-image: linear-gradient(#c3cee0 1px, transparent 1px), linear-gradient(90deg, #c3cee0 1px, transparent 1px); background-size: 8px 8px; }
.paper.dots { background-image: radial-gradient(#9fb0cc 1px, transparent 1.2px); background-size: 8px 8px; }
#busy { position: fixed; inset: 0; z-index: 30; display: grid; place-content: center; justify-items: center; gap: 12px; background: color-mix(in srgb, var(--bg) 72%, transparent); font-weight: 600; }
.spin { width: 30px; height: 30px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
#toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-b) + 92px); z-index: 40; max-width: calc(100% - 32px);
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-weight: 500;
  opacity: 0; transform: translate(-50%, 10px); pointer-events: none; transition: opacity .2s, transform .2s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
@media (prefers-reduced-motion: reduce) { .spin { animation-duration: 2.4s; } #toast { transition: none; } }

/* ---------- save and share sheet ---------- */
.exname { margin-bottom: 14px !important; font-size: 14px; }
.exlist { display: grid; gap: 10px; }
.exlist .big { width: 100%; justify-content: flex-start; text-align: left; height: auto; min-height: 52px; padding-top: 10px; padding-bottom: 10px; }
.exsub { margin: 18px 0 8px; font: 600 13px/1 var(--sans, system-ui); color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }
.apps { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.apps button { height: 46px; border: 1px solid var(--line); background: var(--surface-2); border-radius: 12px; font-weight: 600; color: inherit; }
.apps button:hover { border-color: var(--accent); }
.exnote { margin: 10px 0 0 !important; font-size: 13px; }

/* ---------- select original text ---------- */
.textLayer { position: absolute; inset: 0; overflow: hidden; line-height: 1; pointer-events: none; z-index: 3; }
.textLayer span, .textLayer br { color: transparent; position: absolute; white-space: pre; cursor: text; transform-origin: 0 0; }
body[data-tool="select"] .textLayer { pointer-events: auto; }
body[data-tool="select"] .textLayer ::selection { background: rgba(36, 120, 255, .35); }
body[data-tool="select"] .page .ink { cursor: default; }
.page .annots { z-index: 2; }
.selbar { position: fixed; z-index: 15; display: flex; gap: 2px; padding: 5px; border-radius: 14px; background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow); }
.selbar[hidden] { display: none; }
.selbar button { display: grid; justify-items: center; gap: 2px; min-width: 56px; height: 48px; padding: 4px 6px; border: 0; border-radius: 10px; background: none; color: inherit; font-size: 11px; font-weight: 600; }
.selbar button:hover { background: var(--surface-2); }
.selbar svg { width: 20px; height: 20px; }
.exflat { display: block; margin-top: 14px; font-weight: 600; font-size: 14px; }
.exflat small { display: block; font-weight: 400; color: var(--muted); margin: 2px 0 0 26px; }
.exflat input { margin-right: 8px; }
body[data-tool="select"] .textLayer, body[data-tool="select"] .textLayer span { -webkit-user-select: text; user-select: text; -webkit-touch-callout: default; }
body[data-tool="select"] #viewer { touch-action: pan-x pan-y; }

/* ---------- scanner ---------- */
#scan { position: fixed; inset: 0; z-index: 12; display: flex; flex-direction: column; background: var(--bg); }
#scan[hidden] { display: none; }
.sc-title { font: 600 17px/1 var(--serif); margin-left: 6px; }
.sc-pane { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.sc-pane[hidden] { display: none; }
#scReview { padding: 16px 16px calc(16px + env(safe-area-inset-bottom)); overflow: auto; }
.sc-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 18px 14px; align-content: start; }
.sc-card { position: relative; }
.sc-open { display: block; width: 100%; padding: 0; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, .15); }
.sc-open img { display: block; width: 100%; height: auto; }
.sc-num { position: absolute; left: 6px; top: 6px; min-width: 22px; height: 22px; display: grid; place-items: center; border-radius: 11px; background: rgba(20, 22, 26, .72); color: #fff; font-size: 12px; font-weight: 600; }
.sc-ctl { display: flex; justify-content: center; gap: 4px; margin-top: 6px; }
.sc-ctl button { width: 36px; height: 36px; border: 0; border-radius: 9px; background: var(--surface-2); color: inherit; }
.sc-ctl button:disabled { opacity: .35; }
.sc-ctl svg { width: 18px; height: 18px; }
#scReview .empty { margin: 18px 4px; }
#scReview .empty[hidden] { display: none; }
.sc-actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: auto; padding-top: 18px; position: sticky; bottom: 0; }
.sc-wrap { flex: 1; min-height: 0; display: grid; place-items: center; background: #1b1d22; }
.sc-stage { position: relative; touch-action: none; }
.sc-stage canvas { display: block; width: 100%; height: 100%; background: #fff; }
.sc-quad { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; overflow: visible; }
.sc-quad polygon { fill: rgba(70, 209, 179, .16); stroke: #46d1b3; stroke-width: 2; }
.hnd { position: absolute; width: 44px; height: 44px; margin: -22px 0 0 -22px; border-radius: 50%; touch-action: none; cursor: grab; }
.hnd::after { content: ""; position: absolute; inset: 11px; border-radius: 50%; background: #fff; border: 3px solid #0b6b5c; box-shadow: 0 1px 4px rgba(0, 0, 0, .5); }
.sc-tools { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 10px; padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); background: var(--surface); border-top: 1px solid var(--line); }
.sc-tools[hidden] { display: none; }
.sc-tools > button:not(.big), .sc-row > button:not(.big):not(.icon) { height: 44px; padding: 0 16px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface-2); color: inherit; font-weight: 600; }
.sc-chips { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; width: 100%; }
.sc-chips button { height: 38px; padding: 0 14px; border: 1px solid var(--line); border-radius: 19px; background: var(--surface-2); color: inherit; font-weight: 600; font-size: 14px; }
.sc-chips button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.sc-row { display: flex; align-items: center; justify-content: center; flex-wrap: wrap; gap: 10px; width: 100%; }
.sc-warn { position: absolute; right: 6px; top: 6px; padding: 3px 8px; border-radius: 11px; background: #e08a1e; color: #1c1200; font-size: 11px; font-weight: 700; z-index: 1; }

/* ---------- app lock ---------- */
html.locked #app { visibility: hidden; }
html.locked body { background: var(--bg); }
.lockscreen { position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; padding: 24px; background: var(--bg); visibility: visible; }
.lockscreen[hidden] { display: none; }
.lockbox { width: min(380px, 100%); text-align: center; display: grid; gap: 12px; justify-items: center; }
.lockbox h2 { margin: 0; font: 600 26px/1.2 var(--serif); }
.lockbox p { margin: 0 0 6px; color: var(--muted); }
.lockicon svg { width: 44px; height: 44px; }
.lockbox .big { width: 100%; }
.lockbox .ghost { font-size: 13px; }
.lockbtn { display: inline-flex; align-items: center; gap: 8px; margin: -18px 0 28px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 10px; background: none; color: var(--muted); font-size: 14px; font-weight: 600; }
.lockbtn svg { width: 18px; height: 18px; }
.lockbtn[aria-pressed="true"] { color: var(--accent); border-color: var(--accent); }
