/* ExitMaze — hand-drawn sketchbook theme (drawably.dev inspired)
   Paper + ink + markers: wobbly borders, hard offset shadows, graph paper. */
* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --paper: #FAF6EE;      /* warm cream sheet */
  --card: #FFFDF6;       /* cut-paper card */
  --paper-2: #F3EDDF;    /* aged paper chip */
  --ink: #2E2A24;        /* charcoal pen */
  --ink-soft: #6B6459;   /* faded ink */
  --line: #2E2A24;
  --red: #E4572E;        /* marker red (decorative) */
  --red-deep: #C43D1A;   /* solid-button red (AA on white text) */
  --blue: #2E86AB;       /* pen blue (decorative) */
  --blue-deep: #256C8D;  /* solid-button blue */
  --green: #57A64B;      /* marker green */
  --green-text: #38702F;
  --yellow: #F6C445;     /* highlighter */
  --text: var(--ink);
  --muted: var(--ink-soft);
  --accent: var(--red-deep);
  --shadow: 3px 3px 0 rgba(46, 42, 36, .9);
  --font-body: "Kalam", "Comic Sans MS", "Segoe Print", cursive;
  --font-display: "Gochi Hand", "Kalam", "Comic Sans MS", cursive;
}

html, body { height: 100%; }
body {
  font-family: var(--font-body);
  color: var(--ink);
  line-height: 1.5;
  background-color: var(--paper);
  /* graph-paper sheet */
  background-image:
    linear-gradient(rgba(46, 42, 36, .055) 1px, transparent 1px),
    linear-gradient(90deg, rgba(46, 42, 36, .055) 1px, transparent 1px);
  background-size: 24px 24px;
}

.hidden { display: none !important; }

/* wobbly hand-drawn radius variants — deliberately uneven per component */
.wob-a { border-radius: 255px 15px 225px 15px / 15px 225px 15px 255px; }
.wob-b { border-radius: 15px 225px 15px 255px / 225px 15px 255px 15px; }
.wob-c { border-radius: 125px 10px 20px 185px / 25px 205px 205px 25px; }

/* ---------- shared ---------- */
.card {
  background: var(--card);
  border: 2px solid var(--ink);
  border-radius: 255px 15px 225px 15px / 15px 225px 15px 255px;
  padding: 16px;
  box-shadow: var(--shadow);
}

/* icon tokens: one set, one stroke width (2), two sizes */
.ic { width: 20px; height: 20px; flex-shrink: 0; }
.ic-sm { width: 16px; height: 16px; }

.btn {
  font-family: var(--font-body);
  font-weight: 700;
  font-size: 1rem;
  border: 2px solid var(--ink);
  border-radius: 14px 6px 12px 8px / 8px 12px 6px 14px;
  padding: 10px 18px; /* >=44px tall hit area */
  min-height: 44px;
  cursor: pointer;
  color: var(--ink);
  background: var(--card);
  box-shadow: var(--shadow);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: transform .08s ease, box-shadow .08s ease, background-color .08s ease;
}
.btn:hover { transform: translate(-1px, -1px); box-shadow: 4px 4px 0 rgba(46, 42, 36, .9); }
.btn:active { transform: translate(2px, 2px); box-shadow: 1px 1px 0 rgba(46, 42, 36, .9); }
.btn:disabled { opacity: .5; cursor: not-allowed; box-shadow: var(--shadow); transform: none; }
.btn-primary { background: var(--red-deep); color: #fff; }
.btn-secondary { background: var(--blue-deep); color: #fff; }
.btn-big { width: 100%; padding: 14px; font-size: 1.2rem; }
.btn-small { padding: 8px 14px; font-size: .9rem; min-height: 36px; }

/* visible keyboard focus everywhere (WCAG 2.2) */
:focus-visible { outline: 3px solid var(--blue-deep); outline-offset: 2px; }
.btn:focus-visible, .avatar-option:focus-visible, select:focus-visible {
  outline: 3px solid var(--red-deep);
}

.text-input {
  font-family: var(--font-body);
  font-size: 1rem;
  line-height: 1.25; /* single-line field: keep Kalam's ascenders inside the box */
  width: 100%;
  min-height: 46px;
  padding: 8px 14px;
  /* small-scale wobble only — card-size radii (200px+) collapse on short
     controls and warp the shape into the text */
  border-radius: 12px 5px 10px 6px / 6px 10px 5px 12px;
  border: 2px solid var(--ink);
  background: var(--card);
  color: var(--ink);
  outline: none;
}
.text-input::placeholder { color: #8d8474; }
.text-input:focus { box-shadow: var(--shadow); }

.error-text {
  color: var(--red-deep); min-height: 1.4em; margin-top: 10px;
  font-size: .95rem; text-align: center;
}

h3 {
  font-family: var(--font-display);
  color: var(--ink);
  font-size: 1.05rem;
  letter-spacing: .04em;
  margin: 14px 0 8px;
  text-decoration: underline wavy rgba(46, 42, 36, .35) 2px;
  text-underline-offset: 5px;
}

/* ---------- home ---------- */
.screen { min-height: 100dvh; display: flex; align-items: center; justify-content: center; }
#screen-home { padding: 20px; }
.home-layout {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: center;
  gap: 28px; width: 100%; max-width: 1100px;
}
.home-card {
  width: min(460px, 100%);
  background: var(--card);
  border: 2px solid var(--ink);
  border-radius: 255px 15px 225px 15px / 15px 225px 15px 255px;
  padding: 32px;
  box-shadow: 5px 6px 0 rgba(46, 42, 36, .9);
  transform: rotate(-.4deg);
}
.logo {
  font-family: var(--font-display);
  font-size: 2.6rem;
  text-align: center;
  letter-spacing: .02em;
  line-height: 1.15;
}
.logo span { color: var(--red-deep); }
.logo-doodle {
  display: block; margin: 2px auto 0; width: 150px; height: 10px;
  color: var(--yellow);
}
.tagline { text-align: center; color: var(--ink-soft); margin: 10px 0 24px; line-height: 1.5; }
.field-label { display: block; font-size: .95rem; color: var(--ink-soft); margin: 16px 0 6px; }

.avatar-picker { display: grid; grid-template-columns: repeat(8, 1fr); gap: 6px; }
.avatar-option {
  font-family: var(--font-body);
  font-size: 1.4rem;
  padding: 8px 0;
  min-height: 44px;
  text-align: center;
  background: var(--card);
  color: inherit;
  border: 2px solid transparent;
  border-radius: 45% 55% 50% 50% / 55% 45% 55% 45%;
  cursor: pointer;
  transition: transform .08s ease, background-color .08s ease;
}
.avatar-option:hover { transform: rotate(-4deg) scale(1.08); }
.avatar-option.selected {
  border-color: var(--ink);
  background: var(--yellow);
  box-shadow: 2px 2px 0 rgba(46, 42, 36, .9);
}

.join-row { display: flex; gap: 10px; margin-top: 14px; }
.join-row .text-input { flex: 1; min-width: 0; }
.code-input {
  text-transform: uppercase; text-align: center;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 1.25rem; letter-spacing: .3em; font-weight: 700;
}
#btn-create { margin-top: 22px; }

/* ---------- lobby ---------- */
.lobby-layout {
  display: flex; gap: 18px; width: min(1000px, 100%); margin: 20px auto; padding: 0 16px;
  align-items: flex-start;
}
.lobby-main { flex: 1.4; transform: rotate(-.25deg); }
.lobby-right { flex: 1; display: flex; flex-direction: column; gap: 18px; min-width: 0; }
.lobby-chat {
  display: flex; flex-direction: column; min-height: 420px;
  transform: rotate(.35deg);
}
.lobby-chat .chat-panel { flex: 1; }

.lobby-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.lobby-head h2 { font-family: var(--font-display); font-size: 1.7rem; }
.room-code {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-weight: 700;
  color: var(--red-deep); letter-spacing: .12em;
  background: var(--yellow);
  padding: 0 8px;
  border-radius: 40% 60% 55% 45% / 60% 40% 60% 40%;
}

.player-list { list-style: none; display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 8px; }
.player-list li {
  display: flex; align-items: center; gap: 10px;
  background: var(--paper-2);
  border: 2px solid var(--ink);
  border-radius: 12px 5px 10px 6px / 6px 10px 5px 12px;
  padding: 8px 12px;
}
.player-dot { width: 14px; height: 14px; border-radius: 50% 45% 55% 50%; flex-shrink: 0; border: 2px solid var(--ink); }
.player-name { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.player-tags { margin-left: auto; display: flex; gap: 6px; align-items: center; font-size: .85rem; color: var(--ink-soft); }
.player-points { font-weight: 700; color: var(--red-deep); font-variant-numeric: tabular-nums; }

.settings-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.settings-grid label { display: flex; flex-direction: column; gap: 6px; font-size: .9rem; color: var(--ink-soft); }
.settings-grid select {
  font-family: var(--font-body); font-size: .95rem;
  line-height: 1.25;
  padding: 8px 10px; min-height: 46px;
  border-radius: 10px 5px 12px 6px / 6px 12px 5px 10px;
  border: 2px solid var(--ink); cursor: pointer;
  background: var(--card); color: var(--ink); outline: none;
}
.bot-row { display: flex; gap: 10px; margin: 14px 0; }
#btn-start { margin-top: 6px; }

/* ---------- chat ---------- */
.chat-panel { display: flex; flex-direction: column; height: 100%; min-height: 200px; }
.chat-messages { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 4px; padding: 4px 2px; font-size: .98rem; }
.chat-msg { line-height: 1.5; word-wrap: break-word; overflow-wrap: anywhere; }
.chat-msg .who { font-weight: 700; }
.chat-msg.system { color: var(--ink-soft); font-style: italic; font-size: .9rem; }
.chat-input-row { display: flex; gap: 8px; margin-top: 8px; }
.chat-input-row input { flex: 1; min-width: 0; }
.btn-send { width: 44px; padding: 8px 0; }

/* ---------- game ---------- */
#screen-game { align-items: stretch; overscroll-behavior: none; }
.game-layout { display: flex; gap: 12px; width: 100%; height: 100dvh; padding: 12px; }
.game-canvas-wrap { flex: 1; position: relative; min-width: 0; }
#game-canvas {
  width: 100%; height: 100%; display: block;
  background: #1E1B16;
  border: 2px solid var(--ink);
  border-radius: 225px 15px 255px 15px / 15px 255px 15px 225px;
  box-shadow: var(--shadow);
}
.game-sidebar { width: 280px; display: flex; flex-direction: column; gap: 12px; min-height: 0; }

/* HUD floats over the canvas top so phones (no sidebar) still see it */
.hud-box {
  position: absolute; top: 10px; left: 50%; transform: translateX(-50%) rotate(-.5deg);
  z-index: 3;
  display: flex; justify-content: space-between; align-items: center; gap: 14px;
  background: var(--card); border: 2px solid var(--ink);
  border-radius: 12px 5px 10px 6px / 6px 10px 5px 12px;
  padding: 4px 16px; box-shadow: var(--shadow);
  max-width: calc(100% - 24px);
}
.hud-round { font-weight: 700; font-size: 1rem; white-space: nowrap; }
.hud-timer {
  font-family: var(--font-display);
  font-size: 1.5rem; font-weight: 700; font-variant-numeric: tabular-nums;
  color: var(--ink);
}
.hud-timer.low { color: var(--red-deep); animation: pulse .5s infinite alternate; }
@keyframes pulse { to { opacity: .55; } }

/* touch D-pad: hidden unless touch-first device or narrow screen */
.dpad {
  position: absolute; left: 14px; bottom: 14px; z-index: 4;
  display: none;
  grid-template-columns: repeat(3, 56px);
  grid-template-rows: repeat(3, 56px);
  gap: 6px;
  touch-action: none;      /* no scroll/zoom while steering */
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
}
.dpad-btn {
  font: inherit; color: var(--ink);
  background: rgba(255, 253, 246, .88);
  border: 2px solid var(--ink);
  border-radius: 14px 6px 12px 8px / 8px 12px 6px 14px;
  box-shadow: 2px 2px 0 rgba(46, 42, 36, .8);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  touch-action: none;
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
}
.dpad-btn.active { transform: translate(2px, 2px); box-shadow: 0 0 0 rgba(46,42,36,.8); background: var(--yellow); }
.dpad-up { grid-area: 1 / 2; }
.dpad-left { grid-area: 2 / 1; }
.dpad-right { grid-area: 2 / 3; }
.dpad-down { grid-area: 3 / 2; }
@media (hover: none) and (pointer: coarse), (max-width: 700px) {
  .dpad { display: grid; }
}

.scoreboard { overflow-y: auto; flex-shrink: 0; max-height: 40%; }
#scoreboard-list { list-style: none; display: flex; flex-direction: column; gap: 6px; }
#scoreboard-list li {
  display: flex; align-items: center; gap: 8px; background: var(--paper-2);
  border: 2px solid var(--ink);
  border-radius: 10px 5px 12px 6px / 6px 12px 5px 10px;
  padding: 6px 10px; font-size: .95rem;
}
#scoreboard-list .rank { width: 22px; font-weight: 800; color: var(--ink-soft); font-family: var(--font-display); }
#scoreboard-list .me { background: var(--yellow); }
#scoreboard-list .pts { margin-left: auto; font-weight: 700; color: var(--red-deep); font-variant-numeric: tabular-nums; }
#scoreboard-list .done { opacity: .55; }

.game-chat { flex: 1; min-height: 120px; display: flex; }
.game-chat .chat-panel { width: 100%; }
.game-chat .chat-messages { font-size: .9rem; }

/* overlays */
.countdown-overlay, .results-overlay, .podium-overlay {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  border-radius: 225px 15px 255px 15px / 15px 255px 15px 225px;
  background: rgba(250, 246, 238, .82);
  z-index: 5;
}
.countdown-number {
  font-family: var(--font-display);
  font-size: 9rem; font-weight: 700; color: var(--red-deep);
  text-shadow: 4px 4px 0 rgba(46, 42, 36, .25);
  animation: count-pop .3s ease-out;
}
@keyframes count-pop {
  from { transform: scale(1.4) rotate(-4deg); opacity: .4; }
  to { transform: scale(1) rotate(-2deg); opacity: 1; }
}

.results-card, .podium-card {
  background: var(--card); border: 2px solid var(--ink);
  border-radius: 255px 15px 225px 15px / 15px 225px 15px 255px;
  box-shadow: 5px 6px 0 rgba(46, 42, 36, .9);
  padding: 28px 34px; width: min(480px, 92%); text-align: center;
  max-height: 90%; overflow-y: auto;
  transform: rotate(-.5deg);
}
.results-card h2, .podium-card h2 { margin-bottom: 16px; font-family: var(--font-display); font-size: 1.5rem; }
.results-table { width: 100%; border-collapse: collapse; }
.results-table td { padding: 7px 10px; border-bottom: 2px dashed rgba(46, 42, 36, .3); font-size: .98rem; }
.results-table .pos { font-weight: 800; color: var(--red-deep); width: 34px; }
.results-table .name-cell { max-width: 200px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.results-table .time { color: var(--ink-soft); font-variant-numeric: tabular-nums; }
.results-table .gain { color: var(--green-text); font-weight: 700; }

.podium-row { display: flex; align-items: flex-end; justify-content: center; gap: 14px; margin: 22px 0 18px; }
.podium-col { display: flex; flex-direction: column; align-items: center; gap: 6px; width: 110px; }
.podium-col .avatar { font-size: 2.6rem; }
.podium-col .name { font-weight: 700; font-size: .95rem; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.podium-col .bar {
  width: 100%; border: 2px solid var(--ink);
  border-radius: 10px 10px 40% 40% / 10px 10px 20% 20%;
  background: var(--paper-2);
  display: flex; align-items: center; justify-content: center;
  font-family: var(--font-display); font-weight: 700; font-size: 1.4rem;
}
.podium-col.p1 .bar { height: 110px; background: var(--yellow); }
.podium-col.p2 .bar { height: 78px; }
.podium-col.p3 .bar { height: 56px; }
.podium-standings { list-style: none; text-align: left; margin: 10px 0; }
.podium-standings li { display: flex; gap: 8px; padding: 4px 8px; border-bottom: 2px dashed rgba(46, 42, 36, .25); font-size: .95rem; }
.podium-standings .pts { margin-left: auto; font-weight: 700; color: var(--red-deep); font-variant-numeric: tabular-nums; }
.podium-actions { margin-top: 14px; }

/* ---------- ads ---------- */
.ad-rail { width: 300px; max-width: 100%; }
.ad-label {
  font-size: .68rem; color: var(--ink-soft); text-transform: uppercase;
  letter-spacing: .12em; text-align: center; margin-bottom: 4px;
}
.ad-slot { min-height: 100px; } /* reserved space: no layout shift when ads load */
.ad-rail-game { flex-shrink: 0; margin-top: auto; }
.ad-rail-overlay { width: min(320px, 90%); }

.overlay-col {
  display: flex; flex-direction: column; align-items: center; gap: 12px;
  max-height: 100%; padding: 10px;
}

/* house placeholder shown until AdSense is configured */
.house-ad {
  border: 2px dashed rgba(46, 42, 36, .45);
  border-radius: 12px 5px 10px 6px / 6px 10px 5px 12px;
  background: var(--paper-2);
  padding: 14px;
  display: flex; align-items: center; gap: 12px;
  color: var(--ink-soft);
  transform: rotate(.3deg);
}
.house-ad-mark {
  font-family: var(--font-display);
  font-size: .8rem; font-weight: 700; letter-spacing: .08em;
  border: 2px solid var(--ink-soft); border-radius: 6px;
  padding: 2px 7px; flex-shrink: 0;
}
.house-ad-body { display: flex; flex-direction: column; gap: 2px; font-size: .82rem; line-height: 1.35; }
.house-ad-body strong { color: var(--ink); }
.house-ad-body code { font-size: .78rem; }

@media (max-width: 1023px) {
  .ad-rail { width: min(468px, 100%); }
  .home-layout { gap: 20px; }
}

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

/* snappier taps on touch: kill double-tap-zoom delay on controls */
.btn, .avatar-option, .dpad-btn, select { touch-action: manipulation; }

/* ---------- small screens (375px target) ---------- */
@media (max-width: 480px) {
  .home-card { padding: 20px; }
  .avatar-picker { grid-template-columns: repeat(4, 1fr); }
  .countdown-number { font-size: 5rem; }
  .dpad {
    grid-template-columns: repeat(3, 52px);
    grid-template-rows: repeat(3, 52px);
    gap: 5px;
  }
  .game-layout { padding: 8px; gap: 8px; }
}
@media (max-width: 620px) {
  .settings-grid { grid-template-columns: 1fr; }
  .join-row { flex-direction: column; }
}

@media (max-width: 860px) {
  .game-sidebar { display: none; }
  .lobby-layout { flex-direction: column; }
  .lobby-main, .lobby-chat, .results-card, .podium-card { transform: none; }
}
