/* ── Theme variables ── */
:root { --bg: #f5f0e8; --surface: #faf5ef; --surface2: #f0ebe4; --surface3: #e8e3dc;
  --fg: #1a1a18; --fg-sec: #5c5c54; --border: #cac5bd; --accent: #6e6e66;
  --editor-bg: #f8f4ec; }
.dark { --bg: #0e0e12; --surface: #17171c; --surface2: #1e1e24; --surface3: #26262d;
  --fg: #ececed; --fg-sec: #9d9da8; --border: #2a2a32; --accent: #888;
  --editor-bg: #121216; }
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: 'Courier New', Courier, monospace; background: var(--bg);
  color: var(--fg); height: 100vh; overflow: hidden; }
#app { display: flex; flex-direction: column; height: 100vh; }

/* ── Toolbar ── */
#toolbar { display: flex; align-items: center; gap: 4px; padding: 4px 8px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  font-size: 11pt; flex-wrap: wrap; flex-shrink: 0; }
#toolbar button, #toolbar select, #toolbar label { background: transparent; color: var(--fg);
  border: none; border-radius: 4px; padding: 4px 6px; cursor: pointer;
  font: 10pt 'Courier New', monospace; flex-shrink: 0; }
#toolbar button:hover, #toolbar label:hover { background: var(--surface2); }
#toolbar .hl-btn { font-size:8pt; padding:2px 4px; color: #000 !important; }
#toolbar .sep { width: 1px; height: 20px; background: var(--border); margin: 0 2px; flex-shrink: 0; }
#toolbar .spacer { flex: 1; min-width: 4px; }
#toolbar .timer { font-size: 9pt; font-weight: bold; flex-shrink: 0; }
#toolbar .toolbar-group { display: inline-flex; gap: 2px; align-items: center; }

/* ── Panes ── */
#panes { display: flex; flex: 1; overflow: hidden; }
.pane { overflow-y: auto; }
#pane-left { width: 260px; min-width: 200px; background: var(--surface);
  border-right: 1px solid var(--border); display: flex; flex-direction: column; }
#pane-center { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#pane-right { width: 280px; min-width: 220px; background: var(--surface);
  border-left: 1px solid var(--border); }

/* ── Textarea / Preview ── */
#textarea-wrap { flex: 1; padding: 40px 60px; background: var(--editor-bg); overflow: auto; position: relative; }
#editor { width: 100%; height: 100%; border: none; resize: none; outline: none;
  background: transparent; color: var(--fg);
  font: 12pt 'Courier New', Courier, monospace; line-height: 1.2; }
#preview-wrap { flex: 1; padding: 40px 60px; background: var(--editor-bg);
  overflow-y: auto; display: none; }
#preview h3 { font-size: 12pt; font-weight: bold; text-transform: uppercase; margin: 2.5em 0 0.25em; }
#preview h4 { font-size: 12pt; font-weight: normal; text-transform: uppercase; margin: 0 0 0 37%; }
#preview p { margin: 1em 0; }
#preview .parenthetical { margin-left: 31%; margin-right: 33%; font-style: italic; }
#preview .dialogue p { margin-left: 20%; margin-right: 20%; }
#preview .dual-dialogue { display: flex; gap: 4%; }
#preview .dual-dialogue .dialogue { flex: 1; }
#preview .dual-dialogue .dialogue p { margin-left: 0; margin-right: 0; }
#preview .dual-dialogue h4 { margin-left: 0; text-align: center; }
#preview h2 { font-size: 12pt; font-weight: normal; text-transform: uppercase; text-align: right; margin: 2em 0; }
#preview { max-width: 500px; margin: 0 auto; }

/* ── Scene list ── */
.pane-header { padding: 5px 8px; font: bold 9pt 'Courier New', monospace;
  color: var(--fg-sec); border-bottom: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: center; }
#scene-list { list-style: none; padding: 4px; }
#scene-list li { padding: 4px 6px; cursor: pointer; border-radius: 4px;
  font-size: 10pt; border-left: 3px solid transparent; margin-bottom: 2px; }
#scene-list li:hover { background: var(--surface2); }
#scene-list li.active { border-left-color: var(--accent); background: var(--surface3); }

/* ── Corkboard ── */
#scene-corkboard { padding: 8px; }

.corkboard-act {
  font-size: 8pt;
  font-weight: bold;
  color: var(--fg-sec);
  padding: 4px 6px;
  margin: 8px 0 4px;
  border-radius: 3px;
  background: var(--surface2);
}
.corkboard-act:first-child { margin-top: 0; }

.corkboard-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 8px;
  margin-bottom: 4px;
}

.corkboard-card {
  background: var(--editor-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px;
  cursor: pointer;
  box-shadow: 0 1px 3px rgba(0,0,0,0.12);
  transition: box-shadow 0.15s, transform 0.15s;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-height: 60px;
}
.corkboard-card:hover {
  box-shadow: 0 3px 8px rgba(0,0,0,0.18);
  transform: translateY(-1px);
}

.card-num { font-size: 7pt; color: var(--fg-sec); }
.card-title { font-size: 8pt; font-weight: bold; line-height: 1.3; word-break: break-word; }
.card-plot { font-size: 7pt; font-weight: bold; align-self: flex-start; }
.card-desc { font-size: 7pt; color: var(--fg-sec); line-height: 1.2; }

/* ── Right tabs ── */
.tabs { display: flex; border-bottom: 1px solid var(--border); }
.tab { flex: 1; padding: 4px 3px; border: none; background: transparent;
  color: var(--fg-sec); cursor: pointer; font: bold 8pt 'Courier New', monospace;
  text-align: center; border-bottom: 2px solid transparent; }
.tab:hover { color: var(--fg); }
.tab.active { color: var(--fg); border-bottom-color: var(--accent); }
.tab-content { padding: 4px; display: none; }
.tab-content.active { display: block; }
.beat-item, .char-item, .loc-item { padding: 6px 8px; font-size: 11pt;
  display: flex; gap: 6px; border-bottom: 1px solid var(--border); align-items: stretch;
  transition: transform 0.15s, box-shadow 0.15s, margin 0.15s; }
.beat-item:hover, .char-item:hover, .loc-item:hover { background: var(--surface2); }
.beat-item.drag-source { opacity: 0.4; }
.beat-item.drag-over {
  border-top: 2px solid var(--accent);
  margin-top: 8px;
  margin-bottom: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  background: var(--surface3);
}

/* ── Autocomplete ── */
#autocomplete-box { min-width: 180px; }
#autocomplete-box .ac-item { padding: 4px 8px; cursor: pointer; color: var(--fg); display: flex; justify-content: space-between; }
#autocomplete-box .ac-item:hover,
#autocomplete-box .ac-item.hover { background: var(--surface2); }
#autocomplete-box .ac-count { color: var(--fg-sec); font-size: 7pt; }

/* ── Status bar (compact) ── */
#status { padding: 1px 8px; background: var(--surface2);
  border-top: 1px solid var(--border); font-size: 9pt;
  display: flex; gap: 8px; color: var(--fg-sec); align-items: center;
  flex-shrink: 0; height: 22px; overflow: hidden; }
#status span { white-space: nowrap; }

/* ── Timeline ── */
#timeline-bar { flex-shrink: 0; overflow-x: auto; max-height: 260px; overflow-y: auto;
  border-bottom: 1px solid var(--border); }
#timeline-bar.expanded { max-height: none; flex: 1; }
#app.timeline-expanded #panes { display: none; }

/* ── Page tabs (Título/Ficha/Estrutura) ── */
.page-tab { flex: 1; padding: 8px 12px; border: 1px solid var(--border); background: var(--surface2);
  color: var(--fg-sec); cursor: pointer; font: bold 9pt 'Courier New', monospace;
  text-align: center; border-bottom: 2px solid transparent; transition: 0.15s; border-radius: 4px 4px 0 0; }
.page-tab:hover { color: var(--fg); background: var(--surface3); }
.page-tab.active { color: var(--fg); background: var(--surface); border-bottom-color: var(--accent); }
.page-tab-content { display: none; }
.page-tab-content.active { display: block; }

/* ── Help tabs ── */
.help-tabs { display: flex; border-bottom: 1px solid var(--border); gap: 0; }
.help-tab { flex: 1; padding: 8px 4px; border: none; background: transparent;
  color: var(--fg-sec); cursor: pointer; font: bold 9pt 'Courier New', monospace;
  text-align: center; border-bottom: 2px solid transparent; transition: 0.15s; }
.help-tab:hover { color: var(--fg); background: var(--surface2); }
.help-tab.active { color: var(--fg); border-bottom-color: var(--accent); }
.help-tab-content { display: none; }
.help-tab-content.active { display: block; }

/* ── Modal ── */
.modal-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%;
  background: rgba(0,0,0,0.4); display: flex; align-items: center;
  justify-content: center; z-index: 1000; padding: 20px; }
.modal { background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; width: 100%; max-width: 480px; max-height: 85vh; overflow-y: auto; }
.modal-header { padding: 14px 20px; font-weight: bold; font-size: 12pt;
  border-bottom: 1px solid var(--border); }
.modal-body { padding: 16px 20px; }
.modal-footer { padding: 12px 20px; display: flex; gap: 8px;
  border-top: 1px solid var(--border); align-items: center; }
.modal input, .modal textarea { width: 100%; padding: 8px 12px;
  border: 1px solid var(--border); border-radius: 6px;
  background: var(--surface3); color: var(--fg); font: 11pt 'Courier New', monospace; }
.modal button { padding: 8px 16px; border: 1px solid var(--border);
  border-radius: 6px; background: var(--surface2); color: var(--fg);
  cursor: pointer; font: 11pt 'Courier New', monospace; }
.modal button:hover { background: var(--surface3); }
.modal select { padding: 8px 12px; border: 1px solid var(--border);
  border-radius: 6px; background: var(--surface3); color: var(--fg);
  font: 11pt 'Courier New', monospace; }
.form-row { display: flex; gap: 12px; margin-bottom: 12px; align-items: center; }
.form-row label { min-width: 100px; font-size: 11pt; flex-shrink: 0; }
.form-row input, .form-row textarea, .form-row select { flex: 1; min-width: 0; }

/* ── Projeto ── */
.proj-secao { margin-bottom: 8px; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
.proj-titulo { padding: 10px 14px; margin:0; font-size: 11pt; cursor: pointer; background: var(--surface2); user-select:none; }
.proj-titulo:hover { background: var(--surface3); }
.proj-corpo { padding: 12px 16px; display: none; }
.proj-aberto .proj-corpo { display: block; }
.proj-aberto .proj-titulo::after { content: ' ▾'; }
.proj-linha { display: flex; gap: 10px; margin-bottom: 10px; align-items: center; line-height: 1.5; }
.proj-linha label { min-width: 120px; font-size: 10pt; color: var(--fg-sec); flex-shrink: 0; }
.proj-linha input, .proj-linha textarea { padding: 6px 10px; border: 1px solid var(--border); border-radius: 4px; background: var(--surface3); color: var(--fg); font: 11pt 'Courier New', monospace; }
.proj-linha input[type=checkbox] { width: 18px; height: 18px; cursor: pointer; }

/* ── Mobile pane toggles (checkbox hack, hidden by default) ── */
.pane-toggle, .backdrop { display: none; }
.mobile-only { display: none; }
.pane-btn { cursor: pointer; }

/* ── Responsive ── */
@media (max-width: 1024px) {
  #pane-left { width: 220px; min-width: 160px; }
  #pane-right { width: 240px; min-width: 180px; }
  #textarea-wrap { padding: 30px 40px; }
  .pane-header { font-size: 8pt; }
  #toolbar button { font-size: 9pt; padding: 3px 5px; }
}

@media (max-width: 768px) {
  html, body { height: 100%; }
  body { height: 100%; overflow: hidden; }
  #app { height: 100%; }
  #app { overflow-x: hidden; }
  #pane-center { min-width: 0; overflow-x: hidden; }
  #pane-left { display: none; }
  #pane-right { display: none; }
  .mobile-only { display: inline; }
  .pane-btn { display: inline-flex; cursor: pointer; }
  #textarea-wrap { padding: 20px 24px; }
  #toolbar .sep { display: none; }
  #status { font-size: 8pt; gap: 4px; height: 20px; flex-wrap: wrap; }
  #status span { font-size: 8pt; }
  .modal { max-width: 100%; margin: 10px; border-radius: 6px; }
  .modal .modal-body { max-width: 100%; overflow-x: hidden; }
  #titulo-form { padding: 15px 16px; max-width: 100%; overflow-x: hidden; }
  #titulo-form .form-row { flex-direction: column; align-items: stretch; gap: 4px; margin-bottom: 12px; }
  #titulo-form .form-row label { min-width: 0; font-size: 9pt; }
  #titulo-form select { min-width: 0; }
  #titulo-form input, #titulo-form textarea, #titulo-form select { padding: 8px 10px; font-size: 10pt; max-width: 100%; }
  #projeto-form { padding: 15px 16px !important; max-width: 100%; overflow-x: hidden !important; }
  .proj-linha { flex-direction: column; align-items: stretch; gap: 4px; }
  .proj-linha label { min-width: 0; font-size: 9pt; }
  .proj-linha input, .proj-linha textarea, .proj-linha select { max-width: 100%; font-size: 10pt; }
  .proj-linha input[type=checkbox] { width: 18px; height: 18px; align-self: flex-start; }
  #timeline-bar .expanded { min-height: 200px; }
  #preview { padding: 0 4px; max-width: 100%; }
  #preview-wrap { padding: 15px 16px; }

  /* Checkbox hack: show pane as fixed overlay when checked */
  #show-scenes:checked ~ #panes #pane-left,
  #show-beats:checked ~ #panes #pane-right {
    display: block;
    position: fixed;
    top: 0;
    z-index: 200;
    height: 100%;
    width: 280px;
    background: var(--surface);
    overflow-y: auto;
  }
  #show-scenes:checked ~ #panes #pane-left { left: 0; border-right: 1px solid var(--border); }
  #show-beats:checked ~ #panes #pane-right { right: 0; border-left: 1px solid var(--border); }

  /* Backdrop overlay */
  #show-scenes:checked ~ #backdrop-scenes,
  #show-beats:checked ~ #backdrop-beats {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 199;
    background: rgba(0,0,0,0.3);
    cursor: pointer;
  }
}

/* ── Focus visible (keyboard navigation) ── */
#editor:focus-visible { outline: none; box-shadow: none; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }
.modal button:focus-visible, #toolbar button:focus-visible { outline: 2px solid var(--fg); outline-offset: 2px; }
input:focus-visible, textarea:focus-visible, select:focus-visible { border-color: var(--fg) !important; outline: none; box-shadow: 0 0 0 2px var(--accent); }

/* ── Excalidraw fullscreen toggle ── */
.excalidraw-fullscreen {
  width: 100vw !important;
  max-width: 100vw !important;
  height: 100vh !important;
  max-height: 100vh !important;
  border-radius: 0 !important;
}

/* ── Título/Ficha/Estrutura page ── */
#titulo-form { max-width: 800px; margin: 0 auto; }
#titulo-form .form-row { margin-bottom: 16px; }
#titulo-form .form-row label { min-width: 160px; font-size: 10pt; }
#titulo-form select { min-width: 0; }
#titulo-form input, #titulo-form textarea, #titulo-form select {
  background: var(--surface); border: 1px solid var(--border); border-radius: 6px;
  padding: 10px 14px; font: 11pt 'Courier New', monospace; color: var(--fg);
  transition: border-color 0.15s;
  max-width: 100%; box-sizing: border-box; }
#titulo-form input:focus, #titulo-form textarea:focus, #titulo-form select:focus {
  border-color: var(--accent); outline: none; }
#titulo-form button { padding: 10px 20px; border: 1px solid var(--border);
  border-radius: 6px; background: var(--surface2); color: var(--fg);
  cursor: pointer; font: 11pt 'Courier New', monospace; transition: 0.15s; }
#titulo-form button:hover { background: var(--surface3); }

/* ── Focus mode ── */
.focus-mode #toolbar, .focus-mode #pane-left, .focus-mode #pane-right,
.focus-mode #status, .focus-mode #timeline-bar { display: none !important; }
.focus-mode #textarea-wrap { padding: 60px 80px; }
