/* ===== Design tokens ===== */
:root {
  --bg: #14151a;
  --panel: #1b1d24;
  --panel-alt: #20222b;
  --border: #2b2e38;
  --border-soft: #24262f;
  --text: #ececed;
  --text-dim: #9296a3;
  --text-faint: #6a6e7a;
  --accent: #e7b750;
  --accent-contrast: #14151a;
  --teal: #4fc3b0;
  --danger: #e15b4d;
  --danger-dim: #3a2420;
  --warn: #f0894a;
  --warn-dim: #3a2a1c;
  --radius: 6px;
  --radius-sm: 4px;
  --font-ui: "Space Grotesk", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  --shadow-modal: 0 20px 60px rgba(0, 0, 0, 0.45);
}

[data-theme="light"] {
  --bg: #f4f3f0;
  --panel: #ffffff;
  --panel-alt: #f0efec;
  --border: #ddd9d1;
  --border-soft: #e8e5df;
  --text: #1c1d20;
  --text-dim: #5c5e66;
  --text-faint: #8b8d94;
  --accent: #b3822e;
  --accent-contrast: #ffffff;
  --teal: #2f8f80;
  --danger: #b23c2c;
  --danger-dim: #f6e2de;
  --warn: #b9561a;
  --warn-dim: #f8e6d6;
  --shadow-modal: 0 20px 60px rgba(0, 0, 0, 0.18);
}

/* ===== Reset ===== */
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font-ui);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { margin: 0; font-family: var(--font-ui); }
button { font-family: inherit; }
input, select, textarea { font-family: inherit; color: inherit; }
p { margin: 0 0 8px; }
.muted { color: var(--text-dim); }
.small { font-size: 12px; }

::selection { background: var(--accent); color: var(--accent-contrast); }

*:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* The hidden attribute must always win over component display rules below
   (e.g. .modal-overlay/.modal/.video-empty/.warning-dot set their own
   `display`, which would otherwise override the browser's default
   `[hidden]{display:none}` since author rules beat user-agent rules
   regardless of specificity). */
[hidden] {
  display: none !important;
}

@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; transition: none !important; animation: none !important; }
}

/* thin scrollbars */
* { scrollbar-width: thin; scrollbar-color: var(--border) transparent; }
*::-webkit-scrollbar { width: 8px; height: 8px; }
*::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
*::-webkit-scrollbar-track { background: transparent; }

/* ===== App shell ===== */
#app {
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  height: 100vh;
  height: 100dvh;
}

/* ===== Toolbar ===== */
.toolbar {
  display: flex;
  align-items: center;
  gap: 18px;
  flex-wrap: wrap;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border-soft);
}
.toolbar-brand { display: flex; align-items: center; gap: 8px; margin-right: 4px; }
.brand-mark {
  width: 14px; height: 14px;
  border-radius: 3px;
  background: linear-gradient(135deg, var(--accent), var(--teal));
  display: inline-block;
}
.toolbar-brand h1 { font-size: 17px; font-weight: 700; letter-spacing: 0.01em; }
.toolbar-group { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.toolbar-end { margin-left: auto; }

.btn {
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  background: var(--panel-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 7px 12px;
  cursor: pointer;
  white-space: nowrap;
  transition: border-color 0.15s ease, background 0.15s ease;
}
.btn:hover { border-color: var(--text-faint); }
.btn:active { transform: translateY(0.5px); }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn-ghost { background: transparent; }
.btn-accent { background: var(--accent); border-color: var(--accent); color: var(--accent-contrast); font-weight: 600; }
.btn-accent:hover { filter: brightness(1.06); }
.btn-danger { color: var(--danger); }
.btn-danger:hover { border-color: var(--danger); }
.btn-danger-solid { background: var(--danger); border-color: var(--danger); color: #fff; font-weight: 600; }
.btn-small { padding: 4px 9px; font-size: 12px; }
.btn[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  color: var(--text-dim);
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.icon-btn:hover { color: var(--text); background: var(--panel-alt); }
.icon-btn:disabled { opacity: 0.35; cursor: default; }
.icon-btn:disabled:hover { background: transparent; }
.icon-btn.small { width: 24px; height: 24px; }

.btn-menu { position: relative; }
.menu {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  min-width: 160px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-modal);
  padding: 4px;
  display: none;
  z-index: 20;
}
.menu.open { display: block; }
.menu button {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  color: var(--text);
  font-size: 13px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.menu button:hover { background: var(--panel-alt); }
.menu-sep { height: 1px; background: var(--border-soft); margin: 4px 2px; }

/* ===== Workspace ===== */
.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(340px, 1fr);
  min-height: 0;
  border-bottom: 1px solid var(--border-soft);
}
/* no video loaded: drop the player and give the cue list the full width */
.no-video .workspace { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr); }
.no-video .video-pane { display: none; }
.video-pane {
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-right: 1px solid var(--border-soft);
  background: var(--bg);
}
.video-frame {
  position: relative;
  flex: 1;
  min-height: 0;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
}
#video { width: 100%; height: 100%; object-fit: contain; display: block; }
.video-empty {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--text-dim);
  background: var(--panel);
}

.transport {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 8px 14px;
  background: var(--panel);
  border-top: 1px solid var(--border-soft);
}
.transport-play svg { pointer-events: none; }
.time-display {
  font-family: var(--font-mono);
  font-size: 13px;
  letter-spacing: 0.01em;
}
.time-sep-slash { margin: 0 6px; color: var(--text-faint); }
.rate-control {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-dim);
}

/* ===== Cue pane ===== */
.cue-pane { display: flex; flex-direction: column; min-height: 0; container-type: inline-size; }
.cue-pane-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 10px;
  padding: 12px 14px 8px;
}
.cue-pane-header h2 { font-size: 13px; text-transform: none; font-weight: 600; color: var(--text-dim); display: flex; align-items: baseline; gap: 6px; }
.count-badge {
  font-family: var(--font-mono);
  font-size: 11px;
  background: var(--panel-alt);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1px 7px;
  color: var(--text);
}
.warning-summary { font-size: 12px; color: var(--text-faint); margin-left: auto; }
.warning-summary.has-warnings { color: var(--danger); }

.cue-list {
  min-height: 120px;
  flex: 1;
  overflow-y: auto;
  padding: 4px 12px 16px;
}
.cue-list:empty::after {
  content: "No subtitles yet — open an .srt file or add a cue to get started.";
  display: block;
  margin-top: 24px;
  padding: 0 14px;
  color: var(--text-faint);
  font-size: 13px;
  line-height: 1.5;
  text-align: center;
}

.cue-row {
  border: 1px solid var(--border-soft);
  border-left: 3px solid transparent;
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  margin-bottom: 8px;
  background: var(--panel);
  cursor: default;
}
/* Skip layout/paint for rows scrolled out of view — with thousands of
   auto-sizing textareas this is most of the cost of every update. Opt-in
   class set by app.js everywhere except WebKit: there, once a <video> is on
   the page, on-screen rows stayed skipped and the list rendered blank. */
.cue-list.skip-offscreen .cue-row {
  content-visibility: auto;
  contain-intrinsic-size: auto 72px;
}
.cue-row.selected { border-left-color: var(--accent); background: var(--panel-alt); }
.cue-row.primary { box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--accent) 55%, transparent); }
.cue-row.active { box-shadow: inset 0 0 0 1px var(--teal); }
.cue-row.has-warning { border-left-color: var(--danger); }
.cue-row.has-warning.selected { border-left-color: var(--danger); }

.cue-row-head {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}
.cue-index {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-faint);
  min-width: 18px;
}
.cue-times {
  display: flex;
  align-items: center;
  gap: 4px;
  font-family: var(--font-mono);
}
.time-input {
  width: 108px;
  font-family: var(--font-mono);
  font-size: 12px;
  background: var(--panel-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  padding: 4px 6px;
}
.time-input.invalid { border-color: var(--danger); color: var(--danger); }
.time-sep { color: var(--text-faint); font-size: 12px; }
.cue-duration {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-faint);
  margin-left: 2px;
}
.warning-dot {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background: var(--danger-dim);
  color: var(--danger);
  font-size: 11px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: help;
}
.cue-badges { display: flex; align-items: center; gap: 4px; }
.q-dot {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background: var(--warn-dim);
  color: var(--warn);
  font-size: 11px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: help;
}
.row-delete {
  margin-left: auto;
  color: var(--text-faint);
  font-size: 15px;
}
.row-delete:hover { color: var(--danger); background: var(--danger-dim); }

/* The textarea is transparent over a "mirror" div with identical text
   metrics; <mark>s in the mirror show find matches and questionable text. */
.cue-text-wrap {
  position: relative;
  min-width: 0;
  background: var(--panel-alt);
  border-radius: var(--radius-sm);
}
.cue-text,
.cue-text-mirror {
  font-family: var(--font-ui);
  font-size: 13.5px;
  line-height: 1.4;
  padding: 7px 9px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  white-space: pre-wrap;
  overflow-wrap: break-word;
}
.cue-text {
  position: relative;
  display: block;
  width: 100%;
  resize: none;
  overflow-x: hidden;
  overflow-y: auto;
  field-sizing: content; /* grow to fit wrapped text; JS fallback elsewhere */
  min-height: 34px;
  background: transparent;
  color: var(--text);
}
.cue-text-mirror {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  color: transparent;
  border-color: transparent;
}
.cue-text-mirror mark { color: transparent; border-radius: 2px; }
.hl-find { background: color-mix(in srgb, var(--accent) 38%, transparent); }
.hl-current { background: color-mix(in srgb, var(--accent) 80%, transparent); box-shadow: 0 0 0 1px var(--accent); }
.hl-q { background: color-mix(in srgb, var(--warn) 20%, transparent); box-shadow: inset 0 -2px 0 var(--warn); }

.filter-empty { margin: 16px 14px; text-align: center; color: var(--text-faint); font-size: 13px; }
.list-filter { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-dim); }

.rate-control select,
.list-filter select,
.encoding-control select {
  background: var(--panel-alt);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 4px 6px;
  font-size: 12px;
}

/* ===== Find bar ===== */
.find-bar {
  margin: 0 12px 8px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel-alt);
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.find-row { display: flex; align-items: center; gap: 6px; }
.find-row input[type="text"] {
  flex: 1;
  min-width: 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 8px;
  font-size: 13px;
}
.find-count { min-width: 70px; text-align: right; font-family: var(--font-mono); font-size: 11.5px; color: var(--text-dim); }
.find-count.is-error { color: var(--danger); }
.find-options { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 12px; color: var(--text-dim); }
.find-options label { display: flex; align-items: center; gap: 5px; }

/* ===== Editor panel ===== */
.cue-editor {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px 12px 12px;
  border-top: 1px solid var(--border-soft);
  background: var(--panel);
}
.editor-hidden .cue-editor { display: none; }
.cue-editor-head { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 14px; font-size: 12px; color: var(--text-dim); }
.cue-editor-head label { display: flex; align-items: center; gap: 6px; }
.cue-editor-label { min-width: 48px; font-family: var(--font-mono); font-weight: 600; color: var(--text); }
.dur-input { width: 72px; }
.cue-editor-nav { display: flex; gap: 2px; margin-left: auto; }
.editor-text-wrap .cue-text,
.editor-text-wrap .cue-text-mirror { font-size: 15px; line-height: 1.45; padding: 8px 10px; }
.editor-text { min-height: calc(3 * 1.45em + 18px); max-height: 30vh; }
.cue-editor.is-empty .cue-text-wrap { opacity: 0.5; }
.editor-stats { font-family: var(--font-mono); font-size: 11.5px; color: var(--text-faint); }
.editor-stats .over { color: var(--danger); }

/* Wide list (e.g. no video loaded): one row per cue, SubtitleEdit-style —
   number, times and duration in fixed columns, text filling the rest. */
@container (min-width: 680px) {
  .cue-row {
    display: grid;
    grid-template-columns: 24px auto 52px 34px minmax(0, 1fr) 24px;
    column-gap: 12px;
    align-items: center;
    padding: 8px 12px;
  }
  .cue-row-head { display: contents; }
  .cue-row-head > *, .cue-row > .cue-text-wrap { grid-row: 1; }
  .cue-index { grid-column: 1; min-width: 0; text-align: right; }
  .cue-times { grid-column: 2; }
  .cue-duration { grid-column: 3; margin-left: 0; }
  .cue-badges { grid-column: 4; }
  .cue-row > .cue-text-wrap { grid-column: 5; }
  .row-delete { grid-column: 6; margin-left: 0; }
}

/* ===== Timeline ===== */
.timeline-pane {
  background: var(--panel);
  padding: 8px 14px 12px;
}
.timeline-toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 6px;
}
.zoom-control { display: flex; align-items: center; gap: 6px; }
.zoom-control input[type="range"] {
  width: 140px;
  accent-color: var(--accent);
}
.timeline-canvas-wrap {
  width: 100%;
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: var(--panel);
}
#timeline-canvas { display: block; width: 100%; touch-action: none; }
.minimap-wrap {
  width: 100%;
  margin-top: 4px;
  border-radius: var(--radius-sm);
  overflow: hidden;
  cursor: pointer;
}
#minimap-canvas { display: block; width: 100%; }

/* ===== Status bar ===== */
.statusbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 6px 16px;
  font-size: 11.5px;
  color: var(--text-faint);
  background: var(--panel);
  border-top: 1px solid var(--border-soft);
}
.encoding-control { display: flex; align-items: center; gap: 6px; margin-left: auto; }

/* ===== Modals ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
}
.modal {
  width: 100%;
  max-width: 380px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-modal);
  max-height: 85vh;
  display: flex;
  flex-direction: column;
}
.modal-wide { max-width: 520px; }
.modal-xwide { max-width: 720px; }
.modal-foot-spacer { flex: 1; }
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border-soft);
}
.modal-head h3 { font-size: 15px; font-weight: 600; }
.modal-body { padding: 14px 16px; overflow-y: auto; }
.modal-foot {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border-soft);
}

.field { display: block; font-size: 12.5px; color: var(--text-dim); margin-bottom: 12px; }
.field input {
  display: block;
  width: 100%;
  margin-top: 5px;
  background: var(--panel-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  padding: 7px 9px;
  font-size: 13px;
}
.field-radio { border: none; padding: 0; margin: 0 0 12px; }
.field-radio legend { font-size: 12.5px; color: var(--text-dim); padding: 0 0 6px; }
.field-radio label { display: flex; align-items: center; gap: 6px; font-size: 13px; margin-bottom: 4px; }
.checkbox-field { display: flex; align-items: center; gap: 7px; font-size: 13px; margin-bottom: 10px; }

.shortcut-list { display: grid; gap: 8px; margin: 0; }
.shortcut-list > div { display: flex; justify-content: space-between; gap: 14px; align-items: baseline; }
.shortcut-list dt {
  font-family: var(--font-mono);
  font-size: 12px;
  background: var(--panel-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 2px 7px;
  white-space: nowrap;
}
.shortcut-list dd { margin: 0; font-size: 13px; color: var(--text-dim); text-align: right; }

.shortcut-heading {
  margin: 16px 0 8px;
  font-size: 11.5px;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--text-faint);
}
.modal-body > .shortcut-heading:first-child { margin-top: 0; }
.mono-input { font-family: var(--font-mono) !important; }

/* questionable text */
.check-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 0 16px; margin-bottom: 8px; }
.check-grid .checkbox-field { font-size: 12.5px; margin-bottom: 6px; }
.q-summary { margin: 8px 0; font-size: 13px; font-weight: 600; }
.result-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.result-list button {
  width: 100%;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 3px 12px;
  text-align: left;
  font: inherit;
  font-size: 12.5px;
  color: var(--text);
  background: var(--panel-alt);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  padding: 7px 10px;
  cursor: pointer;
}
.result-list button:hover { border-color: var(--text-faint); }
.r-meta { font-family: var(--font-mono); font-size: 11.5px; color: var(--text-faint); }
.r-labels { color: var(--warn); font-size: 12px; }
.r-snippet { grid-column: 1 / -1; overflow-wrap: anywhere; }
.r-snippet mark { color: inherit; padding: 0 1px; }
.r-snippet .nl { color: var(--text-faint); padding: 0 4px; }

/* fix common errors */
.fix-list { display: flex; flex-direction: column; gap: 6px; }
.fix-row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  padding: 7px 10px;
  font-size: 13px;
  background: var(--panel-alt);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.fix-count { font-family: var(--font-mono); font-size: 11.5px; color: var(--accent); white-space: nowrap; }
.fix-count.is-zero { color: var(--text-faint); }

/* ===== Toasts ===== */
.toasts {
  position: fixed;
  left: 50%;
  bottom: 48px;
  transform: translateX(-50%);
  z-index: 200;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  width: max-content;
  max-width: calc(100vw - 32px);
  pointer-events: none;
}
.toast {
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: 600px;
  padding: 8px 8px 8px 14px;
  font-size: 13px;
  line-height: 1.4;
  color: var(--bg);
  background: var(--text);
  border-radius: var(--radius);
  box-shadow: var(--shadow-modal);
  animation: toast-in 0.15s ease-out;
}
.toast-error { background: var(--danger); color: #fff; }
.toast-action {
  font: inherit;
  font-weight: 700;
  color: inherit;
  background: none;
  border: 1px solid currentColor;
  border-radius: var(--radius-sm);
  padding: 3px 9px;
  cursor: pointer;
}
.toast-close { font-size: 17px; line-height: 1; color: inherit; opacity: 0.6; background: none; border: none; padding: 2px 6px; cursor: pointer; }
.toast-close:hover { opacity: 1; }
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* ===== Drag & drop ===== */
body.drag-over::after {
  content: "Drop a video or subtitle file to open it";
  position: fixed;
  inset: 10px;
  z-index: 150;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 600;
  color: var(--text);
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  border: 2px dashed var(--accent);
  border-radius: 10px;
  pointer-events: none;
}

.timeline-hint { flex: 1; }

/* ===== Responsive ===== */
@media (max-width: 900px) {
  .workspace { grid-template-columns: 1fr; grid-template-rows: auto auto; }
  .video-pane { border-right: none; border-bottom: 1px solid var(--border-soft); }
  .video-frame { aspect-ratio: 16 / 9; flex: none; }
  .cue-pane { min-height: 320px; }
  .toolbar-end { margin-left: 0; }
  .timeline-hint { display: none; }
}
