:root {
  color-scheme: light;
  --bg: #f4f3ee;
  --surface: #fcfcfb;
  --surface-2: #efeee8;
  --ink: #15171d;
  --ink-2: #4b505c;
  --muted: #858a95;
  --border: rgba(21, 23, 29, .10);
  --grid: #e3e2db;
  --gold: #e9a800;
  --gold-ink: #8a6200;
  --gold-wash: rgba(233, 168, 0, .14);
  --low-wash: rgba(208, 59, 59, .10);
  --good: #0ca30c;
  --good-ink: #0a7a0a;
  --bad: #d03b3b;
  --elixir: #9b3fd0;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --diverge-pos: #2a78d6;
  --diverge-neg: #e34948;
  --header: #131a2a;
  --header-ink: #f3f1ea;
  --shadow: 0 1px 2px rgba(0,0,0,.04);
  --radius: 12px;
}
/* Light by default; dark only when chosen with the toggle. */
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0d1016;
  --surface: #151922;
  --surface-2: #1c212c;
  --ink: #eef0f4;
  --ink-2: #b9bfcc;
  --muted: #7f8697;
  --border: rgba(255, 255, 255, .08);
  --grid: #262b36;
  --gold: #f4b400;
  --gold-ink: #f4b400;
  --gold-wash: rgba(244, 180, 0, .13);
  --low-wash: rgba(230, 103, 103, .12);
  --good-ink: #2fc22f;
  --bad: #e66767;
  --series-1: #3987e5;
  --series-2: #d95926;
  --diverge-pos: #3987e5;
  --diverge-neg: #e66767;
  --header: #0a0d14;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.45 Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; }
img { display: block; }
.wrap { max-width: 1240px; margin: 0 auto; padding: 0 16px; }
.display { font-family: "Lilita One", Inter, sans-serif; font-weight: 400; letter-spacing: .01em; }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.ink-2 { color: var(--ink-2); }

/* ---------- top bar ---------- */
.topbar { background: var(--header); color: var(--header-ink); position: sticky; top: 0; z-index: 20; }
.topbar-inner { display: flex; align-items: center; gap: 20px; min-height: 60px; }
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand img { width: 36px; height: 36px; }
.brand .name { font-family: "Lilita One", sans-serif; font-size: 20px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand .tag { font-size: 12px; opacity: .6; }
.tabs { display: flex; gap: 4px; margin-left: auto; overflow-x: auto; scrollbar-width: none; }
.tabs button {
  background: none; border: 0; color: inherit; opacity: .65; font: 600 14px Inter, sans-serif;
  padding: 8px 14px; border-radius: 8px; cursor: pointer; white-space: nowrap;
}
.tabs button:hover { opacity: 1; background: rgba(255,255,255,.06); }
.tabs button[aria-selected="true"] { opacity: 1; color: #f4b400; background: rgba(244,180,0,.12); }
.icon-btn {
  background: none; border: 1px solid rgba(255,255,255,.14); color: inherit; border-radius: 8px;
  width: 34px; height: 34px; display: grid; place-items: center; cursor: pointer; flex: none;
}
@media (max-width: 720px) {
  .topbar-inner { flex-wrap: wrap; gap: 8px; padding-top: 10px; }
  .tabs { order: 3; width: 100%; margin: 0 0 6px; }
  #theme-toggle { margin-left: auto; }
}

/* ---------- layout ---------- */
main { padding-top: 24px; padding-bottom: 24px; }
.view > * + * { margin-top: 20px; }
.grid { display: grid; gap: 16px; }
.g-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
@media (max-width: 980px) { .g-3, .g-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 640px) { .g-2, .g-3 { grid-template-columns: minmax(0, 1fr); } }
@media (max-width: 480px) { .g-4 { grid-template-columns: minmax(0, 1fr); } }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px; box-shadow: var(--shadow); min-width: 0;
}
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.card h2, .card h3 { margin: 0; font-size: 15px; font-weight: 700; }
.card .sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.section-title { display: flex; align-items: center; gap: 10px; margin: 32px 0 0; }
.section-title img { width: 28px; height: 28px; }
.section-title h2 { margin: 0; font-family: "Lilita One", sans-serif; font-weight: 400; font-size: 22px; }

/* ---------- clan hero ---------- */
.clan-hero { display: flex; gap: 20px; align-items: center; flex-wrap: wrap; }
.clan-hero .badge { width: 96px; height: 96px; flex: none; }
.clan-hero h1 { margin: 0; font-family: "Lilita One", sans-serif; font-weight: 400; font-size: 32px; line-height: 1.1; }
.clan-hero .meta { display: flex; flex-wrap: wrap; gap: 6px 16px; color: var(--ink-2); margin-top: 6px; font-size: 13px; }
.clan-hero .desc { color: var(--muted); font-size: 13px; margin-top: 8px; max-width: 640px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px 3px 4px; border-radius: 999px; background: var(--surface-2); font-size: 12px; color: var(--ink-2); }
.chip img { width: 20px; height: 20px; }
.record { margin-left: auto; display: flex; gap: 24px; }
.record div { text-align: center; }
.record .v { font-family: "Lilita One", sans-serif; font-size: 26px; line-height: 1; }
.record .l { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; margin-top: 4px; }
@media (max-width: 720px) { .record { margin-left: 0; width: 100%; justify-content: space-between; } }

/* ---------- stat tiles ---------- */
.tile { display: flex; gap: 14px; align-items: center; }
.tile img { width: 40px; height: 40px; object-fit: contain; flex: none; }
.tile .label { font-size: 12px; color: var(--muted); }
.tile .value { font-size: 26px; font-weight: 700; line-height: 1.15; }
.tile .note { font-size: 12px; color: var(--ink-2); }

/* ---------- leaders ---------- */
.leader .head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.leader .head img, .leader .head .ico { width: 30px; height: 30px; object-fit: contain; flex: none; }
.leader .head h3 { font-size: 13px; color: var(--ink-2); font-weight: 600; }
.leader ol { list-style: none; margin: 0; padding: 0; }
.leader li { display: flex; align-items: center; gap: 8px; padding: 5px 0; cursor: pointer; }
.leader li + li { border-top: 1px solid var(--border); }
.leader li:hover .who { text-decoration: underline; }
.leader .pos { width: 18px; font-weight: 700; color: var(--muted); font-size: 12px; text-align: center; }
.leader li:first-child .pos { color: var(--gold-ink); }
.leader li:first-child .who { font-weight: 700; }
.leader .who { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.leader .val { font-weight: 600; }
.leader li:first-child .val { font-family: "Lilita One", sans-serif; font-weight: 400; font-size: 18px; }
.th-mini { width: 22px; height: 22px; object-fit: contain; flex: none; }

/* ---------- TH distribution ---------- */
.thdist { display: flex; align-items: flex-end; gap: 6px; height: 190px; padding-top: 10px; }
.thdist .col { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; min-width: 0; }
.thdist .bar { width: 100%; max-width: 24px; background: var(--gold); border-radius: 4px 4px 0 0; position: relative; }
.thdist .cnt { font-size: 12px; font-weight: 600; margin-bottom: 4px; }
.thdist img { width: 100%; max-width: 34px; aspect-ratio: 1; object-fit: contain; margin-top: 6px; }
.thdist .lbl { font-size: 11px; color: var(--muted); }

/* ---------- tables ---------- */
.table-tools { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.search {
  background: var(--surface-2); border: 1px solid var(--border); color: var(--ink); border-radius: 8px;
  padding: 7px 10px; font: inherit; width: 220px; max-width: 100%;
}
.seg { display: inline-flex; background: var(--surface-2); border-radius: 8px; padding: 3px; gap: 2px; }
.seg button { border: 0; background: none; color: var(--ink-2); font: 600 12px Inter, sans-serif; padding: 5px 10px; border-radius: 6px; cursor: pointer; }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,.08); }
select.search { width: auto; }
.table-wrap { overflow-x: auto; margin: 0 -18px; padding: 0 18px; -webkit-overflow-scrolling: touch; }

/* Horizontal-scroll cue: hint text, edge fades, and a pinned first column */
.table-scroll { position: relative; }
.scroll-hint { display: none; align-items: center; justify-content: flex-end; gap: 6px; font-size: 12px; font-weight: 600; color: var(--gold-ink); margin: -4px 0 6px; }
.scroll-hint span { display: inline-block; animation: nudge 1.4s ease-in-out infinite; }
@keyframes nudge { 0%, 100% { transform: translateX(0); } 50% { transform: translateX(5px); } }
@media (prefers-reduced-motion: reduce) { .scroll-hint span { animation: none; } }
.table-scroll.scrollable .scroll-hint { display: flex; }
.table-scroll::before, .table-scroll::after {
  content: ""; position: absolute; top: 0; bottom: 0; width: 36px; pointer-events: none; z-index: 3;
  opacity: 0; transition: opacity .2s;
}
.table-scroll::before { left: -18px; background: linear-gradient(to right, var(--surface), transparent); }
.table-scroll::after { right: -18px; background: linear-gradient(to left, var(--surface), transparent); }
.table-scroll.can-left::before, .table-scroll.can-right::after { opacity: 1; }
.table-scroll.scrollable th:first-child, .table-scroll.scrollable td:first-child {
  position: sticky; left: -18px; z-index: 2; background: var(--surface);
}
.table-scroll.scrollable thead th:first-child { z-index: 4; }
.table-scroll.can-left td:first-child, .table-scroll.can-left th:first-child { box-shadow: 6px 0 8px -6px rgba(0, 0, 0, .25); }
tbody tr.click:hover td:first-child { background: var(--surface-2); }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 8px 10px; text-align: left; white-space: nowrap; }
thead th {
  font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600;
  border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface);
}
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--ink); }
th.sorted { color: var(--ink); }
th.sorted::after { content: " ↓"; }
th.sorted.asc::after { content: " ↑"; }
tbody tr { border-bottom: 1px solid var(--border); }
tbody tr:last-child { border-bottom: 0; }
tbody tr.click { cursor: pointer; }
tbody tr.click:hover { background: var(--surface-2); }
td.r, th.r { text-align: right; }
td.c, th.c { text-align: center; }
.player-cell { display: flex; align-items: center; gap: 8px; }
.player-cell .nm { font-weight: 600; }
.player-cell .role { font-size: 11px; color: var(--muted); }
.cell-rank { font-size: 10px; color: var(--muted); margin-left: 4px; font-weight: 500; }
td.hi { background: var(--gold-wash); }
td.lo { background: var(--low-wash); }
.legend { display: flex; gap: 16px; flex-wrap: wrap; font-size: 12px; color: var(--muted); margin-top: 12px; }
.legend i { display: inline-block; width: 12px; height: 12px; border-radius: 3px; vertical-align: -2px; margin-right: 6px; border: 1px solid var(--border); }

/* ---------- meters, pills, stars ---------- */
.meter { height: 6px; border-radius: 999px; background: var(--surface-2); overflow: hidden; min-width: 60px; }
.meter > span { display: block; height: 100%; background: var(--gold); border-radius: 999px; }
.meter.bad > span { background: var(--bad); }
.meter.ok > span { background: var(--good); }
.hero-cell { display: flex; flex-direction: column; gap: 4px; min-width: 74px; }
.hero-cell .lv { font-size: 12px; }
.hero-cell .lv b { font-weight: 700; }
.pill { display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 700; }
.pill.win { background: rgba(12,163,12,.14); color: var(--good-ink); }
.pill.lose { background: rgba(208,59,59,.14); color: var(--bad); }
.pill.tie, .pill.neutral { background: var(--surface-2); color: var(--ink-2); }
.pill.rushed { background: rgba(208,59,59,.14); color: var(--bad); }
.pill.gold { background: var(--gold-wash); color: var(--gold-ink); }
.stars { display: inline-flex; gap: 1px; vertical-align: middle; }
.stars svg { width: 14px; height: 14px; }
.attack { display: inline-flex; align-items: center; gap: 6px; }
.attack .pct { font-size: 12px; color: var(--ink-2); }
.missed { color: var(--bad); font-size: 12px; font-weight: 600; }
.move { font-size: 10px; color: var(--muted); }
.move.up { color: var(--good-ink); }
.move.down { color: var(--bad); }

/* ---------- current war ---------- */
.war-vs { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 16px; }
.war-side { display: flex; align-items: center; gap: 12px; }
.war-side.right { flex-direction: row-reverse; text-align: right; }
.war-side img { width: 56px; height: 56px; }
.war-side .n { font-weight: 700; font-size: 15px; }
.war-score { text-align: center; }
.war-score .s { font-family: "Lilita One", sans-serif; font-size: 34px; line-height: 1; display: flex; align-items: center; gap: 10px; justify-content: center; }
.war-score .s svg { width: 24px; height: 24px; }
.war-score .d { font-size: 12px; color: var(--muted); margin-top: 4px; }
@media (max-width: 640px) {
  .war-vs { grid-template-columns: 1fr; text-align: center; }
  .war-side, .war-side.right { flex-direction: column; text-align: center; }
}

/* ---------- lists ---------- */
.rank-list { list-style: none; margin: 0; padding: 0; }
.rank-list li { display: flex; align-items: center; gap: 10px; padding: 6px 0; font-size: 13px; }
.rank-list li + li { border-top: 1px solid var(--border); }
.rank-list .who { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; }
.rank-list .who:hover { text-decoration: underline; }
.feat { padding: 10px 0; }
.feat + .feat { border-top: 1px solid var(--border); }
.feat .t { font-weight: 700; display: flex; align-items: center; gap: 8px; }
.feat .i { font-size: 12px; color: var(--muted); margin: 2px 0 4px; }
.feat .m { font-size: 12px; color: var(--ink-2); }
.completion-row { display: grid; grid-template-columns: minmax(0, 140px) 1fr 54px; align-items: center; gap: 10px; padding: 4px 0; font-size: 12px; }
.completion-row .who { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; }
.scroll-y { max-height: 360px; overflow-y: auto; padding-right: 4px; }

/* ---------- charts ---------- */
.chart-box { position: relative; width: 100%; }
.chart-box canvas { display: block; }

/* ---------- drawer ---------- */
.drawer { position: fixed; inset: 0; z-index: 50; pointer-events: none; }
.drawer-backdrop { position: absolute; inset: 0; background: rgba(5, 8, 15, .5); opacity: 0; transition: opacity .2s; }
.drawer-panel {
  position: absolute; top: 0; right: 0; height: 100%; width: min(560px, 100%); background: var(--bg);
  border-left: 1px solid var(--border); overflow-y: auto; transform: translateX(100%); transition: transform .22s ease;
  padding: 24px 20px 40px;
}
.drawer.open { pointer-events: auto; }
.drawer.open .drawer-backdrop { opacity: 1; }
.drawer.open .drawer-panel { transform: none; }
.drawer-close { position: absolute; top: 14px; right: 14px; font-size: 22px; color: var(--ink); border-color: var(--border); background: var(--surface); }
.drawer-panel > * + * { margin-top: 16px; }
.p-head { display: flex; gap: 14px; align-items: center; padding-right: 40px; }
.p-head .th { width: 72px; height: 72px; object-fit: contain; }
.p-head h2 { margin: 0; font-family: "Lilita One", sans-serif; font-weight: 400; font-size: 26px; line-height: 1.1; }
.p-head .meta { color: var(--muted); font-size: 12px; margin-top: 4px; }
.kv { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
.kv .k { font-size: 11px; color: var(--muted); }
.kv .v { font-weight: 700; font-size: 16px; }
.kv .r { font-size: 11px; color: var(--muted); }
.hero-list { display: grid; gap: 10px; }
.hero-row { display: grid; grid-template-columns: 40px 1fr 70px; gap: 10px; align-items: center; }
.hero-row img { width: 40px; height: 40px; border-radius: 8px; object-fit: cover; background: var(--surface-2); }
.hero-row .nm { font-size: 12px; font-weight: 600; margin-bottom: 4px; }
.hero-row .lv { text-align: right; font-size: 13px; }

/* ---------- empty / misc ---------- */
.empty { text-align: center; padding: 60px 20px; color: var(--muted); }
.empty h2 { font-family: "Lilita One", sans-serif; font-weight: 400; color: var(--ink); font-size: 24px; margin: 0 0 8px; }
.empty code { background: var(--surface-2); padding: 2px 6px; border-radius: 4px; }
.footer { display: flex; flex-direction: column; gap: 6px; padding-top: 12px; padding-bottom: 32px; font-size: 11px; color: var(--muted); border-top: 1px solid var(--border); }
.note-box { font-size: 12px; color: var(--muted); }
