/* Labyrinth — base layout, tokens and screens. */

:root {
  --ink: #3b2d4f;
  --ink-soft: #6a5a80;
  --paper: #fffaf0;
  --sky-top: #7ec8f2;
  --sky-bottom: #c9f0ff;
  --grass: #8fdc6a;
  --sun: #ffd23f;
  --pink: #ff5d8f;
  --orange: #ff9f1c;
  --green: #3ccf6e;
  --green-dark: #23a052;
  --blue: #3fa7f5;
  --blue-dark: #1f7fd0;
  --purple: #9b6bff;
  --purple-dark: #6f43d6;
  --red: #ff6b6b;
  --red-dark: #d94848;
  --radius: 28px;
  --btn: 72px;
  --gap: clamp(10px, 2.2vmin, 24px);
  --font: ui-rounded, "SF Pro Rounded", "Nunito", "Varela Round", "Arial Rounded MT Bold", "Comic Sans MS", system-ui, sans-serif;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
}

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

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
  -webkit-user-select: none;
}

body {
  font-family: var(--font);
  color: var(--ink);
  background: linear-gradient(180deg, var(--sky-top), var(--sky-bottom) 70%, #e9fbd9);
  font-weight: 800;
}

/* Soft drifting clouds behind the menus. */
body::before, body::after {
  content: "";
  position: fixed;
  pointer-events: none;
  width: 340px; height: 120px;
  background:
    radial-gradient(circle at 30% 60%, #fff 0 38px, transparent 39px),
    radial-gradient(circle at 52% 40%, #fff 0 52px, transparent 53px),
    radial-gradient(circle at 72% 62%, #fff 0 40px, transparent 41px),
    radial-gradient(ellipse at 50% 80%, #fff 0 110px, transparent 111px);
  opacity: .85;
  z-index: 0;
  animation: drift 60s linear infinite;
}
body::before { top: 6vh; left: -360px; }
body::after { top: 58vh; left: -360px; transform: scale(.7); animation-duration: 85s; animation-delay: -40s; }
@keyframes drift { to { translate: calc(100vw + 720px) 0; } }

#app {
  position: fixed;
  inset: 0;
  z-index: 1;
  padding: calc(var(--safe-t) + var(--gap)) calc(var(--safe-r) + var(--gap)) calc(var(--safe-b) + var(--gap)) calc(var(--safe-l) + var(--gap));
  display: flex;
}

button {
  font: inherit;
  color: inherit;
  border: 0;
  background: none;
  padding: 0;
  cursor: pointer;
  touch-action: manipulation;
}
button:focus-visible { outline: 5px solid var(--sun); outline-offset: 4px; }

svg { display: block; }

/* ---------- Screens ---------- */

.screen {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  position: relative;
  z-index: 1;
  animation: pop-in .35s cubic-bezier(.2, 1.4, .4, 1) both;
}
@keyframes pop-in { from { opacity: 0; transform: scale(.94); } }

.topbar, .playbar {
  display: flex;
  align-items: center;
  gap: var(--gap);
  flex: none;
}
.screen-title {
  margin: 0;
  font-size: clamp(28px, 5vmin, 48px);
  display: flex;
  align-items: center;
  gap: 12px;
  text-shadow: 0 3px 0 #fff;
}
.screen-title svg { width: 1.3em; height: 1.3em; }

/* Chunky candy buttons */
.icon-btn, .mode-btn, .level-tile, .choice, .play-btn {
  --c: var(--blue);
  --cd: var(--blue-dark);
  background: var(--c);
  border: 4px solid var(--ink);
  border-radius: var(--radius);
  box-shadow: 0 8px 0 var(--cd), 0 10px 0 var(--ink), 0 16px 18px rgba(59, 45, 79, .25);
  transition: transform .08s, box-shadow .08s;
}
.icon-btn:active, .mode-btn:active, .level-tile:active, .choice:active, .play-btn:active {
  transform: translateY(6px);
  box-shadow: 0 2px 0 var(--cd), 0 4px 0 var(--ink), 0 6px 8px rgba(59, 45, 79, .25);
}

.icon-btn {
  width: var(--btn);
  height: var(--btn);
  min-width: 64px;
  min-height: 64px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  flex: none;
  color: #fff;
}
.icon-btn svg { width: 58%; height: 58%; }
.icon-btn.home { --c: var(--orange); --cd: #d97c00; }
.icon-btn.reset { --c: var(--purple); --cd: var(--purple-dark); }
.icon-btn.go { --c: var(--green); --cd: var(--green-dark); }
.icon-btn.big { width: 104px; height: 104px; }

/* ---------- Main menu ---------- */

.menu { align-items: center; justify-content: center; }
.logo {
  margin: 0;
  font-size: clamp(48px, 11vmin, 120px);
  letter-spacing: .02em;
  line-height: 1;
  display: flex;
  gap: .02em;
}
.logo span {
  display: inline-block;
  color: var(--c);
  -webkit-text-stroke: .06em var(--ink);
  paint-order: stroke fill;
  text-shadow: 0 .07em 0 var(--ink);
  animation: bob 2.4s ease-in-out infinite;
  animation-delay: calc(var(--i) * -.18s);
}
@keyframes bob { 50% { transform: translateY(-.08em) rotate(-3deg); } }

.mode-grid {
  display: flex;
  gap: calc(var(--gap) * 1.5);
  flex-wrap: wrap;
  justify-content: center;
  margin-top: var(--gap);
}
.mode-btn {
  width: clamp(150px, 26vmin, 260px);
  aspect-ratio: 4 / 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 14px 10px 16px;
  color: #fff;
}
.mode-btn.levels { --c: var(--pink); --cd: #d9356b; }
.mode-btn.free { --c: var(--green); --cd: var(--green-dark); }
.mode-btn.endless { --c: var(--purple); --cd: var(--purple-dark); }
.mode-art {
  width: 100%;
  flex: 1;
  min-height: 0;
  background: rgba(255, 255, 255, .9);
  border: 3px solid var(--ink);
  border-radius: 20px;
  display: grid;
  place-items: center;
  padding: 6px;
  margin-bottom: 10px;
}
.mode-art svg { width: 100%; height: 100%; }
.mode-label {
  font-size: clamp(20px, 3.8vmin, 34px);
  -webkit-text-stroke: 5px var(--ink);
  paint-order: stroke fill;
}

/* Landscape phones: keep the menu on one row */
@media (max-height: 520px) and (orientation: landscape) {
  .mode-btn { width: clamp(130px, 22vw, 210px); aspect-ratio: 1; }
  .logo { font-size: 13vmin; }
}

/* ---------- Level picker ---------- */

.level-grid {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(clamp(140px, 17vw, 230px), 1fr));
  grid-auto-rows: clamp(112px, 12.5vw, 180px);
  align-content: start;
  gap: calc(var(--gap) * 1.4);
  padding: 20px 16px 28px;
  overflow-y: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
  scroll-padding: 24px;
}
.level-tile {
  --c: #fff;
  --cd: #d6cbe8;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 0;
  overflow: visible;
  padding: 8px;
  background: var(--tile-bg, #fff);
}
.level-tile .tile-art { width: 100%; flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; gap: 4%; }
.level-tile .tile-art img { width: 46%; height: auto; max-height: 100%; object-fit: contain; }
.tile-num {
  position: absolute;
  top: -14px; left: -12px;
  width: 48px; height: 48px;
  border-radius: 50%;
  background: var(--sun);
  border: 4px solid var(--ink);
  display: grid;
  place-items: center;
  font-size: 24px;
}
.tile-star {
  position: absolute;
  top: -16px; right: -14px;
  width: 52px; height: 52px;
  filter: drop-shadow(0 3px 0 var(--ink));
  animation: twinkle 2s ease-in-out infinite;
}
@keyframes twinkle { 50% { transform: rotate(12deg) scale(1.08); } }

/* ---------- Setup screens ---------- */

.setup-body {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: calc(var(--gap) * 1.4);
  overflow-y: auto;
  padding-bottom: 18px;
}
.choice-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: var(--gap);
}
.choice {
  --c: #fff;
  --cd: #d6cbe8;
  min-width: clamp(96px, 13vmin, 150px);
  min-height: clamp(96px, 13vmin, 150px);
  padding: 10px 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-size: 18px;
}
.choice[aria-pressed="true"] { --c: #9fdcff; --cd: #4fa8dc; transform: scale(1.08); }
.choice[aria-pressed="true"]:active { transform: translateY(6px) scale(1.06); }
.stars { display: flex; gap: 2px; }
.choice .stars svg { width: clamp(30px, 4.5vmin, 48px); height: clamp(30px, 4.5vmin, 48px); }
.choice img { width: clamp(64px, 10vmin, 110px); height: clamp(64px, 10vmin, 110px); object-fit: contain; }
.choice.theme { min-width: 0; min-height: 0; padding: 8px; }
.choice { font-size: clamp(18px, 2.6vmin, 26px); }

.play-btn {
  --c: var(--green);
  --cd: var(--green-dark);
  width: 148px;
  height: 104px;
  border-radius: 999px;
  display: grid;
  place-items: center;
  color: #fff;
  flex: none;
}
.play-btn svg { width: 56px; height: 56px; }

/* ---------- Play screen ---------- */

.play { gap: calc(var(--gap) * .8); }
.playbar { justify-content: space-between; }
.playbar .middle { flex: 1; display: flex; justify-content: center; gap: var(--gap); min-width: 0; }
.pill {
  background: #fff;
  border: 4px solid var(--ink);
  border-radius: 999px;
  padding: 6px 22px;
  font-size: clamp(26px, 5vmin, 44px);
  min-height: 64px;
  display: flex;
  align-items: center;
  gap: 10px;
  box-shadow: 0 6px 0 rgba(59, 45, 79, .25);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.pill svg { width: 1.1em; height: 1.1em; }
.timer.running svg { animation: tick 1s steps(2) infinite; }
@keyframes tick { 50% { transform: rotate(15deg); } }

.board-wrap {
  flex: 1;
  min-height: 0;
  position: relative;
  border-radius: calc(var(--radius) + 6px);
  overflow: hidden;
  border: 5px solid var(--ink);
  box-shadow: 0 10px 0 rgba(59, 45, 79, .35);
  background: #fff;
}
.board {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: crosshair;
}

/* Landscape phones: put the controls in a side column to give the maze height. */
@media (max-height: 520px) and (orientation: landscape) {
  .play { flex-direction: row; }
  .playbar { flex-direction: column; justify-content: space-between; }
  .playbar .middle { flex-direction: column; justify-content: center; align-items: center; }
  .pill { padding: 4px 12px; font-size: 24px; }
}

/* ---------- Celebration overlay ---------- */

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  z-index: 5;
  pointer-events: none;
}
.celebrate-card {
  pointer-events: auto;
  background: #fff;
  border: 5px solid var(--ink);
  border-radius: 36px;
  padding: 18px 28px 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  box-shadow: 0 12px 0 rgba(59, 45, 79, .35);
  animation: card-in .5s cubic-bezier(.2, 1.6, .4, 1) both;
  max-width: calc(100% - 24px);
}
@keyframes card-in { from { transform: scale(.3) rotate(-8deg); opacity: 0; } }
.big-star { width: clamp(90px, 18vmin, 150px); height: clamp(90px, 18vmin, 150px); animation: spin-in 1s ease-out both; }
@keyframes spin-in { from { transform: rotate(-200deg) scale(.2); } }
.result-time { font-size: clamp(36px, 8vmin, 64px); display: flex; align-items: center; gap: 12px; font-variant-numeric: tabular-nums; }
.result-time svg { width: .9em; height: .9em; }
.actions { display: flex; gap: 18px; flex-wrap: wrap; justify-content: center; }
.countdown { position: absolute; inset: -8px; pointer-events: none; }
.countdown circle { fill: none; stroke: #fff; stroke-width: 6; stroke-linecap: round; transform: rotate(-90deg); transform-origin: center; animation: countdown var(--dur, 4s) linear forwards; }
@keyframes countdown { from { stroke-dashoffset: 0; } to { stroke-dashoffset: var(--len); } }
.icon-btn { position: relative; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; }
}

.mode-art img { width: 100%; height: 100%; object-fit: contain; }
.choice .dice { display: block; width: clamp(64px, 10vmin, 110px); height: clamp(64px, 10vmin, 110px); color: var(--purple); }
.choice .dice svg { width: 100%; height: 100%; }
.pill .stars svg { width: 1em; height: 1em; }

/* ---------- Accounts: menu corner, grown-ups area, forms, dialogs ---------- */

input, textarea { user-select: text; -webkit-user-select: text; -webkit-touch-callout: default; }

.menu-corner {
  position: absolute;
  top: 0; left: 0; right: 0;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  pointer-events: none;
}
.menu-corner > * { pointer-events: auto; }

.avatar-btn {
  width: 84px; height: 84px;
  border-radius: 50%;
  background: #fff;
  border: 4px solid var(--ink);
  box-shadow: 0 6px 0 rgba(59, 45, 79, .3);
  display: grid; place-items: center;
  padding: 6px;
  flex: none;
}
.avatar-btn.small { width: 68px; height: 68px; margin-left: auto; }
.avatar-btn img, .avatar-btn svg { width: 100%; height: 100%; }
.avatar-btn .who-icon { width: 70%; height: 70%; color: var(--purple); }
.avatar-img { display: block; object-fit: contain; }

.icon-btn.lock-btn { --c: #efe9f7; --cd: #cfc6df; color: var(--ink-soft); }
.icon-btn.lock-btn svg { width: 44%; height: 44%; }
.icon-btn.no { --c: var(--red); --cd: var(--red-dark); }

.scroll-body {
  flex: 1; min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  display: flex; justify-content: center; align-items: flex-start;
  padding: 4px 4px 24px;
}
.panel {
  width: 100%; max-width: 560px;
  background: #fff;
  border: 4px solid var(--ink);
  border-radius: var(--radius);
  box-shadow: 0 8px 0 rgba(59, 45, 79, .25);
  padding: clamp(16px, 3vmin, 28px);
  display: flex; flex-direction: column; gap: 16px;
}
.panel h3 { margin: 6px 0 0; font-size: 22px; }
.panel-text { margin: 0; font-size: 18px; font-weight: 600; line-height: 1.45; }
.privacy .panel-text { font-size: 17px; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field-label { font-size: 18px; }
.field-hint { font-size: 14px; font-weight: 600; color: var(--ink-soft); }
.field input {
  font: inherit; font-size: 18px; font-weight: 700; color: var(--ink);
  min-height: 56px; padding: 0 14px;
  border: 3px solid var(--ink); border-radius: 14px; background: #fbf9ff;
}
.field input:focus { outline: 4px solid var(--sun); outline-offset: 1px; }
.check { display: flex; align-items: center; gap: 12px; font-size: 17px; font-weight: 700; line-height: 1.3; cursor: pointer; }
.check input { width: 28px; height: 28px; flex: none; accent-color: var(--green); }

.text-btn {
  min-height: 56px; padding: 0 20px;
  border: 3px solid var(--ink); border-radius: 16px;
  background: #fff; font-size: 18px;
  box-shadow: 0 4px 0 var(--ink);
}
.text-btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
.text-btn.primary { background: var(--green); color: #fff; }
.text-btn.danger { background: var(--red); color: #fff; }
.text-btn.big { min-height: 64px; font-size: 20px; }
.text-btn:disabled { opacity: .5; cursor: default; }
.link-btn { align-self: center; padding: 10px; font-size: 16px; text-decoration: underline; color: var(--blue-dark); min-height: 44px; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.stack.spaced { margin-top: 12px; }
.form-error { margin: 0; padding: 10px 14px; border-radius: 12px; background: #ffe1e1; color: #a12a2a; font-weight: 700; }
.form-actions { display: flex; justify-content: flex-end; gap: 12px; }

.dialog-backdrop {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(59, 45, 79, .45);
  display: grid; place-items: center;
  padding: 16px;
}
.dialog {
  width: min(440px, 100%);
  background: #fff; border: 4px solid var(--ink); border-radius: var(--radius);
  padding: 22px; display: flex; flex-direction: column; gap: 16px;
  animation: card-in .35s cubic-bezier(.2, 1.6, .4, 1) both;
}
.dialog-icon { width: 56px; height: 56px; align-self: center; color: var(--purple); }
.dialog-icon svg { width: 100%; height: 100%; }
.shake { animation: shake .4s; }
@keyframes shake { 20%, 60% { transform: translateX(-10px); } 40%, 80% { transform: translateX(10px); } }

.player-list { display: flex; flex-direction: column; gap: 8px; }
.player-row {
  display: flex; align-items: center; gap: 12px;
  min-height: 68px; padding: 6px 14px;
  border: 3px solid var(--ink); border-radius: 18px; background: #fbf9ff; text-align: left;
}
.player-row .avatar-img { width: 54px; height: 54px; }
.player-name { flex: 1; font-size: 20px; }
.row-icon { width: 26px; height: 26px; color: var(--ink-soft); }
.row-icon svg { width: 100%; height: 100%; }

.avatar-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 10px; }
.avatar-choice { border: 3px solid var(--ink); border-radius: 18px; background: #fff; padding: 6px; aspect-ratio: 1; min-height: 64px; }
.avatar-choice img { width: 100%; height: 100%; }
.avatar-choice[aria-pressed="true"] { background: #9fdcff; transform: scale(1.06); box-shadow: 0 0 0 4px var(--sun); }
.avatar-choice:disabled { opacity: .25; cursor: not-allowed; }

.who-grid { flex: 1; min-height: 0; overflow-y: auto; display: flex; flex-wrap: wrap; align-content: center; justify-content: center; gap: calc(var(--gap) * 1.4); padding: 12px; }
.who-tile {
  --c: #fff; --cd: #d6cbe8;
  width: clamp(130px, 22vmin, 220px); aspect-ratio: 1;
  background: var(--c); border: 4px solid var(--ink); border-radius: var(--radius);
  box-shadow: 0 8px 0 var(--cd), 0 10px 0 var(--ink);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; padding: 10px;
}
.who-tile:active { transform: translateY(6px); box-shadow: 0 2px 0 var(--cd), 0 4px 0 var(--ink); }
.who-tile .avatar-img { width: 78%; height: auto; flex: 1; min-height: 0; }
.who-name { font-size: clamp(16px, 2.4vmin, 22px); max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.note-card {
  align-self: center; background: #fff; border: 4px solid var(--ink); border-radius: var(--radius);
  padding: 16px 24px; display: flex; flex-direction: column; align-items: center; gap: 10px; text-align: center; font-size: 20px;
  box-shadow: 0 6px 0 rgba(59, 45, 79, .25);
}
.note-card p { margin: 0; }
.note-art { display: flex; gap: 8px; }
.note-art .avatar-img { width: 56px; height: 56px; }

/* ---------- Completion: leaderboard and notes ---------- */

.celebrate-card { max-height: calc(100% - 16px); overflow-y: auto; }
.result-extra { display: flex; flex-direction: column; align-items: center; gap: 10px; }
.result-extra:empty { display: none; }
.leaderboard { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; width: min(440px, 78vw); }
.leaderboard li {
  display: grid; grid-template-columns: 40px 44px 1fr auto; align-items: center; gap: 10px;
  padding: 4px 12px; border-radius: 14px; background: #f6f2fb; font-size: 20px; min-height: 52px;
}
.leaderboard li.mine { background: #fff1b8; box-shadow: inset 0 0 0 3px #ffb703; animation: pop-in .5s .3s both; }
.leaderboard .medal { width: 36px; height: 36px; }
.leaderboard .medal svg { width: 100%; height: 100%; }
.leaderboard .rank { text-align: center; color: var(--ink-soft); }
.leaderboard .avatar-img { width: 44px; height: 44px; }
.lb-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-time { font-variant-numeric: tabular-nums; }
.result-note { display: flex; align-items: center; gap: 10px; font-size: 18px; padding: 8px 16px; border-radius: 14px; background: #eef6ff; }
.result-note svg { width: 28px; height: 28px; }
.result-note.muted { background: #f1eef6; color: var(--ink-soft); }
.result-note.cheer { background: #e8fbe0; }

.import .import-art { display: flex; align-items: center; justify-content: center; gap: 14px; }
.import-star { width: 84px; height: 84px; }
.import-star svg { width: 100%; height: 100%; }
.import-arrow { width: 44px; height: 44px; color: var(--ink-soft); }
.import-arrow svg { width: 100%; height: 100%; }
.import .avatar-img { width: 96px; height: 96px; }
.import .actions { justify-content: center; }
.tile-art img.face-right { transform: scaleX(-1); }

/* ---------- Sound toggles ---------- */

.corner-right { display: flex; gap: var(--gap); align-items: flex-start; }
.sound-toggles { display: flex; gap: calc(var(--gap) * .8); flex: none; }
.icon-btn.sound-btn { --c: #fff; --cd: #d6cbe8; color: var(--ink); }
.icon-btn.sound-btn svg { width: 46%; height: 46%; }
.icon-btn.sound-btn.off { --c: #efe9f7; color: var(--ink-soft); }
.icon-btn.sound-btn.off::after {
  content: "";
  position: absolute;
  left: 18%; right: 18%; top: 50%;
  height: 5px; border-radius: 3px;
  background: var(--red);
  transform: rotate(-40deg);
  box-shadow: 0 0 0 2px #fff;
}

/* Narrow portrait screens: the toggles get their own row above the board. */
@media (max-width: 600px) and (orientation: portrait) {
  .playbar { flex-wrap: wrap; row-gap: 10px; }
  .playbar .sound-toggles { order: 5; width: 100%; justify-content: center; }
}

/* Landscape phones: a compact 2-column side panel. */
@media (max-height: 520px) and (orientation: landscape) {
  .playbar {
    display: grid;
    grid-template-columns: repeat(2, 64px);
    grid-template-areas: "back reset" "mid mid" "snd snd";
    align-content: space-between;
    justify-content: center;
  }
  .playbar > .home { grid-area: back; }
  .playbar > .reset { grid-area: reset; }
  .playbar > .middle { grid-area: mid; }
  .playbar > .sound-toggles { grid-area: snd; gap: 0; justify-content: space-between; }
  .playbar .icon-btn { width: 64px; height: 64px; }
}

/* ---------- Four modes on the menu ---------- */

.mode-btn.tangles { --c: var(--orange); --cd: #d97c00; }
.mode-btn { width: clamp(140px, 21vmin, 230px); }
/* Narrow or short screens: a 2×2 grid of mode buttons. */
@media (max-aspect-ratio: 1/1) {
  .mode-grid { display: grid; grid-template-columns: repeat(2, auto); }
  .mode-btn { width: clamp(130px, 40vw, 220px); }
}
@media (max-height: 520px) and (orientation: landscape) {
  /* Leave the top strip to the corner buttons (avatar, sound, padlock). */
  .menu { justify-content: center; padding-top: 70px; }
  .menu .logo { font-size: 8vmin; }
  .mode-grid { margin-top: 2px; }
  .mode-btn { width: clamp(100px, 17vw, 150px); aspect-ratio: 5 / 6; padding: 8px 8px 10px; }
  .mode-btn .mode-art { margin-bottom: 6px; }
  .mode-btn .mode-label { font-size: clamp(14px, 4.2vh, 20px); white-space: nowrap; -webkit-text-stroke-width: 4px; }
}

/* ---------- Five modes on the menu ---------- */

.mode-btn.dots { --c: #2bb3a3; --cd: #1d8a7d; }
.mode-btn { width: clamp(130px, 18vmin, 210px); }
@media (max-aspect-ratio: 1/1) {
  .mode-grid { display: flex; flex-wrap: wrap; justify-content: center; max-width: 100%; }
  .mode-btn { width: clamp(100px, 29vw, 180px); }
  .mode-label { font-size: clamp(16px, 4.6vw, 28px); }
}
@media (max-height: 520px) and (orientation: landscape) {
  .mode-btn { width: clamp(92px, 15vw, 140px); }
}
.mode-label { white-space: nowrap; font-size: clamp(16px, 2.9vmin, 30px); }
@media (max-aspect-ratio: 1/1) {
  /* 3 + 2: three buttons always fit a row; the corner buttons keep the top strip. */
  .menu { padding-top: 64px; justify-content: center; }
  .mode-grid { gap: 12px; }
  .mode-btn { width: min(180px, calc((100vw - 32px - 24px - 12px) / 3)); padding: 10px 8px 12px; }
  .mode-label { font-size: clamp(14px, 4.4vw, 26px); -webkit-text-stroke-width: 4px; }
}

/* ---------- Tic-tac-toe: marks ---------- */

.ttt-mark, .ttt-badge { display: block; width: 100%; height: 100%; }
.ttt-mark path, .ttt-badge path { stroke-dasharray: 1; stroke-dashoffset: 0; }
.ttt-mark.draw .s1 { animation: ttt-draw 0.2s ease-out both; }
.ttt-mark.draw .s2 { animation: ttt-draw 0.18s 0.16s ease-out both; }
.ttt-mark.o.draw .s1 { animation-duration: 0.34s; }
@keyframes ttt-draw { from { stroke-dashoffset: 1; } }

/* ---------- Tic-tac-toe: setup ---------- */

.choice-row[hidden] { display: none; }
/* Centred when it fits, scrollable from the top when it doesn't. */
.ttt-setup { justify-content: flex-start; gap: var(--gap); }
.ttt-setup > :first-child { margin-top: auto; }
.ttt-setup > :last-child { margin-bottom: auto; }
.ttt-top { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: var(--gap) calc(var(--gap) * 2); }
@media (max-height: 520px), (max-width: 520px) {
  .ttt-setup .choice { min-width: 76px; min-height: 76px; padding: 6px 10px; }
  .ttt-setup .choice img, .ttt-setup .choice .dice { width: 52px; height: 52px; }
  .ttt-setup .choice .stars svg { width: 26px; height: 26px; }
  .ttt-setup .choice .ttt-mode-art img { width: 38px; height: 38px; }
  .ttt-setup .ttt-slot { width: 100px; }
  .ttt-setup .play-btn { height: 80px; width: 120px; }
}
@media (max-height: 520px) and (orientation: landscape) {
  .ttt-setup { padding-right: 120px; padding-bottom: 0; }
  .ttt-setup .play-btn { position: fixed; right: 16px; bottom: 16px; z-index: 3; width: 104px; }
}
.ttt-mode-art { display: flex; gap: 2px; }
.choice .ttt-mode-art img { width: clamp(44px, 7vmin, 76px); height: clamp(44px, 7vmin, 76px); }
.ttt-slots { display: flex; align-items: center; gap: calc(var(--gap) * 1.2); }
.ttt-vs { font-size: clamp(22px, 3.6vmin, 34px); color: var(--ink-soft); }
.ttt-slot, .ttt-card { --mc: #ff5d5d; }
.ttt-slot.o, .ttt-card.o { --mc: #3fa7f5; }
.ttt-slot {
  position: relative;
  width: clamp(112px, 17vmin, 180px); aspect-ratio: 1;
  background: #fff; border: 4px solid var(--ink); border-radius: var(--radius);
  box-shadow: 0 0 0 5px var(--mc), 0 9px 0 5px var(--ink);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; padding: 10px;
}
.ttt-slot:active:not(:disabled) { transform: translateY(5px); }
.ttt-slot:disabled { cursor: default; }
.ttt-slot img { width: 74%; flex: 1; min-height: 0; object-fit: contain; }
.ttt-slot-badge { position: absolute; width: 34%; aspect-ratio: 1; right: -10%; top: -10%; }

.ttt-picker { width: min(620px, 100%); max-height: calc(100% - 16px); overflow-y: auto; }
.ttt-picker-head { display: flex; align-items: center; justify-content: space-between; }
.ttt-picker-mark { width: 64px; height: 64px; }
.ttt-picker .icon-btn { width: 56px; height: 56px; }
.ttt-pick { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; }
.ttt-pick img { min-height: 0; flex: 1; }
.ttt-pick .who-name { font-size: 14px; }
.ttt-guest-head { display: flex; align-items: center; gap: 8px; color: var(--ink-soft); font-size: 18px; }
.ttt-guest-head span { width: 28px; height: 28px; }
.ttt-guest-head svg { width: 100%; height: 100%; }

/* ---------- Tic-tac-toe: play ---------- */

.ttt-wrap { background: #fff; }
.ttt-bg, .ttt-fx { position: absolute; inset: 0; width: 100%; height: 100%; }
.ttt-fx { pointer-events: none; z-index: 4; }
.ttt-layout {
  position: absolute; inset: 0; z-index: 2;
  display: flex; align-items: center; justify-content: center; gap: 24px;
}
.ttt-layout.port {
  align-items: stretch;
  display: grid; grid-template-columns: auto auto; grid-template-areas: "x o" "b b";
  justify-content: center; align-content: center; gap: 22px 28px;
}
.ttt-layout.port .ttt-card.x { grid-area: x; }
.ttt-layout.port .ttt-card.o { grid-area: o; }
.ttt-layout.port .ttt-grid { grid-area: b; justify-self: center; }

.ttt-grid {
  position: relative; flex: none;
  width: var(--ttt-board, 300px); height: var(--ttt-board, 300px);
  display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr);
  touch-action: manipulation;
}
.ttt-square { background: transparent; border: 0; padding: 14%; border-radius: 20px; cursor: pointer; }
.ttt-square:disabled { cursor: default; }
.ttt-square:focus-visible { outline: 4px solid var(--sun); outline-offset: -8px; }
.ttt-grid.waiting .ttt-square { pointer-events: none; }
.ttt-grid.draw { animation: ttt-wiggle .7s ease-in-out; }
@keyframes ttt-wiggle { 20% { transform: rotate(-3deg); } 45% { transform: rotate(3deg); } 70% { transform: rotate(-1.5deg); } }
.ttt-line { position: absolute; inset: 0; pointer-events: none; }
.ttt-line svg { width: 100%; height: 100%; overflow: visible; }
.ttt-line path { fill: none; stroke-dasharray: 1; }
.ttt-line.on path { animation: ttt-draw .4s ease-out both; }

.ttt-card {
  position: relative; flex: none;
  width: var(--ttt-card, 150px);
  background: #fff; border: 4px solid var(--ink); border-radius: var(--radius);
  padding: 12px 10px 10px;
  display: grid; grid-template-areas: "a" "l" "s"; justify-items: center; align-content: center; gap: 4px;
  box-shadow: 0 8px 0 rgba(59, 45, 79, .35);
  transition: transform .25s, filter .25s, box-shadow .25s;
}
.ttt-card-avatar { grid-area: a; width: 76%; aspect-ratio: 1; }
.ttt-card-avatar img, .ttt-result-avatar img, .ttt-slot img { display: block; }
.ttt-card-avatar img { width: 100%; height: 100%; object-fit: contain; }
.ttt-card-label { grid-area: l; display: flex; align-items: center; justify-content: center; gap: 6px; max-width: 100%; min-width: 0; }
.ttt-card-badge { flex: none; width: clamp(24px, 3.4vmin, 32px); height: clamp(24px, 3.4vmin, 32px); }
.ttt-card .who-name { font-size: clamp(15px, 2.3vmin, 21px); min-width: 0; }
.ttt-score { grid-area: s; font-size: clamp(34px, 7vmin, 60px); line-height: 1; font-variant-numeric: tabular-nums; -webkit-text-stroke: .05em var(--ink); color: var(--mc); text-shadow: 0 .06em 0 var(--ink); }
/* Held upright: avatar and score side by side, mark + name across the bottom. */
.ttt-layout.port .ttt-card {
  grid-template-areas: "a s" "l l"; grid-template-columns: auto auto;
  justify-content: center; align-items: center; column-gap: 12px; row-gap: 4px; padding: 8px 10px;
}
.ttt-layout.port .ttt-card-avatar { width: clamp(44px, 13vw, 84px); }
.ttt-layout.port .ttt-card-badge { width: clamp(20px, 5.6vw, 30px); height: clamp(20px, 5.6vw, 30px); }
.ttt-layout.port .ttt-card-label { gap: 4px; }
.ttt-layout.port .ttt-score { font-size: clamp(30px, 9vw, 48px); }
.ttt-card.turn { transform: translateY(-6px); box-shadow: 0 0 0 6px var(--mc), 0 0 22px 10px var(--mc), 0 12px 0 6px rgba(59, 45, 79, .3); }
.ttt-card.turn .ttt-card-avatar { animation: ttt-bob 1.1s ease-in-out infinite; }
@keyframes ttt-bob { 50% { transform: translateY(-6%) rotate(-3deg); } }
.ttt-card.waiting { filter: grayscale(.7) brightness(.94); }
.ttt-card.winner { animation: ttt-jump .7s cubic-bezier(.2, 1.6, .4, 1) 2; }
@keyframes ttt-jump { 40% { transform: translateY(-26px) scale(1.12) rotate(-4deg); } }
.ttt-thinking { display: none; gap: 5px; position: absolute; bottom: -18px; background: #fff; border: 3px solid var(--ink); border-radius: 999px; padding: 5px 10px; }
.ttt-card.thinking .ttt-thinking { display: flex; }
.ttt-thinking i { width: 9px; height: 9px; border-radius: 50%; background: var(--mc); animation: ttt-dot 1s ease-in-out infinite; }
.ttt-thinking i:nth-child(2) { animation-delay: .15s; }
.ttt-thinking i:nth-child(3) { animation-delay: .3s; }
@keyframes ttt-dot { 50% { transform: translateY(-6px); } }

.ttt-result-art { display: flex; gap: 18px; }
.ttt-result .ttt-star { width: clamp(64px, 12vmin, 110px); height: clamp(64px, 12vmin, 110px); margin-bottom: -8px; }
.ttt-result-avatar { position: relative; width: clamp(96px, 20vmin, 170px); aspect-ratio: 1; }
.ttt-result-avatar img { width: 100%; height: 100%; }
.ttt-result.won .ttt-result-avatar { animation: ttt-jump .8s cubic-bezier(.2, 1.6, .4, 1) infinite; }

/* ---------- Six modes on the menu ---------- */

.mode-btn.tictactoe { --c: var(--sun); --cd: #e0a800; }
@media (min-aspect-ratio: 1/1) {
  .mode-btn { width: clamp(110px, min(18vmin, 14vw), 210px); }
}
@media (max-height: 520px) and (orientation: landscape) {
  .mode-btn { width: clamp(80px, 13.5vw, 130px); }
}

/* ---------- Menu groups: Labyrinth and Other games ---------- */

.mode-groups { display: flex; flex-wrap: wrap; justify-content: center; align-items: flex-start; gap: 12px clamp(20px, 3.5vw, 48px); max-width: 100%; }
.mode-group { display: flex; flex-direction: column; align-items: center; gap: 8px; min-width: 0; }
.mode-group-title {
  margin: 0; font-size: clamp(20px, 3.6vmin, 34px); color: #fff; white-space: nowrap;
  -webkit-text-stroke: .1em var(--ink); paint-order: stroke fill; text-shadow: 0 .08em 0 var(--ink);
}
.mode-group .mode-grid { display: flex; flex-wrap: nowrap; justify-content: center; }
@media (min-aspect-ratio: 1/1) and (min-height: 521px) {
  .mode-group .mode-btn { width: clamp(110px, min(20vmin, 13vw), 210px); }
}
@media (max-aspect-ratio: 1/1) {
  .mode-groups { flex-direction: column; align-items: center; gap: 22px; }
}
@media (max-height: 520px) and (orientation: landscape) {
  .mode-group { gap: 2px; }
  .mode-group-title { font-size: clamp(16px, 5vh, 22px); }
}

/* ---------- Before the game starts: the page's own text ---------- */

/* Crawlers and visitors without JavaScript read this; the game replaces it
   at once. Only the logo shows straight away, as a splash; the rest fades in
   only if the game has not started after 1.5 s. */
.boot {
  margin: auto; max-width: 640px; max-height: 100%; overflow-y: auto;
  text-align: center; font-weight: 700; font-size: 18px; line-height: 1.45;
  user-select: text; -webkit-user-select: text;
}
.boot-logo {
  margin: 0 0 12px; font-size: clamp(44px, 11vmin, 110px); line-height: 1;
  color: var(--sun); -webkit-text-stroke: .06em var(--ink); paint-order: stroke fill; text-shadow: 0 .07em 0 var(--ink);
}
.boot > :not(.boot-logo) { animation: boot-late .4s 1.5s both; }
@keyframes boot-late { from { opacity: 0; visibility: hidden; } }
.boot h2 { margin: 16px 0 4px; font-size: 24px; }
.boot ul { margin: 0; padding: 0; list-style: none; }
.boot li { margin: 4px 0; }
.boot a { color: var(--ink); }

/* ---------- Language switch (menu corner) ---------- */

.icon-btn.lang-btn { --c: #fff; --cd: #d6cbe8; color: var(--ink); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 0; }
.icon-btn.lang-btn svg { width: 40%; height: 40%; }
.lang-btn .lang-code { font-size: 14px; line-height: 1; font-weight: 800; margin-top: 2px; }
.mode-btn .mode-label.long { font-size: .8em; letter-spacing: -.02em; }
/* Narrow phones: five corner buttons at the 64 px minimum, with tighter gaps. */
@media (max-width: 430px) {
  .menu-corner { --btn: 64px; }
  .menu-corner .avatar-btn { width: 64px; height: 64px; padding: 4px; }
  .menu-corner .corner-right, .menu-corner .sound-toggles { gap: 6px; }
}
