/* Gemfall — luminous glass gems. Theme colours live under [data-theme=…];
   everything else reads the tokens. Board classes are prefixed m3- so they
   never collide with panel markup. */

:root,
[data-theme="crystal"] {
  --bg-a: #1b1533;
  --bg-b: #2b1a48;
  --bg-c: #0e1a31;
  --blob1: rgba(255, 61, 110, 0.38);
  --blob2: rgba(61, 123, 255, 0.36);
  --blob3: rgba(255, 176, 46, 0.26);
  --ink: #f5f0fc;
  --ink-soft: #b7add2;
  --paper: #251d3f;
  --paper-2: #312757;
  --line: rgba(255, 255, 255, 0.1);
  --card: rgba(255, 255, 255, 0.07);
  --card-border: rgba(255, 255, 255, 0.12);
  --tray: linear-gradient(160deg, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.025));
  --tray-border: rgba(255, 255, 255, 0.16);
  --tray-shadow: 0 30px 60px -24px rgba(0, 0, 0, 0.7), inset 0 1px 0 rgba(255, 255, 255, 0.18);
  --cell-a: rgba(255, 255, 255, 0.045);
  --cell-b: rgba(255, 255, 255, 0.085);
  --g0: #ff3d6e;
  --g1: #3d7bff;
  --g2: #2fd37a;
  --g3: #ffb02e;
  --g4: #b05cff;
  --g5: #22d6ee;
  --accent: #ffb02e;
  --accent-2: #ff7a3d;
  --accent-ink: #2b1a05;
  --btn-bg: rgba(255, 255, 255, 0.08);
  --btn-border: rgba(255, 255, 255, 0.14);
  --btn-hover: rgba(255, 255, 255, 0.14);
  --star: #ffcf4a;
  --star-off: rgba(255, 255, 255, 0.16);
  --track: rgba(255, 255, 255, 0.1);
  --toast-bg: rgba(20, 14, 36, 0.94);
  --toast-ink: #fff;
  --backdrop: rgba(5, 3, 12, 0.55);
  --shadow-strong: rgba(0, 0, 0, 0.55);
  --spark: #fff;
  --radius: 18px;
  --ease: cubic-bezier(0.4, 0.1, 0.2, 1);
  --ease-fall: cubic-bezier(0.45, 0, 0.55, 1.18);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color-scheme: dark;
}

[data-theme="candy"] {
  --bg-a: #fff3e8;
  --bg-b: #ffe4ee;
  --bg-c: #e6f0ff;
  --blob1: rgba(255, 111, 156, 0.35);
  --blob2: rgba(106, 164, 255, 0.3);
  --blob3: rgba(255, 210, 90, 0.35);
  --ink: #3b2a38;
  --ink-soft: #8d7187;
  --paper: #fffaf6;
  --paper-2: #fbeee9;
  --line: rgba(80, 40, 60, 0.12);
  --card: rgba(255, 255, 255, 0.55);
  --card-border: rgba(255, 255, 255, 0.8);
  --tray: linear-gradient(160deg, rgba(255, 255, 255, 0.85), rgba(255, 255, 255, 0.55));
  --tray-border: rgba(255, 255, 255, 0.9);
  --tray-shadow: 0 30px 60px -28px rgba(140, 80, 110, 0.5), inset 0 1px 0 #fff;
  --cell-a: rgba(255, 150, 190, 0.1);
  --cell-b: rgba(255, 150, 190, 0.2);
  --g0: #ff5c8d;
  --g1: #5b9bff;
  --g2: #43cf92;
  --g3: #ffc83c;
  --g4: #a877ff;
  --g5: #45cfe6;
  --accent: #ff6f9c;
  --accent-2: #ff9a6a;
  --accent-ink: #fff;
  --btn-bg: rgba(255, 255, 255, 0.6);
  --btn-border: rgba(80, 40, 60, 0.12);
  --btn-hover: rgba(255, 255, 255, 0.9);
  --star: #ffb52e;
  --star-off: rgba(80, 40, 60, 0.14);
  --track: rgba(80, 40, 60, 0.1);
  --toast-bg: rgba(59, 42, 56, 0.94);
  --toast-ink: #fff;
  --backdrop: rgba(70, 40, 60, 0.35);
  --shadow-strong: rgba(90, 40, 70, 0.35);
  --spark: #fff;
  color-scheme: light;
}

[data-theme="garden"] {
  --bg-a: #f2ecdc;
  --bg-b: #e6e9d4;
  --bg-c: #d9e4d8;
  --blob1: rgba(217, 72, 79, 0.22);
  --blob2: rgba(92, 166, 79, 0.28);
  --blob3: rgba(233, 165, 60, 0.3);
  --ink: #2f3328;
  --ink-soft: #717660;
  --paper: #f9f6ec;
  --paper-2: #ece7d6;
  --line: rgba(50, 55, 35, 0.12);
  --card: rgba(255, 255, 255, 0.42);
  --card-border: rgba(255, 255, 255, 0.7);
  --tray: repeating-linear-gradient(45deg, rgba(255, 255, 255, 0.18) 0 2px, rgba(0, 0, 0, 0.02) 2px 4px), linear-gradient(160deg, #efe7d1, #e3dcc4);
  --tray-border: rgba(255, 255, 255, 0.8);
  --tray-shadow: 0 30px 60px -28px rgba(60, 60, 30, 0.5), inset 0 1px 0 #fff;
  --cell-a: rgba(90, 80, 40, 0.06);
  --cell-b: rgba(90, 80, 40, 0.12);
  --g0: #e04a52;
  --g1: #4c7fdb;
  --g2: #5aaa4c;
  --g3: #eea63a;
  --g4: #9b74d2;
  --g5: #3fb3bb;
  --accent: #5aaa4c;
  --accent-2: #3f8f3a;
  --accent-ink: #fff;
  --btn-bg: rgba(255, 255, 255, 0.5);
  --btn-border: rgba(50, 55, 35, 0.14);
  --btn-hover: rgba(255, 255, 255, 0.85);
  --star: #f0a83a;
  --star-off: rgba(50, 55, 35, 0.14);
  --track: rgba(50, 55, 35, 0.1);
  --toast-bg: rgba(47, 51, 40, 0.94);
  --toast-ink: #fff;
  --backdrop: rgba(40, 45, 25, 0.38);
  --shadow-strong: rgba(50, 50, 20, 0.35);
  --spark: #fff;
  color-scheme: light;
}

/* gem gradient stops (the library in index.html reads these) */
.st0 { --gx: var(--g0); } .st1 { --gx: var(--g1); } .st2 { --gx: var(--g2); }
.st3 { --gx: var(--g3); } .st4 { --gx: var(--g4); } .st5 { --gx: var(--g5); }
.hi { stop-color: color-mix(in oklab, var(--gx) 62%, white); }
.mid { stop-color: var(--gx); }
.lo { stop-color: color-mix(in oklab, var(--gx) 66%, black); }
.gem-shadow { fill: #000; fill-opacity: 0.3; transform: translate(0, 5px); }
.gem-rim { fill: none; stroke: #fff; stroke-opacity: 0.45; stroke-width: 2; }
.cb0 { fill: var(--g0); } .cb1 { fill: var(--g1); } .cb2 { fill: var(--g2); }
.cb3 { fill: var(--g3); } .cb4 { fill: var(--g4); } .cb5 { fill: var(--g5); }
.cb-ring { transform-origin: 50px 50px; animation: m3-spin 7s linear infinite; }
.sp-bars rect { fill: #fff; fill-opacity: 0.92; stroke: rgba(0, 0, 0, 0.25); stroke-width: 1; }
.sp-ring { fill: none; stroke: #fff; stroke-opacity: 0.92; stroke-width: 5; }
.sp-star { fill: #fff; fill-opacity: 0.95; }
@keyframes m3-spin { to { transform: rotate(360deg); } }

* { box-sizing: border-box; }
[hidden] { display: none !important; }

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

body {
  font-family: var(--sans);
  color: var(--ink);
  background: linear-gradient(165deg, var(--bg-a), var(--bg-b) 55%, var(--bg-c));
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  transition: background 0.8s, color 0.4s;
}

.defs { position: absolute; width: 0; height: 0; overflow: hidden; }

/* ---------------------------------------------------------- background */
.bg { position: fixed; inset: 0; overflow: hidden; z-index: 0; pointer-events: none; }
.bg .blob { position: absolute; border-radius: 50%; filter: blur(70px); will-change: transform; transition: background 0.8s; }
.bg .b1 { width: 62vmax; height: 62vmax; left: -22vmax; top: -28vmax; background: var(--blob1); animation: drift 46s ease-in-out infinite alternate; }
.bg .b2 { width: 54vmax; height: 54vmax; right: -22vmax; bottom: -22vmax; background: var(--blob2); animation: drift 58s ease-in-out infinite alternate-reverse; }
.bg .b3 { width: 42vmax; height: 42vmax; left: 28vw; bottom: -26vmax; background: var(--blob3); animation: drift 70s ease-in-out infinite alternate; }
@keyframes drift {
  from { transform: translate(0, 0) scale(1); }
  to { transform: translate(9vmax, 7vmax) scale(1.14); }
}

/* -------------------------------------------------------------- layout */
.app {
  position: fixed;
  inset: 0;
  z-index: 1;
  display: flex;
  flex-direction: column;
  max-width: 720px;
  margin: 0 auto;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.top { display: flex; align-items: center; justify-content: space-between; padding: 10px 16px 2px; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand img { width: 40px; height: 40px; filter: drop-shadow(0 3px 5px var(--shadow-strong)); }
.brand span { display: flex; flex-direction: column; line-height: 1.1; }
.brand b { font-family: var(--serif); font-weight: 600; font-size: 23px; letter-spacing: 0.02em; }
.brand small { font-size: 11px; color: var(--ink-soft); letter-spacing: 0.1em; text-transform: uppercase; margin-top: 2px; }
.menu { display: flex; gap: 2px; }

.icon-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--ink);
  width: 42px;
  height: 42px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: background 0.2s, transform 0.2s;
}
.icon-btn:hover { background: var(--btn-hover); }
.icon-btn:active { transform: scale(0.92); }
.icon-btn svg, .btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

/* ----------------------------------------------------------------- hud */
.hud { padding: 4px 16px 0; }
.stats { display: grid; grid-template-columns: 1.2fr 1fr 1fr; gap: 8px; }
.stat {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: 14px;
  padding: 6px 8px 5px;
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1.15;
  min-width: 0;
}
.stat span { font-size: 10.5px; letter-spacing: 0.1em; text-transform: uppercase; color: var(--ink-soft); }
.stat b { font-family: var(--serif); font-size: 24px; font-weight: 600; font-variant-numeric: tabular-nums; letter-spacing: 0.01em; }
.stat.main b { font-size: 26px; }
.stat b.bump { animation: m3-bump 0.4s var(--ease); }
.stat b.low { color: var(--accent); }
@keyframes m3-bump { 35% { transform: scale(1.18); } }

.starbar { position: relative; height: 26px; margin: 6px 10px 0; }
.starbar[hidden] { display: none; }
.starbar-track { position: absolute; left: 0; right: 0; top: 50%; height: 6px; transform: translateY(-50%); border-radius: 3px; background: var(--track); overflow: hidden; }
.starbar-fill { position: absolute; inset: 0; width: var(--p, 0%); border-radius: 3px; background: linear-gradient(90deg, var(--accent-2), var(--accent)); transition: width 0.7s var(--ease); box-shadow: 0 0 10px color-mix(in oklab, var(--accent) 60%, transparent); }
.star { position: absolute; top: 50%; width: 22px; height: 22px; transform: translate(-50%, -50%); color: var(--star-off); transition: color 0.3s, transform 0.3s; }
.star svg { width: 100%; height: 100%; fill: currentColor; filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.3)); }
.starbar .star { background: var(--bg-a); border-radius: 50%; padding: 2px; }
.starbar .s1 { left: 30.8%; } .starbar .s2 { left: 61.5%; } .starbar .s3 { left: 100%; }
.star.lit { color: var(--star); }
.star.lit svg { filter: drop-shadow(0 0 6px color-mix(in oklab, var(--star) 70%, transparent)); animation: m3-starpop 0.6s var(--ease); }
@keyframes m3-starpop { 40% { transform: scale(1.5) rotate(-12deg); } }

.stage { flex: 1; min-height: 0; container-type: size; display: grid; place-items: center; padding: 8px 14px 6px; }

.actions { display: flex; gap: 10px; padding: 4px 16px 12px; align-items: stretch; }
.seg { display: flex; flex: 1; background: var(--card); border: 1px solid var(--card-border); border-radius: 15px; padding: 3px; gap: 2px; }
.seg-btn {
  appearance: none;
  border: 0;
  flex: 1;
  background: transparent;
  color: var(--ink-soft);
  font: 600 14px var(--sans);
  padding: 9px 6px;
  border-radius: 12px;
  cursor: pointer;
  transition: background 0.25s, color 0.25s, box-shadow 0.25s;
}
.seg-btn[aria-checked="true"] { background: var(--paper); color: var(--ink); box-shadow: 0 2px 8px -2px var(--shadow-strong); }
.btn {
  appearance: none;
  flex: 1;
  border: 1px solid var(--btn-border);
  background: var(--btn-bg);
  color: var(--ink);
  font: 600 15px var(--sans);
  padding: 11px 12px;
  border-radius: 15px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  transition: transform 0.15s, background 0.2s, opacity 0.2s, filter 0.2s;
}
.btn svg { width: 19px; height: 19px; }
.btn:hover { background: var(--btn-hover); }
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.4; cursor: default; transform: none; }
.btn.primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: var(--accent-ink);
  border-color: transparent;
  box-shadow: 0 8px 18px -8px color-mix(in oklab, var(--accent) 80%, black), inset 0 1px 0 rgba(255, 255, 255, 0.35);
}
.btn.primary:hover { filter: brightness(1.06); }
.btn.subtle { flex: 0 0 auto; background: transparent; border-color: transparent; color: var(--ink-soft); font-size: 13.5px; margin: 18px auto 0; display: flex; }

/* --------------------------------------------------------------- board */
.m3-board {
  --cell: 40px;
  width: min(100cqw, 100cqh);
  aspect-ratio: 1;
  position: relative;
  border-radius: 16px;
  background: var(--tray);
  border: 1px solid var(--tray-border);
  box-shadow: var(--tray-shadow);
  touch-action: none;
  outline: none;
  transition: background 0.6s;
}
.m3-board:focus-visible { box-shadow: var(--tray-shadow), 0 0 0 3px color-mix(in oklab, var(--accent) 60%, transparent); }
.m3-grid, .m3-pieces, .m3-fx { position: absolute; inset: 2.2%; }
.m3-pieces { overflow: hidden; border-radius: 6px; }
.m3-fx { pointer-events: none; overflow: hidden; z-index: 3; }

.m3-cell { position: absolute; left: 0; top: 0; width: 12.5%; height: 12.5%; transform: translate(calc(var(--c) * 100%), calc(var(--r) * 100%)); }
.m3-cell::before { content: ""; position: absolute; inset: 5%; border-radius: 22%; background: var(--cell-a); }
.m3-cell.alt::before { background: var(--cell-b); }

.m3-piece {
  position: absolute;
  left: 0;
  top: 0;
  width: 12.5%;
  height: 12.5%;
  transform: translate(calc(var(--c) * 100%), calc(var(--r) * 100%));
  transition: transform var(--dur, 0.26s) var(--easing, var(--ease)) var(--delay, 0s);
  will-change: transform;
  cursor: grab;
  z-index: 1;
}
.m3-piece.instant { transition: none; }
.m3-piece.sel, .m3-piece.hint, .m3-piece.sp { z-index: 2; }
.m3-piece.pop { pointer-events: none; z-index: 4; }
.m3-piece svg {
  position: absolute;
  inset: 6%;
  width: 88%;
  height: 88%;
  overflow: visible;
  transform-origin: 50% 50%;
  transition: transform 0.22s var(--ease), filter 0.3s;
}
.m3-piece::after {
  content: "";
  position: absolute;
  inset: 2%;
  border-radius: 28%;
  box-shadow: 0 0 0 2px color-mix(in oklab, var(--spark) 90%, transparent), 0 0 14px 2px color-mix(in oklab, var(--gc) 70%, transparent);
  opacity: 0;
  transition: opacity 0.2s;
  pointer-events: none;
}
.m3-piece.sel::after { opacity: 1; }
.m3-piece.sel svg { transform: translateY(-6%) scale(1.14); filter: drop-shadow(0 8px 8px rgba(0, 0, 0, 0.35)); }
.m3-piece.t0 { --gc: var(--g0); } .m3-piece.t1 { --gc: var(--g1); } .m3-piece.t2 { --gc: var(--g2); }
.m3-piece.t3 { --gc: var(--g3); } .m3-piece.t4 { --gc: var(--g4); } .m3-piece.t5 { --gc: var(--g5); } .m3-piece.tc { --gc: #fff; }

.m3-piece.sp svg { filter: drop-shadow(0 0 4px color-mix(in oklab, var(--gc) 85%, white)); animation: m3-glow 1.5s ease-in-out infinite alternate; }
.m3-piece.tc svg { filter: drop-shadow(0 0 5px #fff); animation: m3-glow 1.1s ease-in-out infinite alternate; }
@keyframes m3-glow { to { filter: drop-shadow(0 0 10px color-mix(in oklab, var(--gc) 95%, white)); } }

.m3-piece.hint svg { animation: m3-wiggle 1s ease-in-out infinite; }
@keyframes m3-wiggle {
  0%, 100% { transform: translate(0, 0); }
  30% { transform: translate(calc(var(--hx) * 14%), calc(var(--hy) * 14%)) scale(1.06); }
  60% { transform: translate(calc(var(--hx) * -4%), calc(var(--hy) * -4%)); }
}
.m3-piece.pop svg { animation: m3-pop 0.34s cubic-bezier(0.3, 0, 0.7, 1) forwards; }
@keyframes m3-pop {
  0% { transform: scale(1); opacity: 1; }
  30% { transform: scale(1.28); opacity: 1; filter: brightness(1.9); }
  100% { transform: scale(0.1); opacity: 0; filter: brightness(2.2); }
}
.m3-piece.born svg { animation: m3-born 0.42s var(--ease) both; }
@keyframes m3-born {
  0% { transform: scale(0) rotate(-40deg); opacity: 0; }
  60% { transform: scale(1.25) rotate(6deg); opacity: 1; }
  100% { transform: scale(1) rotate(0); }
}
.m3-piece.convert svg { animation: m3-convert 0.36s var(--ease); }
@keyframes m3-convert { 50% { transform: scale(1.3); filter: brightness(2); } }
.m3-piece.shake svg { animation: m3-shake 0.3s ease-in-out; }
@keyframes m3-shake { 25% { transform: translateX(-8%); } 50% { transform: translateX(7%); } 75% { transform: translateX(-4%); } }

.m3-cursor {
  position: absolute;
  left: 2.2%;
  top: 2.2%;
  width: calc(95.6% / 8);
  height: calc(95.6% / 8);
  transform: translate(calc(var(--c) * 100%), calc(var(--r) * 100%));
  border-radius: 24%;
  box-shadow: inset 0 0 0 2.5px var(--accent), 0 0 10px color-mix(in oklab, var(--accent) 60%, transparent);
  pointer-events: none;
  transition: transform 0.15s var(--ease);
  z-index: 3;
}

/* effects */
.m3-spark {
  position: absolute;
  left: var(--x);
  top: var(--y);
  width: var(--s, 8px);
  height: var(--s, 8px);
  border-radius: 50%;
  background: var(--gc, #fff);
  box-shadow: 0 0 6px var(--gc, #fff);
  animation: m3-spark var(--t, 0.6s) cubic-bezier(0.2, 0.6, 0.4, 1) forwards;
}
.m3-spark.star { border-radius: 0; background: var(--spark); clip-path: polygon(50% 0, 62% 38%, 100% 50%, 62% 62%, 50% 100%, 38% 62%, 0 50%, 38% 38%); box-shadow: none; }
@keyframes m3-spark {
  from { transform: translate(-50%, -50%) scale(1); opacity: 1; }
  to { transform: translate(calc(-50% + var(--dx)), calc(-50% + var(--dy))) scale(0); opacity: 0; }
}
.m3-beam {
  position: absolute;
  background: linear-gradient(90deg, transparent, color-mix(in oklab, var(--gc, #fff) 40%, white), transparent);
  border-radius: 999px;
  animation: m3-beam 0.4s ease-out forwards;
  box-shadow: 0 0 20px color-mix(in oklab, var(--gc, #fff) 70%, white);
}
.m3-beam.h { left: -3%; width: 106%; top: calc(var(--r) * 12.5% + 2.5%); height: 7.5%; }
.m3-beam.v { top: -3%; height: 106%; left: calc(var(--c) * 12.5% + 2.5%); width: 7.5%; background: linear-gradient(180deg, transparent, color-mix(in oklab, var(--gc, #fff) 40%, white), transparent); }
@keyframes m3-beam { 0% { opacity: 0; transform: scale(0.2, 1); } 20% { opacity: 1; transform: scale(1); } 100% { opacity: 0; transform: scale(1.1, 0.4); } }
.m3-beam.v { animation-name: m3-beam-v; }
@keyframes m3-beam-v { 0% { opacity: 0; transform: scale(1, 0.2); } 20% { opacity: 1; transform: scale(1); } 100% { opacity: 0; transform: scale(0.4, 1.1); } }
.m3-burst {
  position: absolute;
  left: var(--x);
  top: var(--y);
  width: var(--d, 37.5%);
  aspect-ratio: 1;
  border-radius: 50%;
  border: 3px solid color-mix(in oklab, var(--gc, #fff) 50%, white);
  box-shadow: 0 0 24px color-mix(in oklab, var(--gc, #fff) 80%, white), inset 0 0 24px color-mix(in oklab, var(--gc, #fff) 60%, transparent);
  animation: m3-burst 0.5s ease-out forwards;
}
@keyframes m3-burst { from { transform: translate(-50%, -50%) scale(0.2); opacity: 1; } to { transform: translate(-50%, -50%) scale(1.15); opacity: 0; } }
.m3-flash { position: absolute; inset: 0; background: radial-gradient(circle, #fff, rgba(255, 255, 255, 0.4)); animation: m3-flash 0.55s ease-out forwards; }
@keyframes m3-flash { from { opacity: 0.85; } to { opacity: 0; } }
.m3-combo {
  position: absolute;
  left: var(--x);
  top: var(--y);
  transform: translate(-50%, -50%);
  font-family: var(--serif);
  font-style: italic;
  font-weight: 600;
  font-size: calc(var(--cell) * var(--k, 0.5));
  color: #fff;
  white-space: nowrap;
  text-shadow: 0 0 12px var(--accent), 0 0 24px var(--accent), 0 2px 2px rgba(0, 0, 0, 0.4);
  animation: m3-combo 1.1s var(--ease) forwards;
}
@keyframes m3-combo {
  0% { transform: translate(-50%, -50%) scale(0.5); opacity: 0; }
  20% { transform: translate(-50%, -55%) scale(1.15); opacity: 1; }
  100% { transform: translate(-50%, -110%) scale(1); opacity: 0; }
}
.m3-board.shuffling .m3-piece { --dur: 0.55s; }

/* -------------------------------------------------------- sheets, dialogs */
.backdrop { position: fixed; inset: 0; z-index: 20; background: var(--backdrop); backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px); animation: fade 0.25s; }
@keyframes fade { from { opacity: 0; } }

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  margin: 0 auto;
  z-index: 21;
  max-width: 560px;
  max-height: 86dvh;
  display: flex;
  flex-direction: column;
  background: var(--paper);
  color: var(--ink);
  border-radius: 24px 24px 0 0;
  box-shadow: 0 -10px 40px var(--shadow-strong);
  padding-bottom: env(safe-area-inset-bottom);
  animation: rise 0.32s var(--ease);
}
@keyframes rise { from { transform: translateY(40px); opacity: 0; } }
.sheet header { display: flex; align-items: center; justify-content: space-between; padding: 14px 12px 6px 22px; flex: 0 0 auto; }
.sheet h2, .dialog h2 { font-family: var(--serif); font-weight: 600; font-size: 24px; margin: 0; letter-spacing: 0.01em; }
.sheet-body { overflow: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; padding: 2px 22px 26px; font-size: 15px; line-height: 1.55; }
.sheet-body h3 { font-family: var(--serif); font-weight: 600; font-size: 17px; margin: 20px 0 8px; color: var(--ink); }
.prose p { margin: 0 0 12px; }
.prose ul { margin: 0; padding-left: 20px; }
.prose li { margin: 5px 0; }
.muted { color: var(--ink-soft); }
.about { font-size: 13px; margin-top: 22px; text-align: center; }
.legend { display: flex; align-items: flex-start; gap: 12px; margin: 8px 0; }
.legend svg { width: 40px; height: 40px; flex: 0 0 auto; overflow: visible; }
.legend span { flex: 1; }

@media (min-width: 700px) and (min-height: 560px) {
  .sheet { bottom: auto; top: 50%; transform: translateY(-50%); border-radius: 24px; max-height: 80dvh; animation: pop 0.28s var(--ease); }
}
@keyframes pop { from { opacity: 0; transform: translateY(-50%) scale(0.96); } }

.dialog {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  z-index: 22;
  width: min(92vw, 400px);
  max-height: 94dvh;
  overflow-x: hidden;
  overflow-y: auto;
  background: var(--paper);
  color: var(--ink);
  border-radius: 24px;
  padding: 26px 24px 22px;
  box-shadow: 0 20px 60px var(--shadow-strong);
  text-align: center;
  animation: popc 0.3s var(--ease);
}
@keyframes popc { from { opacity: 0; transform: translate(-50%, -50%) scale(0.94); } }
.dialog p { margin: 8px 0 18px; color: var(--ink-soft); font-size: 15px; line-height: 1.5; }
.dialog .row { display: flex; gap: 10px; }
.result .glow { position: absolute; inset: -40% -20% auto; height: 70%; background: radial-gradient(ellipse at 50% 40%, color-mix(in oklab, var(--accent) 35%, transparent), transparent 65%); pointer-events: none; }
.result h2 { position: relative; font-size: 30px; }
.result[data-stars="3"] h2 { animation: shimmer 2.4s ease-in-out infinite; }
@keyframes shimmer { 50% { text-shadow: 0 0 18px color-mix(in oklab, var(--star) 80%, white); } }
.result-stars { position: relative; display: flex; justify-content: center; gap: 6px; margin-bottom: 4px; }
.result-stars .star { position: static; transform: none; width: 40px; height: 40px; }
.result-stars .star:nth-child(2) { width: 50px; height: 50px; margin-top: -6px; }
.result-stars .star.lit { animation: m3-starin 0.5s var(--ease) both; animation-delay: var(--d, 0s); }
@keyframes m3-starin { 0% { transform: scale(0) rotate(-30deg); } 60% { transform: scale(1.3) rotate(6deg); } 100% { transform: scale(1); } }
.result-score { display: flex; flex-direction: column; align-items: center; margin: 2px 0 14px; position: relative; }
.result-score span { font-size: 11px; letter-spacing: 0.1em; text-transform: uppercase; color: var(--ink-soft); }
.result-score b { font-family: var(--serif); font-size: 46px; font-weight: 600; line-height: 1.05; font-variant-numeric: tabular-nums; }
.result-score em { font-style: normal; font-size: 12.5px; font-weight: 600; color: var(--accent); margin-top: 2px; }
.result-stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-bottom: 18px; }
.result-stats div, .summary div { display: flex; flex-direction: column; align-items: center; background: var(--paper-2); border-radius: 12px; padding: 8px 4px; line-height: 1.2; }
.result-stats span, .summary span { font-size: 10px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--ink-soft); text-align: center; }
.result-stats b, .summary b { font-family: var(--serif); font-size: 19px; font-weight: 600; }

/* ------------------------------------------------------------- results */
.summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 8px; }
.zen-line { display: flex; justify-content: space-between; align-items: center; margin: 10px 2px 0; font-size: 14px; color: var(--ink-soft); }
.zen-line b { font-family: var(--serif); font-size: 18px; color: var(--ink); }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; font-variant-numeric: tabular-nums; }
th { text-align: left; vertical-align: bottom; font-weight: 500; color: var(--ink-soft); font-size: 11px; letter-spacing: 0.06em; text-transform: uppercase; padding: 4px; border-bottom: 1px solid var(--line); }
td { padding: 7px 4px; border-bottom: 1px solid var(--line); white-space: nowrap; vertical-align: middle; }
th.combo, th.hints, th.time, th.score, td.combo, td.hints, td.time, td.score { text-align: right; }
td.score { font-family: var(--serif); font-weight: 600; font-size: 15px; }
td.stars { color: var(--star); letter-spacing: -1px; font-size: 15px; }
td.stars.none { color: var(--star-off); }
@media (max-width: 420px) { th.hints, td.hints { display: none; } td { padding: 7px 3px; } }

/* ------------------------------------------------------------- settings */
.opts { display: flex; flex-wrap: wrap; gap: 8px; }
.opt {
  appearance: none;
  border: 1.5px solid var(--line);
  background: var(--paper-2);
  color: var(--ink);
  font: 500 14px var(--sans);
  padding: 8px 14px 8px 10px;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  gap: 9px;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s, transform 0.15s, box-shadow 0.2s;
}
.opt:active { transform: scale(0.97); }
.opt[aria-checked="true"] { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in oklab, var(--accent) 30%, transparent); }
.opts.lang .opt { padding-left: 14px; min-width: 96px; justify-content: center; }
.swatch { display: inline-block; width: 26px; height: 26px; border-radius: 50%; flex: 0 0 auto; background: var(--sw0); box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.15); position: relative; overflow: hidden; }
.swatch::before, .swatch::after { content: ""; position: absolute; width: 11px; height: 11px; border-radius: 50%; }
.swatch::before { left: 4px; top: 6px; background: radial-gradient(circle at 35% 30%, #fff, var(--sw1) 60%); }
.swatch::after { left: 12px; top: 10px; background: radial-gradient(circle at 35% 30%, #fff, var(--sw2) 60%); }
.toggle { display: flex; align-items: center; gap: 12px; margin-top: 22px; cursor: pointer; font-size: 15px; line-height: 1.35; }
.toggle input { position: absolute; opacity: 0; width: 0; height: 0; }
.toggle .track { flex: 0 0 auto; width: 46px; height: 28px; border-radius: 999px; background: var(--track); position: relative; transition: background 0.25s; box-shadow: inset 0 0 0 1px var(--line); }
.toggle .track::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3); transition: transform 0.25s var(--ease); }
.toggle input:checked + .track { background: var(--accent); }
.toggle input:checked + .track::after { transform: translateX(18px); }
.toggle input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------------------------------------------------------------- toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(84px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(10px);
  z-index: 15;
  background: var(--toast-bg);
  color: var(--toast-ink);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 14px;
  max-width: min(88vw, 420px);
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s, transform 0.25s;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ------------------------------------------------------- small screens */
@media (max-height: 640px) {
  .top { padding-top: 4px; }
  .brand small { display: none; }
  .brand b { font-size: 19px; }
  .brand img { width: 32px; height: 32px; }
  .stat { padding: 3px 6px 2px; }
  .stat b, .stat.main b { font-size: 19px; }
  .starbar { height: 20px; margin-top: 2px; }
  .actions { padding-bottom: 8px; padding-top: 2px; }
  .btn, .seg-btn { padding: 7px 10px; font-size: 14px; }
  .stage { padding: 4px 14px; }
}
@media (max-width: 400px) {
  .btn span { white-space: nowrap; }
}
@media (min-width: 900px) and (min-height: 700px) {
  .brand b { font-size: 26px; }
  .stage { padding: 14px 16px; }
}

@media (prefers-reduced-motion: reduce) {
  .bg .blob { animation: none; }
  .m3-piece { transition-duration: 0.12s !important; transition-delay: 0s !important; }
  .m3-piece.sp svg, .m3-piece.tc svg, .cb-ring, .m3-piece.hint svg { animation: none; }
  .m3-piece.hint::after { opacity: 1; }
  .m3-spark, .m3-beam, .m3-burst { animation-duration: 0.25s; }
  .sheet, .dialog, .backdrop { animation: none; }
  .starbar-fill { transition: none; }
}
