/* ── Reset & base ────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:        #0d0d0d;
  --surface:   #1a1a1a;
  --surface2:  #222222;
  --border:    #2e2e2e;
  --accent:    #7c6af7;
  --accent-h:  #9d8fff;
  --danger:    #e05252;
  --text:      #e8e8e8;
  --text-dim:  #888;
  --text-mute: #555;
  --radius:    6px;
  --header-h:  54px;
  --timeline-h: 84px;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg:        #f8f8f8;
    --surface:   #ffffff;
    --surface2:  #f0f0f0;
    --border:    #e0e0e0;
    --accent:    #7c6af7;
    --accent-h:  #6a5ae0;
    --danger:    #d64545;
    --text:      #1a1a1a;
    --text-dim:  #666;
    --text-mute: #999;
  }
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  overflow: hidden;
}

/* ── Import bar — desktop fixed chrome ───────────────────────────────── */
#import-bar {
  position: fixed;
  inset: 0 0 auto 0;
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 16px;
  z-index: 100;
}

.brand {
  font-weight: 700;
  font-size: 15px;
  letter-spacing: -0.2px;
  color: var(--accent-h);
  white-space: nowrap;
  margin-right: 6px;
}

/* Hidden native file inputs */
input[type="file"] { display: none; }

.file-btn {
  display: inline-flex;
  align-items: center;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 12px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text-dim);
  white-space: nowrap;
  transition: border-color .15s, color .15s;
}
.file-btn:hover { border-color: var(--accent); color: var(--text); }
.file-btn.loaded { border-color: var(--accent); color: var(--text); }

.input-divider {
  font-size: 11px;
  color: var(--text-mute);
  padding: 0 2px;
  flex-shrink: 0;
}

.yt-group {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 1;
  min-width: 0;
}

#yt-url {
  flex: 1;
  min-width: 0;
  padding: 6px 10px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 13px;
  transition: border-color .15s;
}
#yt-url::placeholder { color: var(--text-mute); }
#yt-url:focus { outline: none; border-color: var(--accent); }

#yt-fetch-btn {
  padding: 6px 14px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-dim);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: border-color .15s, color .15s;
  min-width: 60px;
  text-align: center;
}
#yt-fetch-btn:hover:not(:disabled) { border-color: var(--accent); color: var(--text); }
#yt-fetch-btn:disabled { opacity: .5; cursor: default; }

.inline-label {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--text-dim);
  white-space: nowrap;
}
.inline-label input[type="number"] {
  width: 52px;
  padding: 5px 8px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 13px;
  text-align: center;
}
.inline-label input[type="number"]:focus { outline: none; border-color: var(--accent); }

#analyze-btn, #export-btn {
  padding: 7px 18px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  border: none;
  transition: background .15s, opacity .15s;
}
#analyze-btn { background: var(--accent); color: #fff; }
#analyze-btn:hover:not(:disabled) { background: var(--accent-h); }
#analyze-btn:disabled { opacity: .4; cursor: default; }

#status-msg { font-size: 12px; color: var(--text-dim); margin-left: 4px; }
#status-msg.error { color: var(--danger); }

/* ── Main layout — desktop ───────────────────────────────────────────── */
#main-layout {
  position: fixed;
  inset: var(--header-h) 0 var(--timeline-h) 0;
  display: flex;
  overflow: hidden;
}

/* ── Player panel ────────────────────────────────────────────────────── */
#player-panel {
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 16px;
  gap: 12px;
  min-width: 0;
  background: var(--bg);
}

#video-container {
  position: relative;
  width: 100%;
  max-width: 900px;
  aspect-ratio: 16 / 9;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  flex-shrink: 1;
}

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

/* 9:16 crop overlay */
#crop-overlay {
  position: absolute;
  top: 0;
  border: 2px solid var(--accent);
  box-shadow: 0 0 0 9999px rgba(0,0,0,.55);
  cursor: grab;
  user-select: none;
  display: none;
}
#crop-overlay:active { cursor: grabbing; }

.crop-handle {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 6px;
  height: 40px;
  background: var(--accent);
  border-radius: 3px;
  opacity: .7;
}
.crop-handle--left  { left: -4px; }
.crop-handle--right { right: -4px; }

#playback-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  max-width: 900px;
}

#playpause-btn, #replay-btn {
  padding: 7px 16px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  transition: background .15s, border-color .15s;
}
#playpause-btn:hover:not(:disabled),
#replay-btn:hover:not(:disabled) { background: #2a2a2a; border-color: var(--accent); }
#playpause-btn:disabled,
#replay-btn:disabled { opacity: .35; cursor: default; }

#clip-timer {
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  color: var(--text-dim);
  margin-left: 4px;
}

#trim-row {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  max-width: 900px;
  font-size: 13px;
  color: var(--text-dim);
}

.time-input {
  width: 72px;
  padding: 5px 8px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  text-align: center;
  transition: border-color .15s;
}
.time-input:focus { outline: none; border-color: var(--accent); }
.time-input.invalid { border-color: var(--danger); }

#zoom-row {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text-dim);
  font-size: 13px;
  width: 100%;
  max-width: 900px;
}
#zoom-slider { flex: 1; accent-color: var(--accent); }
#zoom-value { width: 36px; text-align: right; color: var(--text); }

#export-controls {
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  max-width: 900px;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 7px;
  cursor: pointer;
  color: var(--text-dim);
  user-select: none;
}
.checkbox-label input[type="checkbox"] {
  width: 15px; height: 15px;
  accent-color: var(--accent);
  cursor: pointer;
}

#export-btn { background: #2a6e3f; color: #d4f5e2; padding: 7px 20px; }
#export-btn:hover:not(:disabled) { background: #35874e; }
#export-btn:disabled { opacity: .35; cursor: default; }

/* ── Clips sidebar ───────────────────────────────────────────────────── */
#clips-sidebar {
  width: 300px;
  flex-shrink: 0;
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#clips-sidebar h2 {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--text-dim);
  padding: 14px 16px 10px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

#clip-list {
  list-style: none;
  overflow-y: auto;
  flex: 1;
  padding: 8px 0;
}

.empty-state {
  padding: 24px 16px;
  color: var(--text-mute);
  font-size: 13px;
  text-align: center;
}

.clip-item {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background .1s;
}
.clip-item:hover  { background: var(--surface2); }
.clip-item.active { background: #1e1a35; border-left: 3px solid var(--accent); }

.clip-item-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
}
.clip-title { font-weight: 600; font-size: 13px; line-height: 1.35; }
.clip-badge { font-size: 10px; color: var(--text-mute); white-space: nowrap; padding-top: 2px; }
.clip-why   { font-size: 12px; color: var(--text-dim); margin-top: 4px; line-height: 1.4; }

.clip-captions { display: none; margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--border); }
.clip-item.expanded .clip-captions { display: block; }

.caption-platform {
  font-size: 11px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .05em; color: var(--accent-h); margin-top: 8px;
}
.caption-text { font-size: 12px; color: var(--text-dim); line-height: 1.4; cursor: text; user-select: text; }

/* ── Timeline — desktop fixed chrome ─────────────────────────────────── */
#timeline-panel {
  position: fixed;
  inset: auto 0 0 0;
  height: var(--timeline-h);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 0 16px;
  gap: 6px;
  z-index: 100;
}

#timeline-wrap { position: relative; }

#timeline-track {
  position: relative;
  height: 28px;
  background: var(--surface2);
  border-radius: var(--radius);
  cursor: pointer;
  overflow: visible;
}

.clip-highlight {
  position: absolute;
  top: 4px;
  height: 20px;
  border-radius: 3px;
  opacity: .75;
  transition: opacity .15s;
  cursor: pointer;
}
.clip-highlight:hover { opacity: 1; }
.clip-highlight.active-highlight { opacity: 1; outline: 2px solid #fff4; }

#playhead {
  position: absolute;
  top: -4px; bottom: -4px;
  width: 2px;
  background: #fff;
  border-radius: 1px;
  pointer-events: none;
  left: 0;
}
#playhead::before {
  content: '';
  position: absolute;
  top: -4px;
  left: 50%;
  transform: translateX(-50%);
  width: 10px; height: 10px;
  border-radius: 50%;
  background: #fff;
}

#transcript-tooltip {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  background: #111;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 10px;
  font-size: 12px;
  color: var(--text);
  max-width: 420px;
  white-space: normal;
  pointer-events: none;
  z-index: 200;
  box-shadow: 0 4px 16px rgba(0,0,0,.6);
}

#time-display { display: flex; gap: 4px; font-size: 12px; font-variant-numeric: tabular-nums; }
#time-current  { color: var(--text); }
#time-duration { color: var(--text-mute); }

/* ── Mobile tab bar (desktop: hidden) ───────────────────────────────── */
#mobile-tabs { display: none; }

/* ── Scrollbar ───────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #444; }

/* ── Spinner ─────────────────────────────────────────────────────────── */
@keyframes spin { to { transform: rotate(360deg); } }
.spinner {
  display: inline-block;
  width: 13px; height: 13px;
  border: 2px solid rgba(255,255,255,.25);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin .6s linear infinite;
  vertical-align: middle;
  margin-right: 6px;
}


/* ═══════════════════════════════════════════════════════════════════════
   MOBILE — PORTRAIT
   Phones held vertically (or any narrow viewport)
   Tab-based: Player tab / Clips tab
   ═══════════════════════════════════════════════════════════════════════ */
@media (orientation: portrait) and (max-width: 768px) {
  :root {
    --tab-h:      52px;
    --timeline-h: 64px;
  }

  /* Full-height flex column — replaces fixed positioning */
  body {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    overflow: hidden;
  }

  /* ── Header: two compact rows ── */
  #import-bar {
    position: static;
    flex-shrink: 0;
    height: auto;
    flex-wrap: wrap;
    padding: 8px 12px;
    gap: 6px;
  }

  /* Row 1: brand (expands) | clips count | analyze */
  .brand         { order: 1; flex: 1 1 auto; font-size: 14px; margin: 0; }
  .inline-label  { order: 2; flex-shrink: 0; }
  #analyze-btn   { order: 3; flex-shrink: 0; min-height: 38px; }
  /* status below row 1 */
  #status-msg    { order: 4; width: 100%; font-size: 11px; min-height: 0; margin: 0; }
  /* Row 2: YouTube URL + Fetch full width */
  .yt-group      { order: 5; flex: 1 1 100%; }

  /* File upload path — hidden on mobile (YouTube is primary) */
  .file-btn, .input-divider { display: none; }

  /* Touch-friendly sizing */
  #yt-url, #yt-fetch-btn { min-height: 38px; font-size: 14px; }

  /* ── Main layout ── */
  #main-layout {
    position: static;
    flex: 1;
    min-height: 0;
    overflow: hidden;
  }

  /* Tab-driven panel visibility */
  #main-layout[data-mobile-tab="player"] #clips-sidebar { display: none; }
  #main-layout[data-mobile-tab="clips"]  #player-panel  { display: none; }

  /* ── Player panel ── */
  #player-panel {
    flex: 1;
    width: 100%;
    overflow-y: auto;
    padding: 10px;
    gap: 10px;
    justify-content: flex-start;
    align-items: stretch;
  }

  #video-container {
    max-width: none;
    width: 100%;
  }

  #playback-row,
  #trim-row,
  #zoom-row,
  #export-controls { max-width: none; width: 100%; }

  /* ── Clips sidebar ── */
  #clips-sidebar {
    flex: 1;
    width: 100%;
    border-left: none;
    overflow-y: auto;
  }

  /* ── Timeline ── */
  #timeline-panel {
    position: static;
    flex-shrink: 0;
    height: var(--timeline-h);
    padding: 0 12px;
  }

  /* ── Tab bar ── */
  #mobile-tabs {
    display: flex;
    flex-shrink: 0;
    height: var(--tab-h);
    background: var(--surface);
    border-top: 1px solid var(--border);
  }

  .tab-btn {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    background: none;
    border: none;
    border-top: 2px solid transparent;
    color: var(--text-dim);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: color .15s, background .15s;
    -webkit-tap-highlight-color: transparent;
  }
  .tab-btn:active { background: var(--surface2); }
  .tab-btn.active { color: var(--accent-h); border-top-color: var(--accent); }
}


/* ═══════════════════════════════════════════════════════════════════════
   MOBILE — LANDSCAPE
   Phones rotated sideways (landscape height ≤ 550px)
   Side-by-side layout, ultra-compact header, no tab bar
   ═══════════════════════════════════════════════════════════════════════ */
@media (orientation: landscape) and (max-height: 550px) {
  :root {
    --header-h:   44px;
    --timeline-h: 56px;
  }

  /* Full-height flex column — replaces fixed positioning */
  body {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    overflow: hidden;
  }

  /* ── Header: single compact row ── */
  #import-bar {
    position: static;
    flex-shrink: 0;
    height: var(--header-h);
    padding: 0 10px;
    gap: 8px;
    flex-wrap: nowrap;
    overflow: hidden;
  }

  .brand { font-size: 13px; margin: 0; white-space: nowrap; }

  /* File upload hidden — YouTube primary */
  .file-btn, .input-divider { display: none; }

  /* YouTube URL expands, fetch stays compact */
  .yt-group { flex: 1; min-width: 0; }
  #yt-url   { font-size: 13px; padding: 4px 8px; }
  #yt-fetch-btn { padding: 4px 12px; font-size: 13px; }

  .inline-label { flex-shrink: 0; font-size: 12px; }
  .inline-label input[type="number"] { width: 44px; padding: 3px 6px; font-size: 12px; }

  #analyze-btn { padding: 5px 14px; font-size: 13px; flex-shrink: 0; }
  #status-msg  { display: none; } /* no room; errors shown in other ways */

  /* ── Main layout: side-by-side ── */
  #main-layout {
    position: static;
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: row;
    overflow: hidden;
  }

  /* ── Player panel: column, video gets remaining height ── */
  #player-panel {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    justify-content: flex-start;
    padding: 6px;
    gap: 5px;
    overflow-y: auto;
    overflow-x: hidden;
    background: var(--bg);
  }

  /* Video fills ~half the panel height, shrinks if needed */
  #video-container {
    flex-shrink: 1;
    width: 100%;
    max-width: none;
    /* aspect-ratio drives the height; flex-shrink collapses if needed */
    aspect-ratio: 16 / 9;
    max-height: 50%;
    align-self: flex-start;
  }

  /* Compact controls */
  #playback-row,
  #trim-row,
  #zoom-row,
  #export-controls {
    max-width: none;
    width: 100%;
    flex-shrink: 0;
  }

  #playpause-btn, #replay-btn {
    padding: 5px 12px;
    font-size: 12px;
  }

  #trim-row   { font-size: 12px; gap: 6px; }
  .time-input { width: 62px; padding: 4px 6px; font-size: 12px; }

  #zoom-row   { font-size: 12px; gap: 8px; }
  #zoom-value { font-size: 12px; }

  #export-controls { gap: 10px; }
  .checkbox-label  { font-size: 12px; }
  #export-btn      { padding: 5px 14px; font-size: 12px; }

  /* ── Clips sidebar: narrower, full height ── */
  #clips-sidebar {
    width: 220px;
    flex-shrink: 0;
    border-left: 1px solid var(--border);
    overflow-y: auto;
  }

  #clips-sidebar h2 { padding: 10px 12px 8px; font-size: 11px; }
  .clip-item        { padding: 8px 12px; }
  .clip-title       { font-size: 12px; }
  .clip-why         { font-size: 11px; }

  /* ── Timeline ── */
  #timeline-panel {
    position: static;
    flex-shrink: 0;
    height: var(--timeline-h);
    padding: 0 10px;
    gap: 4px;
  }

  #timeline-track { height: 22px; }
  .clip-highlight { top: 3px; height: 16px; }
  #time-display   { font-size: 11px; }

  /* No tab bar in landscape */
  #mobile-tabs { display: none; }
}
