/* Layout: header + subtitles + bottom bar */
#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
  overflow: hidden;
}

/* Header */
.header {
  position: sticky;
  top: 0;
  z-index: var(--z-header);
  display: flex;
  align-items: center;
  gap: var(--sp-md);
  padding: var(--sp-sm) var(--sp-lg);
  height: 54px;
  background: var(--bg-glass);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  overflow: visible;
  /* Prevent flex children from overflowing */
  min-width: 0;
  max-width: 100%;
}

.header__title {
  font-size: var(--fs-lg);
  font-weight: var(--fw-semibold);
  color: var(--text-primary);
  white-space: nowrap;
  margin-left: auto;
}

/* Subtitles area */
.subtitles {
  flex: 1;
  overflow-y: auto;
  padding: var(--sp-lg);
  padding-bottom: 100px; /* space for floating pill bottombar */
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
}

/* Jump-to-latest pill — appears when user scrolled up and new segments
   arrive beneath their viewport. Same pattern as Slack/Discord/Twitch
   chat: tells the user "you're reading older content; N new messages
   below; click to catch up". */
.jump-to-latest {
  position: fixed;
  right: 50%;
  bottom: 110px; /* above the bottombar */
  transform: translate(50%, 20px);
  display: flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.5rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg-card);
  color: var(--text-primary);
  font-size: 0.82rem;
  font-weight: 600;
  box-shadow: 0 4px 16px rgba(0,0,0,0.15);
  cursor: pointer;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s ease, transform 0.18s ease;
  z-index: 50;
  font-family: inherit;
}
.jump-to-latest.visible {
  opacity: 1;
  pointer-events: auto;
  transform: translate(50%, 0);
}
.jump-to-latest:hover {
  border-color: var(--accent);
  color: var(--accent);
}
.jump-to-latest__icon {
  font-size: 1rem;
  font-weight: 700;
  line-height: 1;
}
.jump-to-latest__count {
  font-weight: 700;
  color: var(--accent);
}
.jump-to-latest__label {
  color: var(--text-secondary);
}

.subtitles__empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  gap: var(--sp-sm);
  color: var(--text-muted);
  letter-spacing: 0.02em;
  text-align: center;
  padding: 0 var(--sp-lg);
  /* Live audio level (0..1) driven by JS, consumed by the icon below */
  --audio-level: 0;
}

.subtitles__empty-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--bg-tertiary);
  border: 1.5px solid var(--border);
  position: relative;
  transition: background 0.25s ease, border-color 0.25s ease,
              transform 120ms ease-out, box-shadow 120ms ease-out;
}

/* Listening: calm breathing opacity + audio-reactive glow ring + micro scale.
   The scale + glow only apply here (not during spin) so transforms don't fight. */
.subtitles__empty[data-stage="listening"] .subtitles__empty-icon,
.subtitles__empty[data-stage="hearing"] .subtitles__empty-icon {
  border-color: var(--accent);
  animation: emptyIconBreathe 2s ease-in-out infinite;
  transform: scale(calc(1 + var(--audio-level) * 0.06));
  /* color-mix keeps the glow on-theme for both dark + light */
  box-shadow:
    0 0 0 calc(2px + var(--audio-level) * 10px)
    color-mix(in srgb, var(--accent) calc(18% + var(--audio-level) * 22%), transparent);
}

.subtitles__empty[data-stage="hearing"] .subtitles__empty-icon {
  background: color-mix(in srgb, var(--accent) 18%, transparent);
  animation-duration: 1.1s;
}

.subtitles__empty[data-stage="loading"] .subtitles__empty-icon {
  border-top-color: var(--accent);
  animation: emptyIconSpin 1s linear infinite;
}

.subtitles__empty[data-stage="processing"] .subtitles__empty-icon {
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  border-color: var(--accent);
  animation: emptyIconBreathe 0.9s ease-in-out infinite;
}

@keyframes emptyIconBreathe {
  0%, 100% { opacity: 0.75; }
  50%      { opacity: 1; }
}

@keyframes emptyIconSpin {
  to { transform: rotate(360deg); }
}

.subtitles__empty-title {
  font-size: var(--fs-md);
  font-weight: var(--fw-normal);
  color: var(--text-secondary);
  opacity: 0.85;
}

.subtitles__empty-hint {
  font-size: var(--fs-sm);
  font-weight: var(--fw-normal);
  color: var(--text-muted);
  opacity: 0.65;
  max-width: 32ch;
}

/* Collapse empty hint line entirely so spacing stays tight */
.subtitles__empty-hint:empty {
  display: none;
}

/* Bottom bar — Floating Pill base on desktop */
.bottombar {
  position: fixed;
  bottom: var(--sp-lg);
  left: 50%;
  transform: translateX(-50%);
  z-index: var(--z-header);
  display: flex;
  align-items: center;
  gap: var(--sp-sm);
  padding: var(--sp-sm) var(--sp-lg);
  min-height: 52px;
  background: var(--bg-glass);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
  flex-shrink: 0;
  width: auto;
  max-width: calc(100vw - var(--sp-2xl));
  overflow: visible;
  box-sizing: border-box;
  transition: all 0.3s ease;
}

/* Compact button sizing inside the bottombar */
.bottombar .btn--primary,
.bottombar .btn--danger {
  padding: var(--sp-sm) var(--sp-lg);
  font-size: var(--fs-sm);
  min-height: 40px;
  min-width: auto;
}

/* Settings slide-out panel */
.settings-panel {
  position: fixed;
  top: 0;
  right: 0;
  width: min(400px, 90vw);
  height: 100vh;
  height: 100dvh;
  background: var(--bg-secondary);
  border-left: 1px solid var(--border);
  z-index: var(--z-panel);
  transform: translateX(100%);
  transition: transform var(--duration-normal) var(--ease-out);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: var(--sp-xl);
  padding-top: max(var(--sp-xl), env(safe-area-inset-top));
}

.settings-panel.open {
  transform: translateX(0);
}

.settings-backdrop {
  position: fixed;
  inset: 0;
  background: var(--bg-overlay);
  z-index: calc(var(--z-panel) - 1);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--duration-normal);
}

.settings-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}

/* Safe areas for notched phones (iPhone X+, etc.) */
.header {
  padding-top: max(var(--sp-sm), env(safe-area-inset-top));
  padding-left: max(var(--sp-lg), env(safe-area-inset-left));
  padding-right: max(var(--sp-lg), env(safe-area-inset-right));
}

/* Safe area overrides are already in the .bottombar base rule above */

.bottombar__spacer {
  flex: 1;
}

/* Responsive — small phones */
@media (max-width: 640px) {
  .header {
    padding: var(--sp-xs) var(--sp-sm);
    padding-top: max(var(--sp-xs), env(safe-area-inset-top));
    padding-left: max(var(--sp-sm), env(safe-area-inset-left));
    padding-right: max(var(--sp-sm), env(safe-area-inset-right));
    gap: 6px;
    min-height: 48px;
    height: auto;
  }

  .header .select-wrap {
    flex: 1 1 0;
    min-width: 0;
  }

  .header .select-wrap select {
    font-size: 16px; /* Must be >=16px to prevent iOS Safari auto-zoom */
  }

  .header__title {
    display: none;
  }

  .subtitles {
    padding: var(--sp-md);
    padding-left: max(var(--sp-md), env(safe-area-inset-left));
    padding-right: max(var(--sp-md), env(safe-area-inset-right));
    padding-bottom: 80px; /* space for mobile bottom sheet bar */
  }

  .bottombar {
    bottom: 0;
    left: 0;
    right: 0;
    transform: none;
    width: 100%;
    max-width: 100vw;
    border-radius: var(--radius-xl) var(--radius-xl) 0 0; /* Bottom sheet look */
    border: none;
    border-top: 1px solid var(--border);
    padding: var(--sp-md) var(--sp-sm);
    padding-left: max(var(--sp-sm), env(safe-area-inset-left));
    padding-right: max(var(--sp-sm), env(safe-area-inset-right));
    padding-bottom: max(var(--sp-md), env(safe-area-inset-bottom));
    gap: var(--sp-sm);
    min-height: 60px;
    flex-wrap: nowrap;
    overflow: visible;
  }

  .bottombar__spacer {
    flex: 1;
    min-width: 0;
  }

  .bottombar .stats-text {
    display: none;
  }

  .audio-meter-wrap {
    max-width: 80px;
  }

  .settings-panel {
    top: auto;
    bottom: 0;
    right: 0;
    left: 0;
    width: 100%;
    max-width: 100vw;
    height: 80vh; /* Bottom sheet height */
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    border-left: none;
    border-top: 1px solid rgba(255, 255, 255, 0.05);
    transform: translateY(100%);
    padding: var(--sp-lg);
    padding-bottom: max(var(--sp-lg), env(safe-area-inset-bottom));
  }

  .settings-panel.open {
    transform: translateY(0);
  }
}

/* Responsive — very small phones (iPhone SE, Galaxy S series) */
@media (max-width: 380px) {
  .header {
    gap: 4px;
  }

  .bottombar .separator,
  .bottombar .stats-text {
    display: none;
  }

  .bottombar .source-badge {
    display: none;
  }
}

@media (min-width: 1024px) {
  .subtitles {
    max-width: 900px;
    margin: 0 auto;
    width: 100%;
  }
}