@font-face {
  font-family: "Nunito";
  src: url("/static/fonts/nunito-latin-wght.woff2") format("woff2");
  font-weight: 200 1000;
  font-style: normal;
  font-display: swap;
}

:root {
  --bg: #efe6d6;          /* oat */
  --bg-deep: #e4d6bf;     /* sand */
  --surface: #f8f2e7;
  --tile: #fbf7f0;        /* warm off-white */
  --tile-edge: #d6c6ab;
  --ink: #3b2c20;         /* walnut, near black */
  --ink-soft: #6b5746;
  --correct: #5e7c50;     /* sage/moss */
  --correct-edge: #465f3b;
  --present: #d3ab4c;     /* muted mustard/ochre */
  --present-edge: #a7822f;
  --absent: #857a70;      /* warm clay grey */
  --absent-edge: #665d55;
  --neutral: #e2d6c2;
  --terracotta: #b65a37;
  --terracotta-deep: #924328;
  --walnut: #6b4a2f;
  --error: #9c3b23;
  --focus: #2f5d8a;
  --radius: 12px;
  --shadow-block: 0 2px 0 var(--tile-edge), 0 4px 10px rgba(80, 55, 30, .10);
  --font: "Nunito", ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI", sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  background-image: radial-gradient(circle at 20% 0%, #f4ecdf 0, transparent 50%),
                    radial-gradient(circle at 100% 100%, var(--bg-deep) 0, transparent 45%);
  color: var(--ink);
  font-family: var(--font);
  font-size: 17px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
[hidden] { display: none !important; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; border-radius: 8px; }
h1, h2, h3 { font-weight: 800; line-height: 1.15; margin: .2em 0 .4em; }
h1 { font-size: 1.9rem; color: var(--walnut); }
h2 { font-size: 1.25rem; color: var(--walnut); }
h3 { font-size: 1.05rem; color: var(--walnut); }
.muted { color: var(--ink-soft); }

#app {
  height: 100vh;
  height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.screen { height: 100%; }

/* ---------- buttons ---------- */
.btn {
  font: inherit; font-weight: 800; border: 0; cursor: pointer;
  border-radius: 14px; padding: .7em 1.4em; min-height: 48px;
  color: #fff; background: var(--terracotta);
  box-shadow: 0 3px 0 var(--terracotta-deep), 0 6px 14px rgba(120, 60, 30, .18);
  transition: transform .08s ease, box-shadow .08s ease;
}
.btn:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--terracotta-deep); }
.btn[disabled] { opacity: .6; cursor: default; }
.btn-big { font-size: 1.2rem; width: 100%; min-height: 56px; }
.linkbtn {
  font: inherit; background: none; border: 0; padding: .5em .4em; min-height: 44px;
  color: var(--ink-soft); text-decoration: underline; text-underline-offset: 3px;
  cursor: pointer; font-size: .9rem;
}

/* ---------- cards: login, intro ---------- */
.screen-loading { display: grid; place-items: center; }
.spin-sun { animation: spin 6s linear infinite; opacity: .8; }
@keyframes spin { to { transform: rotate(360deg); } }

.screen-card { display: flex; padding: 16px; overflow-y: auto; }
.screen-card > .card { margin: auto; }  /* centres, but scrolls from the top when taller than the screen */
.card {
  width: 100%; max-width: 440px;
  background: var(--surface);
  border-radius: 22px;
  padding: 26px 22px 22px;
  box-shadow: 0 3px 0 var(--tile-edge), 0 18px 40px rgba(80, 55, 30, .14);
  text-align: center;
  position: relative;
}
.card .lead { font-size: 1.1rem; color: var(--ink-soft); margin-top: 0; }
.deco-sun { display: block; margin: -4px auto 4px; }
.deco-blocks { display: block; margin: 0 auto 6px; }
.deco-leaves { display: block; margin: 18px auto 0; opacity: .9; }

#code {
  width: 100%; font: inherit; font-size: 1.45rem; font-weight: 800;
  letter-spacing: .12em; text-align: center; text-transform: uppercase;
  padding: .7em .5em; min-height: 64px;
  border: 2px solid var(--tile-edge); border-radius: 14px;
  background: var(--tile); color: var(--ink);
  box-shadow: inset 0 2px 4px rgba(80, 55, 30, .08);
}
#code::placeholder { letter-spacing: normal; text-transform: none; font-weight: 600; color: #9d8c7a; }
#code:focus-visible { outline-offset: 0; border-color: var(--focus); }
.error { color: var(--error); font-weight: 700; min-height: 1.5em; margin: .6em 0; }

.rules {
  font-weight: 800; font-size: 1.15rem; color: var(--walnut);
  background: var(--bg); border-radius: 12px; padding: .6em .8em;
}
.legend { list-style: none; padding: 0; margin: 0 0 1em; text-align: left; }
.legend li { display: flex; align-items: center; gap: 12px; margin: 8px 0; }
#screen-intro .card > p { margin: .6em 0; }
#screen-intro .btn-big { margin-top: 10px; }

/* ---------- tiles ---------- */
.tile {
  position: relative;
  display: grid; place-items: center;
  background: var(--tile);
  border: 2px solid var(--tile-edge);
  border-radius: 10px;
  box-shadow: var(--shadow-block);
  font-weight: 900; color: var(--ink);
  text-transform: uppercase;
  user-select: none;
}
.tile-mini { width: 40px; height: 40px; flex: 0 0 40px; font-size: 1.2rem; }
.t-c { background: var(--correct); border-color: var(--correct-edge); color: #fff; box-shadow: 0 2px 0 var(--correct-edge), 0 4px 10px rgba(50, 70, 40, .18); }
.t-p { background: var(--present); border-color: var(--present-edge); color: var(--ink); box-shadow: 0 2px 0 var(--present-edge), 0 4px 10px rgba(120, 90, 30, .18); }
.t-a { background: var(--absent); border-color: var(--absent-edge); color: #fff; box-shadow: 0 2px 0 var(--absent-edge), 0 4px 10px rgba(60, 50, 40, .15); }
.t-n { background: var(--neutral); border-color: var(--tile-edge); }
/* Shape markers so colour is never the only signal: ✓ correct, ● present, – absent. */
.t-c::after, .t-p::after, .t-a::after {
  position: absolute; top: 2px; right: 4px;
  font-size: max(9px, calc(var(--th, 40px) * .26)); line-height: 1; font-weight: 900;
}
.t-c::after { content: "✓"; color: #fff; }
.t-p::after { content: "●"; color: var(--ink); font-size: max(7px, calc(var(--th, 40px) * .2)); }
.t-a::after { content: "–"; color: #fff; }
.tile-mini.t-c::after, .tile-mini.t-p::after, .tile-mini.t-a::after { --th: 36px; }

/* ---------- main layout ---------- */
.screen-main { display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 10px 4px; min-height: 52px;
}
.brand { display: none; align-items: center; gap: 8px; font-weight: 900; font-size: 1.2rem; color: var(--walnut); }
.tabs { display: flex; gap: 6px; flex: 1; }
.tabs button {
  font: inherit; font-weight: 800; font-size: 1.02rem;
  flex: 1; max-width: 160px; min-height: 44px;
  border: 0; border-radius: 12px; cursor: pointer;
  background: transparent; color: var(--ink-soft);
}
.tabs button[aria-selected="true"] {
  background: var(--surface); color: var(--walnut);
  box-shadow: 0 2px 0 var(--tile-edge), 0 3px 8px rgba(80, 55, 30, .10);
}
.meta { display: flex; align-items: center; gap: 4px; margin-left: auto; }
.timer {
  font-variant-numeric: tabular-nums; font-weight: 800; color: var(--walnut);
  background: var(--surface); border-radius: 999px; padding: 3px 10px; font-size: .95rem;
}
.panels { flex: 1; min-height: 0; display: grid; }
.panel { min-height: 0; min-width: 0; }
.screen-main[data-panel="game"] .panel-board,
.screen-main[data-panel="board"] .panel-game { display: none; }

/* ---------- game panel ---------- */
.panel-game { display: flex; flex-direction: column; padding: 0 8px 6px; }
.result {
  text-align: center; background: var(--surface); border-radius: 14px;
  padding: 6px 10px; margin: 2px 0 6px; box-shadow: 0 2px 0 var(--tile-edge);
}
.result p { margin: 0 0 4px; font-weight: 700; }
.result .linkbtn { min-height: 36px; padding: .2em .4em; }
.name-tiles { display: flex; justify-content: center; gap: 6px; }
.name-tiles .tile { --th: 34px; width: 34px; height: 34px; font-size: 1.15rem; }

.board-wrap {
  flex: 1; min-height: 0; position: relative;
  container-type: size;
  display: grid; place-items: center;
}
.board {
  --gap: clamp(3px, 1.1cqh, 7px);
  --th: min(calc((100cqh - 9 * var(--gap)) / 10), 62px);
  --tw: min(calc((100cqw - 5 * var(--gap)) / 6), calc(var(--th) * 1.3), 70px);
  display: grid;
  grid-template-columns: repeat(6, var(--tw));
  grid-auto-rows: var(--th);
  gap: var(--gap);
}
.board .row { display: contents; }
.board .tile { font-size: calc(var(--th) * .56); border-radius: min(10px, calc(var(--th) * .22)); }
.board .tile.filled { border-color: #b9a584; animation: pop .12s ease-out; }
.board .row.current .tile.cursor { border-color: var(--terracotta); }
.board .row.shake { animation: none; }
.board .row.shake .tile { animation: shake .4s ease; }
.board .tile.flip { animation: flip .45s ease both; }
@keyframes pop { 50% { transform: scale(1.08); } }
@keyframes shake { 20%, 60% { transform: translateX(-5px); } 40%, 80% { transform: translateX(5px); } }
@keyframes flip { 0% { transform: rotateX(0); } 50% { transform: rotateX(90deg); } 100% { transform: rotateX(0); } }

.toast {
  position: absolute; left: 50%; bottom: 6px; transform: translateX(-50%);
  width: max-content; max-width: calc(100% - 16px);
  margin: 0; padding: .55em 1em; border-radius: 12px;
  background: var(--walnut); color: #fff; font-weight: 700; text-align: center;
  box-shadow: 0 6px 18px rgba(60, 40, 20, .25);
  opacity: 0; pointer-events: none; transition: opacity .2s ease;
  z-index: 5;
}
.toast.show { opacity: 1; }

/* ---------- keyboard ---------- */
.keyboard {
  --kh: clamp(44px, 7dvh, 58px);
  width: 100%; max-width: 540px; margin: 4px auto 0;
  display: grid; grid-template-columns: repeat(8, 1fr); grid-auto-rows: var(--kh);
  user-select: none; touch-action: manipulation;
}
.key {
  --key-bg: var(--tile); --key-edge: var(--tile-edge); --key-ink: var(--ink);
  position: relative; font: inherit; font-weight: 800; font-size: 1.22rem;
  border: 0; padding: 0; margin: 0; background: none; color: var(--key-ink);
  cursor: pointer; min-width: 44px; min-height: 44px;
}
/* The visible key is inset; the whole grid cell is the touch target. */
.key::before {
  content: ""; position: absolute; inset: 3px 2.5px;
  background: var(--key-bg); border-radius: 10px;
  box-shadow: 0 3px 0 var(--key-edge), 0 4px 8px rgba(80, 55, 30, .10);
  transition: transform .06s ease;
}
.key > span { position: relative; pointer-events: none; }
.key:active::before { transform: translateY(2px); box-shadow: 0 1px 0 var(--key-edge); }
.key:focus-visible { outline: none; }
.key:focus-visible::before { outline: 3px solid var(--focus); outline-offset: 1px; }
.key.k-c { --key-bg: var(--correct); --key-edge: var(--correct-edge); --key-ink: #fff; }
.key.k-p { --key-bg: var(--present); --key-edge: var(--present-edge); --key-ink: var(--ink); }
.key.k-a { --key-bg: var(--absent); --key-edge: var(--absent-edge); --key-ink: #fff; }
.key.k-c::after, .key.k-p::after, .key.k-a::after {
  position: absolute; top: 6px; right: 7px; font-size: 10px; line-height: 1;
}
.key.k-c::after { content: "✓"; color: #fff; }
.key.k-p::after { content: "●"; color: var(--ink); font-size: 8px; }
.key.k-a::after { content: "–"; color: #fff; }
.key-enter { grid-column: span 3; --key-bg: var(--terracotta); --key-edge: var(--terracotta-deep); --key-ink: #fff; font-size: 1.1rem; }
.key-back svg { width: 26px; height: 26px; }
.keyboard[aria-disabled="true"] { opacity: .55; pointer-events: none; }

/* ---------- leaderboard ---------- */
.panel-board { overflow-y: auto; padding: 4px 14px 20px; -webkit-overflow-scrolling: touch; }
.lb-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.lb-head h2 { margin: 0; }
.seg { display: inline-flex; background: var(--bg-deep); border-radius: 12px; padding: 3px; }
.seg button {
  font: inherit; font-weight: 800; font-size: .92rem; border: 0; border-radius: 10px;
  min-height: 44px; padding: 0 12px; background: transparent; color: var(--ink-soft); cursor: pointer;
}
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--walnut); box-shadow: 0 2px 0 var(--tile-edge); }
.lb-list { list-style: none; padding: 0; margin: 0; }
.lb-item {
  display: grid; grid-template-columns: 2.2em 1fr auto; align-items: center; gap: 4px 10px;
  background: var(--surface); border-radius: 14px; padding: 10px 12px; margin-bottom: 8px;
  box-shadow: 0 2px 0 var(--tile-edge);
}
.lb-item.me { outline: 2px solid var(--terracotta); outline-offset: -2px; }
.lb-rank {
  font-weight: 900; font-size: 1.1rem; color: var(--walnut);
  width: 2em; height: 2em; border-radius: 9px; display: grid; place-items: center; background: var(--bg-deep);
}
.lb-item:nth-child(1) .lb-rank { background: var(--present); color: var(--ink); }
.lb-name { font-weight: 800; overflow-wrap: anywhere; }
.lb-name small { font-weight: 700; color: var(--terracotta-deep); margin-left: .3em; }
.lb-stats { color: var(--ink-soft); font-size: .92rem; text-align: right; font-variant-numeric: tabular-nums; }
.lb-stats strong { color: var(--ink); }
.lb-grid { grid-column: 2 / -1; display: flex; flex-direction: column; gap: 2px; }
.lb-losers .lb-item { grid-template-columns: 1fr auto; }
.lb-losers .lb-grid { grid-column: 1 / -1; }
.mini-row { display: flex; gap: 2px; }
/* Mini grid uses shape as well as colour: square = correct, circle = present, small bar = absent. */
.mini { width: 11px; height: 11px; border-radius: 2px; background: transparent; display: grid; place-items: center; }
.mini::before { content: ""; display: block; }
.mini.m-c::before { width: 11px; height: 11px; border-radius: 2px; background: var(--correct); }
.mini.m-p::before { width: 10px; height: 10px; border-radius: 50%; background: var(--present); box-shadow: inset 0 0 0 1px var(--present-edge); }
.mini.m-a::before { width: 9px; height: 4px; border-radius: 2px; background: var(--absent); }
.mini.m-n::before { width: 9px; height: 9px; border-radius: 2px; border: 1.5px solid var(--tile-edge); }
.lb-losers { margin-top: 22px; }
.lb-losers h3 { margin-bottom: 0; }
.lb-losers > p { margin-top: .2em; }
.lb-counts { margin-top: 16px; text-align: center; }
.lb-deco { opacity: .7; }

/* ---------- celebration ---------- */
.celebrate {
  position: fixed; inset: 0; z-index: 20;
  display: grid; place-items: center; padding: 16px;
  background: rgba(239, 230, 214, .88);
  -webkit-backdrop-filter: blur(3px); backdrop-filter: blur(3px);
  overflow: hidden;
}
.celebrate-card { animation: rise .6s ease both; max-height: 100%; overflow-y: auto; }
.celebrate-card .lead { margin-bottom: 10px; }
.big-name {
  font-size: clamp(3.2rem, 17vw, 4.4rem); font-weight: 900; line-height: 1.05;
  color: var(--terracotta-deep); margin: 4px 0 10px; letter-spacing: .02em;
  animation: drop .6s cubic-bezier(.3, 1.4, .5, 1) .25s both;
}
.celebrate .linkbtn { margin-top: 6px; }
.falling { position: absolute; inset: 0; pointer-events: none; }
.falling span {
  position: absolute; top: -40px; width: 22px; height: 22px;
  background: url("/static/img/leaf.svg") center / contain no-repeat;
  animation: fall linear infinite;
  opacity: .85;
}
@keyframes rise { from { opacity: 0; transform: translateY(18px) scale(.97); } }
@keyframes drop { from { opacity: 0; transform: translateY(-24px) rotate(-6deg); } }
@keyframes fall {
  0% { transform: translate(0, 0) rotate(0); }
  50% { transform: translate(28px, 55vh) rotate(160deg); }
  100% { transform: translate(-10px, 110vh) rotate(320deg); }
}

/* ---------- wide screens: both panels side by side ---------- */
@media (min-width: 900px) {
  .brand { display: flex; }
  .tabs { display: none; }
  .topbar { padding: 10px 24px 6px; }
  .panels {
    grid-template-columns: minmax(380px, 600px) minmax(320px, 440px);
    justify-content: center; gap: 28px; padding: 0 24px 16px;
  }
  .screen-main[data-panel] .panel-board,
  .screen-main[data-panel] .panel-game { display: flex; }
  .panel-board {
    flex-direction: column; background: rgba(248, 242, 231, .6); border-radius: 20px;
    padding: 16px 18px; margin-top: 4px;
  }
  .keyboard { --kh: clamp(46px, 6.4dvh, 58px); }
}

@media (max-height: 620px) {
  .keyboard { --kh: 44px; }
  .topbar { min-height: 46px; padding-top: 2px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition: none !important; }
  .falling { display: none; }
  .spin-sun { animation: none; }
}

/* Revealed name: wooden-block colours, purely decorative (the name is also written as text). */
.name-tiles .tile { color: #fff; }
.name-tiles .tile:nth-child(3n+1) { background: #c2714f; border-color: #8f4f33; box-shadow: 0 2px 0 #8f4f33; }
.name-tiles .tile:nth-child(3n+2) { background: var(--correct); border-color: var(--correct-edge); box-shadow: 0 2px 0 var(--correct-edge); }
.name-tiles .tile:nth-child(3n+3) { background: var(--present); border-color: var(--present-edge); box-shadow: 0 2px 0 var(--present-edge); color: var(--ink); }
#code.shake { animation: shake .4s ease; }
