/* Devise Friday brand: light and minimal by default, indigo accent. Dark only via the toggle. */
:root {
  --bg: #f8f9fb;
  --surface: #ffffff;
  --surface-2: #f2f4f7;
  --border: #e4e7ec;
  --text: #101828;
  --muted: #667085;
  --accent: #4f46e5;
  --accent-soft: #eef2ff;
  --good: #067647;
  --good-soft: #ecfdf3;
  --warn: #b54708;
  --warn-soft: #fffaeb;
  --bad: #c01048;
  --bad-soft: #fff1f3;
  --series-1: #4f46e5;
  --series-2: #f59e0b;
  --series-3: #10b981;
  --series-4: #ec4899;
  --bar: #d0d5dd;
  --bar-hot: #4f46e5;
  --shadow: 0 1px 2px rgb(16 24 40 / 0.04), 0 1px 3px rgb(16 24 40 / 0.06);
  --radius: 14px;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #0c0d12; --surface: #15161d; --surface-2: #1d1f28; --border: #282b37;
  --text: #e9eaf0; --muted: #9297a8; --accent: #818cf8; --accent-soft: #1f2040;
  --good: #47cd89; --good-soft: #0f2a1d; --warn: #fdb022; --warn-soft: #33260a;
  --bad: #f97066; --bad-soft: #3a1a1c;
  --series-1: #818cf8; --series-2: #fbbf24; --series-3: #34d399; --series-4: #f472b6;
  --bar: #343849; --bar-hot: #818cf8; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 "IBM Plex Sans Thai", system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; }
img { display: block; max-width: 100%; }
h1, h2, h3 { line-height: 1.25; margin: 0; }
h2 { font-size: 1.05rem; font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.num { font-variant-numeric: tabular-nums; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 24px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; text-decoration: none; white-space: nowrap; }
.brand-mark svg { width: 28px; height: 28px; display: block; }
.brand-mark rect { fill: var(--accent); }
.brand-mark path { stroke: #fff; stroke-width: 3; stroke-linecap: round; fill: none; }
.tabs { display: flex; gap: 4px; overflow-x: auto; flex: 1; scrollbar-width: none; }
.tabs a {
  padding: 7px 12px; border-radius: 8px; text-decoration: none; color: var(--muted);
  white-space: nowrap; font-weight: 500;
}
.tabs a:hover { background: var(--surface-2); color: var(--text); }
.tabs a.active { background: var(--accent-soft); color: var(--accent); }
.top-actions { display: flex; align-items: center; gap: 8px; }
.quota { font-size: 0.8rem; color: var(--muted); white-space: nowrap; font-variant-numeric: tabular-nums; }

.container { max-width: 1200px; margin: 0 auto; padding: 24px; }

/* Controls */
.btn {
  font: inherit; font-weight: 500; cursor: pointer;
  padding: 8px 14px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.05); }
.btn.ghost { background: transparent; }
.btn.sm { padding: 4px 10px; font-size: 0.85rem; }
.btn:disabled { opacity: 0.55; cursor: progress; }
input[type="text"], input[type="password"], input[type="search"], select {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px; min-width: 0;
}
input:focus-visible, select:focus-visible, .btn:focus-visible, .tabs a:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}

.page-head { margin-bottom: 18px; }
.page-head h1 { font-size: 1.5rem; margin-bottom: 4px; }
.searchbar { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 20px; }
.searchbar input { flex: 1 1 280px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 3px 10px; border-radius: 999px; font-size: 0.85rem;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  cursor: default;
}
button.chip { cursor: pointer; font: inherit; font-size: 0.85rem; }
button.chip:hover { border-color: var(--accent); }
.chip .count { color: var(--muted); font-variant-numeric: tabular-nums; }

/* Cards & layout */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px; box-shadow: var(--shadow); min-width: 0;
}
.card + .card, .grid + .card, .card + .grid, .grid + .grid { margin-top: 16px; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.grid { display: grid; gap: 16px; }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.grid.three { grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.stat .label { font-size: 0.8rem; color: var(--muted); }
.stat .value { font-size: 1.45rem; font-weight: 600; font-variant-numeric: tabular-nums; margin-top: 2px; }
.stat .sub { font-size: 0.8rem; color: var(--muted); }

.channel-hero { display: flex; gap: 16px; align-items: center; margin-bottom: 16px; }
.channel-hero img { width: 72px; height: 72px; border-radius: 50%; border: 1px solid var(--border); }
.channel-hero h1 { font-size: 1.35rem; }
.channel-hero .meta { display: flex; gap: 10px; flex-wrap: wrap; color: var(--muted); font-size: 0.9rem; }

/* Badges */
.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 0.78rem; font-weight: 600; white-space: nowrap; }
.badge.good { background: var(--good-soft); color: var(--good); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.neutral { background: var(--surface-2); color: var(--muted); }

/* Tables */
.table-wrap { overflow-x: auto; margin: 0 -18px -18px; }
table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
th, td { padding: 9px 12px; text-align: left; border-top: 1px solid var(--border); vertical-align: middle; }
th { font-weight: 600; color: var(--muted); font-size: 0.8rem; white-space: nowrap; background: var(--surface); position: sticky; top: 0; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--text); }
th.sorted::after { content: " ↓"; }
th.sorted.asc::after { content: " ↑"; }
td.n, th.n { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tbody tr:hover { background: var(--surface-2); }
.vid { display: flex; gap: 10px; align-items: center; min-width: 260px; }
.vid img { width: 96px; aspect-ratio: 16/9; object-fit: cover; border-radius: 6px; flex: none; background: var(--surface-2); }
.vid a { text-decoration: none; font-weight: 500; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.vid a:hover { color: var(--accent); }
.vid .sub { font-size: 0.8rem; color: var(--muted); }

/* Video detail */
.video-hero { display: grid; grid-template-columns: minmax(0, 360px) 1fr; gap: 20px; align-items: start; }
.video-hero img { width: 100%; border-radius: 10px; aspect-ratio: 16/9; object-fit: cover; }
.video-hero h1 { font-size: 1.25rem; margin-bottom: 6px; }
.description { white-space: pre-wrap; font-size: 0.9rem; max-height: 220px; overflow: auto; color: var(--muted); }
.comment { padding: 10px 0; border-top: 1px solid var(--border); font-size: 0.9rem; }
.comment:first-child { border-top: 0; }
.comment b { font-weight: 600; }

/* Charts */
.chart { width: 100%; height: auto; display: block; overflow: visible; }
.chart text { fill: var(--muted); font-size: 11px; font-family: inherit; }
.chart .grid-line { stroke: var(--border); stroke-width: 1; }
.chart .bar { fill: var(--bar); }
.chart .bar.hot { fill: var(--bar-hot); }
.chart .bar:hover { opacity: 0.8; }
.chart .median-line { stroke: var(--muted); stroke-dasharray: 4 4; stroke-width: 1; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 0.8rem; color: var(--muted); margin-top: 8px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 5px; vertical-align: -1px; }
.tooltip {
  position: fixed; pointer-events: none; z-index: 50;
  background: var(--text); color: var(--bg); padding: 6px 10px; border-radius: 6px;
  font-size: 0.8rem; max-width: 280px; opacity: 0; transition: opacity 0.1s;
}

.hbars { display: grid; gap: 6px; }
.hbar { display: grid; grid-template-columns: 70px 1fr 70px; gap: 10px; align-items: center; font-size: 0.85rem; }
.hbar .track { background: var(--surface-2); border-radius: 4px; height: 14px; overflow: hidden; }
.hbar .fill { height: 100%; background: var(--series-1); border-radius: 4px; }
.hbar.best .fill { background: var(--accent); }
.hbar .v { text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); }

.score-ring { display: flex; align-items: center; gap: 16px; }
.score-ring .ring { width: 84px; height: 84px; flex: none; }
.score-ring .ring circle { fill: none; stroke-width: 9; }
.score-ring .ring .track { stroke: var(--surface-2); }
.score-ring .ring text { font-size: 22px; font-weight: 700; fill: var(--text); text-anchor: middle; dominant-baseline: central; }

.issue { display: flex; gap: 10px; align-items: baseline; padding: 8px 0; border-top: 1px solid var(--border); font-size: 0.9rem; }
.issue:first-child { border-top: 0; }
.issue .title { flex: 1; min-width: 0; }
.issue .title a { font-weight: 500; text-decoration: none; }
.issue .title a:hover { color: var(--accent); }

.verdict { padding: 14px 16px; border-radius: 10px; font-weight: 500; }
.verdict.easy { background: var(--good-soft); color: var(--good); }
.verdict.mixed { background: var(--warn-soft); color: var(--warn); }
.verdict.hard { background: var(--bad-soft); color: var(--bad); }

.compare-inputs { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 8px; margin-bottom: 12px; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; }

/* States */
.empty { text-align: center; padding: 48px 16px; color: var(--muted); }
.empty h2 { color: var(--text); margin-bottom: 6px; font-size: 1.1rem; }
.error { background: var(--bad-soft); color: var(--bad); padding: 12px 16px; border-radius: 10px; margin-bottom: 16px; }
.loading { display: flex; align-items: center; gap: 10px; color: var(--muted); padding: 32px 0; justify-content: center; }
.spinner { width: 18px; height: 18px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translate(-50%, 20px);
  background: var(--text); color: var(--bg); padding: 8px 16px; border-radius: 8px;
  opacity: 0; transition: 0.2s; pointer-events: none; z-index: 60; font-size: 0.9rem;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Dialog */
dialog {
  border: 1px solid var(--border); border-radius: 14px; padding: 22px; width: min(520px, calc(100vw - 32px));
  background: var(--surface); color: var(--text);
}
dialog::backdrop { background: rgb(0 0 0 / 0.4); }
dialog h2 { margin-bottom: 6px; font-size: 1.15rem; }
dialog h2.dialog-section { font-size: 0.95rem; margin: 18px 0 2px; padding-top: 14px; border-top: 1px solid var(--border); }
dialog h2 + h2.dialog-section { border-top: 0; padding-top: 0; margin-top: 10px; }
dialog { max-height: calc(100vh - 32px); }
.field select { width: 100%; }

/* AI summary */
.ai-card { border-color: color-mix(in srgb, var(--series-1) 35%, var(--border)); }
.ai-card .card-head h2::before { content: "✦ "; color: var(--series-1); }
.ai-body { font-size: 0.95rem; }
.ai-body h2, .ai-body h3, .ai-body h4 { font-size: 1rem; margin: 16px 0 6px; }
.ai-body h2:first-child, .ai-body h3:first-child { margin-top: 0; }
.ai-body p { margin: 6px 0; }
.ai-body ul, .ai-body ol { margin: 6px 0; padding-left: 22px; }
.ai-body li { margin: 3px 0; }
.ai-body code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; font-size: 0.88em; }
.ai-body hr { border: 0; border-top: 1px solid var(--border); margin: 14px 0; }
.ai-body.streaming::after { content: "▍"; color: var(--series-1); animation: blink 1s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }
.ai-meta { font-size: 0.8rem; color: var(--muted); margin-top: 10px; }
.field { display: grid; gap: 4px; margin: 12px 0 16px; }
.field span { font-size: 0.85rem; color: var(--muted); }
.dialog-actions { display: flex; gap: 8px; align-items: center; }
.spacer { flex: 1; }

/* Bottom navigation (phones only) */
.bottom-nav { display: none; }
.mobile-sort { display: none; }

.demo-banner {
  display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap;
  max-width: 1200px; margin: 16px auto 0; padding: 10px 14px; width: calc(100% - 48px);
  background: var(--accent-soft); border: 1px solid var(--accent); border-radius: 12px; font-size: 0.9rem;
}
.demo-actions { display: flex; gap: 6px; }
.link-btn {
  background: none; border: 0; padding: 0; margin: -4px 0 4px; font: inherit; font-size: 0.85rem;
  color: var(--accent); cursor: pointer; text-align: left;
}
.link-btn:hover { text-decoration: underline; }

#share-dialog { overflow-x: hidden; }
#share-img { display: block; width: 100%; max-width: 420px; max-height: 62vh; object-fit: contain; margin: 0 auto 14px; border-radius: 12px; border: 1px solid var(--border); }
#share-download { text-decoration: none; }

.site-footer {
  max-width: 1200px; margin: 8px auto 0; padding: 16px 24px 28px;
  display: flex; flex-wrap: wrap; justify-content: space-between; gap: 6px 16px;
  border-top: 1px solid var(--border); font-size: 0.8rem; color: var(--muted);
}
.site-footer a { color: var(--muted); }
.site-footer a:hover { color: var(--text); }

.legal { max-width: 760px; }
.legal h1 { font-size: 1.6rem; margin: 14px 0 4px; }
.legal h2 { font-size: 1.1rem; margin: 26px 0 8px; }
.legal p, .legal li { line-height: 1.75; }
.legal ul { padding-left: 1.3em; }
.legal a { color: var(--accent); }
.legal .back { color: var(--muted); text-decoration: none; font-size: 0.9rem; }
.legal .back:hover { color: var(--text); }

@media (max-width: 760px) {
  body { padding-bottom: calc(64px + env(safe-area-inset-bottom)); }
  .demo-banner { width: calc(100% - 32px); margin-top: 12px; font-size: 0.85rem; }
  .site-footer { padding: 14px 16px 20px; }
  .topbar {
    padding: 8px 16px; padding-top: max(8px, env(safe-area-inset-top));
    padding-left: max(16px, env(safe-area-inset-left)); padding-right: max(16px, env(safe-area-inset-right));
    gap: 8px;
  }
  .topbar .tabs { display: none; }
  .brand span:last-child { font-size: 1rem; }
  .top-actions { margin-left: auto; }
  .quota { display: none; }
  .top-actions .btn { min-height: 40px; }

  .bottom-nav {
    display: grid; grid-template-columns: repeat(6, 1fr);
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
    padding: 4px max(4px, env(safe-area-inset-left)) calc(4px + env(safe-area-inset-bottom)) max(4px, env(safe-area-inset-right));
    background: color-mix(in srgb, var(--surface) 92%, transparent);
    backdrop-filter: blur(12px);
    border-top: 1px solid var(--border);
  }
  .bottom-nav a {
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
    min-height: 54px; border-radius: 10px; text-decoration: none;
    color: var(--muted); font-size: 0.72rem; font-weight: 500;
    -webkit-tap-highlight-color: transparent;
  }
  .bottom-nav svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
  .bottom-nav a.active { color: var(--accent); }
  .bottom-nav a.active svg { stroke-width: 2.3; }

  .container { padding: 16px max(16px, env(safe-area-inset-left)) 24px max(16px, env(safe-area-inset-right)); }
  .page-head h1 { font-size: 1.3rem; }
  .page-head p { font-size: 0.9rem; margin: 4px 0 0; }

  /* 16px inputs stop iOS from zooming on focus; bigger tap targets */
  input[type="text"], input[type="password"], input[type="search"], select { font-size: 16px; min-height: 46px; }
  .btn { min-height: 44px; }
  .btn.sm { min-height: 36px; padding: 6px 12px; }
  button.chip { min-height: 36px; padding: 6px 12px; }
  .searchbar { flex-direction: column; }
  .searchbar input, .searchbar select, .searchbar .btn { width: 100%; flex: none; }

  .card { padding: 14px; }
  .card-head { margin-bottom: 10px; }
  .card-head > .chips, .card-head > .btn { width: 100%; }
  .card-head > .chips select { flex: 1; }
  .grid.two, .grid.three { grid-template-columns: 1fr; }

  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .stat { padding: 10px 12px; }
  .stat .value { font-size: 1.2rem; }

  .channel-hero img { width: 56px; height: 56px; }
  .channel-hero h1 { font-size: 1.15rem; }
  .video-hero { grid-template-columns: 1fr; gap: 12px; }
  .video-hero h1 { font-size: 1.1rem; }

  .issue { flex-wrap: wrap; }
  .hbar { grid-template-columns: 56px 1fr 56px; }
  .score-ring { align-items: flex-start; }

  /* Tables become a list of cards */
  .mobile-sort { display: flex; gap: 8px; align-items: center; margin-bottom: 10px; font-size: 0.85rem; color: var(--muted); }
  .mobile-sort select { flex: 1; min-height: 40px; }
  .table-wrap.cards { margin: 0; overflow: visible; }
  .table-wrap.cards table, .table-wrap.cards tbody { display: block; }
  .table-wrap.cards thead { display: none; }
  .table-wrap.cards tr {
    display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px 10px;
    padding: 12px 0; border-top: 1px solid var(--border);
  }
  .table-wrap.cards tr:first-child { border-top: 0; padding-top: 0; }
  .table-wrap.cards tbody tr:hover { background: none; }
  .table-wrap.cards td { display: block; padding: 0; border: 0; text-align: left; min-width: 0; }
  .table-wrap.cards td[data-label]:not([data-label=""])::before {
    content: attr(data-label); display: block; font-size: 0.72rem; color: var(--muted); font-weight: 500;
  }
  .table-wrap.cards td.wide { grid-column: 1 / -1; }
  .table-wrap.cards td.rank { display: none; }
  .table-wrap.cards td.action { grid-column: span 2; align-self: end; }
  .table-wrap.cards td.action .btn { width: 100%; }
  .table-wrap.cards .vid { min-width: 0; }
  .table-wrap.cards .vid img { width: 112px; }
  .table-wrap.cards .vid a { -webkit-line-clamp: 3; }

  /* Compare table keeps its columns but scrolls */
  .compare-inputs { grid-template-columns: 1fr; }

  dialog { padding: 18px; width: calc(100vw - 24px); }
  .dialog-actions { flex-wrap: wrap; }
  .dialog-actions .spacer { display: none; }
  .dialog-actions .btn { flex: 1; }
  .dialog-actions .btn.primary { flex-basis: 100%; order: -1; }

  .toast { bottom: calc(76px + env(safe-area-inset-bottom)); }
}

/* Followed channels */
.avatar-sm { width: 36px; height: 36px; border-radius: 50%; flex: none; object-fit: cover; }
.follow-row { align-items: center; }

/* Title helper */
.titles-form { display: grid; gap: 10px; margin-bottom: 20px; }
.titles-form textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px; min-height: 64px; resize: vertical; }
.titles-form .row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.titles-form .row input[type="search"] { flex: 1 1 260px; }
.title-list { list-style: none; margin: 0; padding: 0; counter-reset: t; }
.title-item { display: flex; gap: 10px; align-items: center; padding: 10px 0; border-top: 1px solid var(--border); }
.title-item:first-child { border-top: 0; }
.title-item::before { counter-increment: t; content: counter(t); color: var(--muted); font-variant-numeric: tabular-nums; width: 1.4em; text-align: right; flex: none; }
.title-item .text { flex: 1; min-width: 0; }
.title-item .text b { font-weight: 600; }
.title-item .angle { font-size: 0.8rem; color: var(--muted); }
