/* =================================================================
   Territory Manager - Main Layout & Core Styles
   
   This file contains the foundation styles for the application:
   - CSS Custom Properties (design tokens)
   - Layout structure (sidebar, main content, responsive grid)
   - Navigation and sidebar components
   - Content areas and page headers
   - Tables and data display
   - Forms and inputs
   - Status badges
   - Login page
   - Alerts
   - Utility classes
   - Responsive breakpoints for mobile/tablet/desktop
   
   Load Order: First (provides base variables and layout)
   ================================================================= */

/* =================================================================
   CSS CUSTOM PROPERTIES (Design Tokens)
   Central color palette and design system values
   - Use these variables throughout the app for consistency
   - Easier theming and maintenance
   ================================================================= */
:root {
    /* Brand Colors - Primary application colors */
    --primary-color: #007bff;    /* Blue - primary actions, links */
    --secondary-color: #6c757d;  /* Gray - secondary actions */
    --success-color: #28a745;    /* Green - success states */
    --danger-color: #dc3545;     /* Red - errors, destructive actions */
    --warning-color: #ffc107;    /* Yellow - warnings, caution */
    --info-color: #17a2b8;       /* Cyan - informational messages */
    --light-color: #f8f9fa;      /* Light gray - backgrounds */
    --dark-color: #343a40;       /* Dark gray - text */
    
    /* Neutral Grays - Full grayscale palette for UI elements */
    --white: #ffffff;
    --gray-100: #f8f9fa;  /* Lightest - page backgrounds */
    --gray-200: #e9ecef;  /* Very light - cards, panels */
    --gray-300: #dee2e6;  /* Light - borders, dividers */
    --gray-400: #ced4da;  /* Medium-light - disabled states */
    --gray-500: #adb5bd;  /* Medium - placeholder text */
    --gray-600: #6c757d;  /* Medium-dark - secondary text */
    --gray-700: #495057;  /* Dark - body text */
    --gray-800: #343a40;  /* Very dark - headings */
    --gray-900: #212529;  /* Darkest - emphasis text */
    
    /* Design System Tokens */
    --border-radius: 0.375rem;  /* 6px - standard rounded corners */
    --box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075);  /* Subtle depth */
    --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;  /* System font stack */
}

/* =================================================================
   GLOBAL RESETS
   Normalize browser defaults for consistent rendering
   ================================================================= */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;  /* Include padding and border in element width */
}

body {
    font-family: var(--font-family);
    font-size: 14px;  /* Base font size */
    line-height: 1.5;  /* Comfortable reading height */
    color: var(--dark-color);
    background-color: var(--gray-100);  /* Light gray page background */
}

/* =================================================================
   LAYOUT - Application Container
   Two-column layout: Sidebar (250px) + Main Content (flexible)
   ================================================================= */
.app-container {
    display: flex;
    height: 100vh;  /* Full viewport height */
}

/* =================================================================
   SIDEBAR - Navigation Panel
   Fixed 250px width column on desktop
   - App branding at top
   - User info section
   - Navigation menu
   - Logout button at bottom
   ================================================================= */
.sidebar {
    width: 250px;
    background: linear-gradient(180deg, #ffffff 0%, #f8f9fa 100%);  /* Subtle gradient */
    border-right: 1px solid var(--gray-300);
    display: flex;
    flex-direction: column;  /* Stack children vertically */
    height: 100vh;
    box-shadow: 2px 0 8px rgba(0,0,0,0.05);  /* Subtle right shadow for depth */
}

/* Desktop: Make sidebar fixed so it stays visible when scrolling content */
@media (min-width: 992px) {
    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        z-index: 1000;  /* Above content but below modals */
    }

    /* Shift main content to right so it doesn't hide under fixed sidebar */
    .main-content {
        margin-left: 250px;
    }
}

/* Main content area - flexible width, fills remaining space */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;  /* Prevent content from overflowing container */
}

/* Sub-navigation toolbar under page headers (filters, actions) */
.subnav-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    padding: 0.5rem 1rem;
    background: var(--white);
    border-top: 1px solid var(--gray-300);
    border-bottom: 1px solid var(--gray-300);
}
.subnav-controls {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.5rem;
}
.subnav-actions { display: flex; align-items: center; gap: 0.5rem; }
.subnav-checkbox { display: inline-flex; align-items: center; gap: 0.35rem; }

@media (max-width: 768px) {
  .subnav-toolbar {
    flex-direction: column;
    align-items: stretch;
    gap: 0.5rem;
    position: sticky;
    top: 56px; /* sits under mobile app bar */
    z-index: 850;
  }
  .subnav-controls { width: 100%; }
  .subnav-actions { width: 100%; justify-content: flex-end; }
}

/* =================================================================
   SIDEBAR COMPONENTS
   ================================================================= */

/* App Logo/Branding Section */
.app-logo {
    padding: 1.5rem 1rem;
    text-align: center;
    border-bottom: 1px solid var(--gray-300);
    background-color: var(--white);
}

/* Territory layers logo icon */
.logo-icon {
    color: var(--primary-color);
    display: inline-block;
}

/* Application title */
.app-title {
    font-size: 1.35rem;
    font-weight: 700;
    color: var(--primary-color);  /* Brand blue */
    letter-spacing: -0.02em;  /* Tighten spacing for modern look */
    margin-bottom: 0.25rem;
}

/* Version badge */
.app-version {
    font-size: 0.75rem;
    color: var(--gray-500);  /* Muted gray */
    font-weight: 500;
    padding: 0.25rem 0.75rem;
    background-color: var(--gray-100);
    border-radius: 12px;  /* Pill shape */
    display: inline-block;
    margin-top: 0.25rem;
}

/* User Info Section - Shows current logged-in user */
.user-info {
    padding: 1rem;
    text-align: center;
    border-bottom: 1px solid var(--gray-300);
    background-color: var(--gray-50);  /* Subtle background */
}

.user-name {
    font-size: 0.875rem;
    color: var(--gray-700);
    font-weight: 500;  /* Medium weight for emphasis */
}

/* Navigation Menu - Main app navigation */
.nav-menu {
    flex: 1;  /* Take up remaining vertical space */
    padding: 0.5rem 0;
    overflow-y: auto;  /* Scroll if menu items exceed height */
    overflow-x: hidden;  /* Hide horizontal overflow */
    -webkit-overflow-scrolling: touch;  /* Smooth scrolling on iOS */
}

/* Navigation Item - Individual menu links */
.nav-item {
    display: flex;
    align-items: center;  /* Vertically center icon and text */
    padding: 0.75rem 1.25rem;
    margin: 0.25rem 0.75rem;  /* Spacing between items */
    color: var(--gray-700);
    text-decoration: none;  /* Remove underline */
    border-radius: var(--border-radius);
    transition: all 0.2s ease;  /* Smooth hover animation */
    font-weight: 500;
    font-size: 0.9rem;
}

/* Hover state - light background and color change */
.nav-item:hover {
    background-color: var(--gray-100);
    color: var(--primary-color);  /* Highlight in brand blue */
    transform: translateX(2px);  /* Subtle slide to right */
}

/* Active state - currently selected tab */
.nav-item.active {
    background-color: var(--primary-color);  /* Solid blue background */
    color: var(--white);  /* White text for contrast */
    box-shadow: 0 2px 4px rgba(0,123,255,0.2);  /* Blue shadow for depth */
}

/* Icon spacing within nav item */
.nav-item svg {
    margin-right: 0.75rem;
    flex-shrink: 0;  /* Prevent icon from shrinking */
}

/* Sidebar Footer - Logout button container */
.sidebar-footer {
    padding: 1rem;
    border-top: 1px solid var(--gray-300);
    background-color: var(--white);
}

/* Content Area - Fixed Header & Footer Layout */
.content-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    height: 100%;
}

/* Page header - fixed at top */
.page-header {
  display: flex;
  flex-direction: column;
    gap: 0.75rem;
    padding: 0.75rem 1rem;
    background: linear-gradient(180deg, #ffffff 0%, #f8f9fa 100%);
    border-bottom: 2px solid #007bff;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
    flex-shrink: 0;
    z-index: 50;
}

/* Tab content wrapper - scrollable container for table only */
.tab-content-wrapper {
    flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
    min-height: 0;
    background-color: var(--white);
}

/* Ensure pagination is at bottom (outside wrapper) */
.content-area > .pagination-controls {
    flex-shrink: 0;
}

.page-title {
    font-size: 1.4rem;
    font-weight: 600;
    color: var(--dark-color);
}

/* Secondary toolbar (below statistics): filters + actions */
.subnav-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .75rem;
  padding: .5rem 1rem;
  background: #fff;
  border-top: 1px solid var(--gray-300);
  border-bottom: 1px solid var(--gray-200);
  border-radius: 6px;
  margin-top: .5rem;
}
.subnav-controls { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.subnav-actions { display: flex; align-items: center; gap: .5rem; }
.subnav-checkbox { display:flex; align-items:center; gap:.35rem; margin: 0 .25rem; color: var(--gray-700); }

@media (max-width: 992px) {
  .subnav-toolbar { padding: .5rem .75rem; gap: .5rem; }
  .subnav-controls { gap: .45rem; }
}

/* Desktop sticky header + subnav (kept outside scroll wrapper) */
@media (min-width: 992px) {
  /* Ensure stacking context */
  #content-area { position: relative; }
  .page-header { position: sticky; top: 0; z-index: 900; }
  /* Subnav sits directly under header; adjust offset as needed */
  .subnav-toolbar { position: sticky; top: var(--header-offset, 64px); z-index: 850; }
}

/* =================================================================
   BUTTONS - Removed duplicates
   All button styling now in buttons.css (loaded second)
   Kept only basic .btn class for backward compatibility
   ================================================================= */

/* Basic button class - most styling in buttons.css */
.btn {
    display: inline-block;
    padding: 0.5rem 1rem;
    margin-bottom: 0;
    font-size: 0.875rem;
    font-weight: 400;
    line-height: 1.5;
    text-align: center;
    text-decoration: none;
    vertical-align: middle;
    cursor: pointer;
    border: 1px solid transparent;
    border-radius: var(--border-radius);
    transition: all 0.2s ease;
    background-color: transparent;
}

/* =================================================================
   TABLES
   Data table styling with hover effects
   ================================================================= */
.table-container {
    background-color: var(--white);
    min-height: 0;
    overflow-x: auto;
}

.table-header {
    padding: 1rem;
    background-color: var(--gray-50);
    border-bottom: 1px solid var(--gray-300);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.table {
    width: 100%;
    border-collapse: collapse;
}

.table thead {
    position: sticky;
    top: 0;
    z-index: 10;
    background-color: var(--gray-50);
}

.table th,
.table td {
    padding: 0.75rem;
    text-align: left;
    border-bottom: 1px solid var(--gray-300);
    word-wrap: break-word;
    overflow-wrap: break-word;
    white-space: normal;
}

.table th {
    background-color: var(--gray-50);
    font-weight: 600;
    color: var(--gray-700);
}

.table tbody tr:hover {
    background-color: var(--gray-50);
}

/* Button spacing in table action cells */
.table td button {
    margin-right: 0.25rem;
    margin-bottom: 0.25rem;
}

.table td button:last-child {
    margin-right: 0;
}

/* Desktop: Actions column flex layout */
.table tbody td:last-child,
.table tbody td.col-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  align-items: flex-start;
}

.table tbody td:last-child .btn {
    flex-shrink: 0;
    white-space: nowrap;
}

/* Territory flags shown inline next to the Name column */
.flag-icons { display: inline-flex; gap: 4px; vertical-align: middle; }
.flag-icon { width: 16px; height: 16px; display: inline-block; vertical-align: -2px; }
.flag-part-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  border-radius: 3px;
  background: #000;
  color: #fff;
  font-size: 10px;
  line-height: 1;
}

/* =================================================================
   UTILITY CLASSES
   Small helper text and muted content
   ================================================================= */
.muted {
    color: var(--gray-600);  /* Dimmed text */
    font-size: 0.85em;
}

/* Multi-part territory assignment display */
.territory-part-info {
    font-size: 0.9em;
    line-height: 1.4;
}

.territory-part-info .muted {
    color: var(--gray-600);
    font-size: 0.85em;
}

/* =================================================================
   MODAL - Removed duplicates (detailed styles now in modals.css)
   Kept only minimal compatibility stubs
   ================================================================= */

/* =================================================================
   FORMS - Input Fields and Controls
   Form elements for user input across all modals and tabs
   ================================================================= */

/* Form group - wraps label and input */
.form-group {
    margin-bottom: 1rem;  /* Space between form fields */
}

/* Form label - field description */
.form-label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 500;  /* Medium weight for readability */
    color: var(--gray-700);
}

/* Form control - text inputs, selects, textareas */
.form-control {
    display: block;
    width: 100%;
    padding: 0.5rem 0.75rem;
    font-size: 0.875rem;
    line-height: 1.5;
    color: var(--gray-700);
    background-color: var(--white);
    border: 1px solid var(--gray-400);  /* Medium gray border */
    border-radius: var(--border-radius);
    transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}

/* Focus state - highlight when typing */
.form-control:focus {
    outline: none;  /* Remove browser default */
    border-color: var(--primary-color);  /* Blue border */
    box-shadow: 0 0 0 0.125rem rgba(0, 123, 255, 0.25);  /* Blue glow */
}

/* Checkbox wrapper */
.form-check {
    display: flex;
    align-items: center;
    gap: 0.5rem;  /* Space between checkbox and label */
    margin-bottom: 0.5rem;
}

.form-check-input {
    margin: 0;
}

/* =================================================================
   LOGIN PAGE - Authentication UI
   Full-page centered login form
   ================================================================= */

/* Login container - vertically and horizontally centers card */
.login-container {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: transparent;  /* Transparent background */
}

/* Login card - white box containing form */
.login-card {
    background-color: rgba(255, 255, 255, 0.7);
    padding: 2rem;
    border-radius: var(--border-radius);
    box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);  /* Elevated shadow */
    width: 100%;
    max-width: 400px;  /* Limit width on large screens */
}

/* Login header - branding area */
.login-header {
    text-align: center;
    margin-bottom: 2rem;
}

.login-title {
    font-size: 1.5rem;
    font-weight: 600;
    color: var(--dark-color);
    margin-bottom: 0.5rem;
}

.login-subtitle {
    color: var(--gray-600);  /* Muted text */
    font-size: 0.875rem;
}

/* =================================================================
   ALERTS - Removed duplicates (now in utilities.css)
   ================================================================= */

/* =================================================================
   STATUS BADGES - Colored Labels
   Visual indicators for status, roles, types
   ================================================================= */
.badge {
    display: inline-block;
    padding: 0.25rem 0.5rem;
    font-size: 0.75rem;
    font-weight: 600;
    line-height: 1;
    text-align: center;
    white-space: nowrap;  /* Prevent wrapping */
    vertical-align: baseline;
    border-radius: var(--border-radius);
}

/* Badge color variants matching alert/button colors */
.badge-primary { background-color: var(--primary-color); color: var(--white); }
.badge-secondary { background-color: var(--secondary-color); color: var(--white); }
.badge-success { background-color: var(--success-color); color: var(--white); }
.badge-danger { background-color: var(--danger-color); color: var(--white); }
.badge-warning { background-color: var(--warning-color); color: var(--dark-color); }  /* Dark text on yellow */
.badge-info { background-color: var(--info-color); color: var(--white); }

/* =================================================================
   UTILITY CLASSES - Removed duplicates (now in utilities.css)
   Kept only common display utilities for backward compatibility
   ================================================================= */

/* =================================================================
   LOADING SPINNER - Removed duplicate (now in utilities.css)
   ================================================================= */

/* =================================================================
   RESPONSIVE DESIGN - TABLET (max-width: 768px)
   Adjust layout for tablets and medium screens
   - Move sidebar to bottom as tab bar
   - Simplify page headers
   - Stack controls vertically
   ================================================================= */
@media (max-width: 768px) and (min-width: 601px) {
    /* TABLET: Vertical icon menu on left side - compact 55px */
    .app-container {
        flex-direction: row !important;
    }
    
    .sidebar {
        width: 55px !important;
        height: 100vh !important;
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        bottom: auto !important;
        border-right: 1px solid var(--gray-300) !important;
        border-top: none !important;
        background: var(--white);
        z-index: 1000;
        box-shadow: 2px 0 4px rgba(0,0,0,0.08);
        display: flex !important;
        flex-direction: column !important;
    }
    
    .app-logo { 
        display: none; 
    }
    
    .nav-menu { 
        display: flex !important;
        flex-direction: column !important;
        padding: 0.3rem 0 !important;
        gap: 0.15rem !important;
        flex: 1 !important;
        overflow-y: auto !important;
    }
    
    .nav-item { 
        width: 100% !important;
        padding: 0.5rem 0.1rem !important;
        text-align: center !important;
        border-radius: var(--border-radius);
        font-size: 0.6rem !important;
        display: flex !important;
        flex-direction: column !important;
        align-items: center !important;
        min-height: 52px !important;
    }
    
    .nav-item span[aria-hidden],
    .nav-item svg {
        display: block !important;
        margin: 0 auto 0.2rem !important;
        width: 20px !important;
        height: 20px !important;
    }
    
    .nav-item span:not([aria-hidden]) {
        margin-left: 0 !important;
        display: none !important; /* Ukryj teksty na tablecie - tylko ikony */
        font-size: 0.6rem !important;
        word-wrap: break-word !important;
        line-height: 1.1 !important;
    }
    
    /* Tablet: Ukryj Dashboard */
    .nav-menu .nav-item[data-tab="dashboard"] {
        display: none !important;
    }
    
    /* Logout at bottom of sidebar */
    .sidebar-footer { 
        display: block !important;
        padding: 0.3rem 0.1rem !important;
        border-top: 1px solid var(--gray-300) !important;
    }
    
    .sidebar-footer .btn-logout {
        width: 100% !important;
        padding: 0.5rem 0.1rem !important;
        font-size: 0.6rem !important;
        display: flex !important;
        flex-direction: column !important;
        align-items: center !important;
        background: transparent !important;
        border: none !important;
        color: var(--danger-color) !important;
    }
    
    .sidebar-footer .btn-logout::before {
        content: "🚪";
        font-size: 1.1rem;
        display: block;
        margin-bottom: 0.2rem;
    }
    
    .main-content { 
        margin-left: 55px !important;
        padding-bottom: 0 !important;
        height: 100vh !important;
        width: calc(100% - 55px) !important;
    }
    
    .content-area {
        padding: 0.5rem;
        overflow-y: auto;
    }
    
    /* Simplify page header on tablets */
    .page-header {
        flex-direction: column;
        gap: 0.5rem;
        align-items: flex-start !important;
    }
    
    .page-header .d-flex {
        flex-wrap: wrap;
        width: 100%;
    }

    .modal-dialog { 
        margin: 1rem; 
        width: calc(100% - 2rem); 
    }
    
    /* TABLET: Enable horizontal scroll for wide tables */
    .table-container {
        overflow-x: auto !important;
    }
    
    .table {
        min-width: 800px; /* Force minimum width to trigger scroll */
    }
}

/* =================================================================
   LAYOUT ENFORCEMENT - Prevent Content Overlap
   Force two-column layout and prevent content from slipping under sidebar
   ================================================================= */
.app-container {
    display: flex !important;
    flex-direction: row !important;
    align-items: flex-start !important;
    height: 100vh !important;
}
.sidebar {
    flex: 0 0 250px !important;  /* Fixed width, no grow/shrink */
}
.main-content {
    flex: 1 1 auto !important;  /* Flexible, fill remaining space */
    min-height: 100vh !important;
}

/* FAB hidden by default (shown only on phones) */
.fab-allocate { display: none !important; }
.mobile-fab-full-map { display: none !important; }

/* Mobile - Phone screens */
@media (max-width: 600px) {
  /* Subnav: simplify to a single search on phones */
  .subnav-toolbar { 
    position: sticky !important; 
    top: 120px !important; /* Pod page-header (56px app bar + ~64px header) */
    z-index: 850 !important; 
    padding: 0.5rem 0.5rem !important;
    background: var(--white) !important; /* Dodane tło */
    box-shadow: 0 2px 4px rgba(0,0,0,0.05) !important;
  }
  /* Hide all controls except the main search field */
  .subnav-toolbar .subnav-controls select,
  .subnav-toolbar .subnav-actions,
  .subnav-toolbar .subnav-checkbox,
  .subnav-toolbar label.subnav-checkbox { 
    display: none !important; 
  }
  .subnav-toolbar #territories-filter { 
    display: block !important; 
    width: 100% !important; 
    max-width: 100% !important; 
  }

  /* =================================================================
     MOBILE LAYOUT - Optimized for small screens
     - Smaller fonts throughout
     - Header takes 15% of screen height
     - Footer scrolls with content (not fixed)
     - Compact spacing
     ================================================================= */
  
  /* Base font size reduction for mobile */
  body {
    font-size: 0.75rem; /* Significantly reduced for mobile */
  }
  
  .page-title { 
    font-size: 0.875rem; /* Smaller page title */
    margin-bottom: 0.25rem; 
  }
  
  .app-title { 
    font-size: 0.8125rem; /* Smaller app title */
  }
  
  /* Sticky, compact filter/search bar below app bar */
  .page-header {
    position: sticky !important;
    top: 56px !important; /* Sits just below the fixed app bar */
    z-index: 900 !important;
    background: var(--white) !important;
    max-height: none !important;
    min-height: 0;
    padding: 0.3rem !important; /* Further reduced */
    overflow-x: auto !important; /* Horizontal scroll */
    overflow-y: visible !important;
    gap: 0.15rem !important; /* Further reduced */
    box-shadow: 0 2px 8px rgba(0,0,0,0.06) !important; /* subtle separation */
    flex-shrink: 0;
    font-size: 0.7rem !important; /* Further reduced */
  }
  
  /* Zmniejsz tytuły stron */
  .page-header h1,
  .page-header .page-title {
    font-size: 1.1rem !important; /* Zmniejszone z domyślnego */
    margin: 0 !important;
  }
  
  /* Hide complex filters on mobile - show only search */
  .page-header > div:first-child {
    display: flex !important;
    flex-direction: row !important; /* Zmienione na row dla scroll */
    gap: 0.25rem !important; /* Zmniejszone */
    flex-wrap: nowrap !important;
  }
  
  .page-header select:not([name*="search"]):not(.pagination select) {
    font-size: 0.75rem !important; /* Mniejsza czcionka */
    padding: 0.3rem !important;
    flex-shrink: 0; /* Nie kurczą się */
  }
  
  .page-header input[type="text"] {
    width: 100% !important;
    font-size: 0.8rem !important; /* Zmniejszone z 0.875rem */
    border-radius: 9999px !important; /* pill */
    background: var(--gray-100) !important;
    border: 1px solid var(--gray-300) !important;
    padding: 0.5rem 0.75rem !important;
  }
  
  /* MOBILE ONLY: App bar at TOP with icons */
  .app-container {
    flex-direction: column !important;
  }
  
  .sidebar {
    position: fixed !important;
    top: 0 !important;
    bottom: auto !important;
    left: 0;
    right: 0;
    width: 100% !important;
    height: 56px !important; /* Compact app bar height */
    border-top: none !important;
    border-bottom: 2px solid var(--primary-color) !important;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1) !important;
    z-index: 1000 !important;
    order: -1; /* Force to top */
  }
  
  /* Hide app logo on mobile */
  .app-logo {
    display: none !important;
  }
  
  /* Sidebar wrapper - horizontal flex container, full width */
  .sidebar {
    display: flex !important;
    flex-direction: row !important;
    align-items: center !important;
    width: 100% !important; /* Full width of viewport without causing horizontal scroll on iOS */
    left: 0 !important;
    right: 0 !important;
  }
  
  /* Navigation menu - scrollable, icons with text */
  .nav-menu {
    display: flex !important;
    flex-direction: row !important;
    justify-content: flex-start !important;
    padding: 0 0.5rem !important;
    min-height: 56px !important;
    gap: 0.3rem !important;
    flex-wrap: nowrap !important;
    overflow-x: auto !important;
    overflow-y: hidden !important;
    flex: 1 1 auto !important;
    align-items: center !important;
  }

  /* Phone IA: show core tabs; hide heavy tabs */
  .nav-menu .nav-item[data-tab="users"],
  .nav-menu .nav-item[data-tab="territories"],
  .nav-menu .nav-item[data-tab="allocations"],
  .nav-menu .nav-item[data-tab="my_territories"],
  .nav-menu .nav-item[data-tab="personal_data"] {
    display: inline-flex !important;
  }
  .nav-menu .nav-item[data-tab="groups"],
  .nav-menu .nav-item[data-tab="reports"],
  .nav-menu .nav-item[data-tab="settings"],
  .nav-menu .nav-item[data-tab="account"],
  .nav-menu .nav-item[data-tab="dashboard"] {
    display: none !important; /* hide heavy tabs on phones */
  }
  
  /* Icons only on phones: hide text labels */
  .nav-menu .nav-item span { display: none !important; }
  
  .nav-item {
    flex: 0 0 auto !important;
    min-width: 60px !important;
    padding: 0.25rem 0.2rem !important;
    font-size: 0.7rem !important;
    text-align: center !important;
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    white-space: nowrap !important;
  }
  
  /* Icon above text - smaller */
  .nav-item svg,
  .nav-item [aria-hidden] {
    margin: 0 0 0.15rem 0 !important;
    width: 22px !important;
    height: 22px !important;
    flex-shrink: 0 !important;
  }
  
  /* Logout at end of menu row - compact */
  .sidebar-footer {
    display: flex !important;
    padding: 0 0.5rem !important;
    border: none !important;
    border-left: 1px solid var(--gray-300) !important;
    margin: 0 !important;
    flex: 0 0 auto !important;
    align-items: center !important;
  }
  
  .sidebar-footer .btn-logout {
    min-width: auto !important;
    padding: 0.4rem 0.6rem !important;
    font-size: 0.75rem !important;
    background: var(--danger-color) !important;
    color: #fff !important;
    border: none !important;
    border-radius: var(--border-radius) !important;
    display: inline-block !important;
    margin: 0 !important;
    text-align: center !important;
    white-space: nowrap !important;
    font-weight: 500 !important;
  }
  
  .sidebar-footer .btn-logout::before {
    display: none !important; /* Hide icon, show only text */
  }
  
  /* Icon-only Logout button on phones (matches HTML id) */
  .sidebar-footer #logout-btn {
    width: 44px !important;
    height: 44px !important;
    min-width: 44px !important;
    padding: 0 !important;
    font-size: 0 !important; /* hide text */
    background: var(--danger-color) !important;
    color: transparent !important;
    border: none !important;
    border-radius: 50% !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
  }
  .sidebar-footer #logout-btn::before {
    content: "";
    width: 24px;
    height: 24px;
    background-image: url('../images/icons/heroicons/outline/power.svg');
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    display: inline-block;
    filter: brightness(0) invert(1); /* Biała ikona */
  }
  
  /* Main content starts below fixed app bar */
  .main-content {
    padding-top: 56px !important; /* Space for fixed top app bar */
    padding-bottom: 0 !important;
    height: auto !important;
    margin-left: 0 !important;
  }
  
  /* Content area - full width, no padding */
  .content-area {
    padding: 0 !important;
    flex: 1 1 auto !important;
    overflow: visible !important;
    display: flex !important;
    flex-direction: column !important;
    width: 100% !important;
  }
  
  /* Tab content wrapper - takes all available space */
  .tab-content-wrapper {
    flex: 1 1 auto !important;
    overflow: visible !important;
    min-height: 0;
    width: 100% !important;
    padding: 0.5rem !important; /* Zwiększone z 0.25rem dla lepszej czytelności */
  }
  
  /* App container scrolls naturally on mobile */
  .app-container {
    height: auto !important;
    min-height: 100vh !important;
    overflow: visible !important;
  }

  /* Hide floating Allocate action button (FAB) on phones per mobile-first design */
  .fab-allocate { display: none !important; }
  
  /* Compact forms - smaller fonts and padding */
  .form-control { 
    font-size: 0.9rem; /* Zwiększone z 0.875rem */
    padding: 0.5rem 0.6rem; /* Zwiększone z 0.3rem 0.4rem */
    min-height: 44px; /* WCAG touch target */
  }
  
  .form-label {
    font-size: 0.85rem; /* Zwiększone z 0.8rem */
    margin-bottom: 0.25rem; /* Zwiększone z 0.15rem */
    font-weight: 600; /* Dodane dla lepszej widoczności */
  }
  
  .form-group {
    margin-bottom: 0.75rem; /* Zwiększone z 0.5rem */
  }
  
  /* Button sizing handled in buttons.css */
  
  /* Page header - stack items vertically with minimal gaps */
  .page-header .d-flex {
    flex-direction: column;
    gap: 0.25rem; /* Reduced gap */
  }
  
  .page-header select,
  .page-header input {
    width: 100% !important;
    padding: 0.3rem 0.4rem !important; /* Compact inputs */
  }

  /* Card-based table view for mobile - full width */
  .table-container {
    overflow-x: visible !important;
    width: 100% !important;
    margin: 0 !important;
    padding: 0 !important;
  }
  
  .table { 
    border: none;
    display: block;
    font-size: 0.8rem; /* Zmniejszone z 0.85rem */
    width: 100% !important;
    margin: 0 !important;
    line-height: 1.35;
  }
  
  .table thead { 
    display: none; 
  }
  
  .table tbody { 
    display: block;
    width: 100% !important;
  }
  
  .table tbody tr { 
    display: block; 
    background: var(--white); 
    box-shadow: var(--box-shadow); 
    border-radius: var(--border-radius); 
    padding: 0.75rem; /* Zwiększone z 0.5rem dla lepszej czytelności */
    margin-bottom: 0.65rem; /* Zwiększone z 0.4rem dla lepszego rozdzielenia */
    border: 1px solid var(--gray-300);
    width: 99.9% !important; /* Avoid iOS rounding gaps */
    box-sizing: border-box !important;
    overflow: hidden; /* contain long content */
  }
  
  .table tbody td { 
    display: block; 
    padding: 0.35rem 0; /* Zwiększone z 0.2rem dla lepszej czytelności */
    border: none;
    text-align: left;
    position: relative;
    padding-left: 45%;
    font-size: 0.875rem; /* Zwiększone z 0.85rem */
    word-break: break-word;
    line-height: 1.5; /* Dodane dla lepszej czytelności */
  }

  /* Emphasize first field as the card title */
  .table tbody td:first-child {
    font-weight: 700 !important;
    font-size: 1rem !important; /* Zwiększone z 0.95rem */
    color: var(--gray-900) !important;
    padding-top: 0; /* Pierwszy element bez top padding */
  }
  
  /* Add data labels before each cell */
  .table tbody td::before {
    content: attr(data-label);
    position: absolute;
    left: 0;
    font-weight: 600;
    color: var(--gray-600);
    font-size: 0.75rem; /* Zwiększone z 0.72rem dla lepszej czytelności */
    text-transform: uppercase;
    letter-spacing: 0.02em;
    line-height: 1.5; /* Dodane dla wyrównania */
  }
  
  /* Actions column full width - lepsze spacing */
  .table tbody td:last-child {
    padding-left: 0;
    padding-top: 0.6rem; /* Zwiększone z 0.4rem */
    border-top: 1px solid var(--gray-200);
    margin-top: 0.5rem; /* Zwiększone z 0.3rem */
  }
  
  .table tbody td:last-child::before {
    content: '';
    display: none;
  }
  
  /* Button wrapping in action cells - reduced gap */
  .table tbody td:last-child {
    display: flex;
    flex-wrap: nowrap;
    gap: 0.2rem; /* Reduced gap */
    min-width: 100%;
    align-content: flex-start;
    overflow-x: auto; /* enable horizontal scroll if many actions */
    padding-bottom: 0.2rem;
  }
  
  .table tbody td:last-child .btn {
    flex-shrink: 0;
    white-space: nowrap;
  }
  
  /* =================================================================
     MOBILE: Uproszczony widok Users - tylko nazwa + imię/nazwisko
     Na mobile pokazujemy klikalne karty, szczegóły w modalu
     ================================================================= */
  /* Users: Ukryj Add User button na mobile */
  .page-header button[data-action="add"][data-type="users"] {
    display: none !important;
  }
  
  .table-users tbody tr {
    cursor: pointer;
    transition: background-color 0.2s;
  }
  
  .table-users tbody tr:hover {
    background-color: var(--gray-100);
  }
  
  /* Users: Ukryj kolumny: Group, Territories, Status na mobile */
  .table-users tbody td:nth-child(4), /* Group */
  .table-users tbody td:nth-child(5), /* Territories */
  .table-users tbody td:nth-child(6)  /* Status */
  {
    display: none !important;
  }
  
  /* Users: Akcje - tylko View i Edit, reszta ukryta */
  .table-users tbody td:last-child button[data-action="change-password"],
  .table-users tbody td:last-child button[data-action="delete"] {
    display: none !important;
  }
  
  /* Users: View button jako główny przycisk */
  .table-users tbody td:last-child button[data-action="view"] {
    flex: 1 !important;
    background: var(--primary-color) !important;
    color: white !important;
  }
  
  /* =================================================================
     MOBILE: Uproszczony widok Territories - tylko numer + nazwa
     ================================================================= */
  .table-territories tbody tr {
    cursor: pointer;
    transition: background-color 0.2s;
  }
  
  .table-territories tbody tr:hover {
    background-color: var(--gray-100);
  }
  
  /* Territories: Ukryj kolumny na mobile - pokazuj tylko Number, Name, Map Icon, Status, Assigned To */
  .table-territories thead th:nth-child(2), /* Note header */
  /* .table-territories thead th:nth-child(3), Map icon header - SHOW */
  .table-territories thead th:nth-child(4), /* Name - move to 4th position in markup but hide header */
  .table-territories thead th:nth-child(5), /* Flags header */
  .table-territories thead th:nth-child(6), /* Type header */
  .table-territories thead th:nth-child(8), /* Last Alloc header */
  .table-territories thead th:nth-child(9), /* Last Worked header */
  .table-territories thead th:nth-child(11) /* Worked Date header */
  {
    display: none !important;
  }
  
  .table-territories tbody td:nth-child(2), /* Note icon */
  /* .table-territories tbody td:nth-child(3), Map/File icon - SHOW on mobile for quick map access */
  /* .table-territories tbody td:nth-child(4), Name - SHOW on mobile */
  .table-territories tbody td:nth-child(5), /* Flags */
  .table-territories tbody td:nth-child(6), /* Type */
  /* .table-territories tbody td:nth-child(7),  Assigned To - SHOW on mobile */
  .table-territories tbody td:nth-child(8), /* Last Alloc */
  .table-territories tbody td:nth-child(9), /* Last Worked */
  /* .table-territories tbody td:nth-child(10),  Status - SHOW on mobile */
  /* .table-territories tbody td:nth-child(11) - Worked Date - SHOW on mobile */
  .table-territories tbody td:nth-child(12) /* Actions column (hidden separately below) */
  {
    display: none !important;
  }
  
  /* Territories: Akcje - tylko View i Allocate */
  .table-territories tbody td:last-child button[data-action="edit"],
  .table-territories tbody td:last-child button[data-action="delete"],
  .table-territories tbody td:last-child button[data-action="notes"],
  .table-territories tbody td:last-child button[data-action="view"],
  .table-territories tbody td:last-child button[data-action="allocate"],
  .table-territories tbody td:last-child button {
    display: none !important; /* On mobile, no action buttons in list view */
  }

  /* Hide the entire Actions column cell on mobile for Territories */
  .table-territories tbody td:last-child {
    display: none !important;
  }
  
  /* =================================================================
     MOBILE: History/Allocations - tylko Territory, Status i akcje
     ================================================================= */
  /* History: Ukryj wszystkie checkboxy (only mine, worked only, etc) */
  .page-header label.subnav-checkbox,
  .page-header input[type="checkbox"] {
    display: none !important;
  }
  
  .table-allocations tbody tr {
    cursor: pointer;
  }
  
  /* Allocations: Ukryj kolumny na mobile */
  .table-allocations tbody td:nth-child(2), /* Part */
  /* .table-allocations tbody td:nth-child(3),  Assigned To (SHOW on mobile) */
  /* .table-allocations tbody td:nth-child(4),  Description (SHOW on mobile) */
  .table-allocations tbody td:nth-child(5), /* Ref */
  .table-allocations tbody td:nth-child(6), /* Start Date */
  .table-allocations tbody td:nth-child(7), /* Worked Date */
  .table-allocations tbody td:nth-child(8)  /* End Date */
  {
    display: none !important;
  }
  
  /* Allocations (History): hide all action buttons and the Actions cell on mobile */
  .table-allocations tbody td:last-child button {
    display: none !important;
  }
  .table-allocations tbody td:last-child {
    display: none !important;
  }
  
  /* =================================================================
     MOBILE: My Territories - fix przyciski filter odcięte przez bar
     ================================================================= */
  .my-territories-container {
    padding-top: 1.75rem !important;
    margin-top: 0.75rem !important;
  }
  
  .my-territories-container .filter-bar {
    margin-top: 0.5rem !important;
    padding: 0.5rem !important;
  }
  
  /* =================================================================
     MOBILE: Personal Data - fix width (pełna szerokość)
     ================================================================= */
  .personal-data-container {
    width: 100% !important;
    max-width: 100% !important;
    padding: 0.5rem !important;
    box-sizing: border-box !important;
  }
  
  .personal-data-card,
  .stats-card {
    width: 100% !important;
    box-sizing: border-box !important;
  }
  
  .stats-grid {
    width: 100% !important;
    box-sizing: border-box !important;
  }

  /* Push content below subnav toolbar to prevent first row overlap */
  .content-area > .subnav-toolbar + .tab-content-wrapper .table-container {
    margin-top: 0.75rem !important;
  }

  /* Stronger override for Personal Data container when inline styles exist */
  #content-area .personal-data-container {
    width: 100% !important;
    max-width: 100% !important;
    margin: 0 !important;
  }
  
  /* Alert positioning - minimal margins */
  .alert-container {
    left: 0;
    right: 0;
    width: auto;
    margin: 0 0.25rem; /* Reduced margin */
  }
  
  .alert {
    font-size: 0.85rem;
    padding: 0.4rem; /* Further reduced padding */
  }
  
  /* Modal full width on small phones with smaller fonts */
  .modal-dialog {
    margin: 0.3rem; /* Reduced margin */
    width: calc(100% - 0.6rem); /* More space */
    max-height: calc(100vh - 1rem);
    font-size: 0.875rem; /* Smaller modal font */
  }
  
  .modal-title {
    font-size: 1rem; /* Smaller modal title */
  }
  
  .modal-body {
    max-height: calc(100vh - 150px);
    font-size: 0.875rem;
    padding: 0.5rem; /* Further reduced padding */
  }
  
  .modal-header {
    padding: 0.5rem; /* Reduced padding */
  }
  
  .modal-footer {
    padding: 0.5rem; /* Reduced padding */
    gap: 0.25rem; /* Reduced gap */
  }
  
  /* Form groups stack fully */
  .form-group > div[style*="flex"] {
    flex-direction: column !important;
  }
  
  .form-group > div[style*="flex"] > div {
    width: 100% !important;
  }
  
  /* =================================================================
     MOBILE: Map Viewer Optimizations
     ================================================================= */
  
  /* Make map icon more prominent and touch-friendly on mobile */
  .table-territories tbody td:nth-child(3) {
    font-size: 1.3em !important;
    padding: 0.5rem !important;
    text-align: center !important;
  }
  
  /* Map viewer modal - full screen on mobile */
  .modal:has(.map-viewer-container) .modal-dialog {
    margin: 0 !important;
    width: 100vw !important;
    max-width: 100vw !important;
    height: 100vh !important;
    max-height: 100vh !important;
  }
  
  .modal:has(.map-viewer-container) .modal-body {
    padding: 0 !important;
    max-height: calc(100vh - 3rem) !important;
  }
  
  .modal:has(.map-viewer-container) .modal-header {
    padding: 0.5rem !important;
    position: sticky;
    top: 0;
    z-index: 10;
    background: white;
    border-bottom: 2px solid var(--primary-color);
  }
  
  /* Map viewer container - full viewport */
  .map-viewer-container {
    height: calc(100vh - 4rem) !important;
    max-height: none !important;
  }
  
  /* Larger touch-friendly zoom controls */
  .map-controls {
    position: fixed !important;
    top: 3.5rem !important;
    right: 0.5rem !important;
    z-index: 1000 !important;
    display: flex !important;
    flex-direction: column !important;
    gap: 0.5rem !important;
    background: rgba(255, 255, 255, 0.95) !important;
    padding: 0.5rem !important;
    border-radius: 0.5rem !important;
    box-shadow: 0 2px 8px rgba(0,0,0,0.2) !important;
  }
  
  .map-controls button {
    min-width: 2.8rem !important;
    min-height: 2.8rem !important;
    font-size: 1.3rem !important;
    padding: 0.5rem !important;
    border: none !important;
    border-radius: 0.375rem !important;
    box-shadow: 0 1px 3px rgba(0,0,0,0.2) !important;
    touch-action: manipulation !important;
    -webkit-tap-highlight-color: transparent !important;
  }
  
  /* Make download/share button more visible on mobile */
  .map-controls .btn-success {
    background: linear-gradient(135deg, #10b981 0%, #059669 100%) !important;
    color: white !important;
    font-weight: 600 !important;
  }
  
  .map-controls button:active {
    transform: scale(0.95) !important;
  }
  
  /* Map files modal - better spacing for touch */
  .map-files-modal .map-file-card {
    padding: 1rem !important;
  }
  
  .map-files-modal button {
    min-height: 2.5rem !important;
    padding: 0.5rem 1rem !important;
    font-size: 0.95rem !important;
  }
  
  /* Mobile Floating Action Button for Full Map */
  .mobile-fab-full-map {
    display: block !important;
    position: fixed;
    bottom: 70px; /* Above navigation bar */
    right: 16px;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    border: none;
    background: linear-gradient(135deg, var(--info-color, #17a2b8) 0%, #0d8ca0 100%);
    color: white;
    font-size: 24px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3), 0 2px 4px rgba(0, 0, 0, 0.2);
    cursor: pointer;
    z-index: 998; /* Below modals (999) but above everything else */
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    align-items: center;
    justify-content: center;
    -webkit-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
  }
  
  .mobile-fab-full-map:active {
    transform: scale(0.92);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
  }
  
  .mobile-fab-full-map:hover {
    transform: scale(1.05);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.4);
  }
}

/* =================================================================
   STATISTICS GRID RESPONSIVE - Dashboard KPIs
   Adjust statistics layout for different screen sizes
   ================================================================= */
@media (max-width: 992px) {
  /* Stack statistics vertically on tablets */
  .page-header > div[style*="grid-template-columns:repeat(12,1fr)"] {
    grid-template-columns: 1fr !important;  /* Single column */
  }
  
  /* KPI rings - show 3 per row on tablets */
  .page-header > div[style*="grid-template-columns"] > div[style*="grid-column:1/7"] {
    grid-column: 1 / -1 !important;  /* Full width */
    display: grid !important;
    grid-template-columns: repeat(3, 1fr) !important;  /* 3 columns */
    gap: 1rem !important;
  }
  
  /* Activity bars - full width below KPIs */
  .page-header > div[style*="grid-template-columns"] > div[style*="grid-column:7/13"] {
    grid-column: 1 / -1 !important;
  }
}

@media (max-width: 600px) {
  /* KPI rings - show 2 per row on phones */
  .page-header > div[style*="grid-template-columns"] > div[style*="grid-column:1/7"] {
    grid-template-columns: repeat(2, 1fr) !important;  /* 2 columns */
  }
}

/* =================================================================
   PHONE LANDSCAPE ORIENTATION
   Optymalizacja dla telefonów w orientacji poziomej
   ================================================================= */
@media (max-width: 900px) and (max-height: 500px) and (orientation: landscape) {
  /* Zmniejsz wysokość top bar na landscape */
  .sidebar {
    height: 48px !important;
    min-height: 48px !important;
  }
  
  .main-content {
    padding-top: 48px !important;
  }
  
  /* Ustaw sticky subnav bez kolizji z górnym paskiem (48px) */
  .subnav-toolbar {
    position: sticky !important;
    top: 48px !important; /* Dokładnie pod paskiem aplikacji */
    padding: 0.35rem 0.5rem !important;
  }
  
  /* Kompaktowy nagłówek strony w landscape, żeby zminimalizować zajętą wysokość */
  .page-header {
    padding: 0.5rem 0.5rem !important;
    gap: 0.5rem !important;
  }
  
  /* Kompaktowe menu - tylko ikony, mniejsze */
  .nav-item {
    min-width: 48px !important;
    padding: 0.2rem !important;
  }
  
  .nav-item svg,
  .nav-item [aria-hidden] {
    width: 20px !important;
    height: 20px !important;
    margin: 0 !important;
  }
  
  /* Mniejszy logout button */
  .sidebar-footer #logout-btn {
    width: 40px !important;
    height: 40px !important;
    min-width: 40px !important;
  }
  
  /* Zmniejsz padding w kartach tabel dla landscape */
  .table tbody tr {
    padding: 0.5rem !important;
    margin-bottom: 0.4rem !important;
  }
  
  .table tbody td {
    padding: 0.25rem 0 !important;
    font-size: 0.8rem !important;
  }
  
  /* Kompaktowa paginacja na landscape */
  .pagination-controls {
    padding: 0.4rem !important;
  }
  
  .pagination-info {
    font-size: 0.75rem !important;
  }
  
  .pagination-buttons .btn,
  .pagination-buttons button {
    min-height: 36px !important;
    padding: 0.3rem 0.5rem !important;
    font-size: 0.75rem !important;
  }
  
  /* Modalne okna - mniejsze dla landscape */
  .modal-dialog {
    margin: 0.25rem !important;
    max-height: calc(100vh - 60px) !important;
  }
  
  .modal-body {
    max-height: calc(100vh - 140px) !important;
    padding: 0.4rem !important;
  }
  
  .modal-header,
  .modal-footer {
    padding: 0.4rem !important;
  }
  
  /* My Territories: zmniejsz górny odstęp, aby przyciski nie były ucięte */
  .my-territories-container {
    padding-top: 0.5rem !important;
    margin-top: 0.25rem !important;
  }
  
  /* Zmniejsz spacing w formularzach */
  .form-group {
    margin-bottom: 0.4rem !important;
  }
  
  .form-control {
    min-height: 36px !important;
    padding: 0.3rem 0.4rem !important;
    font-size: 0.85rem !important;
  }
  
  .form-label {
    font-size: 0.75rem !important;
    margin-bottom: 0.15rem !important;
  }
}

/* =================================================================
   PAGINATION CONTROLS - Removed duplicates (now in utilities.css)
   ================================================================= */

/* Desktop: compact header and stats cap */
@media (min-width: 992px) {
    .page-header { max-height: 15vh; overflow: hidden; }
    .page-header .stats-grid { max-height: 12vh; overflow: hidden; }
}

/* =================================================================
   DESKTOP DATA-GRID ENHANCEMENTS (≥ 1024px)
   Improves readability and usability on wide screens
   - Sticky first column
   - Sticky header (already) with subtle separator
   - Zebra rows and clearer hover
   - Fixed-layout table with safe ellipsis
   - Dedicated width for Actions column with pill buttons
   - Enable horizontal scroll in content wrapper
   ================================================================= */
@media (min-width: 1024px) {
  /* Make tab headers horizontal on desktop: title left, actions right */
  .page-header { 
    flex-direction: row; 
    align-items: center; 
    justify-content: flex-start; 
    gap: 0.75rem; 
  }
  .page-header .page-title { margin-right: auto; }
  .page-header .header-actions { margin-left: auto; display:flex; gap:.5rem; }
  /* Prevent header buttons from stretching */
  .page-header > .btn { width: auto !important; align-self: center !important; margin-left: auto; }
  /* Territories stats: compact scaling for better fit */
  .page-header .stats-grid { padding: 0.75rem !important; gap: 1rem !important; border-radius: 6px !important; }
  .page-header .stats-grid svg { width: 56px !important; height: 56px !important; }
  .page-header .stats-grid div[style*="height:12px"] { height: 8px !important; }
  .page-header .stats-grid .muted { font-size: 0.72rem !important; }
  /* Allow horizontal scroll for wide tables */
  .tab-content-wrapper { overflow-x: auto; }

  .table { 
    table-layout: fixed; /* predictable columns */
    font-size: 0.95rem; 
  }
  .table th,
  .table td { 
    white-space: normal;
    word-wrap: break-word;
    overflow-wrap: break-word;
  }
  /* Sticky header visual separator */
  .table thead th { 
    position: sticky; 
    top: 0; 
    z-index: 15; 
    background: var(--white); 
    box-shadow: inset 0 -1px 0 var(--gray-300);
  }
  /* Freeze first column for easier scanning */
  .table th:first-child,
  .table td:first-child { 
    position: sticky; 
    left: 0; 
    z-index: 16; 
    background: var(--white); 
    border-right: 1px solid var(--gray-300);
  }
  /* Territories: make first column (number) narrower ~50% of typical width */
  .table-territories .table th:first-child,
  .table-territories .table td:first-child {
    width: 60px; /* compact number column */
    min-width: 56px;
    max-width: 80px;
  }
  /* Give Actions column breathing room */
  .table thead th.col-actions,
  .table tbody td.col-actions { 
    width: 120px; 
    min-width: 100px; 
    max-width: 160px; 
  }
  /* Convert text action links into subtle pills when not using .btn */
  .table tbody td:last-child [data-action]:not(.btn) {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 0.25rem 0.5rem;
    border-radius: 9999px;
    background: var(--gray-100);
    border: 1px solid var(--gray-300);
    color: var(--gray-800);
    text-decoration: none;
    line-height: 1;
  }
  .table tbody td:last-child [data-action]:not(.btn):hover {
    background: #eef2f7;
    border-color: var(--gray-400);
  }
  /* Tight, consistent spacing between actions */
  .table tbody td:last-child {
    gap: 0.35rem;
    justify-content: flex-start;
  }
  /* High-contrast row hover and gentle zebra striping */
  .table tbody tr:nth-child(even) { background: #fafafa; }
  .table tbody tr:hover { background: #f1f5f9; }
}

/* Mobile: hide heavy stats grid entirely for space and performance */
@media (max-width: 600px) {
    .page-header .stats-grid { display: none !important; }
    .page-title { font-size: 1.1rem; }
}

/* =================================================================
   UTILITY CLASSES
   ================================================================= */

.w-auto { width: auto !important; }
.w-200 { width: 200px !important; }

.position-fixed { position: fixed !important; }
.top-0 { top: 0 !important; }
.end-0 { right: 0 !important; }
.p-3 { padding: 1rem !important; }
.z-1055 { z-index: 1055 !important; }

.gap-10 { gap: 10px !important; }
.gap-15 { gap: 15px !important; }
.mt-10 { margin-top: 10px !important; }
.mt-15 { margin-top: 15px !important; }

/* Console and Terminal */
.console-output {
    background: #000 !important;
    color: #0f0 !important;
    padding: 15px !important;
    font-family: 'Courier New', monospace !important;
    max-height: 300px !important;
    overflow-y: auto !important;
    border-radius: 4px;
    border: 1px solid #333;
}

.console-output::-webkit-scrollbar { width: 8px; }
.console-output::-webkit-scrollbar-track { background: #222; }
.console-output::-webkit-scrollbar-thumb { background: #0f0; border-radius: 4px; }
.console-output::-webkit-scrollbar-thumb:hover { background: #0c0; }

/* Button Groups */
.button-group {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    margin-top: 15px;
}

/* =================================================================
   ACCESSIBILITY - Removed duplicate (now in utilities.css)
   ================================================================= */

/* =================================================================
   ALERT CONTAINER - Removed duplicate (now in utilities.css)
   ================================================================= */

/* Action Buttons and Elements */
[data-action] {
    cursor: pointer;
    transition: all 0.2s ease;
}

[data-action]:hover {
    transform: translateY(-1px);
}

[data-action]:disabled,
[data-action].disabled {
    pointer-events: none;
    opacity: 0.5;
}

/* Toolbar */
.toolbar {
    display: flex;
    gap: 0.5rem;
    align-items: center;
    flex-wrap: wrap;
}

.toolbar select,
.toolbar input,
.toolbar button {
    margin: 0.2rem 0;
}

/* Status Badges - Text Only Style */
.badge,
.status-badge,
span[class*="badge"] {
    background: transparent !important;
    border: none !important;
    padding: 0 !important;
    font-weight: 600;
    font-size: 0.875rem;
}

.badge-success,
.badge.bg-success,
.status-active,
.status-available {
    color: #10b981 !important;
}

.badge-primary,
.badge.bg-primary,
.status-assigned {
    color: #3b82f6 !important;
}

.badge-warning,
.badge.bg-warning,
.status-pending {
    color: #f59e0b !important;
}

.badge-danger,
.badge.bg-danger,
.status-inactive,
.status-unavailable {
    color: #ef4444 !important;
}

.badge-secondary,
.badge.bg-secondary,
.status-archived {
    color: #6b7280 !important;
}

.badge-info,
.badge.bg-info,
.status-processed {
    color: #06b6d4 !important;
}

/* User Roles */
.role-admin {
    color: #dc2626 !important;
    font-weight: 600;
}

.role-user {
    color: #3b82f6 !important;
}

.role-member {
    color: #10b981 !important;
}

/* Territory Types */
.type-business {
    color: #8b5cf6 !important;
}

.type-residential {
    color: #06b6d4 !important;
}

.type-phone {
    color: #f59e0b !important;
}

.type-letter {
    color: #ec4899 !important;
}

/* =================================================================
   VISIBILITY HELPERS - Backward compatibility only
   ================================================================= */
.hidden { display: none !important; }
.visible { display: block !important; }

/* =================================================================
   DIAGNOSTIC STYLES - Testing and debugging utilities
   ================================================================= */
.diagnostic-section {
    border: 1px solid #dee2e6;
    border-radius: 8px;
    padding: 20px;
    margin-bottom: 20px;
    background: white;
}

.test-result {
    padding: 10px;
    margin: 5px 0;
    border-radius: 4px;
    font-family: monospace;
    font-size: 13px;
}

.test-result.success {
    background-color: #d4edda;
    color: #155724;
    border-left: 4px solid #28a745;
}

.test-result.error {
    background-color: #f8d7da;
    color: #721c24;
    border-left: 4px solid #dc3545;
}

.test-result.warning {
    background-color: #fff3cd;
    color: #856404;
    border-left: 4px solid #ffc107;
}

.test-result.info {
    background-color: #d1ecf1;
    color: #0c5460;
    border-left: 4px solid #17a2b8;
}

/* Debug Panel */
#tm-debug-panel {
    font-family: 'Courier New', monospace;
    line-height: 1.4;
}

#tm-debug-panel button {
    background: #007bff;
    color: white;
    border: none;
    padding: 4px 8px;
    border-radius: 3px;
    cursor: pointer;
    font-size: 11px;
}

#tm-debug-panel button:hover {
    background: #0056b3;
}

/* =================================================================
   FORCE VISIBILITY - Ensure critical elements are always visible
   Prevents layout issues from conflicting styles
   ================================================================= */
.nav-item,
[data-action],
.form-control {
    display: inline-block !important;
    visibility: visible !important;
}

/* Button visibility enforced in buttons.css to avoid conflicts */

/* =================================================================
   PERSONAL DATA TAB - Completely Rebuilt for Mobile
   Simple, clean layout that works reliably on iOS Safari
   ================================================================= */

.personal-data-wrapper {
    max-width: 100%;
    width: 100%;
    margin: 0;
    padding: 0.5rem;
    box-sizing: border-box;
}

.personal-data-section {
    background: #fff;
    border-radius: 6px;
    padding: 1rem;
    margin-bottom: 1rem;
    box-shadow: 0 1px 2px rgba(0,0,0,0.1);
}

.section-title {
    font-size: 1rem;
    font-weight: 700;
    color: #343a40;
    margin: 0 0 0.75rem 0;
    padding-bottom: 0.5rem;
    border-bottom: 2px solid #e9ecef;
}

/* Desktop view-field */
.personal-data-wrapper .view-field {
    display: block;
    padding: 0.75rem;
    margin-bottom: 0.5rem;
    background: #f8f9fa;
    border-left: 3px solid #007bff;
    border-radius: 4px;
}

.personal-data-wrapper .view-field label {
    display: block;
    font-size: 0.6875rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: #6c757d;
    margin-bottom: 0.25rem;
}

.personal-data-wrapper .view-field > div {
    display: block;
    font-size: 0.9375rem;
    font-weight: 600;
    color: #212529;
    word-wrap: break-word;
}

.personal-data-wrapper .view-field:last-child {
    margin-bottom: 0;
}

.stat-value-inline {
    font-size: 1.125rem;
    font-weight: 700;
    color: #007bff;
}

.badge {
    display: inline-block;
    padding: 0.25em 0.5em;
    font-size: 0.75rem;
    font-weight: 600;
    border-radius: 0.25rem;
}

.badge-success {
    background-color: #28a745;
    color: #fff;
}

.badge-secondary {
    background-color: #6c757d;
    color: #fff;
}

/* Mobile - Everything stacked, simple, clean */
@media (max-width: 768px) {
    .personal-data-wrapper {
        padding: 0.5rem;
    }
    
    .personal-data-section {
        padding: 0.875rem;
        margin-bottom: 0.75rem;
    }
    
    .section-title {
        font-size: 0.9375rem;
        margin-bottom: 0.625rem;
        padding-bottom: 0.375rem;
    }
    
    .personal-data-wrapper .view-field {
        padding: 0.625rem 0.75rem;
        margin-bottom: 0.5rem;
    }
    
    .personal-data-wrapper .view-field label {
        font-size: 0.625rem;
        margin-bottom: 0.25rem;
    }
    
    .personal-data-wrapper .view-field > div {
        font-size: 0.875rem;
    }
    
    .stat-value-inline {
        font-size: 1rem;
    }
}

/* Extra small mobile */
@media (max-width: 480px) {
    .personal-data-wrapper {
        padding: 0.375rem;
    }
    
    .personal-data-section {
        padding: 0.75rem;
    }
    
    .personal-data-wrapper .view-field {
        padding: 0.5rem 0.625rem;
    }
}
