/* ── Global: prevent horizontal overflow on mobile ───────────────────────── */
html, body {
  overflow-x: hidden;
  max-width: 100%;
}

/* ── Mac/Retina visual polish ────────────────────────────────────────────── */
html {
  scroll-behavior: smooth;
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* Japanese text: use Noto Sans JP for consistent kana/kanji on all platforms */
.segment-ja, ruby, .modal-seg-ja {
  font-family: 'Noto Sans JP', 'Hiragino Sans', 'Yu Gothic', 'Meiryo', sans-serif;
}

/* Furigana: slightly smaller than current default inheritance */
rt {
  font-size: 0.55em;
  letter-spacing: 0.02em;
}

/* Navbar glass backdrop (cross-browser) */
nav {
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  backdrop-filter: blur(20px) saturate(180%);
}

/* ── Bottom tab bar height token ─────────────────────────────────────────── */
:root {
  --tab-bar-h: 56px;
}

/* ── Mobile bottom tab bar ───────────────────────────────────────────────── */
#bottom-nav {
  /* safe-area padding so icons clear the iPhone home indicator */
  padding-bottom: env(safe-area-inset-bottom, 0px);
  min-height: calc(var(--tab-bar-h) + env(safe-area-inset-bottom, 0px));
}

/* ── Non-episode pages: clear tab bar at bottom of main scroll area ──────── */
@media (max-width: 767px) {
  body:not(.ep-desktop) main {
    padding-bottom: calc(var(--tab-bar-h) + env(safe-area-inset-bottom, 0px) + 1.5rem);
  }
}

/* ── Transcript highlights — type (solid/dashed) + level (color) ─────────── */

.hl-vocab,
.hl-grammar {
  text-underline-offset: 3px;
  text-decoration-thickness: 2px;
  cursor: pointer;
  border-radius: 2px;
  transition: background-color 0.1s;
}
.hl-vocab   { text-decoration: underline; }
.hl-grammar { text-decoration: underline dashed; }

/* Level colors */
.hl-n5              { text-decoration-color: #22c55e; }
.hl-n4              { text-decoration-color: #14b8a6; }
.hl-n3              { text-decoration-color: #60a5fa; }
.hl-n2              { text-decoration-color: #f59e0b; }
.hl-n1              { text-decoration-color: #f43f5e; }
.hl-context-specific { text-decoration-color: #a78bfa; }

/* Hover tints */
.hl-n5:hover              { background-color: rgba(34,  197, 94,  0.15); }
.hl-n4:hover              { background-color: rgba(20,  184, 166, 0.15); }
.hl-n3:hover              { background-color: rgba(96,  165, 250, 0.15); }
.hl-n2:hover              { background-color: rgba(245, 158, 11,  0.15); }
.hl-n1:hover              { background-color: rgba(244, 63,  94,  0.15); }
.hl-context-specific:hover { background-color: rgba(167, 139, 250, 0.15); }

/* Legend demos in controls row */
.hl-n5-demo, .hl-n4-demo, .hl-n3-demo, .hl-n2-demo, .hl-n1-demo, .hl-ctx-demo {
  text-decoration: underline;
  text-underline-offset: 2px;
  text-decoration-thickness: 2px;
}
.hl-n5-demo  { text-decoration-color: #22c55e; }
.hl-n4-demo  { text-decoration-color: #14b8a6; }
.hl-n3-demo  { text-decoration-color: #60a5fa; }
.hl-n2-demo  { text-decoration-color: #f59e0b; }
.hl-n1-demo  { text-decoration-color: #f43f5e; }
.hl-ctx-demo { text-decoration-color: #a78bfa; }

/* ── Tooltip ─────────────────────────────────────────────────────────────── */

#tooltip {
  background: rgba(15, 20, 30, 0.92);
  -webkit-backdrop-filter: blur(16px) saturate(180%);
  backdrop-filter: blur(16px) saturate(180%);
  border: 1px solid rgba(75, 85, 99, 0.5);
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 13px;
  max-width: 260px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5), 0 0 0 1px rgba(255, 255, 255, 0.03);
  line-height: 1.5;
}
.tt-word    { font-size: 18px; font-weight: 700; }
.tt-reading { font-size: 13px; color: #9ca3af; font-weight: 400; margin-left: 4px; }
.tt-badge   {
  display: inline-block;
  font-size: 10px;
  border: 1px solid;
  border-radius: 4px;
  padding: 1px 6px;
  margin-left: 6px;
  vertical-align: middle;
}
/* Level-colored tooltip badge */
.tt-badge-n5               { color: #86efac; border-color: rgba(34,  197, 94,  0.4); background: rgba(34,  197, 94,  0.08); }
.tt-badge-n4               { color: #5eead4; border-color: rgba(20,  184, 166, 0.4); background: rgba(20,  184, 166, 0.08); }
.tt-badge-n3               { color: #93c5fd; border-color: rgba(96,  165, 250, 0.4); background: rgba(96,  165, 250, 0.08); }
.tt-badge-n2               { color: #fcd34d; border-color: rgba(245, 158, 11,  0.4); background: rgba(245, 158, 11,  0.08); }
.tt-badge-n1               { color: #fca5a5; border-color: rgba(244, 63,  94,  0.4); background: rgba(244, 63,  94,  0.08); }
.tt-badge-context-specific { color: #c4b5fd; border-color: rgba(167, 139, 250, 0.4); background: rgba(167, 139, 250, 0.08); }

.tt-register { font-size: 11px; color: #6b7280; margin-top: 1px; margin-bottom: 5px; }
.tt-en  { color: #93c5fd; font-size: 13px; }
.tt-zh  { color: #6ee7b7; font-size: 13px; }

/* ── Transcript segments ─────────────────────────────────────────────────── */

.segment {
  display: grid;
  grid-template-columns: 52px 1fr;
  column-gap: 6px;
  align-items: start;
  padding: 12px 16px;
  border-left: 3px solid transparent;
  border-radius: 6px;
  cursor: pointer;
  transition: background-color 0.12s, border-color 0.12s;
  margin-bottom: 8px;
}
.segment:hover { background-color: #161f2e; }

.segment-active {
  background: linear-gradient(90deg, #102848 0%, #0c1d33 100%) !important;
  border-left-color: #3b82f6;
  box-shadow: inset 0 0 0 1px rgba(59, 130, 246, 0.10);
}

.segment-time {
  font-size: 10px;
  color: #4b5563;
  font-family: ui-monospace, monospace;
  padding-top: 4px;
  user-select: none;
  line-height: 1;
}

.segment-body {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.segment-ja {
  font-size: 17px;
  line-height: 1.75;
}

.translation-en,
.translation-zh {
  font-size: 12px;
  line-height: 1.5;
  display: flex;
  align-items: baseline;
  gap: 5px;
}
.translation-en { color: #60a5fa; }
.translation-zh { color: #34d399; font-weight: 300; opacity: 0.75; }

.trans-tag {
  font-size: 0.65rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  color: #6b7280;
  background: rgba(255,255,255,0.1);
  border-radius: 999px;
  padding: 2px 6px;
  flex-shrink: 0;
  user-select: none;
}

/* ── Translation toggle buttons ──────────────────────────────────────────── */

.toggle-btn {
  padding: 3px 12px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  border: 1px solid #374151;
  background: transparent;
  color: #6b7280;
  cursor: pointer;
  transition: all 0.15s;
  letter-spacing: 0.03em;
}
.toggle-btn:hover { background: #1f2937; color: #d1d5db; }
.toggle-btn.active {
  background: #1e3a5f;
  border-color: #3b82f6;
  color: #93c5fd;
  box-shadow: 0 0 0 1px rgba(59, 130, 246, 0.3), 0 0 10px rgba(59, 130, 246, 0.12);
}

/* ── Playback speed buttons ──────────────────────────────────────────────── */

.speed-btn {
  padding: 2px 7px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 500;
  border: 1px solid #374151;
  background: transparent;
  color: #6b7280;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}
.speed-btn:hover { background: #1f2937; color: #d1d5db; }
.speed-active {
  background: #1e3a5f;
  border-color: #3b82f6;
  color: #93c5fd;
  box-shadow: 0 0 0 1px rgba(59, 130, 246, 0.3), 0 0 10px rgba(59, 130, 246, 0.12);
}

/* Utility: Hide scrollbar but allow scrolling */
.no-scrollbar::-webkit-scrollbar { display: none; }
.no-scrollbar { -ms-overflow-style: none; scrollbar-width: none; }

/* ── Side panel tab buttons ──────────────────────────────────────────────── */

.tab-btn {
  flex: 1;
  padding: 7px 4px;
  font-size: 12px;
  font-weight: 500;
  color: #6b7280;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  transition: all 0.15s;
  text-align: center;
  background: transparent;
  border-top: none;
  border-left: none;
  border-right: none;
}
.tab-btn:hover { color: #d1d5db; }
.tab-btn.tab-active {
  color: #93c5fd;
  border-bottom-color: #3b82f6;
}

/* ── Flashcards ──────────────────────────────────────────────────────────── */

.card {
  border: 1px solid #2a3547;
  border-radius: 8px;
  margin-bottom: 6px;
  overflow: hidden;
  transition: border-color 0.12s, box-shadow 0.12s;
}
.card:hover {
  border-color: #3d4f66;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
}

.card-front {
  background: #111827;
  padding: 8px 10px;
  font-size: 14px;
  font-weight: 500;
  display: flex;
  align-items: baseline;
  gap: 6px;
  flex-wrap: wrap;
}

.card-reading {
  font-size: 11px;
  color: #6b7280;
  font-weight: 400;
}

.card-level {
  font-size: 10px;
  border: 1px solid;
  border-radius: 4px;
  padding: 1px 6px;
  white-space: nowrap;
  font-weight: 600;
}
/* Level-colored card badges */
.card-level-n5               { color: #86efac; border-color: rgba(34,  197, 94,  0.4); background: rgba(34,  197, 94,  0.08); }
.card-level-n4               { color: #5eead4; border-color: rgba(20,  184, 166, 0.4); background: rgba(20,  184, 166, 0.08); }
.card-level-n3               { color: #93c5fd; border-color: rgba(96,  165, 250, 0.4); background: rgba(96,  165, 250, 0.08); }
.card-level-n2               { color: #fcd34d; border-color: rgba(245, 158, 11,  0.4); background: rgba(245, 158, 11,  0.08); }
.card-level-n1               { color: #fca5a5; border-color: rgba(244, 63,  94,  0.4); background: rgba(244, 63,  94,  0.08); }
.card-level-context-specific { color: #c4b5fd; border-color: rgba(167, 139, 250, 0.4); background: rgba(167, 139, 250, 0.08); }

.card-body {
  padding: 7px 10px 8px;
  font-size: 12px;
}

.card-en          { color: #93c5fd; margin-bottom: 1px; }
.card-zh          { color: #6ee7b7; margin-bottom: 4px; }
.card-construction { color: #fbbf24; font-size: 11px; margin-bottom: 3px; font-family: ui-monospace, monospace; }
.card-example     {
  color: #6b7280;
  font-size: 11px;
  font-style: italic;
  border-top: 1px solid #1f2937;
  padding-top: 5px;
  margin-top: 4px;
  line-height: 1.5;
}
.card-register    { font-size: 10px; color: #4b5563; margin-top: 3px; }

/* ── Stats chips ─────────────────────────────────────────────────────────── */

.stat-chip {
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 0.7rem;
  background: rgba(255,255,255,0.08);
  color: #9ca3af;
  white-space: nowrap;
}
.stat-chip-ctx {
  color: #a78bfa;
  background: rgba(167,139,250,0.08);
}

/* ── Empty state ─────────────────────────────────────────────────────────── */
.panel-empty {
  color: #4b5563;
  font-size: 12px;
  padding: 20px 10px;
  text-align: center;
}

/* ── Translation FAB ─────────────────────────────────────────────────────── */

.fab-pill {
  padding: 6px 16px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  border: 1px solid #374151;
  background: #111827;
  color: #6b7280;
  cursor: pointer;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
  transition: all 0.15s;
  letter-spacing: 0.04em;
}
.fab-pill.active {
  background: #1e3a5f;
  border-color: #3b82f6;
  color: #93c5fd;
}

.fab-tray-hidden {
  opacity: 0;
  transform: translateY(6px);
  pointer-events: none;
  transition: opacity 0.15s, transform 0.15s;
}
.fab-tray-visible {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
  transition: opacity 0.15s, transform 0.15s;
}

#fab-main.fab-on {
  background: #1e3a5f;
  border-color: #3b82f6;
  color: #93c5fd;
}

/* ── FAB discovery hint ──────────────────────────────────────────────────── */

.fab-hint {
  position: fixed;
  bottom: calc(var(--tab-bar-h) + 72px + env(safe-area-inset-bottom, 0px));
  right: 72px; /* left of FAB (16px margin + 48px button + 8px gap) */
  z-index: 51;
  background: #1e3a5f;
  border: 1px solid #3b82f6;
  color: #93c5fd;
  font-size: 11px;
  font-weight: 600;
  padding: 5px 12px;
  border-radius: 999px;
  white-space: nowrap;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
  pointer-events: none;
  opacity: 1;
  transition: opacity 0.5s;
}
.fab-hint-fade { opacity: 0; }

/* ── YouTube embed container ─────────────────────────────────────────────── */

.yt-container {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
}

.yt-container iframe,
.yt-container #yt-player {
  position: absolute !important;
  inset: 0 !important;
  width: 100% !important;
  height: 100% !important;
  border: 0;
}

/* ── Compact transcript (video mode) ────────────────────────────────────── */

.compact-mode {
  display: flex !important;
  flex-direction: column;
  justify-content: center;
}

.compact-mode .segment {
  opacity: 0.45;
  transition: opacity 0.2s, background-color 0.12s, border-color 0.12s;
}
.compact-mode .segment.seg-nb-near   { opacity: 0.72; }
.compact-mode .segment.segment-active { opacity: 1; }

/* ── Full transcript modal ───────────────────────────────────────────────── */

.modal-seg {
  display: grid;
  grid-template-columns: 52px 1fr;
  column-gap: 6px;
  align-items: start;
  padding: 7px 10px;
  border-left: 3px solid transparent;
  border-radius: 6px;
  cursor: pointer;
  transition: background-color 0.12s, border-color 0.12s;
  margin-bottom: 2px;
}
.modal-seg:hover { background-color: #1a2332; }
.modal-seg-active { background-color: #0f2240 !important; border-left-color: #3b82f6; }

.modal-seg-time {
  font-size: 10px;
  color: #4b5563;
  font-family: ui-monospace, monospace;
  padding-top: 3px;
  user-select: none;
  line-height: 1;
}

.modal-seg-body {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.modal-seg-ja {
  font-size: 15px;
  line-height: 1.7;
}

/* ── Nearby-segment visibility (desktop video compact mode) ─────────────── */
.seg-nb-hidden { display: none !important; }

/* ── Desktop episode mode: viewport-locked, no-scroll layout ────────────── */
@media (min-width: 768px) {
  body.ep-desktop main {
    height: calc(100vh - var(--nav-h, 57px));
    overflow: hidden;
    padding-bottom: 0;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
  }
  body.ep-desktop #ep-page {
    flex: 1;
    min-height: 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }
  body.ep-desktop #ep-header { flex-shrink: 0; }
  body.ep-desktop #ep-two-col {
    flex: 1;
    min-height: 0 !important;
  }
  body.ep-desktop #ep-left-col {
    min-height: 0;
    overflow: hidden;
  }
  /* Transcript: fill available column space and scroll internally (audio mode) */
  body.ep-desktop #transcript {
    max-height: none !important;
    overflow-y: auto;
  }
  /* Right panel: fill column height and scroll internally */
  body.ep-desktop #side-panel {
    min-height: 0;
    overflow: hidden;
  }
  body.ep-desktop #panel-container {
    flex: 1;
    min-height: 0;
    overflow: hidden;
  }
  /* Audio layout: the direct .overflow-y-auto child of side-panel fills height on desktop */
  body.ep-desktop #side-panel > .overflow-y-auto {
    flex: 1 !important;
    min-height: 0 !important;
    max-height: none !important;
    overflow-y: auto !important;
  }

  /* YouTube video mode */
  body.ep-desktop.yt-mode #ep-left-col {
    overflow: visible;
  }

  /* Cap the YT player height so the controls row (Audio only / Video toggle)
     is never pushed below the ep-page clip boundary.
     --nav-h is set by JS; 160px covers ep-header + controls row + gaps. */
  body.ep-desktop.yt-mode .yt-container {
    max-height: calc(100vh - var(--nav-h, 52px) - 160px);
    width: min(100%, calc((100vh - var(--nav-h, 52px) - 160px) * 16 / 9));
  }
}

/* ── Jump-to-now pill ────────────────────────────────────────────────────── */

.jump-now-pill {
  position: absolute;
  bottom: 12px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 10;
  padding: 5px 16px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  background: #1e3a5f;
  border: 1px solid #3b82f6;
  color: #93c5fd;
  cursor: pointer;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.6);
  white-space: nowrap;
  transition: background 0.15s, color 0.15s;
}
.jump-now-pill:hover { background: #1d4ed8; color: #fff; }

.jump-now-pill.pill-fixed {
  position: fixed;
  /* drawer trigger bar (~40px) stacked on top of tab bar */
  bottom: calc(var(--tab-bar-h) + 40px + 24px + env(safe-area-inset-bottom, 0px));
  left: 50%;
  transform: translateX(-50%);
}

/* ── Mobile (≤ 767px) ────────────────────────────────────────────────────── */

@media (max-width: 767px) {
  /* Page-scroll mode: transcript grows to content, page scrolls (not container). */
  #transcript-card {
    overflow: visible;
  }
  #transcript-card-body {
    overflow: visible;
    flex: none; /* size to content, not flex-1 (which collapses to 0 in auto-height parent) */
  }
  #transcript {
    height: auto !important;
    overflow-y: visible;
    /* clear the drawer trigger bar stacked on top of the tab bar */
    padding-bottom: calc(var(--tab-bar-h) + 72px);
  }

  /* Video mode: fixed-height, no scroll (nearby segments managed by JS) */
  #transcript.compact-mode {
    height: 35vh !important;
    overflow: hidden !important;
    padding-bottom: 0 !important;
  }

  .segment {
    grid-template-columns: 1fr;
    padding: 12px 10px;
    /* Page-scroll auto-follow targets window scroll; keep the active line
       clear of the sticky nav + audio player (top) and the fixed drawer
       trigger bar (bottom) so it isn't hidden behind them. */
    scroll-margin-top: 150px;
    scroll-margin-bottom: 72px;
  }
  /* Compact (video) mode doesn't page-scroll — no offset needed. */
  #transcript.compact-mode .segment {
    scroll-margin-top: 0;
    scroll-margin-bottom: 0;
  }
  .segment-time { display: none; }
  .segment-ja   { font-size: 18px; }

  /* Episode title: clamp to 2 lines on mobile */
  #ep-title.ep-title-clamped {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }

  /* Side panel becomes a slide-up bottom drawer */
  #side-panel {
    position: fixed !important;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100% !important;
    max-height: 80vh;
    display: flex !important;
    flex-direction: column;
    background: #030712;
    border-top: 1px solid #1f2937;
    border-radius: 1rem 1rem 0 0;
    transform: translateY(100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 50;
    overflow: hidden;
  }
  #side-panel.drawer-open {
    transform: translateY(0);
  }

  /* Panel scroll area: fill drawer, not a fixed px height */
  #side-panel .overflow-y-auto {
    max-height: none !important;
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
}

.hide-furigana ruby rt {
  display: none !important;
}

/* ── Episode processing indicators (index page) ──────────────────────────── */

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

/* Spinning ring overlaid on the date badge */
.ep-proc-ring {
  position: absolute;
  inset: -3px;
  border-radius: 11px;
  border: 2px solid rgba(59, 130, 246, 0.15);
  border-top-color: #3b82f6;
  border-right-color: #60a5fa;
  animation: ep-spin 1.1s linear infinite;
  pointer-events: none;
}

/* Pill shown in the status row */
.ep-proc-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 11px;
  color: #60a5fa;
  font-weight: 500;
}

/* Small spinning dot inside the pill */
.ep-proc-spinner {
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  border: 1.5px solid rgba(96, 165, 250, 0.3);
  border-top-color: #60a5fa;
  animation: ep-spin 0.75s linear infinite;
  flex-shrink: 0;
}

/* ── Custom scrollbars (transcript + side panel) ─────────────────────────── */

#transcript::-webkit-scrollbar,
#panel-container::-webkit-scrollbar { width: 4px; }

#transcript::-webkit-scrollbar-track,
#panel-container::-webkit-scrollbar-track { background: transparent; }

#transcript::-webkit-scrollbar-thumb,
#panel-container::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.10);
  border-radius: 2px;
}

#transcript::-webkit-scrollbar-thumb:hover,
#panel-container::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.22);
}
