:root {
  --bg: #f4f4f2;
  --panel: #ffffff;
  --text: #1d1d1f;
  --muted: #6b6b70;
  --border: #d9d9d6;
  --accent: #e5484d;
  --accent-text: #ffffff;
  --sel: #2b6cff;
  --btn: #ffffff;
  --btn-hover: #ececea;
  --shadow: 0 1px 3px rgba(0,0,0,.12), 0 1px 2px rgba(0,0,0,.08);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #18181a;
    --panel: #232326;
    --text: #ececee;
    --muted: #9a9aa2;
    --border: #3a3a3f;
    --btn: #2c2c30;
    --btn-hover: #38383d;
    --sel: #5b8cff;
    --shadow: 0 1px 3px rgba(0,0,0,.5);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
}
[hidden] { display: none !important; }

button, select, input, textarea { font: inherit; color: inherit; }
button, select {
  background: var(--btn);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 10px;
  cursor: pointer;
  white-space: nowrap;
}
input:not([type=checkbox]):not([type=radio]):not([type=range]):not([type=color]), textarea {
  background: var(--btn);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 8px;
}
input[type=number] { width: 72px; }
button:hover:not(:disabled), select:hover { background: var(--btn-hover); }
button:disabled { opacity: .4; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--accent); }
button.active { background: var(--sel); border-color: var(--sel); color: #fff; }
button.linkish { background: none; border: 0; padding: 0; color: var(--sel); text-decoration: underline; }
code { font-size: 12px; background: var(--btn-hover); padding: 1px 4px; border-radius: 4px; }

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand { font-weight: 700; letter-spacing: -.01em; white-space: nowrap; }
.toolbar { display: flex; align-items: center; gap: 6px; flex: 1; flex-wrap: wrap; }
.sep { width: 1px; height: 22px; background: var(--border); margin: 0 4px; }
.grow { flex: 1; }
.label { min-width: 48px; text-align: center; color: var(--muted); font-variant-numeric: tabular-nums; }
.prop { display: inline-flex; align-items: center; }
input[type=color] { width: 34px; height: 30px; padding: 2px; border: 1px solid var(--border); border-radius: 6px; background: var(--btn); cursor: pointer; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* Tools menu */
.menu {
  margin: 0;
  padding: 6px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 8px 30px rgba(0,0,0,.2);
  min-width: 240px;
}
.menu:popover-open { display: flex; flex-direction: column; }
.menu button { border: 0; background: none; text-align: left; padding: 7px 10px; }
.menu button:hover { background: var(--btn-hover); }
.menu hr { border: 0; border-top: 1px solid var(--border); margin: 4px 0; width: 100%; }

main { flex: 1; min-height: 0; display: flex; flex-direction: column; }

/* Empty state */
.empty { flex: 1; display: grid; place-items: center; padding: 24px; }
.dropzone {
  max-width: 600px;
  text-align: center;
  padding: 48px 32px;
  border: 2px dashed var(--border);
  border-radius: 16px;
  background: var(--panel);
}
.dropzone h1 { margin: 0 0 8px; font-size: 24px; }
.task-hint { font-weight: 600; }
.empty { align-content: center; gap: 20px; }
.home-tasks .tool-links { justify-content: center; max-width: 640px; }

/* Landing pages */
html:has(body.doc), body.doc { height: auto; }
body.doc { display: block; }
a.brand { color: inherit; text-decoration: none; }
a.btn {
  display: inline-block;
  padding: 6px 14px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--btn);
  color: inherit;
  text-decoration: none;
  font-weight: 500;
}
a.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
a.btn.primary:hover { filter: brightness(1.08); }
a.btn.big { padding: 12px 26px; font-size: 17px; }
.doc-main { max-width: 760px; margin: 0 auto; padding: 32px 20px 48px; line-height: 1.6; }
.doc-main a:not(.btn) { color: var(--sel); }
.doc-main h1 { font-size: clamp(28px, 5vw, 40px); line-height: 1.15; margin: 8px 0 12px; letter-spacing: -.02em; }
.doc-main h2 { font-size: 20px; margin: 36px 0 10px; }
.hero { text-align: center; padding: 24px 0 8px; }
.hero .lede { font-size: 18px; color: var(--muted); max-width: 620px; margin: 0 auto 22px; }
.hero .small { margin-top: 12px; }
.steps { padding-left: 22px; }
.steps li { margin: 6px 0; }
.doc-main details { border: 1px solid var(--border); border-radius: 8px; padding: 10px 14px; margin: 8px 0; background: var(--panel); }
.doc-main details summary { cursor: pointer; font-weight: 600; }
.doc-main details p { margin: 8px 0 2px; }
.tool-links { list-style: none; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: 8px; }
.tool-links a { display: inline-block; padding: 6px 12px; border: 1px solid var(--border); border-radius: 999px; background: var(--panel); color: inherit; text-decoration: none; font-size: 13px; }
.tool-links a:hover { border-color: var(--sel); color: var(--sel); }
.doc-footer { border-top: 1px solid var(--border); text-align: center; color: var(--muted); font-size: 13px; padding: 20px; }
.doc-footer a { color: inherit; }

/* Organizer */
#organizer { padding: 12px 20px 40px; overflow: auto; flex: 1; }
.hint { color: var(--muted); margin: 4px 0 12px; font-size: 13px; }
.grid {
  position: relative;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 18px;
}
.thumb {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 10px;
  border-radius: 10px;
  border: 2px solid transparent;
  cursor: pointer;
  user-select: none;
}
.thumb:hover { background: var(--btn-hover); }
.thumb.selected { border-color: var(--sel); background: color-mix(in srgb, var(--sel) 10%, transparent); }
.thumb.dragging { opacity: .35; }
.thumb .frame { height: 200px; width: 100%; display: flex; align-items: center; justify-content: center; }
.thumb canvas { max-width: 100%; max-height: 200px; background: #fff; box-shadow: var(--shadow); }
.thumb .placeholder { width: 120px; height: 160px; background: #fff; box-shadow: var(--shadow); opacity: .6; }
.thumb .num { font-size: 12px; color: var(--muted); }
.thumb .badge { position: absolute; top: 4px; right: 6px; font-size: 11px; color: var(--sel); }
.thumb .badge.redact { color: var(--accent); right: 20px; }
.thumb .actions {
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  display: none;
  gap: 4px;
}
.thumb:hover .actions { display: flex; }
.thumb .actions button { padding: 2px 7px; font-size: 13px; box-shadow: var(--shadow); }
.drop-marker {
  position: absolute;
  width: 4px;
  border-radius: 2px;
  background: var(--sel);
  pointer-events: none;
}

/* Editor */
#editor { flex: 1; min-height: 0; display: flex; }
.palette {
  width: 92px;
  flex: none;
  overflow-y: auto;
  padding: 8px 6px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.palette .group { font-size: 10px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 8px 4px 2px; }
.palette button {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 6px 2px;
  border-color: transparent;
  background: none;
  font-size: 11px;
}
.palette button:hover:not(.active) { background: var(--btn-hover); }
.palette svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
.stage-wrap { flex: 1; overflow: auto; padding: 24px; }
.stage { position: relative; margin: 0 auto; box-shadow: var(--shadow); background: #fff; }
#pageCanvas, #stampCanvas { display: block; position: absolute; left: 0; top: 0; pointer-events: none; }
.annots { position: absolute; inset: 0; }
.layer { position: absolute; left: 0; top: 0; transform-origin: 0 0; overflow: hidden; touch-action: none; }
.layer:not(.tool-select) { cursor: crosshair; }
.layer.tool-text { cursor: text; }
.layer.tool-sign, .layer.tool-image, .layer.tool-date, .layer.tool-check, .layer.tool-cross { cursor: copy; }

.annot { position: absolute; }
.annot.svg { left: 0; top: 0; pointer-events: none; overflow: visible; }
.annot.svg .hit { pointer-events: stroke; stroke: transparent; fill: none; }
.annot.text { white-space: pre; line-height: 1.2; transform-origin: 0 0; min-width: 4px; outline: none; }
.annot.text[contenteditable=true], .annot.text[contenteditable=plaintext-only] { cursor: text; outline: 1px dashed var(--sel); background: rgba(43,108,255,.04); }
.annot.image { transform-origin: 0 0; }
.annot.image img { width: 100%; height: 100%; display: block; pointer-events: none; user-select: none; -webkit-user-drag: none; }
.annot.redact { background: #000; outline: 1.5px dashed #ff4d4f; outline-offset: 1px; }
.layer.tool-select .annot:not(.svg), .layer.tool-select .annot .hit { cursor: move; }
.layer.tool-select .annot.rect:hover { outline: 1px dashed #999; }
.annot.selected:not(.svg) { outline: 1.5px solid var(--sel); outline-offset: 1px; }
.annot.svg.selected path:not(.hit) { filter: drop-shadow(0 0 1.5px var(--sel)); }
.handle {
  position: absolute;
  background: #fff;
  border: 2px solid var(--sel);
  border-radius: 3px;
  cursor: nwse-resize;
  z-index: 2;
}

/* Dialogs */
dialog {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel);
  color: var(--text);
  padding: 0;
  width: min(640px, calc(100vw - 32px));
  box-shadow: 0 10px 40px rgba(0,0,0,.3);
}
dialog::backdrop { background: rgba(0,0,0,.4); }
.dlg { padding: 18px 20px; display: flex; flex-direction: column; gap: 12px; }
.dlg h2 { margin: 0; font-size: 18px; }
.dlg p { margin: 0; }
.dlg label { display: inline-flex; align-items: center; gap: 6px; }
.dlg label.stack { display: flex; flex-direction: column; align-items: stretch; gap: 4px; }
.dlg label.radio { display: flex; gap: 8px; }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.tabs { display: flex; gap: 4px; }
.wide { width: 100%; }
.hf-grid { display: grid; grid-template-columns: auto 1fr 1fr 1fr; gap: 6px 8px; align-items: center; }
.hf-grid input { min-width: 0; }
.sig-pad {
  width: 100%;
  height: 200px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 8px;
  touch-action: none;
  cursor: crosshair;
  display: block;
}
.sig-saved { display: flex; gap: 8px; flex-wrap: wrap; }
.sig-saved:empty { display: none; }
.sig-saved .item {
  position: relative;
  height: 64px;
  padding: 6px;
  background: #fff;
  border: 2px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
}
.sig-saved .item.current { border-color: var(--sel); }
.sig-saved .item img { height: 100%; display: block; }
.sig-saved .item .x { position: absolute; top: -8px; right: -8px; padding: 0 6px; border-radius: 10px; font-size: 12px; line-height: 18px; }
.sig-typed-preview {
  margin-top: 8px;
  height: 110px;
  background: #fff;
  color: #111;
  border: 1px solid var(--border);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font: 54px "Snell Roundhand", "Segoe Script", "Brush Script MT", cursive;
  overflow: hidden;
  white-space: nowrap;
}
.sig-image-wrap {
  min-height: 60px;
  border-radius: 8px;
  /* Checkerboard so transparency is visible */
  background: repeating-conic-gradient(#eee 0 25%, #fff 0 50%) 0 0 / 16px 16px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.sig-image-preview { max-height: 140px; max-width: 100%; }
.wm-image-preview { height: 40px; background: #fff; border: 1px solid var(--border); border-radius: 4px; }

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  background: #222;
  color: #fff;
  padding: 10px 16px;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0,0,0,.3);
  z-index: 100;
  pointer-events: none;
}
.drop-overlay {
  position: fixed;
  inset: 0;
  background: color-mix(in srgb, var(--sel) 18%, transparent);
  border: 4px dashed var(--sel);
  display: grid;
  place-items: center;
  font-size: 22px;
  font-weight: 600;
  z-index: 50;
  pointer-events: none;
}

@media (max-width: 700px) {
  .topbar { flex-direction: column; align-items: stretch; gap: 8px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 10px; }
  .thumb .frame { height: 150px; }
  .thumb canvas { max-height: 150px; }
  #editor { flex-direction: column; }
  .palette { width: auto; flex-direction: row; overflow-x: auto; border-right: 0; border-bottom: 1px solid var(--border); }
  .palette .group { display: none; }
  .stage-wrap { padding: 12px; }
}
