/* System-wide UI polish layer.
   Loaded after page layout CSS to normalize spacing without disturbing map canvases. */

:root {
    --ui-bg: #f5f7fb;
    --ui-surface: #ffffff;
    --ui-surface-soft: #f8fafc;
    --ui-border: #e2e8f0;
    --ui-border-soft: #edf2f7;
    --ui-text: #1e293b;
    --ui-muted: #64748b;
    --ui-blue: #2563eb;
    --ui-green: #059669;
    --ui-amber: #d97706;
    --ui-red: #dc2626;
    --ui-shadow: 0 10px 28px rgba(15, 23, 42, 0.08);
    --ui-shadow-soft: 0 4px 14px rgba(15, 23, 42, 0.05);
    --ui-radius: 8px;
    --ui-radius-sm: 8px;
}

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

html,
body.app-shell {
    width: 100%;
    max-width: 100%;
}

body.app-shell {
    background: var(--ui-bg);
    color: var(--ui-text);
}

body.app-shell:not(.page-home):not(.page-map_all):not(.page-admin_replay):not(.page-replay):not(.page-report) .page-content {
    padding: 18px;
}

body.app-shell:not(.page-home):not(.page-map_all):not(.page-admin_replay):not(.page-replay):not(.page-report) .page-content > .main-page,
body.app-shell:not(.page-home):not(.page-map_all):not(.page-admin_replay):not(.page-replay):not(.page-report) .page-content > div[style*="min-height: 100vh"] {
    min-height: calc(100dvh - var(--header-height, 65px) - 32px - 36px) !important;
}

.app-shell .main-header {
    min-width: 0;
    border-bottom-color: rgba(226, 232, 240, 0.86) !important;
}

.app-shell .header-left,
.app-shell .user-profile,
.app-shell .vehicle-status-bar {
    min-width: 0;
}

.app-shell .brand-logo {
    min-width: 0;
}

.app-shell .brand-text,
.app-shell .user-name-text,
.app-shell .status-label,
.app-shell .sidebar-menu span {
    letter-spacing: 0;
}

.app-shell .page-content {
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
}

.app-shell .page-content::-webkit-scrollbar,
.app-shell .sidebar-menu::-webkit-scrollbar,
.app-shell .table-responsive::-webkit-scrollbar,
.app-shell .report-table-wrapper::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

.app-shell .page-content::-webkit-scrollbar-thumb,
.app-shell .sidebar-menu::-webkit-scrollbar-thumb,
.app-shell .table-responsive::-webkit-scrollbar-thumb,
.app-shell .report-table-wrapper::-webkit-scrollbar-thumb {
    background: rgba(148, 163, 184, 0.45);
    border-radius: 999px;
}

.app-shell .page-content::-webkit-scrollbar-track,
.app-shell .sidebar-menu::-webkit-scrollbar-track,
.app-shell .table-responsive::-webkit-scrollbar-track,
.app-shell .report-table-wrapper::-webkit-scrollbar-track {
    background: transparent;
}

/* Cards and page shells */
.app-shell .dash-card,
.app-shell .stat-card,
.app-shell .content-card,
.app-shell .table-section,
.app-shell .main-content-container,
.app-shell .report-card,
.app-shell .report-table-wrapper,
.app-shell .dataTables_wrapper,
.app-shell .card {
    border-radius: var(--ui-radius) !important;
}

.app-shell .dash-card,
.app-shell .stat-card,
.app-shell .content-card,
.app-shell .table-section,
.app-shell .main-content-container,
.app-shell .report-card {
    border: 1px solid var(--ui-border-soft) !important;
    box-shadow: var(--ui-shadow-soft) !important;
}

.app-shell .dashboard-header,
.app-shell .table-header,
.app-shell .card-header,
.app-shell .report-header {
    min-width: 0;
}

.app-shell .dashboard-header h1,
.app-shell .report-header h2,
.app-shell .header-title h1 {
    line-height: 1.2;
    letter-spacing: 0;
}

.app-shell .dashboard-header p,
.app-shell .header-title p,
.app-shell .report-header p {
    line-height: 1.45;
}

/* Form consistency */
.app-shell input:not([type="checkbox"]):not([type="radio"]):not([type="range"]),
.app-shell select,
.app-shell textarea,
.app-shell .custom-input,
.app-shell .custom-select,
.app-shell .form-control,
.app-shell .form-select {
    min-height: 38px;
    max-width: 100%;
    letter-spacing: 0 !important;
}

.app-shell textarea,
.app-shell textarea.custom-input {
    min-height: 92px;
    resize: vertical;
}

.app-shell .form-group {
    min-width: 0;
}

.app-shell label,
.app-shell .form-label {
    letter-spacing: 0 !important;
    line-height: 1.35;
}

/* Button safety: do not let the older global button rule resize icon controls. */
.app-shell .hamburger-btn,
.app-shell .topbar-notification,
.app-shell .logout-btn,
.app-shell .btn-close,
.app-shell .status-summary-btn,
.app-shell .layer-toggle-btn,
.app-shell .leaflet-control button,
.app-shell .leaflet-control a,
.app-shell button[class*="toggle"],
.app-shell button[class*="icon"] {
    box-shadow: none !important;
    letter-spacing: 0 !important;
}

.app-shell .leaflet-control button:hover,
.app-shell .leaflet-control a:hover,
.app-shell .hamburger-btn:hover,
.app-shell .topbar-notification:hover,
.app-shell .logout-btn:hover,
.app-shell .status-summary-btn:hover,
.app-shell .layer-toggle-btn:hover,
.app-shell button[class*="toggle"]:hover,
.app-shell button[class*="icon"]:hover {
    transform: none !important;
}

.app-shell .topbar-notification,
.app-shell .logout-btn,
.app-shell .hamburger-btn,
.app-shell .status-summary-btn,
.app-shell .layer-toggle-btn {
    padding: 0 !important;
}

.app-shell .topbar-notification,
.app-shell .logout-btn,
.app-shell .hamburger-btn {
    flex: 0 0 auto;
}

.app-shell .btn,
.app-shell .save-btn,
.app-shell .action-button,
.app-shell .report-btn,
.app-shell .btn-action,
.app-shell .icon-tool-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 36px;
    white-space: nowrap;
    text-decoration: none !important;
}

.app-shell .btn i,
.app-shell .save-btn i,
.app-shell .action-button i,
.app-shell .report-btn i,
.app-shell .btn-action i {
    flex: 0 0 auto;
}

/* Tables */
.app-shell .table-responsive,
.app-shell .report-table-wrapper,
.app-shell .dataTables_wrapper {
    max-width: 100%;
}

.app-shell .table-responsive,
.app-shell .report-table-wrapper {
    overflow-x: auto !important;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
}

.app-shell table {
    margin-bottom: 0;
}

.app-shell table th,
.app-shell table td {
    vertical-align: middle;
    line-height: 1.45;
    letter-spacing: 0;
}

.app-shell .dataTables_wrapper {
    padding: 0;
}

.app-shell .dataTables_wrapper .dataTables_length,
.app-shell .dataTables_wrapper .dataTables_filter {
    margin: 0 0 12px;
}

.app-shell .dataTables_wrapper .dataTables_filter input,
.app-shell .dataTables_wrapper .dataTables_length select {
    min-height: 34px;
    margin-left: 6px;
}

.app-shell .dataTables_wrapper .dataTables_info {
    color: var(--ui-muted) !important;
    font-size: 0.82rem;
    padding-top: 14px;
}

.app-shell .dataTables_wrapper .dataTables_paginate {
    padding-top: 10px;
}

.app-shell .dataTables_wrapper .dataTables_paginate .paginate_button {
    border-radius: 8px !important;
    border: 1px solid var(--ui-border) !important;
    background: var(--ui-surface) !important;
    color: var(--ui-text) !important;
    margin-left: 4px !important;
    padding: 5px 10px !important;
}

.app-shell .dataTables_wrapper .dataTables_paginate .paginate_button.current,
.app-shell .dataTables_wrapper .dataTables_paginate .paginate_button.current:hover {
    background: var(--ui-blue) !important;
    border-color: var(--ui-blue) !important;
    color: #ffffff !important;
}

/* Map/replay pages: preserve full bleed canvas and controls. */
.app-shell.page-home .page-content,
.app-shell.page-map_all .page-content,
.app-shell.page-admin_replay .page-content,
.app-shell.page-replay .page-content {
    padding: 0 !important;
    overflow: hidden !important;
}

/* Admin home is a dashboard, not a full-bleed map, so it must scroll. */
.app-shell.role-admin.page-home .page-content {
    overflow: auto !important;
}

.app-shell.page-home .app-footer,
.app-shell.page-map_all .app-footer,
.app-shell.page-admin_replay .app-footer,
.app-shell.page-replay .app-footer,
.app-shell.page-report .app-footer {
    display: none;
}

.app-shell.page-home .user-map-page,
.app-shell.page-map_all .card.p-0,
.app-shell.page-admin_replay .admin-replay-layout,
.app-shell.page-replay .user-replay-layout {
    min-height: calc(100dvh - var(--header-height, 65px)) !important;
}

.app-shell.page-map_all .page-content > div[style*="height: 100vh"],
.app-shell.page-map_all .card.p-0 {
    width: 100% !important;
    height: 100% !important;
    min-height: 100% !important;
    max-width: 100% !important;
}

.app-shell.page-map_all #big-map {
    width: 100% !important;
    height: 100% !important;
}

.app-shell.page-map_all #vehicle-popup {
    top: calc(var(--header-height, 65px) + 16px) !important;
    right: 16px !important;
    bottom: 16px !important;
    width: min(350px, calc(100vw - 32px)) !important;
    max-width: calc(100vw - 32px) !important;
    box-sizing: border-box !important;
}

.app-shell.page-map_all #vehicle-popup > *,
.app-shell.page-map_all #vehicle-search-panel,
.app-shell.page-map_all #vehicle-search-panel > div,
.app-shell.page-map_all #vehicle-search {
    min-width: 0 !important;
    max-width: 100% !important;
    box-sizing: border-box !important;
}

.app-shell.page-home .leaflet-container,
.app-shell.page-map_all .leaflet-container,
.app-shell.page-admin_replay .leaflet-container,
.app-shell.page-replay .leaflet-container {
    font-family: 'Kanit', sans-serif;
}

.app-shell.page-home .map-layer-controls,
.app-shell.page-map_all .map-layer-controls {
    max-width: calc(100vw - 24px);
    flex-wrap: nowrap;
}

/* Report shell polish */
.app-shell.page-report .report-shell,
.app-shell.page-report .user-report-shell {
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
    min-height: calc(100dvh - var(--header-height, 65px)) !important;
    max-height: calc(100dvh - var(--header-height, 65px)) !important;
    overflow-x: hidden !important;
}

.app-shell.page-report .page-content {
    padding: 0 !important;
    overflow: hidden !important;
}

.app-shell.page-report .report-sidebar {
    flex: 0 0 260px !important;
    min-width: 0 !important;
}

.app-shell .report-content,
.app-shell .report-main {
    min-width: 0 !important;
    max-width: 100% !important;
    overflow-x: hidden !important;
}

.app-shell .report-page {
    min-width: 0 !important;
    max-width: 100% !important;
}

.app-shell .report-page h1,
.app-shell .report-page h2,
.app-shell .report-page h3 {
    min-width: 0 !important;
    max-width: 100% !important;
    flex-wrap: wrap;
    overflow-wrap: anywhere;
}

.app-shell .report-filter,
.app-shell .report-controls,
.app-shell .filter-grid,
.app-shell .row-group {
    min-width: 0 !important;
    max-width: 100% !important;
}

.app-shell .report-date-presets,
.app-shell .report-export-group,
.app-shell .report-actions {
    min-width: 0 !important;
    max-width: 100% !important;
    flex-wrap: wrap;
}

.app-shell .report-card,
.app-shell .report-table-wrapper,
.app-shell .report-table,
.app-shell .dataTable {
    max-width: 100% !important;
}

.app-shell .report-card form,
.app-shell .report-card .row,
.app-shell .report-card [style*="display: flex"],
.app-shell .report-card [style*="display:flex"] {
    min-width: 0 !important;
    max-width: 100% !important;
}

.app-shell .report-card [style*="min-width"] {
    min-width: min(250px, 100%) !important;
}

/* Dark mode */
body.app-shell.dark-mode {
    --ui-bg: #0b1220;
    --ui-surface: #111c2e;
    --ui-surface-soft: #0f172a;
    --ui-border: #263449;
    --ui-border-soft: rgba(148, 163, 184, 0.16);
    --ui-text: #e2e8f0;
    --ui-muted: #94a3b8;
    --ui-shadow: 0 12px 34px rgba(0, 0, 0, 0.25);
    --ui-shadow-soft: 0 8px 20px rgba(0, 0, 0, 0.18);
    background: var(--ui-bg) !important;
}

body.app-shell.dark-mode .dash-card,
body.app-shell.dark-mode .stat-card,
body.app-shell.dark-mode .content-card,
body.app-shell.dark-mode .table-section,
body.app-shell.dark-mode .main-content-container,
body.app-shell.dark-mode .report-card {
    background: var(--ui-surface) !important;
    border-color: var(--ui-border-soft) !important;
}

body.app-shell.dark-mode .table-responsive,
body.app-shell.dark-mode .report-table-wrapper {
    border-color: var(--ui-border-soft) !important;
}

body.app-shell.dark-mode table th,
body.app-shell.dark-mode table td {
    border-color: var(--ui-border) !important;
}

body.app-shell.dark-mode .dataTables_wrapper .dataTables_paginate .paginate_button {
    background: #0f172a !important;
    border-color: #334155 !important;
    color: #cbd5e1 !important;
}

body.app-shell.dark-mode .dataTables_wrapper .dataTables_info,
body.app-shell.dark-mode .dataTables_wrapper .dataTables_length,
body.app-shell.dark-mode .dataTables_wrapper .dataTables_filter {
    color: #94a3b8 !important;
}

/* Responsive layout guard */
@media (max-width: 992px) {
    body.app-shell:not(.page-home):not(.page-map_all):not(.page-admin_replay):not(.page-replay):not(.page-report) .page-content {
        padding: 12px;
    }

    .app-shell .dashboard-header,
    .app-shell .table-header,
    .app-shell .report-header {
        flex-wrap: wrap;
        gap: 12px;
    }

    .app-shell .dashboard-header > *,
    .app-shell .table-header > *,
    .app-shell .report-header > * {
        min-width: 0;
    }

    .app-shell.page-report .report-sidebar {
        flex-basis: 230px !important;
    }
}

@media (max-width: 768px) {
    .app-shell .topbar-notification-panel {
        max-height: calc(100dvh - var(--header-height, 46px) - 16px);
    }

    .app-shell .btn,
    .app-shell .save-btn,
    .app-shell .action-button,
    .app-shell .report-btn,
    .app-shell .btn-action {
        min-height: 38px;
    }

    .app-shell .dataTables_wrapper .dataTables_length,
    .app-shell .dataTables_wrapper .dataTables_filter,
    .app-shell .dataTables_wrapper .dataTables_info,
    .app-shell .dataTables_wrapper .dataTables_paginate {
        float: none !important;
        text-align: left !important;
        width: 100%;
    }

    .app-shell .dataTables_wrapper .dataTables_filter input {
        width: 100%;
        margin: 6px 0 0;
    }

    .app-shell.page-report .report-shell,
    .app-shell.page-report .user-report-shell {
        overflow-x: hidden !important;
    }

    .app-shell.page-report .report-sidebar {
        flex: 0 0 auto !important;
        width: 100% !important;
        max-height: 42dvh !important;
    }

    .app-shell .report-page {
        padding: 16px !important;
    }
}
