@layer base {
  @keyframes enter {
    from { opacity: 0; transform: translateY(2px); }
    to { opacity: 1; transform: translateY(0); }
  }

  @keyframes shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-1px); }
    75% { transform: translateX(1px); }
  }

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

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

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

  :root {
    --font-sans: 'Inter', system-ui, -apple-system, sans-serif;
    --background: 0 0% 100%;
    --foreground: 240 10% 3.9%;
    --card: 0 0% 100%;
    --card-foreground: 240 10% 3.9%;
    --popover: 0 0% 100%;
    --popover-foreground: 240 10% 3.9%;
    --primary: 240 5.9% 10%;
    --primary-foreground: 0 0% 98%;
    --secondary: 240 4.8% 95.9%;
    --secondary-foreground: 240 5.9% 10%;
    --muted: 240 4.8% 95.9%;
    --muted-foreground: 240 3.8% 46.1%;
    --accent: 240 4.8% 95.9%;
    --accent-foreground: 240 5.9% 10%;
    --destructive: 0 84.2% 60.2%;
    --destructive-foreground: 0 0% 98%;
    --border: 240 5.9% 90%;
    --input: 240 5.9% 90%;
    --ring: 240 5% 64.9%;
    --overlay: 0 0% 0%;
    --radius: 0.5rem;
    --color-correct: 142.1 76.2% 36.3%;
    --color-incorrect: 346 84% 61%;
    --caret-color: 221.2 83.2% 53.3%;
  }

  [data-theme="dark"] {
    --background: 0 0% 3.9%;
    --foreground: 0 0% 98%;
    --card: 0 0% 7%;
    --card-foreground: 0 0% 98%;
    --popover: 0 0% 7%;
    --popover-foreground: 0 0% 98%;
    --primary: 346.8 77.2% 49.8%;
    --primary-foreground: 355.7 100% 97.3%;
    --secondary: 240 3.7% 12%;
    --secondary-foreground: 0 0% 98%;
    --muted: 0 0% 12%;
    --muted-foreground: 240 5% 75%;
    --accent: 12 6.5% 12%;
    --accent-foreground: 0 0% 98%;
    --destructive: 0 62.8% 30.6%;
    --destructive-foreground: 0 85.7% 97.3%;
    --border: 240 3.7% 15.9%;
    --input: 240 3.7% 15.9%;
    --ring: 346.8 77.2% 49.8%;
  }
}

@layer components {
  body {
    font-family: var(--font-sans);
    background-color: hsl(var(--background));
    color: hsl(var(--foreground));
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0;
    padding: 1rem;
    transition: background-color 0.3s ease, color 0.3s ease;
  }

  .container {
    width: 100%;
    max-width: 950px;
    background-color: hsl(var(--card));
    border: 1px solid hsl(var(--border));
    border-radius: var(--radius);
    box-shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1);
    padding: 2rem;
    animation: enter 0.2s ease-out;
  }

  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
  }

  .header h1 {
    font-size: 1.5rem;
    font-weight: 600;
    color: hsl(var(--foreground));
    margin: 0;
  }

  .button-group {
    display: flex;
    gap: 0.5rem;
  }

  button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    height: 2.5rem;
    padding: 0 1rem;
    border-radius: var(--radius);
    border: 1px solid hsl(var(--border));
    background-color: hsl(var(--secondary));
    color: hsl(var(--secondary-foreground));
    font-size: 0.875rem;
    font-weight: 500;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    cursor: pointer;
    user-select: none;
  }

  button:hover {
    background-color: hsl(var(--accent));
    border-color: hsl(var(--ring));
    transform: translateY(-1px);
  }

  button:active {
    transform: translateY(1px);
  }

  .duration-options {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 1.5rem;
  }

  .progress-bar {
    width: 100%;
    height: 4px;
    background-color: hsl(var(--border));
    border-radius: var(--radius);
    overflow: hidden;
    margin-bottom: 1.5rem;
  }

  .progress {
    height: 100%;
    background-color: hsl(var(--primary));
    width: 100%;
    transition: width 1s linear;
  }

  #words {
    font-size: 1.25rem;
    line-height: 2.5rem;
    height: 35vh;
    padding: 1rem 0;
    overflow: hidden;
    position: relative;
    margin-bottom: 1.5rem;
    color: hsl(var(--muted-foreground));
    word-wrap: break-word;
  }

  .word {
    margin-right: 0.6rem;
    position: relative;
    height: 2.5rem;
    display: inline-flex;
    align-items: center;
  }

  .word.active {
    color: hsl(var(--foreground));
  }

  .char {
    transition: all 0.15s ease-out;
    position: relative;
  }

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

  .char.incorrect {
    color: hsl(var(--color-incorrect));
    animation: shake 0.15s ease-in-out;
  }

  .caret {
    position: absolute;
    width: 2px;
    height: 1.2em;
    background-color: hsl(var(--caret-color));
    animation: caretBlink 1s infinite;
  }

  #input {
    width: 100%;
    height: 3rem;
    padding: 0.5rem 1rem;
    margin: 0 0 1.5rem 0;
    background-color: hsl(var(--card));
    border: 2px solid hsl(var(--border));
    border-radius: var(--radius);
    color: hsl(var(--foreground));
    font-size: 1.25rem;
    line-height: 2rem;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  }

  #input:hover {
    border-color: hsl(var(--ring));
  }

  #input:focus {
    outline: none;
    border-color: hsl(var(--ring));
    box-shadow: 0 0 0 2px hsl(var(--ring) / 0.2);
  }

  .stats {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 2.5rem;
    color: hsl(var(--foreground));
  }

  .stat {
    display: inline-flex;
    align-items: center;
    font-weight: 600;
    transition: all 0.2s;
    padding: 0.5rem 1rem;
    border-radius: var(--radius);
  }

  .stat:hover {
    background-color: hsl(var(--accent));
  }

  .stat > span:first-child {
    display: none;
  }

  #wpm, #accuracy, #time-elapsed {
    font-size: 2.5rem;
    font-weight: 600;
    color: hsl(var(--foreground));
  }

  #wpm::after {
    content: " WPM";
    font-size: 1rem;
    font-weight: normal;
    opacity: 0.7;
    margin-left: 0.5rem;
  }

  #accuracy::after {
    content: "%";
    font-size: 1rem;
    font-weight: normal;
    opacity: 0.7;
    margin-left: 0.25rem;
  }

  #time-elapsed::after {
    content: "s";
    font-size: 1rem;
    font-weight: normal;
    opacity: 0.7;
    margin-left: 0.25rem;
  }

  #time-elapsed.warning {
    color: hsl(var(--destructive));
    animation: timerWarning 1s ease-in-out infinite;
  }

  /* Result Popup Styles */
  .result-popup {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: hsl(var(--overlay) / 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease-in-out;
    backdrop-filter: blur(4px);
    z-index: 1000;
  }

  .result-popup.show {
    opacity: 1;
    pointer-events: auto;
  }

  .result-content {
    background-color: hsl(var(--card));
    border: 1px solid hsl(var(--border));
    border-radius: var(--radius);
    padding: 2rem;
    width: 90%;
    max-width: 500px;
    animation: slideUp 0.3s ease-out;
  }

  .result-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
  }

  .result-header h2 {
    font-size: 1.5rem;
    font-weight: 600;
    margin: 0;
    color: hsl(var(--foreground));
  }

  .close-button {
    background: none;
    border: none;
    font-size: 1.5rem;
    color: hsl(var(--muted-foreground));
    cursor: pointer;
    padding: 0.5rem;
    border-radius: var(--radius);
    line-height: 1;
    transition: all 0.2s;
  }

  .close-button:hover {
    color: hsl(var(--foreground));
    background-color: hsl(var(--accent));
  }

  .result-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 2rem;
    margin-bottom: 2rem;
  }

  .result-stat {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
  }

  .result-label {
    font-size: 0.875rem;
    color: hsl(var(--muted-foreground));
    margin-bottom: 0.5rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
  }

  .result-value {
    font-size: 2rem;
    font-weight: 600;
    color: hsl(var(--foreground));
  }

  .result-actions {
    display: flex;
    justify-content: center;
    gap: 1rem;
    margin-top: 1.5rem;
  }

  .result-button {
    background-color: hsl(var(--primary));
    color: hsl(var(--primary-foreground));
    border: none;
    padding: 0.75rem 1.5rem;
    border-radius: var(--radius);
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
  }

  .result-button:hover {
    opacity: 0.9;
    transform: translateY(-1px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
  }

  .result-button:active {
    transform: translateY(1px);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  }
  
  /* Detailed Statistics Styles */
  .result-details {
    margin-top: 2rem;
    border-top: 1px solid hsl(var(--border));
    padding-top: 1.5rem;
  }
  
  .result-details h3 {
    font-size: 1.1rem;
    font-weight: 600;
    margin: 0 0 1rem 0;
    color: hsl(var(--foreground));
    text-align: center;
  }
  
  .result-detail-item {
    margin-bottom: 1.5rem;
  }
  
  .detail-label {
    font-size: 0.875rem;
    color: hsl(var(--muted-foreground));
    margin-bottom: 0.5rem;
    font-weight: 500;
  }
  
  .detail-value {
    font-size: 0.9rem;
    color: hsl(var(--foreground));
    padding: 0.75rem;
    background-color: hsl(var(--secondary));
    border-radius: var(--radius);
    max-height: 100px;
    overflow-y: auto;
  }
  
  .detail-graph {
    height: 100px;
    background-color: hsl(var(--secondary));
    border-radius: var(--radius);
    padding: 0.75rem;
    position: relative;
  }
}

@layer utilities {
  /* Focus mode styles */
  body.focus-mode {
    background-color: rgba(0, 0, 0, 0.95);
  }
  
  body.focus-mode .container {
    max-width: 750px;
    background-color: hsl(var(--card));
    box-shadow: 0 0 50px rgba(0, 0, 0, 0.3);
  }
  
  body.focus-mode-dark .container {
    box-shadow: 0 0 50px rgba(255, 255, 255, 0.1);
  }
  
  /* Animations */
  .word.correct {
    animation: correctWord 0.3s ease-in-out;
  }
  
  @keyframes correctWord {
    0% { transform: translateY(0); }
    50% { transform: translateY(-2px); }
    100% { transform: translateY(0); }
  }
  
  /* Responsive styles */
  @media (max-width: 640px) {
    .container { padding: 1rem; }
    .header {
      flex-direction: column;
      gap: 1rem;
      text-align: center;
    }
    .stats {
      flex-direction: column;
      gap: 1rem;
      text-align: center;
    }
    .result-grid {
      grid-template-columns: 1fr;
      gap: 1rem;
    }
  }
  
  /* Enhanced UI elements */
  .duration-button.active {
    background-color: hsl(var(--primary));
    color: hsl(var(--primary-foreground));
    border-color: transparent;
  }
  
  #input:focus {
    box-shadow: 0 0 0 3px hsl(var(--ring) / 0.3);
    transition: all 0.2s ease;
  }
  
  .detail-graph {
    overflow: hidden;
  }
}
