:root {
  --panel: #c8322f;
  --panel-dark: #9d1f1f;
  --accent: #ffd75e;
  --cream: #ffeedd;
  --cream-deep: #f6dcc4;
  --ink: #5a2a1e;
  --font: ui-rounded, "Baloo 2", "Chalkboard SE", "Trebuchet MS", system-ui, sans-serif;
  /* Notch and home-indicator clearance; 0 on screens without them. */
  --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);
  /* Sizes of the side panels. Each follows the width on a roomy screen and
     the height on a short one, so a phone held sideways gets full-size touch
     targets that still fit between the top and bottom edge. */
  --avatar: clamp(34px, min(max(6vw, 46px), 13vh), 92px);
  --goal: clamp(28px, min(max(4.4vw, 36px), 10.4vh), 66px);
  --tool: clamp(30px, min(max(4.6vw, 42px), 12.4vh), 70px);
  --gap: clamp(4px, min(0.7vw, 1.6vh), 11px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: #171021;
  font-family: var(--font);
  color: #fff;
  user-select: none;
  touch-action: manipulation;
}

#stage { position: fixed; inset: 0; }
#board { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
#safe-probe { position: fixed; left: 0; top: 0; width: 0; height: 0; visibility: hidden; pointer-events: none; padding: var(--safe-t) var(--safe-r) var(--safe-b) var(--safe-l); }

/* ------------------------------------------------------------------ panels */
.hud { position: absolute; z-index: 5; display: flex; flex-direction: column; align-items: center; }

/* Panels are centred in the safe part of the screen, not the whole of it.
   --fit is set from script when a panel is still taller than that. */
.hud { --hud-mid: calc(50% + (var(--safe-t) - var(--safe-b)) / 2); }
.hud-left { left: max(clamp(6px, 1.6vw, 26px), var(--safe-l)); top: var(--hud-mid); transform: translateY(-50%) scale(var(--fit, 1)); transform-origin: left center; gap: 0; }
.hud-right { right: max(clamp(6px, 1.6vw, 26px), var(--safe-r)); top: var(--hud-mid); transform: translateY(-50%) scale(var(--fit, 1)); transform-origin: right center; }

.panel-body {
  background: linear-gradient(180deg, var(--panel) 0%, var(--panel-dark) 100%);
  border-radius: 999px;
  padding: clamp(7px, 1vw, 14px) clamp(5px, 0.7vw, 10px);
  box-shadow: 0 10px 26px rgba(20, 8, 40, 0.45), inset 0 2px 0 rgba(255, 255, 255, 0.22);
  border: 2px solid rgba(255, 255, 255, 0.18);
  display: flex; flex-direction: column; align-items: center;
  gap: var(--gap);
}

.avatar {
  width: var(--avatar); height: var(--avatar);
  border-radius: 50%;
  background: radial-gradient(circle at 40% 30%, #7fb6e8, #3f6fbe);
  border: clamp(3px, 0.5vw, 7px) solid var(--panel);
  box-shadow: 0 6px 16px rgba(20, 8, 40, 0.45), inset 0 0 0 2px rgba(255, 255, 255, 0.3);
  overflow: hidden; display: grid; place-items: center;
  margin-bottom: -8px; z-index: 2;
}
.avatar img { width: 112%; height: 112%; object-fit: cover; }

.goal-list {
  background: linear-gradient(180deg, var(--cream) 0%, var(--cream-deep) 100%);
  border-radius: clamp(14px, 1.6vw, 24px);
  padding: clamp(6px, 0.9vw, 14px) clamp(5px, 0.6vw, 9px);
  display: grid; grid-template-columns: auto; justify-items: center; gap: var(--gap);
  box-shadow: inset 0 -3px 0 rgba(150, 90, 60, 0.18);
}
/* Four or five goals folded into two columns. The script picks one column or
   two, whichever leaves the board the larger cells on this screen. */
.goal-list.two { grid-template-columns: auto auto; }
.goal-list.two .goal:last-child:nth-child(odd) { grid-column: 1 / -1; }

.goal {
  position: relative; display: grid; place-items: center;
  width: var(--goal); height: var(--goal);
}
.goal img { width: 84%; height: 84%; object-fit: contain; filter: drop-shadow(0 2px 3px rgba(90, 42, 30, 0.35)); }
.goal .count {
  position: absolute; right: -2px; bottom: -4px;
  font-size: calc(var(--goal) * 0.33); font-weight: 800; color: #fff;
  -webkit-text-stroke: calc(var(--goal) * 0.06) var(--ink); paint-order: stroke fill;
  font-variant-numeric: tabular-nums;
}
.goal.done .count { display: none; }
.goal .tick {
  position: absolute; right: -6px; bottom: -6px; width: 58%; height: 58%; display: none;
}
.goal.done .tick { display: block; }
.goal.bump { animation: bump 0.32s ease-out; }
@keyframes bump { 0% { transform: scale(1); } 40% { transform: scale(1.22); } 100% { transform: scale(1); } }

.moves-chip {
  display: flex; align-items: center; justify-content: center; gap: 4px;
  min-width: calc(var(--goal) * 1.5);
  font-size: calc(var(--goal) * 0.6); font-weight: 800; color: #fff;
  -webkit-text-stroke: calc(var(--goal) * 0.075) var(--panel-dark); paint-order: stroke fill;
  font-variant-numeric: tabular-nums;
  filter: drop-shadow(0 3px 4px rgba(20, 8, 40, 0.5));
}
.moves-chip.low { color: #ffd0d0; animation: pulse 0.9s infinite; }
@keyframes pulse { 50% { transform: scale(1.1); } }
.moves-chip svg { width: calc(var(--goal) * 0.4); height: calc(var(--goal) * 0.4); }

.luck-chip {
  margin-top: var(--gap); padding: 3px 10px; border-radius: 999px;
  background: rgba(24, 12, 40, 0.62); border: 1.5px solid rgba(255, 255, 255, 0.28);
  font-size: clamp(10px, 1vw, 14px); font-weight: 700; letter-spacing: 0.4px;
  display: flex; align-items: center; gap: 6px; white-space: nowrap;
}
/* On a short screen the chip is as narrow as the panel above it, so it does
   not push the board away: the dot and the word are enough. */
@media (max-height: 520px) { .luck-chip { padding: 2px 8px; gap: 5px; } .luck-word { display: none; } }
.luck-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 8px var(--accent); }

/* -------------------------------------------------------------------- rail */
.tool {
  position: relative; border: 0; padding: 0; cursor: pointer;
  width: var(--tool); height: var(--tool);
  border-radius: 50%; background: radial-gradient(circle at 38% 28%, #8fd9fb, #3aa2dd);
  box-shadow: 0 5px 0 #1f78ab, 0 9px 18px rgba(20, 8, 40, 0.4), inset 0 0 0 3px rgba(255, 255, 255, 0.55);
  display: grid; place-items: center; transition: transform 0.09s, box-shadow 0.09s;
}
.tool:active { transform: translateY(3px); box-shadow: 0 2px 0 #1f78ab, 0 5px 10px rgba(20, 8, 40, 0.4), inset 0 0 0 3px rgba(255, 255, 255, 0.55); }
.tool svg { width: 62%; height: 62%; filter: drop-shadow(0 2px 2px rgba(10, 40, 70, 0.4)); }
.tool.armed { background: radial-gradient(circle at 38% 28%, #ffe89a, #f2a93c); box-shadow: 0 5px 0 #b8741b, 0 0 22px #ffd35e, inset 0 0 0 3px rgba(255, 255, 255, 0.7); }
.tool[disabled] { filter: grayscale(0.55) brightness(0.8); cursor: default; }
.tool .badge {
  position: absolute; right: -3px; bottom: -3px; min-width: 42%; height: 42%;
  border-radius: 999px; background: #e2352f; border: 2px solid #fff;
  display: grid; place-items: center; padding: 0 3px;
  font-size: calc(var(--tool) * 0.22); font-weight: 800; color: #fff;
}
.tool .badge.plus { background: #46b93f; }
.gear {
  margin-top: var(--gap); width: calc(var(--tool) * 0.8); height: calc(var(--tool) * 0.8);
  border-radius: 50%; border: 0; cursor: pointer;
  background: radial-gradient(circle at 38% 28%, #b6ef8c, #4ba83c);
  box-shadow: 0 4px 0 #2c7524, inset 0 0 0 3px rgba(255, 255, 255, 0.5);
  display: grid; place-items: center;
}
.gear svg { width: 60%; height: 60%; }

/* ---------------------------------------------------------------- toasts */
#toast {
  position: absolute; left: 50%; bottom: max(3%, var(--safe-b)); transform: translateX(-50%);
  max-width: 62vw;
  z-index: 20; display: flex; flex-direction: column-reverse; gap: 6px; align-items: center;
  pointer-events: none;
}
.toast {
  background: rgba(28, 14, 46, 0.7); border: 1.5px solid rgba(255, 255, 255, 0.2);
  border-radius: 999px; padding: clamp(4px, 1.4vh, 7px) 18px; font-weight: 700; text-align: center;
  font-size: clamp(11px, 1.05vw, 15px); backdrop-filter: blur(4px);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%;
  animation: toastIn 0.22s ease-out, toastOut 0.4s ease-in 2.1s forwards;
}
@keyframes toastIn { from { opacity: 0; transform: translateY(10px); } }
@keyframes toastOut { to { opacity: 0; transform: translateY(10px); } }

/* --------------------------------------------------------------- dialogs */
.scrim {
  position: absolute; inset: 0; z-index: 30;
  background: rgba(14, 6, 24, 0.72);
  display: grid; place-items: center;
  padding: max(2.5vh, var(--safe-t)) max(3vw, var(--safe-r)) max(2.5vh, var(--safe-b)) max(3vw, var(--safe-l));
  animation: fade 0.2s ease-out;
}
@keyframes fade { from { opacity: 0; } }

.card {
  background: linear-gradient(180deg, var(--cream) 0%, var(--cream-deep) 100%);
  border: clamp(4px, 0.5vw, 8px) solid var(--panel);
  border-radius: clamp(18px, 2vw, 30px);
  box-shadow: 0 20px 50px rgba(10, 4, 20, 0.6);
  color: var(--ink);
  padding: clamp(10px, min(1.8vw, 3.4vh), 28px);
  max-width: min(760px, 100%); max-height: 100%; min-height: 0;
  display: flex; flex-direction: column;
  position: relative; text-align: center;
  animation: cardIn 0.28s cubic-bezier(0.2, 1.3, 0.5, 1);
}
@keyframes cardIn { from { transform: scale(0.82) translateY(18px); opacity: 0; } }

.card-body { overflow: auto; min-height: 0; flex: 1 1 auto; overscroll-behavior: contain; }
.card-actions { flex: 0 0 auto; display: flex; flex-wrap: wrap; justify-content: center; padding-top: clamp(2px, 1vh, 8px); }
.card h2 {
  margin: -2px auto clamp(4px, 1.6vh, 10px); padding: clamp(3px, 1vh, 6px) 26px; width: fit-content; flex: 0 0 auto;
  background: var(--accent); border-radius: 999px;
  color: #7a4a12; font-size: clamp(17px, min(2.2vw, 5.4vh), 32px);
  box-shadow: inset 0 -3px 0 rgba(150, 100, 20, 0.28);
}
.card p { margin: clamp(3px, 1vh, 6px) 0; font-size: clamp(12px, 1.2vw, 17px); line-height: 1.45; }
.card .tip { background: #f3d9c0; border-radius: 12px; padding: 6px 12px; text-align: left; font-weight: 600; }
.card .tip img { width: 1.9em; height: 1.9em; object-fit: contain; vertical-align: middle; margin-right: 6px; }
/* The pre-level card on a short screen: goals on the left, boosters on the right. */
.pre-cols { display: grid; gap: 0 clamp(12px, 2vw, 28px); }
@media (max-height: 520px) and (min-width: 520px) {
  .pre-cols { grid-template-columns: 1fr 1fr; align-items: center; }
}
.card .close {
  position: absolute; right: 10px; top: 8px; border: 0; background: none;
  font-size: clamp(22px, 2vw, 30px); color: #d8443c; cursor: pointer; font-weight: 800;
  min-width: 40px; min-height: 40px; z-index: 2; font-family: var(--font);
}

.btn {
  border: 0; cursor: pointer; font-family: var(--font); font-weight: 800;
  color: #fff; border-radius: 999px;
  padding: clamp(7px, min(0.9vw, 2.2vh), 13px) clamp(18px, 2.4vw, 40px);
  font-size: clamp(15px, min(1.7vw, 4.6vh), 25px);
  background: linear-gradient(180deg, #6cd15a, #3f9c33);
  box-shadow: 0 5px 0 #2c7524, 0 8px 18px rgba(20, 8, 40, 0.35);
  -webkit-text-stroke: 0; transition: transform 0.08s, box-shadow 0.08s;
  margin: clamp(3px, 1vh, 6px) 6px;
}
.btn.small { font-size: clamp(12px, 1.25vw, 18px); padding: clamp(6px, 0.7vw, 10px) clamp(12px, 1.5vw, 24px); margin: 0; box-shadow: 0 3px 0 #5f5070; }
.btn.small.go { box-shadow: 0 3px 0 #2c7524; }
.btn:focus-visible, .tool:focus-visible, .gear:focus-visible, .node:focus-visible, .booster:focus-visible .disc, .switch:focus-visible, .card .close:focus-visible {
  outline: 3px solid #fff; outline-offset: 2px;
}
.btn:active { transform: translateY(3px); box-shadow: 0 2px 0 #2c7524; }
.btn.alt { background: linear-gradient(180deg, #ffb64d, #e4842a); box-shadow: 0 5px 0 #a85c14; }
.btn.ghost { background: linear-gradient(180deg, #b9a9c9, #8d7a9e); box-shadow: 0 5px 0 #5f5070; }
.btn[disabled] { filter: grayscale(0.6) brightness(0.85); cursor: default; }

.booster-row { display: flex; justify-content: center; gap: clamp(8px, 1.4vw, 22px); margin: clamp(4px, 1.6vh, 10px) 0 4px; flex-wrap: wrap; }
.booster {
  border: 0; cursor: pointer; background: none; padding: 0;
  display: flex; flex-direction: column; align-items: center; gap: 5px;
}
.booster .disc {
  width: clamp(44px, min(max(6vw, 52px), 14vh), 92px); height: clamp(44px, min(max(6vw, 52px), 14vh), 92px); border-radius: 50%;
  background: radial-gradient(circle at 38% 28%, #cdf0ff, #8ecbee);
  box-shadow: 0 5px 0 #5c9dc0, inset 0 0 0 4px #fff;
  display: grid; place-items: center; transition: transform 0.1s;
}
.booster .disc img { width: 76%; height: 76%; object-fit: contain; }
.booster.on .disc { background: radial-gradient(circle at 38% 28%, #ffef9d, #f5b53c); box-shadow: 0 5px 0 #b8741b, 0 0 20px #ffd35e, inset 0 0 0 4px #fff; transform: translateY(-3px); }
.booster .stock {
  background: #46b93f; color: #fff; border-radius: 999px; padding: 2px 12px;
  font-size: clamp(11px, 1.1vw, 16px); font-weight: 800; min-width: 46px;
}
.booster[disabled] { filter: grayscale(0.6) opacity(0.7); cursor: default; }

.goal-preview { display: flex; justify-content: center; gap: clamp(8px, 1vw, 16px); flex-wrap: wrap; margin: clamp(3px, 1.4vh, 8px) 0 2px; }
.goal-preview .gp { display: flex; flex-direction: column; align-items: center; font-weight: 800; font-size: clamp(12px, 1.2vw, 18px); }
.goal-preview .gp img { width: clamp(30px, 3.2vw, 48px); height: clamp(30px, 3.2vw, 48px); object-fit: contain; }
.goal-preview .gp svg { width: clamp(34px, min(4vw, 11vh), 60px); height: clamp(34px, min(4vw, 11vh), 60px); }

.progress { background: #e7c7ad; border-radius: 999px; height: clamp(14px, 1.5vw, 22px); overflow: hidden; box-shadow: inset 0 2px 4px rgba(120, 70, 40, 0.3); }
.progress > i { display: block; height: 100%; background: linear-gradient(180deg, #7ee06a, #46b93f); transition: width 0.4s; }

.catgift { display: flex; align-items: center; gap: 10px; background: #f3d9c0; border-radius: 999px; padding: clamp(3px, 1vh, 6px) 12px; margin: 4px 0 clamp(4px, 1.6vh, 10px); }
.catgift img { width: clamp(34px, 3.4vw, 52px); height: clamp(34px, 3.4vw, 52px); }
.catgift .progress { flex: 1; }
.catgift b { font-size: clamp(11px, 1.1vw, 16px); }

/* ------------------------------------------------------------------- map */
.map-wrap { position: absolute; inset: 0; z-index: 10; overflow: auto; background: #1b1226; }
.map-bg { position: fixed; inset: 0; background-size: cover; background-position: center; filter: brightness(0.55) saturate(0.85); }
.map-inner { position: relative; min-height: 100%; padding: calc(clamp(52px, 8vh, 110px) + var(--safe-t)) max(4vw, var(--safe-r)) calc(clamp(24px, 5vh, 60px) + var(--safe-b)) max(4vw, var(--safe-l)); }
.map-title { text-align: center; font-size: clamp(22px, 3vw, 44px); font-weight: 800; text-shadow: 0 4px 12px rgba(0, 0, 0, 0.6); margin-bottom: 4px; }
.map-sub { text-align: center; opacity: 0.85; font-size: clamp(11px, 1.2vw, 16px); margin-bottom: 18px; }
.map-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(clamp(140px, 15vw, 210px), 1fr)); gap: clamp(10px, 1.4vw, 22px); max-width: 1400px; margin: 0 auto; }

.node {
  position: relative; border: 0; cursor: pointer; text-align: left;
  border-radius: clamp(12px, 1.4vw, 20px); overflow: hidden;
  background: #2c2138; color: #fff; font-family: var(--font);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.4);
  transition: transform 0.12s;
}
.node:hover { transform: translateY(-3px); }
.node .thumb { height: clamp(76px, 8vw, 116px); background-size: cover; background-position: center; }
.node .meta { padding: 8px 10px 10px; }
.node .no { font-size: clamp(15px, 1.6vw, 22px); font-weight: 800; }
.node .nm { font-size: clamp(10px, 1.05vw, 14px); opacity: 0.8; }
.node .stars { position: absolute; right: 8px; top: 8px; display: flex; gap: 2px; }
.node .stars svg { width: clamp(14px, 1.5vw, 20px); height: clamp(14px, 1.5vw, 20px); }
.node.locked { filter: grayscale(0.85) brightness(0.55); cursor: default; }
.node.locked:hover { transform: none; }
.node .lock { position: absolute; inset: 0; display: grid; place-items: center; font-size: clamp(22px, 2.4vw, 34px); }
.node.current { outline: 3px solid var(--accent); outline-offset: -3px; }
.node.fresh::after {
  content: 'New'; position: absolute; left: 8px; top: 8px; padding: 1px 8px; border-radius: 999px;
  background: var(--accent); color: #7a4a12; font-weight: 800; font-size: clamp(10px, 1vw, 13px);
}

.topbar {
  position: sticky; left: 0; right: 0; top: 0; z-index: 12; display: flex; gap: clamp(6px, 1vw, 10px);
  align-items: center;
  padding: calc(clamp(6px, 1vw, 16px) + var(--safe-t)) max(clamp(10px, 1.6vw, 26px), var(--safe-r)) clamp(10px, 1.4vw, 20px) max(clamp(10px, 1.6vw, 26px), var(--safe-l));
  background: linear-gradient(180deg, rgba(14, 6, 24, 0.88) 55%, rgba(14, 6, 24, 0));
  margin-bottom: calc(-1 * (clamp(52px, 8vh, 110px) + var(--safe-t)));
}
.stat {
  display: flex; align-items: center; gap: 6px;
  background: rgba(24, 12, 40, 0.7); border: 1.5px solid rgba(255, 255, 255, 0.24);
  border-radius: 999px; padding: 5px clamp(9px, 1.2vw, 14px); font-weight: 800;
  font-size: clamp(11px, 1.2vw, 17px); font-variant-numeric: tabular-nums; white-space: nowrap;
}
.stat svg { width: clamp(14px, 1.5vw, 21px); height: clamp(14px, 1.5vw, 21px); }
.spacer { flex: 1; }

.rows { display: flex; flex-direction: column; gap: 10px; text-align: left; margin: 10px 0; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 14px; background: #f3d9c0; border-radius: 14px; padding: 8px 14px; }
.row label { font-weight: 800; font-size: clamp(12px, 1.2vw, 17px); }
.row .hintline { font-size: clamp(10px, 1vw, 13px); opacity: 0.72; font-weight: 600; }
.switch { width: 56px; height: 30px; border-radius: 999px; border: 0; cursor: pointer; background: #b9a9c9; position: relative; transition: background 0.15s; flex: 0 0 auto; }
.switch::after { content: ''; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; transition: left 0.15s; }
.switch.on { background: #46b93f; }
.switch.on::after { left: 29px; }
.slider { flex: 1; max-width: 220px; }

.loading { position: absolute; inset: 0; display: grid; place-items: center; z-index: 40; background: #171021; }
.loading .inner { text-align: center; }
.loading .bar { width: min(340px, 70vw); height: 16px; border-radius: 999px; background: #2f2440; overflow: hidden; margin-top: 14px; }
.loading .bar > i { display: block; height: 100%; width: 0; background: linear-gradient(90deg, #ffd75e, #ff9b4a); transition: width 0.2s; }

.hidden { display: none !important; }

/* The board is up to 13 cells wide, so the game is played sideways. */
.rotate-note { display: none; }
@media (orientation: portrait) and (max-width: 700px) {
  .rotate-note {
    position: absolute; inset: 0; z-index: 60; display: grid; place-items: center;
    background: #171021; text-align: center; padding: 8vw; font-size: 5vw; font-weight: 800;
  }
}

@media (prefers-reduced-motion: reduce) {
  .card, .scrim, .toast, .goal.bump, .moves-chip.low { animation: none; }
}
