/* Tesla-sized touch-first layout (1920x1200 landscape primary, portrait fallback) */
:root {
  --bg: #0b0d12;
  --surface: #141923;
  --surface-2: #1c2331;
  --border: rgba(255, 255, 255, 0.11);
  --text: #f3f6fc;
  --muted: #98a4b8;
  --accent: #e50914;
  --accent-2: #3b82f6;
  --ok: #22c55e;
  --warn: #f59e0b;
  --hit: 64px;
  --radius: 16px;
  --topbar-h: 88px;
  --font: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 18px;
  overflow: hidden;
  touch-action: manipulation;
}

input {
  user-select: text;
}

button {
  font: inherit;
  color: inherit;
  border: 0;
  background: none;
  cursor: pointer;
}

button:active {
  transform: scale(0.96);
}

.app-shell {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  position: relative;
}

/* Top Bar */
.topbar {
  height: var(--topbar-h);
  min-height: var(--topbar-h);
  padding: 12px 24px;
  display: flex;
  align-items: center;
  gap: 20px;
  background: rgba(14, 18, 26, 0.96);
  border-bottom: 1px solid var(--border);
  z-index: 20;
}

.app-shell.in-player .topbar {
  display: none;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-shrink: 0;
}

.btn-icon {
  min-width: var(--hit);
  min-height: var(--hit);
  border-radius: var(--radius);
  background: var(--surface-2);
  border: 1px solid var(--border);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

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

.brand-title {
  font-weight: 800;
  letter-spacing: 0.08em;
  font-size: 20px;
}

.brand-pill {
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(229, 9, 20, 0.2);
  border: 1px solid rgba(229, 9, 20, 0.5);
  color: #ff6b72;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.06em;
}

.topbar-center {
  flex: 1;
  min-width: 0;
}

.search-form {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  max-width: 960px;
  margin: 0 auto;
}

.search-input {
  flex: 1;
  min-width: 0;
  height: var(--hit);
  padding: 0 22px;
  border-radius: var(--radius);
  border: 2px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 20px;
  outline: none;
}

.search-input:focus {
  border-color: var(--accent-2);
}

.btn-clear {
  min-width: var(--hit);
  min-height: var(--hit);
  border-radius: var(--radius);
  background: var(--surface-2);
  font-size: 28px;
  line-height: 1;
}

.btn-primary {
  min-width: 96px;
  min-height: var(--hit);
  padding: 0 26px;
  border-radius: var(--radius);
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 20px;
}

.btn-ghost {
  min-width: var(--hit);
  min-height: var(--hit);
  padding: 0 18px;
  border-radius: var(--radius);
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-weight: 700;
  font-size: 15px;
  letter-spacing: 0.04em;
}

.btn-ghost[aria-pressed="true"] {
  background: rgba(59, 130, 246, 0.25);
  border-color: var(--accent-2);
  color: #93c5fd;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

.probe-badge {
  padding: 8px 14px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-family: var(--mono);
  font-size: 13px;
  color: var(--ok);
  white-space: nowrap;
}

/* Views */
.view {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.view[hidden] {
  display: none !important;
}

/* Hub View */
.view-hub {
  padding: 40px 48px 64px;
}

.hub-hero {
  margin-bottom: 32px;
}

.hub-heading {
  margin: 0 0 8px;
  font-size: 40px;
  font-weight: 800;
}

.hub-sub {
  margin: 0;
  color: var(--muted);
  font-size: 20px;
}

.hub-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 24px;
}

.hub-tile {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: space-between;
  min-height: 210px;
  padding: 28px;
  border-radius: 24px;
  background: linear-gradient(145deg, var(--surface-2), var(--surface));
  border: 2px solid var(--border);
  text-align: left;
}

.hub-tile.active {
  border-color: rgba(229, 9, 20, 0.55);
}

.hub-tile.disabled {
  opacity: 0.5;
}

.hub-tile-icon {
  width: 68px;
  height: 68px;
  border-radius: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 30px;
  font-weight: 800;
  margin-bottom: 18px;
}

.hub-tile-title {
  font-size: 26px;
  font-weight: 800;
  margin: 0 0 6px;
}

.hub-tile-desc {
  font-size: 16px;
  color: var(--muted);
  margin: 0;
}

/* YouTube Browse View */
.view-youtube {
  padding: 20px 32px 48px;
}

.quick-bar {
  display: flex;
  gap: 12px;
  overflow-x: auto;
  padding-bottom: 18px;
  scrollbar-width: none;
}

.quick-bar::-webkit-scrollbar {
  display: none;
}

.chip {
  flex-shrink: 0;
  min-height: var(--hit);
  padding: 0 24px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: 18px;
  font-weight: 600;
  white-space: nowrap;
}

.chip.selected {
  background: var(--text);
  color: var(--bg);
}

.status-banner {
  margin-bottom: 20px;
  padding: 18px 22px;
  border-radius: var(--radius);
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: 18px;
  color: var(--muted);
}

.status-banner.error {
  border-color: rgba(239, 68, 68, 0.6);
  color: #fca5a5;
}

.results-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 24px;
}

.video-card {
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  overflow: hidden;
  text-align: left;
  padding: 0;
  min-height: 290px;
}

.video-thumb-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  background: #000;
  overflow: hidden;
}

.video-thumb {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.video-duration {
  position: absolute;
  right: 10px;
  bottom: 10px;
  padding: 4px 10px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.84);
  font-family: var(--mono);
  font-size: 14px;
  font-weight: 700;
}

.video-info {
  padding: 16px 18px 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.video-title {
  font-size: 19px;
  font-weight: 700;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.video-meta {
  font-size: 15px;
  color: var(--muted);
}

/* Full-bleed Canvas Player */
.view-player {
  overflow: hidden;
  background: #000;
  position: relative;
}

.player-stage {
  position: relative;
  width: 100%;
  height: 100%;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

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

.subtitle-box {
  position: absolute;
  left: 50%;
  bottom: 132px;
  transform: translateX(-50%);
  max-width: 82%;
  padding: 10px 20px;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.78);
  color: #fff;
  font-size: 28px;
  font-weight: 600;
  line-height: 1.35;
  text-align: center;
  pointer-events: none;
  z-index: 6;
}

.stage-status {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  background: rgba(0, 0, 0, 0.55);
  z-index: 8;
  pointer-events: none;
}

.stage-status[hidden] {
  display: none !important;
}

.spinner {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: 5px solid rgba(255, 255, 255, 0.2);
  border-top-color: #fff;
  animation: spin 0.9s linear infinite;
}

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

.stage-status-text {
  font-size: 22px;
  font-weight: 600;
  max-width: 720px;
  text-align: center;
  padding: 0 24px;
}

.unlock-pill {
  position: absolute;
  top: 96px;
  left: 50%;
  transform: translateX(-50%);
  min-height: var(--hit);
  padding: 0 28px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 20px;
  font-weight: 800;
  z-index: 15;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
}

/* Player Overlay HUD */
.player-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 24px 32px 28px;
  background: linear-gradient(
    to bottom,
    rgba(0, 0, 0, 0.78) 0%,
    rgba(0, 0, 0, 0.12) 28%,
    rgba(0, 0, 0, 0.12) 68%,
    rgba(0, 0, 0, 0.85) 100%
  );
  z-index: 10;
  transition: opacity 0.22s ease;
}

.player-overlay.idle-hidden {
  opacity: 0;
  pointer-events: none;
}

.overlay-top {
  display: flex;
  align-items: center;
  gap: 20px;
}

.overlay-title-wrap {
  flex: 1;
  min-width: 0;
}

.overlay-title {
  font-size: 26px;
  font-weight: 800;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.overlay-meta {
  font-size: 16px;
  color: var(--muted);
  margin-top: 4px;
}

.overlay-top-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

.ctrl-btn {
  min-width: var(--hit);
  min-height: var(--hit);
  padding: 0 20px;
  border-radius: var(--radius);
  background: rgba(20, 25, 35, 0.82);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #fff;
  font-size: 19px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.ctrl-btn.active {
  background: var(--accent);
  border-color: var(--accent);
}

.overlay-center {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 44px;
}

.ctrl-big {
  min-width: 112px;
  min-height: 84px;
  border-radius: 24px;
  font-size: 24px;
}

.ctrl-play {
  min-width: 108px;
  min-height: 108px;
  border-radius: 50%;
  background: rgba(229, 9, 20, 0.92);
  border-color: rgba(255, 255, 255, 0.35);
}

.overlay-bottom {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.seek-row {
  display: flex;
  align-items: center;
  gap: 18px;
}

.time-label {
  font-family: var(--mono);
  font-size: 20px;
  font-weight: 700;
  min-width: 78px;
  text-align: center;
}

.seek-track-wrap {
  flex: 1;
  min-height: var(--hit);
  display: flex;
  align-items: center;
  padding: 0 10px;
  touch-action: none;
}

.seek-track {
  position: relative;
  width: 100%;
  height: 12px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.24);
}

.seek-buffer {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 0%;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.42);
}

.seek-fill {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 0%;
  border-radius: 999px;
  background: var(--accent);
}

.seek-thumb {
  position: absolute;
  top: 50%;
  left: 0%;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: #fff;
  transform: translate(-50%, -50%);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55);
}

/* Popover Menus */
.popover-menu {
  position: absolute;
  right: 32px;
  top: 104px;
  width: 320px;
  max-height: 60vh;
  overflow-y: auto;
  border-radius: 20px;
  background: rgba(16, 20, 29, 0.96);
  border: 1px solid rgba(255, 255, 255, 0.2);
  padding: 12px;
  z-index: 20;
  box-shadow: 0 18px 48px rgba(0, 0, 0, 0.65);
}

.popover-header {
  padding: 10px 14px 12px;
  font-size: 16px;
  font-weight: 800;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  border-bottom: 1px solid var(--border);
  margin-bottom: 8px;
}

.popover-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.popover-item {
  width: 100%;
  min-height: var(--hit);
  padding: 12px 16px;
  border-radius: 12px;
  background: var(--surface-2);
  text-align: left;
  font-size: 18px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.popover-item.selected {
  background: rgba(229, 9, 20, 0.28);
  border: 1px solid var(--accent);
}

/* ?debug=1 Telemetry Overlay */
.debug-hud {
  position: fixed;
  left: 16px;
  top: 16px;
  width: 390px;
  padding: 14px 16px;
  border-radius: 14px;
  background: rgba(6, 10, 18, 0.86);
  border: 1px solid rgba(34, 197, 94, 0.55);
  color: #e2e8f0;
  font-family: var(--mono);
  font-size: 13px;
  z-index: 90;
  pointer-events: none;
  backdrop-filter: blur(6px);
}

.debug-title {
  font-weight: 800;
  color: var(--ok);
  letter-spacing: 0.06em;
  margin-bottom: 8px;
  font-size: 12px;
}

.debug-grid {
  margin: 0;
  display: grid;
  grid-template-columns: 158px 1fr;
  gap: 4px 10px;
}

.debug-grid dt {
  color: var(--muted);
}

.debug-grid dd {
  margin: 0;
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Off-screen hidden video sink — NEVER enters visible viewport */
.offscreen-video-sink {
  position: fixed !important;
  left: -9999px !important;
  top: -9999px !important;
  width: 1px !important;
  height: 1px !important;
  opacity: 0.01 !important;
  pointer-events: none !important;
  z-index: -1 !important;
}

/* Responsive / Portrait Fallback */
@media (max-width: 1400px) {
  .results-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

@media (max-width: 1024px) {
  .topbar {
    flex-wrap: wrap;
    height: auto;
    padding: 12px 16px;
    gap: 12px;
  }
  .topbar-center {
    order: 3;
    flex-basis: 100%;
  }
  .results-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 16px;
  }
  .view-youtube {
    padding: 16px;
  }
  .view-hub {
    padding: 24px 16px;
  }
  .overlay-center {
    gap: 20px;
  }
  .subtitle-box {
    font-size: 20px;
    bottom: 112px;
  }
}

@media (max-width: 640px) {
  .results-grid {
    grid-template-columns: 1fr;
  }
  .brand-pill, .probe-badge {
    display: none;
  }
  .debug-hud {
    width: calc(100vw - 32px);
    font-size: 12px;
  }
  .player-overlay {
    padding: 16px;
  }
  .overlay-title {
    font-size: 18px;
  }
}
