*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg: #06080c; --surface: #0e1219; --surface2: #161c26; --border: #1e2a3a;
  --text: #eaf0f6; --text-muted: #5a6a7e;
  --primary: #00e87b; --primary-dim: #00a858;
  --recovery: #ffb300; --recovery-dim: #a67300;
  --danger: #ff3355; --danger-dim: #aa2040;
  --info: #4499ff;
  --radius: 12px; --radius-lg: 20px;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  --font-mono: 'SF Mono', 'Cascadia Code', 'Fira Code', 'Consolas', 'Menlo', monospace;
}
html, body {
  height: 100%; overflow: hidden; font-family: var(--font); background: var(--bg);
  color: var(--text); -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none; user-select: none; touch-action: manipulation;
}
#app { height: 100%; }
.screen {
  position: fixed; inset: 0; display: flex; flex-direction: column;
  overflow-y: auto; overflow-x: hidden; transition: opacity 0.3s ease;
}
.screen.hidden { display: none; opacity: 0; pointer-events: none; }
.hidden { display: none !important; }

.update-banner, .install-banner {
  position: fixed; left: 12px; right: 12px; z-index: 200;
  background: var(--surface2); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 12px; display: flex; align-items: center; justify-content: space-between; gap: 10px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.3);
}
.update-banner { top: 12px; }
.install-banner { bottom: 12px; }
.banner-text { font-size: 0.85rem; color: var(--text); }
.banner-actions { display: flex; gap: 8px; }

.toast {
  position: fixed; left: 50%; bottom: 18px; transform: translateX(-50%);
  background: var(--surface2); border: 1px solid var(--border); border-radius: 999px;
  padding: 10px 14px; font-size: 0.8rem; color: var(--text);
  opacity: 0; pointer-events: none; transition: opacity 0.2s ease, transform 0.2s ease; z-index: 220;
}
.toast.show { opacity: 1; transform: translate(-50%, -6px); }

/* HOME */
#screen-home {
  padding: 0 24px; padding-top: env(safe-area-inset-top, 20px);
  padding-bottom: calc(env(safe-area-inset-bottom, 20px) + 20px);
  justify-content: flex-start; align-items: center; gap: 18px;
}
.home-topbar { width: 100%; max-width: 380px; display: flex; justify-content: space-between; align-items: center; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface2); color: var(--text); display: inline-flex; align-items: center; justify-content: center;
  cursor: pointer; font-size: 1rem;
}
.icon-btn:active { transform: scale(0.96); }
.logo { text-align: center; margin-bottom: 4px; }
.logo-icon { font-size: 3rem; margin-bottom: 4px; filter: grayscale(0.3); }
.logo h1 {
  font-size: 2.2rem; font-weight: 800; letter-spacing: -0.03em;
  background: linear-gradient(135deg, var(--primary), #00c8ff);
  -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text;
}
.logo .subtitle { font-size: 0.85rem; color: var(--text-muted); letter-spacing: 0.15em; text-transform: uppercase; margin-top: 4px; }

.quick-start {
  width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px 14px; display: flex; flex-direction: column; gap: 8px;
}
.quick-start-title { font-size: 0.65rem; text-transform: uppercase; letter-spacing: 0.12em; color: var(--text-muted); }
.quick-start-row { display: flex; justify-content: space-between; font-size: 0.85rem; color: var(--text); }
.quick-start-row strong { font-family: var(--font-mono); font-weight: 800; }

.test-selector { display: flex; gap: 10px; width: 100%; max-width: 380px; }
.test-option {
  flex: 1; background: var(--surface); border: 2px solid var(--border);
  border-radius: var(--radius); padding: 14px 10px; color: var(--text-muted);
  text-align: center; cursor: pointer; transition: all 0.2s; font-family: var(--font);
}
.test-option.selected { border-color: var(--primary); color: var(--text); background: rgba(0,232,123,0.06); }
.test-option .test-name { display: block; font-size: 0.95rem; font-weight: 700; margin-bottom: 3px; }
.test-option .test-desc { display: block; font-size: 0.65rem; opacity: 0.6; }

.test-preview { display: flex; gap: 10px; width: 100%; max-width: 380px; }
.preview-item {
  flex: 1; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 10px 12px; text-align: center;
}
.preview-label { display: block; font-size: 0.6rem; text-transform: uppercase; letter-spacing: 0.12em; color: var(--text-muted); margin-bottom: 4px; }
.preview-value { font-size: 1rem; font-weight: 700; font-family: var(--font-mono); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  font-family: var(--font); font-weight: 600; border: none; border-radius: var(--radius);
  cursor: pointer; transition: all 0.15s; padding: 14px 28px; font-size: 1rem;
  width: 100%; max-width: 380px;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--primary); color: #000; font-size: 1.15rem; font-weight: 800; padding: 18px 32px; letter-spacing: 0.02em; box-shadow: 0 0 30px rgba(0,232,123,0.2); }
.btn-primary:active { background: var(--primary-dim); }
.btn-secondary { background: var(--surface2); color: var(--text-muted); border: 1px solid var(--border); }
.btn-ghost { background: transparent; color: var(--text-muted); padding: 12px 20px; }
.btn-danger { background: var(--danger); color: #fff; font-weight: 800; box-shadow: 0 0 20px rgba(255,51,85,0.2); }
.btn-danger:active { background: var(--danger-dim); }
.btn-sm { padding: 10px 18px; font-size: 0.85rem; width: auto; }

.home-actions { display: flex; gap: 10px; width: 100%; max-width: 380px; }
.home-actions .btn { flex: 1; }

.ad-slot {
  width: 100%; max-width: 380px; height: 90px; border: 1px dashed var(--border);
  border-radius: var(--radius); display: flex; align-items: center; justify-content: center;
  color: var(--text-muted); font-size: 0.75rem; background: rgba(255,255,255,0.02);
}

.notice {
  background: rgba(255,179,0,0.08); border: 1px solid rgba(255,179,0,0.3);
  border-radius: var(--radius); padding: 16px; width: 100%; max-width: 380px; text-align: center;
}
.notice p { margin-bottom: 8px; font-size: 0.9rem; color: var(--recovery); }
.notice .notice-detail { font-size: 0.8rem; color: var(--text-muted); }
.notice-actions { display: flex; gap: 8px; justify-content: center; margin-top: 12px; }

/* TEST */
#screen-test { padding: 0; justify-content: space-between; overflow: hidden; }
.test-header {
  padding: 16px 20px 12px; padding-top: calc(env(safe-area-inset-top, 16px) + 8px);
  display: flex; justify-content: space-between; align-items: center;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.test-info { display: flex; flex-direction: column; gap: 2px; }
.test-level-label { font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.12em; color: var(--text-muted); }
.test-level-value { font-size: 1.3rem; font-weight: 800; font-family: var(--font-mono); }
.test-shuttle-label { font-size: 0.7rem; color: var(--text-muted); }
.test-stage-label { font-size: 0.65rem; color: var(--info); margin-top: 1px; }
.test-speed { text-align: right; }
.test-speed-value { font-size: 1.6rem; font-weight: 800; font-family: var(--font-mono); }
.test-speed-unit { font-size: 0.65rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.1em; }

.wake-chip {
  margin-top: 8px; display: inline-flex; align-items: center; gap: 6px;
  background: var(--surface2); border: 1px solid var(--border); border-radius: 999px;
  padding: 4px 8px; font-size: 0.6rem; text-transform: uppercase; letter-spacing: 0.1em;
  color: var(--text-muted);
}
.wake-chip.on { border-color: rgba(0,232,123,0.5); color: var(--primary); }
.wake-chip.off { border-color: rgba(255,51,85,0.4); color: var(--danger); }
.wake-chip.warn { border-color: rgba(255,179,0,0.5); color: var(--recovery); }
.wake-banner {
  margin-top: 16px; background: rgba(255,179,0,0.08); border: 1px solid rgba(255,179,0,0.35);
  border-radius: var(--radius); padding: 10px 12px; font-size: 0.75rem; color: var(--recovery);
  text-align: center; max-width: 320px;
}

.test-main { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 20px; }
.phase-ring {
  width: min(280px, 70vw); height: min(280px, 70vw); border-radius: 50%;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  transition: background 0.5s, box-shadow 0.5s;
}
.phase-ring.countdown { background: radial-gradient(circle, rgba(68,153,255,0.08) 0%, transparent 70%); box-shadow: 0 0 80px rgba(68,153,255,0.1); }
.phase-ring.running { background: radial-gradient(circle, rgba(0,232,123,0.1) 0%, transparent 70%); box-shadow: 0 0 80px rgba(0,232,123,0.15); }
.phase-ring.recovery { background: radial-gradient(circle, rgba(255,179,0,0.08) 0%, transparent 70%); box-shadow: 0 0 80px rgba(255,179,0,0.1); }

.phase-label { font-size: 0.8rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.2em; margin-bottom: 4px; }
.phase-label.running { color: var(--primary); }
.phase-label.recovery { color: var(--recovery); }
.phase-label.countdown { color: var(--info); }

.phase-value { font-size: 5.5rem; font-weight: 900; font-family: var(--font-mono); line-height: 1; letter-spacing: -0.04em; transition: color 0.3s; }
.phase-value.running { color: var(--primary); }
.phase-value.recovery { color: var(--recovery); }
.phase-value.countdown { color: var(--info); }

.phase-sub { font-size: 0.9rem; color: var(--text-muted); margin-top: 8px; min-height: 1.2em; }

.direction-indicator { display: flex; align-items: center; gap: 12px; margin-top: 16px; font-size: 0.85rem; font-weight: 600; color: var(--text-muted); min-height: 24px; }
.direction-arrow { font-size: 1.4rem; }
.direction-arrow.out { color: var(--primary); }
.direction-arrow.back { color: var(--primary); transform: scaleX(-1); }

.track-progress { width: min(260px, 65vw); height: 6px; background: var(--surface2); border-radius: 3px; margin-top: 16px; overflow: hidden; }
.track-fill { height: 100%; border-radius: 3px; transition: width 0.1s linear, background 0.3s; background: var(--primary); }
.track-fill.recovery { background: var(--recovery); }

.test-footer {
  padding: 16px 20px; padding-bottom: calc(env(safe-area-inset-bottom, 16px) + 12px);
  background: var(--surface); border-top: 1px solid var(--border);
}
.test-stats { display: flex; justify-content: space-around; margin-bottom: 14px; }
.stat { text-align: center; }
.stat-label { font-size: 0.65rem; text-transform: uppercase; letter-spacing: 0.12em; color: var(--text-muted); margin-bottom: 2px; }
.stat-value { font-size: 1.3rem; font-weight: 800; font-family: var(--font-mono); }
.test-controls { display: flex; gap: 10px; }
.test-controls .btn { max-width: none; }

/* RESULTS */
#screen-results {
  padding: 24px; padding-top: calc(env(safe-area-inset-top, 20px) + 16px);
  padding-bottom: calc(env(safe-area-inset-bottom, 20px) + 20px);
  gap: 16px; align-items: center;
}
.results-header { text-align: center; }
.results-header h2 { font-size: 1.6rem; font-weight: 800; margin-bottom: 4px; }
.results-header .results-date { font-size: 0.8rem; color: var(--text-muted); }
.results-header .results-test-type { font-size: 0.75rem; color: var(--primary); text-transform: uppercase; letter-spacing: 0.15em; font-weight: 700; }

.results-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; width: 100%; max-width: 400px; }
.result-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; text-align: center; }
.result-card.highlight { grid-column: 1 / -1; border-color: var(--primary); background: rgba(0,232,123,0.04); }
.result-card-label { font-size: 0.65rem; text-transform: uppercase; letter-spacing: 0.12em; color: var(--text-muted); margin-bottom: 6px; }
.result-card-value { font-size: 1.8rem; font-weight: 900; font-family: var(--font-mono); }
.result-card.highlight .result-card-value { font-size: 2.4rem; color: var(--primary); }
.result-card-sub { font-size: 0.75rem; color: var(--text-muted); margin-top: 2px; }

/* Notes form */
.notes-section {
  width: 100%; max-width: 400px; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius); padding: 16px;
}
.notes-title { font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.12em; color: var(--text-muted); margin-bottom: 12px; }
.notes-row { display: flex; gap: 10px; margin-bottom: 10px; }
.notes-row:last-child { margin-bottom: 0; }
.notes-field { flex: 1; display: flex; flex-direction: column; gap: 4px; }
.notes-field label { font-size: 0.65rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.08em; }
.notes-field input, .notes-field textarea {
  background: var(--surface2); border: 1px solid var(--border); border-radius: 8px;
  color: var(--text); font-family: var(--font); font-size: 0.9rem;
  padding: 10px 12px; outline: none; transition: border-color 0.2s;
  -webkit-user-select: text; user-select: text;
}
.notes-field input:focus, .notes-field textarea:focus { border-color: var(--primary); }
.notes-field textarea { resize: vertical; min-height: 48px; }

.fitness-scale {
  width: 100%; max-width: 400px; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius); padding: 16px;
}
.fitness-scale-title { font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.12em; color: var(--text-muted); margin-bottom: 12px; text-align: center; }
.fitness-bar-wrapper { position: relative; margin-bottom: 8px; }
.fitness-bar { width: 100%; height: 10px; border-radius: 5px; background: linear-gradient(90deg, #ff3355, #ff8844, #ffbb33, #88cc44, #00cc66, #00aaff); }
.fitness-marker { position: absolute; top: -6px; width: 3px; height: 22px; background: #fff; border-radius: 2px; box-shadow: 0 0 8px rgba(255,255,255,0.5); transition: left 0.6s ease-out; }
.fitness-labels { display: flex; justify-content: space-between; font-size: 0.55rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.05em; }
.fitness-rating { text-align: center; margin-top: 12px; font-size: 1rem; font-weight: 700; }

.results-actions { display: flex; flex-direction: column; gap: 10px; width: 100%; max-width: 400px; }

/* HISTORY */
#screen-history {
  padding: 20px; padding-top: calc(env(safe-area-inset-top, 20px) + 16px);
  padding-bottom: calc(env(safe-area-inset-bottom, 20px) + 20px); gap: 16px;
}
.history-toolbar { display: flex; justify-content: space-between; align-items: center; width: 100%; }
.history-toolbar h2 { font-size: 1.4rem; font-weight: 800; }
.history-toolbar-actions { display: flex; gap: 8px; }
.history-list { display: flex; flex-direction: column; gap: 8px; width: 100%; }

.history-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden; transition: border-color 0.2s;
}
.history-card.expanded { border-color: rgba(0,232,123,0.3); }
.history-card-header {
  padding: 14px 16px; cursor: pointer; display: flex; justify-content: space-between; align-items: center;
}
.history-card-left { flex: 1; }
.history-card-date { font-size: 0.85rem; font-weight: 600; color: var(--text); }
.history-card-summary { display: flex; gap: 12px; margin-top: 4px; font-size: 0.8rem; color: var(--text-muted); }
.history-card-summary strong { color: var(--text); font-family: var(--font-mono); font-weight: 700; }
.history-card-type { font-size: 0.6rem; text-transform: uppercase; letter-spacing: 0.1em; color: var(--primary); font-weight: 700; margin-top: 3px; }
.history-card-chevron { color: var(--text-muted); font-size: 0.9rem; transition: transform 0.2s; padding: 0 4px; }
.history-card.expanded .history-card-chevron { transform: rotate(180deg); }

.history-card-details {
  display: none; padding: 0 16px 16px; border-top: 1px solid var(--border);
}
.history-card.expanded .history-card-details { display: block; padding-top: 14px; }

.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
.detail-label { font-size: 0.6rem; text-transform: uppercase; letter-spacing: 0.1em; color: var(--text-muted); margin-bottom: 2px; }
.detail-value { font-size: 0.95rem; font-weight: 700; font-family: var(--font-mono); }
.detail-notes { margin-top: 8px; }
.detail-notes-label { font-size: 0.6rem; text-transform: uppercase; letter-spacing: 0.1em; color: var(--text-muted); margin-bottom: 4px; }
.detail-notes-text { font-size: 0.85rem; color: var(--text); line-height: 1.5; word-break: break-word; }
.detail-notes-text .note-field { display: block; margin-bottom: 2px; }
.detail-notes-text .note-label { color: var(--text-muted); }

.detail-actions { display: flex; gap: 8px; margin-top: 12px; }
.detail-actions .btn { padding: 8px 14px; font-size: 0.75rem; }

.history-empty { text-align: center; color: var(--text-muted); padding: 40px 20px; font-size: 0.9rem; }

/* LEADERBOARD */
#screen-leaderboard {
  padding: 20px; padding-top: calc(env(safe-area-inset-top, 20px) + 16px);
  padding-bottom: calc(env(safe-area-inset-bottom, 20px) + 20px); gap: 12px;
  align-items: center;
}
.leaderboard-tabs, .leaderboard-filters { display: flex; gap: 8px; width: 100%; max-width: 400px; }
.tab-btn, .filter-btn {
  flex: 1; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 10px 12px; color: var(--text-muted); cursor: pointer;
}
.tab-btn.selected, .filter-btn.selected { border-color: var(--primary); color: var(--text); background: rgba(0,232,123,0.06); }
.leaderboard-list { width: 100%; max-width: 400px; display: flex; flex-direction: column; gap: 8px; }
.leaderboard-item {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 14px; display: grid; grid-template-columns: 30px 1fr auto; gap: 10px; align-items: center;
}
.leaderboard-rank { font-size: 0.8rem; color: var(--text-muted); }
.leaderboard-name { font-size: 0.9rem; font-weight: 700; }
.leaderboard-meta { font-size: 0.7rem; color: var(--text-muted); }
.leaderboard-distance { font-family: var(--font-mono); font-weight: 800; font-size: 1rem; }

/* SETTINGS */
#screen-settings {
  padding: 20px; padding-top: calc(env(safe-area-inset-top, 20px) + 16px);
  padding-bottom: calc(env(safe-area-inset-bottom, 20px) + 20px); gap: 16px; align-items: center;
}
.settings-group {
  width: 100%; max-width: 400px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px; display: flex; flex-direction: column; gap: 12px;
}
.toggle-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.toggle-label { font-size: 0.9rem; font-weight: 600; }
.toggle-desc { font-size: 0.7rem; color: var(--text-muted); margin-top: 4px; }
.switch { position: relative; display: inline-block; width: 46px; height: 26px; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider {
  position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0;
  background: var(--surface2); border: 1px solid var(--border); transition: 0.2s; border-radius: 999px;
}
.slider:before {
  position: absolute; content: ""; height: 20px; width: 20px; left: 3px; top: 2px;
  background: #fff; transition: 0.2s; border-radius: 50%;
}
.switch input:checked + .slider { background: var(--primary); border-color: rgba(0,232,123,0.6); }
.switch input:checked + .slider:before { transform: translateX(20px); background: #000; }

.settings-input {
  width: 100%; background: var(--surface2); border: 1px solid var(--border);
  border-radius: 8px; padding: 10px 12px; color: var(--text); font-size: 0.9rem;
}
.chip-row { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  padding: 6px 10px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface2); font-size: 0.75rem; cursor: pointer; color: var(--text);
}

/* SHARE */
#screen-share {
  padding: 20px; padding-top: calc(env(safe-area-inset-top, 20px) + 16px);
  padding-bottom: calc(env(safe-area-inset-bottom, 20px) + 20px); gap: 16px; align-items: center;
}
.share-card {
  width: 100%; max-width: 400px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px;
}
.share-text { white-space: pre-wrap; font-size: 0.9rem; color: var(--text); }

/* Modal */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.7); display: flex; align-items: center; justify-content: center; z-index: 100; padding: 24px; backdrop-filter: blur(4px); }
.modal-overlay.hidden { display: none; }
.modal-content { background: var(--surface2); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 28px 24px; max-width: 340px; width: 100%; text-align: center; }
.modal-title { font-size: 1.1rem; font-weight: 700; margin-bottom: 8px; }
.modal-detail { font-size: 0.85rem; color: var(--text-muted); margin-bottom: 20px; line-height: 1.5; }
.modal-actions { display: flex; gap: 10px; }
.modal-actions .btn { flex: 1; max-width: none; padding: 14px; }

@keyframes pulse-green { 0%,100%{box-shadow:0 0 60px rgba(0,232,123,0.1)} 50%{box-shadow:0 0 100px rgba(0,232,123,0.25)} }
@keyframes pulse-amber { 0%,100%{box-shadow:0 0 60px rgba(255,179,0,0.08)} 50%{box-shadow:0 0 100px rgba(255,179,0,0.2)} }
.running-pulse { animation: pulse-green 2s ease-in-out infinite; }
.recovery-pulse { animation: pulse-amber 1.5s ease-in-out infinite; }

#screen-results, #screen-history, #screen-leaderboard, #screen-settings, #screen-share { overflow-y: auto; }
