:root {
  --bg: #13203b;
  --bg2: #1b2a4a;
  --ink: #f4f1ea;
  --muted: #a9b4cc;
  --accent: #ffcc4d;
  --good: #4fd18b;
  --bad: #ff6b6b;
  --blue: #5aa9ff;
  --card: #fdfaf3;
  --cardInk: #1d2433;
  --radius: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

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

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
}

#app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
}

/* ---------- top bar ---------- */
#topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(8px + var(--safe-t)) 12px 8px;
  background: var(--bg2);
  border-bottom: 1px solid #ffffff14;
  z-index: 5;
}
#titleWrap { flex: 1; min-width: 0; }
#chapter { font-size: 11px; letter-spacing: .08em; text-transform: uppercase; color: var(--accent); opacity: .9; }
#title { font-size: 18px; font-weight: 800; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#hudStars { font-size: 18px; letter-spacing: 2px; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px; border: 0;
  background: #ffffff14; color: var(--ink); font-size: 28px; line-height: 1;
  cursor: pointer;
}
.icon-btn:disabled { visibility: hidden; }

/* ---------- stage ---------- */
#stage { position: relative; flex: 1; min-height: 0; }
#stage canvas { position: absolute; inset: 0; width: 100% !important; height: 100% !important; display: block; touch-action: none; }

#hint {
  position: absolute; left: 50%; top: 10px; transform: translateX(-50%);
  background: #0b1428cc; color: var(--ink); padding: 6px 12px; border-radius: 999px;
  font-size: 13px; pointer-events: none; white-space: nowrap; max-width: 92%;
  overflow: hidden; text-overflow: ellipsis; backdrop-filter: blur(6px);
}

/* ---------- bottom panel ---------- */
#panel {
  background: var(--bg2);
  border-top: 1px solid #ffffff14;
  padding: 10px 14px calc(12px + var(--safe-b));
  z-index: 5;
}
#task { font-size: 15px; line-height: 1.4; min-height: 1.4em; }
#task b { color: var(--accent); }
#task .good { color: var(--good); font-weight: 700; }
#task .bad { color: var(--bad); font-weight: 700; }
#task .small { font-size: 12.5px; color: var(--muted); }
#actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
#actions:empty { display: none; }

.btn {
  flex: 1 1 auto;
  min-height: 44px;
  padding: 10px 14px;
  border: 0;
  border-radius: 12px;
  font: inherit; font-weight: 700; font-size: 15px;
  background: #ffffff1c; color: var(--ink);
  cursor: pointer;
  transition: transform .08s ease, background .2s;
}
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--accent); color: #2a1f00; }
.btn.good { background: var(--good); color: #052b16; }
.btn.bad { background: var(--bad); color: #2b0505; }
.btn.ghost { background: transparent; border: 1px solid #ffffff33; }
.btn:disabled { opacity: .4; }

/* ---------- meters (paint mixer etc) ---------- */
.meters { display: flex; gap: 8px; margin-top: 8px; }
.meter { flex: 1; font-size: 12px; color: var(--muted); }
.meter .bar { height: 10px; border-radius: 6px; background: #ffffff14; overflow: hidden; position: relative; margin-top: 3px; }
.meter .fill { position: absolute; top: 0; bottom: 0; left: 0; border-radius: 6px; }
.meter.neg .fill { background: repeating-linear-gradient(45deg, var(--bad), var(--bad) 4px, #a33 4px, #a33 8px) !important; }
.swatches { display: flex; gap: 10px; align-items: center; margin-top: 4px; }
.swatch { width: 38px; height: 38px; border-radius: 10px; border: 2px solid #ffffff55; flex: none; }
.swatch.neg { background: repeating-linear-gradient(45deg, #555, #555 5px, #333 5px, #333 10px) !important; }

/* ---------- menu ---------- */
#menu {
  position: absolute; inset: 0; overflow-y: auto; padding: 18px 14px 28px;
  background: linear-gradient(180deg, #13203bcc 0%, #13203b55 40%, #13203bee 100%);
  -webkit-overflow-scrolling: touch;
}
.hero { text-align: center; margin: 4px 0 18px; }
.hero h1 { margin: 0; font-size: 30px; font-weight: 900; letter-spacing: -.01em; }
.hero h1 span { color: var(--accent); }
.hero p { margin: 6px auto 0; max-width: 460px; color: var(--muted); font-size: 14px; line-height: 1.45; }
.chapter-head { margin: 18px 4px 8px; font-size: 12px; letter-spacing: .1em; text-transform: uppercase; color: var(--accent); }
.levels { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 10px; max-width: 900px; margin: 0 auto; }
.level-card {
  display: flex; gap: 12px; align-items: center; text-align: left;
  padding: 12px; border-radius: var(--radius); border: 1px solid #ffffff1a;
  background: #1b2a4ae6; color: var(--ink); font: inherit; cursor: pointer;
  backdrop-filter: blur(6px);
  transition: transform .1s ease, border-color .2s;
}
.level-card:active { transform: scale(.98); }
.level-card:hover { border-color: #ffcc4d88; }
.level-card .emoji { font-size: 34px; width: 52px; height: 52px; display: grid; place-items: center; background: #ffffff10; border-radius: 14px; flex: none; }
.level-card .meta { flex: 1; min-width: 0; }
.level-card .name { font-weight: 800; font-size: 16px; }
.level-card .blurb { font-size: 12.5px; color: var(--muted); margin-top: 2px; line-height: 1.35; }
.level-card .stars { font-size: 14px; margin-top: 4px; letter-spacing: 1px; }
.menu-foot { text-align: center; font-size: 12px; color: var(--muted); margin-top: 22px; line-height: 1.5; }
.menu-foot a { color: var(--blue); }

/* ---------- modal ---------- */
#modal {
  position: absolute; inset: 0; z-index: 20;
  display: grid; place-items: center; padding: 16px;
  background: #0a1122b3; backdrop-filter: blur(3px);
}
.card {
  width: min(520px, 100%); max-height: calc(100% - 20px); overflow-y: auto;
  background: var(--card); color: var(--cardInk);
  border-radius: 22px; padding: 22px 20px 18px;
  box-shadow: 0 20px 60px #0008;
  animation: pop .25s ease-out;
}
@keyframes pop { from { transform: scale(.94); opacity: 0; } to { transform: none; opacity: 1; } }
.card .big-emoji { font-size: 48px; text-align: center; }
.card h2 { margin: 4px 0 2px; text-align: center; font-size: 24px; font-weight: 900; }
.card .sub { text-align: center; color: #6a7080; font-size: 12px; letter-spacing: .08em; text-transform: uppercase; }
.card p { font-size: 15.5px; line-height: 1.5; margin: 12px 0; }
.card .analogy { background: #fff3cf; border-radius: 14px; padding: 10px 12px; font-size: 15px; line-height: 1.45; }
.card .goal { background: #e3f1ff; border-radius: 14px; padding: 10px 12px; font-size: 14.5px; line-height: 1.45; margin-top: 10px; }
.card details { margin-top: 12px; background: #eef0f5; border-radius: 14px; padding: 8px 12px; }
.card summary { cursor: pointer; font-weight: 700; font-size: 14px; }
.card .math { font-family: "STIX Two Math", "Cambria Math", Georgia, serif; font-size: 15.5px; line-height: 1.6; }
.card .math .src { font-family: inherit; font-size: 12px; color: #6a7080; }
.card .btn { width: 100%; margin-top: 14px; }
.card .row { display: flex; gap: 8px; }
.card .row .btn { margin-top: 14px; }
.card .btn:not(.primary) { background: #e6e8ee; color: var(--cardInk); }
.card .result-stars { text-align: center; font-size: 44px; letter-spacing: 6px; margin: 6px 0; }

/* ---------- toast ---------- */
#toast {
  position: absolute; left: 50%; bottom: 140px; transform: translate(-50%, 20px);
  background: #0b1428ee; color: var(--ink); padding: 10px 16px; border-radius: 14px;
  font-size: 15px; font-weight: 700; opacity: 0; pointer-events: none; z-index: 30;
  transition: opacity .2s, transform .2s; max-width: 90%; text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.good { background: #127a46f0; }
#toast.bad { background: #a12f2ff0; }

.hidden { display: none !important; }

@media (min-width: 800px) {
  #task { font-size: 16px; }
  #panel { padding-left: max(14px, calc(50% - 420px)); padding-right: max(14px, calc(50% - 420px)); }
}
