:root {
  --bg: #05060f;
  --bg-2: #0b0d22;
  --text: #e6e8ff;
  --muted: #8b90b8;
  --cyan: #67e8f9;
  --violet: #a78bfa;
  --magenta: #f0abfc;
  --line: rgba(167, 139, 250, 0.22);
  --card: rgba(14, 16, 40, 0.72);
  --glitch: 0;
  --footer-h: 92px;
  --sans: "Space Grotesk", system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: "JetBrains Mono", ui-monospace, "SFMono-Regular", Consolas, monospace;
}

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

html, body {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  background: radial-gradient(120% 80% at 50% 35%, var(--bg-2) 0%, var(--bg) 70%);
  color: var(--text);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

#scene {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

#flash {
  position: fixed;
  inset: 0;
  background: radial-gradient(circle at 50% 42%, #fff 0%, #cffafe 35%, rgba(167, 139, 250, 0) 75%);
  opacity: 0;
  pointer-events: none;
  z-index: 20;
}
#flash.on { animation: flash 1.1s ease-out forwards; }
@keyframes flash {
  0% { opacity: 0; }
  12% { opacity: 1; }
  100% { opacity: 0; }
}

/* ---------- Screens ---------- */
.screen {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: var(--footer-h);
  padding: max(20px, env(safe-area-inset-top)) 16px 12px;
  display: none;
  flex-direction: column;
  align-items: center;
  z-index: 5;
}
body[data-state="intro"] #intro,
body[data-state="hud"] #hud,
body[data-state="result"] #result { display: flex; }

/* ---------- Intro ---------- */
#intro {
  justify-content: center;
  text-align: center;
  gap: 18px;
}
.intro-glyph {
  font-family: var(--mono);
  font-size: clamp(56px, 18vw, 96px);
  font-weight: 600;
  background: linear-gradient(135deg, var(--cyan), var(--violet) 55%, var(--magenta));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 0 24px rgba(103, 232, 249, 0.35));
  animation: breathe 3.2s ease-in-out infinite;
}
@keyframes breathe {
  0%, 100% { transform: scale(1); opacity: 0.9; }
  50% { transform: scale(1.06); opacity: 1; }
}
h1 {
  font-size: clamp(34px, 10vw, 52px);
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1;
}
h1 span {
  background: linear-gradient(90deg, var(--cyan), var(--magenta));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.intro-sub {
  max-width: 30ch;
  color: var(--muted);
  font-size: 17px;
  line-height: 1.45;
}
.intro-hint {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  opacity: 0.75;
  max-width: 34ch;
}

/* ---------- Buttons ---------- */
.btn {
  font: 600 16px/1 var(--sans);
  border-radius: 999px;
  padding: 16px 28px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.2s ease, background 0.2s ease;
}
.btn:active { transform: scale(0.96); }
.btn-primary {
  color: #05060f;
  background: linear-gradient(135deg, var(--cyan), var(--violet));
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.1) inset, 0 8px 32px rgba(103, 232, 249, 0.28);
}
.btn-primary:hover { box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.2) inset, 0 10px 40px rgba(167, 139, 250, 0.4); }
.btn-ghost {
  color: var(--text);
  background: rgba(255, 255, 255, 0.04);
  border-color: var(--line);
}
.btn:focus-visible, footer button:focus-visible, .orb:focus-visible {
  outline: 2px solid var(--cyan);
  outline-offset: 3px;
}

/* ---------- HUD ---------- */
#hud {
  justify-content: space-between;
  transform: translate(calc(var(--glitch) * 3px), calc(var(--glitch) * -2px));
}
.hud-top, .hud-bottom {
  width: 100%;
  max-width: 420px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.ket {
  font-family: var(--mono);
  font-size: clamp(13px, 3.8vw, 16px);
  text-align: center;
  color: var(--cyan);
  white-space: nowrap;
  text-shadow:
    calc(var(--glitch) * 3px) 0 rgba(240, 171, 252, 0.8),
    calc(var(--glitch) * -3px) 0 rgba(103, 232, 249, 0.8);
}
.ket sup { font-size: 0.68em; vertical-align: 0.5em; line-height: 0; }
.probs { display: grid; gap: 6px; }
.prob, .meter {
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  align-items: center;
  gap: 10px;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
}
.meter { grid-template-columns: auto 1fr 44px; }
.prob b, .meter b { color: var(--text); font-weight: 600; text-align: right; }
.bar {
  height: 6px;
  border-radius: 99px;
  background: rgba(255, 255, 255, 0.07);
  overflow: hidden;
}
.bar i {
  display: block;
  height: 100%;
  width: 0%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--cyan), var(--violet));
}
#p1 { background: linear-gradient(90deg, var(--violet), var(--magenta)); }
#energy {
  background: linear-gradient(90deg, var(--cyan), var(--violet), var(--magenta));
  box-shadow: 0 0 12px rgba(167, 139, 250, 0.7);
}

.orb {
  flex: 1;
  width: min(78vw, 46vh, 380px);
  max-height: min(78vw, 46vh, 380px);
  aspect-ratio: 1;
  margin: 8px auto;
  border-radius: 50%;
  background: transparent;
  border: none;
  cursor: pointer;
  touch-action: none;
  -webkit-touch-callout: none;
}

.status {
  text-align: center;
  font-size: 16px;
  font-weight: 500;
  min-height: 2.6em;
  line-height: 1.3;
  white-space: pre-line;
  display: flex;
  align-items: center;
  justify-content: center;
  text-shadow: calc(var(--glitch) * 2px) 0 rgba(240, 171, 252, 0.7);
}
.log {
  font-family: var(--mono);
  font-size: 10.5px;
  line-height: 1.5;
  color: var(--muted);
  opacity: 0.8;
  height: 4.5em;
  overflow: hidden;
  white-space: pre;
  text-overflow: ellipsis;
}

/* ---------- Result ---------- */
#result {
  justify-content: center;
  gap: 22px;
}
.card {
  width: 100%;
  max-width: 420px;
  padding: 24px 22px 20px;
  border-radius: 22px;
  background: var(--card);
  border: 1px solid var(--line);
  box-shadow: 0 0 60px rgba(103, 232, 249, 0.12), 0 20px 60px rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  animation: cardIn 0.7s cubic-bezier(0.2, 0.9, 0.3, 1.2) both;
}
@keyframes cardIn {
  from { opacity: 0; transform: translateY(24px) scale(0.92); filter: blur(8px); }
  to { opacity: 1; transform: none; filter: none; }
}
.card-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  font-family: var(--mono);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--muted);
  margin-bottom: 16px;
}
.card-cat {
  color: var(--magenta);
  text-transform: none;
  letter-spacing: 0;
  font-size: 13px;
}
.phrase {
  font-size: clamp(21px, 6vw, 26px);
  font-weight: 500;
  line-height: 1.32;
  min-height: 3.96em;
  user-select: text;
  -webkit-user-select: text;
}
.phrase .caret {
  display: inline-block;
  width: 0.5ch;
  margin-left: 2px;
  background: var(--cyan);
  animation: blink 0.9s steps(1) infinite;
}
@keyframes blink { 50% { opacity: 0; } }
.card-meta {
  margin-top: 18px;
  padding-top: 12px;
  border-top: 1px dashed var(--line);
  font-family: var(--mono);
  font-size: 11px;
  color: var(--muted);
  display: flex;
  flex-wrap: wrap;
  gap: 4px 14px;
}
.card-meta .q { color: var(--cyan); }
.actions { display: flex; gap: 12px; flex-wrap: wrap; justify-content: center; }
.result-hint {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
}

/* ---------- Footer ---------- */
footer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: var(--footer-h);
  padding: 10px 16px max(10px, env(safe-area-inset-bottom));
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border-top: 1px solid var(--line);
  background: linear-gradient(180deg, rgba(5, 6, 15, 0.4), rgba(5, 6, 15, 0.92));
  z-index: 10;
}
.credits {
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-size: 11px;
  line-height: 1.3;
  color: var(--muted);
  min-width: 0;
}
.credits strong { color: var(--text); font-size: 13px; font-weight: 600; }
.credits em {
  font-style: italic;
  opacity: 0.85;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.toggles { display: flex; flex-direction: column; align-items: flex-end; gap: 8px; flex-shrink: 0; }
.lang {
  display: flex;
  border: 1px solid var(--line);
  border-radius: 99px;
  overflow: hidden;
}
.lang button, .pill {
  font: 600 11px/1 var(--mono);
  color: var(--muted);
  background: transparent;
  border: none;
  padding: 7px 10px;
  cursor: pointer;
  white-space: nowrap;
}
.lang button[aria-pressed="true"] { color: #05060f; background: var(--cyan); }
.pill { border: 1px solid var(--line); border-radius: 99px; }
.toggles-row { display: flex; gap: 6px; }

.link-btn {
  font: 500 14px/1.2 var(--sans);
  color: var(--cyan);
  background: none;
  border: none;
  padding: 6px 4px;
  text-decoration: underline;
  text-underline-offset: 4px;
  text-decoration-color: rgba(103, 232, 249, 0.4);
  cursor: pointer;
}

/* ---------- Info dialog ---------- */
#info {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  margin: 0;
  padding: 0;
  border: none;
  color: var(--text);
  background: radial-gradient(120% 70% at 50% 0%, var(--bg-2) 0%, var(--bg) 70%);
  overflow-y: auto;
  overscroll-behavior: contain;
  user-select: text;
  -webkit-user-select: text;
}
#info[open] { display: block; animation: infoIn 0.35s ease-out both; }
#info::backdrop { background: var(--bg); }
@keyframes infoIn {
  from { opacity: 0; transform: translateY(16px); }
  to { opacity: 1; transform: none; }
}
.info-head {
  position: sticky;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: max(14px, env(safe-area-inset-top)) 16px 14px;
  background: rgba(5, 6, 15, 0.88);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
  z-index: 1;
}
.info-head h2 { font-size: 19px; font-weight: 700; letter-spacing: -0.01em; }
.info-head .pill { font-size: 12px; padding: 9px 14px; color: var(--text); }
.info-body {
  max-width: 640px;
  margin: 0 auto;
  padding: 20px 16px max(40px, env(safe-area-inset-bottom));
  font-size: 16px;
  line-height: 1.6;
  color: #c9cdf0;
}
.info-body .lead { font-size: 17px; color: var(--text); }
.info-body h3 {
  margin: 28px 0 8px;
  font-size: 13px;
  font-family: var(--mono);
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--cyan);
}
.info-body p + p, .info-body p + ol, .info-body ol + p { margin-top: 10px; }
.info-body strong { color: var(--text); font-weight: 600; }
.info-body ol { padding-left: 22px; display: grid; gap: 6px; }
.info-body ol li::marker { color: var(--magenta); font-family: var(--mono); }
.info-body .caption {
  margin: 4px 0 14px;
  text-align: center;
  font-family: var(--mono);
  font-size: 11.5px;
  line-height: 1.5;
  color: var(--muted);
}
.info-body .fine {
  margin-top: 32px;
  padding-top: 16px;
  border-top: 1px dashed var(--line);
  font-size: 13px;
  color: var(--muted);
}

.favor { display: block; width: 100%; max-width: 420px; margin: 8px auto 0; overflow: visible; }
.favor .f-shell { fill: rgba(103, 232, 249, 0.04); stroke: rgba(103, 232, 249, 0.35); stroke-dasharray: 3 5; }
.favor .f-ring { fill: none; stroke-width: 3; }
.favor .f-ring-a { stroke: var(--cyan); }
.favor .f-ring-b { stroke: var(--violet); }
.favor .f-core { fill: rgba(240, 171, 252, 0.15); stroke: var(--magenta); stroke-width: 2; }
.favor .f-core-on { fill: rgba(240, 171, 252, 0.35); filter: drop-shadow(0 0 8px rgba(240, 171, 252, 0.8)); }
.favor .f-nfc { font: 600 9px var(--mono); fill: var(--text); text-anchor: middle; }
.favor .f-arrow path { fill: none; stroke: var(--muted); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.favor .f-arrow text { font: 500 10px var(--mono); fill: var(--muted); text-anchor: middle; }
.favor .f-flip { transform-box: fill-box; transform-origin: center; animation: flip 3.2s ease-in-out infinite alternate; }
.favor .f-flip-b { animation-duration: 4.6s; animation-delay: -1.3s; }
@keyframes flip {
  from { transform: scaleY(1); }
  to { transform: scaleY(-1); }
}
@media (prefers-reduced-motion: reduce) {
  .favor .f-flip { animation: none; transform: scaleY(0.35); }
}

/* Hidden iOS switch used to trigger haptic feedback (Safari has no Vibration API). */
.haptic {
  position: fixed;
  left: 0;
  top: 0;
  width: 1px;
  height: 1px;
  opacity: 0;
  overflow: hidden;
  pointer-events: none;
}

/* ---------- Toast ---------- */
#toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--footer-h) + 16px);
  transform: translate(-50%, 20px);
  padding: 10px 16px;
  border-radius: 99px;
  background: var(--text);
  color: var(--bg);
  font-size: 14px;
  font-weight: 500;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
  z-index: 30;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-height: 640px) {
  :root { --footer-h: 80px; }
  .log { display: none; }
  .credits em { -webkit-line-clamp: 1; }
}

@media (prefers-reduced-motion: reduce) {
  .intro-glyph { animation: none; }
  #hud { transform: none; }
  .card { animation-duration: 0.01s; }
}
