/**
 * AI-SCHOOL — Base Stylesheet & Design Tokens
 *
 * Responsibility:
 * - Define Light Mode (:root) and Dark Mode ([data-theme="dark"]) CSS custom properties.
 * - Prevent white-flash (FOUC) on dark OS themes before JS initializes.
 * - Provide a clean mobile-first CSS reset tailored for 360px-430px screens and Android TWA.
 * - Establish typography, spacing, radius, elevation, z-index scales, and core layout helpers.
 * - Enforce accessibility defaults (focus rings, screen-reader utility, reduced motion).
 *
 * Dependencies: None (always loaded first on every page inside app/).
 */

/* ==========================================================================
   1. DESIGN TOKENS — LIGHT MODE (DEFAULT)
   ========================================================================== */
:root {
  color-scheme: light;

  /* Core Brand & Surface Palette */
  --color-bg: #F6FAFF;
  --color-surface: #FFFFFF;
  --color-surface-subtle: #E8F2FD;
  --color-border: #D2E6FB;
  --color-border-strong: #A8CEF6;

  /* Primary Accent Scale */
  --color-primary: #4F8EF7;
  --color-primary-strong: #2E6FE0;
  --color-primary-muted: #A8CEF6;
  --color-primary-tint: #E8F2FD;
  --color-on-primary: #FFFFFF;

  /* Typography Colors (WCAG AA compliant on light surfaces) */
  --color-text-primary: #0F172A;
  --color-text-secondary: #475569;
  --color-text-muted: #64748B;
  --color-text-link: #2E6FE0;

  /* Semantic Signal Colors */
  --color-success: #22C55E;
  --color-success-bg: #E3FBEA;
  --color-success-text: #15803D;

  --color-danger: #FF7A59;
  --color-danger-bg: #FFE9E2;
  --color-danger-text: #C2410C;

  /* Gamification Accent (used sparingly for Coin/Streak highlights) */
  --color-gold: #F59E0B;
  --color-gold-bg: #FEF3C7;
  --color-gold-text: #B45309;

  /* Overlays & Elevation Shadows */
  --color-backdrop: rgba(15, 23, 42, 0.45);
  --shadow-xs: 0 1px 2px rgba(15, 23, 42, 0.05);
  --shadow-sm: 0 2px 8px rgba(46, 111, 224, 0.08);
  --shadow-md: 0 6px 20px rgba(46, 111, 224, 0.12);
  --shadow-lg: 0 12px 32px rgba(15, 23, 42, 0.16);

  /* Typography */
  --font-family-base: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    "Helvetica Neue", Arial, sans-serif;
  --font-family-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
    "Liberation Mono", monospace;

  --font-size-xs: 0.75rem;     /* 12px */
  --font-size-sm: 0.875rem;    /* 14px */
  --font-size-md: 1rem;        /* 16px */
  --font-size-lg: 1.125rem;    /* 18px */
  --font-size-xl: 1.25rem;     /* 20px */
  --font-size-2xl: 1.5rem;     /* 24px */

  --font-weight-regular: 400;
  --font-weight-medium: 500;
  --font-weight-semibold: 600;
  --font-weight-bold: 700;

  --line-height-tight: 1.25;
  --line-height-base: 1.5;
  --line-height-relaxed: 1.65;

  /* Spacing Scale (4px base unit) */
  --space-1: 0.25rem;  /* 4px */
  --space-2: 0.5rem;   /* 8px */
  --space-3: 0.75rem;  /* 12px */
  --space-4: 1rem;     /* 16px */
  --space-5: 1.25rem;  /* 20px */
  --space-6: 1.5rem;   /* 24px */
  --space-8: 2rem;     /* 32px */
  --space-10: 2.5rem;  /* 40px */

  /* Border Radius Scale */
  --radius-sm: 0.5rem;     /* 8px */
  --radius-md: 0.75rem;    /* 12px */
  --radius-lg: 1rem;       /* 16px */
  --radius-xl: 1.25rem;    /* 20px */
  --radius-full: 9999px;

  /* Mobile Layout & Touch Geometry */
  --container-max-width: 480px;
  --header-height: 56px;
  --navbar-height: 64px;
  --touch-target-min: 44px;

  /* Safe-Area Insets for Notched Phones & Android TWA Gesture Bar */
  --safe-area-top: env(safe-area-inset-top, 0px);
  --safe-area-bottom: env(safe-area-inset-bottom, 0px);
  --safe-area-left: env(safe-area-inset-left, 0px);
  --safe-area-right: env(safe-area-inset-right, 0px);

  /* Motion Timing */
  --transition-fast: 150ms ease;
  --transition-base: 220ms ease;
  --transition-slow: 320ms cubic-bezier(0.22, 1, 0.36, 1);

  /* Centralized Z-Index Layers (prevents ad-hoc z-index collisions) */
  --z-base: 1;
  --z-sticky: 20;
  --z-header: 30;
  --z-navbar: 40;
  --z-backdrop: 50;
  --z-sheet: 60;
  --z-modal: 70;
  --z-toast: 80;
}

/* ==========================================================================
   2. DESIGN TOKENS — DARK MODE (DEEP SLATE / NAVY)
   ========================================================================== */
:root[data-theme="dark"] {
  color-scheme: dark;

  --color-bg: #0B1220;
  --color-surface: #131E32;
  --color-surface-subtle: #1B2A44;
  --color-border: #263B5E;
  --color-border-strong: #375382;

  --color-primary: #4F8EF7;
  --color-primary-strong: #7CB0FA;
  --color-primary-muted: #2B4A78;
  --color-primary-tint: rgba(79, 142, 247, 0.16);
  --color-on-primary: #FFFFFF;

  --color-text-primary: #F1F5F9;
  --color-text-secondary: #CBD5E1;
  --color-text-muted: #94A3B8;
  --color-text-link: #A8CEF6;

  --color-success: #22C55E;
  --color-success-bg: rgba(34, 197, 94, 0.16);
  --color-success-text: #86EFAC;

  --color-danger: #FF7A59;
  --color-danger-bg: rgba(255, 122, 89, 0.16);
  --color-danger-text: #FDBA9F;

  --color-gold: #FBBF24;
  --color-gold-bg: rgba(251, 191, 36, 0.16);
  --color-gold-text: #FDE68A;

  --color-backdrop: rgba(2, 6, 23, 0.72);
  --shadow-xs: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-sm: 0 2px 10px rgba(0, 0, 0, 0.35);
  --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.45);
  --shadow-lg: 0 16px 36px rgba(0, 0, 0, 0.55);
}

/* Prevent light-mode flash (FOUC) before theme-service.js runs when OS is in dark mode */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;

    --color-bg: #0B1220;
    --color-surface: #131E32;
    --color-surface-subtle: #1B2A44;
    --color-border: #263B5E;
    --color-border-strong: #375382;

    --color-primary: #4F8EF7;
    --color-primary-strong: #7CB0FA;
    --color-primary-muted: #2B4A78;
    --color-primary-tint: rgba(79, 142, 247, 0.16);
    --color-on-primary: #FFFFFF;

    --color-text-primary: #F1F5F9;
    --color-text-secondary: #CBD5E1;
    --color-text-muted: #94A3B8;
    --color-text-link: #A8CEF6;

    --color-success: #22C55E;
    --color-success-bg: rgba(34, 197, 94, 0.16);
    --color-success-text: #86EFAC;

    --color-danger: #FF7A59;
    --color-danger-bg: rgba(255, 122, 89, 0.16);
    --color-danger-text: #FDBA9F;

    --color-gold: #FBBF24;
    --color-gold-bg: rgba(251, 191, 36, 0.16);
    --color-gold-text: #FDE68A;

    --color-backdrop: rgba(2, 6, 23, 0.72);
    --shadow-xs: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-sm: 0 2px 10px rgba(0, 0, 0, 0.35);
    --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.45);
    --shadow-lg: 0 16px 36px rgba(0, 0, 0, 0.55);
  }
}

/* ==========================================================================
   3. MOBILE-FIRST RESET & ELEMENT DEFAULTS
   ========================================================================== */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  /* Prevent blue highlight flash on Android taps; focus-visible handles keyboard focus */
  -webkit-tap-highlight-color: transparent;
  scroll-behavior: smooth;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  font-family: var(--font-family-base);
  font-size: var(--font-size-md);
  font-weight: var(--font-weight-regular);
  line-height: var(--line-height-base);
  color: var(--color-text-primary);
  background-color: var(--color-bg);
  /* Use clip instead of hidden so position: sticky elements stay functional */
  overflow-x: clip;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

h1,
h2,
h3,
h4 {
  color: var(--color-text-primary);
  font-weight: var(--font-weight-bold);
  line-height: var(--line-height-tight);
  letter-spacing: -0.01em;
}

h1 {
  font-size: var(--font-size-2xl);
}

h2 {
  font-size: var(--font-size-xl);
}

h3 {
  font-size: var(--font-size-lg);
}

h4 {
  font-size: var(--font-size-md);
}

p {
  color: var(--color-text-secondary);
}

a {
  color: var(--color-text-link);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

ul,
ol {
  list-style: none;
}

img,
svg,
canvas,
video {
  display: block;
  max-width: 100%;
}

button,
input,
textarea,
select {
  font-family: inherit;
  color: inherit;
}

button {
  background: none;
  border: none;
  cursor: pointer;
  touch-action: manipulation;
}

/* Keep form inputs at least 16px to prevent mobile browser auto-zoom on focus */
input,
textarea,
select {
  font-size: var(--font-size-md);
}

button:disabled,
input:disabled,
textarea:disabled,
select:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

/* Accessible Focus Ring for Keyboard & Switch Navigation */
:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

/* ==========================================================================
   4. CORE LAYOUT CONTAINERS & UTILITIES
   ========================================================================== */
.app-container {
  width: 100%;
  max-width: var(--container-max-width);
  margin-inline: auto;
  padding-inline: var(--space-4);
}

/* Main wrapper for standard pages that include the fixed bottom navbar */
.page-shell {
  min-height: 100vh;
  min-height: 100dvh;
  padding-top: var(--safe-area-top);
  padding-bottom: calc(var(--space-6) + var(--safe-area-bottom));
}

.page-shell--with-nav {
  padding-bottom: calc(var(--navbar-height) + var(--space-6) + var(--safe-area-bottom));
}

/* Visually hidden helper for screen readers (accessibility) */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

/* Utility class to toggle visibility from JS without inline styles */
.is-hidden {
  display: none !important;
}

/* Prevent text overflow inside narrow 360px cards */
.text-truncate {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ==========================================================================
   5. RESPONSIVE BREAKPOINT (TABLET & DESKTOP)
   ========================================================================== */
@media (min-width: 768px) {
  :root {
    --container-max-width: 720px;
  }

  .app-container {
    padding-inline: var(--space-6);
  }
}

/* ==========================================================================
   6. REDUCED MOTION ACCESSIBILITY
   ========================================================================== */
@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }

  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
