:root {
  color-scheme: light dark;
  --bg: #f3f4f6;
  --surface: #ffffff;
  --text: #1c2230;
  --muted: #5b6475;
  --line: #d4d8e0;
  --accent: #2f6fde;
  --accent-text: #ffffff;
  --accent-soft: #e8f0fd;
  --page-shadow: 0 1px 3px rgb(0 0 0 / 0.18), 0 6px 18px rgb(0 0 0 / 0.08);
  --error: #b3261e;
  --warning: #8a5a00;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171c;
    --surface: #1d2128;
    --text: #e6e9ef;
    --muted: #9aa3b2;
    --line: #353b45;
    --accent: #6b9cf0;
    --accent-text: #0d1320;
    --accent-soft: #1f2a3d;
    --page-shadow: 0 1px 3px rgb(0 0 0 / 0.6), 0 6px 18px rgb(0 0 0 / 0.4);
    --error: #f2b8b5;
    --warning: #f0c060;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

.visually-hidden {
  position: absolute !important;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.masthead { padding: 28px 16px 8px; max-width: 1200px; margin: 0 auto; }
.masthead h1 { margin: 0; font-size: 1.6rem; }
.masthead p { margin: 4px 0 0; color: var(--muted); }

.app { max-width: 1200px; margin: 0 auto; padding: 16px; display: grid; gap: 16px; }
.app[data-state="empty"] { max-width: 640px; }
.app[data-state="empty"] .panel,
.app[data-state="empty"] .document { display: none; }
.app[data-state="ready"] { grid-template-columns: 280px minmax(0, 1fr); align-items: start; }
.app[data-state="ready"] .opener { display: none; }

@media (max-width: 800px) {
  .app[data-state="ready"] { grid-template-columns: minmax(0, 1fr); }
}

.drop {
  display: block;
  position: relative;
  background: var(--surface);
  border: 2px dashed var(--line);
  border-radius: 12px;
  padding: 48px 16px;
  text-align: center;
  cursor: pointer;
}
.drop:hover, .drop.over, .drop:focus-within { border-color: var(--accent); background: var(--accent-soft); }
.drop strong { color: var(--accent); }
.drop input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.hint { display: block; color: var(--muted); font-size: .875rem; margin: 6px 0 0; }
.sheet .hint { margin: 0; }
.sheet .hint:empty { display: none; }

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 16px;
  position: sticky;
  top: 16px;
}
@media (max-width: 800px) { .panel { position: static; } }

.panel h2 { margin: 0 0 12px; font-size: 1.05rem; }
.add { display: flex; flex-wrap: wrap; gap: 8px; }

.signatures { list-style: none; margin: 14px 0 0; padding: 0; display: grid; gap: 8px; }
.signatures li { display: flex; gap: 6px; align-items: stretch; }
.pick {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 64px;
  padding: 6px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  cursor: pointer;
}
.pick img { max-width: 100%; max-height: 52px; }
.pick[aria-pressed="true"] { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.forget {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  width: 36px;
  cursor: pointer;
  font-size: 1.1rem;
}
.check { display: flex; gap: 8px; align-items: flex-start; font-size: .875rem; color: var(--muted); margin-top: 12px; }

.document { min-width: 0; }
.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 16px;
  position: sticky;
  top: 0;
  z-index: 2;
}
.document-name { margin: 0; font-size: 1rem; overflow-wrap: anywhere; }
.details { margin: 0; color: var(--muted); font-size: .875rem; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }

/* On a narrow screen the toolbar stays pinned above the pages, so it is kept to one short row. */
@media (max-width: 800px) {
  .toolbar { flex-wrap: nowrap; gap: 8px; padding: 8px 12px; }
  .toolbar > div:first-child { min-width: 0; }
  .document-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; overflow-wrap: normal; font-size: .9375rem; }
  .details { display: none; }
  .actions { flex-wrap: nowrap; gap: 6px; }
  .actions .button { padding: 8px 12px; }
  /* Shortened on screen, but still read out in full. */
  .wide { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
}

.pages { display: grid; gap: 20px; padding: 20px 0; justify-items: center; }
.page {
  position: relative;
  width: min(100%, 900px);
  background: #ffffff;
  box-shadow: var(--page-shadow);
}
.page canvas { display: block; width: 100%; height: 100%; }
.page-label { position: absolute; left: 0; bottom: -20px; font-size: .75rem; color: var(--muted); }
.layer { position: absolute; inset: 0; }
.pages.placing .layer { cursor: crosshair; }
/* Drawing a line or shape takes the drag, so a finger does not scroll the page instead. */
.pages.drawing .layer { touch-action: none; }
.pages.placing .page:hover { outline: 2px dashed var(--accent); outline-offset: 4px; }

.item {
  position: absolute;
  touch-action: none;
  cursor: move;
  outline: 1px dashed transparent;
}
.item img { width: 100%; height: 100%; display: block; pointer-events: none; user-select: none; }
/* A mark is drawn a little beyond its box, so a line's thickness is never cut off. */
.item svg { position: absolute; overflow: visible; pointer-events: none; }
/* A level line has no height of its own to grab, so it gets some. */
.item.line::before { content: ""; position: absolute; inset: -10px; }
.item .end {
  position: absolute;
  display: none;
  width: 14px;
  height: 14px;
  margin: -7px 0 0 -7px;
  border-radius: 50%;
  background: var(--accent);
  cursor: move;
}
.item:hover .end, .item:focus-within .end, .item.selected .end { display: block; }
.item:hover, .item:focus-visible, .item.selected { outline-color: var(--accent); }
.item:focus-visible { outline-style: solid; outline-width: 2px; }
.item .remove,
.item .resize { position: absolute; display: none; }
.item:hover .remove, .item:focus-within .remove, .item.selected .remove,
.item:hover .resize, .item:focus-within .resize, .item.selected .resize { display: block; }
.item .remove {
  top: -12px;
  right: -12px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 14px;
  line-height: 24px;
  cursor: pointer;
}
.item .resize {
  right: -7px;
  bottom: -7px;
  width: 14px;
  height: 14px;
  border-radius: 3px;
  background: var(--accent);
  cursor: nwse-resize;
}

/* Fingers need bigger targets: a 36px remove button, and a 32px area around the resize handle. */
@media (pointer: coarse) {
  .item .remove { top: -18px; right: -18px; width: 36px; height: 36px; line-height: 36px; font-size: 18px; }
  .item .resize { right: -16px; bottom: -16px; width: 32px; height: 32px; background: transparent; }
  .item .resize::after { content: ""; position: absolute; inset: 6px; border-radius: 4px; background: var(--accent); }
  .item .end { width: 32px; height: 32px; margin: -16px 0 0 -16px; background: transparent; }
  .item .end::after { content: ""; position: absolute; inset: 8px; border-radius: 50%; background: var(--accent); }
}

.button {
  display: inline-block;
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--accent);
  background: transparent;
  color: var(--accent);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.button.primary { background: var(--accent); color: var(--accent-text); }
.button:disabled { opacity: .5; cursor: not-allowed; }
.button:focus-visible, .pick:focus-visible, .forget:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* A bar pinned to the bottom of the screen, so a message is seen however far down the pages are scrolled. */
.status {
  position: fixed;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  z-index: 10;
  width: max-content;
  max-width: min(640px, calc(100vw - 32px));
  margin: 0;
  padding: 10px 16px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 4px 16px rgb(0 0 0 / 0.18);
}
.status:empty { display: none; }
.status.error { color: var(--error); }
.status.warning { color: var(--warning); }

dialog {
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 0;
  background: var(--surface);
  color: var(--text);
  width: min(640px, calc(100vw - 32px));
}
dialog::backdrop { background: rgb(0 0 0 / 0.45); }
.sheet { padding: 20px; display: grid; gap: 14px; }
.sheet h2 { margin: 0; font-size: 1.15rem; }
.row { display: flex; flex-wrap: wrap; gap: 10px; align-items: end; }
.row.end { justify-content: flex-end; }
.field { display: grid; gap: 4px; font-size: .875rem; color: var(--muted); }
.field input, .field select {
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  min-width: 0;
}
.inks { border: none; margin: 0; padding: 0; display: flex; gap: 12px; }
/* Undo and Clear wrap as a pair on a narrow screen. */
.pad-tools { display: flex; gap: 10px; }

/* The pad's tools: radio buttons shown as a row of icons. */
.drawing-tools { border: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; }
.drawing-tools label { display: block; cursor: pointer; }
.drawing-tools label > span {
  display: grid;
  place-items: center;
  width: 40px;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
}
.drawing-tools svg, .marks svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.marks svg .filled { fill: currentColor; stroke: none; }
.drawing-tools input:checked + span { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.drawing-tools input:focus-visible + span { outline: 3px solid var(--accent); outline-offset: 2px; }
@media (pointer: coarse) {
  .drawing-tools label > span { width: 44px; height: 44px; }
  .marks button { height: 44px; }
}

/* Marks for the page: a row of icon buttons, one pressed while it is being placed. */
.panel .marks-heading { margin-top: 22px; }
/* Six to a row, so none is left on a line of its own in the narrow sidebar. */
.marks { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 6px; max-width: 320px; }
.marks button {
  display: grid;
  place-items: center;
  height: 36px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
}
.marks button[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.marks button:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.mark-ink { margin-top: 10px; }
.pad {
  width: 100%;
  height: 220px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #ffffff;
  touch-action: none;
  cursor: crosshair;
}
/* A ring the size of what the eraser rubs out. */
.pad[data-tool="eraser"] {
  cursor: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20'%3E%3Ccircle cx='10' cy='10' r='8' fill='white' fill-opacity='.5' stroke='%23333' stroke-width='1.5'/%3E%3C/svg%3E") 10 10, cell;
}
.preview {
  display: grid;
  place-items: center;
  min-height: 120px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #ffffff;
  padding: 8px;
}
.preview canvas { max-width: 100%; max-height: 160px; }

.footer { max-width: 1200px; margin: 24px auto; padding: 0 16px; color: var(--muted); font-size: .8125rem; }
.footer a { color: inherit; }
