:root {
  --ink: #121212;
  --ink-2: #3a3833;
  --muted: #6d6a62;
  --paper: #f6f5f1;
  --card: #ffffff;
  --line: #dedbd2;
  --gold: #b8880e;
  --gold-ink: #8a6608;
  --gold-soft: #f4ead2;
  --danger: #a3312a;
  --select: #1f6feb;
  --bar-h: 56px;
  color-scheme: light;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--paper); color: var(--ink);
  font: 15px/1.45 'Questrial', 'Segoe UI', system-ui, -apple-system, sans-serif;
  display: flex; flex-direction: column; overflow: hidden;
}
button, input, select, label { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* top bar */
.bar {
  height: var(--bar-h); flex: none; display: flex; align-items: center; gap: 12px;
  padding: 0 16px; padding-top: env(safe-area-inset-top, 0px);
  background: var(--ink); color: #fff;
}
.brand { display: flex; align-items: center; gap: 10px; color: #fff; text-decoration: none; }
.brand-mark { width: 30px; height: 30px; border: 1px solid var(--gold); color: var(--gold); display: grid; place-items: center; font: 13px 'Marcellus', Georgia, serif; letter-spacing: .04em; }
.brand-name { font: 18px 'Marcellus', Georgia, serif; letter-spacing: .02em; }
.brand-sub { color: #bdb8ab; font-size: 13px; letter-spacing: .12em; text-transform: uppercase; border-left: 1px solid #3c3a35; padding-left: 12px; }
.bar-actions { margin-left: auto; display: flex; gap: 8px; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; height: 36px; padding: 0 14px; border-radius: 4px; border: 1px solid transparent; cursor: pointer; white-space: nowrap; font-size: 14px; }
.btn.gold { background: var(--gold); color: #fff; border-color: var(--gold); }
.btn.gold:hover { background: var(--gold-ink); }
.btn.gold:disabled { opacity: .45; cursor: default; }
.btn.ghost { background: transparent; border-color: currentColor; }
.bar .btn.ghost { color: #fff; border-color: #5a574f; }
.bar .btn.ghost:hover { border-color: var(--gold); }
.btn.big { height: 46px; padding: 0 26px; font-size: 16px; }

/* tool bar */
.tools {
  flex: none; display: flex; flex-wrap: nowrap; gap: 10px; align-items: center;
  padding: 8px 12px; background: var(--card); border-bottom: 1px solid var(--line);
  overflow-x: auto; scrollbar-width: thin;
}
.tool-group { display: flex; gap: 4px; align-items: center; padding-right: 10px; border-right: 1px solid var(--line); flex: none; }
.tool-group:last-child { border-right: 0; }
.tool { display: inline-flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px; min-width: 58px; height: 46px; padding: 2px 6px; border: 1px solid transparent; border-radius: 4px; background: transparent; cursor: pointer; font-size: 11.5px; color: var(--ink-2); }
.tool .ico { font-size: 17px; line-height: 1; color: var(--ink); }
.tool:hover { background: var(--paper); }
.tool.on { border-color: var(--gold); background: var(--gold-soft); color: var(--ink); }
.tool.small { min-width: 34px; height: 34px; font-size: 16px; }
.tool:disabled { opacity: .35; cursor: default; background: transparent; }
.tool[aria-pressed="true"] { border-color: var(--gold); background: var(--gold-soft); }
.prop { display: inline-flex; flex-direction: column; font-size: 11px; color: var(--muted); gap: 1px; }
.prop input[type=number] { width: 56px; height: 28px; border: 1px solid var(--line); border-radius: 4px; padding: 0 6px; background: #fff; }
.prop select { height: 28px; border: 1px solid var(--line); border-radius: 4px; background: #fff; padding: 0 4px; }
.prop.color input { width: 34px; height: 34px; padding: 0; border: 1px solid var(--line); border-radius: 4px; background: #fff; cursor: pointer; }
.zoom { min-width: 46px; text-align: center; font-size: 13px; color: var(--ink-2); font-variant-numeric: tabular-nums; }

/* work area */
.work { flex: 1; min-height: 0; display: flex; }
.thumbs { width: 168px; flex: none; background: var(--card); border-right: 1px solid var(--line); overflow-y: auto; }
.thumbs-head { position: sticky; top: 0; background: var(--card); display: flex; justify-content: space-between; align-items: center; padding: 10px 12px; font-size: 12px; letter-spacing: .1em; text-transform: uppercase; color: var(--muted); border-bottom: 1px solid var(--line); z-index: 1; }
.link { color: var(--gold-ink); cursor: pointer; text-transform: none; letter-spacing: 0; font-size: 13px; }
.link:hover { text-decoration: underline; }
#thumb-list { list-style: none; margin: 0; padding: 10px; display: flex; flex-direction: column; gap: 12px; }
.thumb { position: relative; border: 2px solid transparent; border-radius: 4px; padding: 4px; cursor: pointer; background: var(--paper); }
.thumb.current { border-color: var(--gold); }
.thumb.dragging { opacity: .4; }
.thumb.drop-before { box-shadow: 0 -3px 0 var(--gold); }
.thumb canvas { display: block; width: 100%; height: auto; background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,.15); }
.thumb .num { display: block; text-align: center; font-size: 12px; color: var(--muted); margin-top: 3px; }
.thumb .acts { position: absolute; top: 6px; right: 6px; display: none; gap: 2px; }
.thumb:hover .acts, .thumb.current .acts { display: flex; }
.thumb .acts button { width: 24px; height: 24px; border: 1px solid var(--line); border-radius: 3px; background: #fff; cursor: pointer; font-size: 13px; padding: 0; line-height: 1; }
.thumb .acts button:hover { border-color: var(--gold); }

.viewer { flex: 1; min-width: 0; overflow: auto; position: relative; }
.pages { display: flex; flex-direction: column; align-items: center; gap: 22px; padding: 24px 16px 80px; }
.page-wrap { position: relative; flex: none; }
.page { position: absolute; left: 0; top: 0; transform-origin: 0 0; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.18), 0 6px 22px rgba(0,0,0,.08); }
.page canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.page:not(.ready)::before { content: 'Loading page…'; position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); font-size: 14px; letter-spacing: .04em; }
.layer { position: absolute; inset: 0; }
.page-label { position: absolute; left: 0; top: -18px; font-size: 11px; color: var(--muted); letter-spacing: .08em; }

/* tool cursors */
.viewer[data-tool="text"] .layer { cursor: text; }
.viewer[data-tool="whiteout"] .layer, .viewer[data-tool="highlight"] .layer { cursor: crosshair; }
.viewer[data-tool="draw"] .layer { cursor: crosshair; touch-action: none; }
.viewer[data-tool="place"] .layer { cursor: copy; }

/* existing text spots in Edit text mode */
.spot { position: absolute; border-radius: 2px; cursor: text; }
.viewer[data-tool="edit"] .layer { cursor: text; }
.viewer[data-tool="edit"] .spot:hover { background: rgba(244,234,210,.45); outline: 1px solid rgba(184,136,14,.6); }
.viewer:not([data-tool="edit"]) .spot { pointer-events: none; }

/* objects */
.obj { position: absolute; }
.obj.sel { outline: 1.5px solid var(--select); outline-offset: 1px; }
.obj .handle { position: absolute; right: -6px; bottom: -6px; width: 12px; height: 12px; background: #fff; border: 1.5px solid var(--select); border-radius: 2px; cursor: nwse-resize; display: none; touch-action: none; }
.obj.sel .handle { display: block; }
.obj.text { white-space: pre; line-height: 1.15; padding: 0 1px; min-width: 6px; outline-offset: 1px; }
.obj.replace { white-space: pre; padding: 0; min-width: 6px; outline-offset: 1px; }
.obj.text:focus, .obj.replace:focus { outline: 1.5px solid var(--gold); }
.viewer[data-tool="select"] .obj { cursor: move; }
.viewer[data-tool="select"] .obj.text:focus, .viewer[data-tool="select"] .obj.replace:focus { cursor: text; }
.obj.whiteout { background: #fff; outline: 1px dashed #cfcac0; }
.obj.highlight { background: rgba(255, 221, 51, .42); mix-blend-mode: multiply; }
.obj.image img { width: 100%; height: 100%; display: block; pointer-events: none; user-select: none; }
.obj.ink svg { position: absolute; inset: 0; overflow: visible; pointer-events: none; }
.rubber { position: absolute; border: 1.5px dashed var(--gold); background: rgba(184,136,14,.08); pointer-events: none; }

/* empty state */
.empty { position: absolute; inset: 0; display: grid; place-items: center; padding: 16px; }
.drop { max-width: 520px; width: 100%; text-align: center; background: var(--card); border: 1.5px dashed var(--line); border-radius: 8px; padding: 40px 28px; }
.drop.over { border-color: var(--gold); background: var(--gold-soft); }
.drop h1 { font: 34px/1.15 'Marcellus', Georgia, serif; margin: 0 0 10px; text-wrap: balance; }
.drop p { color: var(--ink-2); margin: 0 auto 22px; max-width: 42ch; }
.drop .hint { color: var(--muted); font-size: 13px; margin: 10px 0 18px; }
.drop .private { font-size: 13px; color: var(--gold-ink); margin: 0; }

/* toast + dialogs */
.toast { position: fixed; left: 50%; bottom: calc(22px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%); background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 4px; font-size: 14px; max-width: calc(100% - 32px); box-shadow: 0 6px 20px rgba(0,0,0,.25); z-index: 50; }
.dialog { border: 0; border-radius: 8px; padding: 22px; width: min(640px, calc(100% - 32px)); box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.dialog::backdrop { background: rgba(18,18,18,.45); }
.dialog h2 { font: 24px 'Marcellus', Georgia, serif; margin: 0 0 6px; }
.muted { color: var(--muted); margin: 0 0 12px; font-size: 14px; }
#sign-pad { width: 100%; height: auto; aspect-ratio: 600 / 220; border: 1px solid var(--line); border-radius: 4px; background: #fff; touch-action: none; cursor: crosshair; display: block; }
.row { display: flex; gap: 8px; align-items: center; margin-top: 16px; }
.grow { flex: 1; }
.dialog .btn.ghost { color: var(--ink); border-color: var(--line); }
.check { display: flex; gap: 10px; align-items: flex-start; margin: 12px 0; font-size: 14px; color: var(--ink-2); }
.check input { margin-top: 4px; accent-color: var(--gold); }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); margin-top: 10px; }
.field input { height: 36px; border: 1px solid var(--line); border-radius: 4px; padding: 0 10px; color: var(--ink); font-size: 15px; }

/* phone */
@media (max-width: 760px) {
  .brand-name, .brand-sub { display: none; }
  .thumbs { display: none; }
  .tool { min-width: 50px; }
  .pages { padding: 18px 8px 80px; gap: 16px; }
  .drop { padding: 28px 18px; }
  .drop h1 { font-size: 28px; }
}

/* lock page */
.drop.lock { border-style: solid; }
.lock form, .lock #unlock, .lock #add { display: flex; flex-direction: column; align-items: center; gap: 14px; }
.lock form[hidden], .lock #unlock[hidden], .lock #add[hidden] { display: none; }
.lock .field { width: min(280px, 100%); text-align: left; margin: 0; }
.lock .field input { text-transform: uppercase; letter-spacing: .12em; font-variant-numeric: tabular-nums; }
.lock-msg { min-height: 1.4em; margin: 14px 0 0; color: var(--danger); font-size: 14px; }
