/**
 * Base CSS - Global styles, CSS reset, and design system variables
 * Mobile-first approach with progressive enhancement
 */

/* ==================== CSS CUSTOM PROPERTIES ==================== */
:root {
  /* Color Palette */
  --color-bg: #ffffff;
  --color-bg-secondary: #f8f9fa;
  --color-card: #ffffff;
  --color-border: #d0d0d0;
  --color-border-light: #e9ecef;
  --color-text: #1a1a1a;
  --color-text-secondary: #5f5f5f;
  --color-muted: #6c757d;
  
  /* Brand Colors */
  --color-primary: #0078d4;
  --color-primary-hover: #0063b1;
  --color-primary-light: #e3f2fd;
  
  /* Status Colors */
  --color-success: #2e7d32;
  --color-success-bg: #e8f5e9;
  --color-success-border: #a5d6a7;
  
  --color-warning: #e65100;
  --color-warning-bg: #fff3e0;
  --color-warning-border: #ffcc80;
  
  --color-danger: #c62828;
  --color-danger-bg: #ffebee;
  --color-danger-border: #ef9a9a;
  
  --color-info: #1565c0;
  --color-info-bg: #e3f2fd;
  --color-info-border: #90caf9;

  /* Typography */
  --font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, Roboto, 'Helvetica Neue', Arial, sans-serif;
  --font-family-mono: ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace;
  
  --font-size-xs: 11px;
  --font-size-sm: 13px;
  --font-size-base: 15px;
  --font-size-lg: 17px;
  --font-size-xl: 20px;
  --font-size-2xl: 24px;
  --font-size-3xl: 30px;

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

  /* Layout */
  --header-height: 56px;
  --nav-height: 64px;
  --content-padding: 16px;
  --max-width: 480px;
  --sidebar-width: 240px;

  /* Touch Targets - minimum 44x44px for accessibility */
  --touch-target-min: 44px;
  --input-height: 48px;
  --button-height: 48px;

  /* Border Radius */
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --radius-full: 9999px;

  /* Shadows */
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.07);
  --shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.1);

  /* Transitions */
  --transition-fast: 150ms ease;
  --transition-normal: 250ms ease;
  --transition-slow: 350ms ease;

  /* Z-index layers */
  --z-base: 0;
  --z-dropdown: 100;
  --z-sticky: 200;
  --z-fixed: 300;
  --z-modal-backdrop: 400;
  --z-modal: 500;
  --z-popover: 600;
  --z-toast: 700;
}

/* ==================== TABLET BREAKPOINT (768px+) ==================== */
@media (min-width: 768px) {
  :root {
    --content-padding: 24px;
    --max-width: 720px;
    --nav-height: 0px; /* Side nav instead of bottom */
    --font-size-base: 14px;
  }
}

/* ==================== DESKTOP BREAKPOINT (1024px+) ==================== */
@media (min-width: 1024px) {
  :root {
    --content-padding: 32px;
    --max-width: 960px;
  }
}

/* ==================== LARGE DESKTOP (1280px+) ==================== */
@media (min-width: 1280px) {
  :root {
    --max-width: 1200px;
  }
}

/* ==================== CSS RESET ==================== */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  /* Prevent font size inflation on mobile */
  -moz-text-size-adjust: none;
  -webkit-text-size-adjust: none;
  text-size-adjust: none;
  
  /* Smooth scrolling */
  scroll-behavior: smooth;
}

body {
  font-family: var(--font-family);
  font-size: var(--font-size-base);
  line-height: 1.5;
  color: var(--color-text);
  background-color: var(--color-bg-secondary);
  min-height: 100vh;
  min-height: 100dvh; /* Dynamic viewport height for mobile */
  
  /* Prevent pull-to-refresh on mobile when not needed */
  overscroll-behavior-y: contain;
  
  /* Better text rendering */
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* Remove default list styles */
ul, ol {
  list-style: none;
}

/* Remove link underlines by default */
a {
  color: inherit;
  text-decoration: none;
}

/* Improve media defaults */
img, picture, video, canvas, svg {
  display: block;
  max-width: 100%;
  height: auto;
}

/* Remove built-in form typography styles */
input, button, textarea, select {
  font: inherit;
  color: inherit;
}

/* Avoid text overflows */
p, h1, h2, h3, h4, h5, h6 {
  overflow-wrap: break-word;
}

/* Balance text wrapping on headings */
h1, h2, h3, h4, h5, h6 {
  text-wrap: balance;
}

/* Make sure textareas without a rows attribute are not tiny */
textarea:not([rows]) {
  min-height: 10em;
}

/* Anything that has been anchored to should have extra scroll margin */
:target {
  scroll-margin-block: 5ex;
}

/* Remove spinner buttons from number inputs */
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

input[type="number"] {
  -moz-appearance: textfield;
}

/* ==================== ACCESSIBILITY ==================== */

/* Focus styles */
:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

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

/* Screen reader only 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;
}

/* Skip link for keyboard navigation */
.skip-link {
  position: absolute;
  top: -40px;
  left: 0;
  background: var(--color-primary);
  color: white;
  padding: var(--space-2) var(--space-4);
  z-index: var(--z-toast);
  transition: top var(--transition-fast);
}

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

/* ==================== LAYOUT UTILITIES ==================== */

.app-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
}

.page-content {
  flex: 1;
  padding: var(--content-padding);
  padding-bottom: calc(var(--nav-height) + var(--content-padding));
  max-width: var(--max-width);
  margin: 0 auto;
  width: 100%;
}

/* Tablet+ layout with sidebar */
@media (min-width: 768px) {
  .app-container {
    flex-direction: row;
  }

  .page-content {
    padding-bottom: var(--content-padding);
    margin-left: var(--sidebar-width);
  }
}

/* ==================== TYPOGRAPHY UTILITIES ==================== */

.text-xs { font-size: var(--font-size-xs); }
.text-sm { font-size: var(--font-size-sm); }
.text-base { font-size: var(--font-size-base); }
.text-lg { font-size: var(--font-size-lg); }
.text-xl { font-size: var(--font-size-xl); }
.text-2xl { font-size: var(--font-size-2xl); }
.text-3xl { font-size: var(--font-size-3xl); }

.font-mono { font-family: var(--font-family-mono); }
.font-medium { font-weight: 500; }
.font-semibold { font-weight: 600; }
.font-bold { font-weight: 700; }

.text-muted { color: var(--color-muted); }
.text-secondary { color: var(--color-text-secondary); }
.text-primary { color: var(--color-primary); }
.text-success { color: var(--color-success); }
.text-warning { color: var(--color-warning); }
.text-danger { color: var(--color-danger); }

.text-center { text-align: center; }
.text-right { text-align: right; }

/* ==================== SPACING UTILITIES ==================== */

.m-0 { margin: 0; }
.m-1 { margin: var(--space-1); }
.m-2 { margin: var(--space-2); }
.m-3 { margin: var(--space-3); }
.m-4 { margin: var(--space-4); }

.mt-1 { margin-top: var(--space-1); }
.mt-2 { margin-top: var(--space-2); }
.mt-3 { margin-top: var(--space-3); }
.mt-4 { margin-top: var(--space-4); }
.mt-6 { margin-top: var(--space-6); }
.mt-8 { margin-top: var(--space-8); }

.mb-1 { margin-bottom: var(--space-1); }
.mb-2 { margin-bottom: var(--space-2); }
.mb-3 { margin-bottom: var(--space-3); }
.mb-4 { margin-bottom: var(--space-4); }
.mb-6 { margin-bottom: var(--space-6); }

.p-0 { padding: 0; }
.p-2 { padding: var(--space-2); }
.p-3 { padding: var(--space-3); }
.p-4 { padding: var(--space-4); }
.p-6 { padding: var(--space-6); }

.py-2 { padding-top: var(--space-2); padding-bottom: var(--space-2); }
.py-3 { padding-top: var(--space-3); padding-bottom: var(--space-3); }
.py-4 { padding-top: var(--space-4); padding-bottom: var(--space-4); }

.px-3 { padding-left: var(--space-3); padding-right: var(--space-3); }
.px-4 { padding-left: var(--space-4); padding-right: var(--space-4); }

.gap-1 { gap: var(--space-1); }
.gap-2 { gap: var(--space-2); }
.gap-3 { gap: var(--space-3); }
.gap-4 { gap: var(--space-4); }

/* ==================== FLEXBOX UTILITIES ==================== */

.flex { display: flex; }
.flex-col { flex-direction: column; }
.flex-wrap { flex-wrap: wrap; }
.flex-1 { flex: 1; }
.items-center { align-items: center; }
.items-start { align-items: flex-start; }
.items-end { align-items: flex-end; }
.justify-center { justify-content: center; }
.justify-between { justify-content: space-between; }
.justify-end { justify-content: flex-end; }

/* ==================== GRID UTILITIES ==================== */

.grid { display: grid; }
.grid-cols-1 { grid-template-columns: 1fr; }
.grid-cols-2 { grid-template-columns: repeat(2, 1fr); }

@media (min-width: 768px) {
  .md\:grid-cols-2 { grid-template-columns: repeat(2, 1fr); }
  .md\:grid-cols-3 { grid-template-columns: repeat(3, 1fr); }
}

/* ==================== VISIBILITY UTILITIES ==================== */

.hidden { display: none !important; }

.mobile-only {
  display: block;
}

.desktop-only {
  display: none;
}

@media (min-width: 768px) {
  .mobile-only {
    display: none;
  }
  
  .desktop-only {
    display: block;
  }
}

/* ==================== ANIMATION KEYFRAMES ==================== */

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

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

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

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

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

.animate-spin {
  animation: spin 1s linear infinite;
}

.animate-pulse {
  animation: pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite;
}

.animate-fadeIn {
  animation: fadeIn var(--transition-normal) ease-out;
}

.animate-slideUp {
  animation: slideUp var(--transition-normal) ease-out;
}

/* ==================== LOADING STATES ==================== */

.skeleton {
  background: linear-gradient(
    90deg,
    var(--color-border-light) 25%,
    var(--color-bg) 50%,
    var(--color-border-light) 75%
  );
  background-size: 200% 100%;
  animation: skeleton-loading 1.5s ease-in-out infinite;
  border-radius: var(--radius-sm);
}

@keyframes skeleton-loading {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}

.skeleton-text {
  height: 1em;
  margin-bottom: 0.5em;
}

.skeleton-text:last-child {
  width: 80%;
}

/* ==================== MISC UTILITIES ==================== */

.truncate {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.overflow-auto {
  overflow: auto;
}

.overflow-hidden {
  overflow: hidden;
}

.rounded { border-radius: var(--radius-md); }
.rounded-lg { border-radius: var(--radius-lg); }
.rounded-full { border-radius: var(--radius-full); }

.shadow-sm { box-shadow: var(--shadow-sm); }
.shadow-md { box-shadow: var(--shadow-md); }
.shadow-lg { box-shadow: var(--shadow-lg); }

.border { border: 1px solid var(--color-border); }
.border-t { border-top: 1px solid var(--color-border); }
.border-b { border-bottom: 1px solid var(--color-border); }

.bg-white { background-color: white; }
.bg-secondary { background-color: var(--color-bg-secondary); }

.w-full { width: 100%; }
.h-full { height: 100%; }

.cursor-pointer { cursor: pointer; }
.pointer-events-none { pointer-events: none; }

.select-none { user-select: none; }

/* ==================== TOUCH OPTIMIZATIONS ==================== */

/* Disable touch callouts and text selection on interactive elements */
.no-touch-callout {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

/* Better tap highlight on mobile */
button,
a,
[role="button"] {
  -webkit-tap-highlight-color: rgba(0, 120, 212, 0.1);
}

/* Ensure touch targets meet minimum size */
.touch-target {
  min-height: var(--touch-target-min);
  min-width: var(--touch-target-min);
}
