:root {
  --bg: #0b0f1f;
  --panel: #171a28;
  --panel-2: #1d2030;
  --line: #2a2d3a;
  --muted: #9aa0b0;
  --text: #e9ebf2;
  --brand: #7c4dff;
  --pink: #e94560;
  --accent: var(--brand);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  /* Disable double-tap / pinch zoom on touch devices */
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  font-family: "Sarabun", system-ui, -apple-system, sans-serif;
  color: var(--text);
  background: radial-gradient(130% 120% at 50% 0%, #241552 0%, #131233 40%, var(--bg) 100%);
  min-height: 100vh;
  min-height: 100dvh;
  position: relative;
  overflow-x: hidden;
}

/* ── animated background ──────────────────────────────────── */
/* A fixed layer of slowly drifting, blurred colour orbs that float behind
   all content. GPU-friendly (only transform/opacity animate) and disabled
   for users who prefer reduced motion. */
.bg-fx {
  position: fixed;
  inset: 0;
  /* iOS Safari fails to paint a fixed layer at a NEGATIVE z-index when it
     also contains blended children, so keep it at 0 and lift the content
     above it instead (see .app). `isolation` gives the orbs a stacking
     context of their own so any blending has a backdrop to work against. */
  z-index: 0;
  isolation: isolate;
  overflow: hidden;
  pointer-events: none;
}

.bg-fx .orb {
  position: absolute;
  border-radius: 50%;
  filter: blur(60px);
  /* No mix-blend-mode: it silently breaks the whole layer on iOS Safari.
     A slightly higher opacity reads just as vivid on the dark backdrop. */
  opacity: 0.6;
  will-change: transform;
}

.bg-fx .orb-1 {
  width: 340px; height: 340px;
  top: -80px; left: -90px;
  background: radial-gradient(circle at 30% 30%, #7c4dff, transparent 70%);
  animation: drift-1 11s ease-in-out infinite;
}

.bg-fx .orb-2 {
  width: 300px; height: 300px;
  top: 35%; right: -110px;
  background: radial-gradient(circle at 30% 30%, #e94560, transparent 70%);
  animation: drift-2 13s ease-in-out infinite;
}

.bg-fx .orb-3 {
  width: 280px; height: 280px;
  bottom: -100px; left: 20%;
  background: radial-gradient(circle at 30% 30%, #3aa0ff, transparent 70%);
  animation: drift-3 15s ease-in-out infinite;
}

.bg-fx .orb-4 {
  width: 220px; height: 220px;
  top: 12%; right: 18%;
  background: radial-gradient(circle at 30% 30%, #34c46a, transparent 70%);
  animation: drift-4 12s ease-in-out infinite;
}

/* A faint shimmering veil that pans across the whole screen. It is a square
   sized to the viewport's longest side (800vmax) and centred, so however it
   rotates its corners always stay outside the viewport instead of sweeping
   across the edges. */
.bg-fx::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 800vmax;
  height: 800vmax;
  background: conic-gradient(
    from 0deg at 50% 50%,
    rgba(124, 77, 255, 0.08),
    rgba(233, 69, 96, 0.06),
    rgba(58, 160, 255, 0.08),
    rgba(124, 77, 255, 0.08)
  );
  transform: translate(-50%, -50%) rotate(0deg);
  transform-origin: center;
  animation: spin-veil 20s linear infinite;
}

@keyframes drift-1 {
  0%, 100% { transform: translate(0, 0) scale(1); }
  50% { transform: translate(60px, 70px) scale(1.15); }
}
@keyframes drift-2 {
  0%, 100% { transform: translate(0, 0) scale(1); }
  50% { transform: translate(-70px, 50px) scale(1.1); }
}
@keyframes drift-3 {
  0%, 100% { transform: translate(0, 0) scale(1); }
  50% { transform: translate(50px, -60px) scale(1.2); }
}
@keyframes drift-4 {
  0%, 100% { transform: translate(0, 0) scale(0.95); }
  50% { transform: translate(-40px, 60px) scale(1.1); }
}
@keyframes spin-veil {
  to { transform: translate(-50%, -50%) rotate(360deg); }
}

@media (prefers-reduced-motion: reduce) {
  .bg-fx .orb,
  .bg-fx::after { animation: none; }
}

.app {
  position: relative;
  z-index: 1; /* sit above the .bg-fx layer (which is now at z-index 0) */
  max-width: 480px;
  margin: 0 auto;
  padding: max(20px, env(safe-area-inset-top)) 18px max(24px, env(safe-area-inset-bottom));
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

/* ── screens ─────────────────────────────────────────────── */
.screen { display: none; animation: fade 0.35s ease; }
.screen.active { display: block; }

@keyframes fade {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 28px 24px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
}

/* ── intro ───────────────────────────────────────────────── */
.badge {
  display: inline-block;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.5px;
  color: #cdbcff;
  background: rgba(124, 77, 255, 0.16);
  border: 1px solid rgba(124, 77, 255, 0.35);
  padding: 5px 11px;
  border-radius: 999px;
  margin-bottom: 16px;
}

.intro h1 { font-size: 28px; margin: 0 0 12px; line-height: 1.2; }
.lead { color: var(--muted); font-size: 15px; line-height: 1.7; margin: 0 0 22px; }

.facts {
  list-style: none;
  display: flex;
  gap: 10px;
  padding: 0;
  margin: 0 0 24px;
}
.facts li {
  flex: 1;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 8px;
  text-align: center;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.5;
}
.facts strong { display: block; font-size: 20px; color: #fff; margin-bottom: 2px; }

/* ── buttons ─────────────────────────────────────────────── */
.btn {
  display: block;
  width: 100%;
  border: 0;
  border-radius: 14px;
  padding: 16px;
  font-family: inherit;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.1s ease, opacity 0.2s ease;
}
.btn:active { transform: scale(0.98); }
.btn.primary { background: linear-gradient(135deg, var(--brand), var(--pink)); color: #fff; }
.btn.ghost {
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: #cfd3e0;
  margin-top: 16px;
}

.link-btn {
  background: none;
  border: 0;
  color: var(--muted);
  font-family: inherit;
  font-size: 13px;
  cursor: pointer;
  padding: 4px;
}
.link-btn:hover { color: var(--text); }

/* ── quiz ────────────────────────────────────────────────── */
.quiz-head { margin-bottom: 18px; }
.progress {
  height: 8px;
  background: var(--panel-2);
  border-radius: 999px;
  overflow: hidden;
  border: 1px solid var(--line);
}
.progress-bar {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--brand), var(--pink));
  border-radius: 999px;
  transition: width 0.35s ease;
}
.progress-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 10px;
  font-size: 13px;
  color: var(--muted);
}

.question h2 {
  font-size: 21px;
  line-height: 1.45;
  margin: 0 0 22px;
}

.options { display: flex; flex-direction: column; gap: 12px; }

/* ── 5-level scale ───────────────────────────────────────── */
.scale { display: flex; flex-direction: column; gap: 18px; }
.scale-poles {
  display: flex;
  justify-content: space-between;
  gap: 14px;
}
.scale-poles .pole {
  flex: 1 1 0;
  font-size: 14px;
  line-height: 1.45;
  color: var(--text);
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--panel-2);
  border: 1px solid var(--line);
}
.scale-poles .pole-left { text-align: left; }
.scale-poles .pole-right { text-align: right; }

.scale-dots {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: clamp(4px, 1.5vw, 10px);
  padding: 4px 2px;
  /* Smallest (neutral, --d:0) → largest (strong ends, --d:1). The dot width
     interpolates continuously from these bounds so every step grows evenly. */
  --dot-min: clamp(22px, 6vw, 28px);
  --dot-max: clamp(34px, 9.5vw, 46px);
}
.dot {
  flex: 0 0 auto;
  width: calc(var(--dot-min) + (var(--dot-max) - var(--dot-min)) * var(--d, 0));
  aspect-ratio: 1;
  border: 2.5px solid;
  border-radius: 50%;
  background: transparent;
  cursor: pointer;
  padding: 0;
  transition: transform 0.12s ease, background 0.15s ease, border-color 0.15s ease,
    box-shadow 0.15s ease;
}
.dot.side-left { border-color: var(--brand); }
.dot.side-right { border-color: var(--pink); }
.dot.side-neutral { border-color: #4a4f63; }
.dot:active { transform: scale(0.9); }
.dot:hover { background: rgba(255, 255, 255, 0.06); }
.dot.side-left.selected {
  background: var(--brand);
  box-shadow: 0 0 0 4px rgba(124, 77, 255, 0.22);
}
.dot.side-right.selected {
  background: var(--pink);
  box-shadow: 0 0 0 4px rgba(233, 69, 96, 0.22);
}
.dot.side-neutral.selected {
  background: #6b7081;
  box-shadow: 0 0 0 4px rgba(154, 160, 176, 0.22);
}

.scale-ends {
  display: flex;
  justify-content: space-between;
  font-size: 11.5px;
  color: var(--muted);
}

/* ── result ──────────────────────────────────────────────── */
.result { padding: 0; overflow: hidden; }
.result-hero {
  padding: 30px 24px 26px;
  text-align: center;
  background: linear-gradient(160deg, var(--accent) 0%, rgba(0, 0, 0, 0.25) 140%);
  position: relative;
}
.result-hero .nick {
  display: inline-block;
  font-size: 13px;
  font-weight: 600;
  color: rgba(255, 255, 255, 0.92);
  background: rgba(0, 0, 0, 0.22);
  padding: 5px 12px;
  border-radius: 999px;
  margin-bottom: 12px;
}
.result-hero .code {
  font-family: "JetBrains Mono", monospace;
  font-size: 52px;
  font-weight: 700;
  letter-spacing: 4px;
  color: #fff;
  margin: 0;
  text-shadow: 0 4px 18px rgba(0, 0, 0, 0.35);
}
.result-hero .desc {
  color: rgba(255, 255, 255, 0.94);
  font-size: 15px;
  line-height: 1.7;
  margin: 14px 0 0;
}
.result-hero .match {
  display: flex;
  gap: 10px;
  margin-top: 18px;
}
.result-hero .match > div {
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 12px 10px;
  border-radius: 14px;
  background: rgba(0, 0, 0, 0.22);
  border: 1px solid rgba(255, 255, 255, 0.16);
}
.result-hero .match .match-label {
  font-size: 11.5px;
  font-weight: 600;
  color: rgba(255, 255, 255, 0.82);
}
.result-hero .match .match-pct {
  font-family: "JetBrains Mono", monospace;
  font-size: 26px;
  font-weight: 700;
  color: #fff;
  line-height: 1;
}
.result-hero .match .match-sub { opacity: 0.92; }
.result-hero .match .match-sub .match-pct { font-size: 22px; }

.result-body { padding: 22px 22px 26px; }

/* Dominant ("Hero") function callout */
.dominant {
  margin: 4px 0 22px;
  padding: 16px 18px;
  border-radius: 16px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-left: 4px solid var(--accent);
}
.dominant-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}
.dominant-badge {
  font-size: 11.5px;
  font-weight: 700;
  letter-spacing: 0.4px;
  color: var(--accent);
  text-transform: uppercase;
}
.dominant-clarity {
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
}
.dominant-fn {
  font-size: 19px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 6px;
}
.dominant-desc {
  font-size: 14px;
  line-height: 1.6;
  color: var(--muted);
  margin: 0;
}

/* Enneagram cross-reference card */
.enneagram {
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--panel-2);
  padding: 16px;
}
.enn-head {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 12px;
}
.enn-num {
  flex: 0 0 auto;
  width: 46px;
  height: 46px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  font-size: 22px;
  font-weight: 800;
  color: #fff;
  background: linear-gradient(135deg, var(--brand), var(--pink));
  box-shadow: 0 4px 14px rgba(124, 77, 255, 0.35);
}
.enn-id { display: flex; flex-direction: column; gap: 2px; }
.enn-name { font-size: 16px; font-weight: 700; color: var(--text); }
.enn-en { font-size: 12.5px; color: var(--muted); }
.enn-core {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 12px;
}
.enn-core p { margin: 0; font-size: 13.5px; line-height: 1.55; color: #d4d8e4; }
.enn-core b { color: var(--text); }
.enn-alts {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-bottom: 12px;
}
.enn-alts-label { font-size: 12px; color: var(--muted); margin-right: 2px; }
.enn-alt {
  font-size: 12.5px;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--line);
  color: #d4d8e4;
}
.enn-note {
  margin: 0 0 8px;
  font-size: 13px;
  line-height: 1.6;
  color: var(--muted);
}
.enn-disclaimer {
  margin: 0;
  font-size: 11.5px;
  line-height: 1.5;
  color: var(--muted);
  opacity: 0.75;
}

.tags { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 22px; }
.tag {
  font-size: 12.5px;
  padding: 7px 12px;
  border-radius: 999px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: #d4d8e4;
}
.tag .dot {
  display: inline-block;
  width: 9px; height: 9px;
  border-radius: 50%;
  margin-right: 6px;
  vertical-align: middle;
}

.section-title {
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.6px;
  margin: 22px 0 12px;
}

/* axis bars */
.axes { display: flex; flex-direction: column; gap: 14px; }
.axis-row { font-size: 13px; }
.axis-labels {
  display: flex;
  justify-content: space-between;
  margin-bottom: 5px;
  color: var(--muted);
}
.axis-labels b { color: var(--text); }
.axis-track {
  position: relative;
  height: 8px;
  border-radius: 999px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  overflow: hidden;
}
.axis-fill {
  position: absolute;
  top: 0; bottom: 0;
  background: linear-gradient(90deg, var(--brand), var(--pink));
}

/* function stacks */
.stack { display: flex; flex-direction: column; gap: 8px; }
.fn-row {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 11px;
  padding: 10px 12px;
}
.fn-badge {
  font-family: "JetBrains Mono", monospace;
  font-weight: 700;
  font-size: 14px;
  min-width: 40px;
  text-align: center;
  padding: 6px 0;
  border-radius: 8px;
  background: rgba(124, 77, 255, 0.18);
  color: #cdbcff;
}
.fn-row.shadow .fn-badge { background: rgba(233, 69, 96, 0.16); color: #ffb3c0; }
.fn-body { display: flex; flex-direction: column; gap: 2px; }
.fn-stage { font-size: 13px; color: var(--muted); }
.fn-desc { font-size: 12.5px; line-height: 1.5; color: #c3c8d6; }

.legend { font-size: 12px; color: var(--muted); line-height: 1.7; margin-top: 14px; }

/* ── traits screen ───────────────────────────────────────── */
.traits { padding: 0; overflow: hidden; }
.traits-hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  padding: 20px 22px 22px;
  background: linear-gradient(160deg, var(--accent) 0%, rgba(0, 0, 0, 0.25) 150%);
}
.type-banner {
  width: 100%;
  border-radius: 16px;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.96);
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.32);
}
.type-banner img {
  display: block;
  width: 100%;
  height: auto;
}
.type-img {
  flex: 0 0 auto;
  background-repeat: no-repeat;
  border-radius: 16px;
  background-color: #fff;
  box-shadow: 0 10px 26px rgba(0, 0, 0, 0.3);
}
.traits-id { color: #fff; text-align: center; }
.traits-id .nick {
  display: inline-block;
  font-size: 13px;
  font-weight: 600;
  color: rgba(255, 255, 255, 0.92);
  background: rgba(0, 0, 0, 0.22);
  padding: 5px 12px;
  border-radius: 999px;
  margin-bottom: 10px;
}
.traits-id .code {
  font-family: "JetBrains Mono", monospace;
  font-size: 40px;
  font-weight: 700;
  letter-spacing: 3px;
  margin: 0;
  text-shadow: 0 4px 18px rgba(0, 0, 0, 0.35);
}
.traits-id .group-pill {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  margin-top: 10px;
  font-size: 13px;
  font-weight: 600;
  color: #fff;
  background: rgba(0, 0, 0, 0.22);
  padding: 5px 13px;
  border-radius: 999px;
}
.traits-id .group-pill .dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  display: inline-block;
}
.overview-text {
  margin: 0 0 6px;
  font-size: 14.5px;
  line-height: 1.7;
  color: var(--text);
}

.trait-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 9px;
}
.trait-list li {
  position: relative;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 11px;
  padding: 12px 14px 12px 38px;
  font-size: 14.5px;
  line-height: 1.5;
}
.trait-list li::before {
  position: absolute;
  left: 13px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 14px;
}
.traits-only li::before { content: "✦"; color: var(--accent); }
.likes-only li::before { content: "♥"; color: var(--pink); }

/* ── clinic role screen ──────────────────────────────────── */
.btn-row { display: flex; gap: 10px; }
.btn-row .btn { margin-top: 16px; }

.clinic { padding: 0; overflow: hidden; }
.clinic-head { padding: 26px 22px 6px; }
.clinic-head h2 { font-size: 22px; margin: 0 0 10px; }
.clinic-head p { color: var(--muted); font-size: 14px; line-height: 1.7; margin: 0; }

.role-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  padding: 20px 22px 26px;
}
.role-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  background: var(--panel-2);
  border: 1.5px solid var(--line);
  border-radius: 14px;
  padding: 18px 12px;
  color: var(--text);
  font-family: inherit;
  cursor: pointer;
  transition: border-color 0.15s ease, transform 0.1s ease, background 0.15s ease;
}
.role-btn:hover { border-color: var(--accent); background: rgba(124, 77, 255, 0.1); }
.role-btn:active { transform: scale(0.98); }
.role-emoji { font-size: 30px; line-height: 1; }
.role-name { font-size: 13.5px; font-weight: 600; text-align: center; line-height: 1.4; }

.clinic-hero {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 24px 22px;
  background: linear-gradient(160deg, var(--accent) 0%, rgba(0, 0, 0, 0.25) 150%);
}
.role-emoji-lg {
  font-size: 46px;
  line-height: 1;
  background: rgba(255, 255, 255, 0.16);
  border-radius: 16px;
  padding: 12px;
}
.clinic-id { color: #fff; }
.clinic-id .nick {
  display: inline-block;
  font-family: "JetBrains Mono", monospace;
  font-size: 12.5px;
  font-weight: 600;
  color: rgba(255, 255, 255, 0.92);
  background: rgba(0, 0, 0, 0.22);
  padding: 4px 11px;
  border-radius: 999px;
  margin-bottom: 9px;
}
.clinic-id h2 { font-size: 24px; margin: 0; text-shadow: 0 4px 18px rgba(0, 0, 0, 0.3); }

.duty-only li::before { content: "▸"; color: var(--accent); }
.clinic-text {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px 16px;
  font-size: 14.5px;
  line-height: 1.7;
  margin: 0;
}
.back-roles { margin-top: 22px; }

/* ── loading ─────────────────────────────────────────────── */
.loading {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: rgba(11, 15, 31, 0.7);
  backdrop-filter: blur(2px);
}
.loading[hidden] { display: none; }
.spinner {
  width: 38px; height: 38px;
  border: 3px solid var(--line);
  border-top-color: var(--brand);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
