@import url('https://fonts.googleapis.com/css2?family=DM+Mono:wght@400;500&family=DM+Sans:wght@400;500&display=swap');

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg:             #0f0f0f;
  --bg-secondary:   #1a1a1a;
  --bg-hover:       #222222;
  --border:         #2a2a2a;
  --border-focus:   #555555;
  --text-primary:   #f0ede8;
  --text-secondary: #888880;
  --text-tertiary:  #707070;
  --accent:         #F59E0B;
  --accent-red:     #E84057;
  --radius-sm:      6px;
  --radius-md:      10px;
  --radius-lg:      14px;
  --font-sans:      'DM Sans', system-ui, sans-serif;
  --font-mono:      'DM Mono', monospace;
}

body.light {
  --bg:             #f5f3ef;
  --bg-secondary:   #ece9e3;
  --bg-hover:       #e2dfd8;
  --border:         #d4d0c8;
  --border-focus:   #999890;
  --text-primary:   #1a1a18;
  --text-secondary: #6b6960;
  --text-tertiary:  #9b9890;
}

body.amoled {
  --bg:             #000000;
  --bg-secondary:   #0a0a0a;
  --bg-hover:       #111111;
  --border:         #1a1a1a;
  --border-focus:   #444444;
  --text-primary:   #ffffff;
  --text-secondary: #999999;
  --text-tertiary:  #666666;
}

body,
.header h1,
.region-card,
.ping-value,
.ping-unit,
.region-city {
  transition: background-color 0.35s ease, border-color 0.35s ease, color 0.35s ease;
}

html, body {
  background: var(--bg);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 16px;
  line-height: 1.5;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

/* ── Animations ── */
@keyframes fadeUp {
  from {
    opacity: 0;
    transform: translateY(14px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

@keyframes pulse {
  0%, 100% { opacity: 0.4; }
  50%      { opacity: 1; }
}

@keyframes splash-logo-in {
  from { opacity: 0; transform: scale(0.88); }
  to   { opacity: 1; transform: scale(1); }
}

@keyframes splash-greeting-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

@keyframes splash-fade-out {
  from { opacity: 1; transform: translateY(0); }
  to   { opacity: 0; transform: translateY(-18px); }
}

/* Pause page load animations while splash is visible */
body.splash-active .header,
body.splash-active .start-section,
body.splash-active .results-grid,
body.splash-active .region-card,
body.splash-active .theme-btn,
body.splash-active .help-btn {
  animation-play-state: paused;
}

/* ── Page layout ── */
.page {
  max-width: 640px;
  margin: 0 auto;
  padding: 3rem 1.5rem 4rem;
}

/* ── Header ── */
.header {
  margin-bottom: 2.5rem;
  animation: fadeUp 0.5s cubic-bezier(0.22, 1, 0.36, 1) both;
  animation-delay: 0.05s;
}

.header h1 {
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--text-secondary);
}

/* ── Start section ── */
.start-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  margin-bottom: 2.5rem;
  animation: fadeUp 0.5s cubic-bezier(0.22, 1, 0.36, 1) both;
  animation-delay: 0.15s;
}

.start-btn {
  font-family: var(--font-sans);
  font-size: 13px;
  font-weight: 500;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-primary);
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 12px 32px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.15s;
}

.start-btn:hover {
  background: var(--bg-hover);
  border-color: var(--border-focus);
  transform: scale(1.03);
}

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

.start-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.start-hint {
  font-size: 11px;
  color: var(--text-tertiary);
  letter-spacing: 0.04em;
}

/* ── Results grid ── */
.results-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 2rem;
  animation: fadeUp 0.55s cubic-bezier(0.22, 1, 0.36, 1) both;
  animation-delay: 0.25s;
}

/* ── Region card ── */
.region-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  position: relative;
  transition: background 0.15s, border-color 0.15s;
  animation: fadeIn 0.35s ease both;
}

.region-card:hover {
  background: var(--bg-hover);
}

.region-card.testing {
  border-color: var(--border-focus);
}

.region-card.done {
  /* will get quality-based border color via JS */
}

.region-card.best {
  border-color: #4A8C5C;
}

.region-card.best::after {
  content: 'best';
  position: absolute;
  top: 8px;
  right: 10px;
  font-family: var(--font-sans);
  font-size: 9px;
  font-weight: 500;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: #4A8C5C;
  background: rgba(74, 140, 92, 0.1);
  padding: 2px 7px;
  border-radius: 4px;
}

.region-name {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-primary);
}

.region-city {
  font-size: 11px;
  color: var(--text-tertiary);
  margin-bottom: 8px;
}

.ping-display {
  display: flex;
  align-items: baseline;
  gap: 3px;
  min-height: 32px;
}

.ping-value {
  font-family: var(--font-mono);
  font-size: 26px;
  font-weight: 500;
  color: var(--text-primary);
  line-height: 1;
}

.ping-unit {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text-tertiary);
  font-weight: 400;
}

/* Quality colors */
.ping-quality-excellent .ping-value { color: #4A8C5C; }
.ping-quality-good .ping-value      { color: #7AB34A; }
.ping-quality-moderate .ping-value  { color: #E8952A; }
.ping-quality-high .ping-value      { color: #E84057; }

/* Loading state */
.ping-loading {
  display: flex;
  gap: 4px;
  align-items: center;
  min-height: 32px;
}

.ping-loading span {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--text-tertiary);
  animation: pulse 1.2s ease-in-out infinite;
}

.ping-loading span:nth-child(2) { animation-delay: 0.2s; }
.ping-loading span:nth-child(3) { animation-delay: 0.4s; }

/* ── Ping meta (jitter) ── */
.ping-meta {
  display: flex;
  align-items: center;
  gap: 4px;
  min-height: 16px;
}

.jitter-label {
  font-family: var(--font-sans);
  font-size: 9px;
  font-weight: 500;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-tertiary);
}

.jitter-value {
  font-family: var(--font-mono);
  font-size: 12px;
  font-weight: 500;
  color: var(--text-secondary);
}

.jitter-unit {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--text-tertiary);
}

/* Waiting state */
.ping-waiting {
  min-height: 32px;
  display: flex;
  align-items: center;
}

.ping-waiting span {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text-tertiary);
  opacity: 0.4;
}

/* ── Progress bar ── */
.progress-bar {
  width: 100%;
  max-width: 200px;
  height: 3px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
  opacity: 0;
  transition: opacity 0.3s;
}

.progress-bar.active {
  opacity: 1;
}

.progress-bar-fill {
  height: 100%;
  width: 0%;
  background: #FF85B3;
  border-radius: 2px;
  transition: width 0.3s ease;
}

/* ── Theme toggle button ── */
.theme-btn {
  animation: fadeUp 0.5s cubic-bezier(0.22, 1, 0.36, 1) both;
  animation-delay: 0.35s;
  position: fixed;
  bottom: 24px;
  right: 72px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: background 0.15s, border-color 0.15s, transform 0.15s;
  z-index: 100;
  font-size: 16px;
  line-height: 1;
}

.theme-btn:hover {
  background: var(--bg-hover);
  border-color: var(--border-focus);
  transform: scale(1.07);
}

/* ── Help button ── */
.help-btn {
  animation: fadeUp 0.5s cubic-bezier(0.22, 1, 0.36, 1) both;
  animation-delay: 0.35s;
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: background 0.15s, border-color 0.15s, transform 0.15s;
  z-index: 100;
}

.help-btn img {
  width: 20px;
  height: 20px;
  opacity: 0.5;
  transition: opacity 0.15s;
}

.help-btn:hover {
  background: var(--bg-hover);
  border-color: var(--border-focus);
  transform: scale(1.07);
}

.help-btn:hover img {
  opacity: 0.85;
}

/* ── Help overlay & modal ── */
.help-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.25s ease;
}

.help-overlay.active {
  opacity: 1;
  pointer-events: all;
}

body.help-open .page,
body.help-open .help-btn {
  filter: blur(4px);
  transition: filter 0.25s ease;
}

body:not(.help-open) .page,
body:not(.help-open) .help-btn {
  filter: blur(0);
  transition: filter 0.25s ease;
}

.help-overlay::before {
  content: '';
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: -1;
  opacity: 0;
  transition: opacity 0.25s ease;
}

.help-overlay.active::before {
  opacity: 1;
}

.help-modal {
  width: calc(100% - 3rem);
  max-width: 480px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  transform: scale(0.95) translateY(8px);
  transition: transform 0.3s cubic-bezier(0.22, 1, 0.36, 1);
}

.help-overlay.active .help-modal {
  transform: scale(1) translateY(0);
}

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

.help-modal-title {
  font-size: 10px;
  font-weight: 500;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-tertiary);
}

.help-close-btn {
  background: none;
  border: none;
  color: var(--text-tertiary);
  font-size: 14px;
  cursor: pointer;
  line-height: 1;
  padding: 2px 4px;
  border-radius: 4px;
  transition: color 0.15s, background 0.15s;
}

.help-close-btn:hover {
  color: var(--text-primary);
  background: var(--bg-hover);
}

.help-modal-body {
  padding: 18px 20px 22px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.help-section {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.help-section-label {
  font-size: 10px;
  font-weight: 500;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-tertiary);
  margin-bottom: 2px;
}

.help-section p {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.6;
}

.help-section p strong {
  color: var(--text-primary);
  font-weight: 500;
}

/* ── Splash screen ── */
.splash {
  position: fixed;
  inset: 0;
  z-index: 99999;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  pointer-events: none;
  transition: none;
}

.splash-logo {
  width: 120px;
  height: 120px;
  object-fit: contain;
  opacity: 0;
  transform: scale(0.88);
  transition: none;
}

.splash-greeting {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.06em;
  color: var(--text-primary);
  opacity: 0;
  transition: none;
}

.splash.visible .splash-greeting {
  animation: splash-greeting-in 0.9s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

.splash.visible .splash-logo {
  animation: splash-logo-in 0.9s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

.splash.hiding {
  animation: splash-fade-out 0.25s cubic-bezier(0.4, 0, 1, 1) forwards;
}

/* ── Deploy date ── */
.deploy-date {
  position: fixed;
  bottom: 10px;
  left: 12px;
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.05em;
  color: var(--text-primary);
  opacity: 0.18;
  pointer-events: none;
  user-select: none;
  z-index: 0;
}

/* ── Mobile responsive ── */
@media (max-width: 480px) {
  .results-grid {
    grid-template-columns: 1fr;
  }

  .ping-value {
    font-size: 22px;
  }
}