/*
 * style.css — Law Memorisation Training App
 *
 * Design system:
 *   --bg-primary   : main background
 *   --bg-secondary : panel / card background
 *   --bg-tertiary  : input / textarea background
 *   --text-primary : main text colour
 *   --text-muted   : secondary / placeholder text
 *   --accent       : brand blue (buttons, active states)
 *   --kw-orange    : keyword highlight colour (Stage 2)
 *   --grayed       : opacity for gray-out blocks in Focus mode
 *
 * Layout:
 *   The main container uses CSS flexbox for the 50:50 left/right split.
 *   The header sits above the split and holds the stage navigation + API key.
 *
 * Responsive:
 *   Below 768 px the panels stack vertically (left above right).
 *
 * NOTE FOR NEXT AI:
 *   Dark-mode variables are in the [data-theme="dark"] block at the bottom.
 *   To add a system-preference based auto-dark-mode, replace the attribute
 *   selector with @media (prefers-color-scheme: dark).
 */

/* =====================================================================
   CSS Custom Properties (Light Theme)
   ===================================================================== */
:root {
  --bg-primary:    #f5f6fa;
  --bg-secondary:  #ffffff;
  --bg-tertiary:   #f0f2f7;
  --border:        #d9dce8;
  --text-primary:  #1a1d2e;
  --text-muted:    #6b7280;
  --text-heading:  #0f172a;
  --accent:        #3b5bdb;
  --accent-hover:  #2f4ac8;
  --accent-light:  #e8edff;
  --danger:        #e03131;
  --warn:          #e67700;
  --success:       #2f9e44;
  --kw-orange:     #fd7e14;
  --kw-orange-bg:  #fff3e0;
  --grayed:        0.22;   /* opacity for grayed-out focus blocks */
  --radius:        8px;
  --radius-sm:     4px;
  --shadow:        0 1px 4px rgba(0,0,0,.08), 0 2px 12px rgba(0,0,0,.06);
  --font-main:     'Pretendard', 'Noto Sans KR', 'Apple SD Gothic Neo', sans-serif;
  --font-mono:     'JetBrains Mono', 'Fira Code', monospace;
}

/* =====================================================================
   Reset & Base
   ===================================================================== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { font-size: 15px; }

body {
  font-family: var(--font-main);
  background: var(--bg-primary);
  color: var(--text-primary);
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* =====================================================================
   App Shell
   ===================================================================== */
#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

/* =====================================================================
   Header
   ===================================================================== */
.app-header {
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  padding: 0.6rem 1.25rem;
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-shrink: 0;
  box-shadow: var(--shadow);
  z-index: 10;
}

.header-title h1 {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text-heading);
  white-space: nowrap;
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex: 1;
  justify-content: space-between;
}

.archive-group {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

/* Save + Archive buttons grouped together in header */
.header-save-group {
  display: flex;
  align-items: center;
  gap: 0.35rem;
}

.header-save-btn {
  font-size: 0.8rem;
  padding: 0.32rem 0.75rem;
}

/* API key group: [provider dropdown] [key input] */
.api-key-group {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

/* Provider dropdown (Claude / GPT / Gemini) */
.api-provider-select {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.32rem 0.5rem;
  font-size: 0.8rem;
  font-family: var(--font-main);
  background: var(--bg-tertiary);
  color: var(--text-primary);
  cursor: pointer;
  outline: none;
}
.api-provider-select:focus { border-color: var(--accent); }

.api-key-group input {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.35rem 0.7rem;
  font-size: 0.8rem;
  width: 190px;
  background: var(--bg-tertiary);
  color: var(--text-primary);
  outline: none;
}

.api-key-group input:focus {
  border-color: var(--accent);
  background: #fff;
}

/* Stage navigation */
.stage-nav {
  display: flex;
  gap: 0.35rem;
}

.stage-btn {
  padding: 0.35rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: transparent;
  font-size: 0.8rem;
  cursor: pointer;
  color: var(--text-muted);
  transition: all 0.15s;
}

.stage-btn:hover:not(.disabled) {
  background: var(--accent-light);
  border-color: var(--accent);
  color: var(--accent);
}

.stage-btn.current {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

.stage-btn.disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* =====================================================================
   Side Menu (icon-only vertical toolbar)
   ===================================================================== */
.side-menu {
  width: 48px;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.6rem 0;
  gap: 0.25rem;
  flex-shrink: 0;
  z-index: 5;
}

.side-btn {
  width: 36px;
  height: 36px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  font-size: 1.1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  transition: background 0.12s;
  color: var(--text-muted);
}

.side-btn:hover { background: var(--accent-light); }

.side-btn-active {
  background: var(--accent-light);
  color: var(--accent);
}

/* Tooltip: appears to the right of the button on hover */
.side-btn::after {
  content: attr(data-tooltip);
  position: absolute;
  left: calc(100% + 8px);
  top: 50%;
  transform: translateY(-50%);
  white-space: nowrap;
  background: var(--text-heading);
  color: #fff;
  font-size: 0.75rem;
  padding: 0.2rem 0.55rem;
  border-radius: var(--radius-sm);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s;
  z-index: 600;
}
.side-btn:hover::after { opacity: 1; }

.side-menu-divider {
  width: 28px;
  height: 1px;
  background: var(--border);
  margin: 0.15rem 0;
  flex-shrink: 0;
}

/* =====================================================================
   Main Container (50:50 split)
   ===================================================================== */
.main-container {
  display: flex;
  flex: 1;
  overflow: hidden;
}

.panel {
  display: flex;
  flex-direction: column;
  flex: 1;
  overflow: hidden;
}

.left-panel  { border-right: 1px solid var(--border); }
.right-panel { background: var(--bg-secondary); }

.panel-divider {
  width: 4px;
  background: var(--border);
  cursor: col-resize;
  flex-shrink: 0;
}

.panel-header {
  padding: 0.6rem 1rem;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  background: var(--bg-secondary);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.right-panel-actions { display: flex; gap: 0.5rem; }

.panel-content {
  flex: 1;
  overflow: hidden;   /* flex children manage their own scroll */
  padding: 1rem;
  position: relative;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

/* =====================================================================
   Stage Views (show/hide pattern)
   ===================================================================== */
.stage-view {
  display: none;
  flex-direction: column;
  /* No height:100% here — panel-content is the scroll container */
}

.stage-view.active {
  display: flex;
  flex: 1;            /* fill panel-content's available height */
  min-height: 0;      /* allow shrinking below content size */
  overflow: hidden;
}

/* Stages whose total content can exceed the panel — need their own scroll */
#stage-0-right.stage-view.active,
#stage-4-right.stage-view.active  { overflow-y: auto; }


/* =====================================================================
   Stage 0: Input / Preview toggle
   ===================================================================== */
#stage-0-edit-section {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}
#stage-0-edit-section.hidden { display: none; }

.stage-0-preview-section {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}
.stage-0-preview-section.hidden { display: none; }

.stage0-preview-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.35rem 0 0.5rem;
  flex-shrink: 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 0.5rem;
}

.stage0-preview-label {
  font-size: 0.78rem;
  color: var(--text-muted);
  font-weight: 600;
}

#stage-0-preview {
  flex: 1;
  overflow-y: auto;
  min-height: 0;
  font-size: 0.88rem;
  line-height: 1.75;
}

/* Modal vault-mappings label */
.modal-mapping-label {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 0.1rem;
}

/* =====================================================================
   Stage 0: Input (textarea)
   ===================================================================== */
#text-input {
  flex: 1;
  width: 100%;
  min-height: 300px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.85rem;
  font-size: 0.9rem;
  font-family: var(--font-main);
  color: var(--text-primary);
  background: var(--bg-tertiary);
  resize: none;
  outline: none;
  line-height: 1.7;
}

#text-input:focus { border-color: var(--accent); background: #fff; }

.input-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.75rem;
  justify-content: flex-end;
}

/* Collapsible case input section (top of Stage 0 left panel) */
.case-input-section {
  flex-shrink: 0;
  margin-bottom: 0.6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.case-input-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.45rem 0.75rem;
  background: var(--accent-light);
  cursor: pointer;
  user-select: none;
}

.case-input-label {
  font-size: 0.84rem;
  font-weight: 600;
  color: var(--accent);
}

.case-toggle-icon {
  font-size: 0.78rem;
  color: var(--accent);
  opacity: 0.75;
}

.case-input-textarea {
  display: block;
  width: 100%;
  min-height: 100px;
  max-height: 200px;
  border: none;
  border-top: 1px solid var(--border);
  padding: 0.65rem 0.75rem;
  font-size: 0.88rem;
  font-family: var(--font-main);
  color: var(--text-primary);
  background: var(--bg-tertiary);
  resize: vertical;
  outline: none;
  line-height: 1.65;
}
.case-input-textarea:focus { background: #fff; }

/* Collapsible case display box (top of Stage 1/2/3 left panels) */
.case-display-box {
  flex-shrink: 0;
  margin-bottom: 0.6rem;
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  overflow: hidden;
}
.case-display-box.hidden { display: none; }

.case-box-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.45rem 0.75rem;
  background: var(--accent-light);
  cursor: pointer;
  user-select: none;
}

.case-box-title {
  font-size: 0.84rem;
  font-weight: 700;
  color: var(--accent);
}

.case-box-body {
  padding: 0.75rem;
  font-size: 0.87rem;
  line-height: 1.7;
  color: var(--text-primary);
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  white-space: pre-wrap;
  max-height: 260px;
  overflow-y: auto;
}

/* Instructions panel (right side stage 0) */
.instructions {
  padding: 1rem;
}

.instructions h3 {
  margin-bottom: 0.75rem;
  font-size: 1rem;
  color: var(--text-heading);
}

.instructions ol {
  padding-left: 1.25rem;
  line-height: 2;
  font-size: 0.9rem;
}

.api-notice {
  margin-top: 1rem;
  padding: 0.75rem 1rem;
  background: var(--accent-light);
  border-radius: var(--radius);
  font-size: 0.85rem;
  color: var(--accent);
}

/* =====================================================================
   Stage 1: Skeleton View
   ===================================================================== */
.skeleton-container {
  flex: 1;
  overflow-y: auto;
  min-height: 0;  /* flex scroll fix */
  padding: 0.5rem 0;
}

.skeleton-item {
  display: flex;
  align-items: baseline;
  gap: 0.25rem;   /* tight: number and title nearly touch */
  padding: 0.2rem 0;
}

/* Level 0: <설문> — topmost, visually prominent section break */
.skeleton-item.level-0 {
  font-size: 1rem;
  font-weight: 800;
  color: var(--accent);
  margin: 0.5rem 0 0.2rem;
  padding-bottom: 0.25rem;
  border-bottom: 2px solid var(--accent);
}

/* All levels use solid text — no gray so every heading is clearly readable */
.skeleton-item.level-1 { font-size: 0.97rem; color: var(--text-heading); font-weight: 700; margin-top: 0.2rem; }
.skeleton-item.level-2 { font-size: 0.90rem; color: var(--text-primary); font-weight: 500; }
.skeleton-item.level-3 { font-size: 0.86rem; color: var(--text-primary); font-weight: 400; }
.skeleton-item.level-4 { font-size: 0.84rem; color: var(--text-primary); font-weight: 400; }

/* No fixed min-width — let the number take its natural width */
.skeleton-num   { color: var(--accent); font-weight: 700; white-space: nowrap; }
.skeleton-title { flex: 1; }

/* AI Analysis panel (bottom of Stage 1 left) */
.ai-panel {
  margin-top: 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  flex-shrink: 0;
}

.ai-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 0.75rem;
  background: var(--bg-tertiary);
  font-size: 0.82rem;
  font-weight: 600;
  border-bottom: 1px solid var(--border);
}

.ai-result-area {
  padding: 0.75rem;
  font-size: 0.87rem;
  line-height: 1.7;
  max-height: 180px;
  overflow-y: auto;
  background: var(--bg-secondary);
}

.ai-placeholder { color: var(--text-muted); font-style: italic; }

/* Shown when AI feature is called without an API key */
.ai-no-key {
  color: var(--warn);
  font-size: 0.84rem;
  line-height: 1.6;
  padding: 0.25rem 0;
}

/* =====================================================================
   Stage 2: Focus View
   ===================================================================== */
.focus-controls {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.65rem 0.85rem;
  margin-bottom: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  flex-shrink: 0;
}

/* Depth level selection: row of buttons, one per existing heading level */
.depth-btn-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.focus-ctrl-label {
  font-size: 0.78rem;
  color: var(--text-muted);
  white-space: nowrap;
}

/* Individual depth buttons (e.g. <>, Ⅰ., 1., (1), 1)) */
.depth-level-btn {
  padding: 0.25rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: transparent;
  font-size: 0.82rem;
  font-weight: 700;
  font-family: var(--font-main);
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.12s;
}
.depth-level-btn:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-light); }
.depth-level-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* Sequence slider row */
.slider-group {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.slider-group label {
  display: flex;
  justify-content: space-between;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.slider-value { font-weight: 600; color: var(--accent); }

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 4px;
  border-radius: 2px;
  background: var(--border);
  outline: none;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  box-shadow: 0 1px 3px rgba(0,0,0,.2);
}

.focus-actions { display: flex; justify-content: flex-end; }

.focus-container {
  flex: 1;
  overflow-y: auto;
  min-height: 0;  /* flex scroll fix */
}

/* Content blocks inside focus view */
.content-block {
  padding: 0.4rem 0.5rem;
  border-radius: var(--radius-sm);
  margin-bottom: 0.3rem;
  transition: opacity 0.25s ease;
}

.block-active {
  opacity: 1;
  background: transparent;
}

/* Grayed blocks become faint but remain readable (accessibility) */
.block-grayed {
  opacity: var(--grayed);
  pointer-events: none;
  user-select: none;
}

.block-heading {
  font-weight: 600;
  margin-bottom: 0.15rem;
  display: flex;
  align-items: baseline;
  gap: 0.2rem;   /* tight gap between number and title */
}

.block-num   { color: var(--accent); white-space: nowrap; flex-shrink: 0; }
.block-body  { font-size: 0.87rem; line-height: 1.65; color: var(--text-primary); white-space: pre-wrap; }

/* Level-based typography in content blocks */
.content-block.level-0 .block-heading {
  font-size: 1rem;
  font-weight: 800;
  color: var(--accent);
  padding-bottom: 0.2rem;
  border-bottom: 2px solid var(--accent);
  margin-bottom: 0.3rem;
}
.content-block.level-1 .block-heading { font-size: 0.97rem; }
.content-block.level-2 .block-heading { font-size: 0.90rem; }
.content-block.level-3 .block-heading { font-size: 0.86rem; }
.content-block.level-4 .block-heading { font-size: 0.84rem; }

/* User-marked bold text → blue */
strong.user-bold { color: var(--accent); }

/* Orange keyword highlight */
mark.kw-highlight {
  background: var(--kw-orange-bg);
  color: var(--kw-orange);
  border-radius: 2px;
  padding: 0 2px;
  font-weight: 700;
  /* No CSS mark reset needed; only styled via class */
}

/* =====================================================================
   Stage 2 Right Panel — Dynamic test area
   ===================================================================== */

/* Scrollable container for the dynamically built section list */
.s2-right-content {
  flex: 1;
  overflow-y: auto;
  min-height: 0;  /* critical: lets flex child create a real scroll container */
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding-bottom: 1rem;  /* breathing room at the bottom */
}

/* One section = target block + its children */
.s2-section {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  flex-shrink: 0;
}

/* Read-only heading label at the top of each section */
.s2-heading-label {
  display: flex;
  align-items: baseline;
  gap: 0.3rem;
  padding: 0.45rem 0.75rem;
  background: var(--bg-tertiary);
  border-bottom: 1px solid var(--border);
  user-select: none;  /* not selectable — reinforces "read-only" feel */
  font-weight: 600;
}

/* Level-based styling for heading labels */
.s2-heading-label.level-0 { background: var(--accent-light); font-size: 0.97rem; font-weight: 800; color: var(--accent); }
.s2-heading-label.level-1 { font-size: 0.95rem; }
.s2-heading-label.level-2 { font-size: 0.90rem; }
.s2-heading-label.level-3 { font-size: 0.86rem; }
.s2-heading-label.level-4 { font-size: 0.84rem; }

.s2-reference-box {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem;
  margin: 0.5rem;
  font-size: 0.9rem;
  color: var(--text-heading);
  line-height: 1.6;
  white-space: pre-wrap;
}
.s2-label-num   { color: var(--accent); font-weight: 700; white-space: nowrap; }
.s2-label-title { color: var(--text-heading); flex: 1; min-width: 0; word-break: keep-all; }

/* Intro text shown before the first sub-item (read-only hint) */
.s2-intro-text {
  padding: 0.4rem 0.75rem 0;
  font-size: 0.82rem;
  color: var(--text-muted);
  font-style: italic;
}

/* Vault Mapping rows in save modal */
#vault-mappings-container {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.vault-mapping-row {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  margin-bottom: 0.3rem;
}

/* Each vault-select and toc-select takes exactly half the available row width */
.vault-mapping-row .save-vault,
.vault-mapping-row .save-toc-item {
  flex: 1 1 0;
  min-width: 0;
  padding: 0.4rem 0.5rem;
  font-size: 0.83rem;
  font-family: var(--font-main);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-tertiary);
  color: var(--text-primary);
  outline: none;
}
.vault-mapping-row .save-vault:focus,
.vault-mapping-row .save-toc-item:focus { border-color: var(--accent); }

.mapping-add-btn, .mapping-del-btn {
  width: 26px;
  height: 26px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-weight: bold;
  border-radius: var(--radius-sm);
  font-size: 1rem;
  line-height: 1;
}

/* One row per sub-item: [label] [input] */
.s2-subitem-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.35rem 0.75rem;
  border-bottom: 1px solid var(--bg-tertiary);
}
.s2-subitem-row:last-child { border-bottom: none; }

.s2-subitem-label {
  font-weight: 700;
  color: var(--accent);
  min-width: 2rem;
  flex-shrink: 0;
  font-size: 0.9rem;
  user-select: none;
}

.s2-subitem-input {
  flex: 1;
  border: none;
  border-bottom: 1px solid var(--border);
  background: transparent;
  padding: 0.2rem 0.3rem;
  font-size: 0.88rem;
  font-family: var(--font-main);
  color: var(--text-primary);
  outline: none;
  transition: border-color 0.15s;
  resize: none;
  overflow: hidden;
  line-height: 1.5;
}
.s2-subitem-input:focus { border-bottom-color: var(--accent); }

/* Freeform textarea for sections without sub-items */
.s2-body-textarea {
  width: 100%;
  min-height: 80px;
  border: none;
  padding: 0.6rem 0.75rem;
  font-size: 0.88rem;
  font-family: var(--font-main);
  color: var(--text-primary);
  background: transparent;
  resize: none;
  overflow: hidden;
  outline: none;
  line-height: 1.65;
}
.s2-body-textarea:focus { background: #fafbff; }

/* =====================================================================
   Stage 3: Reference & Answer
   ===================================================================== */
.reference-container {
  flex: 1;
  overflow-y: auto;
  min-height: 0;
  font-size: 0.88rem;
  line-height: 1.75;
}

.ref-heading { margin: 0.5rem 0 0.2rem; }
.ref-heading.level-1 { font-size: 1rem; }
.ref-heading.level-2 { font-size: 0.92rem; }
.ref-heading.level-3 { font-size: 0.88rem; }
.ref-heading.level-4 { font-size: 0.85rem; }
.ref-body { margin-bottom: 0.2rem; }

.answer-textarea {
  width: 100%;
  flex: 1;
  min-height: 280px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.85rem;
  font-size: 0.9rem;
  font-family: var(--font-main);
  color: var(--text-primary);
  background: var(--bg-tertiary);
  resize: none;
  outline: none;
  line-height: 1.7;
}

.answer-textarea:focus { border-color: var(--accent); background: #fff; }

.test-actions {
  display: flex;
  justify-content: flex-end;
  margin: 0.75rem 0;
}

/* AI feedback */
.ai-feedback-container {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  margin-top: 0.5rem;
}

.ai-feedback-container.hidden { display: none; }

.ai-feedback-container .ai-result-area {
  max-height: 400px;
  font-size: 0.87rem;
}

/* Markdown output styles inside feedback area */
.ai-result-area h2 { font-size: 1rem; margin: 0.5rem 0 0.3rem; }
.ai-result-area h3 { font-size: 0.92rem; margin: 0.4rem 0 0.2rem; color: var(--text-heading); }
.ai-result-area h4 { font-size: 0.88rem; margin: 0.3rem 0 0.15rem; }
.ai-result-area p  { margin-bottom: 0.4rem; }
.ai-result-area ul { padding-left: 1.2rem; margin-bottom: 0.4rem; }
.ai-result-area li { margin-bottom: 0.2rem; }
.ai-result-area strong { color: var(--text-heading); }

/* =====================================================================
   Right panel guide text
   ===================================================================== */
.right-guide {
  padding: 0.5rem 0 0.75rem;
  font-size: 0.82rem;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  margin-bottom: 0.75rem;
}

/* =====================================================================
   Buttons
   ===================================================================== */
.btn-primary {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  padding: 0.5rem 1.1rem;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}
.btn-primary:hover { background: var(--accent-hover); }

.btn-secondary {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.5rem 1rem;
  font-size: 0.88rem;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}
.btn-secondary:hover { border-color: var(--accent); color: var(--accent); }

.btn-ghost {
  background: transparent;
  border: none;
  padding: 0.3rem 0.6rem;
  font-size: 0.8rem;
  cursor: pointer;
  color: var(--text-muted);
  border-radius: var(--radius-sm);
}
.btn-ghost:hover { background: var(--bg-tertiary); }

.btn-ai {
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  padding: 0.4rem 0.9rem;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s;
}
.btn-ai:hover { opacity: 0.88; }

/* =====================================================================
   Loading Overlay
   ===================================================================== */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.loading-overlay.hidden { display: none; }

.loading-spinner {
  background: var(--bg-secondary);
  border-radius: var(--radius);
  padding: 2rem 3rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  box-shadow: var(--shadow);
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

#loading-message {
  font-size: 0.88rem;
  color: var(--text-muted);
}

/* =====================================================================
   Toast Notification
   ===================================================================== */
#toast {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%);
  padding: 0.65rem 1.3rem;
  border-radius: 20px;
  font-size: 0.85rem;
  font-weight: 500;
  z-index: 200;
  box-shadow: var(--shadow);
  animation: fadeInUp 0.2s ease;
}

@keyframes fadeInUp {
  from { opacity: 0; transform: translateX(-50%) translateY(8px); }
  to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}

.toast-error { background: #fff0f0; color: var(--danger); border: 1px solid #ffc9c9; }
.toast-warn  { background: #fff8ed; color: var(--warn);   border: 1px solid #ffd8a8; }
.toast-info  { background: var(--accent-light); color: var(--accent); border: 1px solid #bac8ff; }

/* =====================================================================
   Scrollbar (Webkit)
   ===================================================================== */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* =====================================================================
   Responsive: stack panels vertically below 768px
   ===================================================================== */
@media (max-width: 768px) {
  .main-container { flex-direction: column; }
  .left-panel  { border-right: none; border-bottom: 2px solid var(--border); max-height: 50vh; }
  .panel-divider { display: none; }
  body { overflow: auto; }
  #app { height: auto; }
  .stage-nav { flex-wrap: wrap; }
  .api-key-group input { width: 160px; }
}

/* =====================================================================
   Modal (shared by save-case + vault modals)
   ===================================================================== */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.5);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal.hidden { display: none; }

.modal-box {
  background: var(--bg-secondary);
  border-radius: var(--radius);
  padding: 1.5rem;
  width: 480px;
  max-width: 95vw;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  box-shadow: var(--shadow);
}

.modal-box h3 {
  font-size: 1rem;
  font-weight: 700;
  color: var(--text-heading);
}

.modal-box label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.modal-box input,
.modal-box select,
.modal-box textarea {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.45rem 0.7rem;
  font-size: 0.88rem;
  font-family: var(--font-main);
  background: var(--bg-tertiary);
  color: var(--text-primary);
  outline: none;
}

.modal-box input:focus,
.modal-box select:focus,
.modal-box textarea:focus { border-color: var(--accent); background: #fff; }

.modal-box textarea { min-height: 140px; resize: vertical; }

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 0.25rem;
}

/* =====================================================================
   Archive Panel (full-screen overlay)
   ===================================================================== */
#panel-archive {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 48px; /* side menu always visible */
  background: var(--bg-primary);
  z-index: 150;
  display: flex;
  flex-direction: column;
}
#panel-archive.hidden { display: none; }

.archive-header {
  padding: 0.75rem 1.25rem;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-shrink: 0;
  box-shadow: var(--shadow);
}

.archive-header h2 {
  font-size: 1rem;
  font-weight: 700;
  color: var(--text-heading);
}

.archive-body {
  display: flex;
  flex: 1;
  overflow: hidden;
}

.archive-sidebar {
  width: 230px;
  border-right: 1px solid var(--border);
  padding: 1rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  flex-shrink: 0;
  background: var(--bg-secondary);
}

.archive-backup-actions {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-top: auto;
  padding-top: 0.5rem;
  border-top: 1px solid var(--border);
}
.archive-backup-row {
  display: flex;
  gap: 0.5rem;
}
.archive-backup-row .btn-secondary {
  flex: 1;
  padding: 0.4rem 0.5rem;
  font-size: 0.8rem;
}

/* Drive status row */
.drive-status-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.25rem;
}
.drive-connect-btn {
  flex: 1;
  padding: 0.4rem 0.5rem;
  font-size: 0.8rem;
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s, background 0.15s;
}
.drive-connect-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}
.drive-connect-btn.drive-connected {
  background: var(--accent-light);
  color: var(--accent);
  border-color: var(--accent);
}
.drive-status-label {
  font-size: 0.75rem;
  color: var(--text-muted);
  white-space: nowrap;
}

/* Vault list items */
.archive-vault-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 0.6rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  border: 1px solid transparent;
  gap: 0.4rem;
}
.archive-vault-item:hover { background: var(--bg-tertiary); }
.archive-vault-item.active {
  background: var(--accent-light);
  border-color: var(--accent);
}

.archive-vault-name {
  font-size: 0.88rem;
  font-weight: 500;
  color: var(--text-primary);
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.archive-vault-item.active .archive-vault-name { color: var(--accent); font-weight: 600; }

/* "전체 항목" special item — slightly separated from individual vaults */
.archive-vault-all {
  border-bottom: 1px solid var(--border);
  margin-bottom: 0.35rem;
  padding-bottom: 0.6rem;
}

.archive-vault-actions { display: flex; gap: 0.15rem; flex-shrink: 0; }

/* Main content area */
.archive-main {
  flex: 1;
  padding: 1rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  min-width: 0;
}

.archive-filters {
  display: flex;
  gap: 0.5rem;
  flex-shrink: 0;
}
.archive-filters input {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.4rem 0.7rem;
  font-size: 0.85rem;
  font-family: var(--font-main);
  background: var(--bg-tertiary);
  color: var(--text-primary);
  outline: none;
}
.archive-filters input:focus { border-color: var(--accent); background: #fff; }
.archive-filters select {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.4rem 0.6rem;
  font-size: 0.83rem;
  font-family: var(--font-main);
  background: var(--bg-tertiary);
  color: var(--text-primary);
  outline: none;
  min-width: 160px;
}

/* Case cards */
#archive-case-list {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.archive-case-card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem 1rem;
  background: var(--bg-secondary);
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}
.archive-case-card:hover { border-color: var(--accent); }

/* Title row: badge + title text + hover actions in one flex line */
.case-card-title-row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.92rem;
  font-weight: 600;
  color: var(--text-heading);
  overflow: hidden;
}

.case-card-title-text {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Action buttons always in flex flow (preserves height), invisible until hover */
.case-card-hover-actions {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 0.1rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.12s;
}
.archive-case-card:hover .case-card-hover-actions {
  opacity: 1;
  pointer-events: auto;
}

/* Subtle divider between title text and hover actions */
.case-card-hover-actions .btn-ghost.btn-xs {
  color: var(--text-muted);
  font-weight: 500;
}
.case-card-hover-actions .btn-ghost.btn-xs:last-child {
  color: var(--danger);
}

.case-card-meta {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  flex-wrap: wrap;
  font-size: 0.78rem;
  color: var(--text-muted);
}
.case-meta-sep  { color: var(--border); }
.case-meta-date { margin-left: auto; font-size: 0.75rem; }

/* Type toggle buttons inside the save modal */
.type-btn-group {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.modal-field-label {
  font-size: 0.85rem;
  color: var(--text-muted);
  flex-shrink: 0;
}

.type-btn {
  padding: 0.28rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: transparent;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  color: var(--text-muted);
  font-family: var(--font-main);
  transition: all 0.12s;
}
.type-btn:hover { border-color: var(--accent); color: var(--accent); }
.type-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* Type badge on case cards */
.case-type-badge {
  display: inline-block;
  padding: 0.1rem 0.45rem;
  border-radius: 10px;
  font-size: 0.72rem;
  font-weight: 700;
  flex-shrink: 0;
}
.case-type-badge.type-사례 { background: #e8edff; color: var(--accent); }
.case-type-badge.type-쟁점 { background: #fff3e0; color: var(--warn); }
.case-type-badge.type-조문 { background: #ebfbee; color: var(--success); }

/* XS ghost buttons (inside cards and vault sidebar) */
.btn-xs {
  padding: 0.2rem 0.5rem;
  font-size: 0.75rem;
  border-radius: var(--radius-sm);
}

.archive-empty {
  font-size: 0.85rem;
  color: var(--text-muted);
  text-align: center;
  padding: 1rem 0;
}

/* =====================================================================
   Save Dropdown (appears next to 💾 side button)
   ===================================================================== */
.save-dropdown {
  position: fixed;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index: 600;
  min-width: 170px;
  overflow: hidden;
}
.save-dropdown.hidden { display: none; }

.save-dropdown-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  padding: 0.55rem 1rem;
  border: none;
  background: transparent;
  font-size: 0.85rem;
  font-family: var(--font-main);
  color: var(--text-primary);
  cursor: pointer;
  text-align: left;
  transition: background 0.1s;
}
.save-dropdown-item:hover { background: var(--bg-tertiary); }

/* =====================================================================
   Inline Vault Create (inside save modal mapping rows)
   ===================================================================== */
.inline-vault-create {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.3rem 0 0.1rem;
  animation: fadeInUp 0.12s ease;
}
.inline-vault-input {
  flex: 1;
  border: 1px solid var(--accent);
  border-radius: var(--radius-sm);
  padding: 0.32rem 0.6rem;
  font-size: 0.84rem;
  font-family: var(--font-main);
  background: var(--bg-tertiary);
  color: var(--text-primary);
  outline: none;
}
.inline-vault-input:focus { background: #fff; }

/* =====================================================================
   Selection Toolbar (floating formatting menu)
   ===================================================================== */
.selection-toolbar {
  position: fixed;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.3rem 0.4rem;
  display: flex;
  gap: 0.25rem;
  box-shadow: var(--shadow);
  z-index: 500;
  user-select: none;
}
.selection-toolbar.hidden { display: none; }

.fmt-btn {
  padding: 0.25rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: transparent;
  font-size: 0.88rem;
  cursor: pointer;
  color: var(--text-muted);
  line-height: 1;
  transition: all 0.12s;
}
.fmt-btn:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-light); }
.fmt-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* =====================================================================
   Stage 2: Fill-in Blanks
   ===================================================================== */
.fill-container {
  flex: 1;
  overflow-y: auto;
  min-height: 0;
  font-size: 0.88rem;
  line-height: 1.75;
}

.fill-right-content {
  flex: 1;
  overflow-y: auto;
  min-height: 0;
  font-size: 0.88rem;
  line-height: 1.75;
  padding-bottom: 0.5rem;
}

.fill-blank-input {
  display: inline-block;
  border: none;
  border-bottom: 2px solid var(--accent);
  padding: 0.05rem 0.3rem;
  font-family: var(--font-main);
  font-size: inherit;
  background: rgba(59, 91, 219, 0.05);
  min-width: 3ch;
  vertical-align: baseline;
  outline: none;
  transition: border-color 0.15s, background 0.15s;
}
.fill-blank-input:focus { background: rgba(59, 91, 219, 0.1); }
.fill-blank-input.correct {
  border-bottom-color: var(--success);
  background: rgba(47, 158, 68, 0.08);
}
.fill-blank-input.wrong {
  border-bottom-color: var(--danger);
  background: rgba(224, 49, 49, 0.08);
}

.fill-actions {
  flex-shrink: 0;
  display: flex;
  gap: 0.5rem;
  justify-content: flex-end;
  padding: 0.5rem 0 0;
  border-top: 1px solid var(--border);
  margin-top: 0.5rem;
}

.fill-hint {
  color: var(--text-muted);
  font-size: 0.85rem;
  font-style: italic;
  padding: 1.5rem 0;
  text-align: center;
}

/* =====================================================================
   Stage-next bar (shown at bottom of stages 0–3)
   ===================================================================== */
.stage-next-bar {
  flex-shrink: 0;
  display: flex;
  justify-content: flex-end;
  padding: 0.5rem 0 0;
  border-top: 1px solid var(--border);
  margin-top: 0.5rem;
}

/* Disabled state for next-stage buttons */
.btn-next-stage.disabled,
.btn-secondary.disabled {
  opacity: 0.45;
  cursor: not-allowed;
  pointer-events: none;
}

/* =====================================================================
   Dark Theme
   Switch with: document.documentElement.dataset.theme = 'dark'
   ===================================================================== */
[data-theme="dark"] .side-btn::after {
  background: var(--text-muted);
}

[data-theme="dark"] {
  --bg-primary:   #0f1117;
  --bg-secondary: #181c27;
  --bg-tertiary:  #1e2335;
  --border:       #2e3350;
  --text-primary: #e2e6f0;
  --text-muted:   #7a839e;
  --text-heading: #f0f4ff;
  --accent-light: #1a2340;
  --kw-orange-bg: #3d1e00;
}
