/**
 * SA Objektmanagement - Apple HIG Design System
 * Version: 4.0.0 - Apple Human Interface Guidelines inspired
 * 
 * Design Principles:
 * - Clarity: Content is king, minimize chrome
 * - Deference: Fluid motion, crisp interface
 * - Depth: Layered UI with subtle shadows
 */

/* ===================== CSS VARIABLES ===================== */
:root {
  /* Typography - Apple System Stack */
  --font-primary: 'Century Gothic', -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'Segoe UI', sans-serif;
  --font-secondary: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'SF Mono', SFMono-Regular, Menlo, Monaco, monospace;
  
  /* Font Sizes - Apple Scale */
  --text-xs: 11px;
  --text-sm: 13px;
  --text-base: 15px;
  --text-lg: 17px;
  --text-xl: 20px;
  --text-2xl: 24px;
  --text-3xl: 28px;
  --text-4xl: 34px;
  
  /* Font Weights */
  --weight-regular: 400;
  --weight-medium: 500;
  --weight-semibold: 600;
  --weight-bold: 700;
  
  /* Line Heights */
  --leading-tight: 1.2;
  --leading-normal: 1.47059;
  --leading-relaxed: 1.6;
  
  /* Colors - Apple Neutral Palette */
  --color-white: #FFFFFF;
  --color-gray-50: #F5F5F7;
  --color-gray-100: #E8E8ED;
  --color-gray-200: #D1D1D6;
  --color-gray-300: #AEAEB2;
  --color-gray-400: #8E8E93;
  --color-gray-500: #636366;
  --color-gray-600: #48484A;
  --color-gray-700: #363639;
  --color-gray-800: #2C2C2E;
  --color-gray-900: #1C1C1E;
  --color-black: #000000;
  
  /* Primary Accent - Orange */
  --color-accent: #FF9500;
  --color-accent-hover: #E68600;
  --color-accent-active: #CC7700;
  --color-accent-light: rgba(255, 149, 0, 0.12);
  --color-accent-subtle: rgba(255, 149, 0, 0.08);
  
  /* Semantic Colors */
  --color-success: #34C759;
  --color-success-light: rgba(52, 199, 89, 0.12);
  --color-warning: #FF9500;
  --color-warning-light: rgba(255, 149, 0, 0.12);
  --color-error: #FF3B30;
  --color-error-light: rgba(255, 59, 48, 0.12);
  --color-info: #007AFF;
  --color-info-light: rgba(0, 122, 255, 0.12);
  
  /* Backgrounds - Light Mode */
  --bg-primary: var(--color-gray-50);
  --bg-secondary: var(--color-white);
  --bg-tertiary: var(--color-gray-100);
  --bg-elevated: var(--color-white);
  --bg-grouped: var(--color-gray-50);
  
  /* Text Colors - Light Mode */
  --text-primary: var(--color-black);
  --text-secondary: var(--color-gray-500);
  --text-tertiary: var(--color-gray-400);
  --text-quaternary: var(--color-gray-300);
  --text-inverse: var(--color-white);
  
  /* Separators & Borders */
  --separator: rgba(60, 60, 67, 0.12);
  --separator-opaque: var(--color-gray-200);
  --border-color: var(--color-gray-200);
  
  /* Fill Colors */
  --fill-primary: rgba(120, 120, 128, 0.2);
  --fill-secondary: rgba(120, 120, 128, 0.16);
  --fill-tertiary: rgba(118, 118, 128, 0.12);
  --fill-quaternary: rgba(116, 116, 128, 0.08);
  
  /* Shadows - Apple Style (subtle) */
  --shadow-xs: 0 1px 2px rgba(0, 0, 0, 0.04);
  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.08);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.12);
  --shadow-xl: 0 16px 48px rgba(0, 0, 0, 0.16);
  --shadow-float: 0 22px 70px 4px rgba(0, 0, 0, 0.14);
  
  /* Spacing - 8pt Grid */
  --space-0: 0;
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
  --space-7: 28px;
  --space-8: 32px;
  --space-10: 40px;
  --space-12: 48px;
  --space-16: 64px;
  
  /* Border Radius - Apple Style */
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --radius-xl: 20px;
  --radius-2xl: 28px;
  --radius-full: 9999px;
  
  /* Transitions - Apple Standard */
  --duration-instant: 100ms;
  --duration-fast: 200ms;
  --duration-normal: 300ms;
  --duration-slow: 500ms;
  --ease-default: cubic-bezier(0.25, 0.1, 0.25, 1);
  --ease-in-out: cubic-bezier(0.42, 0, 0.58, 1);
  --ease-spring: cubic-bezier(0.175, 0.885, 0.32, 1.275);
  
  /* Layout */
  --sidebar-width: 260px;
  --topbar-height: 52px;
  --content-max-width: 1120px;
  --touch-target: 44px;
  
  /* ===== Legacy Compatibility (for existing templates) ===== */
  --color-primary: var(--color-accent);
  --color-primary-hover: var(--color-accent-hover);
  --color-primary-light: var(--color-accent-light);
  --color-primary-dark: var(--color-accent-active);
  --color-secondary: var(--color-success);
  --color-secondary-hover: #2DA94B;
  --color-secondary-light: var(--color-success-light);
  --color-neutral: var(--color-gray-500);
  --color-neutral-light: var(--color-gray-400);
  
  --bg-card: var(--bg-secondary);
  --bg-hover: var(--fill-tertiary);
  --bg-sidebar: var(--bg-secondary);
  
  --border-light: var(--separator);
  --divider: var(--separator);
  --shadow-primary: 0 4px 12px rgba(255, 149, 0, 0.25);
  
  --spacing-xs: var(--space-1);
  --spacing-sm: var(--space-2);
  --spacing-md: var(--space-4);
  --spacing-lg: var(--space-6);
  --spacing-xl: var(--space-8);
  --spacing-2xl: var(--space-12);
  
  --transition-fast: var(--duration-fast) var(--ease-default);
  --transition-normal: var(--duration-normal) var(--ease-default);
  
  /* Legacy variables from main.css */
  --bg: var(--bg-primary);
  --card: var(--bg-secondary);
  --muted: var(--text-tertiary);
  --text: var(--text-primary);
  --stroke: var(--separator);
  --brand: var(--color-accent);
  --brand-600: var(--color-accent-hover);
  --ring: var(--color-accent-light);
  --ok: var(--color-success);
  --error: var(--color-error);
  --shadow: var(--shadow-md);
  --radius: var(--radius-lg);
  --btn-sm-bg: var(--fill-tertiary);
  --btn-sm-bg-h: var(--fill-secondary);
  --topbar-bg: var(--bg-secondary);
  --primary: var(--color-accent);
  --border: var(--border-color);
}

/* ===================== DARK MODE ===================== */
[data-theme="dark"] {
  /* Backgrounds - Dark Mode */
  --bg-primary: var(--color-black);
  --bg-secondary: var(--color-gray-900);
  --bg-tertiary: var(--color-gray-800);
  --bg-elevated: var(--color-gray-800);
  --bg-grouped: var(--color-black);
  
  /* Text Colors - Dark Mode */
  --text-primary: var(--color-white);
  --text-secondary: var(--color-gray-400);
  --text-tertiary: var(--color-gray-500);
  --text-quaternary: var(--color-gray-600);
  --text-inverse: var(--color-black);
  
  /* Separators - Dark Mode */
  --separator: rgba(84, 84, 88, 0.65);
  --separator-opaque: var(--color-gray-700);
  --border-color: var(--color-gray-700);
  
  /* Fill Colors - Dark Mode */
  --fill-primary: rgba(120, 120, 128, 0.36);
  --fill-secondary: rgba(120, 120, 128, 0.32);
  --fill-tertiary: rgba(118, 118, 128, 0.24);
  --fill-quaternary: rgba(116, 116, 128, 0.18);
  
  /* Shadows - Dark Mode */
  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.32);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.48);
  
  /* Accent adjustments for dark */
  --color-accent-light: rgba(255, 149, 0, 0.2);
  --color-accent-subtle: rgba(255, 149, 0, 0.12);
  
  /* Legacy Compatibility - Dark Mode */
  --bg-card: var(--bg-secondary);
  --bg-hover: var(--fill-tertiary);
  --bg-sidebar: var(--bg-secondary);
  --bg: var(--bg-primary);
  --card: var(--bg-secondary);
  --muted: var(--text-tertiary);
  --text: var(--text-primary);
  --stroke: var(--separator);
  --topbar-bg: var(--bg-secondary);
  --border: var(--border-color);
}

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

html {
  font-size: 15px;
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: var(--font-secondary);
  font-size: var(--text-base);
  line-height: var(--leading-normal);
  color: var(--text-primary);
  background-color: var(--bg-primary);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

/* ===================== TYPOGRAPHY ===================== */
h1, h2, h3, h4, h5, h6,
.heading {
  font-family: var(--font-primary);
  font-weight: var(--weight-semibold);
  letter-spacing: -0.022em;
}

h1, .h1 {
  font-size: var(--text-4xl);
  font-weight: var(--weight-bold);
  line-height: var(--leading-tight);
  letter-spacing: -0.028em;
  margin-bottom: var(--space-4);
}

h2, .h2 {
  font-size: var(--text-2xl);
  line-height: var(--leading-tight);
  margin-bottom: var(--space-3);
}

h3, .h3 {
  font-size: var(--text-xl);
  margin-bottom: var(--space-2);
}

h4, .h4 {
  font-size: var(--text-lg);
  margin-bottom: var(--space-2);
}

p {
  margin-bottom: var(--space-4);
}

.text-sm { font-size: var(--text-sm); }
.text-xs { font-size: var(--text-xs); }
.text-lg { font-size: var(--text-lg); }

.text-primary { color: var(--text-primary); }
.text-secondary { color: var(--text-secondary); }
.text-tertiary { color: var(--text-tertiary); }
.text-accent { color: var(--color-accent); }
.text-success { color: var(--color-success); }
.text-error { color: var(--color-error); }
.text-muted { color: var(--text-tertiary); }

.font-medium { font-weight: var(--weight-medium); }
.font-semibold { font-weight: var(--weight-semibold); }
.font-bold { font-weight: var(--weight-bold); }

/* ===================== LAYOUT ===================== */
.app-container {
  display: flex;
  min-height: 100vh;
}

.main-wrapper {
  flex: 1;
  margin-left: var(--sidebar-width);
  min-height: 100vh;
  transition: margin-left var(--duration-normal) var(--ease-default);
}

.main-content {
  padding: var(--space-6);
  max-width: var(--content-max-width);
  margin: 0 auto;
}

.wrap {
  max-width: var(--content-max-width);
  margin: var(--space-6) auto;
  padding: 0 var(--space-4);
}

.container {
  max-width: 960px;
  margin: 0 auto;
  padding: var(--space-5) var(--space-4);
}

/* ===================== SIDEBAR ===================== */
.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  width: var(--sidebar-width);
  height: 100vh;
  background: var(--bg-secondary);
  border-right: 1px solid var(--separator);
  display: flex;
  flex-direction: column;
  z-index: 100;
  transition: transform var(--duration-normal) var(--ease-default);
}

.sidebar-header {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-5);
  border-bottom: 1px solid var(--separator);
  height: var(--topbar-height);
}

.sidebar-logo {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-md);
  object-fit: contain;
}

.sidebar-brand {
  font-family: var(--font-primary);
  font-size: var(--text-lg);
  font-weight: var(--weight-bold);
  color: var(--color-accent);
}

.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-3) 0;
}

/* Navigation Categories */
.nav-category {
  margin-bottom: var(--space-1);
}

.nav-category-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-3) var(--space-5);
  cursor: pointer;
  border-radius: 0;
  transition: background var(--duration-fast) var(--ease-default);
  min-height: var(--touch-target);
}

.nav-category-header:hover {
  background: var(--fill-quaternary);
}

.nav-category.active > .nav-category-header {
  background: var(--color-accent-subtle);
}

.nav-category-title {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  font-family: var(--font-primary);
  font-size: var(--text-sm);
  font-weight: var(--weight-semibold);
  color: var(--text-primary);
}

.nav-category-icon {
  font-size: 18px;
  width: 24px;
  text-align: center;
  opacity: 0.8;
}

.nav-category-chevron {
  font-size: var(--text-xs);
  color: var(--text-tertiary);
  transition: transform var(--duration-fast) var(--ease-default);
}

.nav-category.open .nav-category-chevron {
  transform: rotate(90deg);
}

/* Sub Navigation */
.nav-subitems {
  max-height: 0;
  overflow: hidden;
  transition: max-height var(--duration-normal) var(--ease-default);
}

.nav-category.open .nav-subitems {
  max-height: 500px;
}

.nav-subitem {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-5);
  padding-left: calc(var(--space-5) + 36px);
  color: var(--text-secondary);
  text-decoration: none;
  font-size: var(--text-sm);
  min-height: var(--touch-target);
  transition: all var(--duration-fast) var(--ease-default);
}

.nav-subitem:hover {
  color: var(--text-primary);
  background: var(--fill-quaternary);
}

.nav-subitem.active {
  color: var(--color-accent);
  background: var(--color-accent-subtle);
  font-weight: var(--weight-medium);
}

/* Sidebar Footer */
.sidebar-footer {
  border-top: 1px solid var(--separator);
  padding: var(--space-4) var(--space-5);
}

.sidebar-user {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.user-avatar {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-full);
  background: linear-gradient(135deg, var(--color-accent), var(--color-accent-hover));
  color: var(--text-inverse);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: var(--weight-semibold);
  font-size: var(--text-sm);
}

.user-name {
  font-weight: var(--weight-semibold);
  font-size: var(--text-sm);
  color: var(--text-primary);
}

.user-role {
  font-size: var(--text-xs);
  color: var(--text-tertiary);
}

/* ===================== TOPBAR ===================== */
.topbar {
  position: sticky;
  top: 0;
  height: var(--topbar-height);
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--separator);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--space-5);
  z-index: 90;
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: var(--space-4);
}

.topbar-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: var(--touch-target);
  height: var(--touch-target);
  background: none;
  border: none;
  font-size: 20px;
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: var(--radius-md);
  transition: all var(--duration-fast) var(--ease-default);
}

.topbar-toggle:hover {
  background: var(--fill-tertiary);
  color: var(--text-primary);
}

.topbar-breadcrumb {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-sm);
  color: var(--text-tertiary);
}

.topbar-breadcrumb a {
  color: var(--text-tertiary);
  text-decoration: none;
  transition: color var(--duration-fast);
}

.topbar-breadcrumb a:hover {
  color: var(--color-accent);
}

.topbar-breadcrumb .current {
  color: var(--text-primary);
  font-weight: var(--weight-medium);
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.topbar-action {
  display: flex;
  align-items: center;
  justify-content: center;
  width: var(--touch-target);
  height: var(--touch-target);
  background: none;
  border: none;
  font-size: 18px;
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: var(--radius-md);
  transition: all var(--duration-fast) var(--ease-default);
}

.topbar-action:hover {
  background: var(--fill-tertiary);
  color: var(--text-primary);
}

/* ===================== CARDS ===================== */
.card {
  background: var(--bg-secondary);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  padding: var(--space-5);
  margin-bottom: var(--space-5);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-4);
  padding-bottom: var(--space-4);
  border-bottom: 1px solid var(--separator);
}

.card-title {
  font-family: var(--font-primary);
  font-size: var(--text-lg);
  font-weight: var(--weight-semibold);
  color: var(--text-primary);
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.card-body {
  padding: 0;
}

.card-footer {
  margin-top: var(--space-4);
  padding-top: var(--space-4);
  border-top: 1px solid var(--separator);
}

/* Grouped Cards (iOS-style) */
.card-group {
  background: var(--bg-secondary);
  border-radius: var(--radius-lg);
  overflow: hidden;
  margin-bottom: var(--space-5);
}

.card-group-header {
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-tertiary);
  padding: var(--space-4) var(--space-5) var(--space-2);
}

.card-group-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-3) var(--space-5);
  min-height: var(--touch-target);
  border-bottom: 1px solid var(--separator);
}

.card-group-item:last-child {
  border-bottom: none;
}

/* ===================== BUTTONS ===================== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-5);
  min-height: var(--touch-target);
  border: none;
  border-radius: var(--radius-md);
  font-family: var(--font-primary);
  font-size: var(--text-base);
  font-weight: var(--weight-semibold);
  cursor: pointer;
  text-decoration: none;
  transition: all var(--duration-fast) var(--ease-default);
  white-space: nowrap;
}

.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  transform: none !important;
}

/* Primary Button - Filled */
.btn-primary {
  background: var(--color-accent);
  color: var(--text-inverse);
}

.btn-primary:hover:not(:disabled) {
  background: var(--color-accent-hover);
  transform: scale(1.02);
}

.btn-primary:active:not(:disabled) {
  background: var(--color-accent-active);
  transform: scale(0.98);
}

/* Secondary Button - Tinted */
.btn-secondary {
  background: var(--fill-secondary);
  color: var(--text-primary);
}

.btn-secondary:hover:not(:disabled) {
  background: var(--fill-primary);
}

/* Outline Button */
.btn-outline {
  background: transparent;
  border: 1.5px solid var(--border-color);
  color: var(--text-primary);
}

.btn-outline:hover:not(:disabled) {
  background: var(--fill-quaternary);
  border-color: var(--color-accent);
  color: var(--color-accent);
}

.btn-outline-primary {
  background: transparent;
  border: 1.5px solid var(--color-accent);
  color: var(--color-accent);
}

.btn-outline-primary:hover:not(:disabled) {
  background: var(--color-accent);
  color: var(--text-inverse);
}

/* Ghost Button */
.btn-ghost {
  background: transparent;
  color: var(--color-accent);
}

.btn-ghost:hover:not(:disabled) {
  background: var(--color-accent-subtle);
}

/* Success Button */
.btn-success {
  background: var(--color-success);
  color: var(--text-inverse);
}

.btn-success:hover:not(:disabled) {
  filter: brightness(0.9);
}

/* Danger Button */
.btn-danger {
  background: var(--color-error);
  color: var(--text-inverse);
}

.btn-danger:hover:not(:disabled) {
  filter: brightness(0.9);
}

/* Button Sizes */
.btn-sm {
  padding: var(--space-2) var(--space-3);
  min-height: 32px;
  font-size: var(--text-sm);
  border-radius: var(--radius-sm);
}

.btn-lg {
  padding: var(--space-4) var(--space-6);
  min-height: 52px;
  font-size: var(--text-lg);
  border-radius: var(--radius-lg);
}

.btn-icon {
  width: var(--touch-target);
  height: var(--touch-target);
  padding: 0;
}

.btn-full {
  width: 100%;
}

.btn-group {
  display: flex;
  gap: var(--space-2);
  flex-wrap: wrap;
}

/* ===================== FORMS ===================== */
.form-group {
  margin-bottom: var(--space-4);
}

.form-label {
  display: block;
  font-family: var(--font-primary);
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  color: var(--text-primary);
  margin-bottom: var(--space-2);
}

.form-label.required::after {
  content: " *";
  color: var(--color-error);
}

.form-control,
.input,
.textarea,
.select {
  width: 100%;
  padding: var(--space-3) var(--space-4);
  min-height: var(--touch-target);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  font-family: var(--font-secondary);
  font-size: var(--text-base);
  color: var(--text-primary);
  background: var(--bg-secondary);
  transition: all var(--duration-fast) var(--ease-default);
}

.form-control:focus,
.input:focus,
.textarea:focus,
.select:focus {
  outline: none;
  border-color: var(--color-accent);
  box-shadow: 0 0 0 4px var(--color-accent-light);
}

.form-control:disabled,
.input:disabled {
  background: var(--fill-quaternary);
  color: var(--text-tertiary);
  cursor: not-allowed;
}

.form-control.error,
.input.error {
  border-color: var(--color-error);
}

.form-control.error:focus,
.input.error:focus {
  box-shadow: 0 0 0 4px var(--color-error-light);
}

textarea.form-control,
.textarea {
  min-height: 120px;
  resize: vertical;
  line-height: var(--leading-relaxed);
}

select.form-control,
.select {
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%238E8E93' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right var(--space-4) center;
  padding-right: var(--space-10);
}

.form-hint {
  font-size: var(--text-xs);
  color: var(--text-tertiary);
  margin-top: var(--space-1);
}

.form-error {
  font-size: var(--text-xs);
  color: var(--color-error);
  margin-top: var(--space-1);
}

.form-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: var(--space-4);
}

.form-actions {
  display: flex;
  gap: var(--space-3);
  justify-content: flex-end;
  margin-top: var(--space-5);
  padding-top: var(--space-4);
  border-top: 1px solid var(--separator);
}

/* ===================== TABLES ===================== */
.table-container {
  overflow-x: auto;
  margin: calc(-1 * var(--space-5));
  margin-top: 0;
}

.table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-sm);
}

.table th,
.table td {
  padding: var(--space-3) var(--space-4);
  text-align: left;
  border-bottom: 1px solid var(--separator);
}

.table th {
  font-family: var(--font-primary);
  font-weight: var(--weight-semibold);
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-tertiary);
  background: var(--fill-quaternary);
}

.table tbody tr {
  transition: background var(--duration-fast);
}

.table tbody tr:hover {
  background: var(--fill-quaternary);
}

.table tbody tr.selected {
  background: var(--color-accent-subtle);
}

/* ===================== BADGES ===================== */
.badge {
  display: inline-flex;
  align-items: center;
  padding: var(--space-1) var(--space-2);
  border-radius: var(--radius-sm);
  font-family: var(--font-primary);
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
}

.badge-primary {
  background: var(--color-accent-light);
  color: var(--color-accent);
}

.badge-success {
  background: var(--color-success-light);
  color: var(--color-success);
}

.badge-warning {
  background: var(--color-warning-light);
  color: var(--color-accent);
}

.badge-error {
  background: var(--color-error-light);
  color: var(--color-error);
}

.badge-info {
  background: var(--color-info-light);
  color: var(--color-info);
}

.badge-neutral {
  background: var(--fill-tertiary);
  color: var(--text-secondary);
}

/* ===================== ALERTS ===================== */
.alert {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-4);
  border-radius: var(--radius-lg);
  margin-bottom: var(--space-4);
  font-size: var(--text-sm);
}

.alert-icon {
  font-size: 20px;
  flex-shrink: 0;
  margin-top: 1px;
}

.alert-content {
  flex: 1;
}

.alert-title {
  font-weight: var(--weight-semibold);
  margin-bottom: var(--space-1);
}

.alert-success {
  background: var(--color-success-light);
  color: var(--color-success);
}

.alert-warning {
  background: var(--color-warning-light);
  color: var(--color-accent);
}

.alert-error {
  background: var(--color-error-light);
  color: var(--color-error);
}

.alert-info {
  background: var(--color-info-light);
  color: var(--color-info);
}

/* ===================== FLASH MESSAGES ===================== */
.flash {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-md);
  margin-bottom: var(--space-4);
  font-weight: var(--weight-medium);
  font-size: var(--text-sm);
}

.flash-success {
  background: var(--color-success-light);
  color: var(--color-success);
}

.flash-error {
  background: var(--color-error-light);
  color: var(--color-error);
}

.flash-warning {
  background: var(--color-warning-light);
  color: var(--color-accent);
}

.flash-info {
  background: var(--color-info-light);
  color: var(--color-info);
}

/* ===================== STATS CARDS ===================== */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: var(--space-4);
  margin-bottom: var(--space-5);
}

.stats-row {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  gap: var(--space-4);
  margin-bottom: var(--space-5);
  overflow-x: auto;
}

.stat-card {
  background: var(--bg-secondary);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
  text-align: center;
  flex: 1 1 0;
  min-width: 140px;
  box-shadow: var(--shadow-xs);
}

.stat-value {
  font-family: var(--font-primary);
  font-size: var(--text-3xl);
  font-weight: var(--weight-bold);
  color: var(--color-accent);
  line-height: 1;
  margin-bottom: var(--space-1);
}

.stat-label {
  font-size: var(--text-sm);
  color: var(--text-tertiary);
}

.stat-card.success .stat-value { color: var(--color-success); }
.stat-card.error .stat-value { color: var(--color-error); }
.stat-card.info .stat-value { color: var(--color-info); }

/* ===================== TILES (Dashboard) ===================== */
.nav-tiles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: var(--space-5);
  margin-bottom: var(--space-8);
}

.tile {
  height: 200px;
  background: var(--bg-secondary);
  border-radius: var(--radius-xl);
  padding: var(--space-6);
  text-decoration: none;
  color: var(--text-primary);
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  transition: all var(--duration-normal) var(--ease-spring);
  position: relative;
  overflow: hidden;
}

.tile:hover {
  transform: translateY(-6px) scale(1.01);
  box-shadow: var(--shadow-lg);
}

.tile:active {
  transform: translateY(-2px) scale(0.99);
}

.tile-icon {
  font-size: 36px;
  margin-bottom: var(--space-3);
}

.tile-title {
  font-size: var(--text-lg);
  font-weight: var(--weight-bold);
  margin-bottom: var(--space-2);
}

.tile-desc {
  font-size: var(--text-sm);
  color: var(--text-secondary);
  line-height: var(--leading-relaxed);
}

.tile-disabled {
  opacity: 0.4;
  cursor: not-allowed;
  pointer-events: none;
}

/* ===================== EMPTY STATE ===================== */
.empty-state {
  text-align: center;
  padding: var(--space-12) var(--space-5);
}

.empty-icon {
  font-size: 56px;
  margin-bottom: var(--space-4);
  opacity: 0.4;
}

.empty-state h3 {
  color: var(--text-primary);
  margin-bottom: var(--space-2);
}

.empty-state p {
  color: var(--text-secondary);
  margin-bottom: var(--space-5);
}

/* ===================== LOADING ===================== */
.loading-spinner {
  display: inline-block;
  width: 24px;
  height: 24px;
  border: 2.5px solid var(--fill-secondary);
  border-top-color: var(--color-accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

.loading {
  opacity: 0.5;
  pointer-events: none;
  position: relative;
}

/* ===================== TOGGLE SWITCH ===================== */
.toggle-switch {
  position: relative;
  display: inline-block;
  width: 51px;
  height: 31px;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: var(--fill-secondary);
  transition: var(--duration-fast) var(--ease-default);
  border-radius: 31px;
}

.toggle-slider:before {
  position: absolute;
  content: "";
  height: 27px;
  width: 27px;
  left: 2px;
  bottom: 2px;
  background-color: var(--color-white);
  transition: var(--duration-fast) var(--ease-default);
  border-radius: 50%;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

input:checked + .toggle-slider {
  background-color: var(--color-success);
}

input:checked + .toggle-slider:before {
  transform: translateX(20px);
}

/* ===================== MODAL / DIALOG ===================== */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  opacity: 0;
  visibility: hidden;
  transition: all var(--duration-normal) var(--ease-default);
}

.modal-overlay.active {
  opacity: 1;
  visibility: visible;
}

.modal {
  background: var(--bg-secondary);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-float);
  max-width: 480px;
  width: 90%;
  max-height: 90vh;
  overflow: hidden;
  transform: scale(0.95) translateY(20px);
  transition: transform var(--duration-normal) var(--ease-spring);
}

.modal-overlay.active .modal {
  transform: scale(1) translateY(0);
}

.modal-header {
  padding: var(--space-5);
  border-bottom: 1px solid var(--separator);
}

.modal-title {
  font-size: var(--text-lg);
  font-weight: var(--weight-semibold);
}

.modal-body {
  padding: var(--space-5);
  overflow-y: auto;
}

.modal-footer {
  padding: var(--space-4) var(--space-5);
  border-top: 1px solid var(--separator);
  display: flex;
  justify-content: flex-end;
  gap: var(--space-3);
}

/* ===================== RESPONSIVE ===================== */
@media (max-width: 1024px) {
  :root {
    --sidebar-width: 240px;
  }
}

@media (max-width: 768px) {
  :root {
    --text-4xl: 28px;
    --text-3xl: 24px;
  }
  
  .sidebar {
    transform: translateX(-100%);
    width: 280px;
  }
  
  .sidebar.open {
    transform: translateX(0);
  }
  
  .main-wrapper {
    margin-left: 0;
  }
  
  .main-content {
    padding: var(--space-4);
  }
  
  .nav-tiles {
    grid-template-columns: 1fr;
  }
  
  .tile {
    height: auto;
    min-height: 160px;
  }
  
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .form-row {
    grid-template-columns: 1fr;
  }
  
  .form-actions {
    flex-direction: column;
  }
  
  .btn-group {
    flex-direction: column;
  }
  
  .btn-group .btn {
    width: 100%;
  }
}

@media (max-width: 480px) {
  .stats-grid {
    grid-template-columns: 1fr;
  }
  
  .stat-card {
    padding: var(--space-4);
  }
  
  .stat-value {
    font-size: var(--text-2xl);
  }
}

/* ===================== UTILITIES ===================== */
.hidden { display: none !important; }
.sr-only { 
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

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

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

.m-0 { margin: 0; }
.mt-2 { margin-top: var(--space-2); }
.mt-3 { margin-top: var(--space-3); }
.mt-4 { margin-top: var(--space-4); }
.mt-5 { margin-top: var(--space-5); }
.mb-2 { margin-bottom: var(--space-2); }
.mb-3 { margin-bottom: var(--space-3); }
.mb-4 { margin-bottom: var(--space-4); }
.mb-5 { margin-bottom: var(--space-5); }

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

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

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

.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); }

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

/* ===================== ACCESSIBILITY ===================== */
:focus-visible {
  outline: 3px solid var(--color-accent);
  outline-offset: 2px;
}

/* Reduced motion */
@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;
  }
}

/* High contrast mode */
@media (prefers-contrast: high) {
  :root {
    --separator: var(--color-gray-400);
    --border-color: var(--color-gray-400);
  }
}

/* ===================== PRINT ===================== */
@media print {
  .sidebar,
  .topbar,
  .btn,
  .no-print {
    display: none !important;
  }
  
  .main-wrapper {
    margin-left: 0;
  }
  
  .main-content {
    padding: 0;
    max-width: none;
  }
  
  .card {
    box-shadow: none;
    border: 1px solid #ddd;
    break-inside: avoid;
  }
  
  body {
    background: white;
    color: black;
    font-size: 12pt;
  }
}

/* ===================== GLOBAL LAYOUT FIXES ===================== */
/* Prevent horizontal overflow on all containers */
.wrap,
.container,
.main-content,
.card,
.card-body {
  max-width: 100%;
  overflow-x: hidden;
}

/* Ensure all content respects container boundaries */
.card > *,
.card-body > * {
  max-width: 100%;
}

/* Button groups should wrap properly */
.btn-group,
.form-actions,
.action-bar {
  flex-wrap: wrap;
}

/* Forms should be fully responsive */
.form-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: var(--space-4);
}

/* Filter patterns - common layout */
.filter-form,
.search-form {
  width: 100%;
}

.filter-row,
.search-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: var(--space-3);
  align-items: end;
}

/* Tables should scroll horizontally when needed */
.table-responsive,
.table-container {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* Ensure inputs don't overflow */
input,
select,
textarea,
.form-control,
.input {
  max-width: 100%;
  min-width: 0;
}

/* Page header layout */
.page-header {
  margin-bottom: var(--space-6);
}

.page-header h1 {
  font-size: var(--text-3xl);
  font-weight: var(--weight-bold);
  color: var(--text-primary);
  margin-bottom: var(--space-1);
}

.page-header .subtitle,
.page-header p {
  font-size: var(--text-base);
  color: var(--text-secondary);
  margin: 0;
}

/* Action bar improvements */
.action-bar {
  display: flex;
  gap: var(--space-3);
  margin-bottom: var(--space-5);
  flex-wrap: wrap;
}

/* Card header with actions */
.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  flex-wrap: wrap;
}

.card-header h2,
.card-header .card-title {
  margin: 0;
  flex: 1;
  min-width: 200px;
}

.card-header .header-actions {
  display: flex;
  gap: var(--space-2);
  flex-wrap: wrap;
}

/* Breadcrumb improvements */
.breadcrumb {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-sm);
  color: var(--text-tertiary);
  margin-bottom: var(--space-4);
  flex-wrap: wrap;
}

.breadcrumb a {
  color: var(--text-tertiary);
  text-decoration: none;
  transition: color var(--duration-fast);
}

.breadcrumb a:hover {
  color: var(--color-accent);
}

.breadcrumb span {
  color: var(--text-quaternary);
}

/* Small button text fix */
.btn-sm,
.btn-small {
  font-size: var(--text-xs);
  padding: var(--space-2) var(--space-3);
  min-height: 32px;
}

/* Table improvements */
.table {
  width: 100%;
  min-width: 600px; /* Ensures scrolling on small screens */
}

.table th,
.table td {
  padding: var(--space-3) var(--space-4);
  vertical-align: middle;
}

.table .btn {
  white-space: nowrap;
}

/* Mobile-first responsive fixes */
@media (max-width: 768px) {
  .page-header h1 {
    font-size: var(--text-2xl);
  }
  
  .card {
    padding: var(--space-4);
  }
  
  .card-header {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .card-header h2,
  .card-header .card-title {
    min-width: 100%;
    margin-bottom: var(--space-3);
  }
  
  .card-header .header-actions {
    width: 100%;
  }
  
  .form-row {
    grid-template-columns: 1fr;
  }
  
  .action-bar {
    flex-direction: column;
  }
  
  .action-bar .btn {
    width: 100%;
    justify-content: center;
  }
}
