/* ── Design tokens ── */
:root {
  --bg: #0c0d10;
  --surface: #15171c;
  --surface-2: #1c1f25;
  --ink: #f1f1ec;
  --ink-2: #c8c9c4;
  --muted: #8a8d94;
  --muted-2: #5a5d64;
  --hair: rgba(255, 255, 255, 0.06);
  --hair-2: rgba(255, 255, 255, 0.12);
  --accent: #e08363;
  --accent-soft: rgba(224, 131, 99, 0.13);
  --active: #6aa17a;
  --warn: #f8af64;
  --serif: "Instrument Serif", "New York", "Times New Roman", serif;
  --sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", "Segoe UI", system-ui, sans-serif;
  --radius: 14px;
  --radius-sm: 10px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }

body {
  font-family: var(--sans);
  background: var(--bg);
  color: var(--ink);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overflow: hidden;
  letter-spacing: -0.005em;
}

/* ── App shell ── */
.app {
  display: grid;
  grid-template-rows: 56px 1fr;
  height: 100vh;
}

/* ── Topbar ── */
.topbar {
  display: grid;
  grid-template-columns: 1fr auto 360px;
  align-items: center;
  padding: 0 24px 0 28px;
  border-bottom: 1px solid var(--hair);
  background: rgba(12, 13, 16, 0.82);
  backdrop-filter: saturate(140%) blur(20px);
  position: relative;
  z-index: 10;
}

.brand {
  display: flex;
  align-items: baseline;
  gap: 10px;
}

.brand-name {
  font-family: var(--serif);
  font-size: 22px;
  letter-spacing: -0.01em;
  font-weight: 400;
  color: var(--ink);
}
.brand-name em { font-style: italic; color: var(--accent); }

.brand-tag {
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.brand-link {
  text-decoration: none;
  transition: color 0.15s ease;
}
.brand-link:hover { color: var(--ink); }

.topbar-mid {
  display: flex;
  justify-content: center;
}

.clip-picker {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  padding: 7px 8px 7px 14px;
  background: var(--surface);
  border: 1px solid var(--hair);
  border-radius: 999px;
}

.clip-picker-label {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
}

.clip-picker select {
  appearance: none;
  -webkit-appearance: none;
  background: transparent;
  border: none;
  font-family: inherit;
  font-size: 13.5px;
  color: var(--ink);
  padding: 0 22px 0 0;
  cursor: pointer;
  outline: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%2380848e' stroke-width='1.4' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 4px center;
}

/* Status chip — dot comes from ::before */
.status-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  background: var(--surface-2);
  border-radius: 999px;
  font-size: 11.5px;
  color: var(--ink-2);
  white-space: nowrap;
}
.status-chip::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--muted);
  flex-shrink: 0;
}
.status-chip.real::before { background: var(--active); }
.status-chip.stub::before { background: var(--warn); }

.topbar-right {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
}

.ghost-btn {
  background: transparent;
  border: 1px solid var(--hair-2);
  color: var(--ink-2);
  padding: 7px 14px;
  border-radius: 999px;
  font-family: inherit;
  font-size: 13px;
  cursor: pointer;
  transition: border-color 160ms, background 160ms, color 160ms;
}
.ghost-btn:hover {
  border-color: var(--ink-2);
  background: var(--surface);
  color: var(--ink);
}

/* ── Main layout ── */
.app-main {
  display: grid;
  grid-template-columns: 1fr 380px;
  min-height: 0;
  height: 100%;
}

.center {
  display: grid;
  grid-template-columns: minmax(0, 1.05fr) minmax(0, 1fr);
  gap: 14px;
  min-height: 0;
  height: 100%;
  padding: 20px 14px 20px 24px;
}

.right {
  display: flex;
  flex-direction: column;
  min-height: 0;
  height: 100%;
  padding: 20px 24px 16px 10px;
  gap: 12px;
}

/* ── Cards ── */
.card {
  background: var(--surface);
  border: 1px solid var(--hair);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.right-card {
  flex: 1;
  min-height: 0;
}

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px 10px;
  flex-shrink: 0;
}

.card-eyebrow {
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 500;
  margin-bottom: 2px;
}

.card-title {
  margin: 0;
  font-family: var(--serif);
  font-size: 20px;
  font-weight: 400;
  letter-spacing: -0.01em;
  color: var(--ink);
}
.card-title em { font-style: italic; color: var(--accent); }

/* ── Video pane ── */
.video-stage {
  flex: 1;
  margin: 0 16px;
  border-radius: var(--radius-sm);
  background: #000;
  border: 1px solid var(--hair);
  overflow: hidden;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

#clip-video {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

.video-note {
  padding: 6px 20px 12px;
  color: var(--muted);
  font-size: 12px;
  flex-shrink: 0;
  min-height: 26px;
}

/* ── Brain pane ── */
.brain-mount {
  flex: 1;
  margin: 0 16px;
  border-radius: var(--radius-sm);
  background: radial-gradient(70% 50% at 50% 45%, #1a1d24 0%, #0a0b0e 80%);
  border: 1px solid var(--hair);
  overflow: hidden;
  min-height: 0;
  position: relative;
  touch-action: none;
}

.brain-mount canvas { display: block; }

.brain-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 20px 12px;
  flex-shrink: 0;
}

.brain-hint {
  font-size: 11.5px;
  color: var(--muted-2);
}

.legend {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 11.5px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.legend-bar {
  width: 72px;
  height: 5px;
  border-radius: 3px;
  background: linear-gradient(90deg, #7aa3c4 0%, #2a2c32 50%, #e08363 100%);
}

/* HUD lives inside brain-mount */
.hud {
  position: absolute;
  left: 14px;
  bottom: 12px;
  font-size: 11.5px;
  color: var(--muted);
  background: rgba(12, 13, 16, 0.72);
  padding: 5px 8px;
  border-radius: 6px;
  backdrop-filter: blur(8px);
  pointer-events: none;
  z-index: 2;
  max-width: calc(100% - 28px);
}

/* Auto-rotate toggle */
.auto-rotate-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
}

.auto-rotate-toggle input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
  pointer-events: none;
}

.auto-rotate-toggle .switch {
  position: relative;
  display: inline-block;
  width: 30px;
  height: 16px;
  background: var(--surface-2);
  border: 1px solid var(--hair-2);
  border-radius: 999px;
  transition: background 0.18s ease, border-color 0.18s ease;
}

.auto-rotate-toggle .switch::after {
  content: "";
  position: absolute;
  top: 1px;
  left: 1px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--muted);
  transition: transform 0.18s ease, background 0.18s ease;
}

.auto-rotate-toggle input:checked ~ .switch {
  background: color-mix(in srgb, var(--accent) 35%, var(--surface-2));
  border-color: var(--accent);
}

.auto-rotate-toggle input:checked ~ .switch::after {
  transform: translateX(14px);
  background: var(--ink);
}

.auto-rotate-toggle input:focus-visible ~ .switch {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ── ROI tooltip (inside brain-mount) ── */
.roi-tooltip {
  position: absolute;
  pointer-events: none;
  background: rgba(28, 31, 37, 0.94);
  backdrop-filter: blur(14px);
  border: 1px solid var(--hair-2);
  border-radius: 12px;
  padding: 12px 14px;
  min-width: 200px;
  max-width: 240px;
  z-index: 4;
  line-height: 1.4;
}

.roi-tooltip .tip-short {
  font-family: var(--serif);
  font-size: 18px;
  font-style: italic;
  color: var(--accent);
  margin-bottom: 2px;
}

.roi-tooltip .tip-full {
  font-size: 12.5px;
  color: var(--ink);
  margin-bottom: 8px;
}

.roi-tooltip .tip-row {
  display: flex;
  justify-content: space-between;
  font-size: 11.5px;
  margin-top: 3px;
}

.roi-tooltip .tip-row .lbl { color: var(--muted); }
.roi-tooltip .tip-row .val { color: var(--ink-2); font-variant-numeric: tabular-nums; }

.roi-tooltip .tip-keys {
  margin-top: 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.roi-tooltip .tip-keys span {
  font-size: 10.5px;
  background: var(--surface-2);
  color: var(--ink-2);
  padding: 2px 7px;
  border-radius: 999px;
}

.hidden { display: none; }

/* ── Right panel tabs ── */
.tabs {
  display: flex;
  padding: 0 20px;
  border-bottom: 1px solid var(--hair);
  flex-shrink: 0;
}

.tab {
  background: transparent;
  border: none;
  font-family: inherit;
  font-size: 13.5px;
  color: var(--muted);
  padding: 13px 4px;
  margin-right: 22px;
  cursor: pointer;
  position: relative;
  transition: color 160ms;
}
.tab:hover { color: var(--ink-2); }
.tab.active { color: var(--ink); }
.tab.active::after {
  content: "";
  position: absolute;
  left: 0; right: 0; bottom: -1px;
  height: 2px;
  background: var(--ink);
  border-radius: 1px 1px 0 0;
}

.tab-count {
  color: var(--muted-2);
  margin-left: 5px;
  font-size: 11.5px;
  font-variant-numeric: tabular-nums;
}
.tab.active .tab-count { color: var(--muted); }

.tab-pane {
  flex: 1;
  min-height: 0;
  display: none;
  flex-direction: column;
  overflow: hidden;
}
.tab-pane.active { display: flex; }

.tab-empty {
  padding: 28px 20px;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
  font-style: italic;
  margin: 0;
}

/* ── Commentary list ── */
.comm-list {
  flex: 1;
  overflow: auto;
  padding: 4px 8px 16px;
}

.comm-item {
  padding: 14px 12px;
  border-bottom: 1px solid var(--hair);
  border-radius: 8px;
  cursor: pointer;
  transition: background 140ms;
}
.comm-item:last-child { border-bottom: none; }
.comm-item:hover { background: var(--surface-2); }
.comm-item.active { background: var(--accent-soft); }

.comm-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 5px;
}

.comm-window {
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.comm-item.active .comm-window { color: var(--accent); }

.comm-conf {
  font-size: 10.5px;
  color: var(--muted-2);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.comm-rois {
  display: flex;
  gap: 0;
  margin-bottom: 6px;
  flex-wrap: wrap;
  align-items: center;
}

.roi-chip {
  font-family: var(--serif);
  font-style: italic;
  font-size: 14px;
  color: var(--accent);
  line-height: 1;
}
.roi-chip + .roi-chip::before {
  content: "·";
  margin: 0 5px;
  color: var(--muted-2);
  font-style: normal;
  font-family: var(--sans);
}

.comm-headline {
  font-family: var(--serif);
  font-size: 17px;
  font-weight: 400;
  line-height: 1.3;
  margin-bottom: 4px;
  letter-spacing: -0.005em;
}

.comm-body {
  color: var(--ink-2);
  font-size: 12.5px;
  line-height: 1.55;
}

/* ── Notes panel ── */
.notes-form {
  padding: 14px 20px;
  border-bottom: 1px solid var(--hair);
  flex-shrink: 0;
}

.notes-form-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.notes-tc {
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.notes-tc b { color: var(--ink); font-weight: 500; }

.note-input {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 10px 12px;
  font-family: inherit;
  font-size: 13.5px;
  color: var(--ink);
  resize: none;
  height: 64px;
  line-height: 1.5;
  display: block;
  transition: background 140ms, border-color 140ms;
}
.note-input::placeholder { color: var(--muted); }
.note-input:focus {
  outline: none;
  background: var(--surface);
  border-color: var(--hair-2);
}

.notes-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 8px;
}

.save-btn {
  background: var(--ink);
  color: var(--bg);
  border: none;
  border-radius: 999px;
  padding: 8px 16px;
  font-family: inherit;
  font-size: 13px;
  cursor: pointer;
  transition: opacity 140ms, transform 120ms;
}
.save-btn:hover { opacity: 0.88; }
.save-btn:active { transform: scale(0.98); }

.notes-list {
  flex: 1;
  overflow: auto;
  padding: 4px 8px 16px;
}

.note-item {
  padding: 12px;
  border-bottom: 1px solid var(--hair);
  border-radius: 8px;
  transition: background 140ms;
}
.note-item:last-child { border-bottom: none; }
.note-item:hover { background: var(--surface-2); }

.note-time {
  font-size: 11.5px;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
  margin-bottom: 3px;
}

.note-text {
  color: var(--ink);
  font-size: 13px;
  line-height: 1.5;
}

/* ── Disclaimer ── */
.disclaimer {
  font-size: 11px;
  color: var(--muted-2);
  text-align: center;
  padding: 0 4px;
  flex-shrink: 0;
  line-height: 1.4;
}

/* ── Scrollbars ── */
*::-webkit-scrollbar { width: 6px; height: 6px; }
*::-webkit-scrollbar-track { background: transparent; }
*::-webkit-scrollbar-thumb { background: rgba(255, 255, 255, 0.10); border-radius: 3px; }
*::-webkit-scrollbar-thumb:hover { background: rgba(255, 255, 255, 0.18); }

/* ── Responsive ── */
@media (max-width: 1100px) {
  body { overflow: auto; }

  .app-main {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto;
    height: auto;
  }

  .center {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(300px, 1fr) minmax(300px, 1fr);
    height: auto;
    padding: 16px;
  }

  .right {
    padding: 0 16px 16px;
    height: auto;
  }

  .right-card { height: 480px; }
}

/* ── Mobile (single-column, page-scroll, no overlap) ── */
@media (max-width: 720px) {
  body {
    overflow-x: hidden;
    overflow-y: auto;
  }

  .app {
    grid-template-rows: auto 1fr;
    height: auto;
    min-height: 100vh;
    max-width: 100vw;
  }

  /* Topbar: stack into rows, no horizontal overflow */
  .topbar {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto auto auto;
    gap: 8px;
    padding: 10px 14px;
    min-width: 0;
    max-width: 100vw;
  }
  .brand,
  .topbar-mid,
  .topbar-right { min-width: 0; }
  .topbar-mid { justify-content: flex-start; }
  .clip-picker {
    width: 100%;
    min-width: 0;
    max-width: 100%;
    box-sizing: border-box;
  }
  .clip-picker select {
    flex: 1 1 0;
    min-width: 0;
    width: 100%;
  }
  .topbar-right {
    flex-wrap: wrap;
    justify-content: flex-start;
    gap: 6px;
  }
  .ghost-btn { padding: 6px 12px; font-size: 12.5px; }

  /* Main: single column, content-driven height */
  .app-main {
    display: flex;
    flex-direction: column;
    grid-template-columns: none;
    grid-template-rows: none;
    height: auto;
    min-width: 0;
    max-width: 100vw;
  }

  /* Center: stack video then brain */
  .center {
    display: flex;
    flex-direction: column;
    grid-template-columns: none;
    grid-template-rows: none;
    height: auto;
    padding: 12px;
    gap: 12px;
    min-width: 0;
  }

  .right {
    padding: 0 12px 16px;
    height: auto;
    gap: 12px;
    min-width: 0;
  }

  .card { min-width: 0; }

  /* Cards: let the commentary card grow with its content */
  .card { overflow: visible; }

  /* Video pane: landscape 16:9 */
  .video-stage {
    flex: none;
    aspect-ratio: 16 / 9;
    min-height: 0;
    height: auto;
  }

  /* Brain pane: square */
  .brain-mount {
    flex: none;
    aspect-ratio: 1 / 1;
    min-height: 0;
    height: auto;
  }

  /* Commentary card: expand to fit, no internal scroll */
  .right-card {
    flex: none;
    height: auto;
  }
  .tab-pane {
    overflow: visible;
  }
  .comm-list {
    flex: none;
    overflow: visible;
    padding-bottom: 8px;
  }
  .notes-list {
    flex: none;
    overflow: visible;
  }

  /* Upload modal: fit narrow viewport */
  .upload-modal {
    min-width: 0;
    width: calc(100vw - 32px);
    padding: 24px;
  }
}

/* ── Upload overlay ── */
.upload-overlay {
  position: fixed;
  inset: 0;
  background: rgba(12, 13, 16, 0.78);
  backdrop-filter: blur(12px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.upload-overlay.hidden { display: none; }

.upload-modal {
  background: var(--surface);
  border: 1px solid var(--hair-2);
  border-radius: var(--radius);
  padding: 32px 36px;
  min-width: 360px;
  max-width: 480px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  align-items: stretch;
}

.upload-modal-title {
  font-family: var(--serif);
  font-size: 20px;
  color: var(--ink);
  font-style: italic;
}

.upload-progress-wrap {
  height: 4px;
  background: var(--surface-2);
  border-radius: 2px;
  overflow: hidden;
}

.upload-progress-bar {
  height: 100%;
  width: 0%;
  background: var(--accent);
  border-radius: 2px;
  transition: width 100ms linear;
}
.upload-progress-bar.upload-error { background: #e06363; }

.upload-modal-status-row {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 18px;
}

.upload-modal-status {
  font-size: 13px;
  color: var(--muted);
  flex: 1;
}
.upload-modal-status.upload-error { color: #e06363; }

.upload-spinner {
  width: 14px;
  height: 14px;
  border: 2px solid var(--hair-2);
  border-top-color: var(--accent);
  border-radius: 50%;
  flex: 0 0 auto;
  animation: upload-spin 0.8s linear infinite;
}
.upload-spinner.hidden { display: none; }
.upload-spinner.upload-error {
  border-top-color: #e06363;
  animation: none;
}

@keyframes upload-spin {
  to { transform: rotate(360deg); }
}

.upload-elapsed {
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  flex: 0 0 auto;
}
.upload-elapsed:empty { display: none; }

/* ── Clip action buttons (rename / delete) ── */
.clip-delete-btn { color: #e06363; border-color: #e06363; }
.clip-delete-btn:hover { background: rgba(224,99,99,0.12); border-color: #e06363; color: #e06363; }
