:root {
  --bg: #11132a;
  --bg2: #191c3d;
  --panel: rgba(255, 255, 255, 0.055);
  --panel-strong: rgba(255, 255, 255, 0.09);
  --line: rgba(255, 255, 255, 0.1);
  --line-strong: rgba(255, 255, 255, 0.22);
  --text: #eef0ff;
  --muted: #a6abd3;
  --faint: #6f74a3;
  --coral: #ef7c6e;
  --amber: #f2b544;
  --sage: #7fc08f;
  --sky: #5fa8e0;
  --violet: #a487ec;
  --danger: #ff8f8f;
  --radius: 14px;
  --cell-radius: 12px;
  --font: ui-rounded, "SF Pro Rounded", "Nunito", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(120% 80% at 50% -10%, #23275a 0%, var(--bg) 55%, #0b0c1e 100%);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
  touch-action: manipulation;
}
button { font: inherit; color: inherit; cursor: pointer; border: none; background: none; }
.hidden { display: none !important; }

/* soft drifting colour aura */
.aura { position: fixed; inset: 0; overflow: hidden; pointer-events: none; z-index: 0; }
.aura span {
  position: absolute; width: 60vmax; height: 60vmax; border-radius: 50%;
  filter: blur(70px); opacity: 0.16; animation: drift 38s ease-in-out infinite alternate;
}
.aura span:nth-child(1) { background: var(--violet); top: -20vmax; left: -15vmax; }
.aura span:nth-child(2) { background: var(--sky); bottom: -25vmax; right: -20vmax; animation-duration: 46s; }
.aura span:nth-child(3) { background: var(--coral); top: 40%; left: 50%; width: 35vmax; height: 35vmax; opacity: 0.09; animation-duration: 52s; }
@keyframes drift { from { transform: translate(0, 0) scale(1); } to { transform: translate(6vmax, 4vmax) scale(1.12); } }

.app {
  position: relative; z-index: 1;
  max-width: 480px; margin: 0 auto; min-height: 100%;
  padding: max(10px, env(safe-area-inset-top)) 14px max(14px, env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 10px;
}

/* top bar */
.topbar { display: flex; align-items: center; justify-content: space-between; height: 44px; }
.logo { margin: 0; font-size: 1.45rem; letter-spacing: -0.02em; font-weight: 800; }
.logo-chroma {
  background: linear-gradient(90deg, var(--coral), var(--amber), var(--sage), var(--sky), var(--violet));
  background-size: 200% 100%; -webkit-background-clip: text; background-clip: text; color: transparent;
  animation: shimmer 12s linear infinite;
}
.logo-logic { color: var(--text); font-weight: 600; opacity: 0.92; }
@keyframes shimmer { to { background-position: 200% 0; } }
.top-actions { display: flex; gap: 2px; }
.icon-btn { width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; transition: background .2s; }
.icon-btn:hover, .icon-btn:focus-visible { background: var(--panel); outline: none; }
.icon-btn svg, .chip svg, .hint-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn svg .fill { fill: currentColor; stroke: none; }
#btn-sound .snd-off { display: none; }
#btn-sound.muted .snd-on { display: none; }
#btn-sound.muted .snd-off { display: block; }

/* mode tabs */
.modebar { display: flex; background: var(--panel); border-radius: 12px; padding: 3px; gap: 3px; }
.mode-tab { flex: 1; height: 34px; border-radius: 9px; color: var(--muted); font-weight: 600; transition: all .2s; }
.mode-tab.active { background: var(--panel-strong); color: var(--text); box-shadow: 0 1px 0 rgba(255,255,255,.06) inset; }
.practice-bar { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.seg { display: flex; gap: 4px; flex-wrap: wrap; }
.seg button { padding: 6px 11px; border-radius: 999px; background: var(--panel); color: var(--muted); font-size: .86rem; font-weight: 600; }
.seg button.active { background: var(--text); color: var(--bg); }
.text-btn { color: var(--muted); font-weight: 600; padding: 8px 10px; border-radius: 10px; }
.text-btn:hover { color: var(--text); background: var(--panel); }

/* info bar */
.infobar { display: flex; align-items: center; justify-content: space-between; color: var(--muted); font-size: .9rem; min-height: 28px; }
.info-left { font-weight: 600; }
.info-timer { font-variant-numeric: tabular-nums; font-weight: 700; color: var(--text); font-size: 1.05rem; letter-spacing: .02em; }
.info-right { display: flex; gap: 10px; }
.chip { display: inline-flex; align-items: center; gap: 4px; font-variant-numeric: tabular-nums; }
.chip svg { width: 16px; height: 16px; }
.chip b { color: var(--text); font-weight: 700; }
.chip.bump { animation: bump .45s ease; }
@keyframes bump { 40% { transform: scale(1.3); color: var(--danger); } }

/* board */
.board-wrap { display: flex; justify-content: center; }
.board {
  --n: 5;
  width: min(92vw, 440px, calc(100svh - 430px));
  min-width: 236px;
  aspect-ratio: 1;
  display: grid; grid-template-columns: repeat(var(--n), 1fr); grid-template-rows: repeat(var(--n), 1fr); gap: 7px;
  padding: 9px; border-radius: 20px;
  background: rgba(255, 255, 255, 0.035);
  box-shadow: 0 0 0 1px var(--line) inset, 0 20px 60px rgba(0, 0, 0, 0.35);
}
.cell {
  position: relative; border-radius: var(--cell-radius);
  background: rgba(255, 255, 255, 0.06);
  box-shadow: 0 0 0 1px rgba(255,255,255,0.05) inset;
  display: grid; place-items: center; overflow: hidden;
  transition: background-color .28s ease, box-shadow .2s ease, transform .2s ease;
  outline: none;
}
.cell.peer { background: rgba(255, 255, 255, 0.09); }
.cell.selected { box-shadow: 0 0 0 2.5px #fff inset, 0 0 22px rgba(255,255,255,.18); transform: scale(1.03); z-index: 2; }
.cell.has-colour { background: var(--cc); box-shadow: 0 -10px 18px rgba(0,0,0,.12) inset, 0 6px 14px rgba(255,255,255,.12) inset; }
.cell.has-colour.peer { filter: brightness(1.06); }
.cell.has-colour.selected { box-shadow: 0 0 0 2.5px #fff inset, 0 0 24px var(--cc); }
.cell .shape { width: 46%; height: 46%; fill: rgba(255,255,255,0.94); filter: drop-shadow(0 2px 3px rgba(0,0,0,.18)); }
.cell:not(.has-colour) .shape { fill: #cfd3f6; }
.cell .lbl { position: absolute; top: 5px; left: 7px; font-size: .66rem; font-weight: 800; color: rgba(20,20,40,.55); display: none; }
body.labels .cell.has-colour .lbl { display: block; }
.cell.given::after {
  content: ""; position: absolute; right: 6px; bottom: 6px; width: 5px; height: 5px; border-radius: 50%;
  background: rgba(255,255,255,.75); box-shadow: 0 0 0 1px rgba(0,0,0,.1);
}
.cell:not(.has-colour).given::after { background: var(--faint); }
.cell.hinted::before { content: ""; position: absolute; inset: 0; border-radius: inherit; box-shadow: 0 0 0 2px rgba(242,181,68,.9) inset; pointer-events: none; }
.cell.pop .shape, .cell.pop-c { animation: pop .38s cubic-bezier(.3,1.6,.5,1); }
@keyframes pop { 0% { transform: scale(.6); opacity: .3; } 100% { transform: scale(1); opacity: 1; } }
.cell.shake { animation: shake .42s ease; }
@keyframes shake { 20% { transform: translateX(-5px); } 40% { transform: translateX(5px); } 60% { transform: translateX(-3px); } 80% { transform: translateX(3px); } }
.cell.wrong { background: rgba(255, 120, 120, 0.25) !important; }
.cell.hint-flash { animation: hintflash 1.4s ease; }
@keyframes hintflash { 0%, 100% { box-shadow: 0 0 0 0 rgba(242,181,68,0); } 30% { box-shadow: 0 0 0 4px rgba(242,181,68,.9) inset, 0 0 28px rgba(242,181,68,.6); } }
.board.won .cell { animation: wave .9s ease both; animation-delay: calc(var(--d) * 55ms); }
@keyframes wave { 0% { transform: scale(1); } 40% { transform: scale(1.1); filter: brightness(1.35); } 100% { transform: scale(1); } }

/* hint banner */
.hint-banner {
  min-height: 0; max-height: 0; overflow: hidden; opacity: 0;
  background: rgba(242, 181, 68, 0.1); border: 1px solid rgba(242,181,68,.25); border-radius: 12px;
  color: #f8e3b5; font-size: .9rem; line-height: 1.35; padding: 0 12px;
  transition: all .3s ease;
}
.hint-banner.show { max-height: 120px; opacity: 1; padding: 9px 12px; }
.hint-banner.error { background: rgba(255, 143, 143, 0.1); border-color: rgba(255,143,143,.3); color: #ffd0d0; }

/* palette */
.palette { display: flex; flex-direction: column; gap: 8px; }
.pal-row { display: grid; grid-template-columns: repeat(var(--n, 5), 1fr); gap: 8px; }
.pal-btn {
  height: 50px; border-radius: 14px; display: grid; place-items: center; position: relative;
  background: var(--panel); box-shadow: 0 0 0 1px var(--line) inset;
  transition: transform .12s ease, opacity .2s ease, background .2s;
}
.pal-btn:active { transform: scale(.94); }
.pal-btn .dot { width: 30px; height: 30px; border-radius: 50%; background: var(--cc); box-shadow: 0 4px 12px color-mix(in srgb, var(--cc) 45%, transparent), 0 -4px 8px rgba(0,0,0,.12) inset; }
.pal-btn .shape { width: 26px; height: 26px; fill: #dfe2ff; }
.pal-btn .k { position: absolute; right: 6px; bottom: 3px; font-size: .62rem; color: var(--faint); font-weight: 700; }
.pal-btn.dim { opacity: .3; }
.pal-btn.locked { opacity: .18; pointer-events: none; }
.pal-btn .lbl { position: absolute; font-size: .7rem; font-weight: 800; color: rgba(20,20,40,.6); display: none; }
body.labels .pal-btn .lbl { display: block; }

/* lower */
.lower { display: flex; gap: 12px; align-items: stretch; }
.tracker-wrap { background: var(--panel); border-radius: var(--radius); padding: 8px 10px 10px; box-shadow: 0 0 0 1px var(--line) inset; }
.tracker-title { font-size: .7rem; text-transform: uppercase; letter-spacing: .08em; color: var(--faint); margin-bottom: 6px; font-weight: 700; }
.tracker { display: grid; grid-template-columns: 16px repeat(var(--n), 16px); gap: 4px; align-items: center; }
.tracker .th { width: 16px; height: 16px; display: grid; place-items: center; }
.tracker .th .shape { width: 12px; height: 12px; fill: var(--muted); }
.tracker .th .sw { width: 11px; height: 11px; border-radius: 50%; background: var(--cc); }
.tracker .tp { width: 16px; height: 16px; border-radius: 5px; background: rgba(255,255,255,.06); transition: background .3s, transform .3s; }
.tracker .tp.on { background: var(--cc); transform: scale(1.02); box-shadow: 0 0 8px color-mix(in srgb, var(--cc) 50%, transparent); }
.tracker .tp.just { animation: pop .4s ease; }
.lower-actions { flex: 1; display: flex; flex-direction: column; gap: 8px; justify-content: center; }
.hint-btn {
  height: 52px; border-radius: 14px; display: flex; align-items: center; justify-content: center; gap: 8px;
  background: linear-gradient(135deg, rgba(242,181,68,.22), rgba(239,124,110,.18));
  box-shadow: 0 0 0 1px rgba(242,181,68,.3) inset; font-weight: 700; color: #fbe6bd;
  transition: transform .12s;
}
.hint-btn:active { transform: scale(.97); }
.hint-btn:disabled { opacity: .35; }
.kbd-hint { font-size: .72rem; color: var(--faint); text-align: center; }
@media (hover: none) { .kbd-hint, .pal-btn .k { display: none; } }

/* modals */
.modal-backdrop { position: fixed; inset: 0; background: rgba(5, 6, 18, 0.62); backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px); z-index: 20; animation: fade .25s ease; }
.modal {
  position: fixed; z-index: 21; left: 50%; top: 50%; transform: translate(-50%, -50%);
  width: min(92vw, 420px); max-height: 90svh; overflow: auto;
  background: linear-gradient(180deg, #20244e, #171a3a); border-radius: 22px;
  box-shadow: 0 0 0 1px var(--line-strong) inset, 0 30px 80px rgba(0,0,0,.5);
  padding: 22px 20px 20px; animation: rise .32s cubic-bezier(.2,1.2,.4,1);
}
.modal h2 { margin: 0 0 12px; font-size: 1.3rem; }
.modal-close { position: absolute; right: 10px; top: 8px; width: 38px; height: 38px; border-radius: 12px; font-size: 1.6rem; color: var(--muted); }
.modal-close:hover { background: var(--panel); }
@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { opacity: 0; transform: translate(-50%, -46%) scale(.97); } }
.primary-btn {
  height: 46px; padding: 0 22px; border-radius: 14px; font-weight: 800; color: #17183a;
  background: linear-gradient(90deg, var(--amber), var(--coral)); box-shadow: 0 8px 24px rgba(239,124,110,.3);
}
.primary-btn.wide { width: 100%; }
.primary-btn:active { transform: scale(.98); }

/* tutorial */
.tut { min-height: 250px; }
.tut-step { animation: fade .3s ease; }
.tut-step p { color: var(--muted); line-height: 1.5; margin: 12px 0 0; }
.tut-step p b { color: var(--text); }
.tut-illo { display: flex; justify-content: center; gap: 6px; padding: 14px 0 4px; flex-wrap: wrap; }
.tut-illo .cell { width: 48px; height: 48px; }
.tut-illo .cell.x::after { content: ""; position: absolute; inset: 0; background: linear-gradient(135deg, transparent 46%, rgba(255,143,143,.95) 47%, rgba(255,143,143,.95) 53%, transparent 54%); }
.tut-illo .cap { width: 100%; text-align: center; font-size: .75rem; color: var(--faint); }
.tut-illo .col3 { display: flex; flex-direction: column; align-items: center; gap: 6px; font-size: .72rem; color: var(--faint); }
.tut-nav { display: flex; align-items: center; justify-content: space-between; margin-top: 16px; }
.tut-dots { display: flex; gap: 6px; }
.tut-dots i { width: 7px; height: 7px; border-radius: 50%; background: var(--line-strong); transition: all .2s; }
.tut-dots i.on { background: var(--text); width: 18px; border-radius: 4px; }

/* stats */
.stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.stat { background: var(--panel); border-radius: 14px; padding: 12px 6px; text-align: center; }
.stat b { display: block; font-size: 1.45rem; font-variant-numeric: tabular-nums; }
.stat span { font-size: .72rem; color: var(--muted); }
.stat-note { color: var(--muted); font-size: .85rem; margin-top: 14px; line-height: 1.45; }

/* settings */
.setting { display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 12px 0; border-bottom: 1px solid var(--line); cursor: pointer; }
.setting span { display: flex; flex-direction: column; gap: 2px; }
.setting small { color: var(--muted); font-size: .8rem; }
.setting input { display: none; }
.switch { flex: none; width: 44px; height: 26px; border-radius: 999px; background: var(--line-strong); position: relative; transition: background .2s; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; transition: transform .2s; }
.setting input:checked + .switch { background: var(--sage); }
.setting input:checked + .switch::after { transform: translateX(18px); }
.fine { color: var(--faint); font-size: .78rem; margin: 14px 0 0; }

/* win */
.win-glow { position: absolute; inset: 0; border-radius: inherit; pointer-events: none; background: radial-gradient(70% 40% at 50% 0%, rgba(164,135,236,.3), transparent 70%); }
.win-sub { color: var(--muted); margin: -4px 0 14px; }
.win-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 14px; }
.share-preview {
  font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: .92rem; line-height: 1.25;
  background: rgba(0,0,0,.22); border-radius: 12px; padding: 12px; margin: 0 0 14px; white-space: pre-wrap; text-align: center;
}
.win-foot { color: var(--muted); text-align: center; font-size: .85rem; margin-top: 12px; }
.win-actions { display: flex; justify-content: center; margin-top: 6px; }

/* toast */
.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px);
  background: #f3f4ff; color: #17183a; font-weight: 700; padding: 10px 16px; border-radius: 12px;
  opacity: 0; pointer-events: none; transition: all .25s ease; z-index: 40; max-width: 90vw; text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-height: 700px) {
  .app { gap: 8px; }
  .pal-btn { height: 40px; }
  .tracker-title { display: none; }
  .tracker { grid-template-columns: 13px repeat(var(--n), 13px); gap: 3px; }
  .tracker .th, .tracker .tp { width: 13px; height: 13px; }
  .board { width: min(92vw, 440px, calc(100svh - 360px)); }
  .hint-btn { height: 46px; }
  .board { gap: 5px; padding: 7px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important; }
}
