:root {
  --bg: #0b0f14;
  --panel: #141a22;
  --panel-2: #1b232e;
  --logo-bg: #b6c1cf;
  --name-bg: #9dabbb;
  --name-text: #10161d;
  --border: #232d3a;
  --text: #e6edf3;
  --muted: #8b98a8;
  --accent: #2f81f7;
  --accent-2: #e63946;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --control-gap: 28px;
  --btn: 88px;
  --btn-main: 116px;
  --title-offset: calc(50% + var(--btn-main) / 2 + 18px);
}

/* Auto night theme (18:00-06:00): darker cards to reduce glare on the road */

html.theme-night {
  --bg: #030405;
  --panel: #080b0f;
  --panel-2: #0d1218;
  --logo-bg: #545e6a;
  --name-bg: #444d58;
  --name-text: #c7d1dc;
  --border: #181f27;
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

html::-webkit-scrollbar,
body::-webkit-scrollbar {
  display: none;
  width: 0;
  height: 0;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family:
    'Noto Sans Thai',
    system-ui,
    -apple-system,
    'Segoe UI',
    sans-serif;
  line-height: 1.5;
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
}

img {
  display: block;
  max-width: 100%;
}

.muted {
  color: var(--muted);
}

.small {
  font-size: 0.82rem;
}

/* List page */

.content {
  padding: calc(16px + var(--safe-top)) 16px calc(24px + var(--safe-bottom));
}

.status {
  padding: 40px 0;
  text-align: center;
  color: var(--muted);
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 12px;
}

.card {
  position: relative;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  aspect-ratio: 1 / 1;
  color: inherit;
  text-decoration: none;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  transition:
    transform 0.12s ease,
    border-color 0.12s ease;
}

.card.is-pinned {
  border-color: var(--accent);
}

.pin-badge {
  position: absolute;
  top: 6px;
  right: 6px;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  color: #ffd24a;
  background: rgba(0, 0, 0, 0.5);
  border-radius: 50%;
  pointer-events: none;
}

.pin-badge svg {
  width: 15px;
  height: 15px;
  fill: currentColor;
}

.card:active {
  transform: scale(0.97);
}

@media (hover: hover) {
  .card:hover {
    border-color: var(--accent);
  }
}

.thumb {
  position: relative;
  display: flex;
  flex: 1 1 auto;
  align-items: center;
  justify-content: center;
  min-height: 0;
  padding: 14px;
  overflow: hidden;
  background: var(--logo-bg);
}

.thumb img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: center;
}

.thumb-fallback {
  font-size: 2.2rem;
  font-weight: 700;
  color: #fff;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
}

.card-title {
  flex: 0 0 auto;
  padding: 8px 10px 10px;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--name-text);
  text-align: center;
  background: var(--name-bg);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.app-footer {
  padding: 4px 16px calc(16px + var(--safe-bottom));
  font-size: 0.75rem;
  color: var(--muted);
  text-align: center;
}

/* Player overlay */

.player {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: #000;
}

.player[hidden] {
  display: none;
}

.player-stage {
  position: relative;
  width: 100%;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  background: #000;
}

.player-stage video {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #000;
}

.loader {
  position: absolute;
  left: 0;
  right: 0;
  bottom: min(calc(var(--title-offset) + 120px), calc(100% - 60px));
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  pointer-events: none;
}

.loader.is-center {
  top: 0;
  bottom: 0;
  justify-content: center;
}

.loader[hidden] {
  display: none;
}

.loader-spinner {
  width: 46px;
  height: 46px;
  border: 3px solid rgba(230, 57, 70, 0.25);
  border-top-color: #e63946;
  border-radius: 50%;
  animation: loader-spin 0.8s linear infinite;
}

@keyframes loader-spin {
  to {
    transform: rotate(360deg);
  }
}

.unmute {
  position: absolute;
  left: 50%;
  bottom: calc(80px + var(--safe-bottom));
  transform: translateX(-50%);
  padding: 9px 18px;
  font-size: 0.9rem;
  color: #fff;
  background: rgba(0, 0, 0, 0.65);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 999px;
  cursor: pointer;
}

/* Controls overlay */

.controls {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 28px;
  pointer-events: none;
  background: rgba(0, 0, 0, 0.75);
  transition: opacity 0.2s ease;
}

.controls[hidden] {
  display: none;
}

.controls-top {
  position: absolute;
  left: 0;
  right: 0;
  bottom: var(--title-offset);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 calc(16px + var(--safe-right)) 0 calc(16px + var(--safe-left));
}

.controls-logo {
  flex: 0 0 auto;
  height: 46px;
  max-width: 160px;
  object-fit: contain;
  filter: drop-shadow(0 1px 5px rgba(0, 0, 0, 0.65));
}

.controls-title {
  max-width: 90%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 1.15rem;
  font-weight: 600;
  color: #fff;
  text-align: center;
  text-shadow: 0 1px 6px rgba(0, 0, 0, 0.6);
}

.controls-center {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--control-gap);
  padding: 0 16px;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  pointer-events: auto;
  padding: 8px 12px;
  color: #fff;
  background: rgba(0, 0, 0, 0.45);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 999px;
  cursor: pointer;
}

.icon-btn:active {
  background: rgba(0, 0, 0, 0.7);
}

.icon-btn-lg {
  width: var(--btn);
  height: var(--btn);
  padding: 0;
  font-size: calc(var(--btn) * 0.42);
  background: rgba(0, 0, 0, 0.75);
  border-color: rgba(255, 255, 255, 0.22);
}

.control {
  position: relative;
  display: flex;
  justify-content: center;
}

.control-label {
  position: absolute;
  top: calc(100% + 10px);
  left: 50%;
  transform: translateX(-50%);
  font-size: 0.95rem;
  font-weight: 600;
  line-height: 1;
  letter-spacing: 0.02em;
  color: #fff;
  white-space: nowrap;
  text-shadow: 0 1px 6px rgba(0, 0, 0, 0.6);
}

.icon-btn-xl {
  width: var(--btn-main);
  height: var(--btn-main);
  font-size: calc(var(--btn-main) * 0.42);
}

.icon-btn-round {
  width: 56px;
  height: 56px;
  padding: 0;
}

.icon-back {
  display: block;
  width: 1em;
  height: 1em;
  color: #fff;
}

.icon-close {
  position: relative;
  width: 16px;
  height: 16px;
}

.icon-close::before,
.icon-close::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  width: 100%;
  height: 2px;
  background: #fff;
}

.icon-close::before {
  transform: translateY(-50%) rotate(45deg);
}

.icon-close::after {
  transform: translateY(-50%) rotate(-45deg);
}

.icon-play {
  width: 0;
  height: 0;
  margin-left: 0.14em;
  border-style: solid;
  border-width: 0.38em 0 0.38em 0.62em;
  border-color: transparent transparent transparent #fff;
}

.icon-pause {
  width: 0.62em;
  height: 0.68em;
  background:
    linear-gradient(#fff, #fff) left center / 0.16em 100% no-repeat,
    linear-gradient(#fff, #fff) right center / 0.16em 100% no-repeat;
}

.icon-epg {
  display: block;
  width: 1em;
  height: 1em;
  color: #fff;
}

.icon-pin {
  display: block;
  width: 1em;
  height: 1em;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-linejoin: round;
}

.icon-btn.is-pinned .icon-pin {
  fill: #fff;
  stroke: #fff;
}

.icon-volume {
  display: block;
  width: 1em;
  height: 1em;
}

.icon-volume-off {
  display: none;
}

.icon-btn.is-muted .icon-volume-on {
  display: none;
}

.icon-btn.is-muted .icon-volume-off {
  display: block;
}

/* Muted indicator */

.mute-badge {
  position: absolute;
  top: calc(12px + var(--safe-top));
  left: calc(12px + var(--safe-left));
  z-index: 4;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 60px;
  height: 60px;
  color: #fff;
  background: rgba(0, 0, 0, 0.55);
  border-radius: 50%;
  pointer-events: none;
}

.mute-badge[hidden] {
  display: none;
}

.mute-badge svg {
  width: 36px;
  height: 36px;
  fill: currentColor;
}

.debug-res {
  position: absolute;
  left: calc(8px + var(--safe-left));
  bottom: calc(8px + var(--safe-bottom));
  z-index: 4;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 6px;
  font-size: 0.65rem;
  line-height: 1.2;
  color: #ffd400;
  background: rgba(0, 0, 0, 0.5);
  border-radius: 4px;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
}

.debug-res__pulse {
  width: 24px;
  height: 13px;
  flex: none;
}

.debug-res__pulse-base {
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  opacity: 0.35;
}

.debug-res__pulse-line {
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-dasharray: 22 78;
  animation: debug-res-beat 1.6s linear infinite;
}

@keyframes debug-res-beat {
  0% {
    stroke-dashoffset: 100;
  }

  100% {
    stroke-dashoffset: 0;
  }
}

.debug-res[hidden] {
  display: none;
}

/* EPG popup */

.epg-popup {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(0, 0, 0, 0.75);
}

.epg-popup[hidden] {
  display: none;
}

.epg-popup-panel {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 640px;
  max-height: 78%;
  padding-bottom: var(--safe-bottom);
  background: var(--panel);
  border-top-left-radius: var(--radius);
  border-top-right-radius: var(--radius);
  border: 1px solid var(--border);
  overflow: hidden;
}

@media (min-width: 700px) {
  .epg-popup {
    align-items: center;
  }

  .epg-popup-panel {
    border-radius: var(--radius);
    max-height: 80%;
  }
}

.epg-popup-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}

.epg-popup-title {
  flex: 1 1 auto;
  font-size: 1.05rem;
  font-weight: 700;
}

.epg-popup-head .icon-btn-round {
  width: 40px;
  height: 40px;
  background: transparent;
  border: none;
}

.epg-popup-list {
  overflow-y: auto;
  padding: 6px 14px 16px;
  -webkit-overflow-scrolling: touch;
}

.epg-popup-empty {
  padding: 24px 0;
  text-align: center;
}

.epg-item {
  position: relative;
  display: flex;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}

.epg-item:last-child {
  border-bottom: none;
}

.epg-item-time {
  flex: 0 0 88px;
  padding-top: 2px;
  font-size: 0.82rem;
  color: var(--muted);
  white-space: nowrap;
}

.epg-item-body {
  flex: 1 1 auto;
  min-width: 0;
}

.epg-item-body strong {
  font-size: 0.98rem;
}

.epg-item-body p {
  margin: 2px 0 0;
}

.epg-item.is-now .epg-item-time,
.epg-item.is-now .epg-item-body strong {
  color: var(--accent-2);
}

.epg-item-badge {
  flex: 0 0 auto;
  align-self: flex-start;
  margin-top: 2px;
  padding: 1px 8px;
  font-size: 0.68rem;
  color: #fff;
  background: var(--accent-2);
  border-radius: 999px;
  white-space: nowrap;
}

/* Fallback */

.fallback {
  margin-top: 24px;
  padding: 16px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  text-align: center;
}

.fallback-overlay {
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: 3;
  width: min(90%, 420px);
  margin: 0;
  transform: translate(-50%, -50%);
}

.fallback[hidden] {
  display: none;
}

.fallback-head {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-bottom: 10px;
}

.fallback-logo {
  width: 40px;
  height: 40px;
  object-fit: contain;
  border-radius: 8px;
  background: var(--panel-2);
}

.fallback-name {
  font-weight: 600;
  color: var(--text);
}

.fallback-msg {
  margin: 0 0 16px;
  padding: 12px 8px;
  color: var(--accent-2);
  line-height: 1.6;
}

.fallback-actions {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.link-btn {
  margin-top: 14px;
  padding: 6px;
  color: var(--muted);
  background: none;
  border: none;
  font-size: 0.9rem;
  text-decoration: underline;
  cursor: pointer;
}

.btn {
  font: inherit;
  padding: 10px 18px;
  color: #fff;
  background: var(--accent);
  border: none;
  border-radius: 999px;
  font-size: 0.95rem;
  cursor: pointer;
}

.btn-ghost {
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--border);
}

.btn:active {
  opacity: 0.85;
}

/* Pin confirm dialog */

.pin-confirm {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(0, 0, 0, 0.6);
}

.pin-confirm[hidden] {
  display: none;
}

.pin-confirm-panel {
  width: 100%;
  max-width: 360px;
  padding: 22px 20px 18px;
  text-align: center;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.pin-confirm-msg {
  margin: 0 0 18px;
  font-size: 1rem;
  line-height: 1.5;
}

.pin-confirm-actions {
  display: flex;
  gap: 10px;
}

.pin-confirm-actions .btn {
  flex: 1 1 0;
}

/* Landscape: size controls so seven buttons fit across the width */

@media (orientation: landscape) {
  :root {
    --control-gap: 12px;
    --btn: min(
      120px,
      34vh,
      calc((100vw - 32px - var(--safe-left) - var(--safe-right) - 6 * var(--control-gap)) / 7.3)
    );
    --btn-main: calc(var(--btn) * 1.3);
  }
}

/* Portrait phones: size controls so all five buttons fit across the width */

@media (orientation: portrait) and (max-width: 600px) {
  :root {
    --control-gap: 12px;
    --btn: min(
      88px,
      34vh,
      calc((100vw - 32px - var(--safe-left) - var(--safe-right) - 4 * var(--control-gap)) / 5.3)
    );
    --btn-main: calc(var(--btn) * 1.3);
  }
}

/* Tablet and desktop refinements */

@media (min-width: 700px) {
  .grid {
    grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
    gap: 16px;
  }
}

/* Small screens: fewer, larger cards so the channel label stays legible */

@media (max-width: 600px) {
  .grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
  }
}

/* Portrait phones: smaller, single-line card label */

@media (orientation: portrait) and (max-width: 600px) {
  .card-title {
    font-size: 0.8rem;
    line-height: 1.3;
  }
}

/* Static pages */

.doc {
  max-width: 760px;
  margin: 0 auto;
  line-height: 1.7;
}

.doc-back {
  display: inline-block;
  margin-bottom: 12px;
  color: var(--accent);
  text-decoration: none;
  font-size: 0.9rem;
}

.doc h1 {
  font-size: 1.6rem;
  margin: 8px 0 16px;
}

.doc h2 {
  font-size: 1.1rem;
  margin: 24px 0 8px;
}

.doc p {
  margin: 0 0 12px;
}

.doc ul {
  margin: 0 0 12px;
  padding-left: 20px;
}

.doc li {
  margin-bottom: 6px;
}

.doc a {
  color: var(--accent);
}

.doc-links {
  margin-top: 28px;
  color: var(--muted);
  font-size: 0.9rem;
}

.footer-links {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 14px;
  justify-content: center;
  margin-bottom: 6px;
}

.footer-links a {
  color: var(--muted);
  text-decoration: none;
}

.footer-links a:hover {
  color: var(--accent);
}
