:root {
  --bg-outer: #e6e2da;
  --bg-app: #f7f5f0;
  --bg-card: #ffffff;
  --text-main: #232b32;
  --text-muted: #68737d;
  --primary: #2d4c6c;
  --primary-hover: #213a54;
  --primary-text: #ffffff;
  --accent: #d9822b;
  --btn-answer-bg: #ffffff;
  --btn-answer-border: #d6d1c7;
  --btn-answer-active: #e8eef5;
  --correct-flash: #3b8a5a;
  --wrong-flash: #c85a53;
  --border-radius: 18px;
  --scale: 1;
}

body[data-theme="dark"] {
  --bg-outer: #0f1318;
  --bg-app: #181e26;
  --bg-card: #222b36;
  --text-main: #eef1f5;
  --text-muted: #9aa5b1;
  --primary: #4e87b8;
  --primary-hover: #6399c7;
  --primary-text: #ffffff;
  --accent: #f0a44c;
  --btn-answer-bg: #26303d;
  --btn-answer-border: #3a4759;
  --btn-answer-active: #324052;
}

body[data-theme="contrast"] {
  --bg-outer: #000000;
  --bg-app: #000000;
  --bg-card: #121212;
  --text-main: #ffffff;
  --text-muted: #ffe600;
  --primary: #ffe600;
  --primary-hover: #ffd000;
  --primary-text: #000000;
  --accent: #00ffcc;
  --btn-answer-bg: #000000;
  --btn-answer-border: #ffffff;
  --btn-answer-active: #333333;
}

body[data-font-size="normal"] { --scale: 1; }
body[data-font-size="large"]  { --scale: 1.24; }
body[data-font-size="xlarge"] { --scale: 1.48; }

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

html, body {
  width: 100%;
  height: 100%;
  background-color: var(--bg-outer);
  color: var(--text-main);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* Phone-shaped container so desktop never stretches wide */
.app-shell {
  width: 100%;
  max-width: 420px;
  height: 100dvh;
  max-height: 860px;
  background-color: var(--bg-app);
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;
  box-shadow: 0 14px 40px rgba(0, 0, 0, 0.14);
}

@media (min-width: 460px) {
  .app-shell {
    height: 92dvh;
    border-radius: 28px;
    border: 1px solid rgba(0,0,0,0.08);
  }
}

/* Top bar */
.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px 10px;
  min-height: 64px;
  z-index: 10;
}

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

.brand-icon {
  width: 34px;
  height: 34px;
  border-radius: 8px;
}

.brand-title {
  font-weight: 700;
  font-size: calc(1.1rem * var(--scale));
  letter-spacing: -0.01em;
}

.icon-btn {
  background: transparent;
  border: none;
  color: var(--text-main);
  cursor: pointer;
  padding: 8px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: calc(1.15rem * var(--scale));
  transition: background 0.15s;
}

.icon-btn:hover, .icon-btn:active {
  background: rgba(128, 128, 128, 0.15);
}

/* Timer in top bar */
.timer-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-right: 10px;
}

.timer-bar-track {
  width: 100%;
  height: 8px;
  background: rgba(128, 128, 128, 0.2);
  border-radius: 99px;
  overflow: hidden;
}

.timer-bar-fill {
  height: 100%;
  width: 100%;
  background: var(--primary);
  border-radius: 99px;
  transition: width 0.2s linear;
}

.timer-top-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-weight: 600;
  font-size: calc(0.95rem * var(--scale));
}

.quit-btn {
  padding: 2px 8px;
  font-size: 1rem;
}

.live-score {
  color: var(--accent);
  font-weight: 700;
}

/* Screens */
.screen {
  flex: 1;
  display: none;
  flex-direction: column;
  justify-content: space-between;
  padding: 12px 20px 24px;
  overflow-y: auto;
}

.screen.active {
  display: flex;
}

.hidden {
  display: none !important;
}

/* Home Screen */
.home-content {
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin-top: 4px;
}

.subtitle {
  color: var(--text-muted);
  font-size: calc(0.98rem * var(--scale));
  line-height: 1.45;
}

.stats-card {
  background: var(--bg-card);
  border-radius: var(--border-radius);
  padding: 16px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.04);
  border: 1px solid var(--btn-answer-border);
}

.stats-header {
  display: flex;
  justify-content: space-around;
  padding-bottom: 14px;
  border-bottom: 1px solid rgba(128, 128, 128, 0.15);
  margin-bottom: 12px;
}

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

.stat-label {
  font-size: calc(0.8rem * var(--scale));
  color: var(--text-muted);
  text-align: center;
}

.stat-value {
  font-size: calc(1.7rem * var(--scale));
  color: var(--primary);
}

.chart-container {
  position: relative;
}

.chart-title {
  font-size: calc(0.78rem * var(--scale));
  color: var(--text-muted);
  margin-bottom: 8px;
  text-align: center;
}

#historyChart {
  width: 100%;
  height: 115px;
  display: block;
}

.chart-empty {
  text-align: center;
  font-size: calc(0.88rem * var(--scale));
  color: var(--text-muted);
  padding: 28px 10px;
}

.install-banner {
  background: var(--bg-card);
  color: var(--primary);
  border: 1.5px dashed var(--primary);
  border-radius: 12px;
  padding: 12px;
  font-weight: 600;
  font-size: calc(0.92rem * var(--scale));
  cursor: pointer;
}

/* Primary & Secondary Buttons */
.bottom-action-area {
  margin-top: auto;
  padding-top: 12px;
}

.primary-btn {
  width: 100%;
  background: var(--primary);
  color: var(--primary-text);
  border: none;
  border-radius: var(--border-radius);
  padding: calc(16px * var(--scale)) 20px;
  font-size: calc(1.2rem * var(--scale));
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 6px 16px rgba(45, 76, 108, 0.25);
  transition: transform 0.1s, background 0.15s;
}

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

.secondary-btn {
  background: var(--bg-card);
  color: var(--text-main);
  border: 2px solid var(--btn-answer-border);
  border-radius: var(--border-radius);
  padding: calc(14px * var(--scale)) 18px;
  font-size: calc(1.05rem * var(--scale));
  font-weight: 600;
  cursor: pointer;
}

/* SCREEN 2: ERGONOMIC GAME STAGE */
/* Keeps the math problem right next to the thumb buttons at the bottom! */
.game-ergonomic-stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: 18px;
  padding-bottom: 8px;
}

/* Align problem + buttons according to chosen thumb side */
body[data-hand="right"] .game-ergonomic-stage {
  align-items: flex-end;
}

body[data-hand="left"] .game-ergonomic-stage {
  align-items: flex-start;
}

body[data-hand="center"] .game-ergonomic-stage {
  align-items: center;
}

.problem-card {
  background: var(--bg-card);
  border: 2px solid var(--btn-answer-border);
  border-radius: var(--border-radius);
  padding: 18px 26px;
  min-width: 72%;
  text-align: center;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.05);
  transition: border-color 0.15s, transform 0.12s;
}

body[data-hand="center"] .problem-card {
  width: 100%;
}

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

.problem-card.flash-wrong {
  border-color: var(--wrong-flash);
  animation: gentleShake 0.25s ease-in-out;
}

@keyframes gentleShake {
  0%, 100% { transform: translateX(0); }
  30% { transform: translateX(-6px); }
  70% { transform: translateX(6px); }
}

.problem-hint {
  font-size: calc(0.78rem * var(--scale));
  color: var(--text-muted);
  margin-bottom: 4px;
}

.problem-expression {
  font-size: calc(2.45rem * var(--scale));
  font-weight: 800;
  letter-spacing: 0.03em;
  line-height: 1.15;
}

/* 3 Vertical Answer Buttons in Thumb Reach */
.answers-column {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 68%;
  max-width: 280px;
}

body[data-hand="center"] .answers-column,
body[data-font-size="xlarge"] .answers-column {
  width: 100%;
  max-width: 100%;
}

body[data-font-size="xlarge"] .problem-card {
  width: 100%;
}

.answer-btn {
  background: var(--btn-answer-bg);
  color: var(--text-main);
  border: 2.5px solid var(--btn-answer-border);
  border-radius: var(--border-radius);
  padding: calc(15px * var(--scale)) 20px;
  font-size: calc(1.75rem * var(--scale));
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.04);
  transition: transform 0.08s, background 0.12s, border-color 0.12s;
  text-align: center;
}

.answer-btn:active {
  transform: scale(0.96);
  background: var(--btn-answer-active);
}

/* SCREEN 3: RESULT */
.result-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 16px;
  margin-top: 10px;
}

.record-badge {
  background: linear-gradient(135deg, #f6d365 0%, #fda085 100%);
  color: #232b32;
  font-weight: 800;
  padding: 8px 18px;
  border-radius: 99px;
  font-size: calc(0.95rem * var(--scale));
  box-shadow: 0 4px 12px rgba(253, 160, 133, 0.35);
  animation: popIn 0.35s ease-out;
}

@keyframes popIn {
  0% { transform: scale(0.75); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

.result-title {
  font-size: calc(1.4rem * var(--scale));
}

.score-circle {
  width: calc(150px * var(--scale));
  height: calc(150px * var(--scale));
  max-width: 210px;
  max-height: 210px;
  border-radius: 50%;
  background: var(--bg-card);
  border: 4px solid var(--primary);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.06);
}

.score-number {
  font-size: calc(2.7rem * var(--scale));
  font-weight: 800;
  color: var(--primary);
  line-height: 1;
}

.score-caption {
  font-size: calc(0.78rem * var(--scale));
  color: var(--text-muted);
  margin-top: 6px;
}

.result-metrics {
  display: flex;
  gap: 14px;
  width: 100%;
}

.metric-box {
  flex: 1;
  background: var(--bg-card);
  border: 1px solid var(--btn-answer-border);
  border-radius: 14px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.metric-label {
  font-size: calc(0.78rem * var(--scale));
  color: var(--text-muted);
}

.metric-val {
  font-size: calc(1.2rem * var(--scale));
}

.neuro-note {
  font-size: calc(0.84rem * var(--scale));
  color: var(--text-muted);
  line-height: 1.4;
  padding: 0 6px;
}

.result-actions {
  display: grid;
  grid-template-columns: 1fr 1.6fr;
  gap: 12px;
}

/* SETTINGS MODAL */
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.48);
  display: flex;
  align-items: flex-end;
  z-index: 100;
}

.modal-sheet {
  width: 100%;
  max-height: 88%;
  background: var(--bg-card);
  border-radius: 24px 24px 0 0;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  overflow-y: auto;
}

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

.modal-header h3 {
  font-size: calc(1.2rem * var(--scale));
}

.modal-body {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.setting-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.setting-label {
  font-size: calc(0.92rem * var(--scale));
  font-weight: 600;
}

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

.seg-btn {
  padding: 10px 8px;
  border-radius: 12px;
  border: 1.5px solid var(--btn-answer-border);
  background: var(--bg-app);
  color: var(--text-main);
  font-weight: 700;
  font-size: calc(0.95rem * var(--scale));
  cursor: pointer;
}

.seg-btn.active {
  background: var(--primary);
  color: var(--primary-text);
  border-color: var(--primary);
}

.setting-select {
  padding: 11px 12px;
  border-radius: 12px;
  border: 1.5px solid var(--btn-answer-border);
  background: var(--bg-app);
  color: var(--text-main);
  font-size: calc(0.98rem * var(--scale));
  font-weight: 600;
}

.toggle-row {
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
}

.toggle-checkbox {
  width: 24px;
  height: 24px;
  accent-color: var(--primary);
  cursor: pointer;
}

.divider {
  border: none;
  border-top: 1px solid rgba(128, 128, 128, 0.2);
}

.danger-outline-btn {
  background: transparent;
  color: var(--wrong-flash);
  border: 1.5px solid var(--wrong-flash);
  border-radius: 12px;
  padding: 10px;
  font-weight: 600;
  font-size: calc(0.9rem * var(--scale));
  cursor: pointer;
}
