:root {
    --primary-color: #fcd34d;
    /* light yellow */
    --secondary-color: #fbbf24;
    --text-color: #1e293b;
    --text-muted: #64748b;
    --bg-color: #f5f7fa;
    --card-bg: #ffffff;
    --border-color: #e8ecf1;
    --input-bg: #ffffff;
    --input-border: #d1d5db;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.04);
    --hover-shadow: 0 6px 12px rgba(252, 211, 77, 0.18);

    /* Safe area insets for iPhone notch/Dynamic Island */
    --safe-area-top: env(safe-area-inset-top);
    --safe-area-bottom: env(safe-area-inset-bottom);
    --safe-area-left: env(safe-area-inset-left);
    --safe-area-right: env(safe-area-inset-right);
}

body.dark-mode {
    --text-color: #f1f5f9;
    --text-muted: #94a3b8;
    --bg-color: #0f172a;
    --card-bg: #1e293b;
    --border-color: #334155;
    --input-bg: #334155;
    --input-border: #475569;
    --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.5);
    /* Retain primary/secondary or adjust if needed */
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    min-height: 100vh;
    /* Handle iPhone safe areas */
    padding-top: var(--safe-area-top);
    padding-bottom: var(--safe-area-bottom);
    padding-left: var(--safe-area-left);
    padding-right: var(--safe-area-right);
}

/* Auth Layout (Login/Register) */
body.auth-page {
    background: var(--bg-color);
    display: flex;
    justify-content: center;
    align-items: center;
}

.auth-container {
    background: var(--card-bg);
    padding: 2.5rem;
    border-radius: 16px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
    border: 1px solid var(--border-color);
    width: 100%;
    max-width: 400px;
}

/* Dashboard Layout */
.app-container {
    width: 100%;
    max-width: none;
    padding: 1rem 1.5rem;
    margin: 0;
}

.navbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.6rem 1.25rem;
    background: var(--card-bg);
    border-radius: 10px;
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow);
    margin-bottom: 1rem;
    width: 100%;
}

.logo h1 {
    font-size: 1.5rem;
    color: var(--primary-color);
    font-weight: 800;
}

.logo span {
    color: var(--secondary-color);
}

.user-menu {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.btn-logout {
    padding: 0.5rem 1rem;
    border-radius: 8px;
    background: var(--bg-color);
    color: var(--text-color);
    text-decoration: none;
    font-weight: 600;
    border: 1px solid var(--border-color);
    transition: all 0.2s;
}

.btn-logout:hover {
    background: #fde68a;
    box-shadow: 0 0 12px rgba(252, 211, 77, 0.35);
}

/* Module Grid */
.module-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1rem;
    width: 100%;
}

.module-card {
    background: var(--card-bg);
    border-radius: 10px;
    padding: 1.25rem;
    text-align: center;
    text-decoration: none;
    color: var(--text-color);
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow);
    transition: all 0.25s ease;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 140px;
}

.module-card:hover {
    transform: translateY(-3px);
    box-shadow: 0 0 0 2px rgba(252, 211, 77, 0.4), 0 8px 18px rgba(252, 211, 77, 0.18);
    border-color: var(--primary-color);
}

.module-icon {
    font-size: 1.8rem;
    margin-bottom: 0.6rem;
    color: var(--primary-color);
    text-shadow: 0 0 10px rgba(252, 211, 77, 0.35);
    width: 56px;
    height: 56px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    border: 2px solid rgba(252, 211, 77, 0.4);
    box-shadow: 0 0 12px rgba(252, 211, 77, 0.4),
        inset 0 0 8px rgba(252, 211, 77, 0.15);
    transition: all 0.3s ease;
}

.module-card:hover .module-icon {
    border-color: rgba(252, 211, 77, 0.8);
    box-shadow: 0 0 25px rgba(252, 211, 77, 0.7),
        0 0 35px rgba(252, 211, 77, 0.4),
        inset 0 0 15px rgba(252, 211, 77, 0.3);
}

.module-title {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--text-color);
}

/* Forms */
.form-group {
    margin-bottom: 1rem;
}

.form-group label {
    display: block;
    margin-bottom: 0.35rem;
    color: var(--text-muted);
    font-weight: 500;
    font-size: 0.85rem;
}

.form-group input {
    width: 100%;
    padding: 0.55rem 0.75rem;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    font-size: 0.9rem;
    transition: all 0.2s;
}

.form-group input:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.btn {
    width: 100%;
    padding: 0.6rem;
    background: var(--primary-color);
    color: white;
    border: none;
    border-radius: 8px;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.btn:hover {
    background: var(--secondary-color);
}

.link {
    text-align: center;
    margin-top: 1rem;
    font-size: 0.9rem;
    color: var(--text-muted);
}

.link a {
    color: var(--primary-color);
    text-decoration: none;
    font-weight: 600;
}

/* Flash Messages */
.flash-messages {
    margin-bottom: 1rem;
}

.flash-message {
    padding: 1rem;
    border-radius: 8px;
    margin-bottom: 0.5rem;
    text-align: center;
}

.flash-success {
    background-color: #f0fdf4;
    color: #22c55e;
}

.flash-error {
    background-color: #fef2f2;
    color: #ef4444;
}

/* Module Header */
.module-header {
    background: var(--card-bg);
    padding: 1rem 1.25rem;
    border-radius: 10px;
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow);
    margin-bottom: 1rem;
    display: flex;
    align-items: center;
    gap: 1rem;
}

.module-header h1 {
    font-size: 1.2rem;
    color: var(--text-color);
    display: flex;
    align-items: center;
    gap: 0.6rem;
}

.module-header h1 i {
    color: var(--primary-color);
}

.module-content {
    background: var(--card-bg);
    padding: 1rem 1.25rem;
    border-radius: 10px;
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow);
}

/* ============================================
   RESPONSIVE UTILITIES
   ============================================ */

/* Responsive Table Wrapper */
.table-responsive {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0;
    border-radius: 12px;
}

.table-responsive::-webkit-scrollbar {
    height: 8px;
}

.table-responsive::-webkit-scrollbar-track {
    background: var(--bg-color);
    border-radius: 4px;
}

.table-responsive::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 4px;
}

.table-responsive::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

/* Mobile Utility Classes */
.mobile-only {
    display: none;
}

.desktop-only {
    display: block;
}

.mobile-stack {
    display: flex;
    flex-direction: column;
}

.mobile-hide {
    display: block;
}

/* ============================================
   MOBILE & IPHONE OPTIMIZATION
   ============================================ */

/* Tablet Optimization (1024px and below) */
@media (max-width: 1024px) {
    .app-container {
        padding: 1rem;
    }

    .module-grid {
        grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    }

    .navbar {
        padding: 0.5rem 1rem;
    }
}

/* Mobile Landscape (768px and below) */
@media (max-width: 768px) {
    body {
        font-size: 13px;
    }

    .app-container {
        padding: 0.5rem 0.6rem;
        padding-top: max(0.5rem, var(--safe-area-top));
        padding-bottom: max(0.5rem, var(--safe-area-bottom));
    }

    .navbar {
        padding: 0.4rem 0.75rem;
        margin-bottom: 0.6rem;
    }

    .logo h1 {
        font-size: 1.1rem;
    }

    .module-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 0.6rem;
    }

    .module-card {
        height: 110px;
        padding: 0.75rem;
    }

    .module-icon {
        width: 42px;
        height: 42px;
        font-size: 1.4rem;
        margin-bottom: 0.4rem;
    }

    .module-title {
        font-size: 0.85rem;
    }

    /* Mobile utility classes */
    .mobile-only {
        display: block;
    }

    .desktop-only {
        display: none;
    }

    .mobile-hide {
        display: none !important;
    }

    /* Navigation improvements */
    .main-navbar {
        height: 48px;
        padding: 0 0.6rem;
        border-radius: 0 0 12px 12px;
    }

    .main-logo-text {
        font-size: 1.1rem !important;
    }

    .user-avatar,
    .theme-toggle-btn {
        width: 32px;
        height: 32px;
        font-size: 0.85rem;
    }

    .lang-toggle-btn {
        padding: 4px 8px;
        font-size: 0.8rem;
    }

    /* Modal improvements */
    .modal-content {
        width: 95%;
        max-width: 95%;
        margin: 0.5rem;
        max-height: 85vh;
        overflow-y: auto;
    }

    .modal-header {
        padding: 0.75rem;
    }

    .modal-content form {
        padding: 0.75rem;
    }

    /* Form improvements */
    .form-group {
        margin-bottom: 0.75rem;
    }

    .form-group input,
    .form-group select,
    .form-group textarea {
        padding: 0.6rem;
        font-size: 16px;
        /* Prevent iOS zoom */
    }

    /* Auth container */
    .auth-container {
        padding: 1.5rem 1rem;
        margin: 0.5rem;
    }
}

/* Mobile Portrait (480px and below) */
@media (max-width: 480px) {
    .app-container {
        padding: 0.35rem 0.5rem;
    }

    .navbar {
        padding: 0.3rem 0.5rem;
    }

    .module-card {
        height: 100px;
        padding: 0.6rem;
    }

    .module-icon {
        width: 38px;
        height: 38px;
        font-size: 1.2rem;
    }

    .module-title {
        font-size: 0.8rem;
    }

    /* Navigation */
    .main-navbar {
        height: 44px;
        padding: 0 0.5rem;
    }

    .main-navbar>div {
        gap: 0.5rem !important;
    }

    .main-logo-text {
        font-size: 1rem !important;
    }

    .user-avatar,
    .theme-toggle-btn {
        width: 28px;
        height: 28px;
        font-size: 0.8rem;
    }

    .lang-toggle-btn {
        padding: 3px 6px;
        font-size: 0.75rem;
    }

    .lang-text {
        min-width: 18px;
    }

    /* Modals */
    .modal-content {
        width: 100%;
        max-width: 100%;
        margin: 0;
        border-radius: 12px 12px 0 0;
        max-height: 90vh;
        overflow-y: auto;
    }

    .modal-header h3 {
        font-size: 1rem;
    }

    .modal-footer {
        flex-direction: column;
        gap: 0.5rem;
    }

    .modal-footer .btn-primary,
    .modal-footer .btn-secondary {
        width: 100%;
    }

    /* Auth page */
    .auth-container {
        padding: 1rem 0.75rem;
        margin: 0.25rem;
        border-radius: 12px;
    }

    /* Buttons */
    .btn {
        padding: 0.6rem;
        font-size: 0.9rem;
    }
}

/* Small Phones (375px and below - iPhone SE) */
@media (max-width: 375px) {
    body {
        font-size: 12px;
    }

    .app-container {
        padding: 0.25rem 0.4rem;
    }

    .main-navbar {
        height: 42px;
        padding: 0 0.4rem;
    }

    .main-navbar>div {
        gap: 0.35rem !important;
    }

    .main-logo-text {
        font-size: 0.9rem !important;
    }

    .user-avatar,
    .theme-toggle-btn {
        width: 26px;
        height: 26px;
        font-size: 0.75rem;
    }

    .module-card {
        height: 90px;
        padding: 0.5rem;
    }

    .module-icon {
        width: 34px;
        height: 34px;
        font-size: 1.1rem;
    }

    .module-title {
        font-size: 0.75rem;
    }
}

/* iPhone 14 Pro Specific (393 x 852 @3x) */
@media only screen and (device-width: 393px) and (device-height: 852px) and (-webkit-device-pixel-ratio: 3) {

    body {
        /* Extra padding for Dynamic Island */
        padding-top: max(env(safe-area-inset-top), 44px);
    }

    .app-container {
        padding: 0.75rem;
        padding-top: max(0.75rem, calc(env(safe-area-inset-top) + 8px));
        padding-bottom: max(0.75rem, calc(env(safe-area-inset-bottom) + 8px));
    }
}

/* iPhone in PWA standalone mode */
@media (display-mode: standalone) {
    body {
        /* Full bleed in standalone mode */
        background-color: var(--bg-color);
        padding-top: max(env(safe-area-inset-top), 20px);
        padding-bottom: max(env(safe-area-inset-bottom), 20px);
    }

    .navbar {
        margin-top: 0;
        border-radius: 0 0 12px 12px;
    }

    /* Fix for fixed positioned elements */
    .fixed-top {
        top: env(safe-area-inset-top);
    }

    .fixed-bottom {
        bottom: env(safe-area-inset-bottom);
    }
}

/* iPhone X, 11, 12, 13, 14 Series Safe Areas */
@supports (padding: max(0px)) {
    body {
        padding-left: min(env(safe-area-inset-left), 16px);
        padding-right: min(env(safe-area-inset-right), 16px);
    }

    .app-container {
        padding-left: max(1rem, env(safe-area-inset-left));
        padding-right: max(1rem, env(safe-area-inset-right));
    }
}

/* Touch-friendly buttons on mobile */
@media (max-width: 768px) {

    button,
    .btn,
    a.btn {
        min-height: 40px;
        /* Slightly under Apple's 44px for compact UI */
        padding: 0.5rem 1rem;
        font-size: 0.9rem;
    }

    input,
    select,
    textarea {
        min-height: 40px;
        font-size: 16px;
        /* Prevent zoom on iOS */
    }
}

/* Prevent iOS zoom on input focus */
@media screen and (max-width: 768px) {

    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="tel"],
    input[type="number"],
    select,
    textarea {
        font-size: 16px !important;
    }
}

/* Fix for iOS Safari bottom bar */
@supports (-webkit-touch-callout: none) {
    body {
        min-height: -webkit-fill-available;
    }

    .app-container {
        min-height: -webkit-fill-available;
    }
}

/* Vehicle Page Mobile Fixes - Breakpoint 780px */
@media (max-width: 780px) {
    .app-container {
        margin-top: 1rem !important;
        padding: 0 0.75rem !important;
    }

    /* Stack search and counts */
    div[style*="display: flex"][style*="align-items: flex-end"][style*="flex-wrap: wrap"] {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 0.75rem !important;
    }

    div[style*="min-width: 300px"] {
        min-width: 100% !important;
    }

    /* List layout for cards */
    .vehicle-card {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 0.25rem !important;
        padding: 0.5rem !important;
        background: var(--card-bg) !important;
        border-radius: 8px !important;
        border: 1px solid var(--border-color) !important;
    }

    /* Plate number section */
    .vehicle-card>div[style*="min-width: 120px"] {
        min-width: auto !important;
        padding-left: 0.25rem !important;
        border-bottom: 1px solid var(--border-color);
        padding-bottom: 0.25rem;
        margin-bottom: 0.25rem;
    }

    .vehicle-card div[style*="font-size: 1.1rem"] {
        font-size: 1rem !important;
    }

    /* Info container - row layout or compact stack */
    .vehicle-card>div[style*="display: flex"][style*="gap: 3rem"] {
        flex-direction: column !important;
        gap: 0.15rem !important;
        width: 100% !important;
    }

    .vehicle-card>div[style*="display: flex"]>div {
        min-width: auto !important;
        width: 100% !important;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0.1rem 0;
    }

    /* Ensure specific styling for labels and values to look like a list row */
    .vehicle-card div[style*="font-size: 0.7rem"],
    .vehicle-card div[style*="font-size: 0.75rem"] {
        margin-bottom: 0 !important;
        font-size: 0.75rem !important;
        opacity: 0.8;
    }

    .vehicle-card div[style*="font-size: 0.8rem"],
    .vehicle-card div[style*="font-size: 0.95rem"] {
        font-size: 0.85rem !important;
    }

    /* Force visibility of text */
    .vehicle-card,
    .vehicle-card * {
        color: var(--text-color) !important;
    }

    .vehicle-card .badge,
    .vehicle-card .badge * {
        color: inherit !important;
    }

    /* Custom badges to keep their colors but stay legible */
    .badge-success {
        color: #059669 !important;
        background: #ecfdf5 !important;
    }

    .badge-warning {
        color: #d97706 !important;
        background: #fffbeb !important;
    }

    .badge-danger {
        color: #dc2626 !important;
        background: #fef2f2 !important;
    }

    /* Disable transform and heavy gradients on mobile to avoid yellow box issues */
    .vehicle-card:hover {
        transform: none !important;
        background: var(--card-bg) !important;
        border-color: var(--primary-color) !important;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1) !important;
    }

    /* Modal improvements */
    #vehicleModal>div {
        width: 95% !important;
        padding: 1rem !important;
        margin: auto !important;
    }

    /* Saudi Plate in Modal */
    div[style*="width: 440px"][style*="height: 90px"] {
        width: 100% !important;
        max-width: 340px !important;
        min-height: 70px !important;
        height: auto !important;
    }

    div[style*="width: 90px"] img {
        width: 40px !important;
        height: 40px !important;
    }
}

/* ============================================
   iOS & ANDROID WEBVIEW OPTIMIZATIONS
   ============================================ */

/* Smooth scrolling everywhere */
* {
    -webkit-overflow-scrolling: touch;
}

/* Prevent rubber-banding on iOS while allowing scroll inside modals */
html, body {
    overscroll-behavior: none;
    -webkit-tap-highlight-color: transparent;
}

/* Prevent text selection on interactive elements (native app feel) */
button, .btn, a.btn, .tab-btn, .module-card, .nav-link {
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
}

/* Smooth transitions for all interactive elements */
a, button, input, select, textarea, .btn {
    -webkit-transition: all 0.2s ease;
    transition: all 0.2s ease;
}

/* Fix 300ms tap delay on mobile */
a, button, input, select, textarea {
    touch-action: manipulation;
}

/* Better scroll containers */
.notification-list,
.modal-content {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    scroll-behavior: smooth;
}

.table-responsive {
    -webkit-overflow-scrolling: touch;
    scroll-behavior: smooth;
    overscroll-behavior-x: contain;
}

/* Prevent body scroll when modal is open */
body.modal-open {
    overflow: hidden;
    position: fixed;
    width: 100%;
    height: 100%;
}

/* Global modal scroll fix - all modal patterns */
.modal.active,
.modal-container.active,
.modal-overlay.show,
.confirm-modal.show,
.confirm-modal.active {
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.modal.active .modal-content,
.modal-container.active .modal-content,
.modal-overlay.show .modal-content,
.confirm-modal.show .confirm-modal-content,
.confirm-modal.active .confirm-modal-content {
    max-height: 85vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}

/* Sticky modal headers inside scrollable modals */
.modal .modal-header,
.modal-container .modal-header,
.modal-overlay .modal-header {
    position: sticky;
    top: 0;
    z-index: 10;
    background: var(--card-bg);
}

/* Compact table cells globally */
.data-table th,
.data-table td,
.premium-table th,
.premium-table td {
    padding: 0.6rem 0.75rem;
    font-size: 0.85rem;
    line-height: 1.3;
}

.data-table th,
.premium-table th {
    font-size: 0.75rem;
    padding: 0.5rem 0.75rem;
}

/* Compact card content */
.card-header {
    padding: 0.75rem 1rem;
}

.card-header h3 {
    font-size: 1rem;
}

.card-header p {
    font-size: 0.78rem;
    margin-top: 0.15rem;
}

/* Compact stat cards */
.status-card,
.stat-card {
    padding: 0.6rem 0.75rem;
}

.status-count,
.stat-value {
    font-size: 1.2rem;
}

.status-label,
.stat-label {
    font-size: 0.7rem;
}

/* Compact badges */
.badge {
    padding: 0.15rem 0.5rem;
    font-size: 0.7rem;
}

/* Fix for Android WebView keyboard pushing layout */
@media screen and (max-height: 500px) {
    .modal-content {
        max-height: 95vh;
    }
    
    .main-navbar {
        height: 40px;
    }
}

/* WebView-specific: prevent double-tap zoom */
* {
    touch-action: manipulation;
}

/* Ensure modals work on small screens - bottom sheet style */
@media (max-width: 480px) {
    .modal-overlay.show,
    .modal.active,
    .modal-container.active {
        align-items: flex-end;
    }
    
    .modal-overlay.show .modal-content,
    .modal.active .modal-content,
    .modal-container.active .modal-content {
        border-radius: 14px 14px 0 0;
        max-height: 88vh;
        width: 100%;
        max-width: 100%;
        margin: 0;
        animation: modalSlideUp 0.25s ease;
    }
    
    @keyframes modalSlideUp {
        from {
            transform: translateY(100%);
        }
        to {
            transform: translateY(0);
        }
    }
}

/* Compact search boxes */
.search-box input,
.search-container input,
input[type="search"],
input[id*="Search"],
input[id*="search"] {
    padding: 0.45rem 0.75rem;
    font-size: 0.85rem;
}

/* Compact empty states */
div[style*="padding: 3rem"],
div[style*="padding: 4rem"] {
    padding: 1.5rem 1rem !important;
}

div[style*="font-size: 3rem"],
div[style*="font-size: 4rem"] {
    font-size: 2rem !important;
}