:root {
  --bg: #f5f2ea;
  --card: #fffdf8;
  --ink: #1f211d;
  --muted: #77766f;
  --line: #d9d4c8;
  --strong-line: #77736a;
  --accent: #5d6d52;
  --accent-soft: #e5eadf;
  --warm: #bb7857;
  --danger: #b94b4b;
  --blue: #4d6f87;
  --shadow: 0 12px 40px rgba(43, 41, 35, .08);
}

* { box-sizing: border-box; }
html { -webkit-tap-highlight-color: transparent; }
body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", sans-serif;
}
button, input, select { font: inherit; }
button { cursor: pointer; }

.app-shell { width: min(100%, 760px); margin: 0 auto; min-height: 100vh; padding-bottom: 94px; }
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 22px 18px 10px;
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(245,242,234,.92);
  backdrop-filter: blur(18px);
}
h1, h2, h3, p { margin-top: 0; }
h1 { font-size: 24px; letter-spacing: -.04em; margin-bottom: 0; }
h2 { font-size: 30px; letter-spacing: -.045em; margin-bottom: 8px; }
h3 { font-size: 18px; margin-bottom: 14px; }
.eyebrow { font-size: 11px; letter-spacing: .16em; font-weight: 800; color: var(--accent); margin-bottom: 6px; }
.muted { color: var(--muted); line-height: 1.45; }

.profile-pill, .status-badge {
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 999px;
  padding: 9px 13px;
  font-size: 12px;
  font-weight: 750;
  color: var(--ink);
}
.page { display: none; padding: 14px 16px 24px; }
.page.active { display: block; }
.section-heading { margin: 8px 2px 18px; }
.hero-card, .card, .race-card {
  background: var(--card);
  border: 1px solid rgba(217,212,200,.9);
  border-radius: 24px;
  box-shadow: var(--shadow);
}
.hero-card { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; padding: 20px; margin-bottom: 14px; }
.hero-card h2 { margin-bottom: 4px; }
.card { padding: 18px; }
.stack { display: grid; gap: 14px; }
.race-card { padding: 14px; margin-bottom: 14px; display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.racer { padding: 14px; border-radius: 18px; background: #f7f4ed; min-width: 0; }
.racer.me { background: var(--accent-soft); }
.racer strong { display: block; margin-bottom: 5px; }
.racer .big-stat { font-size: 19px; font-weight: 800; letter-spacing: -.03em; }
.racer small { color: var(--muted); }
.progress { height: 6px; background: rgba(31,33,29,.1); border-radius: 999px; margin-top: 10px; overflow: hidden; }
.progress > span { height: 100%; display: block; background: var(--accent); border-radius: inherit; }

label { display: block; font-size: 12px; font-weight: 750; margin: 0 0 7px 2px; color: var(--muted); }
select, input {
  width: 100%;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  border-radius: 14px;
  padding: 13px 14px;
  outline: none;
  margin-bottom: 12px;
}
input:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(93,109,82,.1); }
#joinCode { text-transform: uppercase; font-size: 21px; letter-spacing: .18em; font-weight: 800; text-align: center; }

.primary, .secondary, .ghost, .danger-button {
  border: 0;
  border-radius: 15px;
  min-height: 48px;
  padding: 12px 16px;
  font-weight: 800;
}
.primary { background: var(--ink); color: #fff; }
.secondary { background: var(--accent-soft); color: #34402e; }
.ghost { background: #f1eee6; color: var(--ink); }
.danger-button { background: #f9e8e5; color: #8f3d3d; }
.big { width: 100%; min-height: 54px; }
.action-stack { margin-top: 14px; display: grid; gap: 9px; }
.action-row { display: grid; grid-template-columns: repeat(2, 1fr); gap: 9px; }

.game-shell { margin-top: 14px; }
.game-topline { display: flex; align-items: center; justify-content: space-between; margin: 0 3px 10px; }
.timer { font-variant-numeric: tabular-nums; font-size: 19px; font-weight: 850; letter-spacing: .02em; }
.game-meta { color: var(--muted); font-size: 12px; }
.board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  width: 100%;
  aspect-ratio: 1;
  background: var(--strong-line);
  border: 2px solid var(--strong-line);
  border-radius: 8px;
  overflow: hidden;
  box-shadow: var(--shadow);
}
.cell {
  position: relative;
  border: 0;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  background: #fff;
  color: var(--blue);
  font-size: clamp(18px, 5vw, 28px);
  font-weight: 650;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.cell:nth-child(9n) { border-right: 0; }
.cell:nth-child(n+73) { border-bottom: 0; }
.cell.box-right { border-right: 2px solid var(--strong-line); }
.cell.box-bottom { border-bottom: 2px solid var(--strong-line); }
.cell.given { color: var(--ink); font-weight: 850; background: #fbfaf6; }
.cell.peer { background: #eef2eb; }
.cell.selected { background: #dce7d5; }
.cell.same-number { background: #e8ede3; }
.cell.wrong { color: var(--danger); background: #fae9e7; }
.cell.hinted { color: var(--warm); }
.notes {
  position: absolute;
  inset: 2px;
  display: grid;
  grid-template-columns: repeat(3,1fr);
  grid-template-rows: repeat(3,1fr);
  font-size: clamp(6px, 1.9vw, 10px);
  color: #66716c;
  font-weight: 650;
  line-height: 1;
}
.notes span { display: grid; place-items: center; }

.number-pad { display: grid; grid-template-columns: repeat(9, 1fr); gap: 5px; margin-top: 12px; }
.number-pad button { border: 0; border-radius: 12px; min-height: 45px; background: #fff; box-shadow: 0 2px 9px rgba(43,41,35,.06); font-size: 18px; font-weight: 800; color: var(--blue); }
.tool-row { display: grid; grid-template-columns: repeat(4,1fr); gap: 7px; margin-top: 8px; }
.tool-row button { border: 0; background: #ece9e1; min-height: 43px; border-radius: 12px; font-size: 12px; font-weight: 750; color: var(--ink); }
.tool-row button.active { background: var(--ink); color: #fff; }

.match-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.match-code { padding: 0; border: 0; background: transparent; font-weight: 900; font-size: 28px; letter-spacing: .14em; color: var(--ink); }
.countdown {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(31,33,29,.88);
  color: white;
  display: grid;
  place-items: center;
  font-size: min(32vw, 180px);
  font-weight: 950;
  backdrop-filter: blur(10px);
}

.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: min(100%, 760px);
  z-index: 40;
  display: grid;
  grid-template-columns: repeat(4,1fr);
  gap: 4px;
  padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
  background: rgba(255,253,248,.94);
  border-top: 1px solid var(--line);
  backdrop-filter: blur(20px);
}
.nav-item { border: 0; background: transparent; min-height: 54px; border-radius: 14px; color: var(--muted); display: grid; place-items: center; align-content: center; gap: 2px; }
.nav-item span { font-size: 19px; line-height: 1; }
.nav-item small { font-size: 10px; font-weight: 800; }
.nav-item.active { color: var(--ink); background: var(--accent-soft); }

.modal-backdrop { position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; padding: 20px; background: rgba(31,33,29,.52); backdrop-filter: blur(10px); }
.modal-card { width: min(100%, 430px); padding: 24px; background: var(--card); border-radius: 28px; box-shadow: 0 30px 80px rgba(0,0,0,.2); }
.profile-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 20px; }
.profile-choice { border: 0; min-height: 130px; border-radius: 22px; display: flex; flex-direction: column-reverse; justify-content: space-between; align-items: flex-start; padding: 16px; font-size: 17px; }
.profile-choice span { width: 50px; height: 50px; border-radius: 50%; display: grid; place-items: center; background: rgba(255,255,255,.65); font-size: 22px; font-weight: 900; }
.profile-choice.liam { background: #dde9ee; }
.profile-choice.sofi { background: #f0ddd8; }

.stats-score { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; text-align: center; gap: 12px; padding: 24px 14px; }
.score-person strong { display: block; font-size: 38px; line-height: 1; margin-bottom: 7px; }
.score-person span { font-size: 13px; font-weight: 800; }
.score-divider { font-size: 13px; color: var(--muted); font-weight: 800; }
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 12px; }
.stat-box { background: var(--card); border: 1px solid var(--line); border-radius: 18px; padding: 16px; }
.stat-box small { color: var(--muted); display: block; margin-bottom: 5px; }
.stat-box strong { font-size: 21px; letter-spacing: -.03em; }

.toast { position: fixed; z-index: 200; left: 50%; bottom: calc(88px + env(safe-area-inset-bottom)); transform: translateX(-50%); max-width: calc(100% - 30px); background: var(--ink); color: white; padding: 11px 15px; border-radius: 13px; font-size: 13px; font-weight: 700; box-shadow: var(--shadow); }
.hidden { display: none !important; }

@media (min-width: 640px) {
  .page { padding-left: 24px; padding-right: 24px; }
  .stack { grid-template-columns: 1fr 1fr; }
  .board { max-width: 590px; margin-inline: auto; }
  .number-pad, .tool-row, .game-topline { max-width: 590px; margin-left: auto; margin-right: auto; }
  .number-pad { margin-top: 12px; }
  .tool-row { margin-top: 8px; }
}
