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

:root {
  --primary: #4A7CFF;
  --primary-light: #EEF2FF;
  --success: #22C55E;
  --success-light: #DCFCE7;
  --error: #EF4444;
  --error-light: #FEE2E2;
  --bg: #F5F7FA;
  --card: #FFFFFF;
  --text: #1A1D23;
  --text-secondary: #6B7280;
  --border: #E5E7EB;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.08);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.10);
  --radius: 16px;
  --radius-sm: 10px;
}

html, body {
  height: 100%;
  width: 100%;
  overflow: hidden;
  font-family: -apple-system, 'PingFang SC', 'Helvetica Neue', sans-serif;
  background: var(--bg);
  color: var(--text);
}

/* SCREENS */
.screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: env(safe-area-inset-top, 20px) 20px env(safe-area-inset-bottom, 20px);
  overflow-y: auto;
  opacity: 0;
  pointer-events: none;
  transform: translateY(16px);
  transition: opacity 0.25s ease, transform 0.25s ease;
}

.screen.active {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}

/* HOME */
.home-header {
  margin-top: 40px;
  text-align: center;
  margin-bottom: 36px;
}

.app-title {
  font-size: 34px;
  font-weight: 800;
  letter-spacing: -0.5px;
  background: linear-gradient(135deg, var(--primary), #8B5CF6);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.app-subtitle {
  font-size: 15px;
  color: var(--text-secondary);
  margin-top: 4px;
}

.section-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.8px;
  margin-bottom: 12px;
  text-align: left;
  width: 100%;
  max-width: 360px;
}

.size-selector {
  width: 100%;
  max-width: 360px;
  margin-bottom: 24px;
}

.size-buttons {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 4px;
  scrollbar-width: none;
}

.size-buttons::-webkit-scrollbar { display: none; }

.size-btn {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 12px 16px;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--card);
  cursor: pointer;
  transition: all 0.18s ease;
  touch-action: manipulation;
  min-width: 68px;
}

.size-btn.active {
  border-color: var(--primary);
  background: var(--primary-light);
}

.size-num {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
}

.size-btn.active .size-num {
  color: var(--primary);
}

.size-tag {
  font-size: 11px;
  color: var(--text-secondary);
  margin-top: 3px;
}

.size-btn.active .size-tag {
  color: var(--primary);
}

/* STATS CARD */
.stats-card {
  width: 100%;
  max-width: 360px;
  background: var(--card);
  border-radius: var(--radius);
  padding: 20px;
  display: flex;
  align-items: center;
  justify-content: space-around;
  box-shadow: var(--shadow-sm);
  margin-bottom: 28px;
}

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

.stat-value {
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
}

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

.stat-divider {
  width: 1px;
  height: 36px;
  background: var(--border);
}

/* BUTTONS */
.btn-start {
  width: 100%;
  max-width: 360px;
  padding: 17px;
  background: var(--primary);
  color: white;
  font-size: 17px;
  font-weight: 700;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.12s ease, opacity 0.12s ease;
  box-shadow: 0 4px 14px rgba(74, 124, 255, 0.4);
}

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

.btn-outline {
  width: 100%;
  max-width: 360px;
  padding: 15px;
  background: transparent;
  color: var(--primary);
  font-size: 16px;
  font-weight: 600;
  border: 2px solid var(--primary);
  border-radius: var(--radius);
  cursor: pointer;
  touch-action: manipulation;
  margin-top: 12px;
  transition: background 0.15s ease;
}

.btn-outline:active {
  background: var(--primary-light);
}

.tip-text {
  margin-top: 20px;
  font-size: 13px;
  color: var(--text-secondary);
  text-align: center;
}

/* GAME HEADER */
.game-header {
  width: 100%;
  max-width: 420px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: 8px;
  margin-bottom: 12px;
}

.btn-back {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--card);
  border: none;
  border-radius: 50%;
  cursor: pointer;
  color: var(--text);
  touch-action: manipulation;
  box-shadow: var(--shadow-sm);
}

.timer-display {
  font-size: 32px;
  font-weight: 800;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  letter-spacing: -1px;
  min-width: 90px;
  text-align: center;
}

.game-size-label {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
  background: var(--card);
  padding: 6px 12px;
  border-radius: 20px;
  box-shadow: var(--shadow-sm);
}

/* TARGET BAR */
.target-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}

.target-label {
  font-size: 16px;
  color: var(--text-secondary);
  font-weight: 500;
}

.target-number {
  font-size: 44px;
  font-weight: 900;
  color: var(--primary);
  line-height: 1;
  min-width: 60px;
  text-align: center;
  transition: transform 0.15s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.target-number.pop {
  transform: scale(1.3);
}

/* PROGRESS */
.btn-refresh {
  margin-top: 8px;
  margin-bottom: 0;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: var(--card);
  border: 2px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--text-secondary);
  box-shadow: var(--shadow-sm);
  touch-action: manipulation;
  transition: transform 0.2s ease, color 0.15s ease, border-color 0.15s ease, background 0.15s ease;
  flex-shrink: 0;
}

.btn-refresh:last-child { margin-bottom: 56px; }

.btn-refresh.active {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}

/* Speed slider */
.speed-slider {
  -webkit-appearance: none;
  appearance: none;
  width: 140px;
  height: 4px;
  border-radius: 2px;
  background: var(--border);
  outline: none;
  cursor: pointer;
}

.speed-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--primary);
  cursor: pointer;
  box-shadow: 0 1px 4px rgba(0,0,0,0.2);
}

.speed-slider::-moz-range-thumb {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--primary);
  cursor: pointer;
  border: none;
}

.speed-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  min-width: 28px;
  text-align: left;
}

.btn-refresh:active {
  color: var(--primary);
  border-color: var(--primary);
  transform: rotate(180deg);
}

.btn-nav-back {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  color: var(--text-secondary);
  font-size: 14px;
  font-weight: 500;
  text-decoration: none;
  padding: 6px 0;
  margin-bottom: 8px;
  touch-action: manipulation;
  transition: color 0.15s;
  align-self: flex-start;
}
.btn-nav-back:active { color: var(--primary); }

.progress-bar-wrap {
  width: 100%;
  max-width: 420px;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  margin-bottom: 14px;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), #8B5CF6);
  border-radius: 2px;
  width: 0%;
  transition: width 0.3s ease;
}

/* GRID */
.grid-wrap {
  width: 100%;
  max-width: 420px;
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.grid {
  display: grid;
  width: 100%;
  gap: 6px;
}

.cell {
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--card);
  border-radius: var(--radius-sm);
  font-weight: 800;
  cursor: pointer;
  touch-action: manipulation;
  box-shadow: var(--shadow-sm);
  border: 2px solid transparent;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
  user-select: none;
  -webkit-user-select: none;
}

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

.cell.zone-tl { background-color: rgba(74,124,255,0.10); }
.cell.zone-tr { background-color: rgba(245,158,11,0.10); }
.cell.zone-bl { background-color: rgba(34,197,94,0.10); }
.cell.zone-br { background-color: rgba(139,92,246,0.10); }

.grid.zone-deep .cell.zone-tl { background-color: rgba(74,124,255,0.26); }
.grid.zone-deep .cell.zone-tr { background-color: rgba(245,158,11,0.26); }
.grid.zone-deep .cell.zone-bl { background-color: rgba(34,197,94,0.26); }
.grid.zone-deep .cell.zone-br { background-color: rgba(139,92,246,0.26); }

/* Number input */
.num-input {
  width: 76px;
  padding: 6px 10px;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--card);
  color: var(--text);
  font-size: 16px;
  font-weight: 700;
  text-align: center;
  outline: none;
  -moz-appearance: textfield;
}
.num-input::-webkit-outer-spin-button,
.num-input::-webkit-inner-spin-button { -webkit-appearance: none; }
.num-input:focus { border-color: var(--primary); }

.cell.correct {
  background: var(--success-light);
  border-color: var(--success);
  color: var(--success);
  pointer-events: none;
}

.cell.wrong {
  animation: shake 0.3s ease;
  background: var(--error-light);
  border-color: var(--error);
}

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

@keyframes pop-in {
  0% { transform: scale(0.85); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

.cell {
  animation: pop-in 0.2s ease both;
}

/* RESULT */
.result-content {
  margin-top: 60px;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: 360px;
}

.result-icon {
  font-size: 64px;
  margin-bottom: 16px;
}

.result-time {
  font-size: 64px;
  font-weight: 900;
  color: var(--primary);
  letter-spacing: -2px;
  line-height: 1;
}

.result-label {
  font-size: 15px;
  color: var(--text-secondary);
  margin-top: 6px;
  margin-bottom: 28px;
}

.result-stats {
  display: flex;
  gap: 32px;
  margin-bottom: 36px;
  background: var(--card);
  border-radius: var(--radius);
  padding: 20px 32px;
  box-shadow: var(--shadow-sm);
  width: 100%;
  justify-content: center;
}

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

.result-stat-val {
  font-size: 20px;
  font-weight: 700;
  color: var(--text);
}

.result-stat-lbl {
  font-size: 12px;
  color: var(--text-secondary);
}

/* Toggle switch */
.setting-row {
  width: 100%;
  max-width: 360px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24px;
}

.setting-label {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}

.toggle-switch {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 26px;
  flex-shrink: 0;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-slider {
  position: absolute;
  cursor: pointer;
  inset: 0;
  background: var(--border);
  border-radius: 26px;
  transition: background 0.2s;
}

.toggle-slider:before {
  content: '';
  position: absolute;
  width: 20px;
  height: 20px;
  left: 3px;
  top: 3px;
  background: white;
  border-radius: 50%;
  transition: transform 0.2s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}

.toggle-switch input:checked + .toggle-slider { background: var(--primary); }
.toggle-switch input:checked + .toggle-slider:before { transform: translateX(18px); }

/* Dark mode */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111318;
    --card: #1E2028;
    --text: #F0F2F5;
    --text-secondary: #9CA3AF;
    --border: #2D3139;
    --primary-light: #1E2540;
    --success-light: #14271E;
    --error-light: #2A1515;
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.3);
    --shadow-md: 0 4px 16px rgba(0,0,0,0.4);
  }

  .cell.zone-tl { background-color: rgba(74,124,255,0.18); }
  .cell.zone-tr { background-color: rgba(245,158,11,0.18); }
  .cell.zone-bl { background-color: rgba(34,197,94,0.18); }
  .cell.zone-br { background-color: rgba(139,92,246,0.18); }

  .grid.zone-deep .cell.zone-tl { background-color: rgba(74,124,255,0.42); }
  .grid.zone-deep .cell.zone-tr { background-color: rgba(245,158,11,0.42); }
  .grid.zone-deep .cell.zone-bl { background-color: rgba(34,197,94,0.42); }
  .grid.zone-deep .cell.zone-br { background-color: rgba(139,92,246,0.42); }
}
