/* ---- Self-hosted fonts (copied by scripts/vendor.mjs) ---- */
@font-face { font-family: "Atkinson Hyperlegible"; font-weight: 400; font-display: swap; src: url("/vendor/fonts/atkinson-400.woff2") format("woff2"); }
@font-face { font-family: "Atkinson Hyperlegible"; font-weight: 700; font-display: swap; src: url("/vendor/fonts/atkinson-700.woff2") format("woff2"); }
@font-face { font-family: "Mrs Saint Delafield"; font-weight: 400; font-display: swap; src: url("/vendor/fonts/mrs-saint-delafield-400.woff2") format("woff2"); }

:root {
  color-scheme: light;
  --ink: #55298a;
  --ink-contrast: #fff;
  --tool-icon: #8545d1;
  --surface: #fff;
  --sidebar: #fdfdfd;
  --desk: #f0f0f2;
  --desk-accent: var(--ink);
  --text: #212529;
  --muted: #585c64;
  --border: #d9dce1;
  --hover: #f1eafa;
  --danger: #a52331;
  --radius: .375rem;
  --paper-shadow: 0 1px 2px rgb(20 30 40 / .18), 0 8px 24px rgb(20 30 40 / .12);
  --doc-font: Helvetica, Arial, "Liberation Sans", sans-serif;
  --sig-font: "Mrs Saint Delafield", cursive;
  font-family: "Atkinson Hyperlegible", system-ui, sans-serif;
  line-height: 1.5;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --ink: #c9a9f2;
  --ink-contrast: #20112e;
  --tool-icon: #c39af5;
  --surface: #23252b;
  --sidebar: #1d1e24;
  --desk: #32343b;
  --text: #efedf3;
  --muted: #c0bdc8;
  --border: #464852;
  --hover: #362b44;
  --danger: #ff9da6;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
body { display: flex; margin: 0; color: var(--text); background: var(--surface); }
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: default; color: var(--muted); }
button:focus-visible, input:focus-visible, summary:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
}
p { margin: 0 0 1rem; }
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* ---- Layout ---- */
html, body { height: 100%; }
.sidebar {
  flex: none;
  overflow: auto;
  padding: 1.25rem 1.8rem;
  width: var(--sidebar-width, 15.5rem); /* app.js sets --sidebar-width when it's resized, up to twice this */
  background: var(--sidebar);
  border-right: 1px solid var(--border);
}
.sidebar-logo { display: block; width: 100%; max-width: 10.4rem; height: auto; margin: 0 auto 3.75rem; }

/* The strip between the sidebar and the document. It sits over the sidebar's border without
   taking up space, and shows a line on hover, focus and while dragging. */
.sidebar-resizer {
  position: relative; z-index: 5;
  flex: none;
  width: 9px;
  margin: 0 -5px 0 -4px;
  cursor: col-resize;
  touch-action: none;
}
.sidebar-resizer::after {
  content: "";
  position: absolute; top: 0; bottom: 0; left: 3px;
  width: 3px;
  background: var(--ink);
  opacity: 0;
  transition: opacity .15s;
}
.sidebar-resizer:hover::after,
.sidebar-resizer:focus-visible::after,
body.resizing-sidebar .sidebar-resizer::after { opacity: 1; }
.sidebar-resizer:focus-visible { outline: none; }
body.resizing-sidebar, body.resizing-sidebar * { cursor: col-resize !important; user-select: none; }
/* ---- Desk, drop box and pages ---- */
#desk { flex: 1; container-type: size; min-width: 0; overflow: auto; position: relative; background: var(--desk); }
#desk[aria-busy="true"] { cursor: progress; }
.empty {
  display: flex; flex-direction: column; justify-content: center;
  min-height: 100%; text-align: center; max-width: 32rem; margin: 0 auto; padding: 1.5rem;
}
.empty[hidden] { display: none; }
.note:empty, .error:empty { display: none; }
.dropzone {
  position: relative;
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  width: min(100%, 20rem);
  margin: 0 auto 1rem;
  padding: 1.5rem;
  border: 3px dotted var(--desk-accent);
  border-radius: 10px;
  color: var(--desk-accent);
  font-size: 1.25rem;
  font-weight: 700;
  cursor: pointer;
  transition: background .2s ease;
}
.dropzone:hover, #desk.dragging .dropzone { background: var(--sidebar); }
.dropzone:focus-within { outline: 2px solid var(--desk-accent); outline-offset: 3px; }
.error { color: var(--danger); }
.dropzone input { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; cursor: pointer; }

.pages { display: flex; flex-direction: column; align-items: safe center; gap: 1.5rem; padding: 1.5rem; }
.page:last-child { margin-bottom: max(0px, calc(100cqh - 3rem - var(--page-height))); }
.pages:empty { display: none; }
.page {
  position: relative; flex: none; background: #fff; box-shadow: var(--paper-shadow);
  --select: #55298a; /* outlines and handles sit on white paper, so they keep this colour in dark mode */
}
.page canvas { position: absolute; inset: 0; }
.overlay { position: absolute; inset: 0; }
#desk.placing .overlay { cursor: crosshair; }

/* ---- Placed items ---- */
.item { position: absolute; }
.item:not(.item-text).selected { outline: 1.5px dashed var(--select); outline-offset: 3px; }
.editor {
  position: relative; z-index: 1; /* above the outline's drag area */
  font-family: var(--doc-font);
  line-height: 1.2;              /* must match app.js LINE_HEIGHT */
  color: #14171a;
  white-space: pre;
  min-width: 1ch;
  min-height: 1.2em;
  padding: 0;
  outline: none;
  cursor: text;
}
.editor.wrap { white-space: pre-wrap; overflow-wrap: break-word; } /* a box someone has resized */

/* A text box's outline. The band between the line and the text is the drag area for moving it,
   and the circles resize it. The line shows on hover and while selected; the circles while selected. */
.frame {
  position: absolute; inset: -8px;
  border: 1px solid transparent;
  cursor: move;
  touch-action: none;
}
.frame::before { content: ""; position: absolute; inset: -5px; } /* a little grab room outside the line */
.item-text:hover .frame { border-color: rgb(85 41 138 / .35); }
.item-text.selected .frame { border-color: var(--select); }
.handle {
  display: none;
  position: absolute;
  width: 11px; height: 11px;
  transform: translate(-50%, -50%);
  background: #fff;
  border: 1.5px solid var(--select);
  border-radius: 50%;
  touch-action: none;
}
.handle::before { content: ""; position: absolute; inset: -6px; } /* easier to hit with a finger */
.item-text.selected .handle { display: block; }
.handle[data-h="nw"] { left: 0;    top: 0;    cursor: nwse-resize; }
.handle[data-h="n"]  { left: 50%;  top: 0;    cursor: ns-resize; }
.handle[data-h="ne"] { left: 100%; top: 0;    cursor: nesw-resize; }
.handle[data-h="e"]  { left: 100%; top: 50%;  cursor: ew-resize; }
.handle[data-h="se"] { left: 100%; top: 100%; cursor: nwse-resize; }
.handle[data-h="s"]  { left: 50%;  top: 100%; cursor: ns-resize; }
.handle[data-h="sw"] { left: 0;    top: 100%; cursor: nesw-resize; }
.handle[data-h="w"]  { left: 0;    top: 50%;  cursor: ew-resize; }
.item-image img { display: block; width: 100%; height: 100%; user-select: none; cursor: move; touch-action: none; }
.item-mark { cursor: move; touch-action: none; user-select: none; }
.item-mark > svg { display: block; width: 100%; height: 100%; pointer-events: none; } /* drags go to the item */
.resize {
  position: absolute; right: -8px; bottom: -8px;
  width: 16px; height: 16px;
  display: none;
  background: #fff;
  border: 2px solid var(--select);
  border-radius: 50%;
  cursor: nwse-resize;
  touch-action: none;
}
.item-bar {
  position: absolute; left: -4px; bottom: calc(100% + 18px); /* clear of a text box's top circles */
  display: none;
  gap: 2px;
  padding: 3px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--paper-shadow);
  white-space: nowrap;
}
.item.selected .item-bar { display: flex; }
.item.selected .resize { display: block; }
.item-bar .button svg { display: block; }
.item-bar .remove { color: var(--danger); }

.item-colors {
  display: flex;
  align-items: center;
  gap: .45rem;
  padding: .25rem .4rem;
}
.swatch {
  width: 1.1rem;
  height: 1.1rem;
  padding: 0;
  border: 2px solid var(--surface);
  border-radius: 50%;
  box-shadow: 0 0 0 1px var(--border);
}
.swatch[aria-pressed="true"] { box-shadow: 0 0 0 2px var(--ink); }

/* ---- Floating messages ---- */
.hint {
  position: fixed; left: 50%; bottom: 1.25rem; transform: translateX(-50%);
  display: flex; align-items: center; gap: .5rem;
  padding: .5rem 1rem;
  background: var(--text); color: var(--surface);
  border-radius: 2rem;
  box-shadow: var(--paper-shadow);
  z-index: 6;
}
.hint .link { color: inherit; }
.notice {
  position: fixed; top: 1rem; left: 50%; transform: translateX(-50%);
  max-width: calc(100vw - 2rem);
  border-radius: var(--radius);
  box-shadow: var(--paper-shadow);
  z-index: 6;
  padding: .625rem 1rem;
  background: var(--ink);
  color: var(--ink-contrast);
}
.app-toast {
  position: fixed; right: 1.25rem; bottom: 1.25rem; z-index: 10;
  display: flex; align-items: center; gap: .75rem;
  max-width: min(30rem, calc(100vw - 2.5rem));
  padding: .75rem .75rem .75rem 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 5px solid var(--ink);
  border-radius: var(--radius);
  box-shadow: var(--paper-shadow);
}
.app-toast.warn { border-left-color: var(--danger); }

/* ---- Dialogs ---- */
.app-dialog {
  width: min(36rem, calc(100vw - 2rem));
  padding: 1.5rem;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: .9rem;
}
dialog::backdrop { background: rgb(15 20 25 / .45); }

/* ---- Signature pad ---- */
.pad-wrap { position: relative; }
.pad-wrap::after {                /* the "sign here" line */
  content: "";
  position: absolute; left: 8%; right: 8%; bottom: 28%;
  border-bottom: 1px solid #c3cbc6;
  pointer-events: none;
}
#sigPad, .sig-preview {
  display: block;
  width: 100%;
  height: 180px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 8px;
}
#sigPad { touch-action: none; cursor: crosshair; }
.sig-preview {
  display: flex; align-items: center;
  margin-top: .5rem; padding: 0 1.25rem;
  overflow: hidden; white-space: nowrap;
  font: 64px/1 var(--sig-font);
  color: #14171a;
}
.form-note { min-height: 1.25em; }

.tools h2 {
  margin: 0 0 .5rem;
  font-size: .7rem;
  letter-spacing: .03em;
  text-transform: uppercase;
}
.tool-button, .rail-button, .zoom-label {
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: transparent;
  color: inherit;
}
.tool-button {
  display: flex;
  align-items: center;
  gap: 1.25rem;
  width: 100%;
  min-height: 2.6rem;
  padding: .4rem .55rem;
  text-align: left;
}
.tool-button svg, .rail-button svg {
  flex: none;
  width: 1.15rem;
  height: 1.15rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.65;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.tool-button svg { color: var(--tool-icon); }
.tool-button[data-tool="check"] svg,
.tool-button[data-tool="x"] svg,
.tool-button[data-tool="dot"] svg { fill: currentColor; stroke: none; }
.tool-button:disabled svg { color: inherit; }
.tool-button:hover:not(:disabled), .rail-button:hover:not(:disabled),
.zoom-label:hover:not(:disabled), .button:hover:not(:disabled), .tool-button[aria-pressed="true"] {
  background: var(--hover);
}
.tool-button[aria-pressed="true"] { color: var(--ink); }
.document-actions { margin-top: 3.5rem; }
.mode-badge {
  display: inline-block;
  margin-bottom: .75rem;
  padding: .1rem .55rem;
  border: 1px solid var(--ink);
  border-radius: 1rem;
  color: var(--ink);
  font-size: .75rem;
}

.view-toolbar {
  display: flex;
  flex: 0 0 3rem;
  flex-direction: column;
  align-items: center;
  justify-content: safe flex-end;
  gap: .5rem;
  padding: .5rem .125rem 1.5rem;
  border-left: 1px solid var(--border);
  background: var(--sidebar);
}
.page-nav, .zoom-controls {
  display: grid;
  justify-items: center;
  gap: .125rem;
}
.page-nav { font-size: .8125rem; }
.page-nav output { padding-top: .75rem; }
#pageNumber {
  width: 2rem;
  min-height: 2rem;
  padding: .125rem;
  border: 1px solid var(--border);
  border-radius: .25rem;
  background: var(--surface);
  text-align: center;
}
.rail-button, .dismiss-button { display: grid; place-items: center; }
.rail-button { width: 2.25rem; height: 2.35rem; padding: 0; }
.rail-button:disabled { opacity: .5; }
.zoom-label {
  border-color: var(--border);
  min-width: 2.5rem;
  padding: 0 .2rem;
  font-size: .8125rem;
}
.theme-switch { position: relative; }
.theme-switch summary { cursor: pointer; list-style: none; }
.theme-switch summary::-webkit-details-marker { display: none; }
.theme-switch summary::after {
  content: "";
  position: absolute;
  right: .2rem;
  bottom: .2rem;
  border: .16rem solid transparent;
  border-right-color: currentColor;
  border-bottom-color: currentColor;
}
.theme-switch:not([open]) .theme-options { display: none; }
.theme-options {
  position: absolute;
  z-index: 5;
  top: 50%;
  right: calc(100% + .35rem);
  transform: translateY(-50%);
  display: flex;
  padding: .25rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--paper-shadow);
}

.button {
  padding: .4rem .75rem;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: transparent;
}
.button.primary, .signature-tabs [aria-selected="true"], .theme-options [aria-pressed="true"], .remote-chip.signed {
  background: var(--ink);
  color: var(--ink-contrast);
}
.button.primary:hover:not(:disabled), .theme-options [aria-pressed="true"]:hover,
.signature-tabs [aria-selected="true"]:hover, .remote-chip.signed:hover {
  background: var(--ink);
  filter: brightness(1.12);
}
.button.outline { border-color: var(--border); }
.remote-chip { width: 100%; }
.button.link { padding-inline: 0; color: var(--ink); }
.button.compact { padding: .2rem .45rem; font-size: .875rem; }
.muted, .button.muted { color: var(--muted); }
.danger, .button.danger { color: var(--danger); }
.detail { font-size: .875rem; }
.dismiss-button {
  margin-left: auto;
  padding: 0 .3rem;
  border: 0;
  background: transparent;
  font-size: 1.5rem;
}
.dismiss-button::before { content: "×"; }
.dialog-title { margin: 0 0 1rem; font-size: 1.5rem; line-height: 1.25; }
.dialog-actions, .signature-tabs, .link-field { display: flex; gap: .5rem; }
.dialog-actions { justify-content: flex-end; align-items: center; flex-wrap: wrap; }
.action-spacer { flex: 1; }
.signature-tabs { margin-bottom: 1rem; }
.link-field { margin-bottom: .5rem; }
.field {
  width: 100%;
  min-width: 0;
  padding: .4rem .65rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}
.link-field button { flex: none; }
.remote-status { font-weight: 700; margin: 1rem 0; }
.ink-options { border: 0; padding: 0; margin: 1rem 0; }
.ink-options legend { float: left; margin-right: 1rem; padding: 0; font-weight: 700; }
.ink-options label { margin-right: .75rem; }
.ink-options input, .remember-option input { margin: 0 .3rem 0 0; accent-color: var(--ink); }
.form-note { margin-top: .5rem; }

@media (max-height: 420px) {
  .view-toolbar { gap: 0; padding-block: .25rem; }
  .rail-button { height: 1.75rem; }
  .page-nav output { padding-top: .15rem; }
}
@media (min-width: 768px) {
  .empty { transform: translate(1.375rem, -.125rem); }
}
@media (max-width: 767.98px) {
  body { display: grid; grid-template-columns: minmax(0, 1fr) 3rem; grid-template-rows: auto minmax(0, 1fr); }
  .sidebar { grid-column: 1 / -1; width: auto; max-height: 38dvh; padding: .6rem .8rem; border-right: 0; border-bottom: 1px solid var(--border); }
  .sidebar-logo { width: 6rem; margin: 0 0 .5rem; }
  .sidebar-resizer { display: none; }
  .tools, .document-actions { display: flex; align-items: center; flex-wrap: wrap; gap: .15rem .375rem; }
  .tools h2 { flex-basis: 100%; margin-bottom: 0; }
  .tool-button { width: auto; gap: .4rem; min-height: 2rem; }
  .document-actions { margin-top: .25rem; }
  .remote-chip { width: auto; }
  #desk { grid-column: 1; }
}

@page { margin: 0; }
@media print {
  html, body, #desk { display: block; height: auto; overflow: visible; background: #fff; }
  #desk { container-type: normal; }
  body > :not(#desk), .empty, .frame, .item.selected .item-bar, .item.selected .resize { display: none !important; }
  .pages { display: block; padding: 0; }
  .page, .page:last-child { margin: 0 auto; box-shadow: none; break-inside: avoid; zoom: var(--print-scale, 1); }
  .page + .page { break-before: page; }
  .item:not(.item-text).selected { outline: none; }
}
