:root {
  --primary: #1565C0;
  --primary-light: #1976D2;
  --primary-dark: #0D47A1;
  --accent: #42A5F5;
  --bg: #F0F4F9;
  --card: #FFFFFF;
  --text: #1A1A2E;
  --text-secondary: #64748B;
  --text-light: #94A3B8;
  --success: #16A34A;
  --success-bg: #DCFCE7;
  --error: #DC2626;
  --error-bg: #FEE2E2;
  --warning: #D97706;
  --warning-bg: #FEF3C7;
  --border: #E2E8F0;
  --shadow: 0 2px 8px rgba(0,0,0,0.08);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.12);
  --radius: 12px;
  --radius-sm: 8px;
  --nav-height: 72px;
  --header-height: 60px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, 'PingFang SC', 'Microsoft YaHei', system-ui, sans-serif;
  font-size: 15px;
  color: var(--text);
  background: var(--bg);
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

#view-container {
  flex: 1;
  overflow: hidden;
  position: relative;
}

.view {
  position: absolute;
  inset: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--bg);
  padding-bottom: calc(var(--nav-height) + var(--safe-bottom));
}

/* ===== BOTTOM NAV ===== */
#bottom-nav {
  display: flex;
  align-items: center;
  justify-content: space-around;
  background: var(--card);
  border-top: 1px solid var(--border);
  height: calc(var(--nav-height) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  flex-shrink: 0;
  box-shadow: 0 -2px 12px rgba(0,0,0,0.06);
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  flex: 1;
  padding: 8px 4px;
  cursor: pointer;
  color: var(--text-light);
  transition: color 0.2s;
  background: none;
  border: none;
  font-family: inherit;
}

.nav-item.active {
  color: var(--primary);
}

.nav-item .nav-icon {
  font-size: 22px;
  line-height: 1;
}

.nav-item .nav-label {
  font-size: 11px;
  font-weight: 500;
}

.nav-scan {
  position: relative;
  flex: 1.2;
}

.nav-scan .scan-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: var(--primary);
  border: 3px solid var(--card);
  box-shadow: 0 4px 16px rgba(21,101,192,0.4);
  color: white;
  cursor: pointer;
  font-family: inherit;
  margin-top: -20px;
  justify-content: center;
  transition: transform 0.2s, box-shadow 0.2s;
}

.nav-scan .scan-btn:active {
  transform: scale(0.95);
  box-shadow: 0 2px 8px rgba(21,101,192,0.3);
}

.nav-scan .scan-btn .nav-icon {
  font-size: 24px;
}

.nav-scan .nav-label {
  font-size: 11px;
  color: var(--text-secondary);
}

/* ===== HEADER ===== */
.view-header {
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
  color: white;
  padding: 16px 20px;
  padding-top: calc(16px + env(safe-area-inset-top, 0px));
  position: sticky;
  top: 0;
  z-index: 10;
}

.view-header h1 {
  font-size: 20px;
  font-weight: 700;
}

.view-header p {
  font-size: 13px;
  opacity: 0.85;
  margin-top: 2px;
}

.header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

/* ===== CARDS ===== */
.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin: 12px 16px;
}

.card-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 12px;
}

/* ===== HOME VIEW ===== */
.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  padding: 16px;
}

.stat-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  text-align: center;
}

.stat-value {
  font-size: 32px;
  font-weight: 800;
  color: var(--primary);
  line-height: 1;
}

.stat-label {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 4px;
}

.home-hero {
  background: linear-gradient(135deg, var(--primary) 0%, #1E88E5 100%);
  margin: 0;
  padding: 24px 20px 28px;
  color: white;
}

.home-hero h1 {
  font-size: 24px;
  font-weight: 800;
  margin-bottom: 4px;
}

.home-hero p {
  font-size: 14px;
  opacity: 0.85;
}

.home-quick-scan {
  display: flex;
  align-items: center;
  gap: 12px;
  background: rgba(255,255,255,0.15);
  border-radius: var(--radius);
  padding: 12px 16px;
  margin-top: 16px;
  cursor: pointer;
  transition: background 0.2s;
}

.home-quick-scan:active {
  background: rgba(255,255,255,0.25);
}

.quick-scan-icon {
  width: 44px;
  height: 44px;
  background: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
}

.quick-scan-text h3 {
  font-size: 15px;
  font-weight: 600;
  color: white;
}

.quick-scan-text p {
  font-size: 12px;
  color: rgba(255,255,255,0.8);
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 16px 8px;
}

.section-title {
  font-size: 16px;
  font-weight: 700;
}

.section-action {
  font-size: 13px;
  color: var(--primary);
  cursor: pointer;
}

.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-secondary);
}

.empty-state .empty-icon {
  font-size: 48px;
  margin-bottom: 12px;
}

.empty-state h3 {
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 6px;
}

.empty-state p {
  font-size: 14px;
}

/* ===== SCAN VIEW ===== */
.scan-container {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.camera-area {
  position: relative;
  flex: 1;
  background: #000;
  overflow: hidden;
}

.camera-area video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.camera-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.scan-frame {
  width: 85%;
  max-width: 320px;
  aspect-ratio: 4/3;
  border: 2px solid rgba(255,255,255,0.7);
  border-radius: 12px;
  position: relative;
}

.scan-frame::before,
.scan-frame::after {
  content: '';
  position: absolute;
  width: 24px;
  height: 24px;
  border-color: white;
  border-style: solid;
}

.scan-frame::before {
  top: -2px;
  left: -2px;
  border-width: 3px 0 0 3px;
  border-radius: 10px 0 0 0;
}

.scan-frame::after {
  bottom: -2px;
  right: -2px;
  border-width: 0 3px 3px 0;
  border-radius: 0 0 10px 0;
}

.scan-hint {
  color: rgba(255,255,255,0.9);
  font-size: 13px;
  margin-top: 16px;
  text-shadow: 0 1px 3px rgba(0,0,0,0.5);
}

.camera-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 32px;
  padding: 24px 20px;
  background: #000;
}

.capture-btn {
  width: 70px;
  height: 70px;
  border-radius: 50%;
  border: 4px solid white;
  background: white;
  cursor: pointer;
  position: relative;
  transition: transform 0.15s;
}

.capture-btn::after {
  content: '';
  position: absolute;
  inset: 4px;
  border-radius: 50%;
  background: white;
  transition: background 0.15s;
}

.capture-btn:active {
  transform: scale(0.93);
}

.capture-btn:active::after {
  background: #ccc;
}

.ctrl-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: rgba(255,255,255,0.15);
  border: none;
  color: white;
  font-size: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
}

.ctrl-btn:active {
  background: rgba(255,255,255,0.3);
}

.upload-fallback {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  padding: 40px 20px;
  min-height: 60vh;
}

.upload-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 28px 48px;
  border: 2px dashed var(--primary);
  border-radius: var(--radius);
  cursor: pointer;
  color: var(--primary);
  background: rgba(21,101,192,0.04);
  transition: background 0.2s;
  width: 100%;
  max-width: 320px;
}

.upload-btn:active {
  background: rgba(21,101,192,0.1);
}

.upload-btn .upload-icon {
  font-size: 40px;
}

.upload-btn span {
  font-size: 15px;
  font-weight: 600;
}

.upload-or {
  font-size: 14px;
  color: var(--text-light);
  position: relative;
}

.upload-or::before,
.upload-or::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 60px;
  height: 1px;
  background: var(--border);
}

.upload-or::before { right: 100%; margin-right: 12px; }
.upload-or::after { left: 100%; margin-left: 12px; }

/* ===== PREVIEW ===== */
.preview-container {
  display: flex;
  flex-direction: column;
  height: 100%;
  background: #000;
}

.preview-container img {
  flex: 1;
  object-fit: contain;
  width: 100%;
}

.preview-actions {
  display: flex;
  gap: 12px;
  padding: 20px;
  background: #000;
}

.preview-actions button {
  flex: 1;
  padding: 14px;
  border-radius: var(--radius);
  font-size: 15px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  border: none;
  transition: transform 0.15s;
}

.preview-actions button:active {
  transform: scale(0.97);
}

.btn-retake {
  background: rgba(255,255,255,0.15);
  color: white;
}

.btn-analyze {
  background: var(--primary);
  color: white;
}

/* ===== LOADING VIEW ===== */
.loading-view {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 70vh;
  padding: 40px 20px;
  gap: 24px;
}

.loading-spinner {
  width: 60px;
  height: 60px;
  border: 4px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.loading-title {
  font-size: 18px;
  font-weight: 700;
  text-align: center;
}

.loading-subtitle {
  font-size: 14px;
  color: var(--text-secondary);
  text-align: center;
  max-width: 260px;
}

.loading-tips {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  width: 100%;
  max-width: 320px;
}

.loading-tips p {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.6;
  text-align: center;
}

/* ===== RESULTS VIEW ===== */
.results-header {
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
  padding: 20px;
  color: white;
  text-align: center;
}

.score-circle {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 16px;
}

.score-ring {
  position: relative;
  width: 100px;
  height: 100px;
}

.score-ring svg {
  transform: rotate(-90deg);
}

.score-ring circle {
  fill: none;
  stroke-width: 8;
}

.score-ring .track {
  stroke: rgba(255,255,255,0.2);
}

.score-ring .fill {
  stroke: white;
  stroke-linecap: round;
  transition: stroke-dasharray 1s ease;
}

.score-number {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.score-number .score-val {
  font-size: 28px;
  font-weight: 800;
  line-height: 1;
}

.score-number .score-unit {
  font-size: 11px;
  opacity: 0.8;
}

.score-stats {
  display: flex;
  gap: 24px;
  justify-content: center;
}

.score-stat {
  text-align: center;
}

.score-stat .val {
  font-size: 20px;
  font-weight: 700;
}

.score-stat .lbl {
  font-size: 11px;
  opacity: 0.8;
}

.feedback-banner {
  background: rgba(255,255,255,0.15);
  border-radius: var(--radius-sm);
  padding: 10px 16px;
  margin: 12px 20px 0;
  font-size: 13px;
  text-align: center;
}

/* ===== PROBLEM ITEM ===== */
.problem-item {
  background: var(--card);
  border-radius: var(--radius);
  margin: 10px 16px;
  overflow: hidden;
  box-shadow: var(--shadow);
}

.problem-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}

.problem-status {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 700;
  flex-shrink: 0;
}

.problem-status.correct {
  background: var(--success-bg);
  color: var(--success);
}

.problem-status.wrong {
  background: var(--error-bg);
  color: var(--error);
}

.problem-num {
  font-size: 13px;
  color: var(--text-secondary);
  font-weight: 500;
}

.problem-type-badge {
  margin-left: auto;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 20px;
  background: var(--bg);
  color: var(--text-secondary);
}

.problem-body {
  padding: 12px 14px;
}

.problem-question {
  font-size: 14px;
  line-height: 1.6;
  margin-bottom: 10px;
}

.answer-row {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  font-size: 13px;
  margin-bottom: 4px;
}

.answer-label {
  color: var(--text-secondary);
  flex-shrink: 0;
  font-weight: 500;
}

.answer-val.wrong-answer {
  color: var(--error);
  text-decoration: line-through;
}

.answer-val.correct-answer {
  color: var(--success);
  font-weight: 600;
}

.problem-explanation {
  background: #FFF8E1;
  border-left: 3px solid var(--warning);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  padding: 10px 12px;
  margin-top: 10px;
  font-size: 13px;
  line-height: 1.6;
  color: #5D4037;
}

.problem-explanation strong {
  display: block;
  margin-bottom: 4px;
  color: var(--warning);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.results-actions {
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* ===== NOTEBOOK VIEW ===== */
.notebook-filter {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 12px 16px;
  scrollbar-width: none;
}

.notebook-filter::-webkit-scrollbar { display: none; }

.filter-chip {
  flex-shrink: 0;
  padding: 6px 14px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  border: 1.5px solid var(--border);
  background: var(--card);
  color: var(--text-secondary);
  transition: all 0.2s;
}

.filter-chip.active {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}

.notebook-item {
  background: var(--card);
  border-radius: var(--radius);
  margin: 8px 16px;
  padding: 14px;
  box-shadow: var(--shadow);
  display: flex;
  gap: 12px;
  align-items: flex-start;
}

.nb-wrong-mark {
  width: 32px;
  height: 32px;
  background: var(--error-bg);
  color: var(--error);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: 700;
  flex-shrink: 0;
}

.nb-content {
  flex: 1;
  min-width: 0;
}

.nb-question {
  font-size: 14px;
  line-height: 1.5;
  margin-bottom: 6px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.nb-meta {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.nb-type-badge {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 20px;
  background: rgba(21,101,192,0.1);
  color: var(--primary);
}

.nb-date {
  font-size: 11px;
  color: var(--text-light);
}

.nb-actions {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex-shrink: 0;
}

.nb-btn {
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  font-size: 12px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  border: none;
  transition: transform 0.15s;
}

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

.nb-btn-practice {
  background: var(--primary);
  color: white;
}

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

/* ===== PRACTICE VIEW ===== */
.practice-header {
  background: linear-gradient(135deg, #2E7D32 0%, #388E3C 100%);
  padding: 20px;
  color: white;
}

.practice-header h2 {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 4px;
}

.practice-header p {
  font-size: 13px;
  opacity: 0.85;
}

.practice-problem {
  background: var(--card);
  border-radius: var(--radius);
  margin: 12px 16px;
  overflow: hidden;
  box-shadow: var(--shadow);
}

.practice-num {
  background: var(--primary);
  color: white;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
}

.practice-body {
  padding: 14px;
}

.practice-question {
  font-size: 15px;
  line-height: 1.6;
  margin-bottom: 12px;
  font-weight: 500;
}

.practice-hint {
  font-size: 12px;
  color: var(--warning);
  background: var(--warning-bg);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  margin-bottom: 12px;
}

.practice-input {
  width: 100%;
  padding: 12px 14px;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.2s;
  background: var(--bg);
}

.practice-input:focus {
  border-color: var(--primary);
  background: white;
}

.practice-result-box {
  margin-top: 12px;
  border-radius: var(--radius-sm);
  padding: 12px;
  font-size: 14px;
  line-height: 1.6;
}

.practice-result-box.correct {
  background: var(--success-bg);
  border-left: 3px solid var(--success);
  color: #166534;
}

.practice-result-box.wrong {
  background: var(--error-bg);
  border-left: 3px solid var(--error);
  color: #991B1B;
}

.practice-result-box .result-header {
  font-weight: 700;
  margin-bottom: 6px;
  font-size: 15px;
}

/* ===== BUTTONS ===== */
.btn {
  display: block;
  width: 100%;
  padding: 14px;
  border-radius: var(--radius);
  font-size: 15px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  border: none;
  text-align: center;
  transition: transform 0.15s, box-shadow 0.15s;
}

.btn:active { transform: scale(0.98); }

.btn-primary {
  background: var(--primary);
  color: white;
  box-shadow: 0 4px 12px rgba(21,101,192,0.3);
}

.btn-primary:active {
  box-shadow: 0 2px 6px rgba(21,101,192,0.2);
}

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

.btn-success {
  background: var(--success);
  color: white;
  box-shadow: 0 4px 12px rgba(22,163,74,0.3);
}

.btn-outline {
  background: transparent;
  color: var(--primary);
  border: 2px solid var(--primary);
}

/* ===== TOAST ===== */
.toast {
  position: fixed;
  bottom: calc(var(--nav-height) + 16px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,0.85);
  color: white;
  padding: 10px 20px;
  border-radius: 24px;
  font-size: 14px;
  z-index: 1000;
  white-space: nowrap;
  transition: opacity 0.3s;
  pointer-events: none;
}

.toast.hidden { opacity: 0; }

/* ===== MODAL ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 100;
  display: flex;
  align-items: flex-end;
  transition: opacity 0.3s;
}

.modal-overlay.hidden { opacity: 0; pointer-events: none; }

.modal-content {
  background: var(--card);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 20px;
  width: 100%;
  max-height: 80vh;
  overflow-y: auto;
}

/* ===== HISTORY ITEM ===== */
.history-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  background: var(--card);
  border-radius: var(--radius);
  margin: 8px 16px;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: transform 0.15s;
}

.history-item:active { transform: scale(0.98); }

.history-score-badge {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 800;
  flex-shrink: 0;
}

.score-high { background: var(--success-bg); color: var(--success); }
.score-mid { background: var(--warning-bg); color: var(--warning); }
.score-low { background: var(--error-bg); color: var(--error); }

.history-info {
  flex: 1;
}

.history-info h4 {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 2px;
}

.history-info p {
  font-size: 12px;
  color: var(--text-secondary);
}

.history-arrow {
  color: var(--text-light);
  font-size: 16px;
}

/* ===== UTILITY ===== */
.mt-16 { margin-top: 16px; }
.pb-16 { padding-bottom: 16px; }
.text-center { text-align: center; }
.section-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 16px 16px 6px;
}

/* Back button */
.back-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 15px;
  color: white;
  cursor: pointer;
  opacity: 0.9;
  margin-bottom: 10px;
  background: none;
  border: none;
  font-family: inherit;
  padding: 0;
}

.back-btn:active { opacity: 0.7; }

/* ===== PRACTICE COMPLETE ===== */
.practice-complete {
  text-align: center;
  padding: 32px 20px;
}

.practice-complete .complete-icon {
  font-size: 60px;
  margin-bottom: 12px;
}

.practice-complete h3 {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 6px;
}

.practice-complete p {
  font-size: 14px;
  color: var(--text-secondary);
  margin-bottom: 24px;
}

/* Smooth view transitions */
.view {
  animation: fadeIn 0.2s ease;
}

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