/* Ridgeline, alpine sky, slate rock, snow and trail flags. Calm fixed light palette. */
:root {
  color-scheme: light;
  --bg: #e9f0f3;
  --bg-2: #dce8ee;
  --card: #ffffff;
  --ink: #1f2a36;
  --muted: #667585;
  --line: #d6dfe6;
  --rock: #5d6b7b;
  --accent: #d9622b;
  --accent-dark: #b44d1d;
  --gold: #e8b33a;
  --danger: #b8322a;
  --safe: #2f7a57;
  --shadow: 0 1px 2px rgba(31, 42, 54, .08), 0 0 0 1px var(--line);
  /* tutorial kit (tutorial.css) */
  --tut-accent: #e8b33a;
  --tut-accent-ink: #1f2a36;
  --tut-bubble: #ffffff;
  --tut-ink: #1f2a36;
  --tut-muted: #667585;
  --tut-dim: rgba(20, 30, 42, .55);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
html, body { height: 100%; }
body {
  margin: 0;
  background: linear-gradient(180deg, var(--bg-2) 0%, var(--bg) 40%, #eef3f0 100%) fixed;
  color: var(--ink);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  touch-action: manipulation;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; touch-action: manipulation; }
[hidden] { display: none !important; }

/* ---------- top bar ---------- */
.app {
  position: relative;
  display: flex; flex-direction: column;
  height: 100vh; height: 100dvh;
  max-width: 1200px; margin: 0 auto;
  padding: env(safe-area-inset-top) max(12px, env(safe-area-inset-right)) env(safe-area-inset-bottom) max(12px, env(safe-area-inset-left));
}
.topbar { flex: none; display: grid; grid-template-columns: 48px 1fr 48px; align-items: center; min-height: 52px; }
.back {
  display: inline-flex; align-items: center; justify-content: center; width: 44px; height: 44px;
  color: var(--muted); text-decoration: none; font-size: 24px; border-radius: 22px;
}
.back:hover, .back:focus-visible { color: var(--ink); background: rgba(255, 255, 255, .6); }
.title { margin: 0; text-align: center; font-size: 19px; font-weight: 750; letter-spacing: .04em; }
.iconbtn {
  width: 44px; height: 44px; border: 0; border-radius: 22px; background: transparent;
  font-size: 22px; line-height: 1; color: var(--ink); cursor: pointer; justify-self: end;
}
.iconbtn:hover, .iconbtn[aria-expanded="true"] { background: rgba(255, 255, 255, .7); }

.menu {
  position: absolute; z-index: 15; top: calc(env(safe-area-inset-top) + 50px); right: max(12px, env(safe-area-inset-right));
  min-width: 220px; padding: 6px; border-radius: 16px; background: var(--card);
  box-shadow: 0 12px 32px rgba(31, 42, 54, .2), 0 0 0 1px var(--line);
  display: flex; flex-direction: column; animation: rise .16s ease-out;
}
.menu button {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  min-height: 48px; padding: 0 14px; border: 0; border-radius: 10px; background: transparent;
  text-align: left; font-weight: 600; cursor: pointer;
}
.menu button:hover, .menu button:focus-visible { background: var(--bg); outline: none; }
.tog { width: 36px; height: 22px; border-radius: 11px; background: #cdd6de; position: relative; flex: none; transition: background .15s; }
.tog::after { content: ''; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform .15s; box-shadow: 0 1px 2px rgba(0, 0, 0, .2); }
[aria-checked="true"] .tog { background: var(--safe); }
[aria-checked="true"] .tog::after { transform: translateX(14px); }

.btn {
  min-height: 48px; padding: 0 20px; border-radius: 24px;
  border: 0; background: var(--card); color: var(--ink); box-shadow: var(--shadow);
  font-weight: 700; cursor: pointer; transition: transform .12s, background .15s, opacity .15s, box-shadow .15s;
}
.btn:active { transform: scale(.97); }
.btn.ghost { background: var(--card); }
.btn.primary { background: var(--accent); color: #fff; box-shadow: 0 2px 0 var(--accent-dark); }
.btn.go { background: var(--safe); color: #fff; box-shadow: 0 2px 0 #215a40; }
.btn.wide { width: 100%; min-height: 52px; font-size: 17px; }
.btn:disabled { opacity: .4; cursor: default; transform: none; }
.btn:focus-visible, .opt:focus-visible, .choice button:focus-visible, .chip:focus-visible, .link:focus-visible, .iconbtn:focus-visible, .back:focus-visible {
  outline: 3px solid var(--gold); outline-offset: 2px;
}

/* ---------- layout (portrait first) ---------- */
.game {
  flex: 1; min-height: 0;
  display: flex; flex-direction: column; gap: 8px;
  padding: 0 0 10px; width: 100%; max-width: 620px; margin: 0 auto;
}
.players { flex: none; display: flex; gap: 6px; }
.board-wrap { flex: 1 1 auto; min-height: 0; display: flex; align-items: center; justify-content: center; --ar: .966; }
.controls { flex: none; }

/* ---------- climber chips ---------- */
.chip {
  flex: 1 1 0; min-width: 0; display: flex; align-items: center; justify-content: space-between; gap: 6px;
  min-height: 40px; padding: 0 10px 0 12px; border-radius: 20px; border: 0;
  background: rgba(255, 255, 255, .55); font-size: 14px; font-weight: 650; cursor: pointer;
  box-shadow: 0 0 0 1px rgba(31, 42, 54, .06);
  transition: background .2s, box-shadow .2s;
}
.chip .chip-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip .pips { height: 12px; width: auto; flex: none; }
.chip.active { background: #fff; box-shadow: 0 0 0 2px var(--pc), 0 2px 8px rgba(31, 42, 54, .12); }
.chip.winner { background: #fff6dc; box-shadow: 0 0 0 2px var(--gold); }

/* ---------- status (one line) ---------- */
.status {
  flex: none; display: flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 26px; font-weight: 700; font-size: 16px; text-align: center; white-space: nowrap; overflow: hidden;
}
.status::before { content: ''; width: 10px; height: 10px; border-radius: 50%; background: var(--pc, transparent); flex: none; }
#status-main { overflow: hidden; text-overflow: ellipsis; }
#status-main.warn { color: var(--danger); }
#status-main.good { color: var(--safe); }
.thinking::after { content: ''; display: inline-block; width: 1.2em; text-align: left; animation: dots 1.2s steps(4) infinite; }
@keyframes dots { 0% { content: ''; } 25% { content: '.'; } 50% { content: '..'; } 75% { content: '...'; } }

/* ---------- board ---------- */
.board {
  display: block; width: 100%; height: 100%;
  border-radius: 16px; background: #dcebf2;
  box-shadow: 0 8px 22px rgba(31, 42, 54, .16);
}
@supports (container-type: size) {
  .board-wrap { container-type: size; }
  .board { width: min(100cqw, calc(100cqh * var(--ar))); height: auto; aspect-ratio: var(--ar); }
}
.board .cell { fill: rgba(255, 255, 255, .78); stroke: rgba(31, 42, 54, .2); stroke-width: 1; }
.board .cell.top { fill: #fffdf6; stroke: rgba(154, 112, 20, .5); stroke-dasharray: 3 2; }
.board .col.closed .cell { fill: rgba(255, 255, 255, .45); }
.board .target { fill: none; stroke: var(--safe); stroke-width: 2.4; stroke-dasharray: 4 3; }
.board .target.chosen { stroke: var(--accent); stroke-width: 3.6; stroke-dasharray: none; animation: flash .5s ease-out; }
@keyframes flash { from { stroke-width: 7; opacity: .4; } to { stroke-width: 3.6; opacity: 1; } }
.board .guide-ring { fill: none; stroke-width: 2; opacity: .7; transform-box: fill-box; transform-origin: center; animation: ring 1.4s ease-out infinite; }
@keyframes ring { from { transform: scale(.8); opacity: .8; } to { transform: scale(1.6); opacity: 0; } }
.board .guide.lost { opacity: .45; }
.board .lost-x { stroke: var(--danger); stroke-width: 3; stroke-linecap: round; }
.board .topped { fill: #fff2c7; stroke: var(--gold); stroke-width: 2.5; stroke-dasharray: none; animation: glow 1.2s ease-in-out infinite alternate; }
@keyframes glow { from { fill: #fff8e2; } to { fill: #ffe39a; } }
.board .fresh { transform-box: fill-box; transform-origin: center; animation: pop .5s ease-out; }
@keyframes pop { 0% { transform: scale(.4); opacity: .3; } 70% { transform: scale(1.3); } 100% { transform: scale(1); opacity: 1; } }
.board .flag.fresh { transform-origin: left bottom; animation: raise .7s ease-out; }
@keyframes raise { from { transform: scaleY(0); } to { transform: scaleY(1); } }
.board .route-num { font: 800 13px system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif; text-anchor: middle; dominant-baseline: central; pointer-events: none; }
.board .len-num { font: 700 8.5px system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif; text-anchor: middle; fill: rgba(31, 42, 54, .5); }

/* ---------- controls: dice, actions, climbs ---------- */
.controls { min-height: 140px; display: flex; flex-direction: column; justify-content: center; gap: 10px; }
.tray { position: relative; display: flex; align-items: center; justify-content: center; min-height: 52px; }
.dice { display: flex; gap: 10px; }
.die { width: 54px; height: 54px; display: block; transition: transform .15s; }
.die .face { fill: #fffdf8; stroke: #2a3746; stroke-width: 1.6; }
.die .pip { fill: #1f2a36; }
.die.blank .face { fill: rgba(255, 255, 255, .5); stroke: #a9b5c0; stroke-dasharray: 4 3; }
.dice.rolling .die { animation: shake .16s linear infinite alternate; }
.dice.rolling .die:nth-child(2n) { animation-direction: alternate-reverse; }
@keyframes shake { from { transform: rotate(-9deg) translateY(-2px); } to { transform: rotate(9deg) translateY(1px); } }
.dice.bust .face { fill: #fde6e3; stroke: var(--danger); }
.dice.landed .die { animation: land .25s ease-out; }
@keyframes land { from { transform: scale(1.15); } to { transform: scale(1); } }

.risk {
  position: absolute; right: 0; top: 50%; transform: translateY(-50%);
  display: flex; flex-direction: column; align-items: center; line-height: 1.1;
  font-size: 11px; color: var(--muted); padding: 4px 7px; border-radius: 12px; background: rgba(255, 255, 255, .8);
  box-shadow: 0 0 0 1px var(--line); white-space: nowrap;
}
.risk b { font-size: 14px; }
.risk b { color: var(--safe); }
.risk.mid b { color: #a4760b; }
.risk.high b { color: var(--danger); }

.actions { display: flex; gap: 10px; justify-content: center; }
.actions .btn { flex: 1 1 0; max-width: 260px; min-height: 58px; font-size: 18px; display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1.1; }
.actions .btn small { font-weight: 600; font-size: 12px; opacity: .9; }
.actions .btn.picked { box-shadow: 0 0 0 4px rgba(232, 179, 58, .8); opacity: 1; }

.pairings { display: grid; grid-template-columns: repeat(auto-fit, minmax(96px, 1fr)); gap: 8px; }
.opt {
  min-height: 64px; padding: 4px 6px; border-radius: 16px;
  border: 0; background: var(--card); color: var(--ink); cursor: pointer; box-shadow: 0 0 0 2px var(--safe), 0 2px 6px rgba(31, 42, 54, .08);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; line-height: 1.1;
  transition: transform .12s, background .15s, box-shadow .15s;
}
.opt:active { transform: scale(.97); }
.opt .opt-main { font-weight: 800; font-size: 20px; white-space: nowrap; }
.opt .opt-dice { display: flex; gap: 6px; }
.opt .pr { display: inline-flex; gap: 1px; }
.opt .die { width: 15px; height: 15px; }
.opt .die .face { stroke-width: 3; }
.opt .die .pip { r: 4.6; }
.opt .opt-sub { font-size: 11px; color: var(--muted); white-space: nowrap; }
.opt:disabled { cursor: default; opacity: .6; box-shadow: 0 0 0 1px var(--line); }
.opt.chosen { opacity: 1; background: #fff0e4; box-shadow: 0 0 0 3px var(--accent), 0 2px 8px rgba(217, 98, 43, .25); }

/* ---------- overlays ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 20;
  background: rgba(20, 28, 38, .4);
  display: flex; align-items: flex-end; justify-content: center; padding: 12px;
  animation: fade .18s ease-out;
}
.overlay.soft { background: rgba(233, 240, 243, .55); align-items: center; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@media (min-width: 600px) { .overlay { align-items: center; } }
.sheet {
  background: var(--card); color: var(--ink);
  width: 100%; max-width: 440px; max-height: calc(100vh - 24px); max-height: calc(100dvh - 24px);
  overflow: auto; overscroll-behavior: contain;
  border-radius: 24px; padding: 18px 20px 20px;
  box-shadow: 0 20px 50px rgba(20, 28, 38, .25);
  animation: rise .22s ease-out;
}
@keyframes rise { from { transform: translateY(14px); opacity: .6; } to { transform: none; opacity: 1; } }
.sheet h2 { margin: 0 0 6px; font-size: 21px; }
.sheet-actions { display: flex; gap: 10px; margin-top: 18px; }
.sheet-actions .btn { flex: 1; }

.choice { border: 0; margin: 14px 0 0; padding: 0; min-width: 0; }
.choice legend { font-size: 13px; color: var(--muted); padding: 0; margin-bottom: 6px; }
.seg { display: flex; gap: 4px; padding: 4px; border-radius: 16px; background: var(--bg); }
.seg button {
  flex: 1 1 0; min-height: 44px; padding: 0 10px; border-radius: 12px;
  border: 0; background: transparent; font-weight: 600; color: var(--muted); cursor: pointer;
}
.seg button[aria-pressed="true"] { background: var(--card); color: var(--ink); box-shadow: 0 1px 3px rgba(31, 42, 54, .18); }

.over-sheet { text-align: center; max-width: 360px; padding-bottom: 10px; }
.over-mark { display: flex; justify-content: center; }
.over-mark svg { width: 96px; height: 76px; }
.over-sheet h2 { font-size: 28px; margin: 4px 0 2px; }
.over-way { margin: 0 0 12px; color: var(--muted); font-size: 14px; }
.over-sheet .btn.wide { margin-top: 16px; }
.link { display: block; margin: 6px auto 0; min-height: 44px; padding: 0 16px; border: 0; background: transparent; color: var(--muted); cursor: pointer; }

.standings { width: 100%; border-collapse: collapse; font-size: 14px; text-align: left; }
.standings th { font-size: 12px; color: var(--muted); padding: 4px 6px; font-weight: 600; }
.standings td { padding: 8px 6px; border-top: 1px solid var(--line); vertical-align: middle; }
.standings td.num, .standings th.num { text-align: right; font-variant-numeric: tabular-nums; }
.standings tr.win td, .standings tr.now td { background: #f6f9fb; font-weight: 700; }
.standings tr.win td { background: #fff6dc; }
.standings .mk { width: 14px; height: 14px; vertical-align: -2px; margin-right: 6px; }
.standings .flags svg { width: 10px; height: 13px; margin: 0 1px 0 0; }
.standings small { color: var(--muted); font-weight: 500; font-size: 11px; }
.standings .camps { font-size: 13px; }
.standings .camps small { margin: 0 2px 0 1px; }

.help-sheet, .info-sheet { max-width: 620px; padding: 0; display: flex; flex-direction: column; }
.help-head {
  position: sticky; top: 0; display: flex; align-items: center; justify-content: space-between;
  padding: 8px 10px 8px 20px; background: var(--card); border-bottom: 1px solid var(--line); z-index: 1;
}
.help-head h2 { margin: 0; font-size: 20px; }
.help-body { padding: 4px 20px 20px; }
.help-body h3 { font-size: 16px; margin: 18px 0 4px; }
.help-body p, .help-body li { font-size: 15px; }
.help-body ul, .help-body ol { padding-left: 20px; margin: 6px 0; }
.help-body li { margin: 4px 0; }

.info-body { padding: 8px 20px 0; }
.info-line { font-size: 14px; margin: 10px 0 0; }
.info-line.muted { color: var(--muted); }
.info-h { font-size: 13px; color: var(--muted); font-weight: 600; margin: 18px 20px 0; }
.log { list-style: none; margin: 0; padding: 4px 20px 20px; font-size: 14px; }
.log li { padding: 5px 0 5px 16px; border-bottom: 1px solid var(--line); position: relative; }
.log li::before { content: ''; position: absolute; left: 2px; top: 11px; width: 8px; height: 8px; border-radius: 50%; background: var(--pc, var(--muted)); }
.log li.turn { font-weight: 700; color: var(--ink); }
.log li.bad { color: var(--danger); }
.log li.flag { color: #7a5a06; font-weight: 700; }

/* ---------- small phones ---------- */
@media (max-width: 380px) {
  .app { padding-left: max(8px, env(safe-area-inset-left)); padding-right: max(8px, env(safe-area-inset-right)); }
  .chip { padding: 0 8px 0 10px; font-size: 13px; }
  .chip .pips { height: 11px; }
  .die { width: 44px; height: 44px; }
  .dice { gap: 8px; }
  .pairings { grid-template-columns: repeat(auto-fit, minmax(86px, 1fr)); }
}
/* ---------- short portrait screens ---------- */
@media (max-aspect-ratio: 1/1) and (max-height: 700px) {
  .topbar { min-height: 46px; }
  .game { gap: 6px; }
  .controls { min-height: 116px; gap: 8px; }
  .die { width: 44px; height: 44px; }
  .actions .btn { min-height: 50px; }
  .opt { min-height: 54px; }
}
/* Four climbers on a narrow phone: two rows of chips. */
@media (max-width: 480px) {
  .players.n4 { flex-wrap: wrap; }
  .players.n4 .chip { flex: 1 1 40%; min-height: 34px; }
}

/* ---------- wide / landscape: board left, controls right ---------- */
@media (min-aspect-ratio: 1/1) and (min-width: 640px) {
  .game {
    max-width: 1200px;
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(300px, 380px);
    grid-template-rows: 1fr auto auto auto 1fr;
    grid-template-areas: "board ." "board players" "board status" "board controls" "board .";
    column-gap: 28px; row-gap: 14px;
  }
  .board-wrap { grid-area: board; }
  .players { grid-area: players; flex-wrap: wrap; }
  .players .chip { flex: 1 1 40%; }
  .status { grid-area: status; }
  .controls { grid-area: controls; min-height: 140px; }
  .pairings { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

/* Tutorial kit: lift the first-visit chip above the Roll / Stop button at the bottom of the screen. */
@media (max-aspect-ratio: 1/1) {   /* portrait: the button spans the bottom; tutorial.css loads later, hence body */
  body .tut-chip { bottom: calc(86px + env(safe-area-inset-bottom, 0px)); }
}

/* Online play (online.js): a presence line under the status line, in the same row. */
.status-row { flex: none; display: flex; flex-direction: column; align-items: center; gap: 2px; min-width: 0; }
.net { color: var(--muted); }
.over-sheet #over-leave { margin-top: 8px; }
@media (min-aspect-ratio: 1/1) and (min-width: 640px) {
  .status-row { grid-area: status; }
}
