:root {
  --room: #1c1410;
  --walnut: #3b2314;
  --grain: #a6692e;
  --brass: #c9973a;
  --brass-hi: #f0cf86;
  --brass-lo: #8a6423;
  --parch: #f1e4c8;
  --parch-dim: #b9a888;
  --amber: #ff9f43;
  --panel: rgba(28, 18, 12, 0.74);
  --line: rgba(201, 151, 58, 0.32);
  --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: "Cormorant Garamond", Georgia, "Times New Roman", serif;
  --sans: "Inter", system-ui, -apple-system, sans-serif;
}

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

html, body {
  width: 100%; height: 100%;
  overflow: hidden;
  background: var(--room);
  font-family: var(--sans);
  color: var(--parch);
  -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.dragging { cursor: grabbing; }

#vig {
  position: fixed; inset: 0;
  pointer-events: none;
  background: radial-gradient(120% 100% at 45% 40%, transparent 48%, rgba(10, 6, 3, 0.62) 100%);
  z-index: 2;
}
#grain {
  position: fixed; inset: -50%;
  width: 200%; height: 200%;
  pointer-events: none;
  z-index: 2;
  opacity: 0.05;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='160' height='160'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0.9 0 0 0 0 0.8 0 0 0 0 0.6 0 0 0 0.9 0'/></filter><rect width='160' height='160' filter='url(%23n)'/></svg>");
  animation: grain 1.2s steps(4) infinite;
}
@keyframes grain { 0% { transform: translate(0, 0); } 25% { transform: translate(-2%, 1%); } 50% { transform: translate(1%, -2%); } 75% { transform: translate(-1%, -1%); } 100% { transform: translate(0, 0); } }

#fade {
  position: fixed; inset: 0;
  pointer-events: none;
  z-index: 3;
  background: radial-gradient(60% 60% at var(--fx, 50%) var(--fy, 50%), #060302 0%, #0d0805 60%, #1c1410 100%);
  opacity: 0;
}
#flash {
  position: fixed; inset: 0;
  pointer-events: none;
  z-index: 3;
  opacity: 0;
  box-shadow: inset 0 0 140px rgba(0, 0, 0, 0.9);
  transition: opacity 0.7s ease;
}
#flash.on { opacity: 1; transition: opacity 0.05s ease; }

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

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

#hud { position: fixed; inset: 0; z-index: 5; pointer-events: none; }
#hud[hidden] { display: none; }
#hud button { pointer-events: auto; }

#topbar {
  position: absolute;
  top: calc(var(--safe-t) + 12px);
  left: calc(var(--safe-l) + 14px);
  right: calc(var(--safe-r) + 14px);
  display: flex; align-items: flex-start; justify-content: space-between;
  gap: 10px;
  animation: rise 0.6s cubic-bezier(0.22, 1, 0.36, 1) both;
}
@keyframes rise { from { opacity: 0; transform: translateY(-10px); } to { opacity: 1; transform: none; } }

.marbles {
  display: flex; gap: 7px; align-items: center;
  height: 40px; padding: 0 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 999px;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
.marble {
  width: 14px; height: 14px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #ffffff 0%, #d8d2c6 28%, #7d7466 62%, #2c2620 100%);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.6), inset 0 -1px 2px rgba(0, 0, 0, 0.4);
  transition: opacity 0.4s ease, transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.marble.lost { opacity: 0.18; transform: scale(0.7); }

.glass {
  flex: 1 1 auto;
  max-width: 300px;
  display: flex; flex-direction: column; align-items: center; gap: 5px;
  padding: 8px 14px 7px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
.sand {
  width: 100%; height: 7px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.45);
  border: 1px solid rgba(201, 151, 58, 0.25);
  overflow: hidden;
}
#sandFill {
  height: 100%; width: 100%;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--brass-lo), var(--brass) 50%, var(--brass-hi));
  box-shadow: 0 0 8px rgba(201, 151, 58, 0.45);
  transform-origin: left center;
  transition: background 0.3s ease;
}
#sandFill.low { background: linear-gradient(90deg, #b8541e, var(--amber) 60%, #ffd08a); animation: pulse 0.5s ease-in-out infinite alternate; }
@keyframes pulse { from { filter: brightness(0.85); } to { filter: brightness(1.3); } }
.depth { display: flex; align-items: baseline; gap: 8px; line-height: 1; }
.depth-label { font-size: 10px; letter-spacing: 0.22em; text-transform: uppercase; color: var(--parch-dim); font-weight: 600; }
.depth-num { font-family: var(--serif); font-weight: 700; font-size: 24px; color: var(--parch); letter-spacing: 0.02em; font-variant-numeric: lining-nums tabular-nums; font-feature-settings: "lnum" 1, "tnum" 1; }

.chips { display: flex; gap: 8px; }
.chip {
  width: 40px; height: 40px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--brass);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  transition: transform 0.12s ease, border-color 0.12s ease;
}
.chip:active { transform: scale(0.92); }
.chip.off { color: var(--parch-dim); }

#toast {
  position: absolute;
  top: calc(var(--safe-t) + 78px);
  left: 50%;
  transform: translateX(-50%);
  font-family: var(--serif);
  font-size: 22px; font-weight: 600;
  color: var(--brass-hi);
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.7);
  letter-spacing: 0.04em;
  white-space: nowrap;
  animation: toast-in 0.35s cubic-bezier(0.22, 1, 0.36, 1) both;
}
#toast[hidden] { display: none; }
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, -8px); } to { opacity: 1; transform: translate(-50%, 0); } }

#kudos {
  position: absolute;
  top: 30%;
  left: 50%;
  transform: translateX(-50%);
  font-family: var(--serif);
  font-size: 44px; font-weight: 700;
  letter-spacing: 0.08em;
  background: linear-gradient(170deg, #fff1cf, var(--brass) 55%, var(--brass-lo));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 0 16px rgba(201, 151, 58, 0.55));
  animation: kudos-pop 1.3s cubic-bezier(0.18, 1.5, 0.4, 1) forwards;
  white-space: nowrap;
  pointer-events: none;
}
#kudos[hidden] { display: none; }
@keyframes kudos-pop {
  0% { opacity: 0; transform: translateX(-50%) scale(0.6); }
  18% { opacity: 1; transform: translateX(-50%) scale(1.1); }
  30% { transform: translateX(-50%) scale(1); }
  78% { opacity: 1; }
  100% { opacity: 0; transform: translateX(-50%) translateY(-18px) scale(0.97); }
}

#hint {
  position: absolute;
  bottom: calc(var(--safe-b) + 26px);
  left: 50%;
  transform: translateX(-50%);
  font-size: 13px; font-weight: 500;
  letter-spacing: 0.06em;
  color: var(--parch-dim);
  background: rgba(20, 12, 8, 0.6);
  border: 1px solid rgba(201, 151, 58, 0.18);
  border-radius: 999px;
  padding: 8px 16px;
  white-space: nowrap;
  transition: opacity 0.6s ease;
}
#hint[hidden] { display: none; }
#hint.fade { opacity: 0; }

/* ---------- 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: radial-gradient(80% 70% at 50% 40%, rgba(28, 20, 16, 0.18), rgba(10, 6, 3, 0.78));
  animation: overlay-in 0.35s ease both;
}
.overlay[hidden] { display: none; }
@keyframes overlay-in { from { opacity: 0; } to { opacity: 1; } }

.card {
  width: min(90vw, 360px);
  background: linear-gradient(180deg, rgba(48, 30, 18, 0.9), rgba(28, 18, 12, 0.92));
  border: 1px solid var(--line);
  border-radius: 22px;
  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 30px 80px rgba(0, 0, 0, 0.65), inset 0 1px 0 rgba(241, 228, 200, 0.08);
  position: relative;
  animation: card-in 0.55s cubic-bezier(0.22, 1, 0.36, 1) both;
}
@keyframes card-in { from { opacity: 0; transform: translateY(18px) scale(0.98); } to { opacity: 1; transform: none; } }
.card::before {
  content: "";
  position: absolute; left: 26px; right: 26px; top: 0;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--brass), transparent);
}

.mark { margin-bottom: 4px; filter: drop-shadow(0 6px 14px rgba(0, 0, 0, 0.5)); }
.title {
  font-family: var(--serif);
  font-weight: 700;
  font-size: 64px;
  line-height: 1;
  letter-spacing: 0.14em;
  text-indent: 0.14em;
  color: var(--parch);
  text-shadow: 0 1px 0 rgba(255, 240, 210, 0.35), 0 -1px 0 rgba(0, 0, 0, 0.7), 0 6px 22px rgba(0, 0, 0, 0.5);
  background: linear-gradient(180deg, #fff3d6 0%, var(--brass-hi) 45%, var(--brass) 70%, var(--brass-lo) 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}
.title.small { font-size: 40px; letter-spacing: 0.1em; }
.sub {
  margin-top: 10px;
  font-size: 14px; line-height: 1.55;
  color: var(--parch-dim);
  font-weight: 500;
}

.primary {
  margin-top: 22px;
  width: 100%;
  padding: 16px;
  border-radius: 14px;
  background: linear-gradient(170deg, #f5dba3, var(--brass) 55%, #a87a2a);
  color: #1c1208;
  font-family: var(--serif);
  font-size: 24px; font-weight: 700;
  letter-spacing: 0.18em;
  text-indent: 0.18em;
  box-shadow: 0 12px 30px rgba(201, 151, 58, 0.28), inset 0 1px 0 rgba(255, 255, 255, 0.35);
  transition: transform 0.12s ease, filter 0.12s ease;
}
.primary:hover { filter: brightness(1.06); }
.primary:active { transform: scale(0.97); }

.ghost {
  margin-top: 10px;
  width: 100%;
  padding: 13px;
  border-radius: 12px;
  background: rgba(241, 228, 200, 0.06);
  border: 1px solid rgba(241, 228, 200, 0.1);
  color: var(--parch);
  font-size: 14px; font-weight: 600;
  letter-spacing: 0.08em;
  transition: transform 0.12s ease, background 0.12s ease;
}
.ghost:hover { background: rgba(241, 228, 200, 0.1); }
.ghost:active { transform: scale(0.97); }

.best {
  margin-top: 14px;
  font-size: 11px; letter-spacing: 0.22em; text-transform: uppercase;
  color: var(--brass);
  font-weight: 600;
}
.hints {
  margin-top: 16px;
  display: flex; flex-direction: column; gap: 5px;
  font-size: 12px; color: var(--parch-dim); font-weight: 500;
  letter-spacing: 0.02em;
}

.over-label { font-size: 11px; letter-spacing: 0.24em; text-transform: uppercase; color: var(--parch-dim); font-weight: 600; }
.over-depth { display: flex; flex-direction: column; align-items: center; gap: 2px; margin-top: 12px; }
.over-depth .depth-label { font-size: 11px; }
.big {
  font-family: var(--serif);
  font-weight: 700;
  font-variant-numeric: lining-nums;
  font-feature-settings: "lnum" 1;
  font-size: 96px; line-height: 0.95;
  background: linear-gradient(180deg, #fff3d6 0%, var(--brass-hi) 45%, var(--brass) 70%, var(--brass-lo) 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  filter: drop-shadow(0 6px 18px rgba(0, 0, 0, 0.5));
}

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

#loader {
  position: fixed; inset: 0;
  z-index: 20;
  display: flex; align-items: center; justify-content: center;
  background:
    radial-gradient(90% 70% at 50% 30%, rgba(90, 52, 24, 0.55) 0%, rgba(59, 35, 20, 0.35) 45%, transparent 100%),
    repeating-linear-gradient(92deg, #2a170c 0px, #3b2314 6px, #2b1a0e 11px, #34200f 19px, #24150b 27px),
    var(--room);
  transition: opacity 0.5s ease;
}
#loader.done { opacity: 0; pointer-events: none; }
.loader-inner { display: flex; flex-direction: column; align-items: center; gap: 14px; }
.ring {
  width: 72px; height: 72px;
  border-radius: 50%;
  background: conic-gradient(var(--brass) calc(var(--p, 0) * 1%), rgba(201, 151, 58, 0.15) 0);
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 0 24px rgba(201, 151, 58, 0.25);
  transition: --p 0.3s ease;
}
.ring-core {
  width: 56px; height: 56px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #ffffff 0%, #d8d2c6 26%, #7d7466 60%, #2c2620 100%);
  box-shadow: 0 6px 14px rgba(0, 0, 0, 0.6);
  animation: bob 1.6s ease-in-out infinite;
}
@keyframes bob { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-4px); } }
.loader-title {
  font-family: var(--serif);
  font-size: 40px; font-weight: 700; letter-spacing: 0.16em; text-indent: 0.16em;
  background: linear-gradient(180deg, #fff3d6, var(--brass) 70%, var(--brass-lo));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}
.loader-sub { font-size: 12px; letter-spacing: 0.14em; text-transform: uppercase; color: var(--parch-dim); font-weight: 600; }

@media (max-width: 480px) {
  .title { font-size: 54px; }
  .big { font-size: 84px; }
  .glass { max-width: 190px; padding: 7px 12px 6px; }
  .depth-num { font-size: 20px; }
  .marbles { padding: 0 10px; height: 38px; }
  .chip { width: 38px; height: 38px; }
}

@media (prefers-reduced-motion: reduce) {
  #grain { animation: none; }
  .ring-core { animation: none; }
}
