:root {
  --bg: #121212;
  --bg-soft: #1a1a1a;
  --panel: rgba(27, 27, 27, 0.92);
  --panel-strong: rgba(34, 34, 34, 0.98);
  --line: rgba(255, 255, 255, 0.08);
  --line-strong: rgba(255, 255, 255, 0.14);
  --text: #f7f7f7;
  --text-muted: #a7a7a7;
  --accent: #ff7a1a;
  --accent-soft: rgba(255, 122, 26, 0.18);
  --success: #51d88a;
  --warning: #ffb020;
  --danger: #ff6363;
  --shadow: 0 24px 60px rgba(0, 0, 0, 0.38);
  --radius-lg: 18px;
  --radius-md: 12px;
  --radius-sm: 8px;
  --mono: "SFMono-Regular", "SF Mono", "IBM Plex Mono", "Fira Code", monospace;
  --sans: "Inter", "Segoe UI", sans-serif;
  --toolbar-h: 62px;
  --footer-h: 34px;
  --list-w: 220px;
  --preview-w: 420px;
  --preview-splitter-w: 10px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background:
    radial-gradient(circle at top left, rgba(255, 122, 26, 0.12), transparent 30%),
    linear-gradient(180deg, #131313 0%, #101010 55%, #0c0c0c 100%);
  color: var(--text);
  font-family: var(--sans);
  font-size: 14px;
  overflow: auto;
}

body {
  display: grid;
  grid-template-rows: var(--toolbar-h) minmax(0, 1fr) var(--footer-h);
  min-height: 100vh;
}

/* ========== Toolbar ========== */
.toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 14px;
  background: var(--panel-strong);
  border-bottom: 1px solid var(--line);
  height: var(--toolbar-h);
  flex-wrap: nowrap;
  overflow-x: auto;
}

.toolbar .brand {
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding-right: 6px;
  white-space: nowrap;
}
.toolbar .brand-title {
  font-weight: 700;
  letter-spacing: -0.02em;
  font-size: 0.96rem;
}
.toolbar .brand .dot {
  display: inline-block;
  width: 8px; height: 8px;
  background: var(--accent);
  border-radius: 999px;
  margin-right: 8px;
  vertical-align: middle;
}
.toolbar .brand-version {
  margin-left: 16px;
  margin-top: 2px;
  color: var(--success);
  font-size: 0.67rem;
  font-family: var(--mono);
  letter-spacing: 0.02em;
}

.toolbar .spacer { flex: 1; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: 999px;
  border: 1px solid var(--line-strong);
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
  font-size: 0.78rem;
  cursor: pointer;
  white-space: nowrap;
  position: relative;
  transition: background 140ms ease, border-color 140ms ease;
}
.chip:hover { background: rgba(255, 255, 255, 0.08); }
.chip .swatch {
  width: 12px; height: 12px; border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.25);
}

.popover {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  background: var(--panel-strong);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-md);
  padding: 10px;
  box-shadow: var(--shadow);
  z-index: 20;
  min-width: 180px;
  display: grid;
  gap: 8px;
}
.popover label { font-size: 0.72rem; color: var(--text-muted); }
.popover input[type="color"] { width: 100%; height: 32px; padding: 2px; border-radius: 6px; border: 1px solid var(--line-strong); background: rgba(255,255,255,0.04); }
.popover input[type="number"] { width: 100%; padding: 6px 8px; border-radius: 6px; border: 1px solid var(--line-strong); background: rgba(255,255,255,0.04); color: var(--text); font: inherit; }
.popover input[type="text"] { width: 100%; padding: 6px 8px; border-radius: 6px; border: 1px solid var(--line-strong); background: rgba(255,255,255,0.04); color: var(--text); font: inherit; }
.color-popover-grid { display: grid; gap: 8px; }
.font-popover-grid { display: grid; gap: 10px; }
.font-slot { display: grid; gap: 6px; padding: 2px 0; }
.font-slot-head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.font-slot-label { font-size: 0.76rem; color: var(--text); font-weight: 600; }
.font-slot-state { font-size: 0.72rem; color: var(--text-muted); text-align: right; }
.font-slot-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.hex-input { font-family: var(--mono); text-transform: uppercase; }

.seg {
  display: inline-flex;
  padding: 3px;
  border-radius: 999px;
  background: rgba(255,255,255,0.05);
  border: 1px solid var(--line-strong);
  gap: 2px;
}
.seg button {
  background: transparent;
  border: 0;
  color: var(--text-muted);
  padding: 4px 10px;
  border-radius: 999px;
  font: inherit;
  font-size: 0.76rem;
  cursor: pointer;
}
.seg button.active {
  background: linear-gradient(135deg, var(--accent), #ff9348);
  color: #130d08;
  font-weight: 700;
}

.tb-btn {
  appearance: none;
  border: 1px solid var(--line-strong);
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
  padding: 6px 10px;
  border-radius: 8px;
  font: inherit;
  font-size: 0.78rem;
  cursor: pointer;
  white-space: nowrap;
}
.tb-btn:hover:not(:disabled) { background: rgba(255,255,255,0.08); }
.tb-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.tb-btn.primary {
  background: linear-gradient(135deg, var(--accent), #ff954c);
  color: #1b1209;
  border-color: transparent;
  font-weight: 700;
}

.undo-redo { display: inline-flex; gap: 4px; }

/* ========== Workspace ========== */
.workspace {
  display: grid;
  grid-template-columns: var(--list-w) minmax(0, 1fr) var(--preview-splitter-w) var(--preview-w);
  min-height: 0;
  overflow: auto;
}

.preview-splitter {
  position: relative;
  cursor: col-resize;
  background: linear-gradient(180deg, rgba(255,255,255,0.04), rgba(255,255,255,0.02));
  border-left: 1px solid var(--line);
  border-right: 1px solid rgba(255,255,255,0.03);
}

.preview-splitter::before {
  content: "";
  position: absolute;
  inset: 0;
  margin: auto;
  width: 2px;
  height: 48px;
  border-radius: 999px;
  background: rgba(255,255,255,0.16);
}

.preview-splitter:hover::before,
.preview-splitter.is-dragging::before {
  background: var(--accent);
}

/* ========== Story list ========== */
.story-list {
  border-right: 1px solid var(--line);
  background: rgba(20, 20, 20, 0.6);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: auto;
}
.story-list .list-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 8px 6px;
}
.story-row {
  display: grid;
  grid-template-columns: 14px 10px minmax(0, 1fr);
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  user-select: none;
  font-size: 0.84rem;
  border: 1px solid transparent;
}
.story-row:hover { background: rgba(255, 255, 255, 0.04); }
.story-row.selected {
  background: rgba(255, 122, 26, 0.14);
  border-color: rgba(255, 122, 26, 0.28);
}
.story-row.drag-over-top { box-shadow: inset 0 2px 0 0 var(--accent); }
.story-row.drag-over-bottom { box-shadow: inset 0 -2px 0 0 var(--accent); }
.story-row .handle {
  cursor: grab;
  color: var(--text-muted);
  font-size: 0.9rem;
  line-height: 1;
}
.story-row .handle:active { cursor: grabbing; }
.story-row .status-dot {
  width: 8px; height: 8px; border-radius: 999px;
  background: rgba(255,255,255,0.25);
}
.story-row .status-dot.ready { background: var(--success); }
.story-row .status-dot.invalid { background: var(--warning); }
.story-row .name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.drop-zone {
  margin: 8px;
  padding: 14px 10px;
  border: 1px dashed var(--line-strong);
  border-radius: 10px;
  text-align: center;
  font-size: 0.74rem;
  color: var(--text-muted);
  line-height: 1.4;
  transition: background 140ms ease, border-color 140ms ease;
}
.drop-zone.hover {
  background: rgba(255, 122, 26, 0.1);
  border-color: var(--accent);
  color: var(--text);
}

/* ========== Editor ========== */
.editor {
  padding: 18px 22px;
  overflow-y: auto;
  overflow-x: hidden;
  min-height: 0;
}
.editor h3 {
  margin: 0 0 14px;
  font-size: 1rem;
  letter-spacing: -0.02em;
}
.field {
  display: grid;
  gap: 6px;
  margin-bottom: 14px;
}
.field label {
  font-size: 0.78rem;
  color: var(--text-muted);
}
.field input[type="text"],
.field input[type="number"],
.field select {
  width: 100%;
  max-width: 360px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.03);
  color: var(--text);
  padding: 9px 11px;
  font: inherit;
}
.field select {
  appearance: none;
}
.field input:focus,
.field select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(255, 122, 26, 0.14);
}
.field .help {
  font-size: 0.72rem;
  color: var(--text-muted);
}

.media-row {
  display: grid;
  gap: 8px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(255,255,255,0.02);
  margin-bottom: 10px;
  max-width: 480px;
}
.media-row .media-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}
.media-row .media-label {
  font-size: 0.8rem;
  font-weight: 600;
}
.media-row .media-state {
  font-size: 0.72rem;
  color: var(--text-muted);
}
.media-row .media-state.ready { color: var(--success); }
.media-row .media-state.invalid { color: var(--warning); }
.media-row .media-actions { display: flex; gap: 6px; flex-wrap: wrap; }

.btn {
  appearance: none;
  border: 1px solid var(--line-strong);
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
  padding: 7px 11px;
  border-radius: 8px;
  font: inherit;
  font-size: 0.78rem;
  cursor: pointer;
}
.btn:hover { background: rgba(255, 255, 255, 0.08); }
.btn.danger { color: var(--danger); border-color: rgba(255,99,99,0.3); }
.btn.danger:hover { background: rgba(255, 99, 99, 0.12); }

.editor-actions { margin-top: 18px; display: flex; gap: 8px; flex-wrap: wrap; }

.editor .empty-state {
  color: var(--text-muted);
  padding: 24px 0;
  font-size: 0.86rem;
}

input[type="file"].hidden-file { display: none; }

.error-text {
  font-size: 0.72rem;
  color: var(--warning);
  margin-top: 4px;
  word-break: break-word;
}

/* ========== Preview ========== */
.preview {
  border-left: 1px solid var(--line);
  background: rgba(15, 15, 15, 0.96);
  padding: 18px 14px;
  overflow-y: auto;
  overflow-x: hidden;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.preview .preview-caption {
  font-size: 0.72rem;
  color: var(--text-muted);
  line-height: 1.4;
}
#previewMount {
  min-height: 160px;
}

.preview-stage {
  flex: 1;
  min-height: 240px;
  position: relative;
  overflow: hidden;
  border-radius: 16px;
  background:
    radial-gradient(circle at top, rgba(255, 122, 26, 0.12), transparent 36%),
    linear-gradient(180deg, rgba(255,255,255,0.02), rgba(255,255,255,0.01));
  border: 1px solid rgba(255,255,255,0.06);
}

/* ========== Footer ========== */
.footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 14px;
  background: var(--panel-strong);
  border-top: 1px solid var(--line);
  font-size: 0.74rem;
  color: var(--text-muted);
  height: var(--footer-h);
}
.footer .saved-ok { color: var(--success); }

/* ========== Modal ========== */
.modal-root {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.6);
  display: grid;
  place-items: center;
  z-index: 100;
}
.modal-root[hidden] { display: none; }
.modal {
  width: min(560px, 94vw);
  max-height: 80vh;
  background: var(--panel-strong);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 18px;
  display: grid;
  gap: 10px;
  overflow: auto;
}
.modal h3 { margin: 0; font-size: 1rem; }
.modal textarea {
  width: 100%;
  min-height: 220px;
  background: rgba(0,0,0,0.4);
  border: 1px solid var(--line-strong);
  color: var(--text);
  border-radius: 8px;
  padding: 10px;
  font-family: var(--mono);
  font-size: 0.78rem;
  resize: vertical;
}
.modal .modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}
.modal .summary {
  font-size: 0.8rem;
  color: var(--success);
}
.modal .error {
  font-size: 0.8rem;
  color: var(--danger);
}

/* ========== Dropdown ========== */
.dropdown-menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  background: var(--panel-strong);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow);
  z-index: 30;
  width: 240px;
  min-width: 0;
  max-width: min(240px, calc(100vw - 16px));
  max-height: 320px;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 6px;
  box-sizing: border-box;
}
.dropdown-menu button {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  color: var(--text);
  border: 0;
  padding: 7px 10px;
  border-radius: 6px;
  font: inherit;
  font-size: 0.82rem;
  cursor: pointer;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.dropdown-menu button:hover { background: rgba(255,255,255,0.08); }
.dropdown-menu .divider {
  height: 1px;
  background: var(--line);
  margin: 4px 0;
}
.dropdown-menu .empty {
  padding: 8px 10px;
  font-size: 0.76rem;
  color: var(--text-muted);
}

/* Responsive */
@media (max-width: 900px) {
  body { font-size: 13px; }
  :root { --list-w: 180px; --preview-w: 320px; }
}
