/* ============ Design tokens ============ */
:root {
  --purple-50:#EEEDFE; --purple-100:#CECBF6; --purple-200:#AFA9EC;
  --purple-400:#7F77DD; --purple-600:#534AB7; --purple-800:#3C3489; --purple-900:#26215C;
  --teal-50:#E1F5EE; --teal-100:#9FE1CB; --teal-400:#1D9E75; --teal-600:#0F6E56; --teal-800:#085041;
  --amber-50:#FAEEDA; --amber-600:#BA7517; --amber-800:#633806;
  --red-50:#FCEBEB; --red-600:#A32D2D; --red-800:#791F1F;
  --bg:#F7F6FB; --surface:#FFFFFF; --surface-2:#F1F0F7;
  --text:#26215C; --text-2:#5F5E5A; --text-3:#888780;
  --border:#E4E2EE; --border-strong:#CFCCE0;
  --radius-s:8px; --radius-m:12px; --radius-l:18px; --radius-xl:24px;
  --shadow:0 1px 2px rgba(38,33,92,.05), 0 4px 16px rgba(38,33,92,.06);
  --shadow-lg:0 8px 40px rgba(38,33,92,.14);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --base-size:16px;
}

/* Elder mode: larger text, stronger contrast */
body.elder-mode {
  --base-size:19px;
  --text:#1B173F; --text-2:#403D63; --text-3:#5F5E5A;
  --border:#B9B5D4; --border-strong:#8F8AAE;
  --shadow:0 1px 2px rgba(27,23,63,.08), 0 4px 16px rgba(27,23,63,.1);
}

*, *::before, *::after { box-sizing:border-box; }
html { -webkit-text-size-adjust:100%; }
body {
  margin:0; font-family:var(--font); background:var(--bg); color:var(--text);
  font-size:var(--base-size); line-height:1.6; min-height:100dvh;
  display:flex; flex-direction:column;
}
body.elder-mode { font-size:var(--base-size); }
button, input, select { font-family:inherit; font-size:inherit; }
a { color:var(--purple-600); }
img, svg { max-width:100%; }

/* ============ Accessibility ============ */
.skip-link {
  position:absolute; left:-9999px; top:0; background:var(--purple-600); color:#fff;
  padding:10px 16px; z-index:1000; border-radius:0 0 var(--radius-s) 0;
}
.skip-link:focus { left:0; }
:focus-visible { outline:3px solid var(--purple-400); outline-offset:2px; border-radius:4px; }
.sr-only { position:absolute; width:1px; height:1px; padding:0; margin:-1px; overflow:hidden; clip:rect(0 0 0 0); white-space:nowrap; border:0; }

/* ============ Header ============ */
.app-header {
  position:sticky; top:0; z-index:50; background:rgba(255,255,255,.92);
  backdrop-filter:saturate(180%) blur(12px); border-bottom:1px solid var(--border);
}
.header-inner {
  max-width:1080px; margin:0 auto; padding:12px 20px;
  display:flex; align-items:center; gap:20px;
}
.brand { display:flex; align-items:center; gap:10px; text-decoration:none; color:var(--purple-800); flex-shrink:0; }
.brand-mark { display:grid; place-items:center; width:38px; height:38px; border-radius:11px; background:var(--purple-50); color:var(--purple-600); }
.brand-name { font-weight:700; font-size:1.15rem; letter-spacing:.02em; }
.header-nav { display:flex; gap:4px; margin-left:8px; }
.header-nav a {
  text-decoration:none; color:var(--text-2); padding:7px 14px; border-radius:9px;
  font-weight:500; font-size:.95rem; transition:background .15s, color .15s;
}
.header-nav a:hover { background:var(--surface-2); color:var(--text); }
.header-nav a.active { background:var(--purple-50); color:var(--purple-800); font-weight:600; }
.header-actions { margin-left:auto; display:flex; align-items:center; gap:10px; }
.icon-btn {
  display:inline-flex; align-items:center; gap:6px; background:transparent; border:1px solid var(--border);
  border-radius:10px; padding:7px 10px; color:var(--text-2); cursor:pointer; font-size:.85rem;
}
.icon-btn:hover { background:var(--surface-2); }
.icon-btn[aria-pressed="true"] { background:var(--amber-50); border-color:var(--amber-600); color:var(--amber-800); }
.icon-btn-label { font-size:.82rem; }
.account-area { display:flex; align-items:center; gap:8px; }
.btn { border:0; border-radius:10px; padding:9px 16px; cursor:pointer; font-weight:600; font-size:.92rem; transition:transform .1s, filter .15s, background .15s; }
.btn:active { transform:translateY(1px); }
.btn-primary { background:var(--purple-600); color:#fff; }
.btn-primary:hover { filter:brightness(1.08); }
.btn-ghost { background:var(--surface-2); color:var(--text); border:1px solid var(--border); }
.btn-ghost:hover { background:var(--purple-50); }
.btn-sm { padding:6px 12px; font-size:.85rem; }
.user-chip { display:flex; align-items:center; gap:8px; background:var(--purple-50); border-radius:10px; padding:5px 10px 5px 6px; }
.avatar { width:30px; height:30px; border-radius:50%; background:var(--purple-600); color:#fff; display:grid; place-items:center; font-weight:600; font-size:.85rem; }
.user-name { font-size:.88rem; font-weight:600; color:var(--purple-800); max-width:110px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
.rank-badge { font-size:.72rem; padding:2px 7px; border-radius:20px; background:var(--teal-50); color:var(--teal-800); font-weight:600; }

/* ============ Main / footer ============ */
.app-main { flex:1; width:100%; }
#page-root { max-width:1080px; margin:0 auto; padding:28px 20px 56px; }
.app-footer { border-top:1px solid var(--border); background:var(--surface); }
.footer-inner { max-width:1080px; margin:0 auto; padding:20px; display:flex; flex-wrap:wrap; gap:12px 24px; align-items:center; justify-content:space-between; }
.disclaimer { margin:0; color:var(--text-2); font-size:.85rem; }
.footer-links { display:flex; gap:18px; align-items:center; font-size:.85rem; }
.footer-links a { text-decoration:none; }
.footer-links a:hover { text-decoration:underline; }
.footer-note { color:var(--text-3); }
.noscript { padding:20px; text-align:center; background:var(--red-50); color:var(--red-800); }

/* ============ Generic building blocks ============ */
.page-head { margin-bottom:24px; }
.page-title { font-size:1.7rem; font-weight:700; margin:0 0 6px; color:var(--purple-900); letter-spacing:-.01em; }
.page-sub { margin:0; color:var(--text-2); font-size:.98rem; }
.section-title { font-size:1.15rem; font-weight:700; margin:0 0 14px; color:var(--purple-900); }
.card { background:var(--surface); border:1px solid var(--border); border-radius:var(--radius-l); box-shadow:var(--shadow); }
.muted { color:var(--text-2); }
.hint { color:var(--text-3); font-size:.85rem; }

/* Hero */
.hero {
  position:relative; overflow:hidden; border-radius:var(--radius-xl); padding:32px 34px; margin-bottom:28px;
  background:linear-gradient(135deg, #534AB7 0%, #3C3489 55%, #26215C 100%); color:#fff; box-shadow:var(--shadow-lg);
}
.hero::after { content:""; position:absolute; width:280px; height:280px; border-radius:50%; background:rgba(255,255,255,.07); right:-70px; top:-110px; }
.hero h1 { margin:0 0 10px; font-size:1.9rem; font-weight:700; letter-spacing:-.02em; position:relative; }
.hero p { margin:0 0 20px; max-width:560px; color:rgba(255,255,255,.86); position:relative; font-size:1rem; }
.hero-stats { display:flex; flex-wrap:wrap; gap:26px; position:relative; }
.hero-stat { display:flex; flex-direction:column; }
.hero-stat b { font-size:1.5rem; font-weight:700; line-height:1.1; }
.hero-stat span { font-size:.8rem; color:rgba(255,255,255,.72); }

/* Stat strip */
.stat-strip { display:grid; grid-template-columns:repeat(3,1fr); gap:14px; margin-bottom:28px; }
.stat-box { padding:16px 18px; }
.stat-box .label { color:var(--text-2); font-size:.82rem; }
.stat-box .value { font-size:1.5rem; font-weight:700; color:var(--purple-800); line-height:1.2; margin-top:2px; }
.stat-box .value small { font-size:.8rem; font-weight:500; color:var(--text-3); }

/* Dimension sections */
.dimension { margin-bottom:32px; }
.dimension-head { display:flex; align-items:center; gap:12px; margin-bottom:14px; }
.dim-icon { width:40px; height:40px; border-radius:12px; display:grid; place-items:center; flex-shrink:0; }
.dim-icon.wm { background:var(--purple-50); color:var(--purple-600); }
.dim-icon.at { background:var(--teal-50); color:var(--teal-600); }
.dim-icon.ic { background:var(--amber-50); color:var(--amber-600); }
.dim-icon.cf { background:#FBEAF0; color:#D4537E; }
.dim-titles h2 { margin:0; font-size:1.15rem; font-weight:700; }
.dim-titles p { margin:0; font-size:.82rem; color:var(--text-2); }
.task-grid { display:grid; grid-template-columns:repeat(3,1fr); gap:14px; }
.task-card {
  text-align:left; padding:18px; cursor:pointer; background:var(--surface); border:1px solid var(--border);
  border-radius:var(--radius-m); box-shadow:var(--shadow); transition:transform .15s, box-shadow .15s, border-color .15s;
  position:relative; display:flex; flex-direction:column; gap:8px; color:var(--text); width:100%;
}
.task-card:hover { transform:translateY(-3px); box-shadow:var(--shadow-lg); border-color:var(--purple-200); }
.task-card-top { display:flex; align-items:center; justify-content:space-between; gap:8px; }
.task-card h3 { margin:0; font-size:1.02rem; font-weight:650; }
.task-card .task-desc { margin:0; color:var(--text-2); font-size:.86rem; line-height:1.5; flex:1; }
.task-meta { display:flex; gap:8px; flex-wrap:wrap; align-items:center; }
.tag { font-size:.72rem; padding:3px 9px; border-radius:20px; background:var(--surface-2); color:var(--text-2); font-weight:500; }
.tag.best { background:var(--teal-50); color:var(--teal-800); }
.tag.new { background:var(--amber-50); color:var(--amber-800); }
.play-arrow { color:var(--purple-400); font-size:1.2rem; }

/* ============ Training / game ============ */
.game-layout { max-width:760px; margin:0 auto; }
.game-card { padding:26px; text-align:center; }
.game-head { display:flex; align-items:center; gap:12px; margin-bottom:18px; text-align:left; }
.back-btn { background:var(--surface-2); border:1px solid var(--border); border-radius:9px; padding:7px 12px; cursor:pointer; color:var(--text); font-size:.88rem; }
.game-title { font-size:1.2rem; font-weight:700; margin:0; }
.game-sub { font-size:.85rem; color:var(--text-2); margin:2px 0 0; }
.game-stage {
  position:relative; min-height:320px; border-radius:var(--radius-m); background:var(--surface-2);
  display:flex; flex-direction:column; align-items:center; justify-content:center; padding:24px; overflow:hidden;
}
.stimulus { font-weight:700; color:var(--purple-900); user-select:none; }
.trial-progress { height:6px; border-radius:6px; background:var(--surface-2); overflow:hidden; margin:16px 0 0; }
.trial-progress > i { display:block; height:100%; background:var(--purple-500, var(--purple-400)); width:0; transition:width .2s; }
.game-info-row { display:flex; justify-content:space-between; font-size:.85rem; color:var(--text-2); margin-top:12px; }
.answer-pad { display:flex; gap:10px; justify-content:center; flex-wrap:wrap; margin-top:18px; }
.answer-btn {
  min-width:64px; padding:12px 18px; border-radius:12px; border:1px solid var(--border-strong);
  background:var(--surface); cursor:pointer; font-weight:600; font-size:1.1rem; color:var(--text);
  transition:transform .08s, background .12s;
}
.answer-btn:hover { background:var(--purple-50); }
.answer-btn:active { transform:scale(.95); }
.answer-btn.correct { background:var(--teal-50); border-color:var(--teal-400); color:var(--teal-800); }
.answer-btn.wrong { background:var(--red-50); border-color:var(--red-600); color:var(--red-800); }
.feedback-line { min-height:26px; margin-top:12px; font-weight:600; }
.feedback-line.good { color:var(--teal-600); }
.feedback-line.bad { color:var(--red-600); }

/* N-back grid */
.nback-grid { display:grid; grid-template-columns:repeat(3,64px); grid-template-rows:repeat(3,64px); gap:8px; }
.nback-cell { border-radius:10px; background:var(--surface); border:1px solid var(--border); }
.nback-cell.lit { background:var(--purple-400); border-color:var(--purple-600); box-shadow:0 0 0 4px var(--purple-50); }

/* grid search */
.search-grid { display:grid; gap:6px; }
.search-cell { width:40px; height:40px; border-radius:8px; background:var(--surface); border:1px solid var(--border); display:grid; place-items:center; cursor:pointer; font-size:1.1rem; font-weight:700; }
.search-cell:disabled { cursor:default; }

/* instruction */
.instruction-card { padding:28px; }
.instruction-card .lead { font-size:1.05rem; color:var(--text); }
.instruction-card ul { line-height:1.9; color:var(--text-2); }
.difficulty-pick { display:flex; gap:8px; flex-wrap:wrap; margin:16px 0; }
.difficulty-pick button { padding:8px 16px; border-radius:9px; border:1px solid var(--border-strong); background:var(--surface); cursor:pointer; }
.difficulty-pick button[aria-pressed="true"] { background:var(--purple-600); color:#fff; border-color:var(--purple-600); }

/* Results */
.result-hero { text-align:center; padding:28px; }
.score-ring { width:150px; height:150px; margin:0 auto 16px; position:relative; }
.score-ring svg { transform:rotate(-90deg); }
.score-ring .score-num { position:absolute; inset:0; display:grid; place-items:center; flex-direction:column; }
.score-ring .score-num b { font-size:2.1rem; font-weight:750; color:var(--purple-800); line-height:1; }
.score-ring .score-num span { font-size:.78rem; color:var(--text-2); }
.result-grid { display:grid; grid-template-columns:repeat(auto-fit,minmax(140px,1fr)); gap:12px; margin:18px 0; }
.result-item { padding:14px; text-align:center; }
.result-item .k { font-size:.8rem; color:var(--text-2); }
.result-item .v { font-size:1.25rem; font-weight:700; color:var(--text); }
.delta-up { color:var(--teal-600); } .delta-down { color:var(--red-600); } .delta-flat { color:var(--text-3); }
.badge-pop { background:var(--amber-50); border:1px solid var(--amber-600); color:var(--amber-800); padding:12px 16px; border-radius:12px; margin:14px 0; font-weight:600; }
.result-actions { display:flex; gap:12px; justify-content:center; flex-wrap:wrap; margin-top:8px; }

/* ============ Growth ============ */
.chart-wrap { padding:20px; }
.chart-canvas { width:100%; height:200px; }
.calendar { display:grid; grid-template-columns:repeat(7,1fr); gap:5px; margin-top:10px; }
.calendar-day { aspect-ratio:1; border-radius:6px; background:var(--surface-2); display:grid; place-items:center; font-size:.72rem; color:var(--text-3); }
.calendar-day.on1 { background:var(--purple-100); color:var(--purple-900); }
.calendar-day.on2 { background:var(--purple-200); color:var(--purple-900); }
.calendar-day.on3 { background:var(--purple-400); color:#fff; }
.calendar-day.on4 { background:var(--purple-600); color:#fff; }
.empty-state { text-align:center; padding:50px 20px; color:var(--text-2); }
.empty-state .empty-emoji { font-size:2.6rem; margin-bottom:8px; }
.history-table { width:100%; border-collapse:collapse; font-size:.88rem; }
.history-table th, .history-table td { text-align:left; padding:10px 12px; border-bottom:1px solid var(--border); }
.history-table th { color:var(--text-2); font-weight:600; font-size:.8rem; }

/* ============ Achievements ============ */
.rank-hero { padding:24px; display:flex; align-items:center; gap:20px; flex-wrap:wrap; margin-bottom:22px; }
.rank-medal { width:72px; height:72px; border-radius:50%; display:grid; place-items:center; font-size:1.8rem; background:linear-gradient(135deg,var(--purple-400),var(--purple-800)); color:#fff; flex-shrink:0; }
.rank-info h2 { margin:0; font-size:1.4rem; }
.rank-progress { height:8px; background:var(--surface-2); border-radius:8px; overflow:hidden; width:260px; max-width:100%; margin-top:8px; }
.rank-progress > i { display:block; height:100%; background:var(--purple-600); }
.badge-grid { display:grid; grid-template-columns:repeat(auto-fill,minmax(150px,1fr)); gap:12px; }
.badge-item { padding:16px; text-align:center; opacity:.45; filter:grayscale(1); }
.badge-item.unlocked { opacity:1; filter:none; border-color:var(--purple-200); }
.badge-icon { font-size:2rem; }
.badge-item b { display:block; margin-top:6px; font-size:.92rem; }
.badge-item p { margin:4px 0 0; font-size:.76rem; color:var(--text-2); }

/* ============ Auth & settings ============ */
.auth-wrap { max-width:440px; margin:0 auto; }
.auth-card { padding:28px; }
.auth-card h1 { margin:0 0 6px; font-size:1.5rem; }
.field { margin-bottom:14px; }
.field label { display:block; font-size:.86rem; font-weight:600; margin-bottom:6px; }
.field input { width:100%; padding:11px 13px; border:1px solid var(--border-strong); border-radius:10px; background:var(--surface); color:var(--text); }
.field input:focus { border-color:var(--purple-400); }
.field .hint { margin-top:4px; }
.checkbox-row { display:flex; gap:8px; align-items:flex-start; font-size:.85rem; color:var(--text-2); margin:10px 0; }
.checkbox-row input { margin-top:4px; }
.auth-switch { text-align:center; margin-top:16px; font-size:.9rem; }
.auth-error { color:var(--red-600); font-size:.88rem; min-height:20px; }
.guest-banner { background:var(--amber-50); border:1px solid var(--amber-600); color:var(--amber-800); padding:14px 16px; border-radius:12px; margin-bottom:20px; display:flex; gap:12px; align-items:center; flex-wrap:wrap; }
.guest-banner .grow { flex:1; }
.settings-section { padding:20px; margin-bottom:16px; }
.settings-section h2 { margin:0 0 12px; font-size:1.1rem; }
.settings-row { display:flex; align-items:center; justify-content:space-between; gap:16px; padding:12px 0; border-bottom:1px solid var(--border); flex-wrap:wrap; }
.settings-row:last-child { border-bottom:0; }
.settings-row .label { font-weight:600; }
.settings-row .desc { font-size:.82rem; color:var(--text-2); }
.danger-zone { border-color:var(--red-600); }
.danger-zone h2 { color:var(--red-800); }
.legal-content { padding:28px; line-height:1.9; color:var(--text-2); max-width:820px; margin:0 auto; }
.legal-content h2 { color:var(--purple-900); font-size:1.2rem; margin-top:24px; }
.legal-content h3 { color:var(--text); font-size:1.02rem; margin-top:18px; }

/* ============ Modal / toast ============ */
.modal-backdrop { position:fixed; inset:0; background:rgba(38,33,92,.5); display:grid; place-items:center; z-index:200; padding:20px; }
.modal-box { background:var(--surface); border-radius:var(--radius-l); padding:26px; max-width:440px; width:100%; box-shadow:var(--shadow-lg); }
.modal-box h2 { margin:0 0 8px; font-size:1.25rem; }
.modal-box p { color:var(--text-2); }
.modal-actions { display:flex; gap:10px; justify-content:flex-end; margin-top:20px; flex-wrap:wrap; }
.toast-region { position:fixed; left:50%; bottom:28px; transform:translateX(-50%); z-index:300; display:flex; flex-direction:column; gap:8px; align-items:center; pointer-events:none; }
.toast { background:var(--purple-900); color:#fff; padding:11px 18px; border-radius:12px; box-shadow:var(--shadow-lg); font-size:.9rem; animation:toastIn .25s ease; }
.toast.success { background:var(--teal-800); }
.toast.error { background:var(--red-800); }
@keyframes toastIn { from { opacity:0; transform:translateY(10px); } to { opacity:1; transform:none; } }

/* sync indicator */
.sync-pill { font-size:.75rem; padding:3px 9px; border-radius:20px; background:var(--surface-2); color:var(--text-2); }
.sync-pill.ok { background:var(--teal-50); color:var(--teal-800); }
.sync-pill.err { background:var(--red-50); color:var(--red-800); }

/* ============ Responsive ============ */
@media (max-width:900px) {
  .task-grid { grid-template-columns:repeat(2,1fr); }
}
@media (max-width:640px) {
  .header-inner { padding:10px 14px; gap:10px; flex-wrap:wrap; }
  .brand-name { font-size:1rem; }
  .header-nav { order:3; width:100%; margin-left:0; justify-content:space-around; }
  .header-nav a { flex:1; text-align:center; }
  .icon-btn-label { display:none; }
  #page-root { padding:18px 14px 40px; }
  .task-grid { grid-template-columns:1fr; }
  .stat-strip { grid-template-columns:1fr; }
  .hero { padding:24px 20px; }
  .hero h1 { font-size:1.5rem; }
  .game-card { padding:18px; }
  .game-stage { min-height:280px; padding:16px; }
  .footer-inner { flex-direction:column; align-items:flex-start; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration:.01ms !important; transition-duration:.01ms !important; }
}
