:root {
  --bg: #0b1220;
  --surface: #111a2e;
  --surface-2: #17233c;
  --surface-hover: #1e2d4d;
  --border: #273758;
  --text: #eef3fb;
  --text-muted: #9eb0cf;
  --accent: #38bdf8;
  --accent-strong: #0284c7;
  --danger: #f87171;
  --ok: #34d399;
  --warn: #fbbf24;
  --kw0-bg: rgba(250, 204, 21, 0.28);
  --kw0-bd: #facc15;
  --kw1-bg: rgba(56, 189, 248, 0.26);
  --kw1-bd: #38bdf8;
  --kw2-bg: rgba(52, 211, 153, 0.26);
  --kw2-bd: #34d399;
  --kw3-bg: rgba(244, 114, 182, 0.26);
  --kw3-bd: #f472b6;
  --radius: 10px;
  --shadow: 0 12px 32px rgba(0, 0, 0, 0.36);
}

body[data-theme="light"] {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --surface-hover: #e2e8f0;
  --border: #cbd5e1;
  --text: #0f172a;
  --text-muted: #475569;
  --accent: #0284c7;
  --accent-strong: #0369a1;
  --danger: #dc2626;
  --ok: #059669;
  --warn: #d97706;
  --shadow: 0 10px 28px rgba(15, 23, 42, 0.12);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Pretendard", "Apple SD Gothic Neo", "Noto Sans KR", sans-serif;
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

.hidden {
  display: none !important;
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: 8px;
  padding: 6px 11px;
  min-height: 34px;
  transition: background 0.15s ease, border-color 0.15s ease;
}

button:hover:not(:disabled) {
  background: var(--surface-hover);
  border-color: var(--accent);
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent-strong);
  border-color: var(--accent);
  color: #ffffff;
  font-weight: 600;
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent);
  color: #04101d;
}

.btn-ghost {
  background: transparent;
}

.btn-small {
  min-height: 30px;
  padding: 4px 9px;
  font-size: 12.5px;
}

.btn-link {
  display: inline-flex;
  align-items: center;
  text-decoration: none;
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: 8px;
  color: var(--text);
}

.btn-link:hover {
  border-color: var(--accent);
  background: var(--surface-hover);
}

.btn-block {
  width: 100%;
  min-height: 42px;
}

.btn-pill.active,
.btn-toggle.active,
.side-tab.active,
.admin-tab.active {
  background: var(--accent-strong);
  border-color: var(--accent);
  color: #ffffff;
  font-weight: 600;
}

.btn-clear {
  border: none;
  background: transparent;
  font-size: 20px;
  line-height: 1;
  min-height: 30px;
  padding: 2px 8px;
  color: var(--text-muted);
}

/* ── Topbar ── */
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 8px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 20;
}

.topbar-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  justify-content: space-between;
}

.brand-group {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 4px 8px;
  border-radius: 7px;
  background: linear-gradient(135deg, #0284c7, #0ea5e9);
  color: #fff;
  font-weight: 800;
  font-size: 12px;
  letter-spacing: 0.06em;
}

.brand-title {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
}

.brand-sub {
  font-size: 12px;
  color: var(--text-muted);
}

.topbar-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

.mode-switch {
  display: inline-flex;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 2px;
  gap: 2px;
}

.mode-switch .btn-pill {
  border: none;
  background: transparent;
  min-height: 28px;
  padding: 4px 10px;
  font-size: 12.5px;
}

.user-pill {
  padding: 4px 9px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: 12px;
  color: var(--accent);
}

/* ── Search & Builder ── */
.search-row {
  justify-content: flex-start;
  gap: 10px;
}

.search-form {
  display: flex;
  flex: 1 1 420px;
  gap: 6px;
  min-width: 240px;
}

.search-input-wrap {
  position: relative;
  flex: 1;
  display: flex;
  align-items: center;
}

.search-input-wrap input {
  width: 100%;
  min-height: 38px;
  padding: 7px 34px 7px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
}

.search-input-wrap input:focus {
  outline: 2px solid var(--accent);
  border-color: var(--accent);
}

.search-input-wrap .btn-clear {
  position: absolute;
  right: 4px;
}

.search-options {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

.select-compact {
  min-height: 36px;
  padding: 4px 8px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  font-size: 12.5px;
}

.builder-row,
.docs-filter-row {
  justify-content: flex-start;
}

.kw-builder {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  width: 100%;
}

.kw-quick-input {
  width: 175px;
  min-height: 30px;
  padding: 4px 9px;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: var(--bg);
  font-size: 12.5px;
}

.btn-chip-add {
  min-height: 30px;
  padding: 3px 8px;
  font-size: 12px;
}

.btn-chip-add.op-and {
  border-color: rgba(56, 189, 248, 0.45);
}

.btn-chip-add.op-or {
  border-color: rgba(52, 211, 153, 0.45);
}

.btn-chip-add.op-not {
  border-color: rgba(248, 113, 113, 0.45);
}

.kw-chips-list {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}

.kw-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  border: 1px solid var(--border);
  background: var(--surface-2);
}

.kw-chip.is-neg {
  border-color: var(--danger);
  color: var(--danger);
}

.kw-chip.is-or {
  border-color: var(--ok);
}

.kw-chip-del {
  border: none;
  background: transparent;
  min-height: 18px;
  padding: 0 2px;
  font-size: 14px;
  line-height: 1;
}

.doc-chips {
  display: flex;
  flex-wrap: nowrap;
  overflow-x: auto;
  gap: 6px;
  padding-bottom: 2px;
  width: 100%;
}

.doc-chip {
  flex: 0 0 auto;
  min-height: 29px;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  white-space: nowrap;
}

.doc-chip.active {
  background: var(--accent-strong);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

/* ── Main Workbench ── */
.app-main {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.sync-banner {
  background: rgba(251, 191, 36, 0.14);
  border-bottom: 1px solid rgba(251, 191, 36, 0.45);
  color: var(--warn);
  padding: 8px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  font-size: 13px;
}

/* ── Grid Mode ── */
.grid-view {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 12px 14px;
  overflow-y: auto;
}

.grid-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
}

.grid-summary {
  font-size: 13.5px;
  font-weight: 600;
}

.grid-pager,
.side-pager {
  display: flex;
  align-items: center;
  gap: 8px;
}

.pager-text {
  font-size: 12.5px;
  color: var(--text-muted);
}

.page-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 12px;
}

.page-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  text-align: left;
  padding: 0;
  transition: transform 0.12s ease, border-color 0.12s ease;
}

.page-card:hover {
  transform: translateY(-2px);
  border-color: var(--accent);
}

.page-card.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(56, 189, 248, 0.35);
}

.card-thumb-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 3 / 4;
  background: #ffffff;
  overflow: hidden;
  border-bottom: 1px solid var(--border);
}

.card-thumb-wrap img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

.card-badges {
  position: absolute;
  top: 6px;
  left: 6px;
  right: 6px;
  display: flex;
  justify-content: space-between;
  gap: 4px;
  pointer-events: none;
}

.badge-spec,
.badge-page,
.badge-ocr {
  padding: 2px 6px;
  border-radius: 5px;
  font-size: 11px;
  font-weight: 700;
}

.badge-spec {
  background: rgba(11, 18, 32, 0.86);
  color: #38bdf8;
}

.badge-page {
  background: rgba(11, 18, 32, 0.82);
  color: #f8fafc;
}

.badge-ocr {
  background: rgba(5, 150, 105, 0.9);
  color: #ffffff;
}

.card-body {
  padding: 9px 10px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.card-doc-title {
  font-size: 11.5px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-section {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.card-snippet {
  font-size: 12px;
  color: var(--text-muted);
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

mark.kw-0 { background: var(--kw0-bg); color: var(--text); border-radius: 3px; padding: 0 2px; }
mark.kw-1 { background: var(--kw1-bg); color: var(--text); border-radius: 3px; padding: 0 2px; }
mark.kw-2 { background: var(--kw2-bg); color: var(--text); border-radius: 3px; padding: 0 2px; }
mark.kw-3 { background: var(--kw3-bg); color: var(--text); border-radius: 3px; padding: 0 2px; }

/* ── Detail Split Mode ── */
.detail-view {
  flex: 1;
  min-height: 0;
  display: flex;
}

.sidebar {
  width: 320px;
  flex: 0 0 320px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.sidebar-tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  padding: 8px;
  border-bottom: 1px solid var(--border);
}

.side-tab {
  min-height: 32px;
  font-size: 12px;
  padding: 4px 6px;
}

.tab-count {
  font-weight: 700;
  opacity: 0.9;
}

.side-panel {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.side-list,
.toc-tree {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.side-pager {
  padding: 8px;
  border-top: 1px solid var(--border);
  justify-content: space-between;
}

.side-item {
  width: 100%;
  text-align: left;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.side-item.active {
  border-color: var(--accent);
  background: var(--surface-hover);
}

.side-item-top {
  display: flex;
  justify-content: space-between;
  font-size: 11.5px;
  color: var(--accent);
  font-weight: 600;
}

.side-item-title {
  font-size: 13px;
  font-weight: 600;
}

.side-item-snip {
  font-size: 12px;
  color: var(--text-muted);
}

.toc-doc-head {
  padding: 8px 10px;
  font-size: 12.5px;
  font-weight: 700;
  border-bottom: 1px solid var(--border);
  color: var(--accent);
}

.toc-item {
  width: 100%;
  text-align: left;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 6px;
  border: 1px solid transparent;
  background: transparent;
  font-size: 12.5px;
}

.toc-item:hover {
  background: var(--surface-hover);
}

.toc-item.active {
  background: var(--surface-hover);
  border-color: var(--accent);
  font-weight: 600;
}

.toc-item.lvl-2 { padding-left: 18px; }
.toc-item.lvl-3 { padding-left: 28px; font-size: 12px; color: var(--text-muted); }

/* ── Viewer Stage ── */
.viewer-stage {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.stage-toolbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 7px 12px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.stage-title-group {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.stage-title {
  font-size: 13.5px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.stage-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 5px;
}

.sep {
  width: 1px;
  height: 18px;
  background: var(--border);
  margin: 0 2px;
}

.zoom-label {
  min-width: 46px;
  text-align: center;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.stage-split {
  flex: 1;
  min-height: 0;
  display: flex;
}

.canvas-viewport {
  flex: 1;
  min-width: 0;
  min-height: 0;
  position: relative;
  overflow: hidden;
  background: #1a2234;
  touch-action: none;
  cursor: grab;
  user-select: none;
}

.canvas-viewport.pin-cursor {
  cursor: crosshair;
}

.canvas-transform {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
  will-change: transform;
  background: #ffffff;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
}

.canvas-transform img {
  display: block;
  width: 100%;
  height: auto;
  pointer-events: none;
}

.overlay-boxes,
.overlay-pins {
  position: absolute;
  inset: 0;
}

.overlay-boxes {
  pointer-events: none;
}

.hl-box {
  position: absolute;
  border-radius: 3px;
  border: 2px solid var(--kw0-bd);
  background: var(--kw0-bg);
}

.hl-box.hl-1 { border-color: var(--kw1-bd); background: var(--kw1-bg); }
.hl-box.hl-2 { border-color: var(--kw2-bd); background: var(--kw2-bg); }
.hl-box.hl-3 { border-color: var(--kw3-bd); background: var(--kw3-bg); }
.hl-box.focused {
  box-shadow: 0 0 0 3px #ffffff, 0 0 0 6px var(--accent);
}

.pin-marker {
  position: absolute;
  transform: translate(-50%, -100%);
  width: 26px;
  height: 26px;
  min-height: 26px;
  padding: 0;
  border-radius: 50% 50% 50% 0;
  transform-origin: bottom left;
  background: #ef4444;
  border: 2px solid #ffffff;
  color: #ffffff;
  font-size: 11px;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.45);
}

.empty-stage {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  text-align: center;
  color: var(--text-muted);
}

/* ── Clean Text Inspector ── */
.text-inspector {
  width: 340px;
  flex: 0 0 340px;
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.text-head {
  padding: 9px 12px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.text-head-title {
  display: flex;
  align-items: center;
  gap: 6px;
}

.text-head-actions {
  display: flex;
  gap: 5px;
}

.clean-text-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 12px;
  font-size: 13px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
  user-select: text;
}

.pins-panel {
  border-top: 1px solid var(--border);
  max-height: 180px;
  overflow-y: auto;
  padding: 8px 12px;
}

.pins-head {
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.pins-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.pin-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 7px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: 12px;
}

/* ── Login & Dialogs (Rule 17 Mobile Viewport + Safe Area) ── */
.login-overlay {
  position: fixed;
  inset: 0;
  z-index: 90;
  background: rgba(7, 11, 20, 0.88);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.login-card {
  width: 100%;
  max-width: 390px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 22px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.login-brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.login-brand h2 {
  margin: 0;
  font-size: 19px;
}

.login-desc {
  margin: 0 0 6px;
  font-size: 13px;
  color: var(--text-muted);
}

.field-label {
  font-size: 12.5px;
  font-weight: 600;
}

.login-card input,
.app-dialog input,
.app-dialog textarea,
.app-dialog select {
  width: 100%;
  min-height: 38px;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
}

.login-error {
  color: var(--danger);
  font-size: 12.5px;
}

.app-dialog {
  width: min(94vw, 520px);
  max-height: 90dvh;
  padding: 0;
  margin: auto;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.app-dialog-wide {
  width: min(95vw, 780px);
}

.app-dialog[open] {
  display: flex;
  flex-direction: column;
}

.app-dialog::backdrop {
  background: rgba(4, 8, 16, 0.72);
  backdrop-filter: blur(3px);
}

.dialog-header {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.dialog-header h3 {
  margin: 0;
  font-size: 16px;
}

.dialog-tabs {
  display: flex;
  gap: 6px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
}

.dialog-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.dialog-footer {
  padding: 10px 16px calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.dialog-hint {
  margin: 0;
  font-size: 13px;
  color: var(--text-muted);
}

.sync-status-box,
.pwa-guide-card,
.feature-card,
.admin-row {
  padding: 10px 12px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface-2);
}

.pwa-guides,
.features-list,
.admin-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.pwa-guide-card p,
.feature-card p {
  margin: 4px 0 0;
  font-size: 12.5px;
  color: var(--text-muted);
}

.admin-btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin-bottom: 10px;
}

.user-create-form {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr auto;
  gap: 6px;
  margin-bottom: 10px;
}

.admin-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 12.5px;
}

.version-meta {
  font-size: 12.5px;
  color: var(--accent);
  font-weight: 700;
}

.toast {
  position: fixed;
  bottom: calc(18px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  background: #0f172a;
  color: #f8fafc;
  border: 1px solid var(--accent);
  border-radius: 999px;
  padding: 8px 16px;
  font-size: 13px;
  box-shadow: var(--shadow);
}

.mobile-only {
  display: none;
}

/* ── Mobile Layout (TS-008 Single Touch Scroll & Safe Viewport) ── */
@media (max-width: 900px) {
  .mobile-only {
    display: inline-flex;
  }

  .detail-view {
    flex-direction: column;
    overflow-y: auto;
  }

  .sidebar {
    width: 100%;
    flex: none;
    border-right: none;
    border-bottom: 1px solid var(--border);
    overflow: visible;
  }

  .side-list,
  .toc-tree {
    overflow: visible;
  }

  .detail-view.mobile-stage-open .sidebar {
    display: none;
  }

  .detail-view:not(.mobile-stage-open) .viewer-stage {
    display: none;
  }

  .stage-split {
    flex-direction: column;
  }

  .canvas-viewport {
    min-height: 55dvh;
  }

  .text-inspector {
    width: 100%;
    flex: none;
    border-left: none;
    border-top: 1px solid var(--border);
    max-height: 45dvh;
  }

  .user-create-form {
    grid-template-columns: 1fr;
  }

  .app-dialog {
    width: 96vw;
  }
}
