:root {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --surface-2: #f9fafb;
  --border: #d9dde3;
  --text: #1f2937;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --workspace-bg: #e5e7eb;
  --ok: #15803d;
  --ok-bg: #dcfce7;
  --err: #b91c1c;
  --err-bg: #fee2e2;
  --selected: #dbeafe;
  --group-a: #2563eb;
  --group-b: #0d9488;
  font-family: "Malgun Gothic", "Apple SD Gothic Neo", "Segoe UI", sans-serif;
  font-size: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111827;
    --surface: #1f2937;
    --surface-2: #273244;
    --border: #374151;
    --text: #e5e7eb;
    --muted: #9ca3af;
    --accent: #3b82f6;
    --workspace-bg: #0b1220;
    --ok: #4ade80;
    --ok-bg: #14532d;
    --err: #f87171;
    --err-bg: #450a0a;
    --selected: #1e3a8a;
    --group-a: #60a5fa;
    --group-b: #2dd4bf;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); display: flex; flex-direction: column; overflow: hidden; }
[hidden] { display: none !important; }

/* ---------- 상단 바 ---------- */
.topbar {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 8px 12px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 16px; }
.brand-sub { font-weight: 400; font-size: 12px; color: var(--muted); margin-left: 4px; }
.tabs { display: flex; gap: 4px; padding: 3px; border-radius: 8px; background: var(--surface-2); border: 1px solid var(--border); }
.tab {
  border: 0; background: transparent; color: var(--text); font: inherit; font-weight: 600;
  padding: 6px 14px; border-radius: 6px; cursor: pointer;
}
.tab:hover { color: var(--accent); }
.tab[aria-selected="true"] { background: var(--accent); color: var(--accent-text); }
.privacy { margin-left: auto; color: var(--muted); font-size: 12px; }

.btn {
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  border-radius: 6px; padding: 6px 12px; cursor: pointer; font: inherit; line-height: 1.2;
}
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.small { padding: 3px 8px; font-size: 12px; }
.btn.icon { width: 28px; height: 28px; padding: 0; }
.btn.block { display: block; width: 100%; margin-top: 10px; padding: 9px 12px; font-weight: 600; }

input[type="text"], input[type="number"] {
  height: 32px; border: 1px solid var(--border); border-radius: 6px;
  background: var(--surface); color: var(--text); font: inherit; padding: 0 8px;
}
input[type="text"]:focus, input[type="number"]:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }
.muted { color: var(--muted); font-size: 12px; }

/* ---------- 레이아웃 ---------- */
.layout { flex: 1; display: flex; min-height: 0; }
.workspace { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--workspace-bg); }
.pane { flex: 1; display: flex; flex-direction: column; min-height: 0; position: relative; }
.pane-head {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 8px 12px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.doc-info { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; max-width: 50%; }
.select-tools { display: flex; align-items: center; gap: 6px; margin-left: auto; }

.options {
  width: 300px; flex-shrink: 0; overflow-y: auto;
  background: var(--surface); border-left: 1px solid var(--border); padding: 16px;
}
.option-panel h2 { margin: 0 0 10px; font-size: 16px; }
.hint { margin: 8px 0; color: var(--muted); font-size: 12px; line-height: 1.6; }
.plan { margin: 12px 0 0; padding: 8px 10px; border-radius: 6px; background: var(--surface-2); font-size: 13px; line-height: 1.5; }
.plan:empty { display: none; }
.plan.error { background: var(--err-bg); color: var(--err); }
.field { display: flex; flex-direction: column; gap: 4px; margin: 12px 0 0; font-size: 12px; color: var(--muted); }
.field input { width: 100%; }
.choices { border: 0; padding: 0; margin: 12px 0 0; display: flex; flex-direction: column; gap: 8px; }
.choices legend { padding: 0; margin-bottom: 6px; font-size: 12px; color: var(--muted); }
.choices label { display: flex; align-items: center; gap: 6px; cursor: pointer; }
.inline-number { width: 64px; height: 28px; }
.range-input { width: 100%; }
.button-row { display: flex; align-items: center; gap: 6px; margin-top: 10px; flex-wrap: wrap; }
.row-label { width: 100%; font-size: 12px; color: var(--muted); }
.button-row .btn { flex: 1; }

.empty { flex: 1; display: flex; align-items: center; justify-content: center; padding: 16px; }
.empty-box {
  text-align: center; border: 2px dashed var(--border); border-radius: 12px; padding: 40px 32px;
  background: var(--surface); max-width: 560px; line-height: 1.6;
}
.empty-title { font-size: 18px; font-weight: 700; margin-bottom: 8px; }

/* ---------- 병합 목록 ---------- */
.merge-list { list-style: none; margin: 0; padding: 12px; overflow-y: auto; flex: 1; display: flex; flex-direction: column; gap: 8px; }
.merge-item {
  display: grid; grid-template-columns: auto auto 64px minmax(0, 1fr) 200px auto; align-items: center; gap: 10px;
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface);
}
.merge-item.dragging { opacity: .45; }
.merge-item.drop-before { box-shadow: 0 -3px 0 var(--accent); }
.merge-item.drop-after { box-shadow: 0 3px 0 var(--accent); }
.merge-item.invalid { border-color: var(--err); }
.drag-handle { cursor: grab; color: var(--muted); letter-spacing: -3px; user-select: none; }
.order { min-width: 22px; text-align: center; font-weight: 700; color: var(--accent); }
.merge-thumb { width: 64px; height: 64px; display: flex; align-items: center; justify-content: center; }
.merge-thumb canvas { display: block; background: #fff; outline: 1px solid var(--border); }
.merge-meta { min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.merge-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.field-error { color: var(--err); font-size: 12px; }
.field-error:empty { display: none; }
.merge-range { display: flex; flex-direction: column; gap: 3px; font-size: 11px; color: var(--muted); }
.merge-actions { display: flex; gap: 4px; }

/* ---------- 페이지 썸네일 ---------- */
.page-grid {
  flex: 1; overflow-y: auto; padding: 16px; align-content: start;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(176px, 1fr)); gap: 14px;
}
.page-card {
  position: relative; display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 10px 10px 8px; border: 2px solid transparent; border-radius: 10px; background: var(--surface);
  outline: none; user-select: none;
}
.page-card:focus-visible { border-color: var(--accent); }
body[data-mode="extract"] .page-card, body[data-mode="rotate"] .page-card { cursor: pointer; }
body[data-mode="extract"] .page-card:hover, body[data-mode="rotate"] .page-card:hover { border-color: var(--border); }
.page-card.selected { border-color: var(--accent); background: var(--selected); }
.page-card.removed { border-color: var(--err); background: var(--err-bg); }
.page-card.removed .thumb { opacity: .4; }
.page-card.skipped .thumb { opacity: .35; }
.page-card.group-odd { box-shadow: inset 0 -4px 0 var(--group-a); }
.page-card.group-even { box-shadow: inset 0 -4px 0 var(--group-b); }
.thumb { width: 150px; height: 150px; display: flex; align-items: center; justify-content: center; }
.thumb canvas { display: block; background: #fff; box-shadow: 0 1px 4px rgba(0, 0, 0, .25); transition: transform .18s ease; }
.page-card:not(.rendered) .thumb canvas { width: 106px; height: 150px; }
.card-foot { display: flex; align-items: center; gap: 6px; font-size: 12px; min-height: 20px; }
.page-no { font-weight: 700; }
.badge { border-radius: 4px; padding: 1px 6px; font-size: 11px; font-weight: 600; color: var(--accent); background: var(--selected); white-space: nowrap; }
.page-card.removed .badge { color: var(--err); background: var(--surface); }
.page-card.skipped .badge { color: var(--muted); background: var(--surface-2); }
.page-card.group-even .badge { color: var(--group-b); }

.rotate-controls { display: none; position: absolute; top: 6px; right: 6px; flex-direction: column; gap: 4px; }
body[data-mode="rotate"] .rotate-controls { display: flex; }
.rotate-btn {
  width: 28px; height: 28px; border: 1px solid var(--border); border-radius: 6px; cursor: pointer;
  background: var(--surface); color: var(--text); font-size: 15px; line-height: 1; opacity: .85;
}
.rotate-btn:hover { border-color: var(--accent); color: var(--accent); opacity: 1; }

/* ---------- 상태 · 덮개 ---------- */
.statusbar {
  padding: 5px 12px; font-size: 12px; color: var(--muted); min-height: 26px;
  background: var(--surface); border-top: 1px solid var(--border);
}
.statusbar[data-kind="error"] { color: var(--err); background: var(--err-bg); }
.statusbar[data-kind="ok"] { color: var(--ok); background: var(--ok-bg); }

.drop-overlay {
  position: fixed; inset: 0; background: rgba(37, 99, 235, .15); border: 4px dashed var(--accent);
  display: flex; align-items: center; justify-content: center; z-index: 50; pointer-events: none;
  font-size: 22px; font-weight: 700; color: var(--accent);
}
.busy { position: fixed; inset: 0; z-index: 60; display: flex; align-items: center; justify-content: center; background: rgba(15, 23, 42, .35); }
.busy-box { display: flex; align-items: center; gap: 12px; padding: 16px 22px; border-radius: 10px; background: var(--surface); box-shadow: 0 10px 30px rgba(0, 0, 0, .25); font-weight: 600; }
.spinner { width: 20px; height: 20px; border: 3px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  .spinner { animation-duration: 2.4s; }
  .thumb canvas { transition: none; }
}

@media (max-width: 900px) {
  .layout { flex-direction: column; }
  .options { width: 100%; max-height: 42%; border-left: 0; border-top: 1px solid var(--border); order: 2; }
  .privacy { display: none; }
  .merge-item { grid-template-columns: auto 64px minmax(0, 1fr) auto; }
  .merge-item .drag-handle { display: none; }
  .merge-actions { grid-row: 1; grid-column: 4; }
  .merge-range { grid-row: 2; grid-column: 2 / -1; }
  .page-grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
}

/* ---------- 이미지 추가 ---------- */
.empty-actions { display: flex; gap: 8px; justify-content: center; }
.field select {
  width: 100%; height: 32px; border: 1px solid var(--border); border-radius: 6px;
  background: var(--surface); color: var(--text); font: inherit; padding: 0 6px;
}
.check { display: flex; align-items: center; gap: 6px; margin-top: 10px; cursor: pointer; }
.check:has(input:disabled) { opacity: .5; cursor: default; }
.choices label:has(input:disabled) { opacity: .5; cursor: default; }
.choices .sub-choice { margin: -4px 0 0 22px; font-size: 12px; color: var(--muted); }
.badge-muted { color: var(--muted); background: var(--surface-2); }
.image-card { border-color: var(--group-b); }
.image-card .thumb img { max-width: 150px; max-height: 150px; display: block; background: #fff; box-shadow: 0 1px 4px rgba(0, 0, 0, .25); }
.image-card .badge { color: var(--group-b); }
.card-name { max-width: 90px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); }
.card-controls { position: absolute; top: 6px; right: 6px; display: flex; flex-direction: column; gap: 4px; }
.card-controls .rotate-btn:disabled { opacity: .3; cursor: default; }
#insertGrid .page-card:not(.rendered):not(.image-card) .thumb canvas { width: 106px; height: 150px; }

/* ---------- 개인정보 가리기 ---------- */
.toggle { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; cursor: pointer; }
.mask-pages {
  flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; align-items: center; gap: 18px;
  --mask-fill: rgba(0, 0, 0, .45); --mask-line: #ef4444;
}
.mask-pages.white { --mask-fill: rgba(255, 255, 255, .7); --mask-line: #ef4444; }
.mask-pages.opaque { --mask-fill: #000; }
.mask-pages.opaque.white { --mask-fill: #fff; }
.mask-page {
  position: relative; width: 100%; max-width: 900px; flex-shrink: 0;
  background: #fff; box-shadow: 0 2px 10px rgba(0, 0, 0, .25);
}
.mask-page canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.mask-layer { position: absolute; inset: 0; cursor: crosshair; touch-action: none; user-select: none; }
.mask-page-no {
  position: absolute; left: 8px; bottom: -2px; transform: translateY(100%); padding-top: 2px;
  font-size: 11px; color: var(--muted);
}
.mask-rect { position: absolute; background: var(--mask-fill); outline: 2px solid var(--mask-line); cursor: pointer; }
.mask-rect.auto { outline-style: dashed; outline-color: #f59e0b; }
.mask-rect.selected { outline: 3px solid var(--accent); }
.mask-rect.drawing { outline: 2px dashed var(--accent); background: rgba(37, 99, 235, .15); pointer-events: none; }
.mask-remove {
  position: absolute; top: -10px; right: -10px; width: 20px; height: 20px; padding: 0; border-radius: 50%;
  border: 1px solid var(--border); background: var(--surface); color: var(--err); font-size: 11px; line-height: 1;
  cursor: pointer; display: none;
}
.mask-rect:hover .mask-remove, .mask-rect.selected .mask-remove { display: block; }
