/* ============ Scriptboard ============ */
:root {
  --bg: #16181d;
  --bg2: #1e2128;
  --bg3: #262a33;
  --line: #333845;
  --text: #e8eaf0;
  --dim: #8b93a5;
  --accent: #e8b64c;
  --accent2: #4ca3e8;
  --danger: #e05b5b;
  --page: #fdfdfb;
  --page-ink: #1a1a1a;
  --strip-day: #f7e07c;
  --strip-night: #7cb2f7;
  --strip-dusk: #e8a06c;
  --strip-int: #cfd8dc;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 -apple-system, "Segoe UI", Helvetica, Arial, sans-serif;
}
.hidden { display: none !important; }
button { font: inherit; cursor: pointer; }
.btn {
  background: var(--bg3); color: var(--text);
  border: 1px solid var(--line); border-radius: 6px;
  padding: 6px 12px;
}
.btn:hover { border-color: var(--accent); }
.btn.small { padding: 4px 10px; font-size: 12.5px; }
.btn.primary { background: var(--accent); color: #191308; border-color: var(--accent); font-weight: 600; }
.btn.danger { color: var(--danger); }
.hint { color: var(--dim); font-size: 12px; }

#app { display: flex; flex-direction: column; height: 100vh; }

/* ---------- Top bar ---------- */
#topbar {
  display: flex; align-items: center; gap: 20px;
  padding: 0 16px; height: 52px; flex: none;
  background: var(--bg2); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: baseline; gap: 8px; }
.brand-mark { color: var(--accent); font-size: 15px; }
.brand-name { font-weight: 700; letter-spacing: 3px; font-size: 14px; }
#tabs { display: flex; gap: 4px; flex: 1; }
.tab {
  background: none; border: none; color: var(--dim);
  padding: 8px 14px; border-radius: 6px; font-size: 13.5px; font-weight: 500;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--accent); background: var(--bg3); }
.top-actions { display: flex; align-items: center; gap: 12px; }
#save-status { color: var(--dim); font-size: 12px; }
.menu-wrap { position: relative; }
.menu {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 100;
  background: var(--bg3); border: 1px solid var(--line); border-radius: 8px;
  min-width: 230px; padding: 6px; box-shadow: 0 10px 30px rgba(0,0,0,.5);
}
.menu button {
  display: block; width: 100%; text-align: left;
  background: none; border: none; color: var(--text);
  padding: 8px 10px; border-radius: 5px; font-size: 13px;
}
.menu button:hover { background: var(--bg2); color: var(--accent); }
.menu hr { border: none; border-top: 1px solid var(--line); margin: 5px 0; }

/* ---------- Views ---------- */
.view { display: none; flex: 1; min-height: 0; }
.view.active { display: flex; }
.section-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; border-bottom: 1px solid var(--line); flex: none;
}
.section-bar h2 { margin: 0; font-size: 15px; font-weight: 600; }
.full { flex: 1; display: flex; flex-direction: column; min-width: 0; }

/* ---------- Write view ---------- */
#scene-nav {
  width: 230px; flex: none; background: var(--bg2);
  border-right: 1px solid var(--line);
  display: flex; flex-direction: column; overflow: hidden;
}
.panel-title {
  padding: 12px 14px 6px; font-size: 11px; letter-spacing: 1.5px;
  color: var(--dim); font-weight: 600; flex: none;
}
#scene-list { overflow-y: auto; flex: 1; min-height: 60px; }
.scene-item {
  padding: 6px 14px; font-size: 12px; cursor: pointer;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  border-left: 2px solid transparent; color: var(--text);
}
.scene-item:hover { background: var(--bg3); }
.scene-item .num { color: var(--accent); margin-right: 6px; font-weight: 600; }
.stats-title { border-top: 1px solid var(--line); }
#script-stats { padding: 4px 14px 14px; font-size: 12px; color: var(--dim); flex: none; }
#script-stats b { color: var(--text); }

#editor-area { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#editor-toolbar {
  display: flex; align-items: center; gap: 14px;
  padding: 8px 16px; border-bottom: 1px solid var(--line); flex: none;
}
#el-type {
  background: var(--bg3); color: var(--text); border: 1px solid var(--line);
  border-radius: 6px; padding: 5px 8px; font-size: 13px;
}
#editor-toolbar .hint { flex: 1; }
#page-scroll { flex: 1; overflow-y: auto; padding: 30px 0 120px; }

/* The script "page" */
#editor {
  width: 8.5in; min-height: 11in; margin: 0 auto;
  background: var(--page); color: var(--page-ink);
  padding: 1in 1in 1in 1.5in;
  font-family: "Courier Prime", "Courier New", Courier, monospace;
  font-size: 12pt; line-height: 1;
  box-shadow: 0 4px 30px rgba(0,0,0,.6);
  border-radius: 2px;
}
#editor:focus-within { outline: none; }

.blk {
  outline: none; white-space: pre-wrap; word-wrap: break-word;
  min-height: 12pt; line-height: 12pt; padding: 0; margin: 0;
  caret-color: #000;
}
.blk + .blk { margin-top: 12pt; }
.blk[data-type="scene"]      { text-transform: uppercase; font-weight: 700; max-width: 6in; }
.blk[data-type="action"]     { max-width: 6in; }
.blk[data-type="character"]  { text-transform: uppercase; margin-left: 2.2in; max-width: 3.3in; }
.blk[data-type="paren"]      { margin-left: 1.6in; max-width: 2.5in; }
.blk[data-type="dialogue"]   { margin-left: 1in; max-width: 3.5in; }
.blk[data-type="transition"] { text-transform: uppercase; text-align: right; max-width: 6in; }
.blk[data-type="paren"] + .blk[data-type="dialogue"],
.blk[data-type="character"] + .blk[data-type="dialogue"],
.blk[data-type="character"] + .blk[data-type="paren"] { margin-top: 0; }
.blk.focused { background: rgba(232,182,76,.10); }
.blk:empty::before { content: attr(data-ph); color: #b9b9b0; }

/* page break marker */
.pgbrk {
  border-top: 1px dashed #c9c9bd; margin: 14pt -0.5in 14pt -1in;
  position: relative; height: 0;
}
.pgbrk::after {
  content: attr(data-label); position: absolute; right: -0.45in; top: -7pt;
  font-family: inherit; font-size: 9pt; color: #a5a599;
}

/* Autocomplete */
#autocomplete {
  position: absolute; z-index: 300; background: #fff; color: #222;
  border: 1px solid #bbb; border-radius: 4px; box-shadow: 0 6px 18px rgba(0,0,0,.3);
  font-family: "Courier Prime", "Courier New", monospace; font-size: 12px;
  max-height: 180px; overflow-y: auto; min-width: 180px;
}
#autocomplete div { padding: 4px 10px; cursor: pointer; }
#autocomplete div.sel, #autocomplete div:hover { background: var(--accent); }

/* ---------- Breakdown ---------- */
.side-list {
  width: 250px; flex: none; background: var(--bg2);
  border-right: 1px solid var(--line);
  display: flex; flex-direction: column; overflow: hidden;
}
#bd-scene-list, #sb-scene-list { overflow-y: auto; flex: 1; }
.bd-scene-item {
  padding: 8px 14px; font-size: 12px; cursor: pointer;
  border-left: 3px solid transparent; border-bottom: 1px solid var(--line);
}
.bd-scene-item:hover { background: var(--bg3); }
.bd-scene-item.active { background: var(--bg3); border-left-color: var(--accent); }
.bd-scene-item .slug { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bd-scene-item .meta { color: var(--dim); font-size: 11px; margin-top: 2px; }

#bd-detail, #sb-detail { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#bd-body { flex: 1; overflow-y: auto; padding: 18px; }
.bd-grid { display: grid; grid-template-columns: 1.1fr .9fr; gap: 18px; align-items: start; }
.bd-card {
  background: var(--bg2); border: 1px solid var(--line); border-radius: 10px; padding: 14px 16px;
}
.bd-card h3 { margin: 0 0 10px; font-size: 12px; letter-spacing: 1.2px; color: var(--dim); }
.bd-facts { display: grid; grid-template-columns: repeat(4, auto); gap: 6px 22px; font-size: 13px; }
.bd-facts .k { color: var(--dim); font-size: 11px; }
.bd-facts .v { font-weight: 600; }
#bd-scene-text {
  font-family: "Courier Prime", "Courier New", monospace; font-size: 12.5px; line-height: 1.5;
  background: var(--bg2); border: 1px solid var(--line); border-radius: 10px;
  padding: 16px 18px; white-space: pre-wrap; user-select: text; max-height: 55vh; overflow-y: auto;
}
#bd-scene-text .cue { color: var(--accent2); font-weight: 700; }
.tag-bar { display: flex; flex-wrap: wrap; gap: 6px; margin: 12px 0; }
.tag-bar button {
  border: 1px solid var(--line); background: var(--bg3); color: var(--text);
  border-radius: 20px; padding: 4px 12px; font-size: 12px;
}
.tag-bar button:hover { border-color: var(--accent); color: var(--accent); }
.cat-block { margin-bottom: 10px; }
.cat-name { font-size: 11px; letter-spacing: 1px; color: var(--dim); margin-bottom: 4px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--bg3); border: 1px solid var(--line);
  border-radius: 14px; padding: 2px 10px; font-size: 12px;
}
.chip.auto { border-style: dashed; color: var(--accent2); }
.chip .x { cursor: pointer; color: var(--dim); font-weight: 700; }
.chip .x:hover { color: var(--danger); }
.chip-add {
  background: none; border: 1px dashed var(--line); color: var(--dim);
  border-radius: 14px; padding: 2px 10px; font-size: 12px;
}
.empty-note { color: var(--dim); font-size: 13px; padding: 30px; text-align: center; }

/* ---------- Schedule ---------- */
#sched-area { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.sched-controls { display: flex; align-items: center; gap: 10px; font-size: 12.5px; color: var(--dim); }
.sched-controls input[type=date], .sched-controls input[type=number] {
  background: var(--bg3); color: var(--text); border: 1px solid var(--line);
  border-radius: 5px; padding: 4px 6px; font: inherit;
}
#sched-board {
  flex: 1; display: flex; gap: 14px; overflow-x: auto; overflow-y: hidden;
  padding: 16px; align-items: stretch;
}
#sched-days { display: flex; gap: 14px; }
.day-col {
  width: 250px; flex: none; background: var(--bg2);
  border: 1px solid var(--line); border-radius: 10px;
  display: flex; flex-direction: column; overflow: hidden;
}
.day-head {
  padding: 8px 12px; font-size: 12px; font-weight: 700; letter-spacing: .5px;
  background: var(--bg3); border-bottom: 1px solid var(--line);
  display: flex; justify-content: space-between; align-items: center; gap: 6px;
}
.day-head .day-date { color: var(--dim); font-weight: 400; }
.day-head .day-del { color: var(--dim); background: none; border: none; font-size: 14px; }
.day-head .day-del:hover { color: var(--danger); }
.unsched-head { color: var(--dim); }
.day-strips { flex: 1; overflow-y: auto; padding: 8px; min-height: 60px; }
.day-strips.dragover { background: rgba(232,182,76,.08); }
.day-total { padding: 6px 12px; font-size: 11.5px; color: var(--dim); border-top: 1px solid var(--line); }
.strip {
  border-radius: 6px; padding: 6px 9px; margin-bottom: 6px; cursor: grab;
  color: #14161a; font-size: 11.5px; line-height: 1.35;
  border: 1px solid rgba(0,0,0,.25);
}
.strip.dragging { opacity: .4; }
.strip .strip-top { display: flex; justify-content: space-between; font-weight: 700; }
.strip .strip-cast { opacity: .75; }
.strip[data-tone="day"]   { background: var(--strip-day); }
.strip[data-tone="night"] { background: var(--strip-night); }
.strip[data-tone="dusk"]  { background: var(--strip-dusk); }
.strip[data-tone="int"]   { background: var(--strip-int); }

/* ---------- DOOD & tables ---------- */
.table-scroll { flex: 1; overflow: auto; padding: 16px; }
table.grid { border-collapse: collapse; font-size: 12px; }
table.grid th, table.grid td {
  border: 1px solid var(--line); padding: 5px 8px; text-align: center; white-space: nowrap;
}
table.grid th { background: var(--bg3); position: sticky; top: 0; }
table.grid td.name { text-align: left; font-weight: 600; background: var(--bg2); position: sticky; left: 0; }
td.c-SW, td.c-SWF { background: rgba(120,200,120,.25); font-weight: 700; }
td.c-WF { background: rgba(232,120,120,.25); font-weight: 700; }
td.c-W  { background: rgba(76,163,232,.18); }
td.c-H  { color: var(--dim); }

/* ---------- Storyboards ---------- */
#sb-panels { flex: 1; overflow-y: auto; padding: 18px; display: flex; flex-wrap: wrap; gap: 16px; align-content: flex-start; }
.sb-panel {
  width: 340px; background: var(--bg2); border: 1px solid var(--line);
  border-radius: 10px; overflow: hidden; flex: none;
}
.sb-frame { position: relative; width: 100%; aspect-ratio: 16/9; background: #fff; }
.sb-frame canvas { position: absolute; inset: 0; width: 100%; height: 100%; cursor: crosshair; touch-action: none; }
.sb-frame.has-img canvas { cursor: default; }
.sb-frame img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; background:#111; }
.sb-frame .drop-hint {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  color: #bbb; font-size: 12px; pointer-events: none;
}
.sb-tools { display: flex; gap: 6px; padding: 6px 10px; border-bottom: 1px solid var(--line); align-items: center; }
.sb-tools button { background: none; border: 1px solid var(--line); color: var(--dim); border-radius: 4px; padding: 2px 8px; font-size: 11px; }
.sb-tools button.on, .sb-tools button:hover { color: var(--accent); border-color: var(--accent); }
.sb-tools .spacer { flex: 1; }
.sb-meta { padding: 10px 12px; display: grid; gap: 6px; }
.sb-meta .row { display: flex; gap: 6px; }
.sb-meta select, .sb-meta input, .sb-meta textarea {
  background: var(--bg3); color: var(--text); border: 1px solid var(--line);
  border-radius: 5px; padding: 4px 7px; font: 12px/1.4 inherit; width: 100%;
}
.sb-meta textarea { resize: vertical; min-height: 40px; }
.sb-panel-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 6px 12px; font-size: 12px; font-weight: 700; background: var(--bg3);
}
.sb-panel-head .del { background: none; border: none; color: var(--dim); font-size: 14px; }
.sb-panel-head .del:hover { color: var(--danger); }

/* ---------- Dialog ---------- */
dialog {
  background: var(--bg2); color: var(--text); border: 1px solid var(--line);
  border-radius: 12px; padding: 22px 26px; min-width: 420px;
}
dialog::backdrop { background: rgba(0,0,0,.6); }
dialog h3 { margin: 0 0 14px; }
dialog label { display: block; margin-bottom: 12px; font-size: 13px; color: var(--dim); }
dialog input, dialog textarea {
  display: block; width: 100%; margin-top: 4px;
  background: var(--bg3); color: var(--text); border: 1px solid var(--line);
  border-radius: 6px; padding: 8px 10px; font: inherit;
}
.dlg-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 16px; }

/* ============ PRINT ============ */
#print-root { display: none; }

@media print {
  @page { size: letter; margin: 0; }
  body * { visibility: hidden; }
  #app, dialog { display: none !important; }
  #print-root, #print-root * { visibility: visible; }
  #print-root { display: block; position: absolute; left: 0; top: 0; width: 100%; background: #fff; color: #000; }

  /* --- script pages --- */
  .p-page {
    width: 8.5in; height: 11in; padding: 1in 1in 1in 1.5in;
    page-break-after: always; position: relative; overflow: hidden;
    font-family: "Courier Prime", "Courier New", Courier, monospace;
    font-size: 12pt; line-height: 12pt;
  }
  .p-page .pnum { position: absolute; top: .5in; right: 1in; }
  .p-blk { white-space: pre-wrap; margin-top: 12pt; }
  .p-blk.first { margin-top: 0; }
  .p-blk[data-type="scene"]      { text-transform: uppercase; font-weight: 700; }
  .p-blk[data-type="character"]  { text-transform: uppercase; margin-left: 2.2in; max-width: 3.3in; }
  .p-blk[data-type="paren"]      { margin-left: 1.6in; max-width: 2.5in; margin-top: 0; }
  .p-blk[data-type="dialogue"]   { margin-left: 1in; max-width: 3.5in; margin-top: 0; }
  .p-blk[data-type="transition"] { text-transform: uppercase; text-align: right; }
  .p-blk[data-type="action"], .p-blk[data-type="scene"] { max-width: 6in; }

  .p-titlepage {
    width: 8.5in; height: 11in; page-break-after: always; position: relative;
    font-family: "Courier Prime", "Courier New", Courier, monospace; font-size: 12pt;
  }
  .p-titlepage .tp-center { position: absolute; top: 3.5in; left: 0; right: 0; text-align: center; }
  .p-titlepage .tp-contact { position: absolute; bottom: 1in; left: 1.5in; white-space: pre-wrap; }

  /* --- report pages (breakdown / dood / boards) --- */
  .p-report { width: 8.5in; min-height: 10.5in; padding: .6in; page-break-after: always; font: 11px/1.5 Helvetica, Arial, sans-serif; color:#000; }
  .p-report h1 { font-size: 16px; margin: 0 0 2px; }
  .p-report h2 { font-size: 13px; margin: 14px 0 6px; }
  .p-report .sub { color: #555; margin-bottom: 12px; }
  .p-report table { border-collapse: collapse; width: 100%; font-size: 10.5px; }
  .p-report th, .p-report td { border: 1px solid #999; padding: 3px 6px; text-align: center; }
  .p-report th { background: #eee; }
  .p-report td.name { text-align: left; font-weight: 600; }
  .p-report .facts { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px 16px; margin-bottom: 8px; }
  .p-report .facts .k { color: #666; font-size: 9.5px; }
  .p-report .cats { columns: 2; column-gap: 24px; }
  .p-report .cat { break-inside: avoid; margin-bottom: 8px; }
  .p-report .cat b { display: block; font-size: 10px; letter-spacing: 1px; }

  .p-board-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
  .p-board { break-inside: avoid; border: 1px solid #999; border-radius: 4px; overflow: hidden; }
  .p-board img { width: 100%; aspect-ratio: 16/9; object-fit: contain; display: block; background:#fff; border-bottom:1px solid #ccc; }
  .p-board .cap { padding: 5px 8px; font-size: 10px; }
  .p-board .cap b { font-size: 10.5px; }
}
