/* ============================================
   Cognitive Lab — Design System
   A refined, focused training environment
   ============================================ */

/* --- Design Tokens --- */
:root {
  /* Typography */
  --font-heading: 'Space Grotesk', system-ui, sans-serif;
  --font-body: 'DM Sans', system-ui, sans-serif;
  --font-mono: 'JetBrains Mono', 'Courier New', monospace;

  /* Base scale */
  --fs-xs: 0.75rem;
  --fs-sm: 0.875rem;
  --fs-base: 1rem;
  --fs-lg: 1.125rem;
  --fs-xl: 1.375rem;
  --fs-2xl: 1.75rem;
  --fs-3xl: 2.25rem;
  --fs-4xl: 3rem;
  --fs-5xl: 4rem;

  /* Spacing */
  --sp-1: 4px;
  --sp-2: 8px;
  --sp-3: 12px;
  --sp-4: 16px;
  --sp-5: 20px;
  --sp-6: 24px;
  --sp-8: 32px;
  --sp-10: 40px;
  --sp-12: 48px;
  --sp-16: 64px;
  --sp-20: 80px;

  /* Radii */
  --r-sm: 6px;
  --r-md: 10px;
  --r-lg: 16px;
  --r-xl: 24px;
  --r-full: 999px;

  /* Transitions */
  --t-fast: 0.15s ease;
  --t-base: 0.25s ease;
  --t-slow: 0.4s cubic-bezier(0.4, 0, 0.2, 1);

  /* Light theme (default) */
  --bg: #F4F2EC;
  --bg-grid: rgba(27, 27, 47, 0.03);
  --surface: #FFFFFF;
  --surface-2: #FAF8F3;
  --surface-3: #F0EDE5;
  --text: #1B1B2F;
  --text-2: #5C5C70;
  --text-3: #9E9EAE;
  --border: #E2DED3;
  --border-2: #D4D0C2;
  --shadow-sm: 0 1px 3px rgba(27, 27, 47, 0.06), 0 1px 2px rgba(27, 27, 47, 0.04);
  --shadow-md: 0 4px 16px rgba(27, 27, 47, 0.08), 0 2px 6px rgba(27, 27, 47, 0.04);
  --shadow-lg: 0 12px 40px rgba(27, 27, 47, 0.12), 0 4px 12px rgba(27, 27, 47, 0.06);
  --shadow-glow: 0 0 0 4px var(--accent-soft);

  /* Primary */
  --primary: #4F46E5;
  --primary-hover: #4338CA;
  --primary-soft: rgba(79, 70, 229, 0.12);
  --primary-text: #FFFFFF;

  /* Category colors */
  --cat-attention: #E11D48;
  --cat-attention-soft: rgba(225, 29, 72, 0.12);
  --cat-reaction: #EA580C;
  --cat-reaction-soft: rgba(234, 88, 12, 0.12);
  --cat-memory: #4F46E5;
  --cat-memory-soft: rgba(79, 70, 229, 0.12);
  --cat-logic: #059669;
  --cat-logic-soft: rgba(5, 150, 105, 0.12);

  /* Semantic */
  --success: #16A34A;
  --success-soft: rgba(22, 163, 74, 0.12);
  --error: #DC2626;
  --error-soft: rgba(220, 38, 38, 0.12);
  --warning: #D97706;
  --warning-soft: rgba(217, 119, 6, 0.12);
  --info: #0284C7;
  --info-soft: rgba(2, 132, 199, 0.12);

  --accent: var(--primary);
  --accent-soft: var(--primary-soft);
  --accent-text: var(--primary-text);
}

[data-theme="dark"] {
  --bg: #12121F;
  --bg-grid: rgba(255, 255, 255, 0.025);
  --surface: #1C1C2E;
  --surface-2: #23233A;
  --surface-3: #2A2A44;
  --text: #F0EEF8;
  --text-2: #A8A8C0;
  --text-3: #6E6E88;
  --border: #2E2E48;
  --border-2: #3A3A58;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.35);
  --shadow-lg: 0 12px 40px rgba(0, 0, 0, 0.45);

  --primary: #818CF8;
  --primary-hover: #A5B4FC;
  --primary-soft: rgba(129, 140, 248, 0.18);
  --primary-text: #12121F;

  --cat-attention: #FB7185;
  --cat-attention-soft: rgba(251, 113, 133, 0.18);
  --cat-reaction: #FB923C;
  --cat-reaction-soft: rgba(251, 146, 60, 0.18);
  --cat-memory: #818CF8;
  --cat-memory-soft: rgba(129, 140, 248, 0.18);
  --cat-logic: #34D399;
  --cat-logic-soft: rgba(52, 211, 153, 0.18);

  --success: #4ADE80;
  --success-soft: rgba(74, 222, 128, 0.18);
  --error: #F87171;
  --error-soft: rgba(248, 113, 113, 0.18);
  --warning: #FBBF24;
  --warning-soft: rgba(251, 191, 36, 0.18);
  --info: #38BDF8;
  --info-soft: rgba(56, 189, 248, 0.18);
}

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

html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: var(--font-body);
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
  min-height: 100dvh;
  background-image:
    linear-gradient(var(--bg-grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--bg-grid) 1px, transparent 1px);
  background-size: 32px 32px;
  transition: background-color var(--t-base), color var(--t-base);
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  touch-action: manipulation;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

h1, h2, h3, h4, h5 {
  font-family: var(--font-heading);
  font-weight: 600;
  line-height: 1.25;
  letter-spacing: -0.02em;
}

a { color: inherit; text-decoration: none; }
button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }
input, select, textarea { font-family: inherit; font-size: inherit; }

/* --- Utility --- */
.sr-only {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}

.fade-in { animation: fadeIn var(--t-base) ease both; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

.fade-in-up { animation: fadeInUp var(--t-slow) ease both; }
@keyframes fadeInUp { from { opacity: 0; transform: translateY(24px); } to { opacity: 1; transform: translateY(0); } }

.scale-in { animation: scaleIn var(--t-base) ease both; }
@keyframes scaleIn { from { opacity: 0; transform: scale(0.92); } to { opacity: 1; transform: scale(1); } }

.pop { animation: pop 0.3s cubic-bezier(0.34, 1.56, 0.64, 1); }
@keyframes pop { 0% { transform: scale(1); } 50% { transform: scale(1.08); } 100% { transform: scale(1); } }

.shake { animation: shake 0.4s ease; }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-6px); }
  40%, 80% { transform: translateX(6px); }
}

/* --- Buttons --- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  padding: var(--sp-3) var(--sp-5);
  border-radius: var(--r-md);
  font-family: var(--font-body);
  font-weight: 600;
  font-size: var(--fs-base);
  transition: all var(--t-fast);
  white-space: nowrap;
  user-select: none;
}

.btn-primary {
  background: var(--primary);
  color: var(--primary-text);
  box-shadow: var(--shadow-sm);
}
.btn-primary:hover { background: var(--primary-hover); transform: translateY(-1px); box-shadow: var(--shadow-md); }
.btn-primary:active { transform: translateY(0); }

.btn-ghost {
  background: transparent;
  color: var(--text-2);
  border: 1px solid var(--border);
}
.btn-ghost:hover { background: var(--surface-2); color: var(--text); border-color: var(--border-2); }

.btn-soft {
  background: var(--surface-3);
  color: var(--text);
}
.btn-soft:hover { background: var(--border); }

.btn-lg { padding: var(--sp-4) var(--sp-8); font-size: var(--fs-lg); border-radius: var(--r-lg); }
.btn-sm { padding: var(--sp-2) var(--sp-3); font-size: var(--fs-sm); }

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

/* --- Badges & Pills --- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-1);
  padding: 3px var(--sp-2);
  border-radius: var(--r-full);
  font-size: var(--fs-xs);
  font-weight: 600;
  letter-spacing: 0.03em;
}

/* --- Cards --- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-sm);
}

/* --- Inputs --- */
.select, .input {
  padding: var(--sp-3) var(--sp-4);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: var(--surface);
  color: var(--text);
  font-size: var(--fs-base);
  transition: border-color var(--t-fast), box-shadow var(--t-fast);
  outline: none;
}
.select:focus, .input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

/* --- Scrollbar --- */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-2); border-radius: var(--r-full); }
::-webkit-scrollbar-thumb:hover { background: var(--text-3); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

/* --- Mobile Optimizations --- */
@media (max-width: 480px) {
  html { font-size: 15px; }
  :root {
    --fs-5xl: 2.5rem;
    --fs-4xl: 2rem;
    --fs-3xl: 1.75rem;
  }
}

/* Touch-friendly: larger tap targets on mobile */
@media (max-width: 640px) {
  .btn { padding: var(--sp-3) var(--sp-4); min-height: 44px; }
  .btn-sm { min-height: 36px; }
  .option-btn { padding: var(--sp-2) var(--sp-3); min-height: 40px; }
  .game-back { min-height: 40px; padding: var(--sp-2) var(--sp-3); }
  .theme-toggle { width: 40px; height: 40px; }
  .select, .input { min-height: 44px; font-size: 16px; }
}

/* Prevent text selection on game UI (cards, buttons) */
@media (max-width: 640px) {
  .game-card, .option-btn, .btn, .game-back {
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
  }
}
