/* ============================================================
   TYPING GAME — DESIGN SYSTEM
   ============================================================ */

/* ------------------------------------------------------------
   1. CUSTOM PROPERTIES (dark default)
   ------------------------------------------------------------ */
:root {
  --bg:            #1a1c27;
  --surface:       #21243a;
  --border:        #2e3254;
  --text-dim:      #4e5580;
  --text-mid:      #8890b8;
  --text-bright:   #dce2fa;
  --correct:       #dce2fa;
  --incorrect:     #ff8c8c;
  --extra:         #ff5555;
  --caret:         #f75555;
  --accent:        #f75555;
  --accent-glow:   rgba(247, 85, 85, 0.30);
  --accent-bg:     rgba(247, 85, 85, 0.10);
  --accent-dark:   rgba(247, 85, 85, 0.05);

  --font-mono:     'JetBrains Mono', monospace;
  --font-ui:       'Inter', sans-serif;
  --font-sz:       22px;
  --row-h:         50px;
  --visible-rows:  3;
  --transition-theme: 0.35s ease;
}

/* ------------------------------------------------------------
   2. LIGHT MODE OVERRIDES
   ------------------------------------------------------------ */
[data-theme="light"] {
  --bg:            #f0f2fa;
  --surface:       #ffffff;
  --border:        #d4d8f0;
  --text-dim:      #9196b3;
  --text-mid:      #6d7394;
  --text-bright:   #191d3a;
  --correct:       #191d3a;
  --incorrect:     #d94040;
  --extra:         #b82c2c;
  --caret:         #d93535;
  --accent:        #d93535;
  --accent-glow:   rgba(217, 53, 53, 0.22);
  --accent-bg:     rgba(217, 53, 53, 0.09);
  --accent-dark:   rgba(217, 53, 53, 0.04);
}

/* ------------------------------------------------------------
   3. RESET & BASE
   ------------------------------------------------------------ */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  height: 100%;
}

body {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  background-color: var(--bg);
  background-image:
    radial-gradient(ellipse 60% 50% at 20% 10%,  rgba(247, 85, 85, 0.07) 0%, transparent 60%),
    radial-gradient(ellipse 50% 40% at 80% 90%, rgba(247, 85, 85, 0.05) 0%, transparent 60%);
  color: var(--text-bright);
  font-family: var(--font-ui);
  user-select: none;
  -webkit-user-select: none;
  transition: background-color var(--transition-theme);
  /* Use dynamic viewport height so content doesn't hide behind virtual keyboard */
  min-height: 100dvh;
  /* Respect iPhone notch / home-indicator safe areas */
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

/* ------------------------------------------------------------
   4. THEME-TRANSITION ELEMENTS
   ------------------------------------------------------------ */
.header,
.footer,
.lang-btn,
.ctrl-btn,
.results-card,
.results-stats,
.about-card,
.about-link,
kbd {
  transition:
    background-color var(--transition-theme),
    border-color     var(--transition-theme),
    color            var(--transition-theme);
}

/* ------------------------------------------------------------
   5. HEADER
   ------------------------------------------------------------ */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 48px;
  border-bottom: 1px solid var(--border);
}

.header-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

/* Logo */
.logo {
  font-family: var(--font-mono);
  font-size: 17px;
  color: var(--accent);
  text-decoration: none;
  letter-spacing: -0.5px;
}

.logo-dim {
  color: var(--text-dim);
}

/* Language pill buttons */
.lang-btn {
  display: inline-flex;
  align-items: center;
  padding: 6px 13px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-mid);
  font-family: var(--font-mono);
  font-size: 12px;
  cursor: pointer;
  outline: none;
  line-height: 1;
}

.lang-btn:hover,
.lang-btn.active {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-bg);
}

/* ------------------------------------------------------------
   6. THEME TOGGLE BUTTON
   ------------------------------------------------------------ */
.theme-toggle,
#theme-toggle {
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: transparent;
  cursor: pointer;
  padding: 0;
  outline: none;
  transition:
    border-color var(--transition-theme),
    background   var(--transition-theme);
}

.theme-toggle:hover,
#theme-toggle:hover {
  background: var(--accent-bg);
}

/* Icon slots */
.t-icon {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  transition:
    transform 0.35s cubic-bezier(.4, 0, .2, 1),
    opacity   0.25s ease;
}

.t-icon svg {
  width: 16px;
  height: 16px;
}

/* Dark mode (default): moon visible, sun hidden below */
.t-icon.icon-moon {
  transform: translateY(0) rotate(0deg);
  opacity: 1;
  color: var(--text-mid);
}

.t-icon.icon-sun {
  transform: translateY(110%) rotate(-30deg);
  opacity: 0;
  color: var(--text-mid);
}

/* Light mode: sun visible, moon hidden above */
[data-theme="light"] .t-icon.icon-moon {
  transform: translateY(-110%) rotate(30deg);
  opacity: 0;
}

[data-theme="light"] .t-icon.icon-sun {
  transform: translateY(0) rotate(0deg);
  opacity: 1;
}

/* ------------------------------------------------------------
   7. MAIN
   ------------------------------------------------------------ */
main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 48px;
}

/* ------------------------------------------------------------
   8. STATS BAR
   ------------------------------------------------------------ */
.stats-bar {
  display: flex;
  gap: 28px;
  align-items: center;
  margin-bottom: 44px;
}

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

.stat-val {
  font-family: var(--font-mono);
  font-size: 30px;
  color: var(--accent);
  min-width: 4.5ch;
  text-align: center;
  transition: color var(--transition-theme);
}

.stat-lbl {
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  color: var(--text-dim);
}

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

/* ------------------------------------------------------------
   9. TYPING WRAPPER & WORDS DISPLAY
   ------------------------------------------------------------ */
.typing-wrapper {
  width: 100%;
  max-width: 920px;
  outline: none;
  /* Prevent 300ms tap delay and accidental double-tap zoom on mobile */
  touch-action: manipulation;
  cursor: text;
}

#words-display {
  position: relative;
  height: calc(var(--row-h) * var(--visible-rows));
  overflow: hidden;
  margin-bottom: 18px;
  -webkit-mask-image: linear-gradient(
    to bottom,
    transparent 0%,
    black 20%,
    black 80%,
    transparent 100%
  );
  mask-image: linear-gradient(
    to bottom,
    transparent 0%,
    black 20%,
    black 80%,
    transparent 100%
  );
}

#words-content {
  position: relative;
  display: flex;
  flex-wrap: wrap;
  gap: 0 12px;
  font-family: var(--font-mono);
  font-size: var(--font-sz);
  line-height: var(--row-h);
  transition: transform 0.28s cubic-bezier(.4, 0, .2, 1);
  will-change: transform;
}

/* Words */
.word {
  display: inline-flex;
  position: relative;
}

.word.has-error::after {
  content: '';
  position: absolute;
  bottom: 6px;
  left: 0;
  right: 0;
  height: 2px;
  background: var(--incorrect);
  opacity: 0.45;
  border-radius: 2px;
}

/* Characters */
.char {
  color: var(--text-dim);
  transition: color 0.07s;
}

.char.correct {
  color: var(--correct);
}

.char.incorrect {
  color: var(--incorrect);
}

.char.extra {
  color: var(--extra);
}

/* ------------------------------------------------------------
   10. CARET
   ------------------------------------------------------------ */
#caret {
  position: absolute;
  width: 2px;
  background: var(--accent);
  border-radius: 2px;
  pointer-events: none;
  z-index: 10;
  box-shadow: 0 0 8px 1px var(--accent-glow);
  transition:
    left 0.06s ease,
    top  0.06s ease;
}

#caret.blink {
  animation: caretBlink 1.1s step-start infinite;
}

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

/* ------------------------------------------------------------
   11. START HINT
   ------------------------------------------------------------ */
.start-hint {
  text-align: center;
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text-dim);
  margin-bottom: 22px;
  height: 20px;
  animation: hintPulse 2.5s ease-in-out infinite;
}

.start-hint.gone {
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s;
}

@keyframes hintPulse {
  0%, 100% { opacity: 0.35; }
  50%       { opacity: 0.85; }
}

/* Tap hint (mobile, shown via JS) */
.tap-hint {
  display: none;
  text-align: center;
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text-dim);
  margin-bottom: 22px;
  height: 20px;
}

/* ------------------------------------------------------------
   12. CONTROL BUTTONS
   ------------------------------------------------------------ */
.btn-row {
  display: flex;
  gap: 10px;
  justify-content: center;
}

.ctrl-btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 9px 18px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-mid);
  font-family: var(--font-mono);
  font-size: 13px;
  cursor: pointer;
  outline: none;
  transition: all 0.15s;
}

.ctrl-btn svg {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
}

.ctrl-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-bg);
}

.ctrl-btn:active {
  transform: scale(0.97);
}

.ctrl-btn.accent {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-bg);
}

/* ------------------------------------------------------------
   13. FOOTER
   ------------------------------------------------------------ */
.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 48px;
  border-top: 1px solid var(--border);
}

.kbd-hints {
  display: flex;
  gap: 20px;
}

.kbd-hint {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-dim);
  display: flex;
  align-items: center;
  gap: 5px;
}

kbd {
  display: inline-block;
  padding: 2px 7px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface);
  color: var(--text-dim);
  font-family: var(--font-mono);
  font-size: 10px;
  line-height: 1.6;
}

.footer-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.about-link {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-mid);
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  letter-spacing: 0.04em;
  transition: color var(--transition-theme);
}

.about-link:hover {
  color: var(--accent);
}

.footer-brand {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-dim);
  opacity: 0.45;
}

/* ------------------------------------------------------------
   14. RESULTS OVERLAY
   ------------------------------------------------------------ */
.results-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 16, 26, 0.88);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  z-index: 1000;
  display: none;
  align-items: center;
  justify-content: center;
}

.results-overlay.show {
  display: flex;
  animation: fadeIn 0.3s ease forwards;
}

[data-theme="light"] .results-overlay {
  background: rgba(250, 230, 230, 0.88);
}

/* Results card */
.results-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 48px;
  text-align: center;
  width: min(90vw, 480px);
  animation: slideUp 0.35s cubic-bezier(.4, 0, .2, 1);
}

/* Eyebrow label */
.results-eyebrow {
  font-family: var(--font-mono);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 2px;
  color: var(--text-dim);
  margin-bottom: 8px;
}

/* WPM hero number */
.results-wpm {
  font-family: var(--font-mono);
  font-size: 88px;
  font-weight: 600;
  color: var(--accent);
  letter-spacing: -3px;
  line-height: 1;
  margin-bottom: 4px;
}

.results-wpm-label {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text-dim);
  margin-bottom: 28px;
}

/* Stat row */
.results-stats {
  display: flex;
  flex-direction: row;
  background: var(--accent-dark);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  margin-bottom: 28px;
}

.results-stat {
  flex: 1;
  padding: 20px 12px;
  border-right: 1px solid var(--border);
}

.results-stat:last-child {
  border-right: none;
}

.results-stat-val {
  font-family: var(--font-mono);
  font-size: 22px;
  font-weight: 500;
  color: var(--text-bright);
  margin-bottom: 4px;
}

.results-stat-lbl {
  font-family: var(--font-mono);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: var(--text-dim);
}

/* Actions */
.results-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
}

/* ------------------------------------------------------------
   15. ABOUT OVERLAY
   ------------------------------------------------------------ */
.about-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 16, 26, 0.88);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  z-index: 1000;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.about-overlay.show {
  display: flex;
  animation: fadeIn 0.3s ease forwards;
}

[data-theme="light"] .about-overlay {
  background: rgba(250, 230, 230, 0.88);
}

.about-card {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 40px 40px 36px;
  width: min(92vw, 440px);
  text-align: left;
  animation: slideUp 0.35s cubic-bezier(.4, 0, .2, 1);
  transition:
    background-color var(--transition-theme),
    border-color     var(--transition-theme),
    color            var(--transition-theme);
}

.about-close {
  position: absolute;
  top: 14px;
  right: 14px;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-mid);
  cursor: pointer;
  padding: 0;
  transition:
    border-color var(--transition-theme),
    color        var(--transition-theme),
    background   var(--transition-theme);
}

.about-close:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-bg);
}

.about-close svg {
  width: 14px;
  height: 14px;
}

.about-titles {
  margin-bottom: 22px;
  padding-right: 28px;
}

.about-eyebrow {
  font-family: var(--font-mono);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 2px;
  color: var(--text-dim);
  margin-bottom: 4px;
}

.about-name {
  font-family: var(--font-mono);
  font-size: 28px;
  font-weight: 600;
  color: var(--text-bright);
  letter-spacing: -0.5px;
  line-height: 1.1;
  margin-bottom: 4px;
}

.about-handle {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--accent);
}

.about-bio,
.about-app {
  font-family: var(--font-ui);
  font-size: 14px;
  line-height: 1.65;
  color: var(--text-mid);
  font-weight: 400;
  margin-bottom: 14px;
}

.about-app {
  margin-bottom: 20px;
  color: var(--text-dim);
}

.about-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 28px;
}

.about-tags span {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-mid);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 11px;
  line-height: 1;
}

.about-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.about-actions .ctrl-btn {
  text-decoration: none;
}

.about-actions .ctrl-btn svg {
  width: 14px;
  height: 14px;
}

@media (max-width: 500px) {
  .about-card {
    padding: 28px 22px 24px;
  }

  .about-name {
    font-size: 22px;
  }
}

/* ------------------------------------------------------------
   16. HIDDEN INPUT
   ------------------------------------------------------------ */
#hidden-input {
  /* Kept just above the visible area — iOS shows keyboard reliably when
     the element is near (not 999px away) and focus() is called synchronously. */
  position: fixed;
  top: -60px;
  left: 0;
  width: 100%;
  height: 40px;
  opacity: 0;
  font-size: 16px;   /* ≥16px prevents iOS auto-zoom on focus */
  border: none;
  outline: none;
  background: transparent;
  color: transparent;
  caret-color: transparent;
  pointer-events: none;
  -webkit-user-select: none;
  user-select: none;
}

/* ------------------------------------------------------------
   17. ANIMATIONS
   ------------------------------------------------------------ */
@keyframes fadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

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

@keyframes wrongFlash {
  0%   { background: rgba(248, 113, 113, 0.15); }
  100% { background: transparent; }
}

.wrong-flash {
  animation: wrongFlash 0.25s ease forwards;
}

/* ------------------------------------------------------------
   18. RESPONSIVE — LARGE TABLET  (≤ 1100px)
       iPad Pro 12.9" portrait & landscape (1024px),
       iPad Pro 11" / iPad Air / iPad — all orientations.
       Keeps the header horizontal; only the lang-nav scrolls.
   ------------------------------------------------------------ */
@media (max-width: 1100px) {
  :root {
    --font-sz: 20px;
    --row-h:   46px;
  }

  .header {
    padding: 16px 28px;
  }

  /* Scrollable language pills — one row, no wrapping */
  .lang-nav {
    overflow-x: auto;
    flex-wrap: nowrap;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 2px;
  }
  .lang-nav::-webkit-scrollbar { display: none; }
  .lang-btn { flex-shrink: 0; }

  main {
    padding: 40px 32px 28px;
  }

  .stats-bar {
    gap: 24px;
    margin-bottom: 38px;
  }

  .stat-val {
    font-size: 26px;
  }

  .footer {
    padding: 12px 28px;
  }

  /* Show tap-hint on touch tablets, hide desktop keyboard shortcuts */
  .tap-hint  { display: block; }
  .kbd-hints { display: none;  }

  .results-card {
    padding: 40px 36px;
  }
}

/* ------------------------------------------------------------
   19. RESPONSIVE — SMALL TABLET / LARGE PHONE  (≤ 768px)
       iPad mini portrait (768px), large Android phones.
       Header collapses to column at this width.
   ------------------------------------------------------------ */
@media (max-width: 768px) {
  :root {
    --font-sz: 18px;
    --row-h:   42px;
  }

  .header {
    padding: 12px 20px;
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }

  .header-right {
    width: 100%;
    gap: 8px;
    /* lang-nav already scrolls from the 1100px rule above */
  }

  main {
    padding: 28px 20px 20px;
  }

  .stats-bar {
    gap: 20px;
    margin-bottom: 28px;
  }

  .stat-val {
    font-size: 22px;
  }

  .results-card {
    padding: 32px 24px;
  }

  .results-wpm {
    font-size: 64px;
  }
}

/* ------------------------------------------------------------
   20. RESPONSIVE — PHONE  (≤ 500px)
   ------------------------------------------------------------ */
@media (max-width: 500px) {
  :root {
    --font-sz: 16px;
    --row-h:   38px;
  }

  .logo {
    font-size: 15px;
  }

  .lang-btn {
    padding: 5px 10px;
    font-size: 11px;
  }

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

  .stat-sep {
    height: 24px;
  }

  .theme-toggle,
  #theme-toggle {
    width: 32px;
    height: 32px;
  }

  .results-wpm {
    font-size: 52px;
  }

  .results-stat-val {
    font-size: 18px;
  }

  .results-card {
    padding: 24px 16px;
  }

  .results-actions {
    flex-direction: column;
    align-items: center;
  }

  .results-actions .ctrl-btn {
    justify-content: center;
    width: 100%;
  }

  .tap-hint {
    padding: 10px 0;
    font-size: 13px;
  }

  main {
    padding: 20px 16px 16px;
  }

  .stats-bar {
    gap: 14px;
    margin-bottom: 24px;
  }
}
