/* ── Theme Variables ── */
:root {
  --bg: #0b0d14;
  --bg2: #141823;
  --bg3: #1c2030;
  --surface: #181c28;
  --surface-2: #1e2331;
  --surface-hover: #252b3d;
  --border: #2a3043;
  --border-light: #353c52;
  --text: #eef0f6;
  --text-dim: #9298b0;
  --text-muted: #5d6378;
  --accent: #6c8cff;
  --accent-hi: #8aa4ff;
  --accent-glow: rgba(108, 140, 255, 0.28);
  --accent2: #a78bfa;
  --accent-grad: linear-gradient(135deg, #6c8cff, #a78bfa);
  --success: #34d399;
  --success-bg: rgba(52, 211, 153, 0.12);
  --error: #f87171;
  --error-bg: rgba(248, 113, 113, 0.12);
  --warning: #fbbf24;
  --warning-bg: rgba(251, 191, 36, 0.1);
  --radius: 16px;
  --radius-sm: 10px;
  --radius-xs: 6px;
  --shadow: 0 8px 32px rgba(0,0,0,0.35);
  --shadow-sm: 0 2px 12px rgba(0,0,0,0.2);
  --font-main: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, 'SF Mono', 'Cascadia Code', Consolas, 'Liberation Mono', Menlo, monospace;
  color-scheme: dark;
}

html[data-theme="light"] {
  --bg: #eef0f5;
  --bg2: #ffffff;
  --bg3: #e4e7ee;
  --surface: #ffffff;
  --surface-2: #f7f8fb;
  --surface-hover: #eef0f5;
  --border: #d5d9e6;
  --border-light: #c4cad8;
  --text: #1a1d27;
  --text-dim: #5c6280;
  --text-muted: #949ab0;
  --accent: #4f6bff;
  --accent-hi: #6b85ff;
  --accent-glow: rgba(79, 107, 255, 0.18);
  --accent2: #8b5cf6;
  --accent-grad: linear-gradient(135deg, #4f6bff, #8b5cf6);
  --success: #059669;
  --success-bg: rgba(5, 150, 105, 0.1);
  --error: #dc2626;
  --error-bg: rgba(220, 38, 38, 0.08);
  --warning: #d97706;
  --warning-bg: rgba(217, 119, 6, 0.08);
  --shadow: 0 8px 32px rgba(0,0,0,0.08);
  --shadow-sm: 0 2px 8px rgba(0,0,0,0.05);
  color-scheme: light;
}

@media (prefers-color-scheme: light) {
  html:not([data-theme="dark"]) {
    --bg: #eef0f5;
    --bg2: #ffffff;
    --bg3: #e4e7ee;
    --surface: #ffffff;
    --surface-2: #f7f8fb;
    --surface-hover: #eef0f5;
    --border: #d5d9e6;
    --border-light: #c4cad8;
    --text: #1a1d27;
    --text-dim: #5c6280;
    --text-muted: #949ab0;
    --accent: #4f6bff;
    --accent-hi: #6b85ff;
    --accent-glow: rgba(79, 107, 255, 0.18);
    --accent2: #8b5cf6;
    --accent-grad: linear-gradient(135deg, #4f6bff, #8b5cf6);
    --success: #059669;
    --success-bg: rgba(5, 150, 105, 0.1);
    --error: #dc2626;
    --error-bg: rgba(220, 38, 38, 0.08);
    --warning: #d97706;
    --warning-bg: rgba(217, 119, 6, 0.08);
    --shadow: 0 8px 32px rgba(0,0,0,0.08);
    --shadow-sm: 0 2px 8px rgba(0,0,0,0.05);
    color-scheme: light;
  }
}

/* ── Reset & Base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  margin: 0; padding: 0;
  height: 100%; min-height: 100%;
  overflow: hidden;
  font-family: var(--font-main);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  touch-action: manipulation;
}

body {
  background:
    radial-gradient(ellipse 80% 50% at 50% -10%, rgba(108,140,255,0.06), transparent 60%),
    var(--bg);
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 640px;
  margin: 0 auto;
  padding: 0;
  position: relative;
}

/* ── Top Bar ── */
.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  background: rgba(20, 24, 35, 0.85);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  position: relative;
  z-index: 10;
}

html[data-theme="light"] .top-bar,
html:not([data-theme="dark"]) .top-bar {
  background: rgba(255, 255, 255, 0.85);
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
}

.brand-icon {
  font-size: 20px;
  filter: drop-shadow(0 0 6px var(--accent-glow));
}

.brand-name {
  font-size: 19px;
  font-weight: 800;
  letter-spacing: -0.025em;
  background: var(--accent-grad);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.stats-row {
  display: flex;
  gap: 14px;
}

.stat {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-dim);
  padding: 4px 10px;
  border-radius: 20px;
  background: var(--bg3);
  border: 1px solid var(--border);
}

.stat-icon { font-size: 14px; line-height: 1; }
.stat-val {
  color: var(--text);
  font-variant-numeric: tabular-nums;
  font-size: 14px;
}

/* ── Mode Tabs ── */
.mode-tabs {
  display: flex;
  gap: 4px;
  padding: 10px 14px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.mode-tab {
  flex: 1;
  padding: 9px 6px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-dim);
  font-family: var(--font-main);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  touch-action: manipulation;
  letter-spacing: -0.01em;
}

.mode-tab:hover {
  background: var(--bg3);
  color: var(--text);
}

.mode-tab.active {
  background: var(--accent-grad);
  color: #fff;
  box-shadow: 0 4px 16px var(--accent-glow), inset 0 1px 0 rgba(255,255,255,0.15);
}

/* ── Progress ── */
.progress-wrap {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 10px 18px 12px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.progress-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.progress-track {
  height: 5px;
  background: var(--bg3);
  border-radius: 3px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: var(--accent-grad);
  border-radius: 3px;
  transition: width 0.5s cubic-bezier(0.4, 0, 0.2, 1);
  width: 0%;
  box-shadow: 0 0 12px var(--accent-glow);
  position: relative;
}

.progress-fill::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.25), transparent);
  animation: shimmer 2.5s infinite;
}

@keyframes shimmer {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(200%); }
}

/* ── Game Area ── */
.game-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 20px 18px;
  gap: 16px;
  overflow-y: auto;
  min-height: 0;
  -webkit-overflow-scrolling: touch;
}

/* ── Problem Card ── */
.problem-card {
  background: linear-gradient(160deg, var(--surface-2), var(--surface));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px 24px 24px;
  text-align: center;
  box-shadow: var(--shadow);
  transition: transform 0.2s, box-shadow 0.2s, border-color 0.3s;
  position: relative;
  overflow: hidden;
}

.problem-card::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 3px;
  background: var(--accent-grad);
  opacity: 0.8;
}

.problem-card::after {
  content: '';
  position: absolute;
  top: -40px; right: -40px;
  width: 120px; height: 120px;
  background: radial-gradient(circle, var(--accent-glow), transparent 70%);
  opacity: 0.5;
  pointer-events: none;
}

.problem-card.correct-flash {
  animation: correctPulse 0.6s ease;
  border-color: var(--success);
}

.problem-card.wrong-shake {
  animation: wrongShake 0.5s ease;
  border-color: var(--error);
}

.problem-card.enter {
  animation: cardEnter 0.45s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes cardEnter {
  from { opacity: 0; transform: translateY(14px) scale(0.97); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes correctPulse {
  0%, 100% { box-shadow: var(--shadow); }
  40% { box-shadow: 0 0 0 4px var(--success-bg), 0 8px 40px rgba(52,211,153,0.25); }
}

@keyframes wrongShake {
  0%, 100% { transform: translateX(0); }
  15% { transform: translateX(-10px); }
  30% { transform: translateX(10px); }
  45% { transform: translateX(-7px); }
  60% { transform: translateX(7px); }
  75% { transform: translateX(-4px); }
  90% { transform: translateX(4px); }
}

.problem-label {
  font-size: 11px;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.12em;
  margin-bottom: 14px;
}

.problem-math {
  font-family: var(--font-mono);
  font-size: 30px;
  font-weight: 700;
  color: var(--text);
  line-height: 1.45;
  word-break: break-word;
  letter-spacing: -0.01em;
  position: relative;
  z-index: 1;
}

.problem-note {
  margin-top: 10px;
  font-size: 12px;
  color: var(--text-muted);
  font-style: italic;
  font-weight: 500;
  position: relative;
  z-index: 1;
}

/* ── Fraction Visual (bars) ── */
.fraction-visual {
  margin-top: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
  position: relative;
  z-index: 1;
}

.frac-bar {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  min-width: 80px;
}

.bar-track {
  width: 90px;
  height: 14px;
  background: var(--bg3);
  border-radius: 7px;
  overflow: hidden;
  border: 1px solid var(--border);
}

.bar-fill {
  height: 100%;
  background: var(--accent-grad);
  border-radius: 6px 0 0 6px;
  transition: width 0.6s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.2);
}

.bar-frac {
  font-family: var(--font-mono);
  font-size: 14px;
  font-weight: 700;
  color: var(--text-dim);
}

.bar-slash {
  margin: 0 1px;
  color: var(--text-muted);
  font-size: 12px;
}

.bar-op {
  font-size: 20px;
  font-weight: 700;
  color: var(--text-muted);
  font-family: var(--font-mono);
  align-self: center;
  margin-top: -10px;
}

/* ── Answer Section ── */
.answer-section {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.input-row {
  display: flex;
  gap: 8px;
}

.answer-input {
  flex: 1;
  padding: 15px 18px;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 20px;
  font-weight: 600;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.answer-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px var(--accent-glow);
}

.answer-input::placeholder {
  color: var(--text-muted);
  font-weight: 400;
}

.submit-btn {
  padding: 15px 22px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--accent-grad);
  color: #fff;
  font-family: var(--font-main);
  font-size: 20px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.15s cubic-bezier(0.4, 0, 0.2, 1);
  touch-action: manipulation;
  flex-shrink: 0;
  box-shadow: 0 4px 16px var(--accent-glow), inset 0 1px 0 rgba(255,255,255,0.2);
}

.submit-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 20px var(--accent-glow), inset 0 1px 0 rgba(255,255,255,0.2);
}

.submit-btn:active {
  transform: scale(0.96);
  box-shadow: 0 2px 8px var(--accent-glow);
}

/* ── Multi Input ── */
.multi-input {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.input-group {
  display: flex;
  align-items: center;
  gap: 10px;
}

.input-group label {
  font-family: var(--font-mono);
  font-size: 16px;
  font-weight: 700;
  color: var(--accent);
  min-width: 28px;
}

.input-group input {
  flex: 1;
  padding: 13px 16px;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 18px;
  font-weight: 600;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.input-group input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px var(--accent-glow);
}

.input-group input::placeholder {
  color: var(--text-muted);
  font-weight: 400;
}

/* ── Fraction Input ── */
.fraction-input {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.frac-row {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
  justify-content: center;
}

.frac-group {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.frac-group input {
  width: 76px;
  padding: 11px 8px;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 18px;
  font-weight: 600;
  text-align: center;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.frac-group input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px var(--accent-glow);
}

.frac-group input::placeholder {
  color: var(--text-muted);
  font-weight: 400;
}

.frac-group .frac-line {
  width: 76px;
  height: 2px;
  background: var(--accent);
  border-radius: 1px;
}

.frac-or {
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

/* ── Feedback ── */
.feedback-area {
  min-height: 48px;
}

.feedback {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 13px 16px;
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 600;
  animation: feedbackIn 0.35s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes feedbackIn {
  from { opacity: 0; transform: translateY(-8px); }
  to { opacity: 1; transform: translateY(0); }
}

.feedback.correct {
  background: var(--success-bg);
  color: var(--success);
  border: 1px solid rgba(52, 211, 153, 0.25);
}

.feedback.wrong {
  background: var(--error-bg);
  color: var(--error);
  border: 1px solid rgba(248, 113, 113, 0.25);
}

.fb-icon {
  font-size: 18px;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  flex-shrink: 0;
}

.feedback.correct .fb-icon {
  background: var(--success);
  color: #fff;
  font-size: 13px;
}

.feedback.wrong .fb-icon {
  background: var(--error);
  color: #fff;
  font-size: 13px;
}

.fb-text strong { font-weight: 800; }

.fb-streak {
  margin-left: auto;
  font-size: 13px;
  color: var(--warning);
  background: var(--warning-bg);
  padding: 3px 8px;
  border-radius: 12px;
  border: 1px solid rgba(251, 191, 36, 0.2);
}

/* ── Hint ── */
.hint-area {
  animation: slideUp 0.35s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes slideUp {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

.hint-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--warning);
  border-radius: var(--radius);
  padding: 18px;
}

.hint-header {
  font-size: 14px;
  font-weight: 700;
  color: var(--warning);
  margin-bottom: 12px;
}

.hint-steps {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.hint-step {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text-dim);
  padding: 8px 12px;
  background: var(--bg);
  border-radius: var(--radius-xs);
  line-height: 1.6;
  display: flex;
  align-items: flex-start;
  gap: 10px;
}

.step-num {
  flex-shrink: 0;
  width: 20px;
  height: 20px;
  background: var(--warning-bg);
  color: var(--warning);
  border-radius: 50%;
  font-size: 11px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-main);
  margin-top: 1px;
}

.hint-dismiss {
  margin-top: 14px;
  width: 100%;
  padding: 11px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--accent-grad);
  color: #fff;
  font-family: var(--font-main);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
  touch-action: manipulation;
  box-shadow: 0 2px 12px var(--accent-glow);
}

.hint-dismiss:hover { transform: translateY(-1px); }
.hint-dismiss:active { transform: scale(0.97); }

/* ── Action Row ── */
.action-row {
  display: flex;
  gap: 8px;
  justify-content: center;
  padding-top: 2px;
}

.action-btn {
  padding: 10px 18px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text-dim);
  font-family: var(--font-main);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s cubic-bezier(0.4, 0, 0.2, 1);
  touch-action: manipulation;
}

.action-btn:hover {
  background: var(--surface-hover);
  color: var(--text);
  border-color: var(--border-light);
  transform: translateY(-1px);
}

.action-btn:active { transform: scale(0.96); }

/* ── Results Overlay ── */
.results-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,0.65);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  z-index: 100;
  animation: fadeIn 0.3s ease;
  padding: 20px;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.results-card {
  background: linear-gradient(160deg, var(--surface-2), var(--surface));
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 36px 30px 28px;
  text-align: center;
  max-width: 360px;
  width: 100%;
  box-shadow: 0 24px 70px rgba(0,0,0,0.45);
  animation: popIn 0.45s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  position: relative;
  overflow: hidden;
}

.results-card::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 4px;
  background: var(--accent-grad);
}

@keyframes popIn {
  from { opacity: 0; transform: scale(0.85) translateY(10px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

.results-icon {
  font-size: 52px;
  margin-bottom: 14px;
  animation: bounceIn 0.6s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@keyframes bounceIn {
  0% { opacity: 0; transform: scale(0.3); }
  50% { opacity: 1; transform: scale(1.15); }
  100% { transform: scale(1); }
}

.results-title {
  font-size: 24px;
  font-weight: 800;
  margin-bottom: 22px;
  color: var(--text);
  letter-spacing: -0.02em;
}

.results-stats {
  display: flex;
  flex-direction: column;
  gap: 7px;
  margin-bottom: 24px;
}

.rs-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  background: var(--bg);
  border-radius: var(--radius-sm);
  font-size: 14px;
  border: 1px solid var(--border);
}

.rs-row span:first-child { color: var(--text-dim); font-weight: 500; }
.rs-row span:last-child { font-weight: 700; color: var(--text); font-variant-numeric: tabular-nums; }

.results-btn {
  width: 100%;
  padding: 14px;
  border: none;
  border-radius: var(--radius-sm);
  font-family: var(--font-main);
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.15s cubic-bezier(0.4, 0, 0.2, 1);
  margin-bottom: 8px;
  touch-action: manipulation;
}

.results-btn:last-child { margin-bottom: 0; }

.results-btn.primary {
  background: var(--accent-grad);
  color: #fff;
  box-shadow: 0 4px 16px var(--accent-glow), inset 0 1px 0 rgba(255,255,255,0.2);
}

.results-btn.primary:hover { transform: translateY(-1px); box-shadow: 0 6px 22px var(--accent-glow); }
.results-btn.primary:active { transform: scale(0.97); }

.results-btn.secondary {
  background: var(--bg3);
  color: var(--text-dim);
  border: 1px solid var(--border);
}

.results-btn.secondary:hover { background: var(--surface-hover); color: var(--text); }

/* ── Responsive ── */
@media (max-width: 400px) {
  .problem-math { font-size: 24px; }
  .brand-name { font-size: 17px; }
  .mode-tab { font-size: 12px; padding: 8px 3px; }
  .game-area { padding: 16px 14px; }
  .problem-card { padding: 24px 18px 20px; }
  .stats-row { gap: 8px; }
  .stat { padding: 3px 8px; font-size: 13px; }
  .stat-val { font-size: 13px; }
  .bar-track { width: 72px; }
  .frac-group input { width: 64px; }
  .frac-group .frac-line { width: 64px; }
  .top-bar { padding: 12px 14px; }
  .progress-wrap { padding: 8px 14px 10px; }
}

@media (min-width: 641px) {
  #app {
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
  }
}

/* ── Scrollbar ── */
.game-area::-webkit-scrollbar { width: 5px; }
.game-area::-webkit-scrollbar-track { background: transparent; }
.game-area::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
.game-area::-webkit-scrollbar-thumb:hover { background: var(--border-light); }

/* ── Focus visibility ── */
button:focus-visible, input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

