:root {
  --bg: #eceae3;
  --panel: #ffffff;
  --ink: #23201a;
  --muted: #7c766a;
  --line: #ddd8cc;
  --accent: #b5462f;
  --accent-ink: #fff;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 8px 24px rgba(0,0,0,.08);
  --radius: 10px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* Topbar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 20px;
  background: #fbfaf6;
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 14px; }
.logo { position: relative; font-size: 30px; line-height: 1; }
.logo .slash { position: absolute; right: -6px; bottom: -4px; font-size: 16px; }
.brand h1 { margin: 0; font-size: 18px; letter-spacing: -.02em; }
.brand p { margin: 2px 0 0; font-size: 12.5px; color: var(--muted); }
.brand-note { color: #2f7d4f !important; font-weight: 600; }
.topbar-actions { display: flex; gap: 10px; }

/* Buttons */
.btn {
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  padding: 9px 14px;
  border-radius: 8px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: background .15s, border-color .15s, transform .05s;
  user-select: none;
}
.btn:hover { background: #f4f2ec; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn-primary:hover { background: #9d3b27; }
.btn-ghost { background: transparent; }
.btn-block { width: 100%; }
.btn-danger { color: #fff; background: #a23; border-color: #a23; }
.btn-danger:hover { background: #8a1f2b; }

/* Layout */
.layout { flex: 1; display: grid; grid-template-columns: 320px 1fr; min-height: 0; }
.sidebar {
  background: #f6f4ee;
  border-right: 1px solid var(--line);
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
}
.panel h2 { margin: 0 0 12px; font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }

.preset-row { display: flex; gap: 6px; margin-bottom: 14px; }
.chip {
  flex: 1; font: inherit; font-size: 11.5px; font-weight: 600;
  padding: 7px 4px; border: 1px solid var(--line); background: #fff;
  border-radius: 999px; cursor: pointer; color: var(--muted);
}
.chip.is-active { background: var(--ink); color: #fff; border-color: var(--ink); }

.ctrl { margin-bottom: 12px; }
.ctrl label {
  display: flex; justify-content: space-between; align-items: baseline;
  font-size: 12.5px; font-weight: 600; margin-bottom: 5px;
}
.ctrl output { font-variant-numeric: tabular-nums; color: var(--accent); font-weight: 700; }
input[type=range] {
  width: 100%; -webkit-appearance: none; appearance: none;
  height: 4px; border-radius: 4px; background: var(--line); outline: none;
}
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 16px; height: 16px; border-radius: 50%;
  background: var(--accent); cursor: pointer; border: 2px solid #fff; box-shadow: 0 1px 3px rgba(0,0,0,.3);
}
select { font: inherit; font-size: 13px; width: 100%; padding: 7px; border: 1px solid var(--line); border-radius: 7px; background: #fff; }

.swatches { display: flex; gap: 8px; align-items: center; }
.swatch {
  width: 26px; height: 26px; border-radius: 50%; padding: 0; cursor: pointer;
  border: 2px solid #fff; box-shadow: 0 0 0 1px var(--line); background: #fff;
  position: relative; flex: none;
}
.swatch.is-active { box-shadow: 0 0 0 2px var(--accent); }
.swatch.orig { background: repeating-conic-gradient(#cfcabb 0 25%, #fff 0 50%) 50% / 12px 12px; }
.swatch.custom { overflow: hidden; background: conic-gradient(#f00, #ff0, #0f0, #0ff, #00f, #f0f, #f00); display: inline-flex; }
.swatch.custom input { position: absolute; inset: -6px; width: 160%; height: 160%; border: none; padding: 0; margin: 0; cursor: pointer; opacity: 0; }

.switch { display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--muted); cursor: pointer; }
.switch input { width: 15px; height: 15px; }
.switch.inline { margin-left: auto; }

.mark-actions { display: flex; flex-direction: column; gap: 8px; }
.hint { font-size: 11.5px; color: var(--muted); margin: 10px 0 0; line-height: 1.5; }

/* Stage */
.stage { display: flex; flex-direction: column; min-width: 0; }
.stage-toolbar {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; border-bottom: 1px solid var(--line); background: #fbfaf6;
  font-size: 13px; font-weight: 600;
}
#pageLabel { color: var(--muted); }
.canvas-outer { flex: 1; overflow: auto; padding: 28px; display: flex; align-items: flex-start; justify-content: center; }

.dropzone {
  width: 100%; max-width: 640px; margin-top: 6vh;
  border: 2px dashed var(--line); border-radius: 16px; background: #fbfaf6;
  padding: 60px 24px; text-align: center; transition: border-color .15s, background .15s;
}
.dropzone.drag { border-color: var(--accent); background: #fff; }
.dz-icon { font-size: 30px; margin-bottom: 14px; letter-spacing: 4px; }
.dropzone p { margin: 4px 0; }
.dz-sub { font-size: 12.5px; color: var(--muted); }

.canvas-wrap { position: relative; box-shadow: var(--shadow); background: #fff; line-height: 0; }
.canvas-wrap canvas { display: block; max-width: 100%; height: auto; }
.overlay-layer { position: absolute; inset: 0; }
/* Click-to-place mode: crosshair over the page, and let clicks fall through
   existing marks so a click anywhere lands on the layer. */
.overlay-layer.placing { cursor: crosshair; }
.overlay-layer.placing .overlay { pointer-events: none; }
.canvas-wrap.placing::after {
  content: "Click to place";
  position: absolute; top: 8px; left: 50%; transform: translateX(-50%);
  background: var(--accent); color: #fff; font-size: 12px; font-weight: 700;
  padding: 5px 12px; border-radius: 999px; pointer-events: none; z-index: 5;
}

/* Overlay marks */
.overlay {
  position: absolute; cursor: grab; touch-action: none;
  border: 1px dashed transparent;
}
.overlay img { width: 100%; height: 100%; display: block; pointer-events: none; }
.overlay.selected { border-color: var(--accent); }
.overlay:active { cursor: grabbing; }
.overlay .handle {
  position: absolute; width: 14px; height: 14px; right: -8px; bottom: -8px;
  background: var(--accent); border: 2px solid #fff; border-radius: 50%;
  cursor: nwse-resize; display: none;
}
.overlay.selected .handle { display: block; }

/* Modal */
.modal { position: fixed; inset: 0; background: rgba(20,18,14,.55); display: flex; align-items: center; justify-content: center; z-index: 50; }
.modal-card { background: #fff; border-radius: 14px; padding: 20px; box-shadow: var(--shadow); }
.modal-card h2 { margin: 0 0 14px; font-size: 16px; }
#sigPad { border: 1px solid var(--line); border-radius: 8px; background: #fffef9; cursor: crosshair; touch-action: none; max-width: 100%; }
.modal-actions { display: flex; align-items: center; gap: 10px; margin-top: 14px; }
.pen-controls { display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--muted); margin-right: auto; }
.pen-controls input { width: 90px; }

/* Toast */
.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 11px 18px; border-radius: 999px;
  font-size: 13px; font-weight: 600; box-shadow: var(--shadow); z-index: 60;
}

@media (max-width: 760px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { max-height: 42vh; }
}
