:root {
  --slate: #1e2530;
  --stone: #5e6b7a;
  --sage: #8fa88a;
  --lilac: #d7cfe6;
  --ink: #f4efe6;
  --ink-dim: rgba(244, 239, 230, 0.68);
  --accent: #e07a5f;
  --panel: rgba(30, 37, 48, 0.62);
  --panel-line: rgba(215, 207, 230, 0.22);
  --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);
  --serif: "Iowan Old Style", "Palatino Linotype", "Book Antiqua", Palatino, "Noto Serif", Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  width: 100%; height: 100%;
  overflow: hidden;
  background: var(--slate);
  font-family: var(--sans);
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

#scene {
  position: fixed; inset: 0;
  width: 100%; height: 100%;
  display: block;
  touch-action: none;
  cursor: grab;
}
#scene:active { cursor: grabbing; }

#vig {
  position: fixed; inset: 0;
  pointer-events: none;
  background: radial-gradient(120% 95% at 50% 45%, transparent 55%, rgba(14, 18, 26, 0.55) 100%);
  z-index: 2;
}
#grain {
  position: fixed; inset: -50%;
  pointer-events: none;
  z-index: 2;
  opacity: 0.06;
  background-image: none;   /* filled by main.js with a canvas-made noise tile */
  background-size: 160px 160px;
  animation: grain 1.2s steps(6) infinite;
}
@keyframes grain { 0% { transform: translate(0, 0); } 100% { transform: translate(-6%, 4%); } }
@media (prefers-reduced-motion: reduce) { #grain { animation: none; } }

button {
  font-family: inherit;
  user-select: none;
  -webkit-user-select: none;
  border: none;
  cursor: pointer;
  color: inherit;
}
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }

/* ---------- HUD ---------- */

#hud { position: fixed; inset: 0; z-index: 5; pointer-events: none; }
#hud button { pointer-events: auto; }
#hud[hidden] { display: none; }
#hud #topbar { transition: opacity 0.5s ease; }
#hud.over #topbar { opacity: 0; pointer-events: none; }
#hud.over #topbar button { pointer-events: none; }

#topbar {
  position: absolute;
  top: calc(var(--safe-t) + 12px);
  left: calc(var(--safe-l) + 16px);
  right: calc(var(--safe-r) + 12px);
  display: flex; align-items: flex-start; justify-content: space-between;
  gap: 10px;
}

.height { display: flex; flex-direction: column; gap: 2px; text-shadow: 0 1px 12px rgba(20, 26, 34, 0.7); }
.height-row { display: flex; align-items: baseline; gap: 6px; }
.height-num {
  font-family: var(--serif);
  font-weight: 500;
  font-variation-settings: "opsz" 144;
  font-size: 44px;
  line-height: 1;
  letter-spacing: -0.01em;
  font-variant-numeric: tabular-nums;
}
.height-unit { font-size: 14px; font-weight: 600; color: var(--ink-dim); letter-spacing: 0.06em; }
.height-best { font-size: 12px; font-weight: 600; color: var(--ink-dim); letter-spacing: 0.04em; min-height: 14px; }
.lives { display: flex; gap: 6px; margin-top: 6px; }
.lives i {
  width: 16px; height: 9px;
  border-radius: 50% 50% 45% 45% / 60% 60% 40% 40%;
  background: linear-gradient(160deg, #93a0ad, var(--stone) 55%, #2c3540);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
  transition: opacity 0.4s ease, transform 0.4s ease;
}
.lives i.lost { opacity: 0.22; transform: translateY(3px) scale(0.85); }
.streak { font-size: 12px; font-weight: 700; color: var(--accent); letter-spacing: 0.06em; margin-top: 6px; min-height: 14px; text-transform: uppercase; }

.right { display: flex; align-items: center; gap: 8px; }

.chip {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--panel);
  border: 1px solid var(--panel-line);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-radius: 999px;
  padding: 9px 12px;
  font-size: 12.5px; font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--ink);
  height: 40px;
  transition: border-color 0.3s ease, color 0.3s ease, background 0.3s ease;
}
.chip.gust svg { color: var(--sage); transition: transform 0.3s ease; transform-origin: 50% 100%; }
.chip.gust.soon { border-color: rgba(143, 168, 138, 0.5); }
.chip.gust.warn { border-color: rgba(224, 122, 95, 0.7); color: #f6c7b8; animation: pulse 0.9s ease-in-out infinite; }
.chip.gust.warn svg { color: var(--accent); animation: bend 0.9s ease-in-out infinite alternate; }
.chip.gust.blow { background: rgba(224, 122, 95, 0.28); border-color: var(--accent); }
.chip.gust.blow svg { color: var(--ink); transform: skewX(-22deg); }
@keyframes pulse { 0%, 100% { box-shadow: 0 0 0 0 rgba(224, 122, 95, 0); } 50% { box-shadow: 0 0 0 5px rgba(224, 122, 95, 0.18); } }
@keyframes bend { from { transform: skewX(-6deg); } to { transform: skewX(16deg); } }

.icon-btn {
  width: 44px; height: 44px;
  border-radius: 50%;
  background: var(--panel);
  border: 1px solid var(--panel-line);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  color: var(--ink);
  display: flex; align-items: center; justify-content: center;
  transition: transform 0.12s ease, border-color 0.12s ease;
}
.icon-btn:active { transform: scale(0.92); }
.icon-btn .slash, .ghost .slash { display: none; }
.icon-btn.off .slash, .ghost.off .slash { display: block; }
.icon-btn.off .wave, .ghost.off .wave { display: none; }

#toast {
  position: absolute;
  top: calc(var(--safe-t) + 70px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--panel);
  border: 1px solid var(--panel-line);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-radius: 14px;
  padding: 9px 16px;
  font-size: 14px; font-weight: 600;
  letter-spacing: 0.01em;
  text-align: center;
  max-width: min(84vw, 380px);
  animation: toast-in 0.28s cubic-bezier(0.2, 0.9, 0.3, 1.2);
  z-index: 6;
}
#toast[hidden] { display: none; }
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, -8px); } to { opacity: 1; transform: translate(-50%, 0); } }

#hint {
  position: absolute;
  bottom: calc(var(--safe-b) + 22px);
  left: 50%;
  transform: translateX(-50%);
  font-size: 13px; font-weight: 600;
  color: var(--ink);
  background: rgba(30, 37, 48, 0.55);
  border: 1px solid rgba(215, 207, 230, 0.14);
  border-radius: 999px;
  padding: 8px 15px;
  white-space: nowrap;
  max-width: 92vw;
  overflow: hidden; text-overflow: ellipsis;
  transition: opacity 0.4s ease, transform 0.4s ease;
}
#hint[hidden] { display: none; }
#hint.out { opacity: 0; transform: translate(-50%, 8px); }

#kudos {
  position: absolute;
  top: 30%;
  left: 50%;
  transform: translateX(-50%);
  font-family: var(--serif);
  font-weight: 500;
  font-variation-settings: "opsz" 144;
  font-size: 40px;
  letter-spacing: 0.01em;
  color: var(--accent);
  text-shadow: 0 2px 22px rgba(224, 122, 95, 0.45), 0 1px 2px rgba(20, 26, 34, 0.6);
  animation: kudos-pop 1.25s cubic-bezier(0.18, 1.4, 0.4, 1) forwards;
  white-space: nowrap;
  pointer-events: none;
}
#kudos.held { color: var(--sage); text-shadow: 0 2px 22px rgba(143, 168, 138, 0.5), 0 1px 2px rgba(20, 26, 34, 0.6); }
#kudos.best { color: var(--lilac); text-shadow: 0 2px 22px rgba(215, 207, 230, 0.55), 0 1px 2px rgba(20, 26, 34, 0.6); }
#kudos[hidden] { display: none; }
@keyframes kudos-pop {
  0% { opacity: 0; transform: translateX(-50%) translateY(10px) scale(0.8); }
  20% { opacity: 1; transform: translateX(-50%) translateY(0) scale(1.06); }
  32% { transform: translateX(-50%) scale(1); }
  78% { opacity: 1; }
  100% { opacity: 0; transform: translateX(-50%) translateY(-18px) scale(0.98); }
}

/* ---------- overlays ---------- */

.overlay {
  position: fixed; inset: 0;
  z-index: 10;
  display: flex; align-items: center; justify-content: center;
  padding: calc(var(--safe-t) + 20px) calc(var(--safe-r) + 20px) calc(var(--safe-b) + 20px) calc(var(--safe-l) + 20px);
  background: rgba(20, 26, 34, 0.42);
  animation: fade-in 0.35s ease;
}
.overlay[hidden] { display: none; }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

.overlay.menu {
  background: linear-gradient(180deg, rgba(30, 37, 48, 0.35) 0%, rgba(30, 37, 48, 0) 28%, rgba(30, 37, 48, 0) 55%, rgba(20, 26, 34, 0.72) 100%);
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
}
.menu-top { text-align: center; margin-top: max(4vh, 18px); animation: rise-in 0.8s cubic-bezier(0.2, 0.8, 0.2, 1) both; }
.title {
  font-family: var(--serif);
  font-weight: 500;
  font-variation-settings: "opsz" 144;
  font-size: clamp(56px, 12vw, 84px);
  line-height: 1;
  letter-spacing: -0.01em;
  color: var(--ink);
  text-shadow: 0 2px 30px rgba(20, 26, 34, 0.6);
}
.title.small { font-size: 36px; }
.tagline {
  margin-top: 10px;
  font-family: var(--serif);
  font-style: italic;
  font-weight: 400;
  font-size: 17px;
  color: var(--lilac);
  text-shadow: 0 1px 12px rgba(20, 26, 34, 0.7);
}
.menu-actions {
  width: min(92vw, 360px);
  display: flex; flex-direction: column; align-items: center;
  margin-bottom: max(3vh, 12px);
  animation: rise-in 0.8s 0.1s cubic-bezier(0.2, 0.8, 0.2, 1) both;
}
@keyframes rise-in { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: none; } }

.primary {
  width: 100%;
  padding: 17px;
  border-radius: 18px;
  background: linear-gradient(160deg, #f4efe6, #d7cfe6 70%, #bfb3d4);
  color: #1e2530;
  font-size: 19px; font-weight: 700;
  letter-spacing: 0.02em;
  box-shadow: 0 12px 30px rgba(20, 26, 34, 0.45), inset 0 1px 0 rgba(255, 255, 255, 0.7);
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}
.primary:hover { box-shadow: 0 14px 34px rgba(20, 26, 34, 0.5), inset 0 1px 0 rgba(255, 255, 255, 0.7); }
.primary:active { transform: scale(0.97); }

.best {
  margin-top: 14px;
  font-family: var(--serif);
  font-size: 16px;
  color: var(--ink-dim);
  letter-spacing: 0.02em;
}

.ghost {
  margin-top: 10px;
  width: 100%;
  padding: 13px;
  border-radius: 16px;
  background: rgba(244, 239, 230, 0.08);
  border: 1px solid rgba(215, 207, 230, 0.18);
  color: var(--ink);
  font-size: 15px; font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  transition: transform 0.12s ease, background 0.12s ease;
}
.ghost:active { transform: scale(0.97); }
.ghost.small { width: auto; padding: 9px 16px; border-radius: 999px; font-size: 13px; }

.hints {
  margin-top: 16px;
  display: flex; flex-direction: column; gap: 4px;
  font-size: 12.5px; color: var(--ink-dim); font-weight: 500;
  text-align: center;
}

.card {
  width: min(92vw, 360px);
  background: rgba(30, 37, 48, 0.72);
  border: 1px solid var(--panel-line);
  border-radius: 26px;
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  padding: 28px 26px 22px;
  display: flex; flex-direction: column; align-items: center;
  text-align: center;
  box-shadow: 0 24px 70px rgba(10, 14, 20, 0.55);
  animation: card-in 0.35s cubic-bezier(0.2, 0.9, 0.3, 1.1);
}
@keyframes card-in { from { opacity: 0; transform: translateY(10px) scale(0.98); } to { opacity: 1; transform: none; } }
.card .primary { margin-top: 18px; }
.sub { margin-top: 6px; font-size: 14.5px; color: var(--ink-dim); font-weight: 500; }

.over-label { font-family: var(--serif); font-style: italic; font-size: 20px; color: var(--lilac); }
.over-height { display: flex; align-items: baseline; gap: 8px; margin-top: 6px; }
.over-height .big {
  font-family: var(--serif);
  font-weight: 500;
  font-variation-settings: "opsz" 144;
  font-size: 72px;
  line-height: 1;
  color: var(--ink);
}
.over-height .unit { font-size: 16px; font-weight: 600; color: var(--ink-dim); letter-spacing: 0.08em; }

/* ---------- loader ---------- */

#loader {
  position: fixed; inset: 0;
  z-index: 20;
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(180deg, #1e2530 0%, #2c3542 60%, #3b4457 100%);
  transition: opacity 0.5s ease;
}
#loader.done { opacity: 0; pointer-events: none; }
.loader-inner { display: flex; flex-direction: column; align-items: center; gap: 12px; }
.loader-stone { filter: drop-shadow(0 8px 16px rgba(0, 0, 0, 0.4)); }
.loader-water { transform-origin: 50% 100%; animation: fill-up 0.9s cubic-bezier(0.3, 0.7, 0.3, 1) forwards; }
@keyframes fill-up { from { transform: translateY(70px); } to { transform: translateY(0); } }
.loader-title {
  font-family: var(--serif);
  font-weight: 500;
  font-size: 34px;
  color: var(--ink);
}
.loader-sub { font-size: 13px; color: var(--ink-dim); font-weight: 600; }

@media (max-width: 480px) {
  .height-num { font-size: 40px; }
  .chip { padding: 9px 10px; font-size: 12px; }
}
@media (max-height: 520px) {
  .menu-top { margin-top: 6px; }
  .title { font-size: 44px; }
  .menu-actions { margin-bottom: 8px; }
  .hints { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  .chip.gust.warn, .chip.gust.warn svg, #kudos, #toast, .menu-top, .menu-actions, .card { animation: none; }
}
