/* ============================================================
   Forge — Application styles
   Layer order: reset → shell → components → utilities → responsive
   ============================================================ */

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

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: var(--fs-base);
  line-height: var(--lh-base);
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: none; padding: 0; }

h1, h2, h3, h4 { margin: 0; font-weight: 620; line-height: var(--lh-tight); letter-spacing: var(--tracking-tight); }
p { margin: 0; }
ul, ol { margin: 0; padding: 0; list-style: none; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--r-sm);
}

::selection { background: var(--accent-soft); color: var(--accent-text); }

/* ---------- App shell ---------- */
.app {
  display: grid;
  grid-template-columns: var(--rail-w) minmax(0, 1fr) var(--preview-w);
  grid-template-rows: var(--topbar-h) minmax(0, 1fr);
  grid-template-areas:
    "top top top"
    "rail editor preview";
  height: 100dvh;
  overflow: hidden;
}

.app.preview-collapsed { grid-template-columns: var(--rail-w) minmax(0, 1fr) 0; }
.app.preview-collapsed .preview { display: none; }

/* ---------- Topbar ---------- */
.topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: var(--sp-4);
  padding: 0 var(--sp-4);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 30;
}

.brand { display: flex; align-items: center; gap: var(--sp-3); min-width: 0; }

.brand-mark {
  display: grid;
  place-items: center;
  width: 30px; height: 30px;
  border-radius: var(--r-md);
  background: var(--accent);
  color: var(--text-inverse);
  flex: none;
}
.brand-mark svg { width: 17px; height: 17px; }

.brand-text { display: flex; flex-direction: column; line-height: 1.15; min-width: 0; }
.brand-text strong { font-size: var(--fs-md); letter-spacing: var(--tracking-tight); }
.brand-text span {
  font-size: var(--fs-xs);
  color: var(--text-3);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-spacer { flex: 1 1 auto; }
.topbar-actions { display: flex; align-items: center; gap: var(--sp-2); }

/* ---------- Segmented control ---------- */
.segmented {
  display: inline-flex;
  padding: 3px;
  gap: 2px;
  background: var(--surface-sunken);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
}
.seg {
  padding: 5px 12px;
  border-radius: var(--r-sm);
  font-size: var(--fs-sm);
  font-weight: 550;
  color: var(--text-2);
  white-space: nowrap;
  transition: background var(--dur-fast) var(--ease-out), color var(--dur-fast) var(--ease-out);
}
.seg:hover { color: var(--text); }
.seg[aria-selected="true"] {
  background: var(--surface);
  color: var(--accent-text);
  box-shadow: var(--shadow-sm);
}

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 32px;
  padding: 0 12px;
  border-radius: var(--r-md);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: var(--fs-sm);
  font-weight: 550;
  white-space: nowrap;
  transition: background var(--dur-fast) var(--ease-out), border-color var(--dur-fast) var(--ease-out),
    transform var(--dur-fast) var(--ease-out), color var(--dur-fast) var(--ease-out);
}
.btn:hover { background: var(--surface-2); border-color: var(--border-strong); }
.btn:active { transform: scale(0.985); }
.btn svg { width: 15px; height: 15px; flex: none; }

.btn.ghost { border-color: transparent; background: transparent; color: var(--text-2); }
.btn.ghost:hover { background: var(--surface-sunken); color: var(--text); border-color: transparent; }

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--text-inverse);
}
.btn.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }

.btn.icon { width: 32px; padding: 0; }
.btn.sm { height: 27px; padding: 0 9px; font-size: var(--fs-xs); }
.btn:disabled { opacity: 0.45; pointer-events: none; }

/* ---------- Rail ---------- */
.rail {
  grid-area: rail;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--surface-2);
  border-right: 1px solid var(--border);
}

.rail-head { padding: var(--sp-4) var(--sp-3) var(--sp-3); border-bottom: 1px solid var(--border-soft); }

.progress-block { display: flex; align-items: center; gap: var(--sp-3); }
.ring { width: 46px; height: 46px; flex: none; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 5; stroke-linecap: round; }
.ring .track { stroke: var(--surface-sunken); }
.ring .bar {
  stroke: var(--accent);
  transition: stroke-dashoffset var(--dur-slow) var(--ease-out);
}
.progress-meta { min-width: 0; }
.progress-meta strong { display: block; font-size: var(--fs-lg); letter-spacing: var(--tracking-tight); }
.progress-meta span { font-size: var(--fs-xs); color: var(--text-3); }

.rail-tools { display: flex; gap: var(--sp-2); margin-top: var(--sp-3); }
.rail-tools .btn { flex: 1; }

.rail-nav { flex: 1 1 auto; overflow-y: auto; padding: var(--sp-2) var(--sp-2) var(--sp-6); }

.nav-group-label {
  padding: var(--sp-3) var(--sp-3) var(--sp-1);
  font-size: var(--fs-xs);
  font-weight: 600;
  letter-spacing: var(--tracking-wide);
  color: var(--text-3);
  text-transform: uppercase;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  width: 100%;
  padding: 6px var(--sp-3);
  border-radius: var(--r-sm);
  font-size: var(--fs-sm);
  color: var(--text-2);
  text-align: left;
  transition: background var(--dur-fast) var(--ease-out), color var(--dur-fast) var(--ease-out);
}
.nav-item:hover { background: var(--surface-sunken); color: var(--text); }
.nav-item[aria-current="true"] { background: var(--accent-soft); color: var(--accent-text); font-weight: 580; }
.nav-item .idx {
  flex: none;
  width: 17px;
  font-size: var(--fs-xs);
  font-variant-numeric: tabular-nums;
  color: var(--text-3);
  font-weight: 600;
}
.nav-item[aria-current="true"] .idx { color: var(--accent); }
.nav-item .label { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-item .dot {
  flex: none; width: 6px; height: 6px; border-radius: var(--r-full);
  background: var(--border-strong);
}
.nav-item[data-state="partial"] .dot { background: var(--warn); }
.nav-item[data-state="done"] .dot { background: var(--accent); }

/* ---------- Editor ---------- */
.editor {
  grid-area: editor;
  min-width: 0;
  overflow-y: auto;
  scroll-behavior: smooth;
  padding: var(--sp-7) var(--sp-7) 45vh;
}

.editor-inner { max-width: 760px; margin: 0 auto; }

.editor-title { margin-bottom: var(--sp-2); }
.editor-title h1 { font-size: var(--fs-2xl); }
.editor-lede { color: var(--text-2); font-size: var(--fs-md); max-width: 62ch; }
.editor-lede + .section { margin-top: var(--sp-7); }

.section {
  border-top: 1px solid var(--border);
  padding: var(--sp-6) 0 var(--sp-2);
  scroll-margin-top: var(--sp-5);
}
.section:first-of-type { border-top: none; }

.section-head { display: flex; align-items: baseline; gap: var(--sp-3); margin-bottom: var(--sp-5); }
.section-key {
  flex: none;
  display: grid; place-items: center;
  width: 26px; height: 26px;
  border-radius: var(--r-sm);
  background: var(--surface-sunken);
  border: 1px solid var(--border);
  font-size: var(--fs-xs);
  font-weight: 700;
  color: var(--text-2);
  font-variant-numeric: tabular-nums;
}
.section-head h2 { font-size: var(--fs-xl); }
.section-head .sub { font-size: var(--fs-sm); color: var(--text-3); }

/* ---------- Fields ---------- */
.field { margin-bottom: var(--sp-5); }
.field.hidden { display: none; }

.field-label {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 5px;
  font-size: var(--fs-sm);
  font-weight: 580;
}
.field-label .req { color: var(--accent); font-size: var(--fs-sm); }
.field-hint { margin-bottom: var(--sp-2); font-size: var(--fs-xs); color: var(--text-3); line-height: var(--lh-snug); }

.input, .textarea {
  width: 100%;
  padding: 8px 11px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  color: var(--text);
  transition: border-color var(--dur-fast) var(--ease-out), box-shadow var(--dur-fast) var(--ease-out);
}
.input::placeholder, .textarea::placeholder { color: var(--text-3); }
.input:hover, .textarea:hover { border-color: var(--border-strong); }
.input:focus, .textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: var(--focus-ring);
}

.textarea { resize: vertical; min-height: 62px; line-height: var(--lh-snug); }
.input.short { max-width: 320px; }

/* Choice / tristate */
.choice-row { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  padding: 5px 11px;
  border: 1px solid var(--border);
  border-radius: var(--r-full);
  background: var(--surface);
  font-size: var(--fs-sm);
  color: var(--text-2);
  transition: all var(--dur-fast) var(--ease-out);
}
.chip:hover { border-color: var(--border-strong); color: var(--text); }
.chip[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--text-inverse);
  font-weight: 550;
}

/* List field */
.list-rows { display: flex; flex-direction: column; gap: 6px; }
.list-row { display: flex; align-items: flex-start; gap: 6px; }
.list-row .num {
  flex: none;
  display: grid; place-items: center;
  width: 22px; height: 34px;
  font-size: var(--fs-xs);
  font-variant-numeric: tabular-nums;
  color: var(--text-3);
}
.list-row .input { flex: 1 1 auto; }
.row-del {
  flex: none;
  display: grid; place-items: center;
  width: 30px; height: 34px;
  border-radius: var(--r-sm);
  color: var(--text-3);
  transition: color var(--dur-fast) var(--ease-out), background var(--dur-fast) var(--ease-out);
}
.row-del:hover { color: var(--danger); background: var(--danger-soft); }
.row-del svg { width: 14px; height: 14px; }

.list-add { margin-top: 6px; }

/* Presets（常用敘述） */
.presets { margin-top: 7px; }
.preset-toggle {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 25px;
  padding: 0 5px 0 8px;
  border: 1px dashed var(--border-strong);
  border-radius: var(--r-full);
  background: transparent;
  font-size: var(--fs-xs);
  color: var(--text-3);
  transition: color var(--dur-fast) var(--ease-out),
              border-color var(--dur-fast) var(--ease-out),
              background var(--dur-fast) var(--ease-out);
}
.preset-toggle:hover {
  color: var(--accent-text);
  border-color: var(--accent-border);
  background: var(--accent-soft);
}
.preset-toggle svg { width: 13px; height: 13px; flex: none; }
.preset-count {
  display: grid;
  place-items: center;
  min-width: 17px;
  height: 17px;
  padding: 0 4px;
  border-radius: var(--r-full);
  background: var(--surface-sunken);
  font-size: 10px;
  font-variant-numeric: tabular-nums;
  color: var(--text-3);
}
.preset-row {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin-top: 6px;
  padding: 8px;
  border: 1px solid var(--border-soft);
  border-radius: var(--r-md);
  background: var(--surface-2);
}
.preset-row[hidden] { display: none; }
.preset {
  max-width: 100%;
  padding: 5px 9px;
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  background: var(--surface);
  font-size: var(--fs-xs);
  line-height: var(--lh-snug);
  color: var(--text-2);
  text-align: left;
  white-space: pre-wrap;
  transition: color var(--dur-fast) var(--ease-out),
              border-color var(--dur-fast) var(--ease-out),
              background var(--dur-fast) var(--ease-out);
}
.preset:hover {
  color: var(--accent-text);
  border-color: var(--accent-border);
  background: var(--accent-soft);
}

/* ---------- Preview ---------- */
.preview {
  grid-area: preview;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--surface);
  border-left: 1px solid var(--border);
}

.preview-head { padding: var(--sp-3) var(--sp-4) 0; border-bottom: 1px solid var(--border); }

.preview-top { display: flex; align-items: center; gap: var(--sp-2); margin-bottom: var(--sp-3); }
.preview-top h2 { font-size: var(--fs-md); flex: 1 1 auto; }
.preview-meta { font-size: var(--fs-xs); color: var(--text-3); font-variant-numeric: tabular-nums; }

.tabs { display: flex; gap: var(--sp-4); }
.tab {
  position: relative;
  padding: 0 0 9px;
  font-size: var(--fs-sm);
  font-weight: 550;
  color: var(--text-3);
  transition: color var(--dur-fast) var(--ease-out);
}
.tab:hover { color: var(--text); }
.tab[aria-selected="true"] { color: var(--accent-text); }
.tab[aria-selected="true"]::after {
  content: "";
  position: absolute; left: 0; right: 0; bottom: -1px;
  height: 2px;
  background: var(--accent);
  border-radius: var(--r-full) var(--r-full) 0 0;
}

.options-row {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-3);
  padding: var(--sp-3) 0;
  border-top: 1px dashed var(--border);
  margin-top: var(--sp-1);
}
.opt { display: flex; flex-direction: column; gap: 3px; }
.opt label { font-size: var(--fs-xs); color: var(--text-3); font-weight: 550; }
.opt select {
  height: 28px;
  padding: 0 6px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  font-size: var(--fs-sm);
  color: var(--text);
}
.opt select:focus { outline: none; border-color: var(--accent); box-shadow: var(--focus-ring); }

.preview-body { flex: 1 1 auto; min-height: 0; overflow-y: auto; background: var(--preview-bg); }

.doc {
  margin: 0;
  padding: var(--sp-5) var(--sp-5) var(--sp-9);
  font-family: var(--font-mono);
  font-size: 12px;
  line-height: 1.72;
  color: var(--text);
  white-space: pre-wrap;
  word-break: break-word;
  tab-size: 2;
}

.doc-empty {
  padding: var(--sp-9) var(--sp-5);
  text-align: center;
  color: var(--text-3);
  font-size: var(--fs-sm);
}

/* 4-D report */
.report { padding: var(--sp-5) var(--sp-6) var(--sp-9); }
.report h3 {
  font-size: var(--fs-xs);
  letter-spacing: var(--tracking-wide);
  text-transform: uppercase;
  color: var(--text-3);
  margin: var(--sp-6) 0 var(--sp-3);
}
.report h3:first-child { margin-top: 0; }
.report p { font-size: var(--fs-sm); color: var(--text-2); margin-bottom: var(--sp-2); }
.report ul { display: flex; flex-direction: column; gap: 5px; }
.report li {
  font-size: var(--fs-sm);
  color: var(--text);
  padding-left: 15px;
  position: relative;
}
.report li::before {
  content: "";
  position: absolute; left: 3px; top: 9px;
  width: 4px; height: 4px;
  border-radius: var(--r-full);
  background: var(--border-strong);
}
.report li.ok::before { background: var(--accent); }
.report li.warn::before { background: var(--warn); }

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(96px, 1fr));
  gap: var(--sp-2);
  margin-bottom: var(--sp-2);
}
.stat {
  padding: var(--sp-3);
  background: var(--surface-2);
  border: 1px solid var(--border-soft);
  border-radius: var(--r-md);
}
.stat strong {
  display: block;
  font-size: var(--fs-lg);
  letter-spacing: var(--tracking-tight);
  font-variant-numeric: tabular-nums;
}
.stat span { font-size: var(--fs-xs); color: var(--text-3); }

.note {
  margin-top: var(--sp-4);
  padding: var(--sp-3) var(--sp-4);
  background: var(--accent-soft);
  border: 1px solid var(--accent-border);
  border-radius: var(--r-md);
  font-size: var(--fs-sm);
  color: var(--accent-text);
}
.note.warn { background: var(--warn-soft); border-color: var(--warn-border); color: var(--warn); }

/* ---------- Toast ---------- */
.toast-host {
  position: fixed;
  bottom: var(--sp-5);
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
  z-index: 90;
  pointer-events: none;
}
.toast {
  padding: 9px 15px;
  background: var(--text);
  color: var(--bg);
  border-radius: var(--r-full);
  font-size: var(--fs-sm);
  font-weight: 550;
  box-shadow: var(--shadow-lg);
  animation: toast-in var(--dur-base) var(--ease-out);
}
@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px) scale(0.97); }
  to { opacity: 1; transform: none; }
}

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(20, 20, 18, 0.42);
  backdrop-filter: blur(2px);
  display: grid;
  place-items: center;
  padding: var(--sp-4);
  z-index: 80;
  animation: fade-in var(--dur-base) var(--ease-out);
}
.modal-backdrop[hidden] { display: none; }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

.modal {
  width: min(460px, 100%);
  max-height: 80dvh;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}
.modal-head {
  display: flex; align-items: center; gap: var(--sp-3);
  padding: var(--sp-4);
  border-bottom: 1px solid var(--border);
}
.modal-head h2 { font-size: var(--fs-md); flex: 1 1 auto; }
.modal-body { padding: var(--sp-4); overflow-y: auto; }
.modal-foot {
  display: flex; gap: var(--sp-2); justify-content: flex-end;
  padding: var(--sp-3) var(--sp-4);
  border-top: 1px solid var(--border);
  background: var(--surface-2);
}

.draft-list { display: flex; flex-direction: column; gap: var(--sp-2); }
.draft-item {
  display: flex; align-items: center; gap: var(--sp-3);
  padding: var(--sp-3);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: var(--surface-2);
}
.draft-item .info { flex: 1 1 auto; min-width: 0; }
.draft-item .info strong { display: block; font-size: var(--fs-sm); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.draft-item .info span { font-size: var(--fs-xs); color: var(--text-3); }
.empty-note { text-align: center; color: var(--text-3); font-size: var(--fs-sm); padding: var(--sp-6) 0; }

.field-inline { display: flex; gap: var(--sp-2); margin-bottom: var(--sp-4); }
.field-inline .input { flex: 1 1 auto; }

/* ---------- AI 優化 ---------- */
.modal.wide { width: min(760px, 100%); }
.modal-foot .foot-left { flex: 1 1 auto; }

.ai-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 24px;
  padding: 0 4px 0 9px;
  border-radius: var(--r-full);
  background: var(--accent-soft);
  border: 1px solid var(--accent-border);
  color: var(--accent-text);
  font-size: var(--fs-xs);
  font-weight: 550;
}
.ai-badge[hidden] { display: none; }
.ai-badge svg { flex: none; }
.link-btn {
  padding: 0 7px;
  height: 18px;
  border-radius: var(--r-full);
  background: var(--surface);
  border: 1px solid var(--accent-border);
  font-size: 11px;
  font-weight: 550;
  color: var(--accent-text);
}
.link-btn:hover { background: var(--accent); border-color: var(--accent); color: var(--text-inverse); }

/* 設定表單 */
.set-row { display: flex; flex-direction: column; gap: 4px; margin-bottom: var(--sp-3); }
.set-row[hidden] { display: none; }
.set-label { font-size: var(--fs-xs); font-weight: 550; color: var(--text-3); }
.set-hint { font-size: var(--fs-xs); color: var(--text-3); line-height: var(--lh-snug); }
.set-hint.warn-text { color: var(--warn); }
.set-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: var(--sp-3); }

select.input { height: 32px; padding: 0 8px; cursor: pointer; }

.set-note {
  margin-top: var(--sp-2);
  padding: var(--sp-3);
  border-radius: var(--r-md);
  background: var(--surface-2);
  border: 1px solid var(--border-soft);
  font-size: var(--fs-xs);
  color: var(--text-3);
  line-height: var(--lh-snug);
}

.set-status {
  margin-top: var(--sp-2);
  font-size: var(--fs-xs);
  line-height: var(--lh-snug);
  color: var(--text-2);
  white-space: pre-wrap;
  word-break: break-word;
}
.set-status.ok { color: var(--accent-text); }
.set-status.err { color: var(--danger); }

/* Lyra 自我介紹 */
.lyra {
  padding: var(--sp-3) var(--sp-4);
  margin-bottom: var(--sp-4);
  border-radius: var(--r-md);
  background: var(--accent-soft);
  border: 1px solid var(--accent-border);
}
.lyra-head { display: flex; align-items: center; gap: 6px; color: var(--accent-text); font-size: var(--fs-sm); margin-bottom: 6px; }
.lyra-head svg { flex: none; }
.lyra-text {
  margin: 0;
  font-size: var(--fs-xs);
  line-height: 1.7;
  color: var(--text-2);
  white-space: pre-wrap;
  font-family: var(--font-sans);
}

/* 前置提示詞檢視 */
.aimeta { margin: var(--sp-3) 0; font-size: var(--fs-xs); }
.aimeta summary { cursor: pointer; color: var(--text-3); font-weight: 550; padding: 4px 0; }
.aimeta summary:hover { color: var(--text-2); }
.ai-preprompt {
  margin: var(--sp-2) 0 0;
  max-height: 220px;
  overflow-y: auto;
  padding: var(--sp-3);
  background: var(--surface-sunken);
  border: 1px solid var(--border-soft);
  border-radius: var(--r-sm);
  font-size: 11px;
  line-height: 1.65;
  color: var(--text-2);
  white-space: pre-wrap;
  font-family: var(--font-sans);
}

/* 狀態與結果 */
.ai-status {
  margin-top: var(--sp-3);
  font-size: var(--fs-sm);
  line-height: var(--lh-snug);
}
.ai-status.loading { display: flex; align-items: center; gap: 8px; color: var(--text-2); }
.ai-status.ok strong, .ai-status.err strong { display: block; margin-bottom: 3px; }
.ai-status.err { color: var(--danger); }
.ai-status.err div { white-space: pre-wrap; }

.spinner {
  flex: none;
  width: 15px; height: 15px;
  border: 2px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: var(--r-full);
  animation: spin 700ms linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  .spinner { animation-duration: 2.4s; }
}

.ai-result {
  margin-top: var(--sp-3);
  max-height: 46vh;
  overflow: auto;
  padding: var(--sp-4);
  background: var(--surface-sunken);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  font-size: 12px;
}

/* ---------- Mobile preview toggle ---------- */
.fab {
  display: none;
  position: fixed;
  right: var(--sp-4);
  bottom: var(--sp-4);
  z-index: 40;
  height: 44px;
  padding: 0 var(--sp-4);
  border-radius: var(--r-full);
  background: var(--accent);
  color: var(--text-inverse);
  font-size: var(--fs-sm);
  font-weight: 580;
  box-shadow: var(--shadow-lg);
  align-items: center;
  gap: 7px;
}

/* ---------- Responsive ---------- */
@media (max-width: 1180px) {
  :root { --preview-w: 440px; }
}

@media (max-width: 1000px) {
  .app {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: var(--topbar-h) minmax(0, 1fr);
    grid-template-areas: "top" "editor";
  }
  .rail {
    position: fixed;
    top: var(--topbar-h);
    left: 0; bottom: 0;
    width: 258px;
    transform: translateX(-100%);
    transition: transform var(--dur-base) var(--ease-out);
    z-index: 50;
    box-shadow: var(--shadow-lg);
  }
  .app.rail-open .rail { transform: none; }
  .preview {
    position: fixed;
    inset: var(--topbar-h) 0 0 0;
    z-index: 45;
    transform: translateX(100%);
    transition: transform var(--dur-base) var(--ease-out);
    border-left: none;
  }
  .app.preview-open .preview { transform: none; }
  .app:not(.preview-open) .preview { display: flex; }
  .app.preview-collapsed .preview { display: flex; }
  .fab { display: inline-flex; }
  .editor { padding: var(--sp-5) var(--sp-4) 30vh; }
  .btn-rail-toggle { display: inline-flex !important; }
}

.btn-rail-toggle { display: none; }
.desktop-only { display: none; }

@media (min-width: 1001px) {
  .desktop-only { display: inline-flex; }
}

@media (min-width: 1001px) {
  .preview-close { display: none; }
}

.scrim {
  position: fixed;
  inset: var(--topbar-h) 0 0 0;
  background: rgba(20, 20, 18, 0.32);
  z-index: 44;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--dur-base) var(--ease-out);
}
.app.preview-open .scrim { opacity: 1; pointer-events: auto; }

/* ---------- Scrollbars ---------- */
* { scrollbar-width: thin; scrollbar-color: var(--border-strong) transparent; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: var(--r-full);
  border: 3px solid transparent;
  background-clip: content-box;
}
::-webkit-scrollbar-thumb:hover { background: var(--text-3); background-clip: content-box; }
