:root {
  --navy: #0a1628;
  --navy-mid: #112240;
  --navy-light: #1a3a5c;
  --blue: #1e5f9b;
  --blue-bright: #2e7bc4;
  --gold: #c9a84c;
  --gold-light: #e8c97a;
  --white: #ffffff;
  --text: #d0dce8;
  --text-dim: #7a90a8;
  --danger: #d9534f;
  --highlight: #ffeb3b;
}
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; }
body {
  font-family: 'Source Sans 3', 'Segoe UI', sans-serif;
  background: var(--navy);
  color: var(--text);
  overflow: hidden;
}
a { color: inherit; text-decoration: none; }
button { font-family: inherit; cursor: pointer; }

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

header.topbar {
  height: 56px; flex: none;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px;
  background: rgba(10,22,40,0.97);
  border-bottom: 1px solid rgba(201,168,76,0.2);
  z-index: 50;
}
.topbar-left { display: flex; align-items: center; gap: 14px; min-width: 0; }
.back-link { color: var(--text-dim); font-size: 0.85rem; white-space: nowrap; }
.back-link:hover { color: var(--gold); }
.file-name { font-size: 0.85rem; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 32vw; }
.topbar-right { display: flex; align-items: center; gap: 8px; }

.btn {
  border: 1px solid rgba(201,168,76,0.3);
  background: var(--navy-mid);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 4px;
  font-size: 0.82rem;
  font-weight: 600;
  display: inline-flex; align-items: center; gap: 6px;
  transition: border-color .15s, background .15s;
}
.btn:hover:not(:disabled) { border-color: var(--gold); }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn-icon { padding: 8px; }
.btn-gold {
  background: linear-gradient(135deg, var(--gold), var(--gold-light));
  color: var(--navy); border: none;
}
.btn-gold:hover { filter: brightness(1.05); }
.btn-danger { border-color: rgba(217,83,79,0.5); color: #f2a29e; }
.btn-danger:hover { border-color: var(--danger); }

/* ── Tool ribbon ── */
.ribbon {
  flex: none;
  display: flex; align-items: center; gap: 4px;
  padding: 8px 12px;
  background: var(--navy-mid);
  border-bottom: 1px solid rgba(201,168,76,0.15);
  overflow-x: auto;
}
.tool-btn {
  background: transparent; border: 1px solid transparent; color: var(--text-dim);
  padding: 7px 12px; border-radius: 4px; font-size: 0.78rem; font-weight: 600;
  display: flex; flex-direction: column; align-items: center; gap: 3px; min-width: 58px;
}
.tool-btn svg { width: 18px; height: 18px; stroke: currentColor; fill: none; }
.tool-btn:hover { color: var(--text); background: rgba(255,255,255,0.04); }
.tool-btn.active { color: var(--navy); background: var(--gold-light); }
.tool-btn.active svg { stroke: var(--navy); }
.ribbon-sep { width: 1px; height: 28px; background: rgba(201,168,76,0.2); margin: 0 6px; flex: none; }

.page-nav { display: flex; align-items: center; gap: 8px; margin-left: auto; flex: none; }
.page-nav input { width: 44px; text-align: center; background: var(--navy); border: 1px solid rgba(201,168,76,0.3); color: var(--text); border-radius: 4px; padding: 5px; font-size: 0.82rem; }

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

.sidebar {
  width: 168px; flex: none;
  background: var(--navy-mid);
  border-right: 1px solid rgba(201,168,76,0.15);
  overflow-y: auto;
  padding: 12px 10px;
}
.thumb {
  position: relative;
  background: #0d1e35;
  border: 2px solid transparent;
  border-radius: 5px;
  margin-bottom: 12px;
  cursor: grab;
  overflow: hidden;
  transition: border-color .15s;
}
.thumb.active { border-color: var(--gold); }
.thumb.drag-over { border-color: var(--blue-bright); }
.thumb canvas { display: block; width: 100%; height: auto; }
.thumb-label { position: absolute; bottom: 3px; left: 5px; font-size: 0.65rem; background: rgba(0,0,0,0.55); padding: 1px 5px; border-radius: 3px; color: var(--text); }
.thumb-actions { position: absolute; top: 3px; right: 3px; display: flex; gap: 3px; opacity: 0; transition: opacity .15s; }
.thumb:hover .thumb-actions { opacity: 1; }
.thumb-actions button {
  width: 20px; height: 20px; border: none; border-radius: 3px;
  background: rgba(10,22,40,0.85); color: var(--text); font-size: 0.7rem;
  display: flex; align-items: center; justify-content: center;
}
.thumb-actions button:hover { background: var(--gold); color: var(--navy); }

.canvas-area { flex: 1; overflow: auto; background: #05101f; display: flex; justify-content: center; padding: 32px 16px 90px; }

.page-wrap {
  position: relative;
  background: #fff;
  box-shadow: 0 8px 30px rgba(0,0,0,0.45);
  flex: none;
  height: fit-content;
}
.page-wrap canvas.render-canvas { display: block; width: 100%; height: 100%; }
.ann-layer { position: absolute; top: 0; left: 0; right: 0; bottom: 0; }
.ann-layer.tool-text, .ann-layer.tool-highlight, .ann-layer.tool-whiteout, .ann-layer.tool-draw { cursor: crosshair; }

/* ── Upload screen ── */
.upload-screen {
  flex: 1; display: flex; align-items: center; justify-content: center; flex-direction: column; gap: 18px;
  padding: 40px;
}
.dropzone {
  width: min(560px, 90vw);
  border: 2px dashed rgba(201,168,76,0.4);
  border-radius: 12px;
  padding: 60px 30px;
  text-align: center;
  transition: border-color .15s, background .15s;
}
.dropzone.drag { border-color: var(--gold); background: rgba(201,168,76,0.06); }
.dropzone h2 { font-family: 'Playfair Display', serif; color: var(--white); margin-bottom: 10px; font-size: 1.4rem; }
.dropzone p { color: var(--text-dim); font-size: 0.9rem; margin-bottom: 22px; }
.privacy-line { color: var(--text-dim); font-size: 0.8rem; max-width: 460px; text-align: center; }
.privacy-line strong { color: var(--gold-light); }

/* ── Annotation elements ── */
.ann {
  position: absolute;
  cursor: move;
  user-select: none;
}
.ann.selected { outline: 1.5px dashed var(--blue-bright); outline-offset: 2px; }
.ann-text {
  min-width: 20px; min-height: 20px;
  outline: none;
  white-space: pre-wrap;
  line-height: 1.15;
  padding: 2px;
}
.ann-text[contenteditable="true"] { cursor: text; background: rgba(46,123,196,0.08); }
.ann-text.placeholder-empty:before { content: attr(data-placeholder); color: #8a9bb0; font-style: italic; pointer-events: none; }
.ann-image img { width: 100%; height: 100%; display: block; pointer-events: none; }
.ann-rect { }
.ann svg.draw-svg { position: absolute; top: 0; left: 0; width: 100%; height: 100%; overflow: visible; pointer-events: none; }

.resize-handle {
  position: absolute; right: -6px; bottom: -6px;
  width: 12px; height: 12px; border-radius: 50%;
  background: var(--blue-bright); border: 2px solid #fff;
  cursor: nwse-resize;
}
.ann-toolbar {
  position: absolute; top: -34px; left: 0;
  display: flex; gap: 4px; background: var(--navy-mid);
  border: 1px solid rgba(201,168,76,0.3); border-radius: 5px; padding: 3px;
  white-space: nowrap; z-index: 30;
  pointer-events: auto; /* re-enable even when a draw-type parent sets pointer-events:none */
}
.ann-toolbar button {
  border: none; background: transparent; color: var(--text);
  width: 26px; height: 26px; border-radius: 3px; font-size: 0.85rem;
}
.ann-toolbar button:hover { background: rgba(255,255,255,0.08); }
.ann-toolbar label { display: flex; align-items: center; gap: 4px; font-size: 0.72rem; color: var(--text-dim); padding: 0 6px; }

/* ── Modal ── */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(5,10,20,0.72);
  display: flex; align-items: center; justify-content: center; z-index: 200;
}
.modal {
  background: var(--navy-mid); border: 1px solid rgba(201,168,76,0.25);
  border-radius: 10px; padding: 22px; width: min(480px, 92vw);
}
.modal h3 { font-family: 'Playfair Display', serif; color: var(--white); margin-bottom: 14px; font-size: 1.15rem; }
.modal-tabs { display: flex; gap: 8px; margin-bottom: 14px; }
.modal-tabs button {
  flex: 1; padding: 8px; border-radius: 4px; border: 1px solid rgba(201,168,76,0.25);
  background: transparent; color: var(--text-dim); font-size: 0.8rem; font-weight: 600;
}
.modal-tabs button.active { background: var(--gold); color: var(--navy); border-color: var(--gold); }
#sigCanvas { width: 100%; height: 180px; background: #fff; border-radius: 6px; touch-action: none; }
#sigTypeInput {
  width: 100%; padding: 14px; font-size: 1.6rem; font-family: 'Playfair Display', serif; font-style: italic;
  background: #fff; color: #111; border-radius: 6px; border: none; text-align: center;
}
.modal-actions { display: flex; justify-content: space-between; gap: 10px; margin-top: 16px; }
.stamp-presets { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 14px; }
.stamp-preset {
  border: 2px solid var(--danger); color: var(--danger); background: transparent;
  padding: 10px 4px; border-radius: 4px; font-weight: 700; font-size: 0.72rem; letter-spacing: 0.05em;
}
.stamp-preset:hover { background: rgba(217,83,79,0.1); }
.modal input[type="text"] {
  width: 100%; padding: 10px; border-radius: 5px; border: 1px solid rgba(201,168,76,0.3);
  background: var(--navy); color: var(--text); font-size: 0.9rem;
}
.modal-color-row { display: flex; gap: 8px; margin-top: 10px; }
.color-swatch { width: 26px; height: 26px; border-radius: 50%; border: 2px solid transparent; cursor: pointer; }
.color-swatch.selected { border-color: var(--gold-light); }

/* ── Toast ── */
#toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--navy-mid); border: 1px solid rgba(201,168,76,0.3);
  color: var(--text); padding: 10px 18px; border-radius: 6px; font-size: 0.85rem;
  z-index: 300; opacity: 0; pointer-events: none; transition: opacity .2s;
}
#toast.show { opacity: 1; }

/* ── Progress overlay ── */
#progressOverlay {
  position: fixed; inset: 0; background: rgba(5,10,20,0.85);
  display: none; align-items: center; justify-content: center; flex-direction: column; gap: 14px;
  z-index: 400; color: var(--text);
}
#progressOverlay.show { display: flex; }
.spinner {
  width: 36px; height: 36px; border: 3px solid rgba(201,168,76,0.25); border-top-color: var(--gold);
  border-radius: 50%; animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Hide native file input */
input[type="file"].hidden-input { display: none; }

@media (max-width: 720px) {
  .sidebar { width: 100px; }
  .file-name { max-width: 22vw; }
  .tool-btn { min-width: 46px; padding: 6px 8px; font-size: 0.68rem; }
}
