:root {
  --bg: #f5f1e8;
  --panel-bg: #ffffff;
  --ink: #1e1b16;
  --ink-muted: #6b6255;
  --border: #d8cfbe;
  --accent: #c1440e;
  --accent-ink: #ffffff;
  --warning-bg: #fff3cd;
  --warning-ink: #6b4e00;
  --info-bg: #e3f0ff;
  --info-ink: #1c4e80;
  --danger: #b3261e;
  --radius: 8px;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1a17;
    --panel-bg: #26221d;
    --ink: #f0ebe0;
    --ink-muted: #a89f8f;
    --border: #453e34;
    --accent: #e0713a;
    --accent-ink: #1c1a17;
    --warning-bg: #4a3c14;
    --warning-ink: #ffe28a;
    --info-bg: #163a52;
    --info-ink: #bfe0ff;
  }
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--ink); }
.hidden { display: none !important; }
button { font-family: inherit; cursor: pointer; }
input, textarea, select { font-family: inherit; color: var(--ink); background: var(--panel-bg); border: 1px solid var(--border); border-radius: 6px; padding: 6px 8px; }

/* ---------- Start screen ---------- */
.start-screen { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 24px; }
.start-card { background: var(--panel-bg); border: 1px solid var(--border); border-radius: 12px; padding: 32px; max-width: 420px; width: 100%; }
.start-card h1 { margin-top: 0; }
.subtitle { color: var(--ink-muted); }
.field-label { display: block; margin-top: 16px; margin-bottom: 4px; font-size: 0.85em; color: var(--ink-muted); }
.start-card input { width: 100%; margin-bottom: 4px; }
.hint { font-size: 0.8em; color: var(--ink-muted); margin-top: 12px; }
.error-box { background: var(--warning-bg); color: var(--warning-ink); padding: 10px; border-radius: 6px; margin-bottom: 8px; }

.btn-primary { width: 100%; margin-top: 16px; background: var(--accent); color: var(--accent-ink); border: none; padding: 10px; border-radius: 6px; font-weight: 600; }
.btn-secondary { background: none; border: 1px solid var(--border); padding: 6px 12px; border-radius: 6px; color: var(--ink); }
.btn-small { background: none; border: 1px solid var(--border); padding: 3px 8px; border-radius: 6px; font-size: 0.85em; color: var(--ink); }
.btn-icon { background: none; border: none; font-size: 0.95em; padding: 2px 6px; color: var(--ink-muted); }
.btn-icon.danger:hover { color: var(--danger); }

/* ---------- App shell ---------- */
.app { display: flex; flex-direction: column; min-height: 100vh; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 16px; border-bottom: 1px solid var(--border); background: var(--panel-bg); }
.project-title-input { border: none; background: none; font-size: 1.2em; font-weight: 700; flex: 1; min-width: 0; }
.project-title-input:focus { outline: 1px solid var(--border); }
.topbar-right { display: flex; align-items: center; gap: 10px; }
.status-pill { font-size: 0.8em; padding: 3px 10px; border-radius: 999px; background: var(--border); color: var(--ink-muted); }
.status-pill.status-saved { background: #d7edda; color: #1e5c2b; }
.status-pill.status-saving, .status-pill.status-dirty { background: var(--warning-bg); color: var(--warning-ink); }
.status-pill.status-error { background: #f8d7da; color: var(--danger); }

.banner { padding: 10px 16px; display: flex; align-items: center; justify-content: space-between; gap: 12px; font-size: 0.9em; }
.banner-warning { background: var(--warning-bg); color: var(--warning-ink); }
.banner-info { background: var(--info-bg); color: var(--info-ink); }

.tabs { display: flex; gap: 4px; padding: 8px 16px 0; background: var(--panel-bg); border-bottom: 1px solid var(--border); }
.tab-btn { background: none; border: none; padding: 8px 14px; border-radius: 6px 6px 0 0; color: var(--ink-muted); font-weight: 600; }
.tab-btn.active { background: var(--bg); color: var(--accent); }

.tab-content { flex: 1; overflow: auto; }
.tab-panel { padding: 16px; }

/* ---------- Script editor ---------- */
.editor-layout { display: flex; gap: 20px; align-items: flex-start; }
.sidebar { width: 260px; flex-shrink: 0; background: var(--panel-bg); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; }
.sidebar-header { display: flex; align-items: center; justify-content: space-between; margin-top: 14px; }
.sidebar-header:first-child { margin-top: 0; }
.sidebar-header h3 { margin: 0; font-size: 0.95em; }
.chip-list { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.chip-row { display: flex; align-items: center; gap: 6px; }
.chip-name { flex: 1; min-width: 0; }
.chip-type { width: 62px; }
.swatch { width: 16px; height: 16px; border-radius: 50%; flex-shrink: 0; }

.pages-column { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 20px; }
.page-block { background: var(--panel-bg); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; }
.page-header { display: flex; align-items: center; justify-content: space-between; }
.page-header h2 { margin: 0; font-size: 1.05em; }
.page-controls { display: flex; gap: 2px; }
.panels-wrap { display: flex; flex-direction: column; gap: 12px; margin-top: 10px; }

.panel-card { border: 1px solid var(--border); border-radius: 6px; padding: 10px; background: var(--bg); }
.panel-card-header { display: flex; align-items: center; gap: 10px; }
.panel-card-header strong { flex-shrink: 0; }
.panel-location { flex: 1; }
.panel-controls { display: flex; gap: 2px; margin-left: auto; }
.char-toggles { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.char-toggle { border: 2px solid var(--border); border-radius: 999px; padding: 3px 10px; font-size: 0.82em; background: none; color: var(--ink); }
.char-toggle.active { color: #fff; }
.panel-description { width: 100%; min-height: 50px; margin-top: 8px; resize: vertical; }

.dialogue-list { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; }
.dialogue-line { border-left: 3px solid var(--border); padding-left: 8px; }
.dialogue-line-head { display: flex; gap: 6px; align-items: center; }
.dialogue-line-head select { flex: 1; min-width: 0; }
.parenthetical { width: 100%; margin-top: 4px; font-style: italic; font-size: 0.85em; }
.dialogue-text { width: 100%; min-height: 40px; margin-top: 4px; resize: vertical; }

.btn-add-page, .btn-add-panel { align-self: flex-start; background: none; border: 1px dashed var(--border); border-radius: 6px; padding: 8px 12px; color: var(--ink-muted); }

/* ---------- Script view ---------- */
.script-view { max-width: 680px; margin: 0 auto; background: var(--panel-bg); border: 1px solid var(--border); border-radius: var(--radius); padding: 32px 40px; font-family: "Courier New", monospace; line-height: 1.5; }
.sv-page { border-bottom: 1px solid var(--border); padding-bottom: 4px; }
.sv-panel-head { font-weight: 700; margin-bottom: 2px; }
.sv-desc { color: var(--ink-muted); margin-top: 0; }
.sv-caption { font-style: italic; }
.sv-charname { text-align: center; font-weight: 700; margin-bottom: 0; }
.sv-paren { text-align: center; font-style: italic; margin: 0; font-size: 0.9em; }
.sv-dialogue { max-width: 380px; margin: 0 auto 12px; }

/* ---------- Storyboard ---------- */
.storyboard-wrap { display: flex; flex-direction: column; gap: 28px; }
.sb-page-label { font-weight: 700; margin-bottom: 6px; }
.sb-canvas {
  position: relative;
  width: 100%;
  max-width: 560px;
  aspect-ratio: 6.625 / 10.25;
  background: var(--panel-bg);
  border: 1px solid var(--border);
  box-shadow: 0 1px 4px rgba(0,0,0,0.08);
}
.sb-empty { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: var(--ink-muted); font-size: 0.85em; padding: 20px; text-align: center; }
.sb-panel {
  position: absolute;
  border: 2px solid var(--ink);
  background: var(--bg);
  overflow: hidden;
  touch-action: none;
}
.sb-panel-label { display: flex; gap: 6px; align-items: center; padding: 3px 6px; background: rgba(0,0,0,0.06); font-size: 0.72em; cursor: move; user-select: none; }
.sb-panel-num { font-weight: 700; }
.sb-panel-loc { color: var(--ink-muted); }

.sb-bubble {
  position: absolute;
  background: var(--panel-bg);
  border: 2px solid var(--ink);
  border-radius: 14px;
  padding: 4px 8px;
  font-size: 0.72em;
  overflow: auto;
  touch-action: none;
  box-shadow: 0 1px 2px rgba(0,0,0,0.12);
}
.sb-bubble-caption { border-radius: 3px; background: #fff7d6; }
.sb-bubble-sfx { border-style: dashed; font-weight: 700; text-transform: uppercase; }
.sb-bubble-head { font-weight: 700; cursor: move; user-select: none; margin-bottom: 2px; }
.sb-bubble-text { white-space: pre-wrap; }

.resize-handle {
  position: absolute; right: 0; bottom: 0; width: 12px; height: 12px;
  cursor: nwse-resize; background: linear-gradient(135deg, transparent 50%, var(--ink-muted) 50%);
  touch-action: none;
}

/* ---------- Responsive ---------- */
@media (max-width: 780px) {
  .editor-layout { flex-direction: column; }
  .sidebar { width: 100%; }
  .script-view { padding: 20px; }
}
