/* ---------- Theme ---------- */
:root {
  --ink: #15171c;
  --ink-2: #1d2027;
  --ink-3: #2a2e38;
  --paper: #f3f1ea;
  --paper-edge: #e4e0d4;
  --line: #d8d4c7;
  --text: #1a1c20;
  --muted: #6b6f78;
  --on-dark: #e9e7df;
  --on-dark-mute: #9aa0ab;
  --accent: #ff5a3c;
  --accent-deep: #e23d20;
  --blue: #3b82f6;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 8px 24px rgba(0,0,0,.10);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: "Hanken Grotesk", system-ui, sans-serif;
  color: var(--text);
  background: var(--ink);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

button { font-family: inherit; cursor: pointer; }

/* ---------- App shell ---------- */
#app { display: flex; flex-direction: column; height: 100vh; }

header {
  display: flex; align-items: center; gap: 16px;
  padding: 0 18px; height: 56px;
  background: var(--ink);
  border-bottom: 1px solid #000;
  color: var(--on-dark);
  flex: 0 0 auto;
}
.wordmark { display: flex; align-items: baseline; gap: 8px; letter-spacing: .3px; }
.wordmark b { font-weight: 700; font-size: 17px; }
.wordmark .ser {
  font-family: "Instrument Serif", serif;
  font-size: 21px; font-style: italic; color: var(--accent);
  transform: translateY(1px);
}
.filename {
  color: var(--on-dark-mute); font-size: 13px;
  max-width: 40vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.spacer { flex: 1; }

.hbtn {
  height: 34px; padding: 0 14px; border-radius: 8px;
  border: 1px solid var(--ink-3); background: var(--ink-2);
  color: var(--on-dark); font-size: 13px; font-weight: 600;
  display: inline-flex; align-items: center; gap: 7px;
  transition: background .15s, border-color .15s;
}
.hbtn:hover { background: var(--ink-3); }
.hbtn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.hbtn.primary:hover { background: var(--accent-deep); }
.hbtn:disabled { opacity: .4; cursor: not-allowed; }

/* ---------- Body layout ---------- */
.body { flex: 1; display: flex; min-height: 0; }

/* Tool rail */
.rail {
  width: 64px; flex: 0 0 64px; background: var(--ink-2);
  border-right: 1px solid #000; padding: 10px 0;
  display: flex; flex-direction: column; align-items: center; gap: 6px;
}
.tool {
  width: 46px; height: 46px; border-radius: 10px;
  border: 1px solid transparent; background: transparent;
  color: var(--on-dark-mute); display: grid; place-items: center;
  transition: all .14s;
}
.tool svg { width: 21px; height: 21px; }
.tool:hover { background: var(--ink-3); color: var(--on-dark); }
.tool.active { background: var(--accent); color: #fff; box-shadow: 0 4px 12px rgba(255,90,60,.4); }
.tool small {
  position: absolute; transform: translateY(20px); font-size: 9px;
}

/* Options panel */
.options {
  width: 0; flex: 0 0 auto; background: var(--ink-2);
  border-right: 1px solid #000; color: var(--on-dark);
  overflow: hidden; transition: width .2s ease;
}
.options.open { width: 220px; padding: 16px; }
.options h4 { font-size: 11px; text-transform: uppercase; letter-spacing: 1.2px;
  color: var(--on-dark-mute); margin-bottom: 14px; }
.opt-row { margin-bottom: 16px; }
.opt-row label { display: block; font-size: 12px; color: var(--on-dark-mute); margin-bottom: 7px; }
.swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.sw { width: 26px; height: 26px; border-radius: 7px; border: 2px solid transparent; }
.sw.sel { border-color: #fff; box-shadow: 0 0 0 2px var(--ink-2), 0 0 0 3px #fff; }
input[type=range] { width: 100%; accent-color: var(--accent); }
.sizeval { font-size: 12px; color: var(--on-dark); float: right; }
.hint { font-size: 11.5px; line-height: 1.5; color: var(--on-dark-mute);
  background: var(--ink); padding: 10px; border-radius: 8px; }

/* Canvas / document area */
.stage {
  flex: 1; background:
    radial-gradient(circle at 1px 1px, rgba(0,0,0,.05) 1px, transparent 0) 0 0 / 22px 22px,
    var(--paper);
  overflow: auto; padding: 30px 20px 80px;
}
.stage.tool-text   { cursor: text; }
.stage.tool-rect, .stage.tool-white, .stage.tool-draw { cursor: crosshair; }

.empty {
  height: 100%; display: grid; place-items: center; color: var(--muted);
}
.drop {
  width: min(440px, 90%); text-align: center; padding: 48px 30px;
  border: 2px dashed var(--line); border-radius: 16px; background: #fff;
  box-shadow: var(--shadow); transition: border-color .15s, transform .15s;
}
.drop.hot { border-color: var(--accent); transform: scale(1.01); }
.drop h2 { font-size: 22px; color: var(--text); margin-bottom: 8px; font-weight: 700; }
.drop p { font-size: 14px; margin-bottom: 20px; }
.drop .pick {
  background: var(--ink); color: #fff; border: none; padding: 11px 22px;
  border-radius: 9px; font-weight: 600; font-size: 14px;
}

#pages { display: flex; flex-direction: column; align-items: center; gap: 26px; }

.page-wrap { position: relative; }
.page-card {
  position: relative; background: #fff;
  box-shadow: 0 1px 2px rgba(0,0,0,.08), 0 12px 30px rgba(0,0,0,.13);
  border-radius: 2px;
}
.page-card canvas { display: block; border-radius: 2px; }
.olayer { position: absolute; inset: 0; }            /* SVG vector overlays */
.dlayer { position: absolute; inset: 0; }            /* DOM text/image overlays */
.dlayer > * { pointer-events: auto; }

/* page toolbar */
.page-bar {
  position: absolute; top: -14px; right: 8px;
  display: flex; gap: 4px; background: var(--ink);
  padding: 4px; border-radius: 9px; box-shadow: var(--shadow);
  opacity: 0; transition: opacity .15s;
}
.page-wrap:hover .page-bar { opacity: 1; }
.page-bar button {
  width: 30px; height: 28px; border: none; background: transparent;
  color: var(--on-dark-mute); border-radius: 6px; display: grid; place-items: center;
}
.page-bar button:hover { background: var(--ink-3); color: #fff; }
.page-bar button svg { width: 16px; height: 16px; }
.page-num {
  position: absolute; bottom: -22px; left: 50%; transform: translateX(-50%);
  font-size: 11px; color: var(--muted); letter-spacing: .5px;
}

/* text overlay */
.ov-text {
  position: absolute; min-width: 8px; outline: none; white-space: pre;
  line-height: 1; font-family: Helvetica, Arial, sans-serif;
  border: 1px solid transparent;
}
.ov-text:focus, .ov-text.sel { border: 1px dashed var(--blue); background: rgba(59,130,246,.06); }

/* image overlay */
.ov-img { position: absolute; }
.ov-img img { width: 100%; height: 100%; display: block; -webkit-user-drag: none; }
.ov-img.sel { outline: 1.5px solid var(--blue); }
.handle {
  position: absolute; right: -6px; bottom: -6px; width: 13px; height: 13px;
  background: #fff; border: 2px solid var(--blue); border-radius: 3px; cursor: nwse-resize;
  display: none;
}
.ov-img.sel .handle { display: block; }
.del-dot {
  position: absolute; top: -9px; right: -9px; width: 18px; height: 18px;
  background: var(--accent); color: #fff; border-radius: 50%; border: none;
  font-size: 12px; line-height: 1; display: none; place-items: center;
}
.ov-img.sel .del-dot, .ov-text.sel + .del-dot { display: grid; }

/* zoom */
.zoom {
  position: absolute; bottom: 18px; left: 50%; transform: translateX(-50%);
  display: flex; gap: 2px; background: var(--ink); border-radius: 10px;
  padding: 4px; box-shadow: var(--shadow);
}
.zoom button { width: 34px; height: 30px; border: none; background: transparent;
  color: var(--on-dark); font-size: 16px; border-radius: 7px; }
.zoom button:hover { background: var(--ink-3); }
.zoom span { color: var(--on-dark-mute); font-size: 12px; display: grid; place-items: center;
  min-width: 46px; }

/* toast */
#toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--ink); color: #fff; padding: 12px 18px; border-radius: 10px;
  font-size: 13px; opacity: 0; pointer-events: none; transition: all .25s; z-index: 50;
  box-shadow: var(--shadow);
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.err { background: var(--accent-deep); }

/* signature modal */
.modal-bg {
  position: fixed; inset: 0; background: rgba(10,10,12,.55); display: none;
  place-items: center; z-index: 60;
}
.modal-bg.open { display: grid; }
.modal {
  background: #fff; border-radius: 14px; padding: 22px; width: min(520px, 92vw);
  box-shadow: var(--shadow);
}
.modal h3 { font-size: 17px; margin-bottom: 4px; }
.modal p { font-size: 13px; color: var(--muted); margin-bottom: 14px; }
#sigPad { width: 100%; height: 200px; border: 1px solid var(--line);
  border-radius: 10px; background: #fbfbf9; touch-action: none; cursor: crosshair; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.mbtn { padding: 9px 16px; border-radius: 8px; font-size: 13px; font-weight: 600; border: 1px solid var(--line); background: #fff; }
.mbtn.primary { background: var(--ink); color: #fff; border-color: var(--ink); }
.busy { position: fixed; inset: 0; background: rgba(243,241,234,.6); display: none;
  place-items: center; z-index: 70; }
.busy.open { display: grid; }
.spinner { width: 38px; height: 38px; border: 3px solid var(--line);
  border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
