:root {
  --bg: #ffffff;
  --surface: #ffffff;
  --surface-2: #f1f4f9;
  --text: #1b2533;
  --muted: #4f5b6b;
  --accent: #2f6fde;
  --accent-ink: #ffffff;
  --accent-soft: #e4edfc;
  --duel: #e5583a;
  --gold: #e3a21a;
  --error: #e0383e;
  --error-soft: #fde4e5;
  --line: #c7cfdb;
  --line-strong: #2a3647;
  --cell: #ffffff;
  --rel: #e4ebf5;
  --same: #c3d7f7;
  --sel: #b6cff8;
  --given: #1b2533;
  --user: #2f6fde;
  --border: #e3e8ef;
  --shadow: 0 1px 3px rgba(20, 35, 60, .08);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12161d; --surface: #1b212b; --surface-2: #252c38; --text: #eef2f8; --muted: #a9b4c4;
    --accent: #6ea0ff; --accent-ink: #0d1626; --accent-soft: #22324d; --duel: #ff7a5c; --gold: #f2c04e;
    --error: #ff6b70; --error-soft: #4a2427; --line: #364152; --line-strong: #a9b4c4;
    --cell: #1b212b; --rel: #232c3a; --same: #2c4470; --sel: #36548a; --given: #eef2f8; --user: #8db4ff;
    --border: #2b3340; --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #12161d; --surface: #1b212b; --surface-2: #252c38; --text: #eef2f8; --muted: #a9b4c4;
  --accent: #6ea0ff; --accent-ink: #0d1626; --accent-soft: #22324d; --duel: #ff7a5c; --gold: #f2c04e;
  --error: #ff6b70; --error-soft: #4a2427; --line: #364152; --line-strong: #a9b4c4;
  --cell: #1b212b; --rel: #232c3a; --same: #2c4470; --sel: #36548a; --given: #eef2f8; --user: #8db4ff;
  --border: #2b3340; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 17px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-user-select: none; user-select: none; overscroll-behavior: none;
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; padding: 0; }
input { font: inherit; color: inherit; -webkit-user-select: text; user-select: text; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 28px; font-weight: 800; }
h3 { font-size: 15px; font-weight: 700; color: var(--muted); margin: 22px 2px 8px; }

#app { max-width: 500px; margin: 0 auto; min-height: 100%; }
#view { padding: calc(env(safe-area-inset-top) + 10px) 16px calc(env(safe-area-inset-bottom) + 96px); }
#app.in-game #view { padding-bottom: calc(env(safe-area-inset-bottom) + 16px); }
#app.in-game #nav { display: none; }

/* Bottom nav */
#nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10; max-width: 500px; margin: 0 auto;
  display: grid; grid-template-columns: repeat(4, minmax(0, 1fr));
  background: var(--surface); border-top: 1px solid var(--border);
  padding: 6px 8px calc(env(safe-area-inset-bottom) + 6px);
}
#nav button { position: relative; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 6px 0; font-size: 13px; font-weight: 600; color: var(--muted); }
#nav .ni { font-size: 22px; line-height: 1; }
#nav button.on { color: var(--accent); }
#nav .dot { position: absolute; top: 4px; left: calc(50% + 10px); width: 10px; height: 10px; border-radius: 50%; background: var(--duel); }

/* Basics */
.top { display: flex; align-items: center; justify-content: space-between; min-height: 48px; margin-bottom: 14px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 24px; }
.logo { width: 36px; height: 36px; border-radius: 10px; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-size: 21px; font-weight: 900; }
.avatar { width: 44px; height: 44px; border-radius: 50%; background: var(--surface-2); display: grid; place-items: center; font-size: 24px; flex: none; }
.avatar.lg { width: 64px; height: 64px; font-size: 36px; }
.card { display: block; width: 100%; text-align: left; background: var(--surface); border: 1px solid var(--border); border-radius: 16px; box-shadow: var(--shadow); padding: 14px 16px; }
.row { display: flex; align-items: center; gap: 12px; }
.grow { flex: 1; min-width: 0; }
.sub { color: var(--muted); font-size: 14px; }
.ellip { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.big { font-size: 26px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.btn { display: inline-flex; flex-direction: column; align-items: center; justify-content: center; padding: 14px 16px; border-radius: 14px; font-weight: 700; background: var(--surface-2); min-height: 50px; }
.btn small { font-weight: 500; font-size: 14px; opacity: .85; margin-top: 2px; }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.duel { background: var(--duel); color: #fff; }
.btn.outline { background: var(--surface); border: 2px solid var(--accent); color: var(--accent); }
.btn.ghost { background: transparent; color: var(--accent); }
.btn.light { background: #fff; color: #2557c4; padding: 10px 18px; min-height: 0; }
.btn.sm { padding: 8px 14px; min-height: 0; font-size: 15px; border-radius: 10px; }
.btn.xl { font-size: 20px; min-height: 64px; border-radius: 16px; }
.btn.block { width: 100%; margin-top: 10px; }
.btn.danger { color: var(--error); margin-top: 20px; }
.btn:active, .card:active, .num:active, .tool:active { transform: scale(.97); }
.btn:disabled { opacity: .6; }

.pill { padding: 4px 10px; border-radius: 99px; font-size: 13px; font-weight: 700; background: var(--surface-2); color: var(--muted); }
.pill.win { background: var(--accent-soft); color: var(--accent); }
.pill.loss { background: var(--error-soft); color: var(--error); }
.bar { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; flex: 1; }
.bar > i { display: block; height: 100%; width: 0; background: var(--accent); border-radius: 99px; transition: width .4s; }
.bar.cpu > i { background: var(--duel); }
.empty { text-align: center; padding: 18px 8px; color: var(--muted); }
.list { padding-top: 4px; padding-bottom: 4px; }
.list > * + * { border-top: 1px solid var(--border); }
.li { display: flex; align-items: center; gap: 12px; padding: 12px 0; min-height: 60px; }
.li.me { background: var(--accent-soft); margin: 0 -16px; padding: 12px 16px; }
.rank { width: 26px; text-align: center; font-weight: 800; color: var(--muted); }
.num-r { font-variant-numeric: tabular-nums; font-size: 18px; }
.x { width: 36px; height: 36px; border-radius: 50%; color: var(--muted); font-size: 16px; }
.x:hover { background: var(--surface-2); }

/* Home */
.daily { display: flex; align-items: center; justify-content: space-between; gap: 12px; background: linear-gradient(135deg, #3d7bec, #2557c4); color: #fff; border: 0; padding: 20px; cursor: pointer; }
.daily-k { font-size: 14px; font-weight: 600; opacity: .9; }
.daily-t { font-size: 24px; font-weight: 800; margin: 2px 0 4px; }
.daily-s { font-size: 14px; opacity: .9; }
.alert { display: flex; align-items: center; gap: 12px; margin-top: 12px; color: var(--duel); font-size: 18px; }
.home-actions { display: grid; gap: 12px; margin-top: 28px; }

/* Game */
.g-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.icon-btn { width: 44px; height: 44px; border-radius: 12px; display: grid; place-items: center; font-size: 20px; color: var(--text); }
.icon-btn:hover { background: var(--surface-2); }
.g-diff { font-size: 22px; font-weight: 800; padding: 6px 12px; border-radius: 12px; }
.g-diff span { color: var(--accent); font-size: 16px; }
.g-diff:hover { background: var(--surface-2); }
.modes { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); background: var(--surface-2); padding: 4px; border-radius: 12px; margin-bottom: 10px; }
.modes button { padding: 9px 0; border-radius: 9px; font-size: 15px; font-weight: 700; color: var(--muted); }
.modes button.on { background: var(--surface); color: var(--accent); box-shadow: var(--shadow); }
.g-meta { display: flex; justify-content: space-between; align-items: center; font-size: 15px; color: var(--muted); padding: 0 4px 8px; }
.g-meta b { color: var(--text); font-variant-numeric: tabular-nums; }
.race { display: flex; align-items: center; gap: 8px; font-size: 14px; font-weight: 700; padding: 0 4px 10px; }
.race.target { color: var(--duel); justify-content: center; font-weight: 600; }

.board-wrap { position: relative; }
.board {
  display: grid; grid-template-columns: repeat(9, minmax(0, 1fr)); aspect-ratio: 1; width: 100%;
  border: 2.5px solid var(--line-strong); background: var(--cell); transition: filter .2s;
}
.board.blur { filter: blur(10px); }
.cell {
  position: relative; display: grid; place-items: center; aspect-ratio: 1; cursor: pointer;
  border-right: 1px solid var(--line); border-bottom: 1px solid var(--line);
  font-size: clamp(22px, 7.4vw, 34px); font-weight: 500; color: var(--user); background: var(--cell);
}
.cell.c8 { border-right: 0; } .cell.r8 { border-bottom: 0; }
.cell.bR { border-right: 2.5px solid var(--line-strong); }
.cell.bB { border-bottom: 2.5px solid var(--line-strong); }
.cell.given { color: var(--given); font-weight: 600; }
.cell.rel { background: var(--rel); }
.cell.same { background: var(--same); }
.cell.sel { background: var(--sel); }
.cell.err { color: var(--error); background: var(--error-soft); }
.cell.hint { animation: pulse .9s ease-in-out 2; }
.cell.flash { animation: flash .6s ease; }
.cell.pop { animation: pop .18s ease; }
.notes { position: absolute; inset: 1px; display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); }
.notes span { display: grid; place-items: center; font-size: clamp(9px, 2.7vw, 13px); color: var(--muted); font-weight: 600; line-height: 1; }
.notes span.hl { color: var(--accent); font-weight: 800; }
@keyframes pulse { 50% { background: #ffe08a; } }
@keyframes flash { 40% { background: var(--accent-soft); } }
@keyframes pop { 50% { transform: scale(1.15); } }
.paused { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; font-size: 20px; font-weight: 800; color: var(--accent); }
.paused[hidden] { display: none; }
.paused span { width: 72px; height: 72px; border-radius: 50%; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-size: 30px; }

.hintbar { margin-top: 10px; background: var(--accent-soft); color: var(--text); border-radius: 12px; padding: 10px 14px; font-size: 15px; }
.tools { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); margin: 14px 0 10px; }
.tool { position: relative; display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 6px 0; font-size: 14px; font-weight: 600; color: var(--muted); border-radius: 12px; }
.tool .ti { width: 48px; height: 48px; border-radius: 50%; background: var(--surface-2); display: grid; place-items: center; font-size: 22px; color: var(--text); }
.tool.on .ti { background: var(--accent); color: var(--accent-ink); }
.tool .badge, .tool .tag { position: absolute; top: 0; left: calc(50% + 12px); min-width: 22px; height: 22px; padding: 0 6px; border-radius: 11px; background: var(--accent); color: var(--accent-ink); font-size: 12px; font-weight: 800; display: grid; place-items: center; }
.tool .tag { background: var(--muted); color: var(--bg); font-size: 10px; }
.tool.on .tag { background: var(--accent); color: var(--accent-ink); }
.pad { display: grid; grid-template-columns: repeat(9, minmax(0, 1fr)); gap: 2px; }
.num { padding: 6px 0; font-size: clamp(30px, 9.5vw, 42px); font-weight: 400; color: var(--accent); border-radius: 10px; line-height: 1.25; }
.num:hover { background: var(--surface-2); }
.num.done { visibility: hidden; }
.pad.notes-mode .num { color: var(--muted); font-size: clamp(24px, 7vw, 32px); }

/* Modal */
#modal-root .backdrop { position: fixed; inset: 0; z-index: 50; background: rgba(10, 20, 35, .5); display: flex; align-items: flex-end; justify-content: center; animation: fade .15s; }
.sheet { width: 100%; max-width: 500px; max-height: 90vh; overflow: auto; background: var(--surface); border-radius: 24px 24px 0 0; padding: 26px 20px calc(env(safe-area-inset-bottom) + 20px); text-align: center; animation: up .2s ease; }
.sheet h2 { font-size: 26px; font-weight: 800; }
.m-icon { font-size: 52px; margin-bottom: 6px; }
.m-sub { color: var(--muted); margin: 8px 0 0; font-size: 16px; }
.sheet .actions { display: grid; gap: 10px; margin-top: 22px; }
.diff-list { display: grid; gap: 8px; margin-top: 18px; }
.diff-list button { display: flex; align-items: center; justify-content: space-between; padding: 18px; border-radius: 14px; background: var(--surface-2); font-size: 19px; }
.diff-list button span { color: var(--muted); font-size: 15px; }
.diff-list button:active { background: var(--accent-soft); }
.stats3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 18px; text-align: center; }
.stats3 > div { background: var(--surface-2); border-radius: 12px; padding: 12px 4px; }
.card.stats3 { margin-top: 0; padding: 8px; }
.stats3 b { display: block; font-size: 22px; font-variant-numeric: tabular-nums; }
.stats3 small { color: var(--muted); font-size: 13px; }
.vs { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 10px; margin-top: 18px; }
.vs .side { background: var(--surface-2); border-radius: 14px; padding: 12px 6px; display: grid; gap: 2px; }
.vs .side span { font-size: 28px; }
.vs .side b { font-size: 24px; }
.vs .side small { color: var(--muted); }
.vs .side.won { outline: 3px solid var(--accent); }
.vs-mid { font-weight: 900; color: var(--duel); }
.code { margin-top: 12px; font: 12px ui-monospace, Consolas, monospace; word-break: break-all; background: var(--surface-2); border-radius: 10px; padding: 10px; text-align: left; -webkit-user-select: all; user-select: all; }
@keyframes fade { from { opacity: 0; } }
@keyframes up { from { transform: translateY(40px); opacity: .5; } }

#toast { position: fixed; left: 50%; top: calc(env(safe-area-inset-top) + 14px); transform: translate(-50%, -20px); background: var(--text); color: var(--bg); padding: 12px 18px; border-radius: 14px; font-size: 15px; font-weight: 600; opacity: 0; pointer-events: none; transition: .2s; z-index: 60; max-width: calc(100% - 32px); }
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Friends / scores / me */
.code-input { display: flex; gap: 8px; margin-top: 12px; }
.code-input input { flex: 1; min-width: 0; padding: 12px 14px; border-radius: 12px; border: 1.5px solid var(--line); background: var(--surface); }
.tabs { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); background: var(--surface-2); padding: 4px; border-radius: 12px; margin-bottom: 12px; }
.tabs button { padding: 10px 0; border-radius: 9px; font-size: 15px; font-weight: 700; color: var(--muted); }
.tabs button.on { background: var(--surface); color: var(--accent); box-shadow: var(--shadow); }
.name-input { width: 100%; padding: 8px 10px; border-radius: 10px; border: 1.5px solid var(--line); background: var(--surface); font-weight: 700; font-size: 19px; }
.lvl { display: flex; align-items: center; gap: 10px; margin-top: 8px; font-size: 14px; font-weight: 700; color: var(--muted); }
.avatars { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 8px; margin-top: 14px; }
.avatars button { aspect-ratio: 1; border-radius: 12px; font-size: 26px; background: var(--surface-2); }
.avatars button.on { outline: 3px solid var(--accent); }
.ach-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; padding: 10px; }
.ach { text-align: center; padding: 8px 2px; border-radius: 12px; opacity: .35; filter: grayscale(1); }
.ach.on { opacity: 1; filter: none; background: var(--accent-soft); }
.ach span { font-size: 28px; display: block; }
.ach small { display: block; font-size: 11px; font-weight: 600; line-height: 1.2; margin-top: 2px; }
.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 52px; font-weight: 600; }
.switch-row + .switch-row { border-top: 1px solid var(--border); }
.switch { position: relative; width: 50px; height: 30px; border-radius: 99px; background: var(--line); flex: none; transition: background .2s; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.25); transition: transform .2s; }
.switch[aria-checked="true"] { background: var(--accent); }
.switch[aria-checked="true"]::after { transform: translateX(20px); }
.seg { display: inline-flex; background: var(--surface-2); padding: 3px; border-radius: 10px; }
.seg button { padding: 7px 12px; border-radius: 8px; font-size: 14px; font-weight: 700; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--accent); box-shadow: var(--shadow); }
