:root {
  color-scheme: dark;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Arial, sans-serif;
  background: #0f1115;
  color: #f5f7fb;
  --clr-pair:     #78d3ff;
  --clr-player:   #c084fc;
  --clr-device:   #fb923c;
  --clr-channels: #4ade80;
  --clr-command:  #f472b6;
  --clr-log:      #94a3b8;
  --nav-h:        2.75rem;
  overflow-x: hidden;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  overflow-x: hidden;
  background: radial-gradient(circle at top, #1b2230, #0f1115 45%);
}

button,
input,
select {
  font: inherit;
}

button,
.button-like {
  border: 0;
  border-radius: 10px;
  background: #78d3ff;
  color: #081019;
  padding: 0.7rem 1rem;
  cursor: pointer;
  font-weight: 700;
  touch-action: manipulation;
}

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

.secondary-button {
  background: #253042;
  color: #f5f7fb;
}

input,
select {
  width: 100%;
  border: 1px solid #34445f;
  border-radius: 10px;
  padding: 0.7rem 0.8rem;
  background: #111824;
  color: inherit;
}

code {
  word-break: break-all;
}

.page {
  max-width: 1180px;
  margin: 0 auto;
  padding: 2rem 1rem 4rem;
}

.page > section {
  margin-bottom: 1.5rem;
}

@media (hover: none) and (pointer: coarse), (max-width: 900px) {
  .page {
    max-width: none;
  }
}

.hero,
.grid,
.pairing-layout,
.inline-form,
.status-row,
.channel-grid,
.slot-header,
.slot-actions,
.graph-header,
.graph-legend {
  display: grid;
  gap: 1rem;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

.hero {
  grid-template-columns: 1.8fr 1fr;
  align-items: start;
  margin-bottom: 1.5rem;
}

.eyebrow {
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #78d3ff;
  margin: 0 0 0.6rem;
}

.subtitle {
  max-width: 68ch;
  color: #b7c6d8;
}

.card,
.status-card,
.slot-card {
  background: rgba(17, 24, 36, 0.92);
  border: 1px solid #243248;
  border-radius: 16px;
  padding: 1.2rem;
  box-shadow: 0 18px 48px rgba(0, 0, 0, 0.22);
}

.status-card {
  display: grid;
  gap: 0.75rem;
}

.status-row {
  grid-template-columns: 80px 1fr;
  align-items: center;
}

.player-grid,
.player-stats,
.slot-grid,
.slot-stats,
.slot-list,
.split-channel-grid,
.player-loop-controls,
.player-loop-status,
.media-library-list {
  display: grid;
  gap: 1rem;
}

.player-grid {
  grid-template-columns: repeat(4, minmax(0, 1fr));
  margin-bottom: 1rem;
}

.player-grid--video {
  grid-template-columns: minmax(0, 420px) minmax(0, 1fr);
}

@media (max-width: 700px) {
  .player-grid--video {
    grid-template-columns: minmax(0, 1fr);
  }
}

.url-load-row {
  display: flex;
  gap: 0.5rem;
  align-items: stretch;
}

.url-load-row input {
  flex: 1;
  min-width: 0;
}

.url-load-row button {
  flex-shrink: 0;
  white-space: nowrap;
}

.media-library-playlist-row {
  display: grid;
  grid-template-columns: 1fr minmax(0, 280px);
  gap: 1rem;
  align-items: end;
}

@media (max-width: 700px) {
  .media-library-playlist-row {
    grid-template-columns: 1fr;
  }
}

.field.grow {
  flex: 1;
  min-width: 0;
}

.player-actions {
  margin-bottom: 1rem;
  align-items: center;
}

.player-more-options {
  position: relative;
}

.player-more-summary {
  padding: 0.7rem 1rem;
  border-radius: 10px;
  background: #253042;
  color: #f5f7fb;
  font-weight: 700;
  cursor: pointer;
  list-style: none;
  white-space: nowrap;
  user-select: none;
}

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

.player-more-menu {
  position: absolute;
  top: calc(100% + 0.4rem);
  left: 0;
  z-index: 30;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  padding: 0.6rem;
  border-radius: 12px;
  border: 1px solid #34445f;
  background: rgba(9, 17, 27, 0.98);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.4);
  min-width: 200px;
}

.player-more-menu button {
  width: 100%;
  text-align: left;
}

@media (max-width: 680px) {
  .player-more-options {
    width: 100%;
  }

  .player-more-summary {
    width: 100%;
    display: block;
    text-align: center;
  }

  .player-more-menu {
    position: static;
    box-shadow: none;
    border: none;
    padding: 0.4rem 0 0;
    background: transparent;
  }
}

.player-video {
  width: 100%;
  max-height: 520px;
  border-radius: 14px;
  background: #05080f;
  border: 1px solid #243248;
  margin-bottom: 1rem;
}

.player-stats {
  grid-template-columns: repeat(3, minmax(0, 1fr));
  margin-bottom: 1rem;
}

.player-loop-panel {
  display: grid;
  gap: 1rem;
  margin-bottom: 1rem;
  padding: 1rem;
  border-radius: 14px;
  background: #0b1018;
  border: 1px solid #243248;
}

.player-loop-controls {
  grid-template-columns: minmax(180px, 220px) repeat(3, auto);
  align-items: end;
}

.player-loop-status {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.player-stats strong,
.slot-stats strong {
  font-size: 1rem;
}

.slot-list {
  margin-top: 1rem;
}

.slot-card {
  display: grid;
  gap: 1rem;
}

.slot-advanced {
  border: 1px solid #243248;
  border-radius: 12px;
  background: #0b1018;
}

.slot-advanced-summary {
  padding: 0.7rem 0.9rem;
  cursor: pointer;
  color: #9eb0c8;
  font-size: 0.92rem;
  font-weight: 600;
  list-style: none;
  user-select: none;
}

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

.slot-advanced-summary::before {
  content: "▶ ";
  font-size: 0.7rem;
  vertical-align: middle;
}

.slot-advanced[open] .slot-advanced-summary::before {
  content: "▼ ";
}

.slot-advanced-grid {
  padding: 0 0.9rem 0.9rem;
}

.slot-header {
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: start;
}

.slot-header h3 {
  margin: 0;
}

.slot-subtitle {
  margin: 0.3rem 0 0;
  color: #9eb0c8;
}

.slot-actions {
  grid-auto-flow: column;
  justify-content: start;
  align-items: start;
}

.slot-grid {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.slot-stats {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.graph-card {
  padding: 0.85rem 1rem 1rem;
  border-radius: 14px;
  background: #0b1018;
  border: 1px solid #243248;
}

.media-library-card {
  margin-bottom: 1rem;
  padding: 0.85rem 1rem 1rem;
  border-radius: 14px;
  background: #0b1018;
  border: 1px solid #243248;
}

.media-library-card[open] {
  display: grid;
  gap: 1rem;
}

.media-library-summary {
  list-style: none;
  cursor: pointer;
}

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

.media-library-summary::marker {
  display: none;
}

.media-library-header {
  display: flex;
  gap: 1rem;
  align-items: start;
  justify-content: space-between;
  flex-wrap: wrap;
}

.media-entry {
  display: grid;
  gap: 0.8rem;
  padding: 0.9rem 1rem;
  border: 1px solid #243248;
  border-radius: 12px;
  background: rgba(17, 24, 36, 0.92);
}

.media-entry-header {
  display: flex;
  gap: 1rem;
  align-items: start;
  justify-content: space-between;
  flex-wrap: wrap;
}

.media-entry-title {
  margin: 0;
}

.media-entry-subtitle {
  margin: 0.25rem 0 0;
  color: #9eb0c8;
}

.script-chip-list {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.script-chip {
  display: inline-flex;
  padding: 0.35rem 0.6rem;
  border-radius: 999px;
  background: #182333;
  color: #dce9ff;
  font-size: 0.88rem;
}

.split-config {
  display: grid;
  gap: 1rem;
  padding: 1rem;
  border-radius: 12px;
  border: 1px solid #243248;
  background: #0b1018;
}

.split-channel-grid {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.split-segment-card {
  display: grid;
  gap: 0.6rem;
  padding: 0.9rem;
  border-radius: 12px;
  border: 1px solid #243248;
  background: rgba(17, 24, 36, 0.92);
}

.graph-header,
.graph-legend {
  display: flex;
  gap: 1rem;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
}

.graph-legend {
  justify-content: flex-start;
}

.legend-item {
  display: inline-flex;
  gap: 0.45rem;
  align-items: center;
  color: #c8d8f0;
  font-size: 0.9rem;
}

.legend-swatch {
  display: inline-block;
  width: 14px;
  height: 3px;
  border-radius: 999px;
}

.legend-swatch.expected {
  background: #5ae4ff;
}

.legend-swatch.response {
  background: #b38cff;
}

.legend-swatch.output {
  background: #ffe36a;
}

.legend-swatch.output-a {
  background: #ffb84d;
}

.legend-swatch.output-b {
  background: #6dffb0;
}

.legend-swatch.playhead {
  width: 3px;
  height: 14px;
  background: #8fff93;
}

.player-graph {
  width: 100%;
  height: 180px;
  margin-top: 0.85rem;
  display: block;
  border-radius: 10px;
  background: linear-gradient(180deg, rgba(90, 228, 255, 0.08), rgba(17, 24, 36, 0.2));
}

.checkbox-field {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  padding: 0.75rem 0.9rem;
  background: #111824;
  border: 1px solid #34445f;
  border-radius: 10px;
}

.checkbox-field input {
  width: auto;
  margin: 0;
}

.checkbox-field span {
  color: #dce9ff;
}

.pairing-layout {
  grid-template-columns: 1.2fr 1fr;
  margin-bottom: 1.5rem;
}

.pairing-form {
  display: grid;
  gap: 0.75rem;
  align-content: start;
}

.qr-panel {
  display: grid;
  gap: 1rem;
  justify-items: center;
  align-content: start;
}

#qr-image {
  width: min(280px, 100%);
  background: white;
  border-radius: 12px;
  padding: 0.7rem;
}

.field {
  display: grid;
  gap: 0.45rem;
}

.label-with-help,
.heading-with-help {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  flex-wrap: wrap;
}

.heading-with-help {
  margin: 0 0 0.83rem;
}

.help-popover {
  position: relative;
  display: inline-flex;
  align-items: center;
}

.help-trigger {
  width: 1.4rem;
  height: 1.4rem;
  padding: 0;
  border-radius: 999px;
  background: #253042;
  color: #dce9ff;
  font-size: 0.8rem;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.help-trigger:hover,
.help-trigger:focus-visible {
  background: #34445f;
  outline: none;
}

.help-panel {
  position: absolute;
  top: calc(100% + 0.5rem);
  left: 0;
  width: min(320px, 72vw);
  padding: 0.8rem 0.9rem;
  border-radius: 12px;
  border: 1px solid #34445f;
  background: rgba(9, 17, 27, 0.97);
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.3);
  color: #dce9ff;
  font-size: 0.9rem;
  line-height: 1.45;
  z-index: 20;
  opacity: 0;
  pointer-events: none;
  transform: translateY(-4px);
  transition: opacity 120ms ease, transform 120ms ease;
}

.help-popover:hover .help-panel,
.help-popover:focus-within .help-panel {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}

.help-panel code,
.help-panel strong {
  color: #ffffff;
}

.field span,
.metric-label,
.hint {
  color: #9eb0c8;
  font-size: 0.92rem;
}

.info-collapsible {
  margin: 0 0 1rem;
  padding: 0.95rem 1rem;
  border: 1px solid #243248;
  border-radius: 14px;
  background: #0b1018;
}

.info-collapsible summary {
  cursor: pointer;
  color: #dce9ff;
  font-weight: 700;
}

.info-collapsible-copy {
  margin: 0.75rem 0 0;
}

.hint code {
  color: #ffffff;
}

.warning {
  margin: 0;
  color: #ffd38d;
  background: rgba(132, 78, 21, 0.18);
  border: 1px solid rgba(255, 196, 120, 0.28);
  border-radius: 10px;
  padding: 0.8rem 0.9rem;
}

.suggested-urls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
}

.suggested-url-button {
  background: #182333;
  color: #dce9ff;
  font-weight: 600;
}

.compact {
  min-width: 0;
}

.grow {
  flex: 1;
}

.grid {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  margin-bottom: 1.5rem;
}

.inline-form {
  grid-auto-flow: column;
  align-items: end;
  justify-content: start;
}

.card > .inline-form + .inline-form,
.card > .inline-form + button:not(.help-trigger) {
  margin-top: 0.75rem;
}

.strength-setter {
  grid-template-columns: minmax(140px, 180px) repeat(4, auto);
}

.channel-grid {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.channel-grid strong {
  font-size: 1.8rem;
}

.event-log {
  min-height: 220px;
  max-height: 320px;
  overflow: auto;
  white-space: pre-wrap;
  margin: 0;
  padding: 1rem;
  border-radius: 12px;
  background: #0b1018;
  border: 1px solid #1d2838;
  color: #b5ffe0;
}

@media (max-width: 900px) {
  .hero,
  .pairing-layout,
  .grid,
  .player-grid,
  .player-loop-controls,
  .player-loop-status,
  .slot-header,
  .split-channel-grid {
    grid-template-columns: 1fr;
  }

  .slot-grid,
  .slot-stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .inline-form,
  .slot-actions {
    grid-auto-flow: row;
    grid-template-columns: 1fr;
  }

  .strength-setter {
    grid-template-columns: 1fr;
  }

  .player-stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 540px) {
  .slot-grid,
  .slot-stats {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 680px) {
  .page {
    padding: 1rem 0.75rem 3.5rem;
  }

  .card,
  .status-card,
  .slot-card {
    padding: 0.95rem;
  }

  .actions,
  .inline-form,
  .slot-actions {
    gap: 0.75rem;
  }

  .actions > button,
  .slot-actions > button,
  .media-entry-header > button,
  .card > button:not(.help-trigger) {
    width: 100%;
  }

  .player-video {
    max-height: 42vh;
  }

  .player-stats,
  .channel-grid {
    grid-template-columns: 1fr;
  }

  .channel-grid strong {
    font-size: 1.45rem;
  }

  .event-log {
    min-height: 160px;
    max-height: 220px;
  }
}

/* ── Focus-visible accessibility ────────────────────────────── */
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 2px solid #78d3ff;
  outline-offset: 2px;
}

button:focus-visible {
  outline: 2px solid #78d3ff;
  outline-offset: 3px;
}

/* ── Hover feedback on interactive cards ─────────────────────── */
@media (hover: hover) {
  .slot-card:hover {
    border-color: #344a66;
  }

  .media-entry:hover {
    border-color: #344a66;
  }
}

/* ── Touch-target enlargement ────────────────────────────────── */
@media (pointer: coarse) {
  .help-trigger {
    width: 2.2rem;
    height: 2.2rem;
    font-size: 0.85rem;
  }

  button,
  .button-like {
    padding: 0.8rem 1rem;
  }
}

/* ── WebSocket connection status colors ─────────────────────── */
.status--connected {
  color: #6dffb0;
}

.status--connecting {
  color: #ffe36a;
}

.status--error {
  color: #ff8080;
}

/* ── Sticky mini-status bar (touch devices only) ─────────────── */
@media (pointer: coarse) {
  .sticky-status {
    position: fixed;
    bottom: var(--nav-h);
    left: 0;
    right: 0;
    z-index: 100;
    display: flex;
    gap: 1rem;
    align-items: center;
    justify-content: space-between;
    padding: 0.55rem 1rem;
    background: rgba(9, 17, 27, 0.95);
    border-top: 1px solid #243248;
    backdrop-filter: blur(8px);
    font-size: 0.88rem;
  }

  .sticky-status-dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #9eb0c8;
    flex-shrink: 0;
    margin-right: 0.3rem;
  }

  .sticky-status-dot--connected {
    background: #6dffb0;
  }

  .sticky-status-dot--connecting {
    background: #ffe36a;
  }

  .sticky-status-dot--error {
    background: #ff8080;
  }

  .sticky-status-left {
    display: flex;
    align-items: center;
  }

  .sticky-status-outputs {
    display: flex;
    gap: 0.75rem;
    color: #9eb0c8;
  }

  .sticky-status-outputs strong {
    color: #f5f7fb;
  }

  /* room for sticky bar + nav */
  body {
    padding-bottom: calc(var(--nav-h) + 2.8rem);
  }
}

/* ── Section color tints ─────────────────────────────────────── */
#section-pairing                { border-top: 3px solid var(--clr-pair); }
#section-player                 { border-top: 3px solid var(--clr-player); }
#section-device  > .card        { border-top: 3px solid var(--clr-device); }
#section-channels > .card       { border-top: 3px solid var(--clr-channels); }
#section-command                { border-top: 3px solid var(--clr-command); }
#section-log                    { border-top: 3px solid var(--clr-log); }

/* ── Floating section anchor nav ─────────────────────────────── */
.section-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 90;
  display: flex;
  overflow-x: auto;
  scrollbar-width: none;
  background: rgba(9, 17, 27, 0.95);
  border-top: 1px solid #243248;
  backdrop-filter: blur(8px);
  height: var(--nav-h);
}

.section-nav::-webkit-scrollbar {
  display: none;
}

.section-nav-link {
  flex: 1;
  min-width: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 0.4rem;
  font-size: 0.78rem;
  font-weight: 500;
  color: #7a8fa8;
  text-decoration: none;
  border-top: 2px solid transparent;
  white-space: nowrap;
  transition: color 0.15s, border-color 0.15s;
}

.section-nav-link:hover {
  color: #f5f7fb;
}

.section-nav-link[data-section="pairing"].active    { color: var(--clr-pair);     border-top-color: var(--clr-pair); }
.section-nav-link[data-section="player"].active     { color: var(--clr-player);   border-top-color: var(--clr-player); }
.section-nav-link[data-section="device"].active     { color: var(--clr-device);   border-top-color: var(--clr-device); }
.section-nav-link[data-section="channels"].active   { color: var(--clr-channels); border-top-color: var(--clr-channels); }
.section-nav-link[data-section="command"].active    { color: var(--clr-command);  border-top-color: var(--clr-command); }
.section-nav-link[data-section="log"].active        { color: var(--clr-log);      border-top-color: var(--clr-log); }

/* ── Button toast notification ───────────────────────────────── */
#app-toast {
  position: fixed;
  bottom: calc(var(--nav-h) + 0.5rem);
  left: 50%;
  transform: translateX(-50%) translateY(6px);
  z-index: 95;
  max-width: min(320px, 88vw);
  padding: 0.45rem 1.1rem;
  border-radius: 999px;
  background: rgba(22, 36, 54, 0.96);
  border: 1px solid #34445f;
  color: #dce9ff;
  font-size: 0.85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.18s, transform 0.18s;
}

#app-toast[data-visible] {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

@media (pointer: coarse) {
  #app-toast {
    bottom: calc(var(--nav-h) + 3.2rem);
  }
}
