:root {
  --bg: #07080b;
  --panel: rgba(8, 10, 16, 0.86);
  --text: #e8ebf2;
  --muted: #8d95a8;
  --accent: #f5d326;
  --line: #2b303b;
  --touch-h: 0px;
  color-scheme: dark;
}

@media (pointer: coarse) {
  :root { --touch-h: 112px; }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", "Noto Sans TC", sans-serif;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
}
html, body { overflow: hidden; }

#app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

#stage {
  position: relative;
  aspect-ratio: 416 / 400;
  width: min(100vw, calc((100dvh - var(--touch-h)) * 1.04), 832px);
  touch-action: none;
}

#game {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}
/* Modern theme: canvas at device pixels, smooth scaling. */
#game.modern { image-rendering: auto; }

.overlay {
  position: absolute;
  inset: 12% 6% 4%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: safe center;
  gap: 12px;
  padding: 16px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  text-align: center;
  /* Menus never scroll: src/fit.js shrinks the children (--fit) until they fit the box. */
  overflow: hidden;
}
.overlay > * { zoom: var(--fit, 1); flex-shrink: 0; }

.hidden { display: none !important; }

h1 {
  margin: 0;
  font-size: clamp(22px, 6vw, 34px);
  letter-spacing: 0.06em;
  color: var(--accent);
}
h1 small {
  display: block;
  font-size: 0.5em;
  letter-spacing: 0.2em;
  color: var(--muted);
}
h2 { margin: 0; font-size: clamp(24px, 7vw, 36px); }
p { margin: 0; }

.row { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }

button {
  font: inherit;
  font-weight: 600;
  color: var(--text);
  background: #1b1f28;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 18px;
  min-height: 44px;
  cursor: pointer;
}
button:hover { background: #252a35; }
button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button.primary { background: var(--accent); color: #1a1600; border-color: var(--accent); }
button.primary:hover { background: #ffe24d; }

.modes {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  width: min(100%, 420px);
}
.mode { display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 12px 8px; }
.mode b { font-size: 17px; }
.mode span { font-size: 11px; font-weight: 400; opacity: 0.75; }
.mode.online { border-color: #4a7bd1; }
.mode.online b { color: #8fb4ff; }
.mode.daily {
  grid-column: span 2; position: relative; padding: 8px 10px; gap: 2px;
  border: 2px solid #e0b92e; background: linear-gradient(180deg, #2b2410, #17140a);
  animation: dailyGlow 2.2s ease-in-out infinite;
}
.mode.daily:hover { background: linear-gradient(180deg, #3a3014, #211c0d); }
.mode.daily b { color: var(--accent); font-size: 19px; letter-spacing: 0.02em; }
.mode.daily span { font-size: 12px; opacity: 0.95; }
.dailyTag { font-style: normal; font-size: 10px; letter-spacing: 0.14em; color: #1a1600; background: var(--accent); padding: 1px 8px; border-radius: 99px; font-weight: 700; }
.dailyStreak { font-size: 12px; color: #ff9a4a; font-weight: 700; }
.dailyBadge {
  position: absolute; top: -9px; right: -7px; width: 22px; height: 22px; border-radius: 50%;
  background: #ff3b3b; color: #fff; font: 700 14px/22px system-ui, sans-serif; text-align: center;
  box-shadow: 0 0 0 2px var(--bg); animation: dailyPing 1.2s ease-in-out infinite;
}
.dailyBadge.hidden, .dailyStreak.hidden { display: none; }
@keyframes dailyGlow {
  0%, 100% { box-shadow: 0 0 6px #e0b92e55; }
  50% { box-shadow: 0 0 18px #e0b92ecc; }
}
@keyframes dailyPing { 50% { transform: scale(1.25); } }
.dailyNudge { display: flex; flex-direction: column; align-items: center; gap: 6px; margin-top: 4px; padding-top: 10px; border-top: 1px dashed #5a4a14; }
.dailyNudge.hidden { display: none; }
.dailyNudge p { margin: 0; font-size: 13px; color: var(--muted); }
.dailyCta { background: linear-gradient(180deg, #ffe24d, #e0b92e); color: #1a1600; border: 2px solid #ffe24d; font-weight: 700; animation: dailyGlow 2.2s ease-in-out infinite; }
.dailyCta:hover { background: #ffe24d; }
@media (prefers-reduced-motion: reduce) { .mode.daily, .dailyBadge, .dailyCta { animation: none; } }

/* Name field on the title screen */
.namerow { align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.namerow input {
  font: inherit; font-size: 15px; width: 150px; padding: 5px 8px; min-height: 34px;
  color: var(--text); background: #11141b; border: 1px solid var(--line); border-radius: 8px;
  -webkit-user-select: text; user-select: text;
}

/* Shirt colour picker (src/shirts.js) */
.shirts { align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.shirts button {
  width: 26px; height: 26px; min-height: 0; padding: 0; border-radius: 50%;
  border: 2px solid #0008; box-shadow: 0 0 0 1px var(--line);
}
.shirts button[aria-pressed="true"] { box-shadow: 0 0 0 2px var(--text); }
.shirts button.random {
  background: conic-gradient(from 120deg, #f0453c, #ff8a26, #ffe14a, #3fb84a, #1bbcaa, #3d8cf2, #9a63d6, #f56cbd, #e4e6ea, #3a3f4a, #f0453c);
  font-size: 11px; font-weight: 700; color: #fff; text-shadow: 0 0 2px #000c;
}

/* Daily */
#dailyStatus { font-size: 15px; }
.dailyResult { font-size: 15px; min-height: 1.3em; }
.dailyResult .best { color: var(--accent); font-weight: 700; }
.dailyList { columns: 2; column-gap: 28px; font-size: 13px; margin: 0; }
.dailyList li { white-space: nowrap; break-inside: avoid; }
.dailyList li.self { color: var(--accent); font-weight: 700; }
.dailyList li .ticks { color: var(--muted); font-size: 11px; }

button.small { min-height: 0; padding: 6px 12px; font-size: 13px; font-weight: 400; color: var(--muted); }

.segmented { gap: 0; }
.segmented button { border-radius: 0; min-width: 80px; }
.segmented button:first-child { border-radius: 8px 0 0 8px; }
.segmented button:last-child { border-radius: 0 8px 8px 0; }
.segmented button[aria-pressed="true"] { background: #39404f; color: var(--accent); }

.toggles { font-size: 14px; color: var(--muted); gap: 4px 14px; }
.toggles label { display: inline-flex; align-items: center; gap: 4px; cursor: pointer; }
/* Retro / Modern look toggle, sits in the toggles row next to Sound. */
.toggles .themes { display: inline-flex; }
#title .toggles .themes button { min-width: 0; min-height: 0; padding: 3px 9px; font-size: 13px; font-weight: 400; }

.records { border-collapse: collapse; font-size: 14px; min-width: 200px; }
.records caption { color: var(--muted); font-size: 12px; padding-bottom: 4px; }
.records td { padding: 2px 10px; border-top: 1px solid var(--line); }
.records td:first-child { color: var(--muted); }
.records td:last-child { text-align: right; font-variant-numeric: tabular-nums; }

.hint { font-size: 12px; color: var(--muted); }
.new-record { color: var(--accent); font-weight: 700; margin-bottom: 6px; }

#recordForm { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
#recordForm .new-record { flex-basis: 100%; }
#nameInput {
  font: inherit;
  color: var(--text);
  background: #11141b;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 12px;
  width: 12em;
  -webkit-user-select: text;
  user-select: text;
}

#pauseBtn {
  position: absolute;
  top: 1.5%;
  right: 1.5%;
  min-height: 0;
  padding: 2px 8px;
  font-size: 11px;
  opacity: 0.6;
}

#touch { display: none; }
.touch-only { display: none; }

@media (pointer: coarse) {
  #touch {
    display: flex;
    gap: 16px;
    width: min(100vw, 520px);
    height: var(--touch-h);
    padding: 12px 16px;
  }
  #touch button {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 18px;
    padding: 0;
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
    background: #161a22;
    transition: background 60ms, transform 60ms;
  }
  #touch button:hover { background: #161a22; }
  #touch button.pressed {
    background: #39404f;
    border-color: var(--accent);
    transform: scale(0.97);
  }
  #touch button.pressed .arrow { border-left-color: var(--accent); border-right-color: var(--accent); }
  .arrow { width: 0; height: 0; border-block: 18px solid transparent; pointer-events: none; }
  .arrow.left { border-right: 26px solid var(--text); margin-right: 6px; }
  .arrow.right { border-left: 26px solid var(--text); margin-left: 6px; }
  /* Landscape phones: no room below the field, so float the pads at the screen edges. */
  @media (orientation: landscape) and (max-height: 500px) {
    :root { --touch-h: 0px; }
    #touch {
      position: fixed;
      inset: 0;
      width: auto;
      height: auto;
      padding: 0;
      justify-content: space-between;
      align-items: flex-end;
      pointer-events: none;
    }
    #touch button {
      flex: 0 0 auto;
      pointer-events: auto;
      width: max(96px, calc((100vw - 100dvh * 1.04) / 2 - 16px));
      height: 55dvh;
      margin: 0 max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
      opacity: 0.85;
    }
  }
  .kbd-only { display: none; }
  .mode.primary { grid-column: span 2; } /* 2P is hidden: Daily + 1P span the row, Quick/Party share one */
  .touch-only { display: block; }
}

.stack { display: flex; flex-direction: column; align-items: center; gap: 10px; }
.error { color: #ff7b7b; font-size: 14px; min-height: 1em; }
.code { font-size: 1.5em; letter-spacing: 0.15em; color: var(--accent); }
#roomInput {
  font: inherit;
  font-size: 18px;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  text-align: center;
  width: 6em;
  color: var(--text);
  background: #11141b;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px;
  -webkit-user-select: text;
  user-select: text;
}
#netStatus {
  position: absolute;
  left: 50%;
  bottom: 2%;
  transform: translateX(-50%);
  padding: 3px 10px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.6);
  color: var(--text);
  font-size: 12px;
  pointer-events: none;
  white-space: nowrap;
}
button:disabled { opacity: 0.45; cursor: default; }

/* Party run leaderboard on the death overlay */
.boardTitle { margin: 6px 0 2px; font-size: 13px; }
.board { margin: 0 0 8px; padding: 0; list-style: none; font-size: 13px; text-align: left; display: inline-block; }
.board li.left { opacity: .55; }

/* Party run death overlay: a compact panel at the bottom so the spectator camera stays visible. */
#ghostOver {
  inset: auto 4% 3%;
  max-height: 46%;
  gap: 4px;
  padding: 8px 12px;
  background: color-mix(in srgb, var(--panel) 82%, transparent);
}
#ghostOver h2 { margin: 0; font-size: 18px; }
#ghostOver p { margin: 0; }
#ghostOver .boardTitle { display: none; }
#ghostOver .board { margin: 0; font-size: 12px; columns: 2; column-gap: 16px; }

/* Phones: the field is too small to hold the menus, so menus take the whole
   viewport (they replace the field anyway) and use a compact layout that fits
   without scrolling in portrait and landscape. */
@media (pointer: coarse), (max-height: 520px) {
  #title, #online, #daily, #dailyBoard {
    position: fixed;
    inset: 0;
    z-index: 5;
    border: 0;
    border-radius: 0;
    background: var(--bg);
    padding: max(10px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
    gap: clamp(6px, 1.6dvh, 14px);
  }
  #title h1 { font-size: clamp(20px, 4.5dvh, 34px); }
  #title .mode { padding: clamp(6px, 1.4dvh, 12px) 6px; min-height: 0; }
  #title .mode b { font-size: clamp(14px, 2.6dvh, 17px); }
  #title .segmented button { min-height: 38px; padding: 6px 12px; min-width: 70px; }
  #title .records { font-size: 13px; }
  #title .records td { padding: 1px 8px; }
}
@media (orientation: landscape) and (max-height: 520px) {
  #title {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    grid-auto-flow: dense;
    align-content: safe center;
    justify-items: center;
    column-gap: 20px;
    row-gap: 8px;
  }
  #title.hidden { display: none; }
  #title > h1, #title > .modes, #title > #onlineBtn { grid-column: 1; }
  #title > .segmented, #title > .toggles, #title > .namerow, #title > .shirts, #title > .records, #title > .hint { grid-column: 2; }
  #title > h1 { grid-row: 1; }
  #title > .modes { grid-row: 2 / span 3; align-self: center; }
  #title > #onlineBtn { grid-row: 5; }
  #title .mode span { display: none; }
  #title .records caption { padding-bottom: 2px; }
}
