:root {
  /* Fonts and text styles are shared by both themes */
  --display: 'Bungee', 'Impact', system-ui, sans-serif;
  --body: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
  /* Dark theme (default) */
  --bg: #0b1020; --bg2: #141a33; --panel: #1a2142; --line: #2c3566; --text: #e8ecff; --muted: #9aa3cf;
  --accent: #ffcc33; --accent2: #ff4d8d; --good: #22c55e; --radius: 16px;
  --accent-text: #ffcc33; --glow1: #2a1d5c; --glow2: #4a1238; --input: #0e1430; --raised: #10163a; --pill: #2a2350;
  --shadow: rgba(0, 0, 0, .35); --backdrop: rgba(5, 8, 20, .75); --toast-bg: #fff; --toast-fg: #111; --h1-shadow: #000;
  --tag-bg: #3b2a12; --tag-fg: #fbbf24;
  /* Cards */
  --card-base: #0b1020; --card-end: #11172f; --card-mix: 55%; --card-edge: #fff; --card-shadow: rgba(0, 0, 0, .45);
  --card-chip: rgba(0, 0, 0, .35); --card-well: rgba(0, 0, 0, .28); --card-well2: rgba(0, 0, 0, .3);
  --bar-track: rgba(255, 255, 255, .1); --bar-end: #fff; --aff: #7dd3fc; --legend: #ffd54a; --epic: #d8b4fe;
  color-scheme: dark;
}
:root[data-theme="light"] {
  --bg: #f3f4fa; --bg2: #ffffff; --panel: #ffffff; --line: #d6daea; --text: #171a2e; --muted: #5b6285;
  --accent: #ffbf1f; --accent2: #e0306f; --good: #16a34a;
  --accent-text: #a86b00; --glow1: #e4dcff; --glow2: #ffe0ec; --input: #ffffff; --raised: #eef0f8; --pill: #ece8ff;
  --shadow: rgba(30, 40, 90, .12); --backdrop: rgba(30, 36, 60, .45); --toast-bg: #171a2e; --toast-fg: #fff; --h1-shadow: #c9c3e8;
  --tag-bg: #fff1c7; --tag-fg: #8a5a00;
  --card-base: #ffffff; --card-end: #ffffff; --card-mix: 32%; --card-edge: #000; --card-shadow: rgba(30, 40, 90, .18);
  --card-chip: rgba(255, 255, 255, .75); --card-well: rgba(20, 30, 80, .06); --card-well2: rgba(20, 30, 80, .07);
  --bar-track: rgba(20, 30, 80, .1); --bar-end: #171a2e; --aff: #0369a1; --legend: #a86b00; --epic: #7e22ce;
  color-scheme: light;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: radial-gradient(1200px 600px at 20% -10%, var(--glow1) 0%, transparent 60%),
              radial-gradient(900px 500px at 110% 10%, var(--glow2) 0%, transparent 55%), var(--bg);
  color: var(--text); font-family: var(--body); min-height: 100vh; line-height: 1.45;
}
[hidden] { display: none !important; }
button, input, select { font: inherit; color: inherit; }
button {
  background: var(--bg2); border: 1px solid var(--line); border-radius: 12px; padding: .7em 1.1em;
  cursor: pointer; font-weight: 600; transition: transform .1s, background .2s, border-color .2s;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:active:not(:disabled) { transform: scale(.97); }
button:disabled { opacity: .45; cursor: not-allowed; }
button.primary { background: linear-gradient(135deg, var(--accent), #ff8a3d); color: #1b1300; border: 0; }
button.mini { padding: .35em .7em; font-size: .85rem; }
button.mini.on { border-color: var(--accent); background: var(--pill); }
button.link { background: none; border: 0; color: var(--muted); text-decoration: underline; padding: .4em; }
input, select { background: var(--input); border: 1px solid var(--line); border-radius: 12px; padding: .7em .9em; width: 100%; }
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.muted { color: var(--muted); }
.screen { max-width: 1200px; margin: 0 auto; padding: 24px 16px 48px; }
.topbar, .toolbar { padding-right: 76px; }

/* Home */
.hero { text-align: center; padding: 24px 0 8px; }
.hero h1 { font-family: var(--display); font-size: clamp(2.6rem, 9vw, 5rem); margin: 0; letter-spacing: 1px; text-shadow: 0 4px 0 var(--h1-shadow), 0 0 30px rgba(255, 204, 51, .35); }
.hero h1 span { color: var(--accent-text); }
.tag-line { color: var(--muted); margin: .4em 0 0; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; max-width: 520px; margin: 20px auto; box-shadow: 0 20px 50px var(--shadow); }
.panel h2 { font-family: var(--display); margin: 0 0 12px; }
.field { display: grid; gap: 6px; margin: 12px 0; font-weight: 600; font-size: .9rem; color: var(--muted); }
.actions { display: grid; gap: 10px; margin-top: 12px; }
.join { display: flex; gap: 8px; }
.join input { text-transform: uppercase; letter-spacing: 3px; font-weight: 800; }
.join.highlight input { outline: 2px solid var(--accent2); }
.links { display: flex; flex-wrap: wrap; justify-content: center; gap: 4px 12px; margin-top: 10px; }
.links > * { white-space: nowrap; }
.showcase { display: flex; gap: 16px; justify-content: center; flex-wrap: wrap; margin-top: 8px; }
.showcase .card:nth-child(1) { transform: rotate(-4deg); }
.showcase .card:nth-child(3) { transform: rotate(4deg); }
.why { max-width: 980px; margin: 40px auto 0; text-align: center; }
.why h2 { font-family: var(--display); font-weight: 400; font-size: clamp(1.5rem, 4vw, 2.1rem); margin: 0 0 8px; }
.why-lead { color: var(--muted); max-width: 640px; margin: 0 auto 20px; font-size: 1.02rem; }
.why-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 14px; text-align: left; }
.why-grid article { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; }
.why-grid article > span { font-size: 1.6rem; }
.why-grid h3 { margin: 6px 0 6px; font-size: 1.02rem; }
.why-grid p { margin: 0; color: var(--muted); font-size: .9rem; }
.why-cta { margin: 20px auto 0; max-width: 640px; }
.fine { text-align: center; color: var(--muted); font-size: .8rem; margin-top: 28px; }

/* Lobby */
.code-box { text-align: center; background: var(--raised); border-radius: 12px; padding: 12px; }
.code-box p { margin: 0; color: var(--muted); }
.code { font-family: var(--display); font-size: 2.6rem; letter-spacing: 8px; color: var(--accent-text); margin: 6px 0 10px; }
.players-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; }
.players-list li { display: flex; justify-content: space-between; align-items: center; background: var(--raised); border-radius: 10px; padding: 8px 12px; }

/* Game */
.topbar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; font-weight: 700; }
.spacer { flex: 1; }
.pill { background: var(--pill); border-radius: 99px; padding: .2em .7em; font-size: .85rem; }
.emotes button { background: none; border: 0; padding: .2em; font-size: 1.25rem; }
.players { display: flex; gap: 8px; flex-wrap: wrap; margin: 14px 0; }
.chip { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 6px 12px; display: flex; gap: 10px; align-items: center; }
.chip.me { border-color: #5865f2; }
.chip.active { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(255, 204, 51, .25); }
.chip.out { opacity: .4; text-decoration: line-through; }
.chip-count { font-weight: 800; }
.tag { font-size: .7rem; background: var(--tag-bg); color: var(--tag-fg); padding: 1px 6px; border-radius: 6px; }
.board { display: grid; grid-template-columns: 1fr 260px; gap: 16px; }
.table { min-height: 420px; min-width: 0; }
.board > * { min-width: 0; }
body { overflow-x: hidden; }
.side { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; max-height: 70vh; overflow: auto; }
.side h4 { margin: 0 0 8px; }
.log { list-style: none; margin: 0; padding: 0; font-size: .82rem; color: var(--muted); display: grid; gap: 6px; }
.log li:first-child { color: var(--text); }
.status { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; margin-bottom: 16px; display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.status.your-turn { border-color: var(--accent); animation: pulse 1.6s infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 4px rgba(255, 204, 51, .2); } }
.timer { margin-left: auto; font-variant-numeric: tabular-nums; font-weight: 800; }
.timer.low { color: var(--accent2); }
.hand { display: flex; justify-content: center; }
.spectate { padding: 60px; color: var(--muted); }
.reveal { display: flex; gap: 14px; flex-wrap: wrap; justify-content: center; }
.play { display: flex; flex-direction: column; align-items: center; gap: 4px; animation: flipIn .5s both; }
@keyframes flipIn { from { opacity: 0; transform: rotateY(90deg) translateY(20px); } to { opacity: 1; transform: none; } }
.play-name { font-weight: 800; }
.score { font-family: var(--display); font-size: 1.8rem; line-height: 1; }
.play.win .score { color: var(--good); }
.play.tied .score { color: var(--accent-text); }
.parts { font-size: .72rem; color: var(--muted); text-align: center; max-width: 220px; min-height: 2.2em; }
.play.win .card { box-shadow: 0 0 0 3px var(--good), 0 0 30px rgba(34, 197, 94, .5); }
.play:not(.win) .card { filter: saturate(.7) brightness(.85); }
.banner { text-align: center; font-family: var(--display); font-size: 2rem; margin: 8px 0; }
.banner.won { color: var(--accent-text); text-shadow: 0 0 30px rgba(255, 204, 51, .5); }
.banner small { display: block; font-family: var(--body); font-size: .9rem; color: var(--muted); }
.standings { max-width: 320px; margin: 0 auto 12px; }
.row { display: flex; gap: 10px; justify-content: center; margin-bottom: 16px; align-items: center; }
.sub { text-align: center; color: var(--muted); margin: 0 0 8px; }
.emote-pop { position: fixed; top: 80px; left: 50%; transform: translateX(-50%) scale(.8); background: var(--panel); border: 1px solid var(--accent); border-radius: 99px; padding: 8px 16px; font-size: 1.2rem; opacity: 0; pointer-events: none; transition: .25s; }
.emote-pop.show { opacity: 1; transform: translateX(-50%) scale(1); }

/* Card */
.card {
  --tc: #8b5cf6;
  width: 270px; border-radius: 18px; padding: 10px; position: relative; overflow: hidden;
  background: linear-gradient(160deg, color-mix(in srgb, var(--tc) var(--card-mix), var(--card-base)) 0%, var(--card-end) 55%);
  border: 3px solid color-mix(in srgb, var(--tc) 80%, var(--card-edge) 20%);
  box-shadow: 0 12px 30px var(--card-shadow);
}
.card.small { width: 230px; font-size: .9em; }
.card.r-legendary { border-color: #ffd54a; }
:root[data-theme="light"] .card.r-legendary { border-color: #f0b400; }
.card.r-legendary::after {
  content: ''; position: absolute; inset: 0; pointer-events: none;
  background: linear-gradient(115deg, transparent 35%, rgba(255, 255, 255, .22) 50%, transparent 65%);
  background-size: 250% 100%; animation: shine 3.5s infinite;
}
@keyframes shine { from { background-position: 150% 0; } to { background-position: -100% 0; } }
.card.r-epic { border-color: #c084fc; }
.c-head { display: flex; gap: 6px; align-items: center; font-size: .72rem; font-weight: 800; text-transform: uppercase; letter-spacing: .5px; }
.c-type { background: var(--card-chip); padding: 2px 8px; border-radius: 99px; }
.c-new { background: var(--accent2); color: #fff; padding: 2px 6px; border-radius: 6px; }
.c-rarity { margin-left: auto; opacity: .85; }
.r-legendary .c-rarity { color: var(--legend); }
.r-epic .c-rarity { color: var(--epic); }
.c-art { position: relative; margin: 8px 0 6px; border-radius: 12px; overflow: hidden; aspect-ratio: 16 / 11; background: #000; border: 2px solid rgba(255, 255, 255, .15); }
.portrait, .portrait-img { width: 100%; height: 100%; display: block; object-fit: cover; }
.c-best { position: absolute; bottom: 6px; left: 6px; background: rgba(0, 0, 0, .7); padding: 2px 8px; border-radius: 99px; font-size: .72rem; font-weight: 700; color: var(--accent); }
.c-title h3 { margin: 0; font-family: var(--display); font-size: 1.1rem; font-weight: 400; letter-spacing: .5px; }
.c-title p { margin: 0 0 6px; font-size: .72rem; color: var(--muted); }
.stats { display: grid; gap: 3px; }
.stat {
  display: grid; grid-template-columns: 20px 82px 1fr auto; gap: 6px; align-items: center; width: 100%;
  background: var(--card-well); border: 1px solid transparent; border-radius: 8px; padding: 4px 6px; font-size: .78rem; text-align: left;
}
button.stat:hover { border-color: var(--accent); background: rgba(255, 204, 51, .12); }
.stat.best .s-lbl { color: var(--accent-text); }
.stat.chosen { border-color: var(--accent); background: rgba(255, 204, 51, .2); }
.s-lbl { font-weight: 700; white-space: nowrap; }
.aff { color: var(--aff); font-style: normal; margin-left: 3px; }
.s-bar { height: 7px; background: var(--bar-track); border-radius: 99px; overflow: hidden; }
.s-bar b { display: block; height: 100%; background: linear-gradient(90deg, var(--tc), color-mix(in srgb, var(--tc) 35%, var(--bar-end))); border-radius: 99px; }
.s-val { font-weight: 800; font-variant-numeric: tabular-nums; min-width: 2ch; text-align: right; }
.s-val small { color: var(--muted); font-weight: 600; margin-right: 4px; }
.bonus { font-style: normal; font-weight: 800; font-size: .82em; color: var(--good); margin-left: 4px; }
.bonus.neg { color: var(--accent2); }
.c-ability { margin-top: 6px; font-size: .74rem; background: var(--card-well2); border-radius: 8px; padding: 6px 8px; min-height: 2.6em; }

/* Dialogs */
dialog { background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: var(--radius); max-width: 640px; width: calc(100% - 32px); padding: 20px 24px; }
dialog.wide { max-width: 1180px; }
dialog::backdrop { background: var(--backdrop); }
dialog .close { position: absolute; right: 12px; top: 12px; padding: .3em .6em; }
dialog h2 { font-family: var(--display); margin-top: 0; }
.filters { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 14px; }
.g-item { display: grid; gap: 6px; justify-items: center; align-content: start; }
.facts { width: 270px; font-size: .78rem; background: var(--bg2); border: 1px solid var(--line); border-radius: 10px; padding: 6px 10px; }
.facts summary { cursor: pointer; font-weight: 700; color: var(--muted); }
.facts ul { list-style: none; margin: 6px 0 0; padding: 0; display: grid; gap: 2px; }
.facts p { margin: 6px 0 0; color: var(--muted); font-size: .72rem; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 16px; justify-items: center; }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translate(-50%, 120px); background: var(--toast-bg); color: var(--toast-fg); padding: 10px 16px; border-radius: 12px; font-weight: 600; transition: transform .3s; max-width: calc(100% - 32px); z-index: 10; }
.toast.show { transform: translate(-50%, 0); }

@media (max-width: 820px) {
  .board { grid-template-columns: 1fr; }
  .side { max-height: 200px; }
  .showcase .card:not(:first-child) { display: none; }
  .showcase .card { transform: none !important; }
  .reveal { flex-wrap: nowrap; justify-content: flex-start; overflow-x: auto; scroll-snap-type: x mandatory; padding-bottom: 8px; margin: 0 -16px; padding-left: 16px; padding-right: 16px; }
  .play { scroll-snap-align: center; flex: 0 0 auto; }
  .reveal .card.small { width: 210px; }
}
.boot-error { background: #7f1d1d; color: #fff; padding: 12px 16px; text-align: center; }
.theme-toggle {
  position: fixed; right: 14px; top: calc(14px + env(safe-area-inset-top, 0px)); z-index: 1000;
  width: 64px; height: 32px; padding: 0; border-radius: 99px; border: 1px solid var(--line);
  background: var(--panel); box-shadow: 0 4px 14px var(--shadow); display: flex; align-items: center; justify-content: space-between;
  padding-inline: 8px; font-size: .85rem; line-height: 1;
}
.theme-toggle:active:not(:disabled) { transform: none; }
.theme-toggle:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.tt-icon { position: relative; z-index: 1; transition: opacity .25s; }
.tt-knob {
  position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), #ff8a3d); box-shadow: 0 2px 6px rgba(0, 0, 0, .3);
  transition: transform .25s cubic-bezier(.4, 1.4, .6, 1), background .25s;
}
.theme-toggle[aria-checked="true"] .tt-knob { transform: translateX(32px); background: linear-gradient(135deg, #8b9cff, #5b4bd8); }
.theme-toggle[aria-checked="false"] .tt-sun, .theme-toggle[aria-checked="true"] .tt-moon { opacity: 1; }
.theme-toggle[aria-checked="false"] .tt-moon, .theme-toggle[aria-checked="true"] .tt-sun { opacity: .55; }
@media (prefers-reduced-motion: reduce) { .tt-knob { transition: none; } }
@media print { .theme-toggle { display: none; } }
a.link { color: var(--muted); text-decoration: underline; padding: .4em; font-weight: 600; }
