/* Layout: a calm grey desk with white paper in the middle; chrome in three thin bands
   (file, tools, context) above it and the page rail at the left. Blue pencil accent. */
:root {
  --desk: #e7eaf0;
  --bg: #f5f6f9;
  --surface: #ffffff;
  --raise: #edf0f5;
  --line: #d5dae3;
  --fg: #161a23;
  --muted: #5a6373;
  --accent: #2446d6;
  --accent-ink: #ffffff;
  --accent-soft: #e3e8fd;
  --danger: #c2321d;
  --danger-soft: #fbe5e1;
  --shadow: 0 1px 2px rgba(20, 28, 45, .08), 0 10px 26px rgba(20, 28, 45, .10);
  --paper: #ffffff;
  --mark: #2446d6;
  --mark-soft: rgba(36, 70, 214, .09);
  --f-ui: "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --f-mono: "IBM Plex Mono", ui-monospace, Menlo, Consolas, monospace;
  --f-display: "Source Serif 4", Georgia, "Times New Roman", serif;
  --r: 6px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --desk: #0e1014; --bg: #14171d; --surface: #1a1e26; --raise: #232833; --line: #2c323e;
    --fg: #e7eaf0; --muted: #9aa3b3; --accent: #8ba0ff; --accent-ink: #0a0f2e; --accent-soft: #222a4a;
    --danger: #ff8b76; --danger-soft: #3a1e19;
    --shadow: 0 1px 2px rgba(0, 0, 0, .45), 0 12px 30px rgba(0, 0, 0, .45);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --desk: #0e1014; --bg: #14171d; --surface: #1a1e26; --raise: #232833; --line: #2c323e;
  --fg: #e7eaf0; --muted: #9aa3b3; --accent: #8ba0ff; --accent-ink: #0a0f2e; --accent-soft: #222a4a;
  --danger: #ff8b76; --danger-soft: #3a1e19;
  --shadow: 0 1px 2px rgba(0, 0, 0, .45), 0 12px 30px rgba(0, 0, 0, .45);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 14px/1.45 var(--f-ui);
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; color: inherit; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.mono { font-family: var(--f-mono); font-variant-numeric: tabular-nums; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.app {
  height: 100%;
  display: grid;
  grid-template-rows: auto auto auto minmax(0, 1fr) auto;
  min-width: 0;
}

/* ---------- top band ---------- */
.top {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding-block: 10px;
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 600; letter-spacing: -.01em; white-space: nowrap; }
.brand-mark { width: 22px; height: 22px; fill: none; stroke: var(--fg); stroke-width: 1.5; stroke-linejoin: round; }
.brand-mark .brand-line { stroke: var(--accent); stroke-linecap: round; }
.docname-wrap {
  display: flex; align-items: center; min-width: 0; flex: 1 1 160px; max-width: 360px;
  border: 1px solid var(--line); border-radius: var(--r); background: var(--surface);
  padding-inline: 10px 8px;
}
.docname-wrap:focus-within { border-color: var(--accent); }
.docname { border: 0; background: transparent; padding: 6px 0; min-width: 0; flex: 1; outline: none; }
.ext { color: var(--muted); font-family: var(--f-mono); font-size: 12px; }
.engine { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 12.5px; }
.engine-bar { width: 52px; height: 3px; border-radius: 3px; background: var(--raise); position: relative; overflow: hidden; }
.engine-bar::after { content: ""; position: absolute; inset: 0 auto 0 0; width: 40%; background: var(--accent); border-radius: 3px; animation: slide 1.1s ease-in-out infinite; }
.engine.ready .engine-bar { display: none; }
.engine.failed { color: var(--danger); }
.engine.failed .engine-bar { display: none; }
@keyframes slide { 0% { transform: translateX(-100%); } 100% { transform: translateX(250%); } }
.top-actions { display: flex; gap: 8px; margin-left: auto; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 34px; padding: 0 14px; border-radius: var(--r);
  border: 1px solid var(--line); background: var(--surface); cursor: pointer; font-weight: 500;
  white-space: nowrap;
}
.btn:hover { background: var(--raise); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.lg { min-height: 42px; padding: 0 18px; font-size: 15px; }
.btn:disabled { opacity: .45; cursor: default; filter: none; }

/* ---------- tools band ---------- */
.tools {
  display: flex; flex-wrap: wrap; gap: 2px;
  padding-block: 6px;
  border-bottom: 1px solid var(--line);
}
.tool {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  width: 66px; padding: 5px 2px 4px; border-radius: var(--r);
  border: 1px solid transparent; background: transparent; cursor: pointer;
  font-size: 11.5px; color: var(--muted); line-height: 1.2;
}
.tool svg, .chip.icon svg, .dropzone svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
.tool svg .faint { opacity: .45; }
.tool svg .solid { fill: currentColor; stroke: none; }
.tool:hover { background: var(--raise); color: var(--fg); }
.tool[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); border-color: color-mix(in srgb, var(--accent) 35%, transparent); }
.tool.danger[aria-pressed="true"] { background: var(--danger-soft); color: var(--danger); border-color: color-mix(in srgb, var(--danger) 35%, transparent); }

/* ---------- context band ---------- */
.ctx {
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px 14px;
  padding-block: 7px;
  border-bottom: 1px solid var(--line);
  min-height: 48px;
}
.ctx-group { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; min-width: 0; }
.ctx-spacer { flex: 1 1 0; }
.field {
  min-height: 32px; border: 1px solid var(--line); border-radius: var(--r);
  background: var(--surface); padding: 0 8px;
}
.font-field { width: 190px; max-width: 46vw; }
.num { width: 62px; font-family: var(--f-mono); }
.unit, .lbl { color: var(--muted); font-size: 12px; }
.lbl { text-transform: uppercase; letter-spacing: .06em; font-size: 11px; font-weight: 500; }
.note { color: var(--muted); font-size: 12.5px; }
.chip {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 32px; min-height: 32px; padding: 0 8px; border-radius: var(--r);
  border: 1px solid var(--line); background: var(--surface); cursor: pointer;
}
.chip.wide { padding: 0 11px; white-space: nowrap; }
.chip.icon { padding: 0; width: 32px; }
.chip:hover { background: var(--raise); }
.chip[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.chip:disabled { opacity: .4; cursor: default; }
.swatch {
  position: relative; width: 32px; height: 32px; border-radius: var(--r);
  border: 1px solid var(--line); overflow: hidden; cursor: pointer; display: inline-block;
}
.swatch input { position: absolute; inset: -8px; width: calc(100% + 16px); height: calc(100% + 16px); border: 0; padding: 0; cursor: pointer; }
.range-wrap { display: flex; align-items: center; gap: 6px; }
.range-wrap input { width: 96px; accent-color: var(--accent); }
.range-wrap output { width: 28px; font-size: 12px; color: var(--muted); }
.check { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.check input { accent-color: var(--accent); width: 16px; height: 16px; }
.dot {
  width: 26px; height: 26px; border-radius: 50%; border: 2px solid var(--surface);
  background: var(--c); box-shadow: 0 0 0 1px var(--line); cursor: pointer;
}
.dot[aria-pressed="true"] { box-shadow: 0 0 0 2px var(--accent); }
.zoomval { width: 48px; text-align: center; font-size: 12.5px; }

/* ---------- work area ---------- */
.work {
  display: grid;
  grid-template-columns: 204px minmax(0, 1fr);
  grid-template-areas: "rail stage";
  min-height: 0;
  margin-inline: calc(-1 * max(16px, env(safe-area-inset-left, 0px))) calc(-1 * max(16px, env(safe-area-inset-right, 0px)));
}
.stage {
  grid-area: stage;
  position: relative;
  overflow: auto;
  background: var(--desk);
  min-height: 0;
  overscroll-behavior: contain;
}
.stage.dragging { outline: 2px dashed var(--accent); outline-offset: -8px; }
.rail {
  grid-area: rail;
  display: flex; flex-direction: column; min-height: 0;
  border-right: 1px solid var(--line);
  background: var(--bg);
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: 10px;
}
.rail-head { display: flex; justify-content: space-between; align-items: baseline; padding-block: 10px 6px; }
.thumbs { flex: 1 1 auto; overflow: auto; display: flex; flex-direction: column; gap: 12px; padding: 4px 4px 12px; min-height: 0; }
.thumb {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  border: 0; background: transparent; cursor: pointer; padding: 4px; border-radius: var(--r);
}
.thumb canvas, .thumb .tph {
  display: block; background: var(--paper); box-shadow: 0 0 0 1px var(--line);
  max-width: 100%;
}
.thumb span { font-size: 11.5px; color: var(--muted); font-family: var(--f-mono); }
.thumb[aria-current="true"] canvas, .thumb[aria-current="true"] .tph { box-shadow: 0 0 0 2px var(--accent); }
.thumb[aria-current="true"] span { color: var(--accent); font-weight: 500; }
.rail-tools { display: flex; flex-wrap: wrap; gap: 6px; padding-block: 10px 12px; border-top: 1px solid var(--line); }
.rail-tools .chip { flex: 1 1 calc(50% - 6px); min-width: 0; font-size: 12px; padding: 0 4px; }

/* ---------- pages ---------- */
.pages {
  display: flex; flex-direction: column; align-items: flex-start; gap: 22px;
  width: max-content; min-width: 100%;
  padding: 24px 20px 64px;
}
.page {
  position: relative;
  margin-inline: auto;
  background: var(--paper);
  box-shadow: var(--shadow);
  isolation: isolate;
  touch-action: pan-x pan-y pinch-zoom;
}
.app:not([data-tool="select"]):not([data-tool="erase"]):not([data-tool="image"]):not([data-tool="sign"]) .page { touch-action: none; }
.page > canvas.bitmap { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.page .hl, .page .ov { position: absolute; left: 0; top: 0; transform-origin: 0 0; }
.page .hl { mix-blend-mode: multiply; pointer-events: none; }
.page .ov { cursor: default; }
.app[data-tool="text"] .ov { cursor: text; }
.app[data-tool="draw"] .ov, .app[data-tool="rect"] .ov, .app[data-tool="ellipse"] .ov,
.app[data-tool="line"] .ov, .app[data-tool="arrow"] .ov, .app[data-tool="highlight"] .ov,
.app[data-tool="whiteout"] .ov, .app[data-tool="redact"] .ov { cursor: crosshair; }
.ov.on-text { cursor: text; }
.ov.on-obj { cursor: move; }
.app[data-tool="erase"] .ov.on-obj { cursor: pointer; }
.pnum {
  position: absolute; top: 100%; left: 50%; transform: translate(-50%, 4px);
  font: 11px var(--f-mono); color: var(--muted);
}
.pages .page + .page { margin-top: 4px; }

.obj { position: absolute; left: 0; top: 0; transform-origin: 0 0; pointer-events: none; }
.obj.editing-src { visibility: hidden; }
.tl { position: absolute; white-space: pre; top: 0; }
.obj img { display: block; width: 100%; height: 100%; user-select: none; -webkit-user-drag: none; }
svg.obj { overflow: visible; }

.hov {
  position: absolute; left: 0; top: 0; transform-origin: 0 0; pointer-events: none;
  outline: calc(1.25px / var(--z, 1)) solid var(--mark); background: var(--mark-soft);
  border-radius: 1px;
}
.sel {
  position: absolute; pointer-events: none;
  outline: calc(1.25px / var(--z, 1)) dashed var(--mark);
  outline-offset: calc(2px / var(--z, 1));
}
.handle {
  position: absolute; pointer-events: auto;
  width: calc(11px / var(--z, 1)); height: calc(11px / var(--z, 1));
  margin: calc(-5.5px / var(--z, 1)) 0 0 calc(-5.5px / var(--z, 1));
  background: var(--paper); border: calc(1.5px / var(--z, 1)) solid var(--mark); border-radius: 2px;
  cursor: nwse-resize; touch-action: none;
}
.handle.pt { border-radius: 50%; cursor: crosshair; }
.draft { position: absolute; left: 0; top: 0; overflow: visible; pointer-events: none; }
.marquee { position: absolute; pointer-events: none; outline: calc(1px / var(--z, 1)) dashed var(--mark); background: var(--mark-soft); }
.marquee.redact { outline-color: #c2321d; background: rgba(194, 50, 29, .14); }

.editor { position: absolute; left: 0; top: 0; transform-origin: 0 0; z-index: 5; }
.editor textarea {
  display: block; margin: 0; padding: 0; border: 0; resize: none; overflow: hidden;
  background: var(--paper);
  outline: calc(1.5px / var(--z, 1)) dashed var(--mark);
  outline-offset: calc(2px / var(--z, 1));
  box-shadow: 0 0 0 calc(2px / var(--z, 1)) var(--paper);
  white-space: pre; overflow-wrap: normal; word-break: normal;
  caret-color: var(--mark);
  tab-size: 4;
}
.editor textarea.wrap { white-space: pre-wrap; overflow-wrap: break-word; }

/* ---------- welcome ---------- */
.welcome {
  display: grid; grid-template-columns: minmax(0, 1.05fr) minmax(0, .95fr); gap: 48px;
  align-items: center;
  max-width: 1040px; margin: 0 auto; padding: 48px 32px 56px;
}
.welcome[hidden] { display: none; }
.eyebrow { margin: 0 0 10px; color: var(--accent); font-size: 12px; font-weight: 600; letter-spacing: .1em; text-transform: uppercase; }
.welcome h1 {
  margin: 0; font-family: var(--f-display); font-weight: 600;
  font-size: clamp(30px, 4.2vw, 46px); line-height: 1.06; letter-spacing: -.015em;
  text-wrap: balance; max-width: 15ch;
}
.lede { margin: 16px 0 0; font-size: 16px; color: var(--muted); max-width: 46ch; }
.cta { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 24px; }
.dropzone {
  margin-top: 16px; display: flex; align-items: center; justify-content: center; gap: 10px;
  min-height: 64px; border: 1.5px dashed var(--line); border-radius: 10px; color: var(--muted);
  background: color-mix(in srgb, var(--surface) 55%, transparent);
}
.dropzone.over { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.caps { list-style: none; padding: 0; margin: 22px 0 0; display: grid; gap: 6px; font-size: 13.5px; color: var(--muted); }
.caps b { display: inline-block; width: 74px; color: var(--fg); font-weight: 600; }
.fine { margin: 18px 0 0; font-size: 12.5px; color: var(--muted); }

.demo { margin: 0; display: flex; flex-direction: column; align-items: center; gap: 12px; }
.sheet {
  width: min(100%, 360px); aspect-ratio: 210 / 297; max-width: 100%;
  background: var(--paper); box-shadow: var(--shadow); padding: 30px 28px;
  display: flex; flex-direction: column; gap: 9px; color: #1c2230;
  transform: rotate(-1.2deg);
}
.s-title { font: 600 15px/1 var(--f-ui); letter-spacing: .14em; }
.s-meta { font: 10.5px var(--f-mono); color: #6b7383; margin-bottom: 8px; }
.s-bar { height: 7px; border-radius: 2px; background: #e4e7ee; }
.w80 { width: 80%; } .w92 { width: 92%; } .w64 { width: 64%; } .w72 { width: 72%; } .w40 { width: 40%; }
.s-row { display: flex; justify-content: space-between; font: 11px var(--f-ui); border-bottom: 1px solid #e4e7ee; padding-block: 5px; }
.s-row span:last-child { font-family: var(--f-mono); }
.s-edit { position: relative; display: inline-flex; gap: 6px; align-items: center; }
.s-edit s { color: #b4473a; text-decoration-thickness: 1.5px; opacity: .8; }
.s-edit em { font-style: normal; color: #1c2230; outline: 1.5px dashed var(--mark); outline-offset: 2px; padding-inline: 1px; }
.caret { display: inline-block; width: 1.5px; height: 12px; background: var(--mark); margin-left: 1px; vertical-align: -2px; animation: blink 1.1s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }
.s-sign { margin-top: auto; width: 120px; }
.s-sign svg { width: 100%; fill: none; stroke: #1d3a8a; stroke-width: 1.6; stroke-linecap: round; }
.demo figcaption { font-size: 12.5px; color: var(--muted); text-align: center; max-width: 34ch; }

/* ---------- status ---------- */
.status {
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  padding-block: 6px; border-top: 1px solid var(--line); font-size: 12.5px; color: var(--muted);
}
.hint { flex: 1 1 260px; min-width: 0; }
.paper { font-size: 12px; }
.pager { display: flex; align-items: center; gap: 6px; }
.pager .chip.icon { width: 28px; min-height: 28px; min-width: 28px; }

/* ---------- toast and dialog ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%);
  max-width: min(520px, calc(100vw - 32px));
  background: var(--fg); color: var(--bg); padding: 10px 16px; border-radius: 8px;
  box-shadow: var(--shadow); font-size: 13.5px; z-index: 50;
}
.scrim {
  position: fixed; inset: 0; z-index: 40; display: grid; place-items: center;
  background: rgba(10, 14, 22, .45);
  padding: max(16px, env(safe-area-inset-top, 0px)) max(16px, env(safe-area-inset-right, 0px)) max(16px, env(safe-area-inset-bottom, 0px)) max(16px, env(safe-area-inset-left, 0px));
}
.scrim[hidden] { display: none; }
.dialog { width: min(640px, 100%); background: var(--surface); border-radius: 12px; padding: 20px; box-shadow: var(--shadow); }
.dialog h2 { margin: 0; font-family: var(--f-display); font-weight: 600; font-size: 22px; }
.dialog-note { margin: 6px 0 14px; color: var(--muted); font-size: 13px; }
.sigpad { width: 100%; height: auto; aspect-ratio: 3 / 1; background: var(--paper); border: 1px solid var(--line); border-radius: 8px; touch-action: none; display: block; }
.dialog-actions { display: flex; gap: 8px; margin-top: 14px; flex-wrap: wrap; }
.grow { flex: 1; }

/* ---------- narrow screens ---------- */
@media (max-width: 760px) {
  html, body { height: auto; }
  .app { height: auto; display: block; }
  .brand span { display: none; }
  .engine { order: 5; width: 100%; }
  .top-actions { margin-left: 0; }
  .tool { width: calc((100% - 12px) / 7); min-width: 44px; font-size: 10.5px; }
  .tool svg { width: 19px; height: 19px; }
  .work { display: flex; flex-direction: column; }
  .stage { height: 68vh; min-height: 340px; }
  .rail { border-right: 0; border-top: 1px solid var(--line); padding-right: max(16px, env(safe-area-inset-right, 0px)); }
  .thumbs { flex-direction: row; overflow-x: auto; overflow-y: hidden; padding-bottom: 8px; }
  .thumb { flex: 0 0 auto; }
  .welcome { grid-template-columns: minmax(0, 1fr); gap: 28px; padding: 28px 18px 36px; }
  .demo { order: -1; }
  .sheet { width: 230px; padding: 20px; gap: 6px; }
  .s-row { font-size: 9.5px; }
  .font-field { width: 150px; }
  .status { padding-block: 8px; }
}
@media (max-width: 420px) {
  .tool span { font-size: 9.5px; }
  .ctx { gap: 6px 10px; }
  .range-wrap input { width: 72px; }
}
@media (prefers-reduced-motion: reduce) {
  .engine-bar::after, .caret { animation: none; }
}
