/* The Red Cantor's Journey — a pixel world on a canvas, with soft modern
   panels on top: wine-dark glass, warm wood and brass. */

:root {
  --bg: #1a0d10;
  --ink: #fff4e2;
  --ink-dim: rgba(255, 244, 226, 0.7);
  --wine: #a3202f;
  --wine-2: #6b1220;
  --gold: #f2c75a;
  --gold-2: #c8922a;
  --glass: rgba(30, 14, 18, 0.78);
  --glass-2: rgba(58, 26, 30, 0.9);
  --line: rgba(255, 220, 170, 0.16);
  --radius: 18px;
  --ease: cubic-bezier(0.3, 0.7, 0.2, 1);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --sans: ui-rounded, "SF Pro Rounded", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
}

#game {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* ------------------------------------------------------------------ hud */
.hud {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 5;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
  padding: calc(8px + var(--safe-t)) calc(10px + var(--safe-r)) 0 calc(10px + var(--safe-l));
  pointer-events: none;
}
.hud-l, .hud-r { display: flex; align-items: center; gap: 8px; }
.hud .pill, .hearts, .golds {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 34px;
  padding: 0 10px;
  border-radius: 999px;
  background: rgba(20, 8, 12, 0.55);
  border: 1px solid var(--line);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
}
.hud img, .hearts img, .golds img { image-rendering: pixelated; width: 20px; height: 20px; }
.hearts img.off, .golds img.off { opacity: 0.22; filter: grayscale(1); }
.hearts img.pop, .golds img.pop { animation: pop 0.45s var(--ease); }
.pill b { font-size: 16px; font-variant-numeric: tabular-nums; letter-spacing: 0.02em; }
.pill.score b { min-width: 4ch; text-align: right; color: var(--gold); }
.icon-btn {
  pointer-events: auto;
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: rgba(20, 8, 12, 0.55);
  color: var(--ink);
  cursor: pointer;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}
.icon-btn:active { transform: scale(0.94); }
@keyframes pop { 0% { transform: scale(1); } 40% { transform: scale(1.5); } 100% { transform: scale(1); } }

/* ---------------------------------------------------------------- touch */
.touch {
  position: fixed;
  inset: 0;
  z-index: 3;
  touch-action: none;
}
.tpad {
  position: absolute;
  left: calc(18px + var(--safe-l));
  bottom: calc(18px + var(--safe-b));
  display: flex;
  gap: 14px;
  pointer-events: none;
}
.tbtn {
  display: grid;
  place-items: center;
  width: 74px;
  height: 74px;
  border-radius: 50%;
  background: radial-gradient(circle at 50% 35%, rgba(255, 255, 255, 0.22), rgba(255, 255, 255, 0.08));
  border: 2px solid rgba(255, 255, 255, 0.28);
  color: rgba(255, 255, 255, 0.85);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.25), inset 0 -4px 0 rgba(0, 0, 0, 0.15);
  transition: transform 0.08s, background 0.08s;
  pointer-events: none;
}
.tbtn svg { width: 34px; height: 34px; stroke-width: 3; }
.tbtn.on { transform: scale(0.92); background: radial-gradient(circle at 50% 35%, rgba(255, 230, 160, 0.55), rgba(242, 199, 90, 0.25)); border-color: rgba(255, 230, 160, 0.7); }
.tjump {
  position: absolute;
  right: calc(22px + var(--safe-r));
  bottom: calc(22px + var(--safe-b));
  width: 92px;
  height: 92px;
}
.tjump svg { width: 40px; height: 40px; }
@media (orientation: portrait) {
  .tbtn { width: 80px; height: 80px; }
  .tjump { width: 96px; height: 96px; }
}

/* --------------------------------------------------------------- toasts */
.toast {
  position: fixed;
  left: 50%;
  top: calc(56px + var(--safe-t));
  z-index: 8;
  transform: translate(-50%, -12px);
  opacity: 0;
  padding: 9px 16px;
  border-radius: 999px;
  background: var(--glass-2);
  border: 1px solid var(--line);
  font-weight: 700;
  font-size: 15px;
  white-space: nowrap;
  pointer-events: none;
  transition: opacity 0.25s, transform 0.35s var(--ease);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* -------------------------------------------------------------- screens */
.screen {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: calc(16px + var(--safe-t)) calc(16px + var(--safe-r)) calc(16px + var(--safe-b)) calc(16px + var(--safe-l));
  background: radial-gradient(120% 90% at 50% 40%, rgba(20, 6, 10, 0.25), rgba(14, 4, 8, 0.72));
  overflow-y: auto;
  animation: fade 0.3s var(--ease);
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.screen.title { background: linear-gradient(180deg, rgba(14, 4, 8, 0.1), rgba(14, 4, 8, 0.15) 40%, rgba(14, 4, 8, 0.7)); gap: 10px; }
#dlg-confirm { z-index: 20; }

.title-art { display: flex; align-items: center; justify-content: center; gap: clamp(8px, 2vw, 22px); }
.logo {
  width: min(calc(96vw - 120px), 560px);
  image-rendering: pixelated;
  filter: drop-shadow(0 6px 0 rgba(40, 6, 12, 0.55)) drop-shadow(0 14px 22px rgba(0, 0, 0, 0.45));
  animation: float 4s ease-in-out infinite;
}
.hero {
  width: 80px;
  height: 112px;
  image-rendering: pixelated;
  filter: drop-shadow(0 8px 10px rgba(0, 0, 0, 0.45));
}
@keyframes float { 50% { transform: translateY(-5px); } }
.tagline {
  margin: 0 0 8px;
  max-width: 34em;
  text-align: center;
  font-size: clamp(15px, 2.2vw, 18px);
  font-weight: 650;
  color: var(--ink);
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.7);
}
.foot { margin: 8px 0 0; font-size: 12px; color: var(--ink-dim); letter-spacing: 0.08em; text-shadow: 0 1px 4px #000; }

.menu { display: flex; flex-direction: column; align-items: stretch; gap: 10px; width: min(92vw, 360px); }
.row { display: flex; gap: 10px; justify-content: center; }
.row > .btn { flex: 1; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 48px;
  padding: 10px 18px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: linear-gradient(180deg, rgba(74, 34, 38, 0.92), rgba(44, 18, 22, 0.92));
  color: var(--ink);
  font: 700 16px/1.1 var(--sans);
  cursor: pointer;
  box-shadow: 0 4px 0 rgba(10, 2, 4, 0.55), 0 10px 22px rgba(0, 0, 0, 0.3), inset 0 1px 0 rgba(255, 255, 255, 0.08);
  transition: transform 0.08s, box-shadow 0.08s, filter 0.15s;
}
.btn:hover { filter: brightness(1.1); }
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(10, 2, 4, 0.55), 0 6px 14px rgba(0, 0, 0, 0.3); }
.btn:focus-visible, .icon-btn:focus-visible, .chip:focus-visible, .switch:focus-visible, .level:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }
.btn.primary {
  background: linear-gradient(180deg, #d6383f, #9c1b28);
  border-color: rgba(255, 190, 160, 0.4);
  box-shadow: 0 4px 0 #5a0d16, 0 12px 26px rgba(120, 10, 20, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.25);
}
.btn.primary:active { box-shadow: 0 1px 0 #5a0d16, 0 6px 14px rgba(120, 10, 20, 0.35); }
.btn.big { min-height: 58px; font-size: 20px; }
.btn.danger { background: linear-gradient(180deg, #6a2a2a, #3e1414); color: #ffd4c8; }

.panel {
  width: min(94vw, 420px);
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
  padding: 18px;
  border-radius: var(--radius);
  background: var(--glass);
  border: 1px solid var(--line);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5), inset 0 1px 0 rgba(255, 255, 255, 0.06);
  animation: rise 0.4s var(--ease);
}
.panel.wide { width: min(94vw, 720px); }
@keyframes rise { from { transform: translateY(14px) scale(0.98); opacity: 0; } to { transform: none; opacity: 1; } }
.panel-h { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.panel h2 { margin: 0; font-size: 22px; letter-spacing: 0.01em; }
.panel .menu { width: 100%; }
.kicker { margin: 0 0 4px; font-size: 13px; font-weight: 800; letter-spacing: 0.14em; text-transform: uppercase; color: var(--gold); }

.intro { text-align: center; width: min(94vw, 460px); }
.intro h2 { font-size: 28px; margin-bottom: 10px; }
.story { margin: 0 0 12px; font-size: 16px; line-height: 1.45; color: var(--ink); }
.goal { margin: 0 0 16px; padding: 10px 12px; border-radius: 12px; background: rgba(242, 199, 90, 0.1); border: 1px solid rgba(242, 199, 90, 0.25); font-size: 14px; font-weight: 650; color: #ffe6a8; }
.intro .btn { width: 100%; }

/* levels */
.levels { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 10px; }
.level {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 100%;
  min-height: 118px;
  padding: 12px 12px 10px;
  border-radius: 14px;
  border: 1px solid var(--line);
  color: var(--ink);
  text-align: left;
  font: inherit;
  cursor: pointer;
  overflow: hidden;
  box-shadow: 0 4px 0 rgba(10, 2, 4, 0.5);
  transition: transform 0.12s var(--ease), filter 0.15s;
}
.level:hover { transform: translateY(-2px); filter: brightness(1.08); }
.level:active { transform: translateY(2px); }
.level[disabled] { cursor: default; filter: grayscale(0.85) brightness(0.6); transform: none; }
.level .n { font-size: 12px; font-weight: 800; letter-spacing: 0.12em; text-transform: uppercase; color: rgba(255, 244, 226, 0.75); }
.level .name { font-size: 17px; font-weight: 800; line-height: 1.15; text-shadow: 0 2px 6px rgba(0, 0, 0, 0.5); }
.level .best { margin-top: auto; font-size: 13px; color: var(--ink-dim); }
.level .lvstars { display: flex; gap: 3px; }
.level .lvstars img { width: 18px; height: 18px; image-rendering: pixelated; }
.level .lvstars img.off { opacity: 0.25; filter: grayscale(1); }
.level .art { position: absolute; right: 8px; bottom: 6px; width: 48px; height: 48px; image-rendering: pixelated; opacity: 0.95; }
.level.t-vineyard { background: linear-gradient(160deg, #3f8fcf 0%, #6fb24a 62%, #3f6b26 100%); }
.level.t-cellar { background: linear-gradient(160deg, #5a2c22 0%, #2a1410 100%); }
.level.t-village { background: linear-gradient(160deg, #4a2e6b 0%, #c45a6a 55%, #f0a060 100%); }
.level.t-forest { background: linear-gradient(160deg, #0b1030 0%, #1f3a5a 60%, #20402f 100%); }
.level.t-greatcellar { background: linear-gradient(160deg, #6a1a24 0%, #2a0c10 100%); }

/* tally */
.tally { text-align: center; }
.tally h2 { font-size: 26px; margin-bottom: 8px; }
.stars { display: flex; justify-content: center; gap: 10px; margin: 6px 0 12px; }
.stars img { width: 40px; height: 40px; image-rendering: pixelated; opacity: 0.18; filter: grayscale(1); transform: scale(0.6); transition: all 0.45s var(--ease); }
.stars img.on { opacity: 1; filter: drop-shadow(0 0 10px rgba(255, 220, 120, 0.7)); transform: scale(1); }
.tally dl { display: grid; grid-template-columns: 1fr auto; gap: 6px 14px; margin: 0 0 12px; text-align: left; font-size: 15px; }
.tally dt { color: var(--ink-dim); }
.tally dd { margin: 0; font-weight: 800; font-variant-numeric: tabular-nums; text-align: right; }
.tally dl .total { padding-top: 8px; border-top: 1px solid var(--line); font-size: 19px; color: var(--ink); }
.tally dl dd.total { color: var(--gold); }
.tally dl .row-in { animation: fade 0.3s var(--ease) both; }
.record { margin: 0 0 12px; color: var(--gold); font-weight: 900; letter-spacing: 0.06em; text-transform: uppercase; animation: pop 0.6s var(--ease); }
.end .hero { margin: 0 auto 6px; display: block; }

/* help */
.help { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.help li { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 12px; background: rgba(255, 255, 255, 0.04); border: 1px solid var(--line); font-size: 15px; line-height: 1.4; }
.help canvas { width: 40px; height: 40px; image-rendering: pixelated; flex: none; }

/* settings */
.settings { display: flex; flex-direction: column; gap: 12px; }
.set-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; font-weight: 650; }
.set-row.col { flex-direction: column; align-items: stretch; }
.switch { position: relative; width: 52px; height: 30px; border-radius: 999px; border: 1px solid var(--line); background: rgba(255, 255, 255, 0.1); cursor: pointer; transition: background 0.2s; }
.switch i { position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; transition: transform 0.2s var(--ease); }
.switch[aria-checked="true"] { background: #b52a35; }
.switch[aria-checked="true"] i { transform: translateX(22px); }
.seg { display: flex; gap: 6px; }
.seg button { flex: 1; min-height: 40px; border-radius: 10px; border: 1px solid var(--line); background: rgba(255, 255, 255, 0.06); color: var(--ink); font: 700 14px var(--sans); cursor: pointer; }
.seg button[aria-pressed="true"] { background: #b52a35; border-color: rgba(255, 190, 160, 0.4); }
.toggles { margin-top: 4px; }
.chip { flex: 1; min-height: 38px; border-radius: 999px; border: 1px solid var(--line); background: rgba(255, 255, 255, 0.06); color: var(--ink-dim); font: 700 13px var(--sans); cursor: pointer; }
.chip[aria-pressed="true"] { color: var(--ink); background: rgba(181, 42, 53, 0.55); }
#confirm-text { margin: 0 0 16px; font-size: 16px; line-height: 1.4; text-align: center; }

@media (max-height: 460px) {
  .screen.title { gap: 4px; }
  .logo { width: min(56vw, 440px); }
  .hero { width: 60px; height: 84px; }
  .btn.big { min-height: 50px; font-size: 18px; }
  .menu { gap: 8px; }
  .btn { min-height: 42px; }
  .panel { padding: 14px; }
  .intro h2 { font-size: 22px; }
  .story { font-size: 14px; margin-bottom: 8px; }
  .goal { margin-bottom: 10px; }
  .foot { display: none; }
}
@media (max-width: 480px) {
  .hero { width: 60px; height: 84px; }
  .logo { width: calc(96vw - 96px); }
}
@media (max-width: 380px) {
  .hud .pill, .hearts, .golds { padding: 0 7px; gap: 2px; height: 30px; }
  .hud img, .hearts img, .golds img { width: 16px; height: 16px; }
  .pill b { font-size: 14px; }
  .icon-btn { width: 34px; height: 34px; }
}
@media (prefers-reduced-motion: reduce) {
  .logo { animation: none; }
  .screen, .panel { animation: none; }
}
