:root {
  --ink: #1c1b22;
  --muted: #6b6875;
  --line: #e4e1ea;
  --paper: #f7f6fa;
  --card: #ffffff;
  --accent: #5b3cc4;
  --accent-soft: #efeaff;
  --accent-ink: #3a2597;
  --ok: #1f9d64;
  --warn: #b26a00;
  --err: #d23c3c;
  --radius: 12px;
  --mono: "IBM Plex Mono", "SFMono-Regular", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; } /* id-selector display rules below must not resurrect hidden screens */
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
}
h1, h2, h3 { font-weight: 650; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.flex-spacer { flex: 1; }
kbd {
  background: #eee;
  border: 1px solid #ccc;
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 0 4px;
  font-size: 11px;
  font-family: var(--mono);
  color: #333;
}

/* -------------------------------------------------------------- header -- */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 30;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand > .bi { font-size: 26px; color: var(--accent); }
.brand strong { font-size: 17px; letter-spacing: 0.01em; }
.brand .tagline { font-size: 11.5px; color: var(--muted); margin-top: 2px; }

.header-right { display: flex; align-items: center; gap: 12px; }
.save-indicator { font-size: 12px; color: var(--muted); }
.save-indicator.saved { color: var(--ok); }
.save-indicator.dirty { color: var(--warn); }
.save-indicator.error { color: var(--err); }
.icon-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  width: 32px;
  height: 32px;
  border-radius: 8px;
  font-size: 16px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:hover { background: var(--accent-soft); color: var(--accent-ink); }

main { flex: 1; min-height: 0; display: flex; flex-direction: column; }
main > section { flex: 1; min-height: 0; }

/* -------------------------------------------------------------- upload -- */
#screen-upload {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
}
.upload-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 34px;
  max-width: 520px;
  width: 100%;
  text-align: center;
}
.dropzone {
  margin-top: 22px;
  border: 2px dashed #c9c3da;
  border-radius: var(--radius);
  padding: 40px 18px;
  cursor: pointer;
  transition: 0.15s;
  background: #fbfaff;
}
.dropzone:hover, .dropzone.dragover { border-color: var(--accent); background: var(--accent-soft); }
.dropzone .bi { font-size: 34px; color: var(--accent); }
.dz-title { font-weight: 600; margin-top: 8px; }
.dz-sub { font-size: 12px; color: var(--muted); }

/* --------------------------------------------------------------- setup -- */
.screen-inner { max-width: 520px; margin: 0 auto; padding: 26px 20px 60px; }
.screen-head h2 { font-size: 20px; margin: 6px 0 4px; }
.screen-head .btn-link { padding: 0; text-decoration: none; }
.setup-skip {
  margin-top: 20px;
  text-align: center;
}
.setup-skip p { margin: 8px 0 0; }

/* ------------------------------------------------------------- editor -- */
#screen-editor { display: flex; flex-direction: column; min-height: 0; position: relative; }
.editor-toolbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 8px 16px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.editor-toolbar .btn-link { padding: 0; text-decoration: none; }
.seg-count { font-size: 12.5px; color: var(--muted); }
.verify-toggle {
  font-size: 12.5px;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 0;
  cursor: pointer;
}
.verify-toggle input { width: 15px; height: 15px; }

.editor-empty { text-align: center; color: var(--muted); padding: 30px 20px; margin: 0; }

/* --- segment list --- */
.seg-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
/* Sits between two segment cards (a flex sibling of .seg-row in #seg-list,
   not an overlay on either one) - adds a new segment right after the card
   above it. */
.seg-gap { display: flex; justify-content: center; }
.seg-gap-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 20px;
  padding: 0;
  border: 1px dashed #c9c3da;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font-size: 11px;
  cursor: pointer;
  transition: 0.15s;
}
.seg-gap-btn:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.seg-row {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  flex: 0 0 auto; /* never let the flex column squash rows (this was the "segments not loading" bug) */
}
.seg-row.verified { border-color: #bfe6cf; }
.seg-row.verified .seg-bar { background: #f2fbf5; }

/* The active segment (clicked anywhere in it, its region clicked, or playback
   is inside it). Highlight reads across the bar, transcription and preview. */
.seg-row.is-active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}
.seg-row.is-active .seg-bar { background: var(--accent-soft); }
.seg-row.is-active .seg-panes { background: #f7f4ff; }
.seg-row.is-active .pane-label { background: #efeaff; color: var(--accent-ink); }
.seg-row.is-active .rsml-output,
.seg-row.is-active .rsml-plain { background: #fbfaff; }
.seg-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: #fcfbff;
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.seg-check { width: 16px; height: 16px; cursor: pointer; accent-color: var(--ok); }
.seg-verified-label { font-size: 10px; font-weight: 700; letter-spacing: 0.04em; text-transform: uppercase; color: var(--ok); }
.seg-idx {
  background: var(--accent);
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  min-width: 22px;
  height: 22px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.seg-speaker {
  font-size: 11px;
  padding: 2px 4px;
  height: 26px;
  max-width: 160px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--accent-soft);
  color: var(--accent-ink);
}
.seg-dur { font-size: 11px; color: var(--muted); font-family: var(--mono); }

.time-group {
  display: inline-flex;
  align-items: center;
  background: #2c2838;
  color: #fff;
  border-radius: 8px;
  padding: 3px 8px;
  font-family: var(--mono);
  font-size: 12px;
}
.time-group span { opacity: 0.6; padding: 0 1px; }
.time-group input {
  width: 2.2ch;
  border: none;
  background: transparent;
  color: #fff;
  font: inherit;
  text-align: center;
  padding: 0;
  -moz-appearance: textfield;
}
.time-group input.ms { width: 3ch; }
.time-group input::-webkit-outer-spin-button,
.time-group input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.time-group input:focus { outline: none; background: rgba(255, 255, 255, 0.14); border-radius: 3px; }
.time-sep { color: var(--muted); }

.seg-panes { display: grid; grid-template-columns: 1fr 1fr; align-items: stretch; position: relative; }
.seg-pane { display: flex; flex-direction: column; min-width: 0; }
.seg-pane + .seg-pane { border-left: 1px solid var(--line); }
.pane-label {
  font-size: 10.5px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
  padding: 6px 12px;
  background: #fcfbff;
  border-bottom: 1px solid var(--line);
}
/* By default each pane hugs its own content (min-height only) - the grid's
   default stretch then pulls the shorter pane up to match the taller one, so
   their bottom edges line up for the shared puller below. Dragging
   .panes-resize sets --panes-h, which pins both to one explicit height
   instead (flex-basis: the height property, so this slots into the same
   flex-grow-to-fill mechanism the default case already uses). */
.rsml-host {
  flex: 1 1 auto;
  height: var(--panes-h, auto);
  min-height: 96px;
}
.rsml-textarea {
  width: 100%;
  height: 100%;
  border: none;
  resize: none;
  padding: 12px;
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.7;
  outline: none;
  color: var(--ink);
}
.cm-editor { height: 100%; }
.rsml-output {
  flex: 1 1 auto;
  height: var(--panes-h, auto);
  min-height: 96px;
  padding: 12px;
  font-size: 13.5px;
  line-height: 1.8;
  overflow-y: auto;
}
.rsml-plain {
  height: 100%;
  padding: 12px;
  overflow-y: auto;
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.7;
  white-space: pre-wrap;
  cursor: text;
  color: var(--ink);
}
.rsml-plain.is-empty { color: var(--muted); font-style: italic; font-family: inherit; }

/* One puller, centered on the shared border at the bottom of the pane pair,
   drags --panes-h to resize the editor and preview together. */
.panes-resize {
  position: absolute;
  left: 50%;
  bottom: 2px;
  transform: translateX(-50%);
  width: 44px;
  height: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--card);
  cursor: ns-resize;
  touch-action: none;
  z-index: 3;
}
.panes-resize::before {
  content: "";
  width: 26px;
  height: 3px;
  border-radius: 2px;
  background: var(--line);
}
.panes-resize:hover,
.panes-resize.dragging {
  border-color: var(--accent);
}
.panes-resize:hover::before,
.panes-resize.dragging::before {
  background: var(--accent);
}

/* --- bottom: waveform + foot --- */
.editor-bottom { border-top: 1px solid var(--line); background: var(--card); }
.waveform-note { padding: 10px 16px; font-size: 12.5px; color: var(--muted); }
.waveform-panel { padding: 8px 14px 10px; }
.wf-controls { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 6px; }
.wf-clock { font-family: var(--mono); font-size: 13px; color: var(--muted); }
.wf-inline { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.wf-inline .form-select { width: auto; }
.wf-inline .form-range { width: 150px; }
#wf-loop.active { background: var(--accent); color: #fff; border-color: var(--accent); }

/* Dedicated strip for moving the playhead. Sits above the waveform so a drag
   here scrubs playback and never touches segment regions. */
.wf-scrub {
  position: relative;
  height: 20px;
  border: 1px solid var(--line);
  border-bottom: none;
  border-radius: 8px 8px 0 0;
  background: repeating-linear-gradient(90deg, #f3eefc, #f3eefc 1px, transparent 1px, transparent 40px);
  cursor: ew-resize;
  touch-action: none;
}
.wf-scrub::before {
  content: "drag to move playhead";
  position: absolute;
  left: 8px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 9.5px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
  pointer-events: none;
}
.wf-scrub-head {
  position: absolute;
  top: -1px;
  bottom: -1px;
  width: 2px;
  background: var(--accent);
  left: 0;
  pointer-events: none;
}
.wf-scrub-marks {
  position: absolute;
  inset: 0;
  pointer-events: none;
}
.wf-scrub-mark {
  position: absolute;
  top: 0;
  bottom: 0;
  background: rgba(31, 157, 100, 0.35);
  pointer-events: none;
}
.waveform {
  background: #fff7f3;
  border: 1px solid var(--line);
  border-radius: 0 0 8px 8px;
  overflow: hidden;
}
.waveform .region-active { outline: 2px solid var(--accent); outline-offset: -2px; }

.editor-foot {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border-top: 1px solid var(--line);
}

/* ------------------------------------------------------ settings drawer -- */
.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(20, 18, 30, 0.35);
  z-index: 80;
}
.drawer {
  position: fixed;
  top: 0;
  right: 0;
  height: 100%;
  width: 380px;
  max-width: 90vw;
  background: var(--card);
  border-left: 1px solid var(--line);
  box-shadow: -12px 0 32px rgba(0, 0, 0, 0.12);
  z-index: 81;
  display: flex;
  flex-direction: column;
}
.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}
.drawer-body { padding: 8px 16px 20px; overflow-y: auto; }
.drawer-body section { padding: 14px 0; border-bottom: 1px solid var(--line); }
.drawer-body section:last-child { border-bottom: none; }
.drawer-body h4 {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 0 0 10px;
}

/* --------------------------------------------------------------------
   shortcuts modal (centered, distinct from the side-sliding drawer above) */
.shortcuts-modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 420px;
  max-width: 90vw;
  max-height: 80vh;
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
  z-index: 85;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.shortcuts-modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}
.shortcuts-modal-body { padding: 12px 16px 18px; overflow-y: auto; }
.shortcuts { margin: 0; font-size: 13px; }
.shortcuts dt { margin-bottom: 2px; }
.shortcuts dd { margin: 0 0 10px; color: var(--muted); }

/* RSML tag config editors (drawer -> RSML tags) */
.rsml-cat {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  margin-bottom: 8px;
  background: var(--paper);
}
.rsml-cat + .rsml-cat { margin-top: 0; }
.rsml-cat summary {
  cursor: pointer;
  font-weight: 600;
  font-size: 13px;
  list-style: none;
}
.rsml-cat summary::-webkit-details-marker { display: none; }
.rsml-cat summary::before {
  content: "\25B8";
  display: inline-block;
  margin-right: 6px;
  color: var(--muted);
  transition: transform 0.15s;
}
.rsml-cat[open] summary::before { transform: rotate(90deg); }
.rsml-cat-count {
  color: var(--muted);
  font-weight: 400;
  font-size: 11.5px;
}
.rsml-cat-hint { margin: 6px 0 8px; }
.rsml-cat-empty { display: block; margin: 4px 0; }
.rsml-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.rsml-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: var(--accent-soft);
  color: var(--accent-ink);
  border: 1px solid var(--accent-soft);
  border-radius: 999px;
  padding: 2px 4px 2px 10px;
  font-size: 12px;
  font-family: var(--mono);
}
.rsml-chip button {
  border: none;
  background: none;
  color: inherit;
  opacity: 0.6;
  font-size: 14px;
  line-height: 1;
  padding: 2px 6px;
  cursor: pointer;
}
.rsml-chip button:hover { opacity: 1; }
.rsml-add-row { display: flex; gap: 6px; margin-bottom: 6px; }
.rsml-add-row input { flex: 1; min-width: 0; }
.rsml-add-row-map .rsml-add-code { flex: 0 0 30%; }
.rsml-add-row-map .rsml-add-label { flex: 1; }
.rsml-map-rows { margin-bottom: 8px; }
.rsml-map-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 3px 0;
  border-bottom: 1px dotted var(--line);
  font-size: 12.5px;
}
.rsml-map-row:last-child { border-bottom: none; }
.rsml-map-code {
  flex: 0 0 auto;
  min-width: 44px;
  font-size: 11.5px;
  font-weight: 700;
  color: var(--accent-ink);
}
.rsml-map-label { flex: 1; color: var(--ink); }
.rsml-map-row button {
  border: none;
  background: none;
  color: var(--muted);
  font-size: 14px;
  line-height: 1;
  padding: 2px 4px;
  cursor: pointer;
}
.rsml-map-row button:hover { color: var(--err); }
.rsml-reset { padding: 0; font-size: 12px; }

/* Speaker roster (drawer -> Speakers) */
.spk-rows { margin-bottom: 8px; }
.spk-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 0;
  border-bottom: 1px dotted var(--line);
  font-size: 12.5px;
}
.spk-row:last-child { border-bottom: none; }
.spk-row-label { color: var(--ink); }
.spk-default-tag {
  font-size: 9.5px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--accent-ink);
  background: var(--accent-soft);
  border-radius: 4px;
  padding: 1px 5px;
  margin-left: 2px;
}
.spk-edit,
.spk-remove {
  border: none;
  background: none;
  color: var(--muted);
  font-size: 14px;
  line-height: 1;
  padding: 2px 5px;
  cursor: pointer;
}
.spk-edit { font-size: 12px; }
.spk-edit:hover { color: var(--accent-ink); }
.spk-remove:hover { color: var(--err); }
.spk-remove:disabled { color: var(--line); cursor: not-allowed; }

/* Add-speaker modal (reuses .shortcuts-modal chrome) */
.speaker-modal-foot {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 10px 16px 16px;
  border-top: 1px solid var(--line);
}

/* select2 (language selects only - #default-codemix-lang and
   #speaker-modal-lang). The library's own theme font size doesn't pick up
   this app's compact ~12.5px scale, especially the dropdown popup, which
   mounts straight onto <body> outside any of our own scoping. */
.select2-container--bootstrap-5 .select2-selection__rendered,
.select2-dropdown,
.select2-results__option,
.select2-search__field {
  font-size: 12.5px;
}

/* -------------------------------------------------------------- toasts -- */
.toast-area {
  position: fixed;
  right: 16px;
  bottom: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 90;
  max-width: 360px;
}
.app-toast {
  background: var(--ink);
  color: #fff;
  border-radius: 9px;
  padding: 10px 12px;
  font-size: 13px;
  display: flex;
  gap: 10px;
  align-items: flex-start;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
}
.app-toast button { background: none; border: none; color: inherit; font-size: 16px; line-height: 1; cursor: pointer; opacity: 0.7; }
.toast-success { background: #14663f; }
.toast-error { background: #8f2020; }
.toast-warn { background: #7a4a00; }

@media (max-width: 720px) {
  .seg-panes { grid-template-columns: 1fr; }
  .seg-pane + .seg-pane { border-left: none; border-top: 1px solid var(--line); }
}
