/* ===== 基础重置 ===== */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --color-bg: #f0f4ff;
  --color-surface: #ffffff;
  --color-primary: #5b8def;
  --color-primary-dark: #4a7de0;
  --color-accent: #ff8a65;
  --color-success: #4caf87;
  --color-danger: #ef5a5a;
  --color-warning: #ffa726;
  --color-text: #2d3748;
  --color-text-secondary: #718096;
  --color-border: #e2e8f0;
  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.06);
  --shadow-md: 0 4px 20px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 8px 30px rgba(0, 0, 0, 0.15);
  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 20px;
}

html {
  font-size: 16px;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  background: linear-gradient(135deg, #e8f0fe 0%, #f3e8ff 100%);
  color: var(--color-text);
  min-height: 100vh;
  overflow-x: hidden;
  user-select: none;
  -webkit-user-select: none;
}

/* ===== 顶部导航 ===== */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 24px;
  background: var(--color-surface);
  box-shadow: var(--shadow-sm);
  position: sticky;
  top: 0;
  z-index: 100;
}

.app-title {
  font-size: 1.4rem;
  font-weight: 700;
  background: linear-gradient(135deg, var(--color-primary), #a78bfa);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.header-right {
  display: flex;
  gap: 8px;
}

.icon-btn {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 42px;
  height: 42px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--color-bg);
  color: var(--color-text-secondary);
  cursor: pointer;
  transition: all 0.2s;
}

.icon-btn:hover {
  background: #e0e8ff;
  color: var(--color-primary);
}

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

.badge {
  position: absolute;
  top: 2px;
  right: 2px;
  min-width: 18px;
  height: 18px;
  padding: 0 4px;
  border-radius: 9px;
  background: var(--color-danger);
  color: #fff;
  font-size: 0.7rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

/* ===== 主区域 ===== */
.app-main {
  max-width: 960px;
  margin: 0 auto;
  padding: 16px 20px 40px;
}

/* ===== 进度条 ===== */
.progress-bar-container {
  margin-bottom: 20px;
}

.progress-info {
  display: flex;
  justify-content: space-between;
  font-size: 0.85rem;
  color: var(--color-text-secondary);
  margin-bottom: 6px;
  font-weight: 600;
}

.progress-bar {
  height: 8px;
  background: #e8edf5;
  border-radius: 4px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--color-primary), #a78bfa);
  border-radius: 4px;
  width: 10%;
  transition: width 0.4s ease;
}

/* ===== 内容区 ===== */
.content-area {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

/* ===== 题目区域 ===== */
.problem-section {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.problem-card {
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  padding: 32px 24px;
  text-align: center;
  box-shadow: var(--shadow-sm);
  border: 2px solid transparent;
  transition: border-color 0.3s;
}

.problem-card.correct {
  border-color: var(--color-success);
}

.problem-card.wrong {
  border-color: var(--color-danger);
}

.problem-number {
  font-size: 0.85rem;
  color: var(--color-text-secondary);
  margin-bottom: 12px;
  font-weight: 600;
}

.problem-expression {
  font-size: 2.6rem;
  font-weight: 800;
  color: var(--color-text);
  letter-spacing: 0.05em;
  font-variant-numeric: tabular-nums;
  min-height: 4rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ===== 答案区域 ===== */
.answer-section {
  background: var(--color-surface);
  border-radius: var(--radius-md);
  padding: 16px 20px;
  box-shadow: var(--shadow-sm);
}

.answer-label {
  display: block;
  font-size: 0.8rem;
  color: var(--color-text-secondary);
  margin-bottom: 8px;
  font-weight: 600;
}

.answer-display {
  font-size: 2.2rem;
  font-weight: 700;
  color: var(--color-primary);
  min-height: 3rem;
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 3px dashed var(--color-border);
  font-variant-numeric: tabular-nums;
  transition: color 0.3s;
}

.answer-display.has-value {
  color: var(--color-text);
}

.answer-display.correct {
  color: var(--color-success);
}

.answer-display.wrong {
  color: var(--color-danger);
}

.answer-display .cursor-blink {
  display: inline-block;
  width: 2px;
  height: 2rem;
  background: var(--color-primary);
  margin-left: 4px;
  animation: blink 1s steps(2) infinite;
}

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

/* ===== 键盘 ===== */
.keypad {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 12px;
}

.keypad-numbers {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

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

.key {
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 1.5rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.15s;
  min-height: 52px;
  font-variant-numeric: tabular-nums;
}

.key:active {
  transform: scale(0.93);
}

.key-num {
  background: var(--color-surface);
  color: var(--color-text);
  box-shadow: var(--shadow-sm);
}

.key-num:hover {
  background: #f0f6ff;
}

.key-zero {
  grid-column: span 3;
}

.key-minus {
  background: #fff3e0;
  color: var(--color-accent);
  font-size: 1.3rem;
}

.key-back {
  background: #fff3e0;
  color: var(--color-accent);
}

.key-clear {
  background: #ffebee;
  color: var(--color-danger);
  font-size: 1.3rem;
}

.key-submit {
  background: linear-gradient(135deg, var(--color-primary), var(--color-primary-dark));
  color: #fff;
  flex: 1;
}

.key-submit:hover {
  filter: brightness(1.08);
}

/* ===== 草稿纸 ===== */
.scratch-section {
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.scratch-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--color-border);
}

.scratch-title {
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--color-text-secondary);
}

.scratch-tools {
  display: flex;
  gap: 6px;
}

.tool-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--color-bg);
  color: var(--color-text-secondary);
  cursor: pointer;
  transition: all 0.15s;
}

.tool-btn:hover {
  background: #e0e8ff;
  color: var(--color-primary);
}

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

.scratch-canvas {
  flex: 1;
  width: 100%;
  background-color: #fffef7;
  background-image:
    linear-gradient(rgba(91, 141, 239, 0.08) 1px, transparent 1px),
    linear-gradient(90deg, rgba(91, 141, 239, 0.08) 1px, transparent 1px);
  background-size: 24px 24px;
  cursor: crosshair;
  touch-action: none;
  min-height: 280px;
}

/* ===== 模态框 ===== */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 16px;
}

.modal {
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  width: 100%;
  max-width: 420px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
  animation: modalIn 0.25s ease;
}

.modal-large {
  max-width: 640px;
}

@keyframes modalIn {
  from {
    opacity: 0;
    transform: translateY(20px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px;
  border-bottom: 1px solid var(--color-border);
}

.modal-header h2 {
  font-size: 1.2rem;
  font-weight: 700;
}

.modal-header-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.close-btn {
  border: none;
  background: none;
  font-size: 1.6rem;
  color: var(--color-text-secondary);
  cursor: pointer;
  padding: 0 4px;
  line-height: 1;
}

.close-btn:hover {
  color: var(--color-text);
}

.modal-body {
  padding: 24px;
}

.modal-footer {
  display: flex;
  gap: 12px;
  padding: 16px 24px 24px;
  justify-content: flex-end;
}

/* ===== 设置面板 ===== */
.setting-group {
  margin-bottom: 20px;
}

.setting-label {
  display: block;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--color-text);
  margin-bottom: 10px;
}

.preset-buttons {
  display: flex;
  gap: 8px;
}

.preset-btn {
  flex: 1;
  padding: 10px;
  border: 2px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-surface);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  color: var(--color-text-secondary);
}

.preset-btn:hover {
  border-color: var(--color-primary);
}

.preset-btn.active {
  border-color: var(--color-primary);
  background: var(--color-primary);
  color: #fff;
}

.slider {
  width: 100%;
  height: 6px;
  border-radius: 3px;
  background: #e8edf5;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}

.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--color-primary);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
}

.slider::-moz-range-thumb {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--color-primary);
  cursor: pointer;
  border: none;
  box-shadow: var(--shadow-sm);
}

.range-inputs {
  display: flex;
  align-items: center;
  gap: 12px;
}

.range-input {
  width: 80px;
  padding: 8px 12px;
  border: 2px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-size: 1rem;
  text-align: center;
  font-weight: 600;
  outline: none;
  transition: border-color 0.2s;
}

.range-input:focus {
  border-color: var(--color-primary);
}

.range-separator {
  font-size: 1.1rem;
  color: var(--color-text-secondary);
}

.checkbox-group,
.radio-group {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
}

.checkbox-label,
.radio-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.95rem;
  cursor: pointer;
  color: var(--color-text);
}

.checkbox-label input,
.radio-label input {
  width: 18px;
  height: 18px;
  accent-color: var(--color-primary);
  cursor: pointer;
}

/* ===== 按钮 ===== */
.btn-primary,
.btn-secondary {
  padding: 10px 24px;
  border-radius: var(--radius-sm);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  border: none;
}

.btn-primary {
  background: var(--color-primary);
  color: #fff;
}

.btn-primary:hover {
  background: var(--color-primary-dark);
}

.btn-secondary {
  background: var(--color-bg);
  color: var(--color-text-secondary);
}

.btn-secondary:hover {
  background: #e0e8ff;
}

.btn-text-danger {
  border: none;
  background: none;
  color: var(--color-danger);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  padding: 4px 8px;
}

.btn-text-danger:hover {
  text-decoration: underline;
}

/* ===== 错题本 ===== */
.wrong-answers-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.wrong-answer-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  background: #fff5f5;
  border-radius: var(--radius-sm);
  border-left: 4px solid var(--color-danger);
}

.wrong-answer-expression {
  font-size: 1.2rem;
  font-weight: 700;
  color: var(--color-text);
  font-variant-numeric: tabular-nums;
}

.wrong-answer-answers {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.9rem;
  font-variant-numeric: tabular-nums;
}

.wrong-answer-user {
  color: var(--color-danger);
  text-decoration: line-through;
  font-weight: 600;
}

.wrong-answer-arrow {
  color: var(--color-text-secondary);
}

.wrong-answer-correct {
  color: var(--color-success);
  font-weight: 700;
}

.wrong-answer-date {
  font-size: 0.75rem;
  color: var(--color-text-secondary);
  margin-top: 4px;
}

.empty-tip {
  text-align: center;
  color: var(--color-text-secondary);
  padding: 40px 0;
  font-size: 1rem;
}

/* ===== 结果页 ===== */
.result-body {
  text-align: center;
  padding: 40px 24px;
}

.result-emoji {
  font-size: 4rem;
  margin-bottom: 12px;
}

.result-title {
  font-size: 1.5rem;
  margin-bottom: 24px;
}

.result-stats {
  display: flex;
  justify-content: center;
  gap: 32px;
  margin-bottom: 20px;
}

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

.stat-value {
  font-size: 2.2rem;
  font-weight: 800;
  color: var(--color-success);
  font-variant-numeric: tabular-nums;
}

.stat-value.stat-wrong {
  color: var(--color-danger);
}

.stat-label {
  font-size: 0.85rem;
  color: var(--color-text-secondary);
  font-weight: 600;
}

.result-time {
  font-size: 0.85rem;
  color: var(--color-text-secondary);
  margin-bottom: 24px;
}

.result-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
}

/* ===== 反馈提示 ===== */
.feedback-toast {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 20px 40px;
  border-radius: var(--radius-lg);
  font-size: 2rem;
  font-weight: 700;
  z-index: 300;
  animation: toastIn 0.3s ease;
  pointer-events: none;
}

.feedback-toast.correct {
  background: var(--color-success);
  color: #fff;
}

.feedback-toast.wrong {
  background: var(--color-danger);
  color: #fff;
}

@keyframes toastIn {
  from {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.7);
  }
  to {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
  }
}

/* ===== 抖动动画 ===== */
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-6px); }
  40%, 80% { transform: translateX(6px); }
}

.shake {
  animation: shake 0.4s ease;
}

/* ===== 响应式 ===== */
@media (max-width: 720px) {
  .content-area {
    grid-template-columns: 1fr;
  }

  .problem-expression {
    font-size: 2rem;
  }

  .scratch-canvas {
    min-height: 200px;
  }

  .key {
    min-height: 48px;
    font-size: 1.3rem;
  }

  .app-title {
    font-size: 1.2rem;
  }

  .result-stats {
    gap: 20px;
  }

  .stat-value {
    font-size: 1.8rem;
  }
}
