:root {
  --bg0: #0e1116;
  --bg1: #171c24;
  --card: #1c232e;
  --line: #2c3544;
  --text: #e8edf5;
  --mute: #8b95a7;
  --accent: #ff5a36;
  --ok: #3dd68c;
  --warn: #f5c542;
  --radius: 18px;
  --font: "Space Grotesk", system-ui, sans-serif;
  --display: "Russo One", "RussoOneGameloft", Impact, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: var(--font);
  color: var(--text);
  background:
    radial-gradient(1200px 600px at 10% -10%, #2a1a14 0%, transparent 55%),
    radial-gradient(900px 500px at 100% 0%, #14243a 0%, transparent 50%),
    var(--bg0);
  min-height: 100vh;
  padding: 20px 16px 48px;
}
.noise {
  pointer-events: none;
  position: fixed;
  inset: 0;
  opacity: 0.04;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  z-index: 0;
}
.top, main { position: relative; z-index: 1; max-width: 420px; margin: 0 auto; }
.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 22px;
}
.brand {
  font-family: var(--display);
  font-weight: 800;
  font-size: 1.35rem;
  letter-spacing: -0.03em;
}
.brand span { color: var(--accent); }
.brand-link { text-decoration: none; color: inherit; }
.seg-back {
  position: relative;
  z-index: 1;
  max-width: 420px;
  margin: -6px auto 14px;
  font-size: 0.85rem;
}
.seg-back a {
  color: var(--mute);
  text-decoration: none;
  font-weight: 600;
}
.seg-back a:hover { color: var(--text); }
.modes {
  display: flex;
  gap: 6px;
  background: var(--bg1);
  padding: 4px;
  border-radius: 999px;
  border: 1px solid var(--line);
}
.mode-btn {
  border: 0;
  background: transparent;
  color: var(--mute);
  font: inherit;
  font-weight: 600;
  padding: 8px 14px;
  border-radius: 999px;
  cursor: pointer;
}
.mode-btn.is-active {
  background: var(--accent);
  color: #fff;
}


.calc-wrap {
  background: linear-gradient(180deg, #222a36, var(--card));
  border: 1px solid var(--line);
  border-radius: calc(var(--radius) + 6px);
  padding: 16px;
  box-shadow: 0 24px 60px rgba(0,0,0,.35);
}
.calc-wrap[hidden] { display: none !important; }
.hint {
  margin: 0 0 12px;
  color: var(--warn);
  font-size: 0.85rem;
  font-weight: 500;
}

.progress-bar {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px 10px;
  align-items: center;
  margin-bottom: 12px;
  padding: 10px 12px;
  border-radius: 14px;
  background: #141a24;
  border: 1px solid var(--line);
}
.prog-stats {
  display: flex;
  gap: 14px;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--mute);
}
.prog-stats b { color: var(--text); font-size: 1.05rem; }
.prog-pts b { color: var(--warn); }
.prog-combo {
  color: #ff8fd0;
  animation: pulse .5s ease;
}
.prog-combo b { color: #ffb8e0; }
.confetti-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 100;
}
.lucky-cat {
  position: fixed;
  z-index: 90;
  width: 64px;
  height: 64px;
  border: 0;
  background: transparent;
  cursor: pointer;
  padding: 0;
  filter: drop-shadow(0 8px 16px rgba(0,0,0,.45));
  animation: cat-pop .35s ease, float-bob 1.2s ease-in-out infinite .35s;
}
.lucky-cat svg { width: 100%; height: 100%; color: #ffb347; }
.lucky-cat.caught {
  animation: cat-caught .4s ease forwards;
  pointer-events: none;
}
.lucky-cat.gone-cat {
  animation: cat-flee .35s ease forwards;
  pointer-events: none;
}
@keyframes cat-pop {
  from { transform: scale(0) rotate(-20deg); opacity: 0; }
  to { transform: scale(1) rotate(0); opacity: 1; }
}
@keyframes cat-caught {
  to { transform: scale(1.6) translateY(-40px); opacity: 0; }
}
@keyframes cat-flee {
  to { transform: translate(80px, -60px) scale(0.4) rotate(40deg); opacity: 0; }
}
.btn-levelup {
  border: 0;
  border-radius: 10px;
  background: linear-gradient(180deg, #ff7a55, var(--accent));
  color: #fff;
  font: inherit;
  font-weight: 700;
  font-size: 0.82rem;
  padding: 9px 12px;
  cursor: pointer;
  white-space: nowrap;
}
.btn-levelup:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  filter: grayscale(0.4);
}
.btn-levelup:not(:disabled):hover { filter: brightness(1.08); }
.prog-desc {
  grid-column: 1 / -1;
  margin: 0;
  font-size: 0.75rem;
  color: var(--mute);
  line-height: 1.35;
}
#chaos.lvl-high .progress-bar {
  border-color: #3a6a50;
  background: linear-gradient(180deg, #15241c, #141a24);
}
#chaos.lvl-max .btn-levelup { background: var(--ok); color: #062416; }

/* fortune wheel */
.fortune {
  margin-bottom: 12px;
  border-radius: 14px;
  border: 1px solid #4a3a20;
  background: #18140e;
  overflow: hidden;
}
.fortune > summary {
  list-style: none;
  cursor: pointer;
  padding: 10px 12px;
  font-weight: 700;
  font-size: 0.88rem;
  color: var(--warn);
  user-select: none;
}
.fortune > summary::-webkit-details-marker { display: none; }
.fortune > summary::after {
  content: "▾";
  float: right;
  opacity: 0.6;
}
.fortune[open] > summary::after { content: "▴"; }
.fortune-body {
  display: grid;
  grid-template-columns: 120px 1fr;
  gap: 12px;
  padding: 0 12px 14px;
  align-items: center;
}
.fortune-wheel-wrap {
  position: relative;
  width: 112px;
  height: 112px;
  margin: 0 auto;
}
.fortune-pointer {
  position: absolute;
  top: -4px;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 7px solid transparent;
  border-right: 7px solid transparent;
  border-top: 12px solid var(--warn);
  z-index: 2;
  filter: drop-shadow(0 2px 2px rgba(0,0,0,.4));
}
.fortune-wheel {
  width: 112px;
  height: 112px;
  border-radius: 50%;
  border: 3px solid #f5c542;
  background: conic-gradient(
    #3dd68c 0 45deg,
    #5a2230 45deg 90deg,
    #f5c542 90deg 135deg,
    #5a2230 135deg 180deg,
    #3dd68c 180deg 225deg,
    #5a2230 225deg 270deg,
    #f5c542 270deg 315deg,
    #5a2230 315deg 360deg
  );
  box-shadow: inset 0 0 0 6px #18140e, 0 8px 20px rgba(0,0,0,.35);
  transition: transform 0.1s linear;
  position: relative;
}
.fortune-wheel span {
  position: absolute;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  padding-top: 8px;
  font-size: 0.62rem;
  font-weight: 800;
  color: #0c1014;
  text-shadow: 0 0 2px rgba(255,255,255,.35);
  pointer-events: none;
  transform-origin: 50% 50%;
}
.fortune-wheel span:nth-child(1) { transform: rotate(22deg); }
.fortune-wheel span:nth-child(2) { transform: rotate(67deg); }
.fortune-wheel span:nth-child(3) { transform: rotate(112deg); }
.fortune-wheel span:nth-child(4) { transform: rotate(157deg); }
.fortune-wheel span:nth-child(5) { transform: rotate(202deg); }
.fortune-wheel span:nth-child(6) { transform: rotate(247deg); }
.fortune-wheel span:nth-child(7) { transform: rotate(292deg); }
.fortune-wheel span:nth-child(8) { transform: rotate(337deg); }
.fortune-wheel.is-spinning {
  transition: transform 3.2s cubic-bezier(0.12, 0.75, 0.08, 1);
}
.fortune-controls { min-width: 0; }
.fortune-tip {
  margin: 0 0 8px;
  font-size: 0.72rem;
  color: var(--mute);
  line-height: 1.35;
}
.fortune-tip b { color: #ff8f78; }
.fortune-bets {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
}
.fortune-bets button {
  border: 1px solid var(--line);
  background: #2a2318;
  color: var(--text);
  font: inherit;
  font-size: 0.78rem;
  font-weight: 700;
  padding: 6px 10px;
  border-radius: 8px;
  cursor: pointer;
}
.fortune-bets button.is-on {
  background: var(--warn);
  color: #1a1408;
  border-color: var(--warn);
}
.fortune-stake {
  margin: 0 0 8px;
  font-size: 0.82rem;
  color: var(--mute);
}
.fortune-stake b { color: var(--warn); }
.btn-fortune {
  width: 100%;
  border: 0;
  border-radius: 10px;
  background: linear-gradient(180deg, #f5d76e, #e0a800);
  color: #1a1408;
  font: inherit;
  font-weight: 800;
  padding: 10px 12px;
  cursor: pointer;
}
.btn-fortune:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
.btn-fortune:not(:disabled):hover { filter: brightness(1.06); }
.fortune-status {
  margin: 8px 0 0;
  font-size: 0.75rem;
  color: var(--mute);
  min-height: 1.2em;
}
.fortune-status.win { color: var(--ok); font-weight: 700; }
.fortune-status.lose { color: #ff8f78; font-weight: 700; }
@media (max-width: 480px) {
  .fortune-body { grid-template-columns: 1fr; }
}
.chaos-out-wrap { min-width: 0; }
#chaos.sane-pad .chaos-pad {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  height: auto;
  min-height: 280px;
  overflow: visible;
  padding: 6px;
}
#chaos.sane-pad .chaos-pad button {
  position: relative !important;
  left: auto !important;
  top: auto !important;
  transform: none !important;
  width: 100%;
}
#chaos.sane-pad .float-svg { display: none; }
#chaos.sane-pad .chaos-pad button.will-flee,
#chaos.sane-pad .chaos-pad button.will-shrink {
  outline: none;
}
#chaos.sane-pad .chaos-pad button.liar { background: #2a3342; }
#chaos.sane-pad .chaos-pad button.trap { background: #3a4456; }
.display {
  background: #0b0e13;
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 14px;
  border: 1px solid #10151d;
  min-height: 88px;
}
.chaos-display {
  display: grid;
  grid-template-columns: 110px 1fr;
  gap: 12px;
  align-items: center;
}
.wheel-box {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.wheel-box label {
  font-size: 0.7rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--mute);
}
#wheel {
  width: 100%;
  font: inherit;
  font-size: 2rem;
  font-weight: 700;
  text-align: center;
  background: #151b24;
  color: var(--ok);
  border: 1px dashed var(--line);
  border-radius: 12px;
  padding: 8px;
  /* discourage typing */
  caret-color: transparent;
  user-select: none;
}
.wheel-tip { font-size: 0.72rem; color: var(--mute); text-align: center; }
.out {
  display: block;
  font-family: var(--display);
  font-size: clamp(1.6rem, 6vw, 2.2rem);
  font-weight: 700;
  text-align: right;
  word-break: break-all;
  letter-spacing: -0.02em;
}
.expr {
  color: var(--mute);
  font-size: 0.9rem;
  text-align: right;
  min-height: 1.2em;
  margin-bottom: 4px;
}

.pad {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  position: relative;
  min-height: 280px;
}
.pad button {
  border: 0;
  border-radius: 14px;
  background: #2a3342;
  color: var(--text);
  font: inherit;
  font-size: 1.25rem;
  font-weight: 600;
  min-height: 58px;
  cursor: pointer;
  transition: transform .12s ease, opacity .2s ease, background .15s;
}
.pad button:active { transform: scale(0.96); }
.pad button.op { background: #3a4456; color: #c9d3e3; }
.pad button.accent { background: var(--accent); color: #fff; }
.pad button.eq { background: var(--ok); color: #062416; }
.pad button.span2 { grid-column: span 2; }
.pad button.gone {
  opacity: 0;
  pointer-events: none;
  transform: scale(0.5);
}

/* chaos: absolute scattered buttons */
.chaos-pad {
  display: block;
  height: 380px;
  overflow: hidden;
  border-radius: 14px;
  background: repeating-linear-gradient(
    -12deg,
    transparent,
    transparent 10px,
    rgba(255,90,54,0.04) 10px,
    rgba(255,90,54,0.04) 11px
  );
  border: 1px dashed var(--line);
}
.chaos-pad button {
  position: absolute;
  min-width: 56px;
  min-height: 52px;
  padding: 0 12px;
  box-shadow: 0 8px 20px rgba(0,0,0,.25);
  z-index: 2;
}
.chaos-pad button.has-svg {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  font-size: 0.95rem;
  padding: 6px 10px;
}
.chaos-pad button.has-svg svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}
.chaos-pad button.liar { background: #5a3a2a; }
.chaos-pad button.trap { background: #3a2a55; }
.chaos-pad button.will-flee { outline: 1px dashed rgba(255,90,54,.35); }
.chaos-pad button.fleeing {
  transition: left .08s linear, top .08s linear, transform .08s linear;
  background: #7a2f22 !important;
  z-index: 5;
}
.chaos-pad button.tiny {
  transform: scale(0.45) !important;
  opacity: 0.55;
  z-index: 1;
}
.chaos-pad button.mirror { transform: scaleX(-1) rotate(12deg) !important; }
.chaos-pad button.is-wrong { background: #5a2230 !important; }
.chaos-pad.shake { animation: pad-shake .42s ease; }
.float-svg {
  position: absolute;
  width: 28px;
  height: 28px;
  color: var(--accent);
  pointer-events: none;
  z-index: 0;
  animation: float-bob 5s ease-in-out infinite;
}
.float-svg svg { width: 100%; height: 100%; }
.out.glitch {
  animation: glitch .5s steps(2) both;
  color: var(--accent);
}
.hint.pulse { color: #fff; animation: pulse .4s ease; }
body.tilt main { transform: rotate(-1.8deg); transition: transform .35s ease; }
body.hell-mode {
  filter: hue-rotate(90deg) saturate(1.4);
}

/* chaos fake windows */
.calc-wrap { position: relative; }
.chaos-popups {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 50;
}
.chaos-popups[hidden] { display: none !important; }
.chaos-win {
  pointer-events: auto;
  position: absolute;
  width: min(280px, 86vw);
  background: #1a2030;
  border: 2px solid #5a6a88;
  border-radius: 10px;
  box-shadow: 8px 12px 0 rgba(0,0,0,.35), 0 20px 50px rgba(0,0,0,.45);
  font-size: 0.9rem;
  animation: win-in .22s ease;
  overflow: hidden;
}
.chaos-win.skew { transform: rotate(-2.5deg); }
.chaos-win.tiny-win { width: min(200px, 70vw); font-size: 0.8rem; }
.chaos-win.closing { opacity: 0; transform: scale(0.85); transition: .18s ease; }
.chaos-win-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  background: linear-gradient(180deg, #3a4a66, #2a3548);
  cursor: grab;
  user-select: none;
  border-bottom: 1px solid #4a5a78;
}
.chaos-win-icon { display: flex; width: 16px; height: 16px; color: var(--warn); }
.chaos-win-icon svg { width: 100%; height: 100%; }
.chaos-win-title {
  flex: 1;
  font-family: var(--display);
  font-weight: 700;
  font-size: 0.85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chaos-win-x {
  border: 0;
  background: #c44;
  color: #fff;
  width: 22px;
  height: 22px;
  border-radius: 4px;
  font-weight: 700;
  cursor: pointer;
  line-height: 1;
}
.chaos-win-body {
  padding: 14px 12px 8px;
  color: var(--text);
  line-height: 1.35;
}
.chaos-win-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  padding: 8px 12px 12px;
}
.chaos-win-btn {
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font: inherit;
  font-weight: 600;
  padding: 8px 14px;
  cursor: pointer;
  transition: transform .12s ease;
}
.chaos-win-btn.alt { background: #3a4456; }
.chaos-win-btn:hover { filter: brightness(1.08); }

/* math gate — blocks chaos calc */
.chaos-gate {
  position: absolute;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(8, 10, 14, 0.78);
  backdrop-filter: blur(4px);
  border-radius: inherit;
}
.chaos-gate[hidden] { display: none !important; }
.chaos-gate-card {
  width: min(320px, 100%);
  background: linear-gradient(180deg, #2a2030, #1a1520);
  border: 2px solid #ff5a36;
  border-radius: 16px;
  padding: 16px 14px 14px;
  box-shadow: 0 18px 40px rgba(0,0,0,.5);
  text-align: center;
  animation: win-in .2s ease;
}
.chaos-gate-eyebrow {
  margin: 0 0 6px;
  font-size: 0.7rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: #ff8f78;
  font-weight: 700;
}
.chaos-gate-title {
  margin: 0 0 12px;
  font-size: 0.92rem;
  color: var(--text);
  line-height: 1.35;
  font-weight: 500;
}
.chaos-gate-expr {
  margin: 0 0 14px;
  font-family: var(--display);
  font-size: clamp(1.8rem, 7vw, 2.4rem);
  font-weight: 800;
  letter-spacing: -0.03em;
  color: var(--warn);
}
.chaos-gate-answers {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}
.chaos-gate-answers button {
  border: 0;
  border-radius: 12px;
  background: #3a2a40;
  color: var(--text);
  font: inherit;
  font-size: 1.2rem;
  font-weight: 700;
  padding: 12px 8px;
  cursor: pointer;
  transition: transform .1s ease, background .15s;
}
.chaos-gate-answers button:hover { background: #4a3552; }
.chaos-gate-answers button:active { transform: scale(0.96); }
.chaos-gate-answers button.is-wrong {
  background: #5a2230;
  animation: pad-shake .35s ease;
}
.chaos-gate-answers button.is-right {
  background: var(--ok);
  color: #062416;
}
.chaos-gate-timer {
  height: 6px;
  border-radius: 999px;
  background: #2a2030;
  overflow: hidden;
  border: 1px solid #4a3040;
}
.chaos-gate-timer i {
  display: block;
  height: 100%;
  width: 100%;
  background: linear-gradient(90deg, var(--accent), var(--warn));
  transform-origin: left center;
  animation: none;
}
.chaos-gate-timer i.running {
  animation: gate-drain 3s linear forwards;
}
.chaos-gate-status {
  margin: 8px 0 0;
  font-size: 0.8rem;
  color: var(--mute);
  min-height: 1.2em;
}
.chaos-gate.is-penalty .chaos-gate-card {
  border-color: #c44;
  filter: saturate(1.2);
}
#chaos.is-locked .chaos-display,
#chaos.is-locked .chaos-pad,
#chaos.is-locked .hint {
  pointer-events: none;
  filter: blur(2px) grayscale(0.4);
  opacity: 0.55;
}
@keyframes gate-drain {
  from { transform: scaleX(1); }
  to { transform: scaleX(0); }
}

@keyframes pad-shake {
  0%, 100% { transform: translate(0); }
  20% { transform: translate(-6px, 3px) rotate(-1deg); }
  40% { transform: translate(7px, -2px) rotate(1deg); }
  60% { transform: translate(-4px, -3px); }
  80% { transform: translate(5px, 2px); }
}
@keyframes glitch {
  0% { transform: translate(0); }
  20% { transform: translate(-4px, 1px); text-shadow: 3px 0 #0ff, -3px 0 #f0f; }
  40% { transform: translate(4px, -1px); }
  60% { transform: translate(-2px, 2px); }
  100% { transform: translate(0); }
}
@keyframes pulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.04); }
  100% { transform: scale(1); }
}
@keyframes float-bob {
  0%, 100% { transform: translateY(0) rotate(0deg); }
  50% { transform: translateY(-10px) rotate(8deg); }
}
@keyframes win-in {
  from { opacity: 0; transform: scale(0.85) translateY(8px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

.chaos-msg {
  margin: 14px 0 0;
  padding: 12px 14px;
  border-radius: 12px;
  background: #2a1820;
  border: 1px solid #5a2a38;
  color: #ffb4c0;
  font-family: var(--display);
  font-weight: 700;
  font-size: 1.15rem;
  text-align: center;
}

@media (max-width: 480px) {
  .chaos-display { grid-template-columns: 1fr; }
  .pad button { min-height: 52px; font-size: 1.1rem; }
}
