:root {
  --brand: #f0442f;
  --brand-2: #e8173f;
  --grad: linear-gradient(135deg, #ff7a3d, #e8173f);
  --bg: #eef0f4;
  --bar: #ffffff;
  --fg: #1b1b22;
  --muted: #6b7080;
  --line: #e2e4ea;
  --hover: #f1f2f6;
  --active: #ffe9e4;
  --focus: #2563eb;
  --shadow: 0 1px 3px rgb(0 0 0 / .12), 0 8px 24px rgb(0 0 0 / .08);
  color-scheme: light dark;
  font: 14px/1.4 Inter, "Segoe UI", system-ui, -apple-system, Helvetica, Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #15161b; --bar: #1e2027; --fg: #ececf1; --muted: #9a9fb0; --line: #2d3039; --hover: #2a2d36; --active: #47231f; }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { display: flex; flex-direction: column; background: var(--bg); color: var(--fg); overflow: hidden; }
[hidden] { display: none !important; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* Toolbar */
.bar { display: flex; align-items: center; gap: 6px; padding: 6px 10px; padding-top: max(6px, env(safe-area-inset-top)); background: var(--bar); border-bottom: 1px solid var(--line); overflow-x: auto; scrollbar-width: none; flex: none; z-index: 5; }
.bar::-webkit-scrollbar { display: none; }
.brand { display: flex; align-items: center; gap: 8px; color: var(--fg); text-decoration: none; font-weight: 800; font-size: 18px; letter-spacing: -.5px; padding-right: 6px; flex: none; }
.brand b { background: var(--grad); -webkit-background-clip: text; background-clip: text; color: transparent; }
.group { display: flex; align-items: center; gap: 2px; padding: 0 6px; border-left: 1px solid var(--line); flex: none; }
.spacer { flex: 1; }
button { font: inherit; color: inherit; background: none; border: 0; border-radius: 8px; height: 34px; min-width: 34px; padding: 0 7px; display: inline-flex; align-items: center; justify-content: center; gap: 6px; cursor: pointer; }
button:hover:not(:disabled) { background: var(--hover); }
button:disabled { opacity: .35; cursor: default; }
button[aria-pressed="true"] { background: var(--active); color: var(--brand-2); }
button.primary { background: var(--grad); color: #fff; font-weight: 600; padding: 0 14px; }
button.primary:hover:not(:disabled) { background: var(--grad); filter: brightness(1.08); }
button.zoom { min-width: 54px; font-variant-numeric: tabular-nums; font-size: 13px; }
input[type="color"] { width: 30px; height: 30px; border: 1px solid var(--line); border-radius: 8px; padding: 2px; background: none; cursor: pointer; margin-left: 4px; }
.pager { display: flex; align-items: center; gap: 4px; color: var(--muted); flex: none; padding-left: 6px; }
.pager input { width: 52px; height: 30px; text-align: center; border: 1px solid var(--line); border-radius: 8px; background: transparent; color: var(--fg); font: inherit; -moz-appearance: textfield; }
.pager input::-webkit-inner-spin-button { display: none; }
body:not(.has-doc) .needs-doc { display: none; }
body.readonly .edit { display: none !important; }

/* Layout */
.main { flex: 1; display: flex; min-height: 0; position: relative; }
.viewer { flex: 1; overflow: auto; position: relative; outline: none; }
.pages { display: flex; flex-direction: column; align-items: center; gap: 16px; padding: 20px 20px 40vh; width: max-content; min-width: 100%; }
body:not(.has-doc) .thumbs { display: none; }

/* Pages */
.page { position: relative; background: #fff; box-shadow: var(--shadow); flex: none; user-select: none; -webkit-user-select: none; }
.page canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.page .ink { z-index: 2; pointer-events: none; }
.page .textLayer { z-index: 1; width: 100% !important; height: 100% !important; }
body:not([data-tool="select"]) .page { cursor: crosshair; touch-action: none; }
body:not([data-tool="select"]) .textLayer { pointer-events: none; }
body[data-tool="text"] .page { cursor: text; }
.text-input { position: absolute; z-index: 3; border: 1px dashed var(--focus); background: rgb(255 255 255 / .85); padding: 0 2px; margin: 0 0 0 -3px; font-family: Helvetica, Arial, sans-serif; line-height: 1; height: 1.25em; min-width: 6em; field-sizing: content; outline: none; color-scheme: light; }

/* Thumbnails */
.thumbs { width: 172px; flex: none; overflow-y: auto; background: var(--bar); border-right: 1px solid var(--line); padding: 12px 0 40px; display: flex; flex-direction: column; align-items: center; gap: 14px; }
.thumbs.hide { display: none; }
.thumb { position: relative; width: 124px; cursor: pointer; text-align: center; }
.thumb .frame { width: 124px; min-height: 60px; background: #fff; box-shadow: var(--shadow); border: 2px solid transparent; border-radius: 3px; display: flex; align-items: center; justify-content: center; overflow: hidden; }
.thumb canvas { display: block; max-width: 100%; }
.thumb.active .frame { border-color: var(--brand); }
.thumb.drag-over .frame { outline: 2px dashed var(--brand); outline-offset: 3px; }
.thumb .num { display: block; font-size: 12px; color: var(--muted); margin-top: 4px; }
.thumb .ops { position: absolute; top: 4px; right: 4px; display: flex; gap: 2px; opacity: 0; transition: opacity .15s; }
.thumb:hover .ops, .thumb:focus-within .ops { opacity: 1; }
.thumb .ops button { height: 26px; min-width: 26px; padding: 0; background: rgb(20 20 28 / .75); color: #fff; }
.thumb .ops svg { width: 15px; height: 15px; }

/* Welcome */
.welcome { min-height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 40px 20px; gap: 14px; }
.welcome-logo { width: min(340px, 80vw); height: auto; }
.tagline { font-size: 17px; color: var(--muted); margin: 0 0 10px; }
button.big { height: 50px; font-size: 16px; padding: 0 26px; border-radius: 12px; box-shadow: 0 6px 20px rgb(232 23 63 / .3); }
.hint { color: var(--muted); margin: 0; }
.hint a { color: var(--brand); }
.features { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; padding: 0; margin: 26px 0 0; list-style: none; }
.features li { display: flex; align-items: center; gap: 6px; background: var(--bar); border: 1px solid var(--line); padding: 7px 12px; border-radius: 999px; color: var(--muted); }
.features svg { width: 16px; height: 16px; color: var(--brand); }

/* Overlays */
.drop { position: fixed; inset: 0; z-index: 20; background: rgb(240 68 47 / .12); display: flex; align-items: center; justify-content: center; pointer-events: none; border: 3px dashed var(--brand); }
.drop div { background: var(--grad); color: #fff; font-size: 20px; font-weight: 700; padding: 16px 28px; border-radius: 14px; }
.busy { position: fixed; inset: 0; z-index: 30; display: flex; align-items: center; justify-content: center; background: rgb(0 0 0 / .15); }
.spinner { width: 42px; height: 42px; border-radius: 50%; border: 4px solid rgb(255 255 255 / .5); border-top-color: var(--brand); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.toast { position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); transform: translate(-50%, 20px); background: #1b1b22; color: #fff; padding: 10px 16px; border-radius: 10px; box-shadow: var(--shadow); opacity: 0; pointer-events: none; transition: .2s; z-index: 40; max-width: calc(100vw - 32px); }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Signature dialog */
dialog { border: 0; border-radius: 16px; padding: 20px; width: min(560px, calc(100vw - 24px)); background: var(--bar); color: var(--fg); box-shadow: 0 20px 60px rgb(0 0 0 / .3); }
dialog::backdrop { background: rgb(10 10 20 / .45); }
dialog h2 { margin: 0 0 12px; font-size: 18px; }
.tabs { display: flex; gap: 4px; margin-bottom: 10px; }
.tabs button[aria-selected="true"] { background: var(--active); color: var(--brand-2); }
#signPad { width: 100%; aspect-ratio: 3; background: #fff; border: 1px solid var(--line); border-radius: 10px; touch-action: none; cursor: crosshair; display: block; }
#signName { width: 100%; height: 38px; margin-bottom: 10px; border: 1px solid var(--line); border-radius: 8px; padding: 0 10px; font: inherit; background: transparent; color: var(--fg); }
.row { display: flex; align-items: center; gap: 6px; margin-top: 14px; }

@media (max-width: 1100px) {
  .brand span, .pager > span { display: none; }
  .group { padding: 0 3px; }
  button { min-width: 32px; padding: 0 5px; }
}
@media (max-width: 1000px) {
  #btnSave span { display: none; }
  button.primary { padding: 0 10px; }
}
@media (max-width: 760px) {
  .thumbs { position: absolute; inset: 0 auto 0 0; z-index: 4; box-shadow: var(--shadow); }
  .pages { padding: 12px 12px 40vh; gap: 12px; }
  .bar { flex-wrap: wrap; row-gap: 4px; padding-left: 6px; padding-right: 6px; }
  .bar .spacer { display: none; }
  .group { border-left: 0; }
}
