:root {
  --bg: #FFF4E0;
  --paper: #FFFBF3;
  --ink: #3D2F86;
  --muted: #8A82AD;
  --violet: #7C5CFF;
  --violet-soft: #ECE6FF;
  --green: #22C55E;
  --orange: #FF8A3D;
  --card: #FFFFFF;
  --shadow: 0 3px 0 rgba(61, 47, 134, 0.14), 0 6px 18px rgba(61, 47, 134, 0.1);
  --bar: clamp(60px, 9vmin, 78px);
  --btn: clamp(48px, 7.4vmin, 62px);
  --font: ui-rounded, "SF Pro Rounded", "Nunito", "Varela Round", "Arial Rounded MT Bold", system-ui, sans-serif;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--bg); color: var(--ink);
  font-family: var(--font);
  overscroll-behavior: none;
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

button { font: inherit; color: inherit; touch-action: manipulation; cursor: pointer; }
svg { display: block; }
.hidden { display: none !important; }
.spacer { flex: 1; }

#app { position: fixed; inset: 0; }

.screen {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  padding: var(--safe-t) var(--safe-r) var(--safe-b) var(--safe-l);
}

/* ---------- bars & buttons ---------- */

.bar {
  flex: none; height: var(--bar);
  display: flex; align-items: center; gap: clamp(6px, 1.4vmin, 14px);
  padding: 0 clamp(8px, 2vmin, 20px);
}

.btn {
  border: none; background: var(--card); box-shadow: var(--shadow);
  transition: transform .08s ease, box-shadow .08s ease, background .15s;
}
.btn:active { transform: translateY(2px) scale(0.97); box-shadow: 0 1px 0 rgba(61,47,134,.14); }
.btn.round {
  width: var(--btn); height: var(--btn); border-radius: 50%;
  display: grid; place-items: center; flex: none;
}
.btn.round svg { width: 52%; height: 52%; }
.btn.round.big { width: calc(var(--btn) * 1.35); height: calc(var(--btn) * 1.35); }
.btn.ghost { background: transparent; box-shadow: none; color: var(--muted); }
.btn.primary { background: var(--violet); color: #fff; }
.btn.accent { background: var(--orange); color: #fff; }
.btn.accent svg { width: 46%; height: 46%; transform: translateX(6%); }
.btn.case { font-size: calc(var(--btn) * 0.42); font-weight: 800; color: var(--violet); }
.btn.pill { border-radius: 999px; padding: 12px 22px; font-weight: 700; font-size: 17px; }
.btn.danger { color: #E5484D; }

.logo { display: flex; align-items: center; gap: 10px; font-weight: 800; }
.logo-mark {
  width: var(--btn); height: var(--btn); border-radius: 30%;
  display: grid; place-items: center; color: #fff; font-size: calc(var(--btn) * .42);
  background: linear-gradient(135deg, #8E7CF0, #7C5CFF 55%, #5B3DF5);
  box-shadow: var(--shadow);
}
.logo-text { font-size: clamp(20px, 3.6vmin, 30px); letter-spacing: .2px; }
.title { font-weight: 800; font-size: clamp(18px, 3vmin, 26px); }

.seg {
  display: flex; background: var(--violet-soft); border-radius: 999px; padding: 4px; margin-left: 8px;
}
.seg button {
  border: none; background: transparent; border-radius: 999px;
  padding: 0 clamp(14px, 2.6vmin, 24px); height: calc(var(--btn) - 8px);
  font-weight: 800; font-size: clamp(18px, 3vmin, 24px); color: var(--muted);
}
.seg button.on { background: #fff; color: var(--violet); box-shadow: 0 2px 6px rgba(61,47,134,.15); }

/* ---------- home grid ---------- */

.grid-wrap { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; padding: 6px clamp(10px, 2.4vmin, 26px) 24px; }
.grid {
  display: grid; gap: clamp(10px, 1.8vmin, 18px);
  grid-template-columns: repeat(auto-fill, minmax(clamp(92px, 15vmin, 140px), 1fr));
}
.tile {
  position: relative; border: none; aspect-ratio: 1 / 1.06; border-radius: 22px;
  background: #fff; box-shadow: var(--shadow);
  display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 8px 6px 10px;
  border-bottom: 5px solid hsl(var(--h) 80% 86%);
  transition: transform .08s ease;
}
.tile:active { transform: scale(0.95); }
.tile .glyph { flex: 1; width: 100%; display: grid; place-items: center; min-height: 0; }
.tile .glyph svg { height: 100%; max-width: 90%; }
.tile .glyph svg polyline, .tile .glyph svg circle { stroke: hsl(var(--h) 62% 48%); }
.tile .glyph svg circle { fill: hsl(var(--h) 62% 48%); stroke: none; }
.tile .stars { display: flex; gap: 2px; height: 18px; }
.tile .stars i { width: 18px; height: 18px; color: #EDE9F6; }
.tile .stars i.on { color: #FFC845; }
.tile .emblem { position: absolute; top: 6px; right: 6px; width: clamp(22px, 3.6vmin, 30px); height: clamp(22px, 3.6vmin, 30px); color: var(--muted); }
.tile .emblem svg { width: 100%; height: 100%; }
.tile.r0 .emblem { opacity: .5; }
.tile.r5 .emblem { filter: drop-shadow(0 0 6px rgba(140, 210, 255, .9)); }

/* ---------- practice ---------- */

.lnav { display: flex; align-items: center; gap: 2px; }
.chip {
  height: calc(var(--btn) * 1.12); aspect-ratio: 1.15; border: none; border-radius: 18px;
  background: #fff; box-shadow: var(--shadow); padding: 2px 4px; display: grid; place-items: center;
}
.chip svg { height: 100%; }
.chip svg polyline { stroke: var(--violet); }
.chip svg circle { fill: var(--violet); }

.stage {
  position: relative; flex: 1; min-height: 0; margin: 0 clamp(6px, 1.4vmin, 16px);
  border-radius: 26px; overflow: hidden; background: var(--paper);
  box-shadow: inset 0 0 0 3px #F3E6CC, 0 4px 16px rgba(120, 90, 30, 0.08);
}
.stage canvas {
  position: absolute; inset: 0; display: block;
  touch-action: none; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}
.cv-fx { cursor: crosshair; }

.toast {
  position: absolute; left: 50%; top: 14px; transform: translate(-50%, -24px);
  background: #fff; border-radius: 999px; padding: 10px 20px; font-weight: 800; font-size: clamp(16px, 2.8vmin, 22px);
  box-shadow: var(--shadow); opacity: 0; pointer-events: none; transition: opacity .2s, transform .25s cubic-bezier(.3, 1.6, .5, 1);
  white-space: nowrap; color: var(--ink);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.start { color: #15803D; }
.toast.direction, .toast.order, .toast.shape, .toast.off { color: #E0522F; }
.toast.watch { color: var(--orange); }

.btn-done { position: absolute; right: 18px; bottom: 18px; }
.btn-done svg { width: 46% !important; height: 46% !important; }

.result {
  position: absolute; left: 50%; bottom: 16px; transform: translateX(-50%);
  background: rgba(255, 255, 255, 0.96); border-radius: 28px; box-shadow: var(--shadow);
  padding: 14px 26px 16px; display: flex; flex-direction: column; align-items: center; gap: 6px;
  animation: pop .35s cubic-bezier(.3, 1.6, .5, 1);
}
.rstars { display: flex; gap: 6px; }
.rs { width: clamp(40px, 7vmin, 60px); height: clamp(40px, 7vmin, 60px); color: #EDE9F6; }
.rs.on { color: #FFC845; animation: starIn .5s cubic-bezier(.3, 1.8, .5, 1) both; animation-delay: calc(var(--i) * 0.14s); }
.rs.on svg { filter: drop-shadow(0 2px 2px rgba(200, 140, 0, .35)); }
.rmsg { font-weight: 800; font-size: clamp(20px, 3.6vmin, 30px); }
.rnote { display: flex; align-items: center; gap: 8px; color: var(--muted); font-weight: 700; }
.ricon svg { width: 34px; height: 34px; }
.rbtns { display: flex; gap: 18px; margin-top: 6px; }

@media (min-aspect-ratio: 5/4) {
  .result { left: auto; right: 22px; bottom: 50%; transform: translateY(50%); animation-name: popSide; }
}
@media (max-aspect-ratio: 5/4) {
  .result { flex-direction: row; gap: 14px; padding: 10px 14px; bottom: 10px; border-radius: 999px; }
  .result .rmsg, .result .rnote { display: none; }
  .result .rbtns { margin: 0; gap: 10px; }
  .result .btn.round.big { width: var(--btn); height: var(--btn); }
  .rs { width: 34px; height: 34px; }
}
@keyframes popSide { from { transform: translateY(50%) scale(.6); opacity: 0; } }
@keyframes pop { from { transform: translateX(-50%) scale(.6); opacity: 0; } }
@keyframes starIn { from { transform: scale(0) rotate(-90deg); } }

/* assist level picker */
.assist { display: flex; gap: clamp(4px, 1vmin, 10px); background: #fff; padding: 5px; border-radius: 22px; box-shadow: var(--shadow); }
.lvl {
  border: none; background: transparent; border-radius: 17px; height: calc(var(--bar) - 14px);
  min-width: calc(var(--bar) - 6px); padding: 2px 6px; display: flex; flex-direction: column; align-items: center; justify-content: center;
  color: var(--muted); font-weight: 800; font-size: 11px; letter-spacing: .2px;
}
.lvl svg { width: calc(var(--bar) * 0.5); height: calc(var(--bar) * 0.5); }
.lvl.on { background: var(--violet-soft); color: var(--violet); box-shadow: inset 0 0 0 2.5px var(--violet); }

/* crayons */
.crayons { display: flex; gap: clamp(4px, 1vmin, 10px); align-items: center; }
.crayon {
  width: calc(var(--btn) * 0.72); height: calc(var(--btn) * 0.72); border-radius: 50%; border: 4px solid #fff;
  background: var(--c); box-shadow: var(--shadow); transition: transform .12s;
}
.crayon.on { transform: scale(1.18); box-shadow: 0 0 0 3px var(--c), var(--shadow); }

/* phones / narrow portrait: hide labels, shrink */
@media (max-width: 620px) {
  .lvl span { display: none; }
  .crayon:nth-child(n+4) { display: none; }
  .logo-text { display: none; }
  .lnav .btn.ghost { width: 36px; }
}
@media (max-width: 420px) {
  .crayons { display: none; }
}

/* ---------- settings dialog ---------- */

dialog {
  border: none; border-radius: 26px; padding: 0; width: min(520px, 92vw);
  box-shadow: 0 20px 60px rgba(40, 20, 100, .3); color: var(--ink);
}
dialog::backdrop { background: rgba(40, 25, 90, .35); }
dialog form { padding: 18px 22px 20px; display: flex; flex-direction: column; gap: 6px; }
.dlg-head { display: flex; align-items: center; justify-content: space-between; }
.dlg-head h2 { margin: 0; font-size: 24px; }
.tgl { display: flex; align-items: center; gap: 14px; padding: 9px 0; font-weight: 700; font-size: 17px; }
.tgl input { display: none; }
.tgl span {
  flex: none; width: 52px; height: 30px; border-radius: 999px; background: #E3DEF0; position: relative; transition: background .2s;
}
.tgl span::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,.2); transition: transform .2s;
}
.tgl input:checked + span { background: var(--green); }
.tgl input:checked + span::after { transform: translateX(22px); }
.help { color: var(--muted); font-size: 14px; line-height: 1.45; margin: 8px 0 4px; }
.dlg-foot { display: flex; justify-content: space-between; margin-top: 8px; }
