:root {
  --paper: #E6E8F4;
  --paper-deep: #D6D9EC;
  --card: #FBFBFE;
  --ink: #23286B;
  --ink-soft: #565C9C;
  --saffron: #F2AE1C;
  --saffron-deep: #C98A00;
  --pomegranate: #D6293E;
  --mint: #2FA58B;
  --mint-deep: #1D7A66;

  --font-display: 'Lalezar', 'Readex Pro', system-ui, sans-serif;
  --font-body: 'Readex Pro', system-ui, sans-serif;

  --step--1: 0.875rem;
  --step-0: 1rem;
  --step-1: 1.25rem;
  --step-2: 1.625rem;
  --step-3: 2.25rem;
  --step-5: clamp(4.5rem, 12vw, 8.5rem);

  --radius-tile: 10px;
  --radius-ui: 14px;
  --shadow-print: 4px 4px 0 var(--ink);

  --star: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='28' height='28' viewBox='0 0 28 28'%3E%3Cg fill='none' stroke='%231D7A66' stroke-width='1.4'%3E%3Crect x='7' y='7' width='14' height='14'/%3E%3Crect x='7' y='7' width='14' height='14' transform='rotate(45 14 14)'/%3E%3C/g%3E%3C/svg%3E");
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: var(--step-0);
  line-height: 1.6;
}

[hidden] { display: none !important; }

h1, h2, h3, p, ol { margin: 0; }

button, input { font: inherit; color: inherit; }

:focus-visible {
  outline: 3px solid var(--saffron);
  outline-offset: 3px;
}

.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

.muted { color: var(--ink-soft); }
.small { font-size: var(--step--1); }

.display {
  font-family: var(--font-display);
  font-weight: 400;
  font-size: var(--step-3);
  line-height: 1.15;
}

/* The wordmark is printed twice, slightly out of register. */
.wordmark {
  font-family: var(--font-display);
  font-weight: 400;
  line-height: 1;
  color: var(--ink);
  text-shadow: 0.06em 0.05em 0 var(--saffron);
}
.wordmark-xl { font-size: var(--step-5); }
.wordmark-sm { font-size: var(--step-2); }

/* Buttons */
.btn {
  --btn-bg: var(--card);
  --btn-fg: var(--ink);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .5em;
  min-height: 44px;
  padding: .55em 1.25em;
  border: 2px solid var(--ink);
  border-radius: 999px;
  background: var(--btn-bg);
  color: var(--btn-fg);
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 3px 0 var(--ink);
  transition: transform .08s, box-shadow .08s;
}
.btn:active:not(:disabled) { transform: translateY(3px); box-shadow: 0 0 0 var(--ink); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary { --btn-bg: var(--saffron); }
.btn-ink { --btn-bg: var(--ink); --btn-fg: var(--card); }
.btn-yes { --btn-bg: var(--mint); --btn-fg: #fff; }
.btn-no { --btn-bg: var(--pomegranate); --btn-fg: #fff; }
.btn-ghost { --btn-bg: transparent; box-shadow: none; border-color: transparent; text-decoration: underline; text-underline-offset: 4px; }
.btn-ghost:active:not(:disabled) { transform: none; }
.btn-sm { min-height: 36px; padding: .3em .9em; font-size: var(--step--1); }
.btn-lg { min-height: 60px; padding: .6em 2em; font-size: var(--step-1); }
.btn-block { width: 100%; }

.row { display: flex; flex-wrap: wrap; gap: .75rem; align-items: center; }

/* ---------- Tiles ---------- */
.tile {
  position: relative;
  display: block;
  width: 100%;
  aspect-ratio: 100 / 136;
  padding: 0;
  border: 0;
  background: none;
  perspective: 700px;
  cursor: pointer;
}
.tile-inner {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  transition: transform .5s cubic-bezier(.34, 1.3, .5, 1);
}
.tile.down .tile-inner { transform: rotateX(180deg); }
.tile-face, .tile-back {
  position: absolute;
  inset: 0;
  border: 2px solid var(--ink);
  border-radius: var(--radius-tile);
  overflow: hidden;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}
.tile-face {
  display: flex;
  flex-direction: column;
  background: var(--card);
}
.tile-face svg { display: block; width: 100%; flex: 1; min-height: 0; }
.tile-name {
  padding: .15em .25em .25em;
  background: var(--ink);
  color: var(--card);
  font-size: clamp(.7rem, 1.2vw, .9rem);
  font-weight: 500;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tile-back {
  transform: rotateX(180deg);
  display: grid;
  place-items: end center;
  padding-bottom: .4em;
  background: var(--mint) var(--star) center / 28px;
  color: rgba(255, 255, 255, .8);
  font-size: clamp(.65rem, 1.1vw, .8rem);
}
.tile-back span {
  padding: 0 .5em;
  border-radius: 999px;
  background: var(--mint-deep);
}

/* Lift on hover only for the board, and only where hover exists. */
@media (hover: hover) {
  .board .tile:not(.down):hover .tile-face { box-shadow: var(--shadow-print); transform: translate(-2px, -2px); }
  .board .tile-face { transition: transform .12s, box-shadow .12s; }
}

/* ---------- Name ---------- */
.welcome {
  min-height: 100vh;
  display: grid;
  grid-template-columns: minmax(0, 1.25fr) minmax(300px, 400px);
  gap: clamp(2rem, 6vw, 6rem);
  align-items: center;
  max-width: 1180px;
  margin: 0 auto;
  padding: 3rem clamp(16px, 4vw, 3rem);
}
.tagline {
  max-width: 28ch;
  margin-top: 1.25rem;
  font-size: var(--step-1);
  line-height: 1.5;
}
.teaser {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: .6rem;
  max-width: 560px;
  margin-top: 2.5rem;
}
.teaser .tile { cursor: default; }

.name-card {
  display: grid;
  gap: 1rem;
  padding: 1.75rem;
  background: var(--card);
  border: 2px solid var(--ink);
  border-radius: var(--radius-ui);
  box-shadow: 8px 8px 0 var(--ink);
}
.field { display: grid; gap: .35rem; font-weight: 500; }
.field input, .code-input {
  min-height: 48px;
  padding: .5em .9em;
  border: 2px solid var(--ink);
  border-radius: 10px;
  background: var(--card);
}
.field input:focus, .code-input:focus { outline: none; border-color: var(--saffron-deep); box-shadow: 0 0 0 3px var(--saffron); }
.form-hint { font-size: var(--step--1); color: var(--ink-soft); }
.form-error { min-height: 1.4em; font-size: var(--step--1); font-weight: 500; color: var(--pomegranate); }
.form-error:empty { display: none; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: .9rem clamp(16px, 3vw, 2rem);
  border-bottom: 2px solid var(--ink);
  background: var(--card);
}
.me { display: flex; align-items: center; gap: .9rem; flex-wrap: wrap; justify-content: flex-end; }
.me-name { font-weight: 700; }

/* ---------- Lobby ---------- */
.lobby-main {
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(280px, 1fr);
  gap: clamp(1.5rem, 4vw, 3.5rem);
  max-width: 1180px;
  margin: 0 auto;
  padding: clamp(1.5rem, 4vw, 3.5rem) clamp(16px, 3vw, 2rem);
}
.lobby-play { display: grid; gap: 1rem; align-content: start; justify-items: start; }
.lobby-play > .display { font-size: clamp(2.25rem, 5vw, 3.25rem); }
.lobby-split {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  width: 100%;
  margin-top: 1.5rem;
}
.lobby-box {
  display: grid;
  gap: .75rem;
  align-content: start;
  justify-items: start;
  padding: 1.25rem;
  border: 2px solid var(--ink);
  border-radius: var(--radius-ui);
  background: var(--card);
}
.size-picker {
  display: flex;
  align-items: center;
  gap: .6rem;
  margin: 0;
  padding: 0;
  border: 0;
}
/* A legend can't be a flex item, so float it into the row. */
.size-picker legend { float: inline-start; padding: 0; font-size: var(--step--1); color: var(--ink-soft); }
.size-options {
  display: inline-flex;
  padding: 2px;
  border: 1.5px solid var(--ink);
  border-radius: 999px;
}
.size-option { position: relative; cursor: pointer; }
.size-option input { position: absolute; opacity: 0; pointer-events: none; }
.size-option span {
  display: block;
  min-width: 2.4em;
  padding: .1em .6em;
  border-radius: 999px;
  font-size: var(--step--1);
  font-weight: 600;
  text-align: center;
}
.size-option input:checked + span { background: var(--ink); color: var(--card); }
.size-option input:focus-visible + span { outline: 3px solid var(--saffron); outline-offset: 2px; }

.lobby-box h3, .lobby-side h3 { font-size: var(--step-1); font-weight: 700; }
.code-input {
  width: 100%;
  font-family: var(--font-display);
  font-size: var(--step-2);
  letter-spacing: .3em;
  text-align: center;
  text-transform: uppercase;
}

.lobby-side { display: grid; gap: 2rem; align-content: start; }
.rules ol {
  display: grid;
  gap: .7rem;
  margin-top: .75rem;
  padding: 0;
  list-style: none;
  counter-reset: step;
}
.rules li {
  position: relative;
  padding-inline-start: 2.6rem;
  counter-increment: step;
}
.rules li::before {
  content: counter(step, arabic-indic);
  position: absolute;
  inset-inline-start: 0;
  top: -.1rem;
  display: grid;
  place-items: center;
  width: 1.9rem;
  height: 1.9rem;
  border: 2px solid var(--ink);
  border-radius: 50%;
  background: var(--saffron);
  font-weight: 700;
  font-size: var(--step--1);
}

/* ---------- Waiting ---------- */
.waiting {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 2rem 16px;
}
.waiting-card { display: grid; gap: 1.5rem; justify-items: center; text-align: center; max-width: 36rem; }
.code-tiles { display: flex; gap: .75rem; }
.code-tiles span {
  display: grid;
  place-items: center;
  width: clamp(64px, 17vw, 96px);
  aspect-ratio: 100 / 136;
  border: 2px solid var(--ink);
  border-radius: var(--radius-tile);
  background: var(--card);
  box-shadow: var(--shadow-print);
  font-family: var(--font-display);
  font-size: clamp(2.25rem, 8vw, 3.5rem);
}
.code-tiles.searching span {
  background: var(--mint) var(--star) center / 28px;
  animation: seek 1.6s ease-in-out infinite;
}
.code-tiles.searching span:nth-child(2) { animation-delay: .15s; }
.code-tiles.searching span:nth-child(3) { animation-delay: .3s; }
.code-tiles.searching span:nth-child(4) { animation-delay: .45s; }
@keyframes seek {
  0%, 60%, 100% { transform: none; }
  30% { transform: rotateX(180deg); }
}

/* ---------- Game ---------- */
.game { min-height: 100vh; display: flex; flex-direction: column; }
.opp { display: flex; align-items: center; gap: .9rem; font-size: var(--step--1); white-space: nowrap; }
.opp-name { font-weight: 700; font-size: var(--step-0); }
.opp-dot { width: .6rem; height: .6rem; border-radius: 50%; background: var(--mint); }
.opp-dot.off { background: var(--pomegranate); }
.opp-meter { display: flex; gap: 2px; direction: ltr; }
.opp-meter i { width: 5px; height: 14px; border-radius: 2px; background: var(--ink); }
.opp-meter i.out { background: var(--paper-deep); }

.game-main {
  flex: 1;
  display: grid;
  /* The board column is capped so all four rows fit on screen without scrolling:
     width per column ≈ row height / 1.36, plus padding. */
  --cols: 6;
  grid-template-columns: minmax(0, calc((100vh - 10rem) * var(--cols) / 5.55)) minmax(300px, 380px);
  justify-content: center;
  gap: clamp(1rem, 2.5vw, 2rem);
  width: 100%;
  margin: 0 auto;
  padding: clamp(1rem, 2.5vw, 2rem) clamp(16px, 2.5vw, 2rem);
}
.board-wrap { display: grid; gap: 1rem; align-content: start; }
.board {
  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
  gap: clamp(.4rem, 1vw, .8rem);
  padding: clamp(.6rem, 1.5vw, 1.2rem);
  border: 2px solid var(--ink);
  border-radius: 18px;
  background: var(--paper-deep);
}
.board.guessing { background: var(--saffron); }
.board.guessing .tile.down { opacity: .55; cursor: not-allowed; }
.board.locked .tile { cursor: default; }

.guess-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: .75rem 1rem;
  border: 2px solid var(--ink);
  border-radius: var(--radius-ui);
  background: var(--card);
  font-weight: 500;
}

.panel {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  min-height: 0;
  padding: 1.25rem;
  border: 2px solid var(--ink);
  border-radius: 18px;
  background: var(--card);
  align-self: start;
  position: sticky;
  top: 1rem;
  max-height: calc(100vh - 2rem);
}
.secret { display: grid; grid-template-columns: 88px 1fr; gap: 1rem; align-items: center; }
.secret-card .tile { cursor: default; }
.secret-card .tile-face { border-color: var(--saffron-deep); border-width: 3px; box-shadow: 3px 3px 0 var(--saffron); }
.secret-label { font-size: var(--step--1); color: var(--ink-soft); }
.secret-name { font-family: var(--font-display); font-size: var(--step-2); line-height: 1.2; }

.notice {
  padding: .6rem .8rem;
  border-radius: 10px;
  background: #FBE3E6;
  color: #8E1A26;
  font-size: var(--step--1);
  font-weight: 500;
}

.turn {
  font-family: var(--font-display);
  font-weight: 400;
  font-size: var(--step-2);
  line-height: 1.2;
  padding-top: .75rem;
  border-top: 2px solid var(--ink);
}
.turn.mine { color: var(--ink); }
.turn.theirs { color: var(--ink-soft); }

.log {
  flex: 1;
  min-height: 80px;
  overflow-y: auto;
  display: grid;
  gap: .5rem;
  align-content: start;
  padding: 0;
  list-style: none;
}
.log li {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: .25rem .75rem;
  align-items: start;
  padding: .55rem .75rem;
  border-radius: 10px;
  background: var(--paper);
}
.log li.theirs { background: transparent; border: 1px dashed var(--ink-soft); }
.log .who { grid-column: 1 / -1; font-size: .75rem; color: var(--ink-soft); }
.log .q { font-weight: 500; }
.ans {
  padding: 0 .6em;
  border-radius: 999px;
  font-size: var(--step--1);
  font-weight: 700;
  color: #fff;
  white-space: nowrap;
}
.ans.yes { background: var(--mint); }
.ans.no { background: var(--pomegranate); }
.ans.wait { background: var(--paper-deep); color: var(--ink-soft); }
.log .empty { background: transparent; color: var(--ink-soft); font-size: var(--step--1); }

.actions { display: grid; gap: .75rem; }
.ask-form { display: grid; gap: .6rem; }
.ask-form textarea {
  width: 100%;
  min-height: 72px;
  resize: vertical;
  padding: .6em .8em;
  border: 2px solid var(--ink);
  border-radius: 10px;
  background: var(--card);
  font: inherit;
  color: inherit;
}
.ask-form textarea:focus { outline: none; box-shadow: 0 0 0 3px var(--saffron); }
.chips { display: flex; flex-wrap: wrap; gap: .4rem; }
.chip {
  padding: .2em .7em;
  border: 1px solid var(--ink-soft);
  border-radius: 999px;
  background: transparent;
  font-size: .8rem;
  cursor: pointer;
}
.chip:hover { background: var(--paper); }
.question-big {
  padding: .9rem 1rem;
  border-inline-start: 4px solid var(--saffron);
  background: var(--paper);
  border-radius: 0 10px 10px 0;
  font-size: var(--step-1);
  font-weight: 500;
}
.answer-row { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; }
.answer-row .btn { min-height: 56px; font-size: var(--step-1); }

/* ---------- Dialogs ---------- */
.dialog {
  width: min(460px, calc(100vw - 32px));
  padding: 1.75rem;
  border: 2px solid var(--ink);
  border-radius: 18px;
  background: var(--card);
  color: var(--ink);
  box-shadow: 8px 8px 0 var(--ink);
  text-align: center;
}
.dialog::backdrop { background: rgba(35, 40, 107, .55); }
.dialog form, .dialog-result > div { display: grid; gap: 1rem; justify-items: center; }
.dialog .row { justify-content: center; }
.dialog-card { width: 120px; }
.dialog-card .tile { cursor: default; }
.result-title { font-family: var(--font-display); font-weight: 400; font-size: clamp(3rem, 12vw, 4.5rem); line-height: 1; }
.result-title.won { color: var(--mint-deep); text-shadow: .05em .05em 0 var(--saffron); }
.result-title.lost { color: var(--pomegranate); }
.result-cards { display: flex; gap: 1.5rem; justify-content: center; }
.result-cards figure { margin: 0; display: grid; gap: .4rem; width: 120px; font-size: var(--step--1); }
.result-cards figcaption { color: var(--ink-soft); }

/* ---------- Status ---------- */
.conn {
  position: fixed;
  inset: 0 0 auto;
  z-index: 10;
  padding: .5rem 1rem;
  background: var(--pomegranate);
  color: #fff;
  font-weight: 500;
  text-align: center;
}
.toast {
  position: fixed;
  inset-inline: 16px;
  bottom: 1.5rem;
  z-index: 10;
  width: fit-content;
  max-width: calc(100vw - 32px);
  margin: 0 auto;
  padding: .7rem 1.2rem;
  border-radius: 999px;
  background: var(--ink);
  color: var(--card);
  font-weight: 500;
  box-shadow: 0 4px 0 var(--saffron);
  opacity: 0;
  transform: translateY(1rem);
  pointer-events: none;
  transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: none; }

/* ---------- Responsive ---------- */
@media (max-width: 960px) {
  .welcome { grid-template-columns: 1fr; align-content: start; padding-top: 2.5rem; }
  .teaser { margin-top: 1.75rem; }
  .lobby-main { grid-template-columns: 1fr; }
  .game-main { grid-template-columns: minmax(0, 1fr); }
  /* On small screens the turn and its actions come before the board. */
  .panel { order: -1; position: static; max-height: none; padding: 1rem; }
  .log { max-height: 180px; }
}

@media (max-width: 640px) {
  .lobby-split { grid-template-columns: 1fr; }
  .board { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .teaser { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .teaser .tile:nth-child(n + 5) { display: none; }
  .opp-meter, .game .topbar .wordmark { display: none; }
  .secret { grid-template-columns: 64px 1fr; }
  .topbar .wordmark-sm { font-size: var(--step-1); }
}

@media (prefers-reduced-motion: reduce) {
  .tile-inner, .btn, .toast, .board .tile-face { transition: none; }
  .code-tiles.searching span { animation: none; }
}
