:root {
  color-scheme: dark;
  --bg: #0e1116;
  --surface: #171b22;
  --surface-strong: #202631;
  --text: #f3f5f8;
  --muted: #aab3c0;
  --line: #2c3441;
  --accent: #f15a24;
  --accent-text: #fff7f2;
  --shadow: 0 24px 80px rgb(0 0 0 / 0.32);
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f7f7f4;
  --surface: #ffffff;
  --surface-strong: #e9eef2;
  --text: #161b22;
  --muted: #5d6672;
  --line: #d7dde4;
  --accent: #d9461e;
  --accent-text: #ffffff;
  --shadow: 0 24px 70px rgb(32 39 49 / 0.18);
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    color-scheme: light;
    --bg: #f7f7f4;
    --surface: #ffffff;
    --surface-strong: #e9eef2;
    --text: #161b22;
    --muted: #5d6672;
    --line: #d7dde4;
    --accent: #d9461e;
    --accent-text: #ffffff;
    --shadow: 0 24px 70px rgb(32 39 49 / 0.18);
  }
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
select {
  font: inherit;
}

a {
  color: inherit;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.app-shell {
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  overflow: hidden;
}

.app-shell.controls-hidden .topbar,
.app-shell.controls-hidden .info-panel {
  opacity: 0;
  pointer-events: none;
}

.app-shell:fullscreen,
.app-shell:-webkit-full-screen {
  width: 100vw;
  height: 100vh;
  background: var(--bg);
}

.app-shell:fullscreen .library,
.app-shell:-webkit-full-screen .library {
  display: none;
}

.topbar {
  display: grid;
  grid-template-columns: minmax(220px, 1fr) auto;
  gap: 12px;
  align-items: center;
  padding: max(12px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) 12px max(14px, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  border-bottom: 1px solid var(--line);
  z-index: 20;
  backdrop-filter: blur(16px);
  transition: opacity 180ms ease;
}

.subreddit-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
}

input,
.select-control {
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-strong);
  color: var(--text);
  padding: 0 12px;
  outline: none;
}

input:focus,
.select-control:focus,
button:focus-visible,
a:focus-visible {
  outline: 2px solid color-mix(in srgb, var(--accent) 70%, white);
  outline-offset: 2px;
}

.toolbar,
.library-actions,
.panel-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.primary-button,
.secondary-button,
.icon-button {
  border: 1px solid transparent;
  border-radius: 8px;
  cursor: pointer;
  min-height: 42px;
  transition: transform 120ms ease, border-color 120ms ease, background 120ms ease;
}

.primary-button {
  background: var(--accent);
  color: var(--accent-text);
  padding: 0 18px;
  font-weight: 800;
}

.secondary-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  background: var(--surface-strong);
  border-color: var(--line);
  color: var(--text);
  padding: 0 14px;
  font-weight: 700;
}

.icon-button {
  width: 42px;
  display: inline-grid;
  place-items: center;
  background: var(--surface-strong);
  border-color: var(--line);
  color: var(--text);
  font-size: 26px;
  line-height: 1;
}

.speaker-icon,
.speaker-icon svg,
.fullscreen-icon,
.fullscreen-icon svg,
.control-icon,
.control-icon svg {
  width: 22px;
  height: 22px;
  display: block;
}

.speaker-icon svg,
.fullscreen-icon svg,
.control-icon svg {
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-button:hover,
.secondary-button:hover,
.primary-button:hover {
  transform: translateY(-1px);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.48;
}

.icon-button.is-active,
.secondary-button.is-active {
  border-color: color-mix(in srgb, var(--accent) 60%, var(--line));
  color: var(--accent-text);
  background: color-mix(in srgb, var(--accent) 72%, var(--surface-strong));
}

.favorite-button.is-active .control-icon svg {
  fill: currentColor;
}

.viewer {
  min-height: 0;
  overflow: hidden;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  outline: none;
}

.stage {
  height: 100%;
  min-height: 0;
  position: relative;
  overflow: hidden;
  background:
    radial-gradient(circle at 24% 18%, color-mix(in srgb, var(--accent) 16%, transparent), transparent 26rem),
    linear-gradient(135deg, color-mix(in srgb, var(--surface-strong) 70%, var(--bg)), var(--bg));
  touch-action: pan-y;
}

.media-host {
  position: absolute;
  inset: 0;
  min-width: 0;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.media-host img,
.media-host video {
  display: block;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  box-shadow: var(--shadow);
  user-select: none;
  -webkit-user-drag: none;
}

.media-host video {
  width: 100%;
  height: 100%;
}

.media-host video,
.media-host iframe {
  background: #000;
}

.media-host iframe {
  width: min(100%, 1200px);
  height: 100%;
  max-width: 1200px;
  max-height: 100%;
  border: 0;
  box-shadow: var(--shadow);
}

.media-error,
.empty-state,
.loading-state {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 12px;
  padding: 28px;
  text-align: center;
  z-index: 2;
}

.empty-state h1 {
  margin: 0;
  font-size: clamp(2rem, 8vw, 5rem);
}

.empty-state p,
.media-error p,
.loading-state {
  color: var(--muted);
  margin: 0;
}

.media-error {
  background: color-mix(in srgb, var(--bg) 78%, transparent);
}

.spinner {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 4px solid var(--line);
  border-top-color: var(--accent);
  animation: spin 800ms linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(1turn);
  }
}

.info-panel {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 16px;
  align-items: center;
  padding: 14px max(14px, env(safe-area-inset-right)) 14px max(14px, env(safe-area-inset-left));
  background: var(--surface);
  border-top: 1px solid var(--line);
  transition: opacity 180ms ease;
}

.favorite-button {
  min-width: 42px;
  padding: 0 10px;
}

.auth-button {
  white-space: nowrap;
}

.post-title {
  display: block;
  font-weight: 850;
  text-decoration: none;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.meta-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
  color: var(--muted);
  font-size: 0.92rem;
  margin-top: 4px;
}

.library {
  min-height: 0;
  border-top: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface) 88%, var(--bg));
  padding: 12px max(14px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
}

.library-summary {
  display: none;
}

.library-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 10px;
}

.library h2 {
  font-size: 1rem;
  margin: 0;
}

.saved-lists {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 2px;
}

.list-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  white-space: nowrap;
  min-height: 38px;
  padding: 0 8px 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-strong);
  color: var(--text);
}

.chip-load,
.chip-delete {
  border: 0;
  background: transparent;
  color: inherit;
  cursor: pointer;
  padding: 6px;
}

.chip-load {
  font-weight: 800;
}

.chip-delete {
  color: var(--muted);
  font-size: 18px;
}

@media (max-width: 780px) {
  .app-shell {
    grid-template-rows: auto minmax(0, 1fr) auto;
  }

  .topbar {
    grid-template-columns: 1fr;
    gap: 6px;
    padding: max(6px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) 6px max(8px, env(safe-area-inset-left));
  }

  .subreddit-form {
    gap: 6px;
  }

  input,
  .select-control,
  .primary-button,
  .secondary-button,
  .icon-button {
    min-height: 34px;
    border-radius: 7px;
  }

  input,
  .select-control {
    padding: 0 10px;
  }

  .primary-button,
  .secondary-button {
    padding: 0 10px;
    font-size: 0.9rem;
  }

  .icon-button {
    width: 36px;
    font-size: 22px;
  }

  .toolbar {
    display: grid;
    grid-template-columns: repeat(5, 36px) minmax(0, 1fr) minmax(0, 1fr) auto;
    gap: 6px;
  }

  .viewer {
    min-height: 0;
  }

  .media-host {
    min-height: 0;
  }

  .info-panel,
  .library-header {
    grid-template-columns: 1fr;
    display: grid;
  }

  .info-panel {
    gap: 8px;
    padding: 8px max(8px, env(safe-area-inset-right)) 8px max(8px, env(safe-area-inset-left));
  }

  .post-title {
    font-size: 0.9rem;
    white-space: normal;
    display: -webkit-box;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical;
  }

  .meta-row {
    gap: 6px 10px;
    font-size: 0.78rem;
    margin-top: 2px;
  }

  .panel-actions {
    justify-content: end;
  }

  .panel-actions .secondary-button {
    flex: 0 0 auto;
    min-width: 64px;
  }

  .panel-actions .favorite-button {
    min-width: 36px;
    width: 36px;
  }

  .panel-actions,
  .library-actions {
    width: 100%;
  }

  .library-actions > * {
    flex: 1;
  }

  .library {
    padding: 0 max(8px, env(safe-area-inset-right)) max(6px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  }

  .library-summary {
    min-height: 34px;
    display: flex;
    align-items: center;
    cursor: pointer;
    color: var(--text);
    font-weight: 800;
    list-style: none;
  }

  .library-summary::-webkit-details-marker {
    display: none;
  }

  .library-summary::after {
    content: "+";
    margin-left: auto;
    color: var(--muted);
  }

  .library[open] .library-summary::after {
    content: "-";
  }

  .library-content {
    max-height: 34vh;
    overflow: auto;
    padding-bottom: 6px;
  }

  .library-header {
    gap: 8px;
    margin-bottom: 8px;
  }

  .library h2 {
    display: none;
  }

  .saved-lists {
    gap: 6px;
  }

  .list-chip {
    min-height: 34px;
  }
}
