/* typeit.in — Stylesheet
   Tokyo Night palette · JetBrains Mono · buttery smooth
   ============================================================ */

/* ─── Custom Properties ─────────────────────────────────────── */
:root {
  /* Base palette */
  --bg:          #1a1b26;
  --bg-alt:      #13141e;
  --surface:     #1f2335;
  --surface-2:   #24283b;
  --surface-3:   #292e42;
  --border:      #292e42;
  --border-focus:#414868;

  /* Text */
  --text:        #c0caf5;
  --text-muted:  #565f89;
  --text-dim:    #363b54;

  /* Accent */
  --blue:        #7aa2f7;
  --purple:      #bb9af7;
  --cyan:        #7dcfff;
  --green:       #9ece6a;
  --orange:      #ff9e64;
  --red:         #f7768e;
  --yellow:      #e0af68;
  --teal:        #2ac3de;

  /* Syntax token colors */
  --tk-keyword:   var(--purple);
  --tk-builtin:   var(--cyan);
  --tk-string:    var(--green);
  --tk-comment:   var(--text-muted);
  --tk-number:    var(--orange);
  --tk-function:  var(--blue);
  --tk-operator:  var(--teal);
  --tk-decorator: var(--orange);
  --tk-plain:     var(--text);

  /* Typing state colors */
  --correct-glow:    rgba(154, 206, 106, 0.08);
  --incorrect-color: var(--red);
  --incorrect-bg:    rgba(247, 118, 142, 0.18);
  --cursor-color:    var(--blue);
  --cursor-glow:     rgba(122, 162, 247, 0.5);
  --untyped-opacity: 0.28;

  /* Fonts */
  --font-mono:   'JetBrains Mono', 'Fira Code', 'Cascadia Code', 'SF Mono', monospace;
  --font-sans:   'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;

  /* Spacing */
  --radius:  8px;
  --radius-sm: 4px;
  --radius-lg: 12px;
}

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

html {
  height: 100%;
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  min-height: 100%;
  background: var(--bg-alt);
  color: var(--text);
  font-family: var(--font-sans);
  display: flex;
  flex-direction: column;
}

/* ─── Layout ──────────────────────────────────────────────────── */
.page-wrap {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  max-width: 900px;
  margin: 0 auto;
  width: 100%;
  padding: 0 24px 48px;
}

/* ─── Header ─────────────────────────────────────────────────── */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 28px 0 20px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 32px;
}

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

.logo-mark {
  font-family: var(--font-mono);
  font-size: 1.35rem;
  font-weight: 600;
  color: var(--blue);
  letter-spacing: -0.02em;
}

.logo-mark span {
  color: var(--text-muted);
}

.tagline {
  font-size: 0.78rem;
  color: var(--text-muted);
  font-family: var(--font-mono);
  letter-spacing: 0.01em;
}

/* ─── Language Tabs ──────────────────────────────────────────── */
.lang-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 24px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0;
}

.lang-tab {
  padding: 8px 18px 10px;
  border: none;
  background: none;
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: 0.82rem;
  font-weight: 500;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  border-radius: var(--radius-sm) var(--radius-sm) 0 0;
  transition: color 0.18s ease, background 0.18s ease, border-color 0.18s ease;
  letter-spacing: 0.03em;
  position: relative;
}

.lang-tab:hover {
  color: var(--text);
  background: rgba(255,255,255,0.04);
}

.lang-tab.active {
  color: var(--blue);
  border-bottom-color: var(--blue);
  background: rgba(122,162,247,0.06);
}

.lang-tab .lang-dot {
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  margin-right: 7px;
  vertical-align: middle;
  opacity: 0.6;
}
.lang-tab[data-lang="python"]     .lang-dot { background: #3572a5; }
.lang-tab[data-lang="javascript"] .lang-dot { background: #f1e05a; }
.lang-tab[data-lang="r"]          .lang-dot { background: #198ce7; }
.lang-tab[data-lang="sql"]        .lang-dot { background: #e38c00; }
.lang-tab.active .lang-dot { opacity: 1; }

/* ─── Stats Bar ──────────────────────────────────────────────── */
.stats-bar {
  display: flex;
  align-items: center;
  gap: 32px;
  margin-bottom: 16px;
  font-family: var(--font-mono);
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
}

.stat-label {
  font-size: 0.68rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.stat-value {
  font-size: 1.6rem;
  font-weight: 600;
  color: var(--text);
  line-height: 1;
  transition: color 0.2s ease;
  min-width: 3ch;
}

.stat-value.wpm-val { color: var(--blue); }
.stat-value.acc-val { color: var(--green); }
.stat-value.time-val { color: var(--text-muted); }

/* ─── Controls row ─────────────────────────────────────────────── */
.controls-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

.snippet-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.snippet-counter {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  color: var(--text-muted);
  background: var(--surface-2);
  padding: 2px 8px;
  border-radius: 100px;
  border: 1px solid var(--border);
}

.snippet-title {
  font-size: 0.8rem;
  color: var(--text-muted);
  font-family: var(--font-mono);
}

.mode-control {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.78rem;
  color: var(--text-muted);
  font-family: var(--font-mono);
  cursor: pointer;
  user-select: none;
}

.mode-control label { cursor: pointer; }

/* Toggle switch */
.toggle-wrap {
  position: relative;
  width: 34px;
  height: 18px;
}
.toggle-wrap input {
  opacity: 0;
  width: 0;
  height: 0;
}
.toggle-track {
  position: absolute;
  inset: 0;
  background: var(--surface-3);
  border-radius: 100px;
  border: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.2s ease, border-color 0.2s ease;
}
.toggle-track::after {
  content: '';
  position: absolute;
  left: 2px;
  top: 50%;
  transform: translateY(-50%);
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--text-muted);
  transition: left 0.18s ease, background 0.2s ease;
}
.toggle-wrap input:checked + .toggle-track {
  background: rgba(122, 162, 247, 0.25);
  border-color: var(--blue);
}
.toggle-wrap input:checked + .toggle-track::after {
  left: 18px;
  background: var(--blue);
}

/* ─── Code Panel ─────────────────────────────────────────────── */
.code-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 28px 32px;
  position: relative;
  overflow: hidden;
  cursor: text;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
  min-height: 240px;
}

.code-panel:focus-within,
.code-panel.focused {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 1px var(--border-focus), 0 4px 32px rgba(0,0,0,0.4);
}

/* Click-to-focus hint */
.focus-hint {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(26, 27, 38, 0.6);
  backdrop-filter: blur(2px);
  color: var(--text-muted);
  font-size: 0.85rem;
  font-family: var(--font-mono);
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s ease;
  border-radius: var(--radius-lg);
  z-index: 5;
}

/* Line numbers gutter */
.line-numbers {
  position: absolute;
  left: 0;
  top: 28px;
  bottom: 28px;
  width: 40px;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  padding-right: 12px;
  border-right: 1px solid var(--border);
  pointer-events: none;
  user-select: none;
  overflow: hidden;
}

.line-num {
  font-family: var(--font-mono);
  font-size: 0.8rem;
  color: var(--text-dim);
  line-height: 1.7;
  min-height: 1.7em;
}

/* Code display */
.typing-area {
  position: relative;
}

#codeDisplay {
  font-family: var(--font-mono);
  font-size: 0.92rem;
  line-height: 1.75;
  white-space: pre;
  position: relative;
  outline: none;
  tab-size: 4;
  /* Don't show scrollbar horizontally; wrap if truly necessary */
  overflow-x: auto;
  padding-bottom: 8px;
}

/* Hidden input — focus trap */
#hiddenInput {
  position: absolute;
  left: -9999px;
  top: 0;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

/* ─── Character States ───────────────────────────────────────── */

.char {
  display: inline;
  position: relative;
  transition: color 0.05s ease;
}

/* Untyped: dimmed syntax color */
.char.st-untyped {
  opacity: var(--untyped-opacity);
}

/* Correct: full brightness syntax color */
.char.st-correct {
  opacity: 1;
}

/* Incorrect: red, highlighted */
.char.st-incorrect {
  color: var(--incorrect-color) !important;
  background: var(--incorrect-bg);
  border-radius: 2px;
  text-decoration: underline;
  text-decoration-color: var(--red);
  text-decoration-thickness: 1px;
  opacity: 1;
}

/* Cursor: blinking cursor element positioned absolutely (see JS) */
#typingCursor {
  position: absolute;
  width: 2px;
  background: var(--cursor-color);
  box-shadow: 0 0 10px var(--cursor-glow), 0 0 20px var(--cursor-glow);
  border-radius: 1px;
  pointer-events: none;
  z-index: 10;
  animation: cursorBlink 1.1s step-end infinite;
  transition: top 0.04s ease, left 0.04s ease, height 0.04s ease;
}

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

.cursor-complete {
  animation: cursorFlash 0.4s ease forwards !important;
}

@keyframes cursorFlash {
  0%   { opacity: 1; box-shadow: 0 0 20px var(--cursor-glow), 0 0 40px var(--cursor-glow); }
  100% { opacity: 0; box-shadow: none; }
}

/* ─── Syntax Token Colors ────────────────────────────────────── */

/* Correct typed */
.st-correct.tk-keyword   { color: var(--tk-keyword);   }
.st-correct.tk-builtin   { color: var(--tk-builtin);   }
.st-correct.tk-string    { color: var(--tk-string);    }
.st-correct.tk-comment   { color: var(--tk-comment);   }
.st-correct.tk-number    { color: var(--tk-number);    }
.st-correct.tk-function  { color: var(--tk-function);  }
.st-correct.tk-operator  { color: var(--tk-operator);  }
.st-correct.tk-decorator { color: var(--tk-decorator); }
.st-correct.tk-plain     { color: var(--tk-plain);     }

/* Untyped (dim via opacity — no color override needed) */
.st-untyped.tk-keyword   { color: var(--tk-keyword);   }
.st-untyped.tk-builtin   { color: var(--tk-builtin);   }
.st-untyped.tk-string    { color: var(--tk-string);    }
.st-untyped.tk-comment   { color: var(--tk-comment);   }
.st-untyped.tk-number    { color: var(--tk-number);    }
.st-untyped.tk-function  { color: var(--tk-function);  }
.st-untyped.tk-operator  { color: var(--tk-operator);  }
.st-untyped.tk-decorator { color: var(--tk-decorator); }
.st-untyped.tk-plain     { color: var(--tk-plain);     }

/* ─── Shake animation (strict mode errors) ───────────────────── */
@keyframes shake {
  0%   { transform: translateX(0); }
  15%  { transform: translateX(-5px); }
  30%  { transform: translateX(5px); }
  45%  { transform: translateX(-4px); }
  60%  { transform: translateX(4px); }
  75%  { transform: translateX(-2px); }
  90%  { transform: translateX(2px); }
  100% { transform: translateX(0); }
}

.code-panel.shake {
  animation: shake 0.28s ease;
  border-color: var(--red) !important;
  box-shadow: 0 0 0 1px var(--red), 0 4px 20px rgba(247, 118, 142, 0.15) !important;
}

/* Completed flash — class added directly to .code-panel */
.code-panel.completed-flash {
  animation: completedFlash 0.6s ease;
}

@keyframes completedFlash {
  0%   { background: var(--surface); border-color: var(--border); }
  35%  { background: rgba(154, 206, 106, 0.1); border-color: rgba(154, 206, 106, 0.4); }
  100% { background: var(--surface); border-color: var(--border); }
}

/* ─── Results Overlay ────────────────────────────────────────── */
.results-overlay {
  position: fixed;
  inset: 0;
  background: rgba(13, 14, 22, 0.85);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.28s ease, visibility 0.28s;
}

.results-overlay.visible {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

/* .hidden is kept as a class flag in JS — no extra display:none needed */
.results-overlay.hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.results-card {
  background: var(--surface);
  border: 1px solid var(--border-focus);
  border-radius: 20px;
  padding: 48px 56px;
  min-width: 480px;
  text-align: center;
  box-shadow: 0 24px 80px rgba(0,0,0,0.5), 0 0 0 1px rgba(122,162,247,0.1);
  animation: cardSlideUp 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes cardSlideUp {
  from { transform: translateY(30px) scale(0.96); opacity: 0; }
  to   { transform: translateY(0)    scale(1);    opacity: 1; }
}

.results-emoji {
  font-size: 2.5rem;
  margin-bottom: 12px;
  line-height: 1;
}

.results-heading {
  font-family: var(--font-mono);
  font-size: 1.05rem;
  color: var(--text-muted);
  margin-bottom: 36px;
  letter-spacing: 0.04em;
}

.results-metrics {
  display: flex;
  justify-content: center;
  gap: 48px;
  margin-bottom: 36px;
}

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

.result-value {
  font-family: var(--font-mono);
  font-size: 2.8rem;
  font-weight: 700;
  line-height: 1;
  color: var(--blue);
}

.result-metric:nth-child(2) .result-value { color: var(--green); }
.result-metric:nth-child(3) .result-value { color: var(--text-muted); font-size: 2rem; }

.result-label {
  font-size: 0.72rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  font-family: var(--font-mono);
}

.result-errors {
  font-family: var(--font-mono);
  font-size: 0.82rem;
  color: var(--text-muted);
  margin-bottom: 36px;
}

.result-errors.perfect { color: var(--green); }
.result-errors.has-errors { color: var(--orange); }

.results-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
}

.btn {
  font-family: var(--font-mono);
  font-size: 0.82rem;
  font-weight: 500;
  padding: 10px 22px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  cursor: pointer;
  transition: all 0.18s ease;
  letter-spacing: 0.03em;
}

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
}
.btn-ghost:hover {
  background: var(--surface-3);
  color: var(--text);
  border-color: var(--border-focus);
}

.btn-primary {
  background: rgba(122, 162, 247, 0.15);
  color: var(--blue);
  border-color: rgba(122, 162, 247, 0.3);
}
.btn-primary:hover {
  background: rgba(122, 162, 247, 0.25);
  border-color: var(--blue);
  box-shadow: 0 0 16px rgba(122, 162, 247, 0.2);
}

.btn .key-hint {
  font-size: 0.68rem;
  opacity: 0.5;
  margin-left: 6px;
}

/* ─── Footer ─────────────────────────────────────────────────── */
footer {
  margin-top: auto;
  padding: 20px 0;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.75rem;
  color: var(--text-muted);
  font-family: var(--font-mono);
}

.footer-shortcuts {
  display: flex;
  gap: 16px;
}

.shortcut {
  display: flex;
  align-items: center;
  gap: 5px;
}

kbd {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 1px 5px;
  font-size: 0.7rem;
  font-family: var(--font-mono);
  color: var(--text-muted);
}

/* ─── Scrollbar ──────────────────────────────────────────────── */
#codeDisplay::-webkit-scrollbar { height: 4px; }
#codeDisplay::-webkit-scrollbar-track { background: transparent; }
#codeDisplay::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 2px;
}

/* ─── Animations for completion ──────────────────────────────── */
#codeDisplay.completed .st-correct {
  animation: correctPulse 0.5s ease;
}

@keyframes correctPulse {
  0%   { filter: brightness(1); }
  50%  { filter: brightness(1.4); }
  100% { filter: brightness(1); }
}

/* ─── Responsive ─────────────────────────────────────────────── */
@media (max-width: 640px) {
  .page-wrap { padding: 0 16px 32px; }

  header { padding: 20px 0 16px; }

  .stats-bar { gap: 20px; }
  .stat-value { font-size: 1.3rem; }

  .code-panel { padding: 20px 18px; }
  #codeDisplay { font-size: 0.82rem; }

  .results-card {
    min-width: auto;
    width: calc(100vw - 32px);
    padding: 36px 28px;
  }

  .results-metrics { gap: 28px; }
  .result-value { font-size: 2.2rem; }

  .footer-shortcuts { display: none; }
}

/* ─── Focus indicator for accessibility ──────────────────────── */
.code-panel:focus-within #typingCursor {
  animation: cursorBlink 1.1s step-end infinite;
}

/* Pause blink when not focused */
.code-panel:not(:focus-within) #typingCursor {
  animation: none;
  opacity: 0.4;
}
