/* ============================================
   BASE.CSS - Foundation & Core Styles
   KlassKit Hub
   ============================================ */

/* ============================================
   1. CSS CUSTOM PROPERTIES (Design Tokens)
   ============================================ */

:root {
  /* --- Color Palette (Classic / Legacy Default) --- */
  --color-pink-rgb: 255 71 133;
  --color-orange-rgb: 255 126 51;
  --color-green-rgb: 0 208 99;
  --color-blue-rgb: 30 167 253;

  --color-pink: rgb(var(--color-pink-rgb));
  --color-orange: rgb(var(--color-orange-rgb));
  --color-green: rgb(var(--color-green-rgb));
  --color-blue: rgb(var(--color-blue-rgb));
  --color-dark: #111827;
  --color-chalk: rgb(var(--bg-primary-rgb));

  /* --- Semantic Colors (Light Mode) --- */
  --bg-primary-rgb: 248 250 252;
  --bg-secondary-rgb: 255 255 255;
  --bg-tertiary-rgb: 226 232 240;
  --surface-card-rgb: 255 255 255;
  --oled-rgb: 2 6 23;

  --text-primary-rgb: 30 41 59;
  --text-secondary-rgb: 71 85 105;
  --text-tertiary-rgb: 148 163 184;
  --ink-rgb: 30 41 59;

  --border-primary-rgb: 51 65 85;
  --border-secondary-rgb: 203 213 225;

  --bg-primary: rgb(var(--bg-primary-rgb));
  --bg-secondary: rgb(var(--bg-secondary-rgb) / 0.85);
  /* For glassmorphism */
  --bg-tertiary: rgb(var(--bg-tertiary-rgb));

  --text-primary: rgb(var(--text-primary-rgb));
  --text-secondary: rgb(var(--text-secondary-rgb));
  --text-tertiary: rgb(var(--text-tertiary-rgb));

  --border-primary: rgb(var(--border-primary-rgb));
  --border-secondary: rgb(var(--border-secondary-rgb));

  --surface-card: rgb(var(--surface-card-rgb));
  --surface-overlay: rgb(var(--bg-secondary-rgb) / 0.95);

  /* --- Grid Pattern --- */
  --grid-color: #e2e8f0;
  --grid-size: 32px;
  /* Slightly larger, more breathable */

  /* --- Shadows (Soft Brutalism) --- */
  --shadow-color: var(--border-primary);
  /* Hard block shadows for Soft Brutalism */
  --shadow-sm: 2px 2px 0px var(--shadow-color);
  --shadow-md: 4px 4px 0px var(--shadow-color);
  --shadow-lg: 8px 8px 0px var(--shadow-color);
  --shadow-xl: 12px 12px 0px var(--shadow-color);

  --shadow-hard-sm: 2px 3px 0px var(--shadow-color);
  --shadow-hard-md: 4px 5px 0px var(--shadow-color);

  /* --- Borders --- */
  --border-width-thin: 1.5px;
  --border-width-medium: 2px;
  --border-width-thick: 3px;
  /* Slightly thinner for a cleaner look */

  /* --- Border Radius --- */
  --radius-sm: 0.5rem;
  --radius-md: 0.75rem;
  --radius-lg: 1rem;
  --radius-xl: 1.5rem;
  --radius-2xl: 2rem;

  /* --- Spacing --- */
  --spacing-xs: 0.5rem;
  --spacing-sm: 1rem;
  --spacing-md: 1.5rem;
  --spacing-lg: 2rem;
  --spacing-xl: 3rem;

  /* --- Transitions --- */
  --transition-fast: 0.15s cubic-bezier(0.2, 0.8, 0.2, 1);
  --transition-base: 0.3s cubic-bezier(0.2, 0.8, 0.2, 1);
  --transition-slow: 0.5s cubic-bezier(0.2, 0.8, 0.2, 1);

  /* --- Z-Index Scale --- */
  --z-background: -1;
  --z-base: 0;
  --z-elevated: 10;
  --z-overlay: 40;
  --z-modal: 50;
  --z-controls: 60;
  --z-tooltip: 70;
}

/* --- Dark Mode Overrides (OLED Modern) --- */
.dark {
  --bg-primary-rgb: 2 6 23;
  --bg-secondary-rgb: 15 23 42;
  --bg-tertiary-rgb: 30 41 59;
  --surface-card-rgb: 15 23 42;

  --text-primary-rgb: 248 250 252;
  --text-secondary-rgb: 148 163 184;
  --text-tertiary-rgb: 100 116 139;

  --border-primary-rgb: 51 65 85;
  --border-secondary-rgb: 30 41 59;

  --bg-secondary: rgb(var(--bg-secondary-rgb) / 0.75);
  --surface-overlay: rgb(var(--bg-primary-rgb) / 0.95);

  --grid-color: rgba(255, 255, 255, 0.06);
  /* Make grid more visible in dark mode for better aesthetic */

  --shadow-color: #000000;
  /* Pitch black hard shadows for dark mode */
  --shadow-hard-sm: 2px 3px 0px var(--shadow-color);
  --shadow-hard-md: 4px 5px 0px var(--shadow-color);
}

/* --- Color Themes (Accents + Surfaces, Beta) --- */
[data-theme="pastel"] {
  --color-pink-rgb: 251 113 133;   /* Coral Rose #fb7185 */
  --color-orange-rgb: 251 191 36;  /* Sticky-Note Amber #fbbf24 */
  --color-green-rgb: 52 211 153;   /* Mint Marker #34d399 */
  --color-blue-rgb: 96 165 250;    /* Sky Ink #60a5fa */

  --bg-primary-rgb: 253 251 247;   /* Soft Notebook Paper #fdfbf7 */
  --bg-tertiary-rgb: 243 238 230;
  --border-secondary-rgb: 229 221 210;
  --grid-color: #ece4d8;
}

.dark[data-theme="pastel"] {
  --bg-primary-rgb: 11 17 32;      /* Soft Twilight Slate #0b1120 */
  --bg-secondary-rgb: 21 30 50;
  --surface-card-rgb: 21 30 50;
  --bg-tertiary-rgb: 34 45 71;
  --oled-rgb: 11 17 32;
  --grid-color: rgba(255, 255, 255, 0.06);
}

[data-theme="botanical"] {
  --color-pink-rgb: 224 122 95;    /* Terracotta #e07a5f */
  --color-orange-rgb: 224 159 62;  /* Warm Ochre #e09f3e */
  --color-green-rgb: 42 157 143;   /* Sage Teal #2a9d8f */
  --color-blue-rgb: 94 96 206;     /* Slate Indigo #5e60ce */

  --bg-primary-rgb: 247 244 239;   /* Warm Studio Cream #f7f4ef */
  --bg-secondary-rgb: 255 253 249;
  --surface-card-rgb: 255 253 249;
  --bg-tertiary-rgb: 232 226 216;

  --text-primary-rgb: 41 47 43;    /* Deep Moss Charcoal */
  --text-secondary-rgb: 87 95 89;
  --ink-rgb: 41 47 43;
  --border-primary-rgb: 55 65 58;
  --border-secondary-rgb: 214 206 194;
  --grid-color: #e4ddd0;
}

.dark[data-theme="botanical"] {
  --bg-primary-rgb: 12 18 16;      /* Deep Forest Slate #0c1210 */
  --bg-secondary-rgb: 20 30 26;
  --surface-card-rgb: 20 30 26;
  --bg-tertiary-rgb: 31 46 40;
  --oled-rgb: 12 18 16;
  --border-primary-rgb: 54 74 65;
  --border-secondary-rgb: 31 46 40;
  --grid-color: rgba(214, 229, 220, 0.07);
}

[data-theme="oceanic"] {
  --color-pink-rgb: 99 102 241;    /* Indigo #6366f1 */
  --color-orange-rgb: 245 158 11;  /* Lighthouse Amber #f59e0b */
  --color-green-rgb: 20 184 166;   /* Seafoam Teal #14b8a6 */
  --color-blue-rgb: 14 165 233;    /* Ocean Blue #0ea5e9 */

  --bg-primary-rgb: 242 248 252;   /* Cool Sea Mist #f2f8fc */
  --bg-tertiary-rgb: 219 234 245;
  --border-secondary-rgb: 194 218 235;
  --grid-color: #dbeaf5;
}

.dark[data-theme="oceanic"] {
  --bg-primary-rgb: 4 13 26;       /* Deep Abyssal Navy #040d1a */
  --bg-secondary-rgb: 11 25 46;
  --surface-card-rgb: 11 25 46;
  --bg-tertiary-rgb: 22 43 74;
  --oled-rgb: 4 13 26;
  --border-primary-rgb: 43 73 112;
  --border-secondary-rgb: 22 43 74;
  --grid-color: rgba(186, 230, 253, 0.07);
}

[data-theme="monochrome"] {
  --color-pink-rgb: 255 71 133;    /* Signature Pop #ff4785 */
  --color-orange-rgb: 71 85 105;   /* Slate 600 #475569 */
  --color-green-rgb: 51 65 85;     /* Slate 700 #334155 */
  --color-blue-rgb: 30 41 59;      /* Ink Slate #1e293b */

  --bg-primary-rgb: 250 250 250;   /* Crisp Paper #fafafa */
  --bg-tertiary-rgb: 228 228 231;
  --text-primary-rgb: 24 24 27;
  --text-secondary-rgb: 82 82 91;
  --ink-rgb: 24 24 27;
  --border-primary-rgb: 24 24 27;
  --border-secondary-rgb: 212 212 216;
  --grid-color: #e4e4e7;
}

.dark[data-theme="monochrome"] {
  --color-pink-rgb: 255 71 133;    /* Signature Pop #ff4785 */
  --color-orange-rgb: 148 163 184; /* Slate 400 #94a3b8 */
  --color-green-rgb: 100 116 139;  /* Slate 500 #64748b */
  --color-blue-rgb: 71 85 105;     /* Slate 600 #475569 */

  --bg-primary-rgb: 9 9 11;        /* Pure Zinc Ink #09090b */
  --bg-secondary-rgb: 24 24 27;
  --surface-card-rgb: 24 24 27;
  --bg-tertiary-rgb: 39 39 42;
  --oled-rgb: 9 9 11;
  --text-primary-rgb: 250 250 250;
  --text-secondary-rgb: 161 161 170;
  --border-primary-rgb: 63 63 70;
  --border-secondary-rgb: 39 39 42;
  --grid-color: rgba(255, 255, 255, 0.06);
}

/* ============================================
   2. BASE STYLES & RESETS
   ============================================ */

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  font-family: 'Nunito', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background-color: var(--bg-primary);
  background-image:
    linear-gradient(var(--grid-color) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid-color) 1px, transparent 1px);
  background-size: var(--grid-size) var(--grid-size);
  color: var(--text-primary);

  /* Performance optimizations */
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;

  /* User experience */
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;

  /* Smooth transitions */
  transition:
    background-color var(--transition-slow),
    color var(--transition-slow);
}

/* --- Content Visibility for below-fold sections --- */
.category-section,
#recent-section,
#pinned-section,
#featured-section {
  content-visibility: auto;
  contain-intrinsic-size: auto 400px;
}

/* --- Reduce animations for users who prefer reduced motion --- */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

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

/* ============================================
   3. UTILITY CLASSES
   ============================================ */

/* Gradient mask for horizontal scrolling */
.mask-gradient {
  -webkit-mask-image: linear-gradient(to right,
      transparent,
      black 2%,
      black 98%,
      transparent);
  mask-image: linear-gradient(to right,
      transparent,
      black 2%,
      black 98%,
      transparent);
}

/* Focus visible styles (for keyboard navigation) */
.focus-ring:focus-visible {
  outline: 3px solid var(--color-blue);
  outline-offset: 2px;
}

/* Glass panel utility */
:where(.glass-panel) {
  background: var(--bg-secondary);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: var(--border-width-medium) solid rgba(255, 255, 255, 0.4);
  box-shadow: var(--shadow-md);
}

:where(.dark .glass-panel) {
  background: var(--bg-secondary);
  border-color: rgba(255, 255, 255, 0.08);
}

/* Skip to content link (accessibility) */
.skip-link {
  position: absolute;
  top: -40px;
  left: 0;
  padding: 8px;
  background: var(--color-blue);
  color: white;
  z-index: 100;
  text-decoration: none;
  font-weight: bold;
}

.skip-link:focus {
  top: 0;
}

/* ============================================
   4. ANIMATIONS & KEYFRAMES
   ============================================ */

@keyframes slide-down {
  0% {
    transform: translateY(-100%);
    opacity: 0;
  }

  100% {
    transform: translateY(0);
    opacity: 1;
  }
}

@keyframes slide-in-left {
  from {
    opacity: 0;
    transform: translateX(-20px) scale(0.8);
  }

  to {
    opacity: 1;
    transform: translateX(0) scale(1);
  }
}

@keyframes pop-in {
  0% {
    opacity: 0;
    transform: scale(0.95);
  }

  100% {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@keyframes fade-in {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

@keyframes pulse {

  0%,
  100% {
    opacity: 1;
  }

  50% {
    opacity: 0.5;
  }
}

/* ============================================
   5. SCROLLBAR CUSTOMIZATION (Classroom Look)
   ============================================ */

/* Chrome, Edge, and Safari */
::-webkit-scrollbar {
  width: 14px;
  height: 14px;
}

/* Track: Notebook Paper Style */
::-webkit-scrollbar-track {
  background: var(--bg-primary);
  background-image: 
    linear-gradient(90deg, rgba(255, 71, 133, 0.1) 1px, transparent 1px), /* Red margin line */
    linear-gradient(var(--grid-color) 1px, transparent 1px); /* Blue horizontal lines */
  background-size: 24px 100%, 100% 20px;
  background-position: 4px 0, 0 0;
}

/* Thumb: "Chalk Stick" Style */
::-webkit-scrollbar-thumb {
  background: #ffffff; /* Chalk White */
  border: 3px solid var(--bg-primary);
  border-radius: 6px; /* Slightly squared like a chalk block */
  box-shadow: 
    inset 2px 2px 0 rgba(255, 255, 255, 0.8),
    inset -2px -2px 0 rgba(0, 0, 0, 0.05),
    var(--shadow-sm); /* Soft Brutalist shadow */
  transition: all var(--transition-fast);
}

.dark ::-webkit-scrollbar-thumb {
  background: #e2e8f0;
  border-color: var(--bg-primary);
  box-shadow: 
    inset 2px 2px 0 rgba(255, 255, 255, 0.2),
    inset -2px -2px 0 rgba(0, 0, 0, 0.2);
}

/* Hover: "Highlighter" Glow */
::-webkit-scrollbar-thumb:hover {
  background: #fde047; /* Highlighter Yellow */
  border-color: var(--bg-primary);
  transform: scale(1.1);
}

.dark ::-webkit-scrollbar-thumb:hover {
  background: var(--color-pink); /* Neon Pink Marker */
}

::-webkit-scrollbar-corner {
  background: var(--bg-primary);
}

/* Firefox */
* {
  scrollbar-width: auto;
  scrollbar-color: #ffffff var(--bg-primary);
}

.dark * {
  scrollbar-color: #e2e8f0 var(--bg-primary);
}

/* Thin Scrollbar for Classroom Sidebars */
.custom-scrollbar,
#recent-list,
#panel-ann-list,
.overflow-y-auto {
  scrollbar-width: thin;
}

.custom-scrollbar::-webkit-scrollbar,
#recent-list::-webkit-scrollbar,
#panel-ann-list::-webkit-scrollbar,
.overflow-y-auto::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}

.custom-scrollbar::-webkit-scrollbar-thumb,
#recent-list::-webkit-scrollbar-thumb,
#panel-ann-list::-webkit-scrollbar-thumb,
.overflow-y-auto::-webkit-scrollbar-thumb {
  border-width: 2px;
  border-radius: 4px;
}

/* ============================================
   6. ACCESSIBILITY & REDUCED MOTION
   ============================================ */

@media (prefers-reduced-motion: reduce) {

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

  .shape {
    animation: none;
  }
}

/* ============================================
   7. RESPONSIVE ADJUSTMENTS
   ============================================ */

@media (max-width: 767px) {
  :root {
    --grid-size: 16px;
    --border-width-medium: 2px;
    --border-width-thick: 3px;
    --radius-xl: 0.75rem;
    --radius-2xl: 1rem;
    --shadow-hard: 0 4px 0 var(--border-primary);
  }

  body {
    padding-bottom: 64px; /* Space for bottom nav */
    font-size: 14px;
  }

  .main-workspace {
    height: calc(100vh - 64px);
  }

  h1, .font-heading {
    letter-spacing: -0.02em !important;
  }
}

@media (max-width: 640px) {
  :root {
    --grid-size: 12px;
  }
}

/* ============================================
   8. MOBILE NAVIGATION (Bottom Nav)
   ============================================ */

.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 64px;
  background: var(--bg-secondary);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-top: var(--border-width-thick) solid var(--border-primary);
  display: none; /* Hidden by default (desktop) */
  align-items: center;
  justify-content: space-around;
  padding: 0 1rem;
  z-index: var(--z-controls);
  box-shadow: 0 -4px 15px rgba(0, 0, 0, 0.05);
}

@media (max-width: 767px) {
  .bottom-nav {
    display: flex; /* Show only on mobile */
  }
}

.dark .bottom-nav {
  background: rgba(15, 23, 42, 0.85);
  border-top-color: var(--border-primary);
}

/* Hide bottom nav and remove padding when game modal is open */
body.game-modal-open {
  padding-bottom: 0 !important;
}

body.game-modal-open .bottom-nav {
  display: none !important;
}

.bottom-nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.25rem;
  color: var(--text-tertiary);
  font-family: 'Fredoka', sans-serif;
  font-weight: 700;
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  transition: all var(--transition-fast);
  flex: 1;
}

.bottom-nav-item i,
.bottom-nav-item svg {
  width: 1.5rem;
  height: 1.5rem;
  transition: transform var(--transition-fast);
}

.bottom-nav-item.active {
  color: var(--color-blue);
}

.bottom-nav-item.active i,
.bottom-nav-item.active svg {
  transform: translateY(-4px) scale(1.1);
  color: var(--color-blue);
}

.bottom-nav-item:active i,
.bottom-nav-item:active svg {
  transform: scale(0.9);
}

/* Sidebar Backdrop Blur */
#sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 65;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--transition-base);
}

#sidebar-backdrop.active {
  opacity: 1;
  pointer-events: auto;
}

/* Shared Slide-Over Drawer Backdrop (Notifications / History) */
#drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.4);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 72;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--transition-base);
}

#drawer-backdrop.active {
  opacity: 1;
  pointer-events: auto;
}

/* ============================================
   9. BROWSER-SPECIFIC FIXES
   ============================================ */

/* Safari iOS bounce fix */
@supports (-webkit-overflow-scrolling: touch) {
  body {
    -webkit-overflow-scrolling: touch;
  }
}

/* Firefox focus outline fix */
@-moz-document url-prefix() {
  .btn-chunky:focus-visible {
    outline-offset: 3px;
  }
}

/* Edge/IE grid background fallback */
@supports not (background-image: linear-gradient(var(--grid-color) 1px, transparent 1px)) {
  body {
    background-image:
      linear-gradient(#E2E8F0 1px, transparent 1px),
      linear-gradient(90deg, #E2E8F0 1px, transparent 1px);
  }
}

/* ============================================
   10. PRO & NEW BADGE UI
   ============================================ */

:where(.pro-badge,
.new-badge) {
  display: inline-block;
  letter-spacing: 0.05em;
  line-height: 1;
  text-transform: uppercase;
  font-family: 'Fredoka', sans-serif;
  vertical-align: middle;
  width: fit-content;
}

/* ============================================
   9. PRINT STYLES
   ============================================ */

@media print {
  body {
    background: white;
    background-image: none;
    color: black;
  }

  .living-bg,
  .floating-stick,
  #sound-btn,
  [data-action="toggleTheme"] {
    display: none;
  }

  .hub-card,
  .btn-chunky {
    box-shadow: none;
    border: 1px solid black;
  }
}