:root {
  --bg-header: #1f2430;
  --accent: #4f7cff;
  --accent-dark: #3a5fd9;
  --danger: #e5534b;
  --danger-bg: #fdecea;
  --open: #f0ad4e;
  --resolved: #3fb27f;
  --text: #24292f;
  --muted: #6b7280;
  --border: #e2e5e9;
  --radius-pill: 999px;
  --radius-card: 10px;
}

* { box-sizing: border-box; }
html, body {
  height: 100%;
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  color: var(--text);
}

#app { height: 100%; }

#auth-gate {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  /* Same dark surface as <header>, subtly deepened at the edges -- the
     gate reads as the same product, not a bolted-on login page. */
  background:
    radial-gradient(80% 60% at 50% 0%, rgba(79,124,255,.16), transparent 60%),
    var(--bg-header);
}
/* [hidden] is only (0,1,0) specificity -- an ID selector like #auth-gate
   setting display:flex would otherwise win over the UA's display:none
   and defeat `element.hidden = true` outright. */
#auth-gate[hidden], #app[hidden] { display: none; }
#auth-gate-form {
  width: 340px;
  background: #fff;
  border-radius: var(--radius-card);
  padding: 34px 30px;
  box-shadow: 0 20px 60px rgba(0,0,0,.45);
  text-align: center;
}
.auth-gate-icon {
  width: 44px;
  height: 44px;
  margin: 0 auto 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: rgba(79,124,255,.12);
  font-size: 1.15em;
}
#auth-gate-form h1 {
  font-size: 1.05em;
  font-weight: 600;
  margin: 0 0 8px;
  color: var(--text);
}
#auth-gate-form p { color: var(--muted); font-size: 0.88em; line-height: 1.4; margin: 0 0 20px; }
#auth-gate-input {
  width: 100%;
  font: inherit;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-pill);
  margin-bottom: 14px;
  text-align: center;
  transition: border-color .15s ease, box-shadow .15s ease;
}
#auth-gate-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(79,124,255,.18);
}
#auth-gate-form button {
  width: 100%;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  padding: 10px 0;
}
#auth-gate-form button:hover:not(:disabled) { background: var(--accent-dark); }
#auth-gate-error {
  color: var(--danger);
  font-size: 0.85em;
  min-height: 1.2em;
  margin: 14px 0 0;
}
#auth-gate-error:not(:empty) {
  background: var(--danger-bg);
  border-radius: var(--radius-card);
  padding: 8px 10px;
}

button {
  font: inherit;
  cursor: pointer;
  border: none;
  border-radius: var(--radius-pill);
  padding: 6px 16px;
  background: #eef0f3;
  color: var(--text);
  transition: background .15s ease, color .15s ease, transform .1s ease;
}
button:hover:not(:disabled) { background: #e2e5eb; }
button:active:not(:disabled) { transform: scale(0.97); }
button:disabled { opacity: 0.5; cursor: default; }

header {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 18px;
  background: var(--bg-header);
  color: #fff;
}
header select {
  font: inherit;
  padding: 6px 14px;
  border-radius: var(--radius-pill);
  border: 1px solid rgba(255,255,255,.2);
  background: rgba(255,255,255,.08);
  color: #fff;
}
header select option { color: #000; }
header input {
  font: inherit;
  padding: 5px 12px;
  border-radius: var(--radius-pill);
  border: 1px solid rgba(255,255,255,.2);
  background: rgba(255,255,255,.08);
  color: #fff;
}
header input::placeholder { color: rgba(255,255,255,.6); }
.author-field { display: flex; align-items: center; gap: 6px; font-size: 0.9em; color: rgba(255,255,255,.8); }

#doc-meta {
  font-size: 0.85em;
  color: rgba(255,255,255,.75);
  padding: 4px 12px;
  border-radius: var(--radius-pill);
  background: rgba(255,255,255,.08);
  margin-left: auto;
}
#doc-meta .status-draft { color: #ffb46b; }
#doc-meta .status-in-review { color: #7fb4ff; }
#doc-meta .status-approved { color: #7fe0ab; }

#mode-btn {
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  padding: 7px 20px;
}
#mode-btn:hover:not(:disabled) { background: var(--accent-dark); }
#mode-btn.active { background: var(--danger); }
#mode-btn.active:hover:not(:disabled) { background: #c9433c; }

#status-msg { color: #ffc861; font-size: 0.9em; }

#body {
  display: flex;
  height: calc(100vh - 56px);
}

#viewer-container {
  flex: 1;
  overflow: auto;
  background: #5b5f66;
  padding: 24px;
}
#viewer { display: flex; flex-direction: column; align-items: center; gap: 18px; }

.page {
  position: relative;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px rgba(0,0,0,.35);
}
.page.comment-mode { cursor: crosshair; }
.page canvas { display: block; border-radius: 4px; }

.textLayer {
  position: absolute;
  inset: 0;
  overflow: clip;
  opacity: 1;
  line-height: 1;
  transform-origin: 0 0;
}
.textLayer span, .textLayer br {
  color: transparent;
  position: absolute;
  white-space: pre;
  cursor: text;
  transform-origin: 0% 0%;
}

.markerLayer { position: absolute; inset: 0; pointer-events: none; }
.marker {
  position: absolute;
  width: 16px;
  height: 16px;
  margin-left: -8px;
  margin-top: -8px;
  border-radius: 50% 50% 50% 0;
  transform: rotate(-45deg);
  background: var(--open);
  border: 1px solid #8a6317;
  box-shadow: 0 1px 3px rgba(0,0,0,.4);
  pointer-events: auto;
  cursor: pointer;
  transition: transform .1s ease;
}
.marker:hover { transform: rotate(-45deg) scale(1.15); }
.marker.resolved { background: var(--resolved); border-color: #227a55; }
/* Position recomputed from another document's comment — dashed to signal
   "same content, reviewed elsewhere". */
.marker.foreign { border-style: dashed; }
.marker.highlight { outline: 3px solid var(--accent); }

.range-highlight {
  position: absolute;
  background: rgba(240, 173, 78, 0.35);
  mix-blend-mode: multiply; /* keeps canvas text crisp under the tint */
  border-radius: 2px;
  pointer-events: auto;
  cursor: pointer;
}
.range-highlight.resolved { background: rgba(63, 178, 127, 0.30); }
.range-highlight.foreign { outline: 1px dashed var(--accent); }
/* Preview of the range being commented while the popup is open. */
.range-highlight.pending { background: rgba(79, 124, 255, 0.40); pointer-events: none; }
.range-highlight.highlight { background: rgba(79, 124, 255, 0.35); outline: 2px solid var(--accent); }

/* In comment mode, highlights must not steal the drag from the text
   layer — otherwise already-highlighted text can't be re-selected. */
.page.comment-mode .range-highlight { pointer-events: none; }
.page.comment-mode .textLayer ::selection { background: rgba(79, 124, 255, 0.40); }

.popup {
  position: fixed;
  z-index: 1000;
  background: #fff;
  border: 1px solid var(--border);
  box-shadow: 0 8px 24px rgba(0,0,0,.25);
  padding: 12px;
  width: 260px;
  border-radius: var(--radius-card);
}
.popup textarea {
  width: 100%;
  height: 64px;
  font: inherit;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px;
  resize: vertical;
}
.popup .quote-preview {
  font-size: 0.8em;
  color: var(--muted);
  border-left: 3px solid var(--border);
  padding-left: 8px;
  margin-bottom: 8px;
  max-height: 3.6em;
  overflow: hidden;
}
.popup .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
.popup .actions button:last-child { background: var(--accent); color: #fff; }
.popup .actions button:last-child:hover:not(:disabled) { background: var(--accent-dark); }

#sidebar {
  width: 360px;
  border-left: 1px solid var(--border);
  overflow-y: auto;
  padding: 14px 16px;
  background: #f7f8fa;
}
#sidebar h2 {
  font-size: 0.95em;
  font-weight: 600;
  margin: 4px 0 12px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
#comment-list { list-style: none; margin: 0; padding: 0; }

.comment-item {
  border: 1px solid var(--border);
  border-left-width: 4px;
  border-left-color: var(--open);
  border-radius: var(--radius-card);
  padding: 10px 12px;
  margin-bottom: 10px;
  background: #fff;
  font-size: 0.85em;
  box-shadow: 0 1px 3px rgba(0,0,0,.05);
}
.comment-item.resolved { border-left-color: var(--resolved); opacity: 0.75; }
.comment-item.stale { background: #fff8e6; }
/* Owned by another document that \input's the same source (source_doc). */
.comment-item.foreign { border-left-style: dashed; }
/* Foreign comment we couldn't place on this PDF: no marker to jump to. */
.comment-item.unlocated .loc { color: var(--muted); font-style: italic; }
.comment-item .meta { color: var(--muted); font-size: 0.85em; margin-bottom: 3px; }
.comment-item .origin-tag { color: var(--accent); }
.comment-item .loc { color: #555; font-size: 0.85em; margin-bottom: 6px; }
.comment-item .loc.stale-warning { color: var(--danger); font-weight: 600; }
.comment-item blockquote {
  margin: 0 0 6px;
  padding-left: 8px;
  border-left: 3px solid var(--border);
  color: var(--muted);
  font-style: italic;
}
.comment-item .text { margin-bottom: 8px; white-space: pre-wrap; }
.comment-item .actions { display: flex; gap: 6px; }
.comment-item .actions button { font-size: 0.85em; padding: 4px 12px; }
.comment-item .actions button:last-child { background: var(--danger-bg); color: var(--danger); }
.comment-item .actions button:last-child:hover:not(:disabled) { background: #f9d7d4; }
