:root {
  --bg: #eef0f3;
  --surface: #ffffff;
  --surface-2: #f6f7f9;
  --border: #dcdfe4;
  --text: #1d2129;
  --muted: #6b7280;
  --accent: #b4650c;
  --accent-ink: #ffffff;
  --accent-soft: #fbefdf;
  --focus: #2563eb;
  --select: #2563eb;
  --redact: #d61f3a;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 2px 8px rgba(16, 24, 40, .08);
  --page-shadow: 0 1px 3px rgba(0, 0, 0, .18), 0 6px 18px rgba(0, 0, 0, .08);
  --radius: 8px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #17191d; --surface: #22252b; --surface-2: #2a2e35; --border: #383d46;
    --text: #e8eaed; --muted: #9aa1ac; --accent: #e39a3c; --accent-soft: #3a2a17;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 2px 8px rgba(0,0,0,.3);
    --page-shadow: 0 2px 10px rgba(0,0,0,.6);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text); background: var(--bg);
  display: grid; grid-template-rows: auto auto 1fr; overflow: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }
i[data-icon] { display: inline-flex; width: 18px; height: 18px; flex: none; }
i[data-icon] svg { width: 100%; height: 100%; }
.muted { color: var(--muted); }
[hidden] { display: none !important; }
body:not(.has-doc) [data-needs-doc] { display: none !important; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 10px; padding: 8px 12px;
  background: var(--surface); border-bottom: 1px solid var(--border); min-width: 0;
}
.brand { display: flex; align-items: center; gap: 8px; font-size: 16px; margin-right: 6px; color: inherit; text-decoration: none; white-space: nowrap; }
.brand-name { font-weight: 500; }
.brand-name b { font-weight: 750; }
.logo { width: 28px; height: 28px; display: grid; place-items: center; }
.logo img { width: 28px; height: 28px; display: block; }
.group { display: flex; align-items: center; gap: 2px; padding-left: 10px; border-left: 1px solid var(--border); }
.brand + .group { border-left: 0; padding-left: 0; }
.spacer { flex: 1; }
.btn, .icon-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid transparent; background: transparent; border-radius: 6px; cursor: pointer;
  padding: 6px 10px; white-space: nowrap;
}
.btn { border-color: var(--border); background: var(--surface); }
.btn:hover, .icon-btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.07); background: var(--accent); }
.btn.danger { color: #fff; background: var(--redact); border-color: var(--redact); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.lg { padding: 10px 16px; font-size: 15px; }
.btn.block { width: 100%; }
.btn:disabled, .icon-btn:disabled { opacity: .4; cursor: default; }
.icon-btn { padding: 6px; }
.icon-btn.sm { padding: 3px; }
.icon-btn.sm i[data-icon] { width: 16px; height: 16px; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }
.zoom-label { min-width: 58px; }
.page-nav input { width: 42px; text-align: center; padding: 4px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); }
.page-nav span { color: var(--muted); margin-left: 4px; }
.search { display: flex; align-items: center; gap: 4px; border: 1px solid var(--border); border-radius: 6px; padding: 2px 4px 2px 8px; background: var(--surface-2); }
.search input { border: 0; background: transparent; width: 160px; outline: none; padding: 3px 0; }
.search #searchCount { color: var(--muted); font-size: 12px; min-width: 34px; text-align: right; }

/* ---------- options bar ---------- */
.optionsbar {
  display: flex; align-items: center; gap: 14px; padding: 6px 14px; min-height: 42px;
  background: var(--surface); border-bottom: 1px solid var(--border); font-size: 13px; flex-wrap: wrap;
}
.tool-name { font-weight: 600; }
.tool-hint { color: var(--muted); }
.opts { display: flex; gap: 14px; align-items: center; margin-left: auto; flex-wrap: wrap; }
.opts label { display: flex; gap: 6px; align-items: center; color: var(--muted); }
.opts input[type=color] { width: 30px; height: 24px; padding: 0; border: 1px solid var(--border); border-radius: 4px; background: none; }
.opts input[type=number] { width: 56px; }
.opts input[type=number], .opts select { padding: 3px 4px; border: 1px solid var(--border); border-radius: 5px; background: var(--surface); color: var(--text); }
.toggles { gap: 2px !important; }
.toggle { width: 28px; height: 26px; border: 1px solid var(--border); background: var(--surface); border-radius: 5px; cursor: pointer; color: var(--text); }
.toggle.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

/* ---------- workspace ---------- */
.workspace { display: grid; grid-template-columns: auto auto 1fr auto; min-height: 0; }
.thumbs { width: 176px; background: var(--surface); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.thumbs-head { display: flex; align-items: center; justify-content: space-between; padding: 8px 8px 6px 12px; font-weight: 600; border-bottom: 1px solid var(--border); }
.thumb-list { overflow-y: auto; padding: 10px; display: flex; flex-direction: column; gap: 12px; align-items: center; }
.thumb { position: relative; cursor: pointer; text-align: center; font-size: 12px; color: var(--muted); user-select: none; }
.thumb .frame { border: 2px solid transparent; border-radius: 4px; padding: 2px; background: transparent; }
.thumb img { display: block; max-width: 128px; max-height: 170px; background: #fff; box-shadow: var(--shadow); }
.thumb.current .frame { border-color: var(--accent); }
.thumb.selected .frame { border-color: var(--select); background: rgba(37,99,235,.12); }
.thumb.drop-before::before, .thumb.drop-after::after { content: ""; position: absolute; left: 0; right: 0; height: 3px; background: var(--select); border-radius: 2px; }
.thumb.drop-before::before { top: -8px; }
.thumb.drop-after::after { bottom: -8px; }

.toolstrip { width: 52px; background: var(--surface); border-right: 1px solid var(--border); display: flex; flex-direction: column; align-items: center; padding: 8px 0; gap: 2px; overflow-y: auto; }
.toolstrip .sep { width: 28px; height: 1px; background: var(--border); margin: 6px 0; flex: none; }
.tool { width: 38px; height: 38px; border-radius: 8px; border: 0; background: transparent; cursor: pointer; display: grid; place-items: center; flex: none; }
.tool:hover { background: var(--surface-2); }
.tool.active { background: var(--accent-soft); color: var(--accent); }
.tool i[data-icon] { width: 20px; height: 20px; }

.viewer { overflow: auto; position: relative; min-width: 0; }
.pages { display: flex; flex-direction: column; align-items: center; gap: 18px; padding: 24px; min-width: max-content; }
.page {
  position: relative; background: #fff; box-shadow: var(--page-shadow); flex: none;
}
.page > img { position: absolute; inset: 0; width: 100%; height: 100%; user-select: none; -webkit-user-drag: none; }
.page .label { position: absolute; left: -2px; top: -18px; font-size: 11px; color: var(--muted); }
.overlay { position: absolute; inset: 0; touch-action: none; }
body[data-tool="select"] .overlay { cursor: default; }
body:not([data-tool="select"]):not([data-tool="edittext"]) .overlay { cursor: crosshair; }
body[data-tool="note"] .overlay { cursor: copy; }
.mark { position: absolute; pointer-events: none; }
.mark.redact { border: 1.5px solid var(--redact); background: rgba(214, 31, 58, .18); pointer-events: auto; cursor: pointer; }
.mark.redact:hover { background: rgba(214, 31, 58, .35); }
.mark.redact .x { position: absolute; right: -9px; top: -9px; width: 18px; height: 18px; border-radius: 50%; background: var(--redact); color: #fff; font-size: 12px; line-height: 18px; text-align: center; display: none; }
.mark.redact:hover .x { display: block; }
.mark.search { background: rgba(255, 200, 0, .4); mix-blend-mode: multiply; }
.mark.search.current { background: rgba(255, 120, 0, .55); outline: 2px solid #ff7a00; }
.mark.rubber { border: 1.5px dashed var(--select); background: rgba(37,99,235,.08); }
.mark.rubber.redact-draw { border-color: var(--redact); background: rgba(214,31,58,.15); }
.mark.textline { border: 1px dashed rgba(37,99,235,.55); pointer-events: auto; cursor: text; }
.mark.textline:hover { background: rgba(37,99,235,.1); border-style: solid; }
.mark.annot { pointer-events: auto; cursor: pointer; }
body[data-tool="select"] .mark.annot:hover { outline: 1.5px solid var(--select); }
.mark.annot.picked { outline: 2px solid var(--select); }
svg.draw { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; overflow: visible; }
.inline-editor {
  position: absolute; z-index: 5; margin: 0; padding: 0 1px; border: 1.5px solid var(--select); background: rgba(255,255,255,.97);
  resize: none; overflow: hidden; line-height: 1.15; color: #000; outline: none; white-space: pre; min-width: 40px;
}
.inline-editor.box { white-space: pre-wrap; }
.popover {
  position: absolute; z-index: 6; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 8px; display: flex; flex-direction: column; gap: 6px; width: 240px;
}
.popover textarea { width: 100%; min-height: 70px; border: 1px solid var(--border); border-radius: 5px; padding: 6px; background: var(--surface-2); }
.popover .row { display: flex; gap: 6px; justify-content: flex-end; }

.empty { position: absolute; inset: 0; display: grid; place-items: center; padding: 24px; }
body.has-doc .empty { display: none; }
.drop { text-align: center; max-width: 520px; padding: 48px 40px; border: 2px dashed var(--border); border-radius: 16px; background: var(--surface); }
body.dragging .drop { border-color: var(--accent); background: var(--accent-soft); }
.drop-icon { width: 64px; height: 64px; margin: 0 auto 12px; border-radius: 16px; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; }
.drop-icon i[data-icon] { width: 32px; height: 32px; }
.drop h1 { font-size: 22px; margin: 0 0 6px; }
.drop .row { display: flex; gap: 10px; justify-content: center; margin: 14px 0 18px; flex-wrap: wrap; }

/* ---------- right panel ---------- */
.panel { width: 320px; background: var(--surface); border-left: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
body.panel-closed .panel { display: none; }
.panel-head { display: flex; align-items: center; gap: 6px; padding: 8px 8px 8px 10px; border-bottom: 1px solid var(--border); }
.panel-head h2 { font-size: 15px; margin: 0; flex: 1; }
.panel-body { overflow-y: auto; padding: 12px 14px 24px; }
.tool-grid { display: grid; grid-template-columns: 1fr; gap: 4px; }
.tool-section { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 14px 2px 4px; font-weight: 600; }
.tool-section:first-child { margin-top: 0; }
.tool-card { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px; border: 0; background: transparent; cursor: pointer; text-align: left; width: 100%; }
.tool-card:hover { background: var(--surface-2); }
.tool-card .ic { width: 32px; height: 32px; border-radius: 8px; display: grid; place-items: center; flex: none; background: var(--surface-2); }
.tool-card .ic.red { background: #fde8eb; color: #c8102e; }
.tool-card .ic.blue { background: #e6efff; color: #1d4ed8; }
.tool-card .ic.green { background: #e3f6ea; color: #15803d; }
.tool-card .ic.purple { background: #f1e9ff; color: #7e22ce; }
.tool-card .ic.amber { background: #fff3dc; color: #b45309; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .tool-card .ic { filter: brightness(.85) saturate(1.2); }
}
.tool-card b { display: block; font-weight: 600; }
.tool-card small { color: var(--muted); }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.field > span { font-size: 12px; color: var(--muted); font-weight: 600; }
.field input[type=text], .field input[type=password], .field input[type=number], .field select, .field textarea {
  padding: 7px 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface-2); width: 100%;
}
.field textarea { min-height: 70px; resize: vertical; }
.check { display: flex; align-items: center; gap: 8px; margin: 6px 0; cursor: pointer; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.row3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 6px; }
.actions { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.note { font-size: 12.5px; color: var(--muted); background: var(--surface-2); border-radius: 6px; padding: 8px 10px; margin: 0 0 12px; }
.note.warn { background: #fff4e0; color: #8a4b00; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .note.warn { background: #3a2b12; color: #f5c27a; } }
.list { display: flex; flex-direction: column; gap: 4px; margin: 6px 0 10px; }
.list-item { display: flex; align-items: flex-start; gap: 8px; padding: 7px 8px; border-radius: 6px; background: var(--surface-2); cursor: pointer; font-size: 13px; }
.list-item:hover { outline: 1px solid var(--border); }
.list-item .grow { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.list-item .meta { color: var(--muted); font-size: 11.5px; }
.badge { display: inline-block; padding: 1px 7px; border-radius: 10px; background: var(--accent-soft); color: var(--accent); font-size: 12px; font-weight: 600; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.chip { border: 1px solid var(--border); border-radius: 14px; padding: 3px 10px; cursor: pointer; background: var(--surface); font-size: 12.5px; }
.chip.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.diff-old { background: rgba(214,31,58,.12); color: #b4152d; text-decoration: line-through; padding: 1px 4px; border-radius: 3px; display: block; }
.diff-new { background: rgba(21,128,61,.12); color: #15803d; padding: 1px 4px; border-radius: 3px; display: block; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .diff-old { color: #ff8a9a; } :root:not([data-theme="light"]) .diff-new { color: #6ee7a0; } }
hr.sep { border: 0; border-top: 1px solid var(--border); margin: 16px 0; }

/* ---------- modal, toasts, busy ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.4); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal { background: var(--surface); border-radius: 12px; box-shadow: 0 20px 50px rgba(0,0,0,.3); padding: 18px; width: min(560px, 100%); max-height: 90vh; overflow: auto; }
.modal h3 { margin: 0 0 12px; font-size: 17px; }
.sig-pad { width: 100%; height: 180px; border: 1px dashed var(--border); border-radius: 8px; background: #fff; touch-action: none; cursor: crosshair; display: block; }
.toast-wrap { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 60; align-items: center; }
.toast { background: #1f2329; color: #fff; padding: 9px 14px; border-radius: 8px; box-shadow: var(--shadow); font-size: 13.5px; max-width: 520px; }
.toast.error { background: #b4152d; }
.busy { position: fixed; inset: 0; background: rgba(255,255,255,.35); display: flex; align-items: center; justify-content: center; gap: 10px; z-index: 70; font-weight: 600; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .busy { background: rgba(0,0,0,.35); } }
.spinner { width: 22px; height: 22px; border: 3px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- responsive ---------- */
@media (max-width: 1100px) {
  .topbar .btn span, .search { display: none; }
  .topbar .btn.primary span { display: inline; }
}
@media (max-width: 860px) {
  .thumbs { display: none; }
  .panel { position: fixed; right: 0; top: 0; bottom: 0; z-index: 40; width: min(340px, 100vw); box-shadow: var(--shadow); }
  .group.page-nav, .brand-name { display: none; }
  .topbar { gap: 6px; padding: 6px 8px; overflow-x: auto; }
  .pages { padding: 16px; }
}

/* ---------- branding, about, engine status ---------- */
.credit { font-size: 12.5px; color: var(--muted); margin: 14px 0 0; }
.credit a, .about a { color: var(--accent); }
.engine-status { display: flex; align-items: center; justify-content: center; gap: 8px; color: var(--muted); font-size: 13px; }
.spinner.sm { width: 14px; height: 14px; border-width: 2px; }
.about { text-align: center; }
.about img { width: 64px; height: 64px; }
.about h3 { margin: 8px 0 2px; }
.about .ver { color: var(--muted); font-size: 13px; margin-bottom: 14px; }
.about .links { display: flex; flex-wrap: wrap; gap: 6px 14px; justify-content: center; margin: 14px 0 4px; font-size: 13.5px; }
.about .legal { font-size: 11.5px; color: var(--muted); margin-top: 14px; line-height: 1.5; }

/* Pin layout slots: before a document is open the option bar, thumbnails,
   tool strip and panel are hidden, and auto-placement would otherwise squeeze
   the viewer into a zero-size cell (blank start screen). */
.topbar { grid-row: 1; }
.optionsbar { grid-row: 2; }
.workspace { grid-row: 3; }
.thumbs { grid-column: 1; }
.toolstrip { grid-column: 2; }
.viewer { grid-column: 3; }
.panel { grid-column: 4; }

.engine-status.error { color: var(--redact); }
