/* ══════════════════════════════════════════════
   速算练习 - Speed Math App
   Aesthetic: Electric precision + game-like energy
   ══════════════════════════════════════════════ */

/* === Variables === */
:root {
  --bg: #0D0D1A;
  --bg2: #141428;
  --bg3: #1E1E36;
  --bg4: #2A2A45;
  --text: #EAEAF0;
  --text2: #8888AA;
  --text3: #555570;
  --accent: #00D4FF;
  --accent2: #7B61FF;
  --success: #00E59C;
  --danger: #FF4D6A;
  --warning: #FFB830;
  --border: #2A2A45;
  --card-radius: 16px;
  --kb-bottom: max(16px, env(safe-area-inset-bottom, 16px));
}

[data-light] {
  --bg: #F0F2F8;
  --bg2: #FFFFFF;
  --bg3: #E8EAF2;
  --bg4: #D0D4E0;
  --text: #1A1A2E;
  --text2: #6B7280;
  --text3: #9CA3AF;
  --accent: #4A6CF7;
  --accent2: #8B5CF6;
  --success: #10B981;
  --danger: #EF4444;
  --warning: #F59E0B;
  --border: #E0E4EC;
}

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

html { height: 100%; overscroll-behavior: none; }

body {
  font-family: 'Outfit', -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

#app { width: 100%; min-height: 100vh; min-height: 100dvh; position: relative; }

/* === Pages === */
.page {
  display: none;
  min-height: 100vh; min-height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
  position: relative;
}
.page.active { display: flex; flex-direction: column; }

/* ═══ HOME PAGE ═══ */

#page-home { overflow: auto; }

/* Dot grid background */
.home-bg-pattern {
  position: fixed;
  inset: 0;
  background-image: radial-gradient(circle, var(--border) 1px, transparent 1px);
  background-size: 24px 24px;
  opacity: 0.3;
  z-index: 0;
  pointer-events: none;
}

.home-content {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  min-height: 100vh; min-height: 100dvh;
  padding: 20px 18px;
  padding-top: max(20px, env(safe-area-inset-top, 20px));
}

/* Header */
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 28px;
}

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

.logo-icon {
  font-family: 'JetBrains Mono', monospace;
  font-size: 32px;
  font-weight: 700;
  color: var(--accent);
  background: var(--accent);
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 12px;
  color: var(--bg);
}

h1 {
  font-size: 28px;
  font-weight: 800;
  letter-spacing: -0.5px;
}

/* Theme toggle */
.icon-btn {
  width: 44px; height: 44px;
  border: none;
  border-radius: 12px;
  background: var(--bg3);
  color: var(--text2);
  font-size: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  position: relative;
  overflow: hidden;
}

.icon-btn .icon-moon { display: block; }
.icon-btn .icon-sun { display: none; }
[data-light] .icon-btn .icon-moon { display: none; }
[data-light] .icon-btn .icon-sun { display: block; }

.icon-btn:active { transform: scale(0.9); }

/* Card Grid */
.card-grid {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  align-content: center;
}

/* Cards */
.card {
  position: relative;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--card-radius);
  padding: 24px 16px 20px;
  text-align: center;
  cursor: pointer;
  transition: transform 0.15s, border-color 0.2s;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 140px;
  -webkit-touch-callout: none;
}

.card:active { transform: scale(0.97); }

/* Glow effect per card */
.card-glow {
  position: absolute;
  top: -30px; right: -30px;
  width: 100px; height: 100px;
  border-radius: 50%;
  filter: blur(40px);
  opacity: 0.15;
  transition: opacity 0.2s;
}

.card:active .card-glow { opacity: 0.3; }

.card:nth-child(1) .card-glow { background: #7B61FF; }
.card:nth-child(2) .card-glow { background: #00D4FF; }
.card:nth-child(3) .card-glow { background: #FF6B35; }
.card:nth-child(4) .card-glow { background: #00E59C; }
.card:nth-child(5) .card-glow { background: #FFB830; }
.card:nth-child(6) .card-glow { background: #FF4D6A; }
.card:nth-child(7) .card-glow { background: #00D4FF; }
.card:nth-child(8) .card-glow { background: #7B61FF; }

.card-badge {
  font-family: 'JetBrains Mono', monospace;
  font-size: 30px;
  font-weight: 700;
  color: var(--accent);
  margin-bottom: 10px;
  line-height: 1;
}

.card:nth-child(1) .card-badge { color: #7B61FF; }
.card:nth-child(2) .card-badge { color: #00D4FF; }
.card:nth-child(3) .card-badge { color: #FF6B35; }
.card:nth-child(4) .card-badge { color: #00E59C; }
.card:nth-child(5) .card-badge { color: #FFB830; }
.card:nth-child(6) .card-badge { color: #FF4D6A; }
.card:nth-child(7) .card-badge { color: #00D4FF; }
.card:nth-child(8) .card-badge { color: #7B61FF; }

.card h2 {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 4px;
}

.card p {
  font-size: 12px;
  color: var(--text2);
  font-weight: 400;
}

.card-arrow {
  position: absolute;
  bottom: 10px;
  right: 12px;
  font-size: 14px;
  color: var(--text3);
  opacity: 0;
  transform: translateX(-4px);
  transition: all 0.2s;
}

.card:active .card-arrow {
  opacity: 1;
  transform: translateX(0);
}

/* Footer */
.home-footer {
  padding: 20px 0;
  padding-bottom: max(20px, var(--kb-bottom));
  text-align: center;
}

/* ═══ MODULE PAGE ═══ */

.page-header-module {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 16px 12px;
  padding-top: max(16px, env(safe-area-inset-top, 16px));
  flex-shrink: 0;
}

.page-header-module h2 {
  font-size: 20px;
  font-weight: 700;
  flex: 1;
}

.btn-back {
  width: 40px; height: 40px;
  border: none;
  border-radius: 12px;
  background: var(--bg3);
  color: var(--text);
  font-size: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-touch-callout: none;
}

.btn-back:active { transform: scale(0.9); }

.module-tabs {
  display: flex;
  padding: 0 16px;
  gap: 0;
  border-bottom: 1px solid var(--border);
  position: relative;
  z-index: 1;
  flex-shrink: 0;
}

.tab {
  flex: 1;
  padding: 14px 0;
  border: none;
  background: none;
  font-family: 'Outfit', sans-serif;
  font-size: 15px;
  font-weight: 500;
  color: var(--text2);
  cursor: pointer;
  position: relative;
  transition: color 0.2s;
  -webkit-touch-callout: none;
}

.tab::after {
  content: '';
  position: absolute;
  bottom: -1px;
  left: 20%; right: 20%;
  height: 2px;
  background: var(--accent);
  border-radius: 1px;
  transform: scaleX(0);
  transition: transform 0.25s;
}

.tab.active {
  color: var(--text);
}

.tab.active::after {
  transform: scaleX(1);
}

.tab-content {
  flex: 1;
  padding: 16px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  min-height: 0;
}

.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* ═══ LEARN VIEWS ═══ */

.learn-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.learn-item {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 8px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 13px;
  text-align: center;
  transition: border-color 0.15s;
}

.learn-item:active {
  border-color: var(--accent);
}

.learn-item .formula { color: var(--text); }
.learn-item .result { color: var(--accent); }

.multiplier-selector select {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg2);
  color: var(--text);
  font-family: 'Outfit', sans-serif;
  font-size: 15px;
  -webkit-appearance: none;
  margin-bottom: 12px;
}

.formula-explanation {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--card-radius);
  padding: 16px;
  margin-bottom: 12px;
}

.formula-explanation h3 {
  font-size: 15px;
  font-weight: 600;
  color: var(--accent);
  margin-bottom: 10px;
}

.formula-step {
  font-family: 'JetBrains Mono', monospace;
  font-size: 12px;
  padding: 5px 0;
  color: var(--text2);
  line-height: 1.5;
  white-space: nowrap;
  overflow-x: auto;
}

.formula-example {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  font-size: 13px;
  line-height: 1.7;
  color: var(--text);
}

/* ═══ PRACTICE PAGE ═══ */

.practice-bg-glow {
  position: fixed;
  top: 40%; left: 50%;
  transform: translate(-50%, -50%);
  width: 200px; height: 200px;
  border-radius: 50%;
  background: var(--accent);
  filter: blur(120px);
  opacity: 0.06;
  pointer-events: none;
  z-index: 0;
}

#page-practice {
  background: var(--bg);
  max-height: 100dvh;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 300px;
}

.practice-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top, 12px));
  position: relative;
  z-index: 1;
}

.btn-back-sm {
  width: 36px; height: 36px;
  border: none;
  border-radius: 10px;
  background: var(--bg3);
  color: var(--text2);
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  -webkit-touch-callout: none;
}

.btn-back-sm:active { transform: scale(0.9); }

.progress-wrap {
  flex: 1;
}

#practice-progress {
  font-size: 13px;
  font-weight: 600;
  color: var(--text2);
}

.progress-track {
  height: 4px;
  background: var(--bg4);
  border-radius: 2px;
  margin-top: 6px;
  overflow: hidden;
}

#progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--accent2));
  border-radius: 2px;
  transition: width 0.3s ease;
  width: 0%;
}

.timer {
  font-family: 'JetBrains Mono', monospace;
  font-size: 14px;
  font-weight: 600;
  color: var(--text2);
  min-width: 50px;
  text-align: right;
}

/* Question area */
.question-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px 20px 8px;
  position: relative;
  z-index: 1;
  flex-shrink: 0;
}

.question-display {
  font-family: 'JetBrains Mono', monospace;
  font-size: 42px;
  font-weight: 700;
  margin-bottom: 24px;
  white-space: nowrap;
  text-align: center;
}

.question-display .num { color: var(--text); }
.question-display .op { color: var(--text3); margin: 0 8px; }
.question-display .op.eq { color: var(--accent); }

.answer-box {
  font-family: 'JetBrains Mono', monospace;
  font-size: 40px;
  font-weight: 700;
  padding: 14px 28px;
  background: var(--bg2);
  border: 2px solid var(--border);
  border-radius: 16px;
  min-width: 160px;
  text-align: center;
  transition: all 0.25s;
  position: relative;
}

.answer-box.shake {
  animation: shake 0.4s ease;
  border-color: var(--danger);
}

.answer-box.correct {
  border-color: var(--success);
  background: var(--success);
  color: var(--bg);
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  50% { transform: translateX(6px); }
  75% { transform: translateX(-3px); }
}

.cursor {
  animation: blink 0.8s step-end infinite;
  color: var(--accent);
}

@keyframes blink {
  50% { opacity: 0; }
}

.practice-footer {
  display: flex;
  gap: 12px;
  justify-content: center;
  padding: 8px 16px;
  flex-shrink: 0;
  position: relative;
  z-index: 2;
}

/* ═══ KEYBOARD ═══ */

#keyboard-container {
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 480px;
  background: var(--bg3);
  border-top: 1px solid var(--border);
  padding: 10px 8px;
  padding-bottom: max(10px, var(--kb-bottom));
  z-index: 100;
}

.kb-inner {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.kb-row {
  display: flex;
  gap: 8px;
  justify-content: center;
}

.kb-key {
  flex: 1;
  height: 56px;
  border: none;
  border-radius: 12px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 24px;
  font-weight: 600;
  background: var(--bg4);
  color: var(--text);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: transform 0.08s;
}

.kb-key:active {
  transform: scale(0.93);
  background: var(--bg2);
}

.kb-key.key-del {
  background: var(--danger);
  color: var(--bg);
  font-size: 22px;
}

.kb-key.key-submit {
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: var(--bg);
  font-size: 26px;
}

.kb-key.key-submit:active {
  opacity: 0.85;
}

/* ═══ RESULTS PAGE ═══ */

.results-bg-glow {
  position: fixed;
  top: 30%; left: 50%;
  transform: translate(-50%, -50%);
  width: 200px; height: 200px;
  border-radius: 50%;
  background: var(--success);
  filter: blur(120px);
  opacity: 0.05;
  pointer-events: none;
  z-index: 0;
}

.page-header-results {
  padding: 20px 16px 12px;
  padding-top: max(20px, env(safe-area-inset-top, 20px));
  position: relative;
  z-index: 1;
}

.page-header-results h2 {
  font-size: 22px;
  font-weight: 700;
}

.summary-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  padding: 0 16px 16px;
}

.stat {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 6px;
  text-align: center;
  display: flex;
  flex-direction: column;
}

.stat-num {
  font-family: 'JetBrains Mono', monospace;
  font-size: 22px;
  font-weight: 700;
}

.stat-label {
  font-size: 10px;
  color: var(--text2);
  margin-top: 4px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.stat-success .stat-num { color: var(--success); }

.results-table-wrap {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  margin: 0 16px 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
}

.results-table-wrap table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}

.results-table-wrap th {
  background: var(--bg3);
  font-size: 10px;
  font-weight: 600;
  color: var(--text2);
  padding: 10px 6px;
  text-align: center;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.results-table-wrap td {
  padding: 10px 6px;
  text-align: center;
  border-bottom: 1px solid var(--border);
  font-family: 'JetBrains Mono', monospace;
  white-space: nowrap;
}

.results-table-wrap td.ok { color: var(--success); }
.results-table-wrap td.no { color: var(--danger); }

.results-table-wrap tr:last-child td { border-bottom: none; }

.results-actions {
  display: flex;
  gap: 10px;
  padding: 12px 16px;
  padding-bottom: max(12px, var(--kb-bottom));
}

/* ═══ HISTORY PAGE ═══ */

.history-list {
  flex: 1;
  padding: 8px 16px 16px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.history-item {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
}

.history-top {
  display: flex;
  justify-content: space-between;
  margin-bottom: 6px;
}

.history-mod {
  font-weight: 600;
  font-size: 14px;
}

.history-date {
  font-size: 12px;
  color: var(--text2);
}

.history-stats {
  display: flex;
  gap: 14px;
  font-size: 12px;
  color: var(--text2);
  flex-wrap: wrap;
}

.history-empty {
  text-align: center;
  color: var(--text3);
  padding: 60px 20px;
  font-size: 15px;
}

/* ═══ BUTTONS ═══ */

.btn-ghost {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 10px 20px;
  border: none;
  background: none;
  color: var(--text2);
  font-family: 'Outfit', sans-serif;
  font-size: 14px;
  cursor: pointer;
  border-radius: 10px;
  transition: all 0.15s;
  -webkit-touch-callout: none;
}

.btn-ghost:active {
  background: var(--bg3);
  color: var(--text);
}

.btn-ghost-sm {
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg2);
  color: var(--text2);
  font-family: 'Outfit', sans-serif;
  font-size: 13px;
  cursor: pointer;
  -webkit-touch-callout: none;
}

.btn-ghost-sm:active {
  transform: scale(0.95);
  background: var(--bg3);
}

.btn-fill {
  flex: 1;
  padding: 14px;
  border: none;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: var(--bg);
  font-family: 'Outfit', sans-serif;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  -webkit-touch-callout: none;
}

.btn-fill:active { transform: scale(0.97); opacity: 0.85; }

/* ═══ PRACTICE SETUP ═══ */

.practice-setup {
  text-align: center;
  padding: 20px 0;
}

.practice-setup h3 {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 16px;
  color: var(--text2);
}

.count-select {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
  margin-bottom: 20px;
}

.count-btn {
  padding: 10px 18px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--bg2);
  color: var(--text);
  font-family: 'Outfit', sans-serif;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.15s;
  -webkit-touch-callout: none;
}

.count-btn:active { transform: scale(0.95); }

.count-btn.active {
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: var(--bg);
  border-color: transparent;
}

.btn-start {
  padding: 14px 40px;
  border: none;
  border-radius: 14px;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: var(--bg);
  font-family: 'Outfit', sans-serif;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  -webkit-touch-callout: none;
}

.btn-start:active { transform: scale(0.97); opacity: 0.85; }

/* ═══ RESPONSIVE ═══ */

@media (max-width: 360px) {
  .question-display { font-size: 34px; }
  .answer-box { font-size: 32px; min-width: 140px; padding: 12px 20px; }
  .card { min-height: 120px; padding: 20px 12px 16px; }
  .card-badge { font-size: 30px; }
  .stat-num { font-size: 18px; }
  h1 { font-size: 24px; }
}

@media (min-width: 768px) {
  #app { max-width: 480px; margin: 0 auto; }
}

/* ═══ UPDATE TOAST ═══ */

#update-toast {
  position: fixed;
  top: max(12px, env(safe-area-inset-top, 12px));
  left: 50%;
  transform: translateX(-50%);
  z-index: 200;
  width: 90%;
  max-width: 360px;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: var(--bg);
  padding: 12px 16px;
  border-radius: 14px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.3);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  font-family: 'Outfit', sans-serif;
  font-size: 14px;
  font-weight: 600;
  animation: toastIn 0.3s ease;
}

@keyframes toastIn {
  from { opacity: 0; transform: translateX(-50%) translateY(-20px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

.btn-update {
  padding: 6px 16px;
  border: none;
  border-radius: 8px;
  background: var(--bg);
  color: var(--accent);
  font-family: 'Outfit', sans-serif;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  -webkit-touch-callout: none;
}

.btn-update:active { transform: scale(0.95); opacity: 0.8; }
