:root {
  --ink: #1c2530;
  --muted: #6b7684;
  --line: #e2e6eb;
  --bg: #f7f8fa;
  --card: #ffffff;
  --accent: #1F9D5A;
  --accent-ink: #146B3E;
  --good: #1a7f4e;
  --warn: #b4740e;
  --bad: #c0342c;
  --locked: #9aa2ac;
  --radius: 10px;
}
* { box-sizing: border-box; }
body { margin: 0; font-family: -apple-system, "Segoe UI", Roboto, Arial, sans-serif; background: var(--bg); color: var(--ink); }
.topbar { display:flex; align-items:center; justify-content:space-between; padding: 14px 24px; background: var(--card); border-bottom: 1px solid var(--line); position: sticky; top:0; z-index:5; }
.brand { font-weight: 700; font-size: 18px; cursor: pointer; display: flex; align-items: center; gap: 8px; }
.brand-icon { width: 28px; height: 28px; border-radius: 7px; }
.brand .tag { font-weight: 400; font-size: 12px; color: var(--muted); border:1px solid var(--line); border-radius:6px; padding:1px 6px; margin-left:6px; }
nav { display:flex; gap:14px; font-size: 14px; }
nav a { color: var(--muted); text-decoration:none; cursor:pointer; }
nav a:hover { color: var(--ink); }
#app { max-width: 880px; margin: 0 auto; padding: 24px; min-height: 70vh; }
.footer { text-align:center; color: var(--muted); font-size:12px; padding: 24px; }

h1 { font-size: 22px; margin: 0 0 6px; }
h2 { font-size: 17px; margin: 24px 0 10px; }
p.lead { color: var(--muted); margin-top:0; }

.card { background: var(--card); border:1px solid var(--line); border-radius: var(--radius); padding: 18px; margin-bottom: 14px; }
.grid { display:grid; gap:12px; grid-template-columns: repeat(auto-fill, minmax(160px,1fr)); }
.tile { border:1px solid var(--line); border-radius: var(--radius); padding:14px; cursor:pointer; background:var(--card); }
.tile:hover { border-color: var(--accent); }
.tile.locked { color: var(--locked); cursor: default; background: #fafafa; }
.tile.selected { border-color: var(--accent); background: #eaf7ef; }
.tile .badge { display:inline-block; font-size:11px; background:#eef2f7; color:var(--muted); padding:2px 6px; border-radius:6px; margin-top:6px; }

button, .btn { font: inherit; background: var(--accent); color:#fff; border:none; border-radius: 8px; padding: 9px 14px; cursor:pointer; }
button.secondary { background: #eef2f7; color: var(--ink); }
button.danger { background: var(--bad); }
button:disabled { opacity: .5; cursor:not-allowed; }
button.small { padding: 5px 10px; font-size: 13px; }

input, select, textarea { font: inherit; padding: 8px 10px; border:1px solid var(--line); border-radius:8px; width:100%; margin-bottom:10px; background:#fff; }
label { display:block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.row { display:flex; gap:10px; flex-wrap:wrap; }
.row > * { flex:1; min-width:140px; }

.chip { display:inline-flex; align-items:center; gap:6px; border:1px solid var(--line); border-radius:20px; padding:5px 12px; margin:3px; cursor:pointer; font-size:13px; }
.chip.selected { background: var(--accent); color:#fff; border-color: var(--accent); }
.chip.correct { background:#eaf7ef; border-color: var(--good); color: var(--good); cursor:default; }
.chip.wrong { background:#fdecea; border-color: var(--bad); color: var(--bad); cursor:default; }

table { width:100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align:left; padding: 8px 6px; border-bottom: 1px solid var(--line); }
th { color: var(--muted); font-weight:600; font-size:12px; text-transform:uppercase; }

.banner { border-radius:8px; padding:10px 14px; font-size:14px; margin-bottom:12px; }
.banner.warn { background:#fff6e5; color: var(--warn); border:1px solid #f2d998; }
.banner.bad { background:#fdecea; color: var(--bad); border:1px solid #f3b9b4; }
.banner.good { background:#eaf7ef; color: var(--good); border:1px solid #b8e3c8; }

.muted { color: var(--muted); font-size: 13px; }
.pill { display:inline-block; font-size:11px; padding:2px 8px; border-radius:12px; }
.pill.good { background:#eaf7ef; color:var(--good); }
.pill.warn { background:#fff6e5; color:var(--warn); }
.pill.bad { background:#fdecea; color:var(--bad); }

canvas.answer-canvas { border:1px dashed var(--line); border-radius: 8px; background:#fff; touch-action:none; cursor:crosshair; }

/* ---------- gamification ---------- */
@keyframes popIn { 0% { transform: scale(0.6); opacity: 0; } 60% { transform: scale(1.08); opacity: 1; } 100% { transform: scale(1); } }
.encourage { display: inline-block; font-weight: 700; color: var(--accent); animation: popIn 0.35s ease-out; margin-top: 8px; }
.nickname-badge { display: inline-block; background: linear-gradient(135deg, var(--accent), var(--accent-ink)); color: #fff; padding: 4px 12px; border-radius: 20px; font-size: 13px; font-weight: 600; animation: popIn 0.4s ease-out; }
.streak-flame { display: inline-block; animation: popIn 0.4s ease-out; }
.progress-track { height: 8px; background: #eef2f7; border-radius: 6px; overflow: hidden; margin: 10px 0; }
.progress-fill { height: 100%; background: var(--accent); transition: width 0.3s ease; }
.back-link { display: inline-block; margin-bottom: 12px; color: var(--muted); text-decoration: none; font-size: 14px; }
.back-link:hover { color: var(--ink); }

/* ---------- mobile ---------- */
@media (max-width: 640px) {
  .topbar { flex-direction: column; align-items: flex-start; gap: 8px; padding: 12px 16px; }
  nav { flex-wrap: wrap; gap: 10px; width: 100%; }
  #app { padding: 14px; }
  .row { flex-direction: column; }
  .row > * { min-width: 0; }
  table { display: block; overflow-x: auto; white-space: nowrap; }
  .grid { grid-template-columns: 1fr 1fr; }
  button, .btn { width: 100%; }
  button.small, .row button { width: auto; }
}

/* ---------- math notation ---------- */
.frac { display:inline-flex; flex-direction:column; vertical-align:middle; text-align:center; margin:0 3px; font-size:0.88em; line-height:1.15; position:relative; top:2px; }
.frac .num { border-bottom:1.4px solid currentColor; padding:0 3px 1px; }
.frac .den { padding:1px 3px 0; }

/* ---------- visual refresh: layered background, motion, depth ---------- */
:root {
  --brand-deep: #0d3b2a;
  --brand-mid: #146b3e;
  --shadow-sm: 0 1px 2px rgba(16, 38, 29, .06), 0 2px 8px rgba(16, 38, 29, .05);
  --shadow-md: 0 6px 20px rgba(16, 38, 29, .10), 0 2px 6px rgba(16, 38, 29, .06);
  --ease: cubic-bezier(.2, .7, .2, 1);
}

body {
  background:
    radial-gradient(1100px 600px at 10% -10%, rgba(31, 157, 90, .10), transparent 60%),
    radial-gradient(900px 500px at 110% 10%, rgba(20, 107, 62, .08), transparent 60%),
    linear-gradient(180deg, #f4faf6 0%, #f7f8fa 45%, #f3f6f9 100%);
  background-attachment: fixed;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

/* Drifting colour shapes behind the page. Purely decorative. */
body::before, body::after {
  content: ""; position: fixed; z-index: 0; pointer-events: none;
  width: 42vmax; height: 42vmax; border-radius: 50%; filter: blur(60px); opacity: .35;
}
body::before { background: radial-gradient(circle at 30% 30%, #7fd6a6, transparent 70%); top: -14vmax; left: -10vmax; animation: drift 26s ease-in-out infinite alternate; }
body::after { background: radial-gradient(circle at 60% 40%, #9cc9ff, transparent 70%); bottom: -16vmax; right: -12vmax; animation: drift 32s ease-in-out infinite alternate-reverse; }
@keyframes drift {
  0%   { transform: translate(0, 0) scale(1); }
  100% { transform: translate(6vmax, 4vmax) scale(1.12); }
}

.topbar, #app, .footer { position: relative; z-index: 1; }

.topbar {
  background: linear-gradient(115deg, var(--brand-deep) 0%, var(--brand-mid) 55%, var(--accent) 100%);
  border-bottom: none;
  box-shadow: 0 4px 18px rgba(13, 59, 42, .25);
  color: #fff;
}
.brand { color: #fff; }
.brand-icon { box-shadow: 0 0 0 2px rgba(255, 255, 255, .25); transition: transform .4s var(--ease); }
.brand:hover .brand-icon { transform: rotate(-8deg) scale(1.06); }
.brand .tag { color: rgba(255, 255, 255, .85); border-color: rgba(255, 255, 255, .35); }
nav a { color: rgba(255, 255, 255, .82); position: relative; padding: 4px 0; transition: color .2s; }
nav a::after {
  content: ""; position: absolute; left: 0; bottom: -2px; height: 2px; width: 100%;
  background: #fff; transform: scaleX(0); transform-origin: left; transition: transform .3s var(--ease);
}
nav a:hover, nav a.active { color: #fff; }
nav a:hover::after, nav a.active::after { transform: scaleX(1); }
.topbar #langToggle { background: rgba(255, 255, 255, .16); color: #fff; }
.topbar #langToggle:hover { background: rgba(255, 255, 255, .28); }

#app { padding-top: 32px; }
#app > * { animation: rise .5s var(--ease) both; }
#app > *:nth-child(2) { animation-delay: .05s; }
#app > *:nth-child(3) { animation-delay: .10s; }
#app > *:nth-child(n+4) { animation-delay: .15s; }
@keyframes rise {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: none; }
}

h1 { font-size: 26px; letter-spacing: -.01em; position: relative; padding-bottom: 10px; }
h1::after {
  content: ""; position: absolute; left: 0; bottom: 0; width: 44px; height: 3px;
  border-radius: 3px; background: linear-gradient(90deg, var(--accent), #7fd6a6);
}

.card {
  background: rgba(255, 255, 255, .92);
  backdrop-filter: blur(6px);
  border: 1px solid rgba(226, 230, 235, .9);
  box-shadow: var(--shadow-sm);
  transition: box-shadow .3s var(--ease), transform .3s var(--ease);
}
.card:hover { box-shadow: var(--shadow-md); }
.card[style*="cursor:pointer"]:hover, .card[style*="cursor: pointer"]:hover { transform: translateY(-2px); }

.tile {
  background: #fff; box-shadow: var(--shadow-sm);
  transition: transform .25s var(--ease), box-shadow .25s var(--ease), border-color .2s;
}
.tile:not(.locked):hover { transform: translateY(-3px); box-shadow: var(--shadow-md); }
.tile.selected { box-shadow: 0 0 0 3px rgba(31, 157, 90, .18); }

button, .btn {
  background: linear-gradient(135deg, var(--accent), var(--accent-ink));
  box-shadow: 0 2px 6px rgba(20, 107, 62, .25);
  transition: transform .15s var(--ease), box-shadow .2s, filter .2s;
  font-weight: 600; letter-spacing: .01em;
}
button:hover:not(:disabled) { transform: translateY(-1px); box-shadow: 0 6px 14px rgba(20, 107, 62, .28); filter: brightness(1.03); }
button:active:not(:disabled) { transform: scale(.98); }
button.secondary {
  background: #fff; color: var(--accent-ink);
  border: 1px solid #cfe9da; box-shadow: var(--shadow-sm);
}
button.secondary:hover:not(:disabled) { background: #f1faf5; box-shadow: var(--shadow-md); }
button.danger { background: linear-gradient(135deg, #d9473f, var(--bad)); box-shadow: 0 2px 6px rgba(192, 52, 44, .25); }

input, select, textarea {
  background: #fff; transition: border-color .2s, box-shadow .2s;
}
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(31, 157, 90, .18);
}

.chip { background: #fff; box-shadow: var(--shadow-sm); transition: transform .2s var(--ease), background .2s; }
.chip:hover { transform: translateY(-1px); }
.chip.selected { box-shadow: 0 2px 8px rgba(31, 157, 90, .35); }

.banner { box-shadow: var(--shadow-sm); animation: rise .35s var(--ease) both; }
.pill { letter-spacing: .02em; }
.back-link { transition: transform .2s var(--ease), color .2s; }
.back-link:hover { transform: translateX(-3px); }

table { background: #fff; border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow-sm); }
th { background: #f6faf8; }
tr { transition: background .15s; }
tbody tr:hover { background: #f6faf8; }

.footer { color: rgba(28, 37, 48, .55); }

/* Respect users who ask for less motion. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

@media (max-width: 640px) {
  .topbar { padding: 12px 16px; }
  #app { padding-top: 20px; }
  h1 { font-size: 22px; }
}

/* ---------- matrices: [[a, b], [c, d]] in question text ---------- */
.matrix { display: inline-flex; flex-direction: column; justify-content: center; vertical-align: middle; border-left: 2px solid currentColor; border-right: 2px solid currentColor; border-radius: 6px; padding: 4px 10px; margin: 0 6px; }
.mrow { display: flex; justify-content: space-around; gap: 18px; padding: 2px 0; }
.mrow > span { min-width: 1.6em; text-align: center; }
