/* ChemAI games: shared shell (top bar, stage, overlays, leaderboard). */
:root {
  --bg: #f6f3ea; --card: #fffdf7; --fg: #1f2a24; --muted: #5d675f; --line: #e3ddcc;
  --accent: #1f6f5c; --accent-fg: #ffffff; --warn: #b4442c; --gold: #c99a1a;
  --stage: #0f1d2b;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) { --bg: #121815; --card: #1a221e; --fg: #e8eee9; --muted: #a2ada5; --line: #2a3530; --accent: #5fc3a6; --accent-fg: #0c1512; --warn: #ec7a5f; --gold: #e3bb48; color-scheme: dark; }
}
:root[data-theme="dark"] { --bg: #121815; --card: #1a221e; --fg: #e8eee9; --muted: #a2ada5; --line: #2a3530; --accent: #5fc3a6; --accent-fg: #0c1512; --warn: #ec7a5f; --gold: #e3bb48; color-scheme: dark; }

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--fg);
  font: 15px/1.5 "Nunito", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
a { color: var(--accent); }
button { font: inherit; cursor: pointer; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- game page ---------- */
.cg-page { display: flex; flex-direction: column; height: 100dvh; min-height: 100vh; }
.cg-bar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 8px max(12px, env(safe-area-inset-right, 0px)) 8px max(12px, env(safe-area-inset-left, 0px));
  padding-top: max(8px, env(safe-area-inset-top, 0px));
  border-bottom: 1px solid var(--line); background: var(--card);
}
.cg-back { text-decoration: none; font-weight: 700; white-space: nowrap; }
.cg-title { font-weight: 800; font-size: 17px; margin: 0; flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cg-hud { display: flex; gap: 8px; flex-wrap: wrap; }
.cg-pill {
  border: 1px solid var(--line); border-radius: 999px; padding: 2px 10px; font-weight: 700;
  font-variant-numeric: tabular-nums; background: var(--bg); white-space: nowrap;
}
.cg-pill b { color: var(--accent); }
.cg-icon { border: 1px solid var(--line); background: var(--bg); color: var(--fg); border-radius: 10px; width: 36px; height: 32px; }
.cg-stage {
  position: relative; flex: 1 1 auto; min-height: 0; background: var(--stage);
  display: flex; align-items: center; justify-content: center; overflow: hidden;
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
.cg-stage canvas { display: block; touch-action: none; user-select: none; -webkit-user-select: none; }

/* ---------- overlays ---------- */
.cg-over {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  padding: 16px; background: rgba(8, 16, 24, .62); backdrop-filter: blur(3px); z-index: 5; overflow-y: auto;
}
.cg-card {
  background: var(--card); color: var(--fg); border-radius: 18px; border: 1px solid var(--line);
  width: min(560px, 100%); padding: 22px clamp(16px, 4vw, 28px); box-shadow: 0 20px 60px rgba(0,0,0,.35);
  max-height: 100%; overflow-y: auto;
}
.cg-card h2 { margin: 0 0 6px; font-size: 26px; line-height: 1.15; text-wrap: balance; }
.cg-card p { margin: 6px 0; }
.cg-card ol, .cg-card ul { margin: 8px 0 12px; padding-left: 20px; }
.cg-card li { margin: 3px 0; }
.cg-row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 14px; align-items: center; }
.cg-btn {
  border: 0; border-radius: 12px; padding: 11px 20px; font-weight: 800; font-size: 16px;
  background: var(--accent); color: var(--accent-fg); box-shadow: 0 3px 0 rgba(0,0,0,.25);
}
.cg-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(0,0,0,.25); }
.cg-btn.ghost { background: transparent; color: var(--fg); border: 1px solid var(--line); box-shadow: none; }
.cg-btn:focus-visible, .cg-icon:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }
.cg-big { font-size: 46px; font-weight: 900; color: var(--accent); line-height: 1; font-variant-numeric: tabular-nums; }
.cg-record { color: var(--gold); font-weight: 800; }
.cg-muted { color: var(--muted); font-size: 13px; }
.cg-learn { border-left: 3px solid var(--accent); padding: 6px 10px; background: var(--bg); border-radius: 0 10px 10px 0; margin: 10px 0; }
.cg-chips { display: flex; gap: 6px; flex-wrap: wrap; margin: 8px 0; }
.cg-chip { border: 1px solid var(--line); border-radius: 999px; padding: 4px 12px; background: var(--bg); color: var(--fg); font-weight: 700; }
.cg-chip[aria-pressed="true"] { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }

/* ---------- leaderboard ---------- */
.cg-board { margin-top: 12px; }
.cg-board h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 12px 0 4px; }
.cg-board ol { list-style: none; padding: 0; margin: 0; counter-reset: r; }
.cg-board li { display: flex; justify-content: space-between; gap: 10px; padding: 4px 8px; border-radius: 8px; counter-increment: r; }
.cg-board li::before { content: counter(r) "."; width: 26px; color: var(--muted); font-variant-numeric: tabular-nums; }
.cg-board li span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cg-board li b { font-variant-numeric: tabular-nums; }
.cg-board li.me { background: color-mix(in srgb, var(--accent) 16%, transparent); font-weight: 800; }

/* ---------- toast ---------- */
.cg-toast {
  position: absolute; left: 50%; top: 14px; transform: translateX(-50%); z-index: 4; pointer-events: none;
  background: rgba(10, 20, 30, .86); color: #fff; padding: 8px 16px; border-radius: 12px; font-weight: 700;
  max-width: calc(100% - 32px); text-align: center; transition: opacity .25s; opacity: 0;
}
.cg-toast.on { opacity: 1; }
.cg-toast.good { background: rgba(20, 110, 80, .92); }
.cg-toast.bad { background: rgba(160, 50, 30, .92); }
.cg-rotate { display: none; }
@media (orientation: portrait) and (max-width: 600px) {
  .cg-rotate { display: block; position: absolute; bottom: 14px; left: 50%; transform: translateX(-50%); color: #fff; background: rgba(0,0,0,.45); padding: 6px 12px; border-radius: 999px; font-size: 13px; font-weight: 700; z-index: 3; pointer-events: none; white-space: nowrap; }
}

/* Phones held sideways: slim top bar so the game gets the height. */
@media (max-height: 520px) {
  .cg-bar { padding-top: max(3px, env(safe-area-inset-top, 0px)); padding-bottom: 3px; gap: 6px; }
  .cg-title { font-size: 14px; }
  .cg-back { font-size: 13px; }
  .cg-pill { padding: 0 8px; font-size: 12px; }
  .cg-icon { width: 30px; height: 26px; font-size: 13px; }
  .cg-card { padding: 14px 18px; }
  .cg-card h2 { font-size: 20px; }
  .cg-card ol { margin: 4px 0 6px; }
  .cg-big { font-size: 34px; }
}
:fullscreen .cg-page, .cg-page:fullscreen { height: 100vh; }

/* ---------- effects ---------- */
.cg-fxlayer { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 4; }
.cg-dom { will-change: transform; }
.cg-over:not([hidden]) .cg-card { animation: cg-in .38s cubic-bezier(.2,1.3,.4,1) both; }
@keyframes cg-in { from { opacity: 0; transform: translateY(18px) scale(.94); } to { opacity: 1; transform: none; } }
.cg-a-bump { animation: cg-bump .35s ease-out; }
@keyframes cg-bump { 0% { transform: scale(1); } 40% { transform: scale(1.18); background: color-mix(in srgb, var(--gold) 30%, var(--bg)); } 100% { transform: scale(1); } }
.cg-a-pop { animation: cg-pop .45s cubic-bezier(.2,1.6,.4,1); }
@keyframes cg-pop { 0% { transform: scale(.85); } 60% { transform: scale(1.12); } 100% { transform: scale(1); } }
.cg-a-shake { animation: cg-shake .42s ease-in-out; }
@keyframes cg-shake { 0%,100% { transform: translateX(0); } 20% { transform: translateX(-9px); } 40% { transform: translateX(8px); } 60% { transform: translateX(-6px); } 80% { transform: translateX(4px); } }
.cg-a-glow { animation: cg-glow .9s ease-out; }
@keyframes cg-glow { 0% { box-shadow: 0 0 0 0 rgba(95,195,166,.0); } 30% { box-shadow: 0 0 0 6px rgba(95,195,166,.55), 0 0 28px 6px rgba(95,195,166,.45); } 100% { box-shadow: 0 0 0 0 rgba(95,195,166,0); } }
.cg-a-bad { animation: cg-bad .6s ease-out, cg-shake .42s ease-in-out; }
@keyframes cg-bad { 0% { box-shadow: 0 0 0 0 rgba(220,70,40,0); } 30% { box-shadow: 0 0 0 5px rgba(220,70,40,.55), 0 0 24px 4px rgba(220,70,40,.4); } 100% { box-shadow: 0 0 0 0 rgba(220,70,40,0); } }
.cg-pill { transition: background .3s; }
.cg-btn { position: relative; overflow: hidden; }
.cg-btn:not(.ghost)::after { content: ""; position: absolute; inset: 0; background: linear-gradient(110deg, transparent 30%, rgba(255,255,255,.35) 50%, transparent 70%); transform: translateX(-120%); animation: cg-shine 3.2s ease-in-out infinite 1s; pointer-events: none; }
@keyframes cg-shine { 0%, 70% { transform: translateX(-120%); } 100% { transform: translateX(120%); } }
@media (prefers-reduced-motion: reduce) { .cg-over:not([hidden]) .cg-card, .cg-btn::after { animation: none !important; } }

/* ---------- combo meter, stars, skins ---------- */
.cg-combo { font-weight: 900; border-radius: 999px; padding: 2px 10px; color: #fff; background: linear-gradient(90deg, #f0a020, #ff5a3a); box-shadow: 0 0 12px rgba(255,120,40,.5); white-space: nowrap; font-variant-numeric: tabular-nums; }
.cg-combo[data-level="3"] { background: linear-gradient(90deg, #ff5a3a, #e0307a); }
.cg-combo[data-level="4"] { background: linear-gradient(90deg, #e0307a, #8a4fd6); box-shadow: 0 0 18px rgba(200,60,200,.6); }
.cg-combo:not([hidden]) { animation: cg-flicker 1.2s ease-in-out infinite; }
@keyframes cg-flicker { 0%,100% { filter: brightness(1); } 50% { filter: brightness(1.18); } }
[data-music][aria-pressed="false"] { opacity: .45; text-decoration: line-through; }
.cg-stars { display: inline-flex; gap: 2px; color: var(--line); font-size: 16px; vertical-align: -1px; }
.cg-stars i { font-style: normal; }
.cg-stars i.on { color: var(--gold); }
.cg-stars.big { display: flex; justify-content: center; gap: 10px; font-size: 44px; margin: 4px 0 2px; }
.cg-stars.big i { color: color-mix(in srgb, var(--line) 80%, transparent); transform: scale(.9); }
.cg-stars.big i.on { color: var(--gold); animation: cg-star .55s cubic-bezier(.2,1.8,.4,1) both; animation-delay: var(--d); text-shadow: 0 0 18px rgba(255,200,60,.6); }
@keyframes cg-star { 0% { transform: scale(0) rotate(-40deg); opacity: 0; } 100% { transform: scale(1) rotate(0); opacity: 1; } }
.cg-skin i { display: inline-block; width: 12px; height: 12px; border-radius: 50%; margin-right: 6px; vertical-align: -1px; border: 1px solid rgba(0,0,0,.2); }
.cg-skin:disabled { opacity: .5; cursor: not-allowed; }
@media (prefers-reduced-motion: reduce) { .cg-combo, .cg-stars.big i.on { animation: none !important; } }

/* ---------- Prof. AKAY mascot ---------- */
.cg-akay { position: absolute; right: max(10px, env(safe-area-inset-right, 0px)); bottom: max(10px, env(safe-area-inset-bottom, 0px)); z-index: 4; display: flex; align-items: flex-end; gap: 6px; pointer-events: none; max-width: min(360px, 80%); }
.cg-akay-fig svg { display: block; filter: drop-shadow(0 4px 8px rgba(0,0,0,.25)); }
.cg-say { background: #fffdf7; color: #1f2a24; border-radius: 14px 14px 4px 14px; padding: 8px 12px; font-weight: 800; font-size: 15px; box-shadow: 0 6px 18px rgba(0,0,0,.25); margin-bottom: 40px; }
.cg-a-in { animation: cg-akay-in .45s cubic-bezier(.2,1.5,.4,1) both; }
@keyframes cg-akay-in { from { transform: translateY(30px) scale(.7); opacity: 0; } to { transform: none; opacity: 1; } }
.cg-head { display: flex; align-items: center; gap: 10px; }
.cg-head svg { flex: none; }
.cg-head.center { justify-content: center; margin-bottom: -6px; }
.cg-head .akay-wave { animation: cg-wave 2.4s ease-in-out infinite; transform-origin: 50% 90%; }
@keyframes cg-wave { 0%, 100% { transform: rotate(0); } 10% { transform: rotate(-6deg); } 20% { transform: rotate(5deg); } 30% { transform: rotate(0); } }
@media (max-height: 520px) { .cg-head svg { width: 44px; height: 44px; } .cg-say { font-size: 13px; } }
@media (prefers-reduced-motion: reduce) { .cg-head .akay-wave { animation: none; } }
