/* Knight Kids — a bright, chunky, cartoon look aimed at young players. */

:root {
  --bg-deep: #2a1b4f;
  --bg-mid: #3d2670;
  --panel: #fffaf2;
  --panel-2: #fff2dd;
  --ink: #3a2c50;
  --ink-soft: #6b5b85;

  --sun: #ffc542;
  --coral: #ff6b6b;
  --mint: #4ecdc4;
  --sky: #5aa9e6;
  --grape: #9b6bde;
  --leaf: #58b09c;

  --sq-light: #ffe9c2;
  --sq-dark: #58b09c;
  --sq-light-hi: #ffd977;
  --sq-dark-hi: #4a9c88;

  --hint-arrow: #ff6b6b;
  --radius: 22px;
  --shadow: 0 10px 0 rgba(0, 0, 0, .14), 0 18px 34px rgba(0, 0, 0, .22);
  --shadow-sm: 0 4px 0 rgba(0, 0, 0, .12);

  --font: 'Baloo 2', 'Nunito', 'Comic Sans MS', 'Trebuchet MS', system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: var(--font);
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  color: var(--ink);
  background:
    radial-gradient(1100px 700px at 12% -10%, #5a3a9e 0%, transparent 60%),
    radial-gradient(900px 620px at 105% 10%, #2f6fb0 0%, transparent 55%),
    linear-gradient(160deg, var(--bg-mid), var(--bg-deep));
  overflow: hidden;
  user-select: none;
  -webkit-font-smoothing: antialiased;
}

#app {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding:
    max(14px, env(safe-area-inset-top))
    max(20px, env(safe-area-inset-right))
    max(18px, env(safe-area-inset-bottom))
    max(20px, env(safe-area-inset-left));
  gap: 14px;
}

/* ---------------------------------------------------------------- topbar */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  color: #fff;
}

.brand-mark {
  width: 52px;
  height: 52px;
  display: grid;
  place-items: center;
  font-size: 30px;
  color: #43306b;
  background: linear-gradient(160deg, var(--sun), #ff9f43);
  border-radius: 16px;
  box-shadow: var(--shadow-sm);
  transform: rotate(-6deg);
}

.brand h1 {
  margin: 0;
  font-size: 26px;
  letter-spacing: .3px;
  text-shadow: 0 3px 0 rgba(0, 0, 0, .25);
}

.tagline {
  margin: 2px 0 0;
  font-size: 12.5px;
  opacity: .78;
}

.topbar-controls {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

/* --------------------------------------------------------------- buttons */

.btn {
  font-family: inherit;
  font-size: 14.5px;
  font-weight: 700;
  color: var(--ink);
  border: none;
  border-radius: 16px;
  padding: 11px 16px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  background: var(--panel);
  box-shadow: 0 4px 0 rgba(0, 0, 0, .18);
  transition: transform .12s ease, box-shadow .12s ease, background .15s ease;
}

.btn:hover { transform: translateY(-2px); box-shadow: 0 6px 0 rgba(0, 0, 0, .18); }
.btn:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(0, 0, 0, .18); }
.btn:disabled { opacity: .5; cursor: not-allowed; transform: none; }
.btn:focus-visible { outline: 3px solid var(--sun); outline-offset: 3px; }

.btn-emoji { font-size: 17px; line-height: 1; }

.btn-ghost {
  background: rgba(255, 255, 255, .16);
  color: #fff;
  backdrop-filter: blur(4px);
}
.btn-ghost:hover { background: rgba(255, 255, 255, .26); }
.btn-ghost[aria-pressed="false"] { opacity: .6; }

.btn-primary { background: linear-gradient(160deg, var(--sun), #ff9f43); }

.btn-hint {
  width: 100%;
  justify-content: center;
  font-size: 16px;
  padding: 14px;
  background: linear-gradient(160deg, #ffe27a, var(--sun));
}
.btn-hint.thinking { animation: pulse 1s ease-in-out infinite; }

.btn-soft { background: var(--panel-2); flex: 1; justify-content: center; }
.btn-soft[aria-pressed="true"] {
  background: linear-gradient(160deg, var(--mint), #3bb5ad);
  color: #10403c;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.03); }
}

/* ---------------------------------------------------------------- layout */

.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 400px;
  gap: 20px;
}

/*
 * --board-size lives here, on the common ancestor, so the board and both
 * player strips inherit the same value. The `100%` term keeps the board
 * inside its column on narrow screens, which is what stopped phones from
 * scrolling sideways.
 */
.board-column {
  --board-size: min(62vh, 620px, 100%);
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

/* --------------------------------------------------------- player strips */

.player-strip {
  width: var(--board-size);
  max-width: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 14px;
  border-radius: 18px;
  background: rgba(255, 255, 255, .12);
  color: #fff;
  transition: background .2s ease, box-shadow .2s ease;
}

.player-strip.active {
  background: rgba(255, 255, 255, .24);
  box-shadow: 0 0 0 3px var(--sun);
}

.avatar {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  font-size: 25px;
  background: rgba(255, 255, 255, .2);
  border-radius: 13px;
}

.player-meta { flex: 1; min-width: 0; }
.player-name { display: block; font-weight: 700; font-size: 15px; }

.captured-tray {
  display: flex;
  flex-wrap: wrap;
  gap: 1px;
  min-height: 20px;
  align-items: center;
}
.captured-tray .piece-icon { opacity: .92; }
.captured-tray .tray-score {
  font-size: 12px;
  font-weight: 700;
  margin-left: 5px;
  opacity: .85;
}

.turn-dot {
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: rgba(255, 255, 255, .25);
}
.player-strip.active .turn-dot {
  background: var(--sun);
  box-shadow: 0 0 12px var(--sun);
  animation: pulse 1.4s ease-in-out infinite;
}

/* ----------------------------------------------------------------- board */

/*
 * Needs an explicit width. The frame's own width contains a `100%` term, so
 * leaving this to shrink-to-fit makes the percentage circular — the frame
 * then resolves against the full column and sits flush left instead of
 * centred, clipping the rank labels.
 */
.board-wrap {
  position: relative;
  width: 100%;
  display: flex;
  justify-content: center;
}

.board-frame {
  position: relative;
  width: var(--board-size);
  height: var(--board-size);
  padding: 0;
  border-radius: 20px;
  background: linear-gradient(160deg, #8c6239, #6d4a2a);
  box-shadow: var(--shadow);
  border: 9px solid #a97c50;
}

.board {
  width: 100%;
  height: 100%;
  touch-action: none;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border-radius: 8px;
  overflow: hidden;
}

.square {
  position: relative;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: background-color .15s ease;
}

.square.light { background: var(--sq-light); }
.square.dark { background: var(--sq-dark); }
.square.light.last-move { background: var(--sq-light-hi); }
.square.dark.last-move { background: var(--sq-dark-hi); }

.square.selected::after {
  content: '';
  position: absolute;
  inset: 3px;
  border: 4px solid var(--sun);
  border-radius: 10px;
  pointer-events: none;
}

/* A dot means "you can move here"; a ring means "you can capture here". */
.square .target-dot {
  position: absolute;
  width: 30%;
  height: 30%;
  border-radius: 50%;
  background: rgba(60, 40, 20, .32);
  pointer-events: none;
  animation: dot-in .18s ease;
}
.square .target-ring {
  position: absolute;
  inset: 5%;
  border: 6px solid rgba(255, 107, 107, .75);
  border-radius: 50%;
  pointer-events: none;
  animation: dot-in .18s ease;
}

@keyframes dot-in { from { transform: scale(.3); opacity: 0; } to { transform: scale(1); opacity: 1; } }

.square.check-square::before {
  content: '';
  position: absolute;
  inset: 0;
  background: radial-gradient(circle, rgba(255, 60, 60, .85) 0%, rgba(255, 60, 60, 0) 70%);
  animation: pulse 1s ease-in-out infinite;
  pointer-events: none;
}

.square.hint-from { box-shadow: inset 0 0 0 5px rgba(255, 107, 107, .8); }
.square.hint-to { box-shadow: inset 0 0 0 5px rgba(255, 107, 107, .8); }

.piece {
  width: 92%;
  height: 92%;
  pointer-events: none;
  transition: transform .12s ease;
  filter: drop-shadow(0 3px 2px rgba(0, 0, 0, .28));
}
.piece svg { width: 100%; height: 100%; display: block; }

.square.can-lift .piece { cursor: grab; }
.square:hover .piece.liftable { transform: translateY(-3px) scale(1.04); }
.piece.dragging { opacity: .35; }

.piece-arrive { animation: arrive .28s cubic-bezier(.2, .9, .3, 1.4); }
@keyframes arrive {
  0% { transform: scale(1.18); }
  60% { transform: scale(.94); }
  100% { transform: scale(1); }
}

.drag-ghost {
  position: fixed;
  width: 68px;
  height: 68px;
  pointer-events: none;
  z-index: 90;
  transform: translate(-50%, -50%) scale(1.12);
  filter: drop-shadow(0 8px 8px rgba(0, 0, 0, .4));
}
.drag-ghost svg { width: 100%; height: 100%; }

.pc-white { --pc-body: #fff7e8; --pc-shade: #e6cfa4; --pc-line: #7a5230; --pc-detail: #ffc542; --pc-shadow: #000; }
.pc-black { --pc-body: #6a5495; --pc-shade: #493a6d; --pc-line: #2b2044; --pc-detail: #ffc542; --pc-shadow: #000; }

.piece-icon { display: inline-block; vertical-align: middle; }
.piece-icon svg { width: 100%; height: 100%; display: block; }

/* Coordinates around the board */
.coords {
  position: absolute;
  display: flex;
  color: #f4e2c6;
  font-size: 11px;
  font-weight: 700;
  opacity: .85;
  pointer-events: none;
}
.coords-rank {
  flex-direction: column;
  justify-content: space-around;
  left: -21px;
  top: 0;
  bottom: 0;
  align-items: center;
  width: 16px;
}
.coords-file {
  justify-content: space-around;
  bottom: -20px;
  left: 0;
  right: 0;
  height: 16px;
  align-items: center;
}

.board-overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 5;
}
.hint-arrow-line {
  stroke: var(--hint-arrow);
  stroke-width: .17;
  stroke-linecap: round;
  fill: none;
  opacity: .9;
  marker-end: url(#arrowhead);
  animation: arrow-in .35s ease;
}
@keyframes arrow-in { from { opacity: 0; stroke-width: .02; } to { opacity: .9; stroke-width: .17; } }

/* ----------------------------------------------------------- coach panel */

.coach-column {
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 14px;
  overflow: hidden;
}

.coach-card, .panel {
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}

/* The coach gets the leftover space — its explanations are the whole point. */
.panel-grow {
  flex: 0 0 auto;
  max-height: 190px;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.coach-card {
  display: flex;
  flex-direction: column;
  gap: 12px;
  flex: 1 1 auto;
  min-height: 240px;
}

.coach-head { display: flex; gap: 12px; align-items: flex-start; }

.mascot {
  width: 54px;
  height: 54px;
  flex: none;
  display: grid;
  place-items: center;
  font-size: 33px;
  background: linear-gradient(160deg, #ffe9b8, var(--sun));
  border-radius: 18px;
  box-shadow: var(--shadow-sm);
}
.mascot.thinking { animation: bob .8s ease-in-out infinite; }
@keyframes bob { 0%, 100% { transform: translateY(0) rotate(-3deg); } 50% { transform: translateY(-5px) rotate(3deg); } }

.speech {
  position: relative;
  flex: 1;
  background: var(--panel-2);
  border-radius: 16px;
  padding: 11px 13px;
  font-size: 14.5px;
  line-height: 1.45;
}
.speech::before {
  content: '';
  position: absolute;
  left: -8px;
  top: 18px;
  border: 8px solid transparent;
  border-right-color: var(--panel-2);
  border-left: 0;
}
.speech p { margin: 0; }
.speech.tone-great { background: #dcf6e4; }
.speech.tone-good { background: #e2f0ff; }
.speech.tone-warn { background: #fff0d6; }
.speech.tone-bad { background: #ffe1e1; }

.coach-actions { display: flex; flex-direction: column; gap: 9px; }
.coach-actions-row { display: flex; gap: 9px; }

.coach-body { overflow-y: auto; min-height: 0; padding-right: 4px; }

.reason {
  display: flex;
  gap: 10px;
  padding: 10px 11px;
  border-radius: 14px;
  background: var(--panel-2);
  margin-bottom: 8px;
  animation: slide-in .3s ease backwards;
}
.reason.warn { background: #ffeaea; }
.reason-icon { font-size: 20px; line-height: 1.2; flex: none; }
.reason-title { font-weight: 800; font-size: 14px; margin: 0 0 2px; }
.reason-text { margin: 0; font-size: 13.5px; line-height: 1.5; color: var(--ink-soft); }

@keyframes slide-in { from { opacity: 0; transform: translateX(14px); } to { opacity: 1; transform: none; } }

.coach-headline {
  font-size: 16px;
  font-weight: 800;
  margin: 0 0 10px;
  padding: 11px 13px;
  border-radius: 14px;
  background: linear-gradient(160deg, #ffe9b8, var(--sun));
  display: flex;
  gap: 9px;
  align-items: center;
}
.coach-headline .score-chip {
  margin-left: auto;
  font-size: 12px;
  background: rgba(0, 0, 0, .18);
  color: #fff;
  padding: 3px 9px;
  border-radius: 999px;
}

.plan { margin: 6px 0 8px; }
.plan h3, .section-title {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .6px;
  color: var(--ink-soft);
  margin: 12px 0 6px;
}
.plan-step {
  display: flex;
  gap: 9px;
  align-items: baseline;
  font-size: 13.5px;
  padding: 5px 0;
  border-bottom: 1px dashed #e8dcc8;
}
.plan-step .san {
  font-weight: 800;
  background: #efe4d2;
  border-radius: 7px;
  padding: 1px 7px;
  font-size: 12.5px;
  flex: none;
}
.plan-step.theirs .san { background: #ded4ef; }

.concept-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--mint);
  color: #10403c;
  font-weight: 700;
  font-size: 13px;
  border: none;
  border-radius: 999px;
  padding: 7px 13px;
  margin: 3px 5px 3px 0;
  cursor: pointer;
  font-family: inherit;
  box-shadow: 0 3px 0 rgba(0, 0, 0, .15);
}
.concept-chip:hover { transform: translateY(-1px); }

.alt {
  font-size: 13px;
  color: var(--ink-soft);
  padding: 6px 0;
}
.alt .san { font-weight: 800; color: var(--ink); }

.tips { display: flex; flex-direction: column; gap: 7px; }
.tip {
  display: flex;
  gap: 9px;
  font-size: 13.5px;
  line-height: 1.45;
  background: var(--panel-2);
  border-radius: 13px;
  padding: 9px 11px;
}

/* ---------------------------------------------------------- level picker */

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
  gap: 10px;
}
.panel-head h2 {
  margin: 0;
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: .7px;
  color: var(--ink-soft);
}

.level-picker { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }

.level-btn {
  font-family: inherit;
  cursor: pointer;
  border: 3px solid transparent;
  background: var(--panel-2);
  border-radius: 15px;
  padding: 9px 5px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  transition: transform .12s ease;
}
.level-btn:hover { transform: translateY(-2px); }
.level-btn .lvl-emoji { font-size: 23px; }
.level-btn .lvl-name { font-size: 11.5px; font-weight: 800; text-align: center; line-height: 1.2; }
.level-btn.active {
  border-color: var(--sun);
  background: #fff;
  box-shadow: 0 4px 0 rgba(0, 0, 0, .12);
}

.switch { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--ink-soft); cursor: pointer; }
.switch input { width: 16px; height: 16px; accent-color: var(--mint); cursor: pointer; }

/* ------------------------------------------------------------- move list */

.move-list {
  list-style: none;
  margin: 0;
  padding: 0 4px 0 0;
  overflow-y: auto;
  min-height: 0;
  flex: 1;
  font-size: 13.5px;
}
.move-row {
  display: grid;
  grid-template-columns: 30px 1fr 1fr;
  gap: 6px;
  align-items: center;
  padding: 3px 0;
}
.move-row .num { color: var(--ink-soft); font-size: 12px; }
.move-ply {
  padding: 3px 7px;
  border-radius: 8px;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 5px;
}
.move-ply.grade-best { background: #d7f5df; }
.move-ply.grade-great { background: #e0f3ff; }
.move-ply.grade-good { background: #f2f0e6; }
.move-ply.grade-ok { background: #fff2d6; }
.move-ply.grade-mistake { background: #ffe2cf; }
.move-ply.grade-blunder { background: #ffd6d6; }

/* ---------------------------------------------------------------- modals */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(20, 10, 40, .68);
  display: grid;
  place-items: center;
  z-index: 100;
  backdrop-filter: blur(3px);
  animation: fade-in .18s ease;
}
.modal-backdrop[hidden] { display: none; }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

.modal {
  position: relative;
  background: var(--panel);
  border-radius: 26px;
  padding: 28px;
  width: min(560px, 92vw);
  max-height: 88vh;
  overflow-y: auto;
  text-align: center;
  box-shadow: var(--shadow);
  animation: pop-in .28s cubic-bezier(.2, .9, .3, 1.35);
}
.modal-small { width: min(420px, 92vw); }
.modal-wide { width: min(880px, 94vw); text-align: left; }
@keyframes pop-in { from { transform: scale(.85); opacity: 0; } to { transform: scale(1); opacity: 1; } }

.modal h2 { margin: 0 0 8px; font-size: 24px; }
.modal p { margin: 0 0 16px; color: var(--ink-soft); line-height: 1.55; }

.modal-close {
  position: absolute;
  top: 14px;
  right: 14px;
  border: none;
  background: var(--panel-2);
  width: 34px;
  height: 34px;
  border-radius: 50%;
  font-size: 15px;
  cursor: pointer;
  font-family: inherit;
}

.modal-actions { display: flex; gap: 10px; justify-content: center; margin-top: 8px; flex-wrap: wrap; }

.result-emoji { font-size: 70px; line-height: 1; margin-bottom: 6px; animation: bob 1.4s ease-in-out infinite; }
.lesson-emoji { font-size: 56px; line-height: 1; }

.promo-choices { display: flex; gap: 12px; justify-content: center; }
.promo-choice {
  width: 84px;
  height: 96px;
  border: 3px solid transparent;
  background: var(--panel-2);
  border-radius: 18px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  font-family: inherit;
  font-weight: 700;
  font-size: 12px;
  transition: transform .12s ease;
}
.promo-choice:hover { transform: translateY(-4px); border-color: var(--sun); }
.promo-choice .piece-icon { width: 50px; height: 50px; }

.side-choices { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; }
.side-choice {
  width: 140px;
  padding: 16px 10px;
  border: 3px solid transparent;
  background: var(--panel-2);
  border-radius: 18px;
  cursor: pointer;
  font-family: inherit;
  display: flex;
  flex-direction: column;
  gap: 3px;
  align-items: center;
  transition: transform .12s ease;
}
.side-choice:hover { transform: translateY(-4px); border-color: var(--sun); }
.side-choice .side-emoji { font-size: 32px; }
.side-choice small { color: var(--ink-soft); }

.rules-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 14px; }
.rule-card {
  display: flex;
  gap: 12px;
  background: var(--panel-2);
  border-radius: 18px;
  padding: 13px;
}
.rule-card .piece-icon { width: 56px; height: 56px; flex: none; }
.rule-card h3 { margin: 0 0 4px; font-size: 15.5px; }
.rule-card p { margin: 0; font-size: 13px; line-height: 1.5; }
.rule-card .worth {
  display: inline-block;
  background: var(--sun);
  color: #4a3306;
  border-radius: 999px;
  padding: 1px 9px;
  font-size: 11.5px;
  font-weight: 800;
  margin-top: 6px;
}

.rules-extra { margin-top: 18px; }
.rules-extra h3 { font-size: 16px; margin: 0 0 8px; }
.rules-extra ul { margin: 0; padding-left: 20px; }
.rules-extra li { margin-bottom: 9px; font-size: 13.5px; line-height: 1.55; color: var(--ink-soft); }
.rules-extra strong { color: var(--ink); }

/* -------------------------------------------------------------- confetti */

.confetti-layer { position: fixed; inset: 0; pointer-events: none; z-index: 200; overflow: hidden; }
.confetti-bit {
  position: absolute;
  width: 11px;
  height: 15px;
  border-radius: 2px;
  animation: fall linear forwards;
}
@keyframes fall {
  from { transform: translateY(-40px) rotate(0deg); opacity: 1; }
  to { transform: translateY(105vh) rotate(760deg); opacity: .9; }
}

/* ----------------------------------------------------------- scrollbars */

::-webkit-scrollbar { width: 9px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #d9cbb4; border-radius: 9px; }
::-webkit-scrollbar-thumb:hover { background: #c4b295; }

/* ------------------------------------------------------------ responsive */

@media (max-width: 1180px) {
  .layout { grid-template-columns: minmax(0, 1fr) 340px; }
  .board-column { --board-size: min(54vh, 520px, 100%); }
}

/* ---- Tablet and below: stack the coach under the board ---- */
@media (max-width: 940px) {
  html, body { height: auto; }
  body { overflow: auto; }
  #app { height: auto; min-height: 100%; }
  .layout { grid-template-columns: 1fr; }
  .coach-column { overflow: visible; }
  .coach-card { max-height: none; min-height: 0; }
  .panel-grow { max-height: 260px; }
  .btn-text { display: none; }
  .btn-hint .btn-text, .btn-soft .btn-text { display: inline; }
  /* Height no longer constrains the board once the page scrolls. */
  .board-column { --board-size: min(78vh, 620px, 100%); }
  /*
   * Anything this narrow is a tablet or a phone in practice, so guarantee
   * finger-sized targets here as well as via `pointer: coarse` below — the
   * media feature reports the *primary* pointer and misses some devices.
   */
  .btn { min-height: 44px; min-width: 44px; }
  .level-btn { min-height: 60px; }
}

/* ---- Phones ---- */
@media (max-width: 760px) {
  #app { gap: 10px; padding-left: max(10px, env(safe-area-inset-left)); padding-right: max(10px, env(safe-area-inset-right)); }

  .topbar { gap: 8px; }
  .brand-mark { width: 40px; height: 40px; font-size: 23px; border-radius: 12px; }
  .brand h1 { font-size: 19px; }
  .tagline { display: none; }
  .topbar-controls { gap: 6px; }

  /* Thumb-sized hit areas — 44px is the smallest comfortable touch target. */
  .btn { min-height: 44px; min-width: 44px; justify-content: center; padding: 10px 13px; }
  .btn-emoji { font-size: 19px; }
  .btn-hint { min-height: 52px; font-size: 16.5px; }

  /* Leave room either side for the rank labels so nothing spills offscreen. */
  .board-column { --board-size: min(calc(100% - 40px), 68vh); gap: 8px; }
  .board-frame { border-width: 6px; border-radius: 14px; }
  .coords-rank { left: -17px; width: 14px; }
  .coords-file { bottom: -17px; height: 14px; }
  .coords { font-size: 10px; }

  .player-strip { padding: 6px 10px; gap: 9px; }
  .avatar { width: 34px; height: 34px; font-size: 20px; border-radius: 10px; }
  .player-name { font-size: 13.5px; }

  .coach-card, .panel { padding: 13px; border-radius: 18px; }
  .mascot { width: 46px; height: 46px; font-size: 28px; }
  .speech { font-size: 14px; }
  .level-btn { min-height: 62px; }
  .panel-grow { max-height: 200px; }

  .modal { padding: 22px 18px; border-radius: 20px; }
  .modal h2 { font-size: 20px; }
  .promo-choices { flex-wrap: wrap; }
  .promo-choice { width: 72px; height: 86px; }
  .side-choice { width: 100%; max-width: 260px; }
  .rules-grid { grid-template-columns: 1fr; }
}

/*
 * Touch targets key off the input device, not the screen width — a tablet is
 * wide but still operated with fingers. 44px is the smallest comfortable tap
 * target, and children are less accurate than adults.
 */
@media (pointer: coarse) {
  .btn { min-height: 44px; min-width: 44px; }
  .level-btn { min-height: 60px; }
  .concept-chip { min-height: 38px; }
  .modal-close { width: 42px; height: 42px; }
  .promo-choice { min-height: 90px; }
  .switch input { width: 20px; height: 20px; }
  /* Hover lift is meaningless on touch and leaves buttons stuck raised. */
  .btn:hover, .level-btn:hover, .side-choice:hover, .promo-choice:hover { transform: none; }
}

/* Short, wide screens — a phone held sideways. Keep the board visible. */
@media (max-width: 940px) and (orientation: landscape) and (max-height: 560px) {
  .layout { grid-template-columns: minmax(0, 1fr) minmax(280px, 42%); }
  .board-column { --board-size: min(96vh, 100%); }
  .tagline, .brand h1 { display: none; }
}

/* Respect a reduced-motion preference — several things here bounce and pulse. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
  }
}

/* ------------------------------------------------- players, history, board */

.player-panel {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
}

.player-chip {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 9px;
  font-family: inherit;
  font-size: 15px;
  font-weight: 800;
  color: var(--ink);
  background: var(--panel-2);
  border: none;
  border-radius: 14px;
  padding: 8px 12px;
  cursor: pointer;
  min-height: 44px;
  text-align: left;
}
.player-chip:hover { background: #ffe9c2; }
.chip-avatar { font-size: 22px; line-height: 1; }
.chip-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip-caret { opacity: .5; font-size: 12px; }

.btn.icon-only { flex: none; padding: 10px 12px; min-width: 46px; justify-content: center; }

/* Profile list inside the players modal */
.profile-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 18px; }

.profile-row {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--panel-2);
  border: 3px solid transparent;
  border-radius: 16px;
  padding: 10px 12px;
  text-align: left;
}
.profile-row.active { border-color: var(--sun); background: #fff; }
.profile-row .row-avatar { font-size: 26px; }
.profile-row .row-meta { flex: 1; min-width: 0; }
.profile-row .row-name { font-weight: 800; font-size: 15px; }
.profile-row .row-sub { font-size: 12.5px; color: var(--ink-soft); }
.profile-row button {
  font-family: inherit;
  border: none;
  border-radius: 11px;
  padding: 9px 12px;
  min-height: 40px;
  cursor: pointer;
  font-weight: 700;
  font-size: 13px;
  background: #fff;
  box-shadow: 0 2px 0 rgba(0, 0, 0, .12);
}
.profile-row .row-pick { background: var(--mint); color: #10403c; }
.profile-row .row-remove { background: #ffe1e1; }

.add-profile { background: var(--panel-2); border-radius: 18px; padding: 14px; }
.add-profile h3, .cloud-box h3 { margin: 0 0 10px; font-size: 15px; }
.add-profile input {
  width: 100%;
  font-family: inherit;
  font-size: 15px;
  padding: 11px 13px;
  border: 3px solid #e8dcc8;
  border-radius: 13px;
  margin-bottom: 10px;
  background: #fff;
  color: var(--ink);
}
.add-profile input:focus { outline: none; border-color: var(--sun); }

/* A grid rather than wrapped flex so the last row lines up with the ones above
   it, and capped at roughly three rows: the list is long enough now that on a
   phone it would otherwise push "Add player" off the bottom of the card. */
.avatar-picker {
  display: grid;
  grid-template-columns: repeat(auto-fill, 44px);
  justify-content: center;
  gap: 6px;
  margin-bottom: 12px;
  max-height: 164px;
  overflow-y: auto;
  padding: 2px;
}
.avatar-option {
  width: 44px;
  height: 44px;
  font-size: 23px;
  border: 3px solid transparent;
  border-radius: 12px;
  background: #fff;
  cursor: pointer;
}
.avatar-option.selected { border-color: var(--sun); background: #fff6e0; }

.cloud-box { margin-top: 18px; border-top: 2px dashed #e8dcc8; padding-top: 16px; }
.cloud-note { font-size: 13px; line-height: 1.55; }

/* Stats */
.stat-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 10px; margin-bottom: 16px; }
.stat-card { background: var(--panel-2); border-radius: 16px; padding: 12px; text-align: center; }
.stat-value { font-size: 24px; font-weight: 800; line-height: 1.1; }
.stat-label { font-size: 11.5px; color: var(--ink-soft); text-transform: uppercase; letter-spacing: .5px; margin-top: 3px; }

.spark { width: 100%; height: 66px; display: block; }
.spark-wrap { background: var(--panel-2); border-radius: 16px; padding: 12px; margin-bottom: 6px; }
.spark-title { font-size: 12px; color: var(--ink-soft); margin-bottom: 6px; }

.game-list { display: flex; flex-direction: column; gap: 7px; max-height: 320px; overflow-y: auto; }
.game-row {
  display: grid;
  grid-template-columns: 34px 1fr auto auto;
  gap: 10px;
  align-items: center;
  background: var(--panel-2);
  border-radius: 13px;
  padding: 9px 12px;
  font-size: 13.5px;
}
.game-result { font-size: 20px; text-align: center; }
.game-meta { min-width: 0; }
.game-when { color: var(--ink-soft); font-size: 12px; }
.game-acc { font-weight: 800; }
.game-moves {
  font-size: 12px;
  color: var(--ink-soft);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}

.empty-note { color: var(--ink-soft); font-size: 14px; text-align: center; padding: 22px 10px; }

/* Leaderboard */
.rank-table { display: flex; flex-direction: column; gap: 7px; }
.rank-row {
  display: grid;
  grid-template-columns: 34px 34px 1fr auto auto;
  gap: 10px;
  align-items: center;
  background: var(--panel-2);
  border-radius: 13px;
  padding: 10px 12px;
}
.rank-row.me { background: #fff2d0; box-shadow: inset 0 0 0 3px var(--sun); }
.rank-place { font-weight: 800; text-align: center; }
.rank-avatar { font-size: 22px; text-align: center; }
.rank-name { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank-stat { font-size: 13px; color: var(--ink-soft); white-space: nowrap; }

.board-actions { display: flex; gap: 8px; flex-wrap: wrap; margin: 14px 0; }
.join-row { display: flex; gap: 8px; }
.join-row input {
  flex: 1;
  font-family: inherit;
  font-size: 16px;
  letter-spacing: 2px;
  text-transform: uppercase;
  padding: 11px 13px;
  border: 3px solid #e8dcc8;
  border-radius: 13px;
  background: #fff;
  color: var(--ink);
  min-width: 0;
}
.join-code {
  display: inline-block;
  font-size: 22px;
  font-weight: 800;
  letter-spacing: 4px;
  background: var(--sun);
  color: #4a3306;
  border-radius: 12px;
  padding: 8px 16px;
  margin: 6px 0;
}

/* ---------------------------------------------- account button and sign-up */

/*
 * The account button sits in the top bar so signing in is findable without
 * hunting through the players panel — but it is styled quieter than New Game,
 * because the child's next move matters more than the grown-up's account.
 */
.btn-account {
  background: rgba(255, 255, 255, .16);
  color: #fff;
  backdrop-filter: blur(4px);
  border: 2px solid rgba(255, 255, 255, .35);
}
.btn-account:hover { background: rgba(255, 255, 255, .28); }
.btn-account.signed-in {
  border-color: rgba(78, 205, 196, .8);
  background: rgba(78, 205, 196, .22);
}

.welcome-card { text-align: center; width: min(500px, 92vw); }
.welcome-mark {
  font-size: 44px;
  line-height: 1;
  color: var(--grape);
  margin-bottom: 6px;
}
.welcome-lead { color: var(--ink-soft); font-size: 15px; margin: 8px 0 4px; }

.welcome-points {
  list-style: none;
  text-align: left;
  margin: 16px auto;
  padding: 14px 16px;
  background: var(--panel-2);
  border-radius: 16px;
  max-width: 400px;
  display: grid;
  gap: 10px;
}
.welcome-points li {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  font-size: 14px;
  color: var(--ink);
}
.welcome-points span { font-size: 17px; line-height: 1.2; }

.welcome-fineprint {
  font-size: 12px;
  color: var(--ink-soft);
  margin-top: 14px;
  line-height: 1.5;
}

.invite-bar { display: flex; gap: 8px; align-items: center; margin: 10px 0 4px; flex-wrap: wrap; }
.invite-said {
  font-size: 13px;
  font-weight: 700;
  color: var(--leaf);
  animation: fade-in .18s ease;
}
.invite-hint { font-size: 12px; color: var(--ink-soft); margin-bottom: 10px; }

/* On a phone the account label would crowd out New Game; the emoji carries it. */
@media (max-width: 940px) {
  .btn-account .btn-text { display: none; }
  .btn-account.signed-in .btn-text { display: none; }
  .invite-bar .btn { flex: 1 1 45%; }
}
