:root[data-theme="light"] {
  color-scheme: light;
  --bg-color: #ffffff;
  --main-color: #1976d2;
  --accent-color-rgb: 25, 118, 210;
  --sub-color: #64748b;
  --text-color: #1e293b;
  --caret-color: #1976d2;
  --error-color: #ef4444;
  --error-extra-color: #7e2a33;
  --colorful-error-color: #ef4444;
  --colorful-error-extra-color: #7e2a33;
  --sidebar-bg: #f1f5f9;
  --brand-navy: #0f2b50;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg-color: #1a1f2e;
  --main-color: #60a5fa;
  --accent-color-rgb: 96, 165, 250;
  --sub-color: #94a3b8;
  --text-color: #f8fafc;
  --caret-color: #60a5fa;
  --error-color: #ef4444;
  --error-extra-color: #7e2a33;
  --colorful-error-color: #ef4444;
  --colorful-error-extra-color: #7e2a33;
  --sidebar-bg: #252d3d;
  --brand-navy: #ffffff;
}

* {
  box-sizing: border-box;
}

body,
.main-global-header,
.segment-btn,
.icon-btn,
.card,
.footer-section,
.logo-title {
  transition: background-color 0.25s, color 0.25s, border-color 0.25s, box-shadow 0.25s;
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  min-height: 100vh;
  min-height: 100dvh;
  background:
    radial-gradient(circle at 12% -8%, color-mix(in srgb, var(--accent-color) 12%, transparent) 0%, transparent 45%),
    radial-gradient(circle at 88% 110%, color-mix(in srgb, var(--accent-secondary, #3b82f6) 10%, transparent) 0%, transparent 42%),
    var(--bg-primary, var(--bg-color));
  background-attachment: fixed;
  color: var(--text-primary, var(--text-color));
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  overflow-x: hidden;
  overflow-y: auto;
}

body:not([data-shop-theme]) {
  --bg-primary: var(--bg-color);
  --text-primary: var(--text-color);
  --text-secondary: var(--sub-color);
  --accent-color: var(--main-color);
}

#app {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-height: 100vh;
  min-height: 100dvh;
  height: auto;
  max-width: 1250px;
  margin: 0 auto;
  padding: 1rem 2rem 0;
  opacity: 1;
  transition: opacity 0.2s;
}

@media (max-height: 700px) {
  #app {
    padding-top: 0.5rem;
  }

  header {
    padding-bottom: 0.5rem !important;
  }

  main {
    padding-top: 1vh !important;
  }

  .site-heading {
    margin-bottom: 0.8rem !important;
    font-size: 1.4rem !important;
  }

  footer {
    padding-top: 1rem !important;
    gap: 0.5rem !important;
  }
}

/* Quick Switch Transition Animation - REMOVED for speed */


header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 1rem;
  position: relative;
  user-select: none;
}

header::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 1px;
  background: linear-gradient(90deg,
      transparent 0%,
      color-mix(in srgb, var(--accent-color) 18%, transparent) 18%,
      color-mix(in srgb, var(--accent-color) 30%, transparent) 50%,
      color-mix(in srgb, var(--accent-color) 18%, transparent) 82%,
      transparent 100%);
  pointer-events: none;
}

.logo {
  font-size: 1.3rem;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.logo-title {
  font-size: 1.3rem;
  line-height: 1;
}

.nav-right {
  display: flex;
  gap: 0.4rem;
  align-items: center;
}

.icon-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  transition: all 0.2s;
  padding: 0;
}

.icon-btn:hover {
  background: rgba(0, 0, 0, 0.05);
  color: var(--accent-color);
}

.icon-btn.active {
  color: var(--accent-color);
  background: rgba(var(--accent-color-rgb, 25, 118, 210), 0.1);
}

[data-theme="dark"] .icon-btn.active {
  background: rgba(var(--accent-color-rgb, 96, 165, 250), 0.15);
}

main {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  padding-top: 2vh;
  position: relative;
}

.site-heading {
  margin: 0 auto 1.2rem;
  font-size: 1.7rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  color: var(--text-primary);
  opacity: 1;
  text-align: center;
  max-width: 720px;
  line-height: 1.35;
  transition: opacity 0.4s ease, transform 0.4s ease, max-height 0.4s ease, margin 0.4s ease;
  overflow: hidden;
}

.stats-container {
  display: flex;
  justify-content: center;
  gap: clamp(3.5rem, 16vw, 12rem);
  width: 100%;
  font-size: 2.2rem;
  font-weight: 600;
  color: var(--accent-color);
  margin-bottom: 1rem;
  opacity: 1;
  transition: opacity 0.2s;
}

#timer {
  color: var(--accent-color);
}

#text-display {
  font-family: 'Roboto', sans-serif;
  font-size: 2rem;
  line-height: 1.5;
  word-break: normal;
  overflow-wrap: normal;
  user-select: none;
  position: relative;
  outline: none;
  height: 9.7rem;
  /* 3 lines + extra bottom room so descenders (g/j/p/q/y) are never clipped */
  overflow: hidden;
  color: var(--text-secondary);
  letter-spacing: 0.05em;
}

.word {
  display: inline-block;
  margin-right: 0.6em;
  /* More space between words */
  border-bottom: 2px solid transparent;
  /* Placeholder for error underline */
  transition: border-color 0.2s;
}

.word.error {
  border-bottom-color: var(--error-color);
}

.char {
  display: inline-block;
  /* Essential for accurate getBoundingClientRect */
  border-bottom: 2px solid transparent;
  transition: color 0.08s ease;
  vertical-align: top;
}

.correct {
  color: var(--text-primary);
}

.incorrect {
  color: var(--error-color);
}

.char.extra {
  opacity: 0.6;
}

.char.skipped {
  border-bottom: 2px solid var(--error-color);
}

#caret {
  position: absolute;
  width: 4px;
  height: 2.4rem;
  background-color: var(--accent-color);
  box-shadow: 0 0 10px var(--accent-color);
  border-radius: 4px;
  transition: opacity 0.1s ease, height 0.08s linear;
  animation: caretFlash 1s infinite;
  pointer-events: none;
  z-index: 10;
  will-change: transform;
  backface-visibility: hidden;
  contain: paint;
  top: 0;
  left: 0;
}

#caret.no-blink {
  animation: none;
  opacity: 1;
}

@keyframes caretFlash {

  0%,
  100% {
    opacity: 1;
  }

  50% {
    opacity: 0;
  }
}

.hidden-input {
  position: absolute;
  opacity: 0;
  top: 0;
  left: 0;
  pointer-events: none;
}

footer {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: var(--text-primary);
  font-size: 0.8rem;
  gap: 0.4rem;
  padding: 0.1rem 1rem 0.5rem;
  user-select: none;
  text-align: center;
  transition: opacity 0.4s ease, transform 0.4s ease, max-height 0.6s cubic-bezier(0.4, 0, 0.2, 1), padding 0.4s ease;
  overflow: hidden;
}

.footer-nav {
  display: flex;
  justify-content: center;
  gap: 0.8rem;
  flex-wrap: wrap;
  margin-bottom: 0.3rem;
}

.home-seo-section {
  max-width: 760px;
  margin: 0 auto;
  text-align: center;
  line-height: 1.45;
  font-size: 0.76rem;
  color: var(--text-primary);
  opacity: 1;
  padding: 0 0.5rem;
}

.seo-visible-line {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  column-gap: 0.4rem;
  margin-bottom: 0.15rem;
}


.seo-visible-line .sep {
  margin: 0 0.4rem;
  opacity: 0.4;
}

.home-seo-section h2 {
  margin: 0.4rem 0 0.2rem;
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--text-primary);
}

.seo-body {
  max-height: 100px;
  /* Show a few lines initially */
  overflow: hidden;
  position: relative;
  transition: max-height 0.6s cubic-bezier(0.4, 0, 0.2, 1);
  mask-image: linear-gradient(to bottom, black 50%, transparent 100%);
  -webkit-mask-image: linear-gradient(to bottom, black 50%, transparent 100%);
}

.seo-body.expanded {
  max-height: 2000px;
  mask-image: none;
  -webkit-mask-image: none;
}

.home-seo-section p {
  margin: 0.15rem 0 0;
}

.home-seo-section a {
  color: var(--accent-color);
  text-decoration: none;
  font-weight: 500;
}

.home-seo-section a:hover {
  text-decoration: underline;
}

/* Collapsible SEO body */
.seo-body {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.4s ease, opacity 0.3s ease;
  opacity: 0;
}

.seo-body.expanded {
  max-height: 12rem;
  overflow-y: auto;
  opacity: 1;
  padding-right: 8px;
  /* Room for scrollbar */
}

/* Custom Scrollbar for SEO Body */
.seo-body::-webkit-scrollbar {
  width: 4px;
}

.seo-body::-webkit-scrollbar-track {
  background: transparent;
}

.seo-body::-webkit-scrollbar-thumb {
  background: var(--accent-color);
  opacity: 0.3;
  border-radius: 10px;
}

.seo-body:hover::-webkit-scrollbar-thumb {
  opacity: 0.6;
}

.seo-body {
  scrollbar-width: thin;
  scrollbar-color: var(--accent-color) transparent;
}

.seo-read-more {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  margin-top: 0.15rem;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 0.72rem;
  color: var(--accent-color);
  font-weight: 600;
  padding: 0;
  transition: opacity 0.2s;
}

.seo-read-more:hover,
.seo-read-more:active {
  opacity: 0.75;
  text-decoration: none !important;
}

.seo-read-more .arrow {
  display: inline-block;
  transition: transform 0.3s ease;
  font-size: 0.6rem;
}

.seo-read-more.open .arrow {
  transform: rotate(180deg);
}

/* Smooth fade-out and collapse for site heading during test, and footer when mobile keyboard is open */
#app.engaged .site-heading,
#app.running .site-heading,
#app.mobile-keyboard-open footer {
  opacity: 0;
  max-height: 0;
  margin-top: 0;
  margin-bottom: 0;
  padding-top: 0;
  padding-bottom: 0;
  pointer-events: none;
  transform: translateY(-10px);
}

/* Footer fade out in running state without collapsing height to prevent text shifting */
#app.running footer {
  opacity: 0;
  pointer-events: none;
}

.home-seo-section .seo-body a,
.home-seo-section .seo-visible-line a {
  color: var(--accent-color);
  text-decoration: none;
  font-weight: 600;
  transition: color 0.2s, opacity 0.2s;
}

.home-seo-section .seo-body a:hover,
.home-seo-section .seo-visible-line a:hover {
  opacity: 0.8;
  text-decoration: underline;
}

.home-seo-section .footer-nav a {
  color: var(--text-tertiary);
  font-weight: 500;
}

.home-seo-section .footer-nav a:hover {
  color: var(--text-primary);
  opacity: 1;
  text-decoration: none;
}


.toggle-switch {
  width: 40px;
  height: 20px;
  background: var(--text-secondary);
  border-radius: 10px;
  position: relative;
  cursor: pointer;
  transition: 0.3s;
}

/* Attribution Styles */
.attribution-container {
  margin-top: 0.5rem;
  font-size: 0.8rem;
  color: var(--text-secondary);
  display: flex;
  justify-content: flex-end;
  align-items: center;
  opacity: 0;
  transition: opacity 0.3s;
  min-height: 1.2rem;
}

.source-citation {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  background: rgba(0, 0, 0, 0.03);
  padding: 0.2rem 0.6rem;
  border-radius: 4px;
}

[data-theme="dark"] .source-citation {
  background: rgba(255, 255, 255, 0.05);
}

.source-citation a {
  color: var(--accent-color);
  text-decoration: none;
  font-weight: 500;
}

.source-citation a:hover {
  text-decoration: underline;
}

.license-badge {
  font-size: 0.7rem;
  opacity: 0.7;
  border: 1px solid var(--text-secondary);
  padding: 0 0.2rem;
  border-radius: 3px;
}

.original-badge {
  background-color: var(--accent-color);
  color: white;
  padding: 0 0.4rem;
  border-radius: 3px;
  font-size: 0.7rem;
  font-weight: bold;
}

.toggle-switch::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  background: var(--bg-primary);
  border-radius: 50%;
  transition: 0.3s;
}

.toggle-switch.active {
  background: var(--accent-color);
}

.toggle-switch.active::after {
  left: 22px;
}

.menu-btn,
.icon-btn {
  background: none;
  border: none;
  color: var(--text-primary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.2s, opacity 0.2s;
}

.menu-btn:hover,
.icon-btn:hover {
  opacity: 0.7;
}

#menu-btn {
  width: 36px;
  height: 36px;
  padding: 0;
  border-radius: 8px;
  color: var(--text-primary);
  flex-shrink: 0;
}

#menu-btn svg {
  display: block;
  width: 22px;
  height: 22px;
  stroke-width: 2.35;
}

@media (max-width: 600px) {
  #app {
    padding: 0.8rem;
    gap: 0.5rem;
  }

  header::after {
    left: 4px;
    right: 4px;
  }

  #text-display {
    font-size: 1.5rem;
    height: 7.8rem;
    /* Keep 3 visible lines while preserving full descenders on line 3 */
  }

  #caret {
    height: 1.5rem;
  }

  .stats-container {
    font-size: 1.6rem;
    font-weight: 600;
  }

  .site-heading {
    font-size: clamp(1.12rem, 4.9vw, 1.34rem);
    margin-bottom: 0.8rem;
    line-height: 1.3;
  }

  .home-seo-section {
    font-size: 0.72rem;
  }

  .footer-nav {
    gap: 0.6rem;
    font-size: 0.72rem;
  }

  .home-seo-section h2 {
    font-size: 1rem;
    margin-top: 0.4rem;
  }

  /* Mobile Results Polish */
  .result-content {
    padding: 1.5rem;
    gap: 1.5rem;
  }

  .wpm-big {
    font-size: 3.5rem;
  }

  .wpm-label {
    font-size: 1rem;
  }

  .chart-container {
    height: 200px;
  }

  .stats-grid-new {
    gap: 1.2rem;
    grid-template-columns: 1fr 1fr;
    /* 2 columns on mobile */
  }

  .stat-box .stat-value {
    font-size: 1.4rem;
  }

  .result-footer {
    flex-direction: column;
  }

  .result-footer .setting-btn {
    width: 100%;
    justify-content: center;
  }
}

@media (max-width: 420px) {
  header {
    padding-bottom: 0.6rem;
  }

  .logo-title {
    font-size: 1.1rem;
  }

  .logo img {
    width: 30px !important;
    height: 30px !important;
  }

  .nav-right {
    gap: 0.1rem;
  }

  .icon-btn,
  .menu-btn {
    width: 32px;
    height: 32px;
  }

  .icon-btn svg,
  .menu-btn svg {
    width: 18px;
    height: 18px;
  }

  /* Login icon (user) has a cleaner design and often looks smaller, bump it to 21px for visual parity */
  #quick-switch-btn svg,
  #quick-logout-btn svg {
    width: 21px;
    height: 21px;
  }
}

/* Advanced CTA Styles */
.advanced-cta {
  display: inline-flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.55rem 1rem;
  background: rgba(var(--accent-color-rgb, 25, 118, 210), 0.08);
  color: var(--accent-color);
  text-decoration: none;
  border-radius: 100px;
  font-size: 0.9rem;
  font-weight: 500;
  margin-bottom: 1.5rem;
  align-self: center;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  border: 1px solid rgba(var(--accent-color-rgb, 25, 118, 210), 0.15);
  backdrop-filter: blur(4px);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.03);
}

[data-theme="dark"] .advanced-cta {
  background: rgba(var(--accent-color-rgb, 96, 165, 250), 0.12);
  border-color: rgba(var(--accent-color-rgb, 96, 165, 250), 0.2);
}

.advanced-cta:hover {
  background: var(--accent-color);
  color: white;
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(var(--accent-color-rgb, 25, 118, 210), 0.25);
}

.advanced-cta svg {
  transition: transform 0.3s ease;
}

.advanced-cta:hover svg {
  transform: translateX(3px);
}

.advanced-cta .cta-text {
  letter-spacing: 0.02em;
}

.advanced-cta .badge {
  font-size: 0.7rem;
  background: var(--accent-color);
  color: white;
  padding: 1px 6px;
  border-radius: 4px;
  margin-left: 0.2rem;
  text-transform: uppercase;
  font-weight: 700;
  transition: background 0.3s;
}

.advanced-cta:hover .badge {
  background: white;
  color: var(--accent-color);
}

.footer-social {
  display: flex;
  justify-content: center;
  gap: 0.6rem;
  margin-top: 0.3rem;
  margin-bottom: 0.3rem;
}

.social-link {
  color: var(--text-secondary);
  transition: color 0.2s, transform 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.social-link:hover {
  color: var(--accent-color);
  transform: translateY(-2px);
}

.social-link svg {
  width: 20px;
  height: 20px;
}
