/* ==========================================
   CSS CUSTOM PROPERTIES — THEME SYSTEM
   ========================================== */
:root {
    /* Backgrounds */
    --bg-body: #f5f5f5;
    --bg-card: #ffffff;
    --bg-sidebar: #ffffff;
    --bg-input: #ffffff;
    --bg-hover: #ecf0f1;
    --bg-table-header: #f8f9fa;
    --bg-code: #f0f0f0;
    --bg-category: #f8f9fa;
    --bg-map: #ecf0f1;
    --bg-legend: #f8f9fa;
    --bg-tab: #ecf0f1;
    --bg-tab-hover: #d5dbdb;
    --bg-timeline-hover: #f8f9fa;
    --bg-timeline-active: #eef5ff;
    --bg-summary: #f0f8ff;
    --bg-warning-banner: #fff3cd;

    /* Text */
    --text-primary: #333333;
    --text-secondary: #555555;
    --text-muted: #666666;
    --text-hint: #888888;
    --text-faint: #999999;
    --text-heading: #2c3e50;
    --text-category: #7f8c8d;
    --text-sidebar-item: #34495e;

    /* Borders */
    --border-color: #dddddd;
    --border-light: #eeeeee;
    --border-category: #ecf0f1;
    --border-timeline: #dee2e6;

    /* Shadows */
    --shadow-card: 0 2px 4px rgba(0, 0, 0, 0.1);
    --shadow-login: 0 2px 10px rgba(0, 0, 0, 0.1);
    --shadow-sidebar: 4px 0 20px rgba(0, 0, 0, 0.15);
    --shadow-overlay: rgba(0, 0, 0, 0.4);

    /* Accent */
    --color-primary: #3498db;
    --color-primary-hover: #2980b9;
    --color-primary-light: rgba(52, 152, 219, 0.1);

    /* Navbar */
    --bg-navbar: #2c3e50;
    --text-navbar: #ffffff;

    /* Leaflet */
    --map-filter: grayscale(100%);

    /* Popup */
    --bg-popup: #ffffff;
    --text-popup: #2c3e50;
}

/* ==========================================
   DARK THEME
   ========================================== */
[data-theme="dark"] {
    --bg-body: #1a1a2e;
    --bg-card: #16213e;
    --bg-sidebar: #121a30;
    --bg-input: #1a1a40;
    --bg-hover: #1a1a40;
    --bg-table-header: #1a1a40;
    --bg-code: #1a1a40;
    --bg-category: #1a2744;
    --bg-map: #16213e;
    --bg-legend: #1a1a40;
    --bg-tab: #1a1a40;
    --bg-tab-hover: #0f3460;
    --bg-timeline-hover: #1a1a40;
    --bg-timeline-active: #1a2a4e;
    --bg-summary: #1a2a4e;
    --bg-warning-banner: #3a2e00;

    --text-primary: #e0e0e0;
    --text-secondary: #b0b0b0;
    --text-muted: #9e9e9e;
    --text-hint: #888888;
    --text-faint: #777777;
    --text-heading: #e8eaf6;
    --text-category: #90a4ae;
    --text-sidebar-item: #b0bec5;

    --border-color: #2a3a5e;
    --border-light: #1a2a4e;
    --border-category: #1a2a4e;
    --border-timeline: #2a3a5e;

    --shadow-card: 0 2px 4px rgba(0, 0, 0, 0.3);
    --shadow-login: 0 2px 10px rgba(0, 0, 0, 0.4);
    --shadow-sidebar: 4px 0 20px rgba(0, 0, 0, 0.4);
    --shadow-overlay: rgba(0, 0, 0, 0.6);

    --color-primary: #5dade2;
    --color-primary-hover: #3498db;
    --color-primary-light: rgba(93, 173, 226, 0.15);

    --bg-navbar: #1a2744;
    --text-navbar: #e0e0e0;

    --map-filter: grayscale(100%) invert(1) contrast(0.9) hue-rotate(180deg);

    --bg-popup: #16213e;
    --text-popup: #e8eaf6;
}

/* Dark theme — status badge adjustments */
[data-theme="dark"] .status-active {
    background-color: #1b4332;
    color: #95d5b2;
}
[data-theme="dark"] .status-inactive {
    background-color: #2b2d31;
    color: #9ca3af;
}
[data-theme="dark"] .status-offline {
    background-color: #3c1518;
    color: #f4a0a0;
}
[data-theme="dark"] .status-warning {
    background-color: #3a2e00;
    color: #ffd95a;
}
[data-theme="dark"] .severity-critical {
    background-color: #3c1518;
    color: #f4a0a0;
}
[data-theme="dark"] .severity-high {
    background-color: #3a2e00;
    color: #ffd95a;
}
[data-theme="dark"] .severity-medium {
    background-color: #0a2a3a;
    color: #7ec8e3;
}
[data-theme="dark"] .severity-low {
    background-color: #1b4332;
    color: #95d5b2;
}
[data-theme="dark"] .success-message {
    background-color: #1b4332;
    color: #95d5b2;
    border-color: #2d6a4f;
}
[data-theme="dark"] .error-message {
    color: #f4a0a0;
}

/* Dark theme — Leaflet popups */
[data-theme="dark"] .leaflet-popup-content-wrapper {
    background: var(--bg-popup);
    color: var(--text-primary);
}
[data-theme="dark"] .leaflet-popup-tip {
    background: var(--bg-popup);
}

/* Dark theme — inline style color overrides for common JS patterns */
[data-theme="dark"] .card [style*="color:#666"],
[data-theme="dark"] .card [style*="color: #666"] {
    color: var(--text-muted) !important;
}
[data-theme="dark"] .card [style*="color:#555"],
[data-theme="dark"] .card [style*="color: #555"] {
    color: var(--text-secondary) !important;
}
[data-theme="dark"] .card [style*="color:#888"],
[data-theme="dark"] .card [style*="color: #888"] {
    color: var(--text-hint) !important;
}
[data-theme="dark"] .card [style*="color:#999"],
[data-theme="dark"] .card [style*="color: #999"] {
    color: var(--text-faint) !important;
}
[data-theme="dark"] .card [style*="background:#f0f0f0"],
[data-theme="dark"] .card [style*="background: #f0f0f0"] {
    background: var(--bg-code) !important;
}
[data-theme="dark"] .card [style*="background:#f8f9fa"],
[data-theme="dark"] .card [style*="background: #f8f9fa"] {
    background: var(--bg-table-header) !important;
}
[data-theme="dark"] .card [style*="background:#f0f8ff"],
[data-theme="dark"] .card [style*="background: #f0f8ff"] {
    background: var(--bg-summary) !important;
}
[data-theme="dark"] .card [style*="background:#fff3cd"],
[data-theme="dark"] .card [style*="background: #fff3cd"] {
    background: var(--bg-warning-banner) !important;
}
[data-theme="dark"] .card [style*="color:#856404"],
[data-theme="dark"] .card [style*="color: #856404"] {
    color: #ffd95a !important;
}
[data-theme="dark"] .card [style*="border:1px solid #ddd"],
[data-theme="dark"] .card [style*="border: 1px solid #ddd"] {
    border-color: var(--border-color) !important;
}
[data-theme="dark"] .card [style*="border:1px solid #ffc107"],
[data-theme="dark"] .card [style*="border: 1px solid #ffc107"] {
    border-color: #806600 !important;
}
[data-theme="dark"] .card code[style*="background:#f0f0f0"],
[data-theme="dark"] .card code[style*="background: #f0f0f0"] {
    background: var(--bg-code) !important;
    color: var(--text-primary) !important;
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background-color: var(--bg-body);
    color: var(--text-primary);
    transition: background-color 0.3s, color 0.3s;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
}

#login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
}

.login-box {
    background: var(--bg-card);
    padding: 40px;
    border-radius: 8px;
    box-shadow: var(--shadow-login);
    width: 100%;
    max-width: 400px;
}

.login-box h1 {
    color: var(--text-heading);
    margin-bottom: 10px;
    text-align: center;
}

.subtitle {
    text-align: center;
    color: var(--text-category);
    margin-bottom: 20px;
    font-size: 14px;
}

.login-tabs {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
}

.tab-btn {
    flex: 1;
    padding: 10px;
    background-color: var(--bg-tab);
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    color: var(--text-primary);
    transition: background-color 0.3s;
}

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

.tab-btn.active {
    background-color: var(--color-primary);
    color: white;
}

.tab-content {
    display: none;
}

.tab-content.active {
    display: block;
}

.help-text {
    font-size: 12px;
    color: var(--text-category);
    margin-top: 5px;
}

.form-group {
    margin-bottom: 20px;
}

.form-group label {
    display: block;
    margin-bottom: 5px;
    color: var(--text-secondary);
    font-weight: 500;
}

.form-group input {
    width: 100%;
    padding: 10px;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    font-size: 14px;
    background-color: var(--bg-input);
    color: var(--text-primary);
}

.form-group input:focus {
    outline: none;
    border-color: var(--color-primary);
}

.btn {
    padding: 10px 20px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    transition: background-color 0.3s;
}

.btn-primary {
    background-color: var(--color-primary);
    color: white;
    width: 100%;
}

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

.btn-secondary {
    background-color: #95a5a6;
    color: white;
}

.btn-secondary:hover {
    background-color: #7f8c8d;
}

.btn-success {
    background-color: #27ae60;
    color: white;
}

.btn-success:hover {
    background-color: #219a52;
}

.btn-warning {
    background-color: #f39c12;
    color: white;
}

.btn-warning:hover {
    background-color: #d68910;
}

.btn-danger {
    background-color: #e74c3c;
    color: white;
}

.btn-danger:hover {
    background-color: #c0392b;
}

.error-message {
    color: #e74c3c;
    font-size: 14px;
    margin-top: 10px;
    text-align: center;
}

.navbar {
    background-color: var(--bg-navbar);
    color: var(--text-navbar);
    padding: 15px 30px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.navbar-left {
    display: flex;
    align-items: center;
    gap: 10px;
}

.navbar-brand {
    font-size: 20px;
    font-weight: bold;
}

.navbar-user {
    display: flex;
    align-items: center;
    gap: 15px;
}

.badge {
    background-color: var(--color-primary);
    color: #ffffff;
    padding: 4px 12px;
    border-radius: 12px;
    font-size: 12px;
    font-weight: 500;
}

/* Badge status variants — saturated colors that stay legible in both themes */
.badge-primary { background-color: var(--color-primary); color: #ffffff; }
.badge-success { background-color: #27ae60; color: #ffffff; }
.badge-warning { background-color: #f39c12; color: #ffffff; }
.badge-danger  { background-color: #e74c3c; color: #ffffff; }

.main-content {
    display: flex;
    min-height: calc(100vh - 60px);
}

.sidebar {
    width: 250px;
    background-color: var(--bg-sidebar);
    border-right: 1px solid var(--border-color);
    padding: 10px 0;
    overflow-y: auto;
}

.sidebar ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

.sidebar > ul > li {
    padding: 12px 20px;
    cursor: pointer;
    transition: background-color 0.3s;
    color: var(--text-primary);
}

.sidebar > ul > li:hover {
    background-color: var(--bg-hover);
}

.sidebar > ul > li.active {
    background-color: var(--color-primary);
    color: white;
}

/* Category headers */
.sidebar .menu-category-header {
    padding: 10px 16px;
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-category);
    background-color: var(--bg-category);
    border-top: 1px solid var(--border-category);
    border-bottom: 1px solid var(--border-category);
    cursor: pointer;
    display: flex;
    align-items: center;
    user-select: none;
    transition: background-color 0.2s;
    margin-top: 4px;
}

.sidebar .menu-category-header:first-child {
    margin-top: 0;
    border-top: none;
}

.sidebar .menu-category-header:hover {
    background-color: var(--bg-hover);
    color: var(--text-heading);
}

.sidebar .menu-category-header.collapsed {
    border-bottom-color: transparent;
}

.sidebar .menu-category-icon {
    margin-right: 6px;
    font-size: 13px;
}

.sidebar .menu-category-arrow {
    margin-left: auto;
    font-size: 9px;
    transition: transform 0.2s;
    color: #bdc3c7;
}

/* Category sub-items */
.sidebar .menu-category-items {
    list-style: none;
    overflow: hidden;
    max-height: 600px;
    transition: max-height 0.3s ease, opacity 0.2s ease;
    opacity: 1;
}

.sidebar .menu-category-items.collapsed {
    max-height: 0;
    opacity: 0;
}

.sidebar .menu-category-items li {
    padding: 10px 20px 10px 36px;
    cursor: pointer;
    font-size: 13px;
    color: var(--text-sidebar-item);
    transition: background-color 0.2s, color 0.2s;
    border-left: 3px solid transparent;
}

.sidebar .menu-category-items li:hover {
    background-color: var(--bg-hover);
    border-left-color: var(--color-primary);
}

.sidebar .menu-category-items li.active {
    background-color: var(--color-primary);
    color: white;
    border-left-color: var(--color-primary-hover);
}

.content {
    flex: 1;
    padding: 30px;
    overflow-y: auto;
}

.dashboard-header {
    margin-bottom: 30px;
}

.dashboard-header h2 {
    color: var(--text-heading);
    margin-bottom: 10px;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 16px;
    margin-bottom: 24px;
}

.stat-card {
    background: var(--bg-card);
    padding: 16px 20px;
    border-radius: 8px;
    box-shadow: var(--shadow-card);
    display: flex;
    align-items: center;
    gap: 14px;
    border-left: 4px solid var(--border-color);
    transition: box-shadow 0.15s;
}
.stat-card:hover { box-shadow: 0 4px 12px rgba(0,0,0,0.12); }

/* colour variants */
.stat-card--green  { border-left-color: #27ae60; }
.stat-card--blue   { border-left-color: #3498db; }
.stat-card--red    { border-left-color: #e74c3c; }
.stat-card--orange { border-left-color: #e67e22; }
.stat-card--purple { border-left-color: #9b59b6; }
.stat-card--gray   { border-left-color: #95a5a6; }
.stat-card--teal   { border-left-color: #16a085; }

.stat-icon { font-size: 26px; line-height: 1; opacity: 0.8; flex-shrink: 0; }

.stat-body { flex: 1; min-width: 0; }

.stat-label {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.6px;
    color: var(--text-muted);
    margin-bottom: 3px;
}

.stat-val {
    font-size: 28px;
    font-weight: 700;
    color: var(--text-heading);
    line-height: 1.1;
}

.stat-sub {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 2px;
}

/* Legacy stat-card selectors — kept for compatibility */
.stat-card h3 {
    color: var(--text-category);
    font-size: 14px;
    font-weight: normal;
    margin-bottom: 10px;
}
.stat-card .value {
    font-size: 32px;
    font-weight: bold;
    color: var(--text-heading);
}

/* ── Dashboard split layout ── */
.db-split {
    display: flex;
    gap: 20px;
    flex-wrap: wrap;
    align-items: flex-start;
}
.db-split-main { flex: 2; min-width: 300px; }
.db-split-side { flex: 1; min-width: 260px; max-width: 400px; }

.db-map-container {
    height: 360px;
    border-radius: 6px;
    overflow: hidden;
    background: var(--bg-map);
}

/* ── Events feed ── */
.ev-feed { display: flex; flex-direction: column; }

.ev-feed-item {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    padding: 9px 0;
    border-bottom: 1px solid var(--border-light);
}
.ev-feed-item:last-child { border-bottom: none; }

.ev-feed-dot {
    width: 9px; height: 9px;
    border-radius: 50%;
    flex-shrink: 0;
    margin-top: 4px;
}
.ev-dot--critical { background: #e74c3c; }
.ev-dot--high     { background: #e67e22; }
.ev-dot--medium   { background: #f39c12; }
.ev-dot--low      { background: #27ae60; }
.ev-dot--info     { background: #3498db; }

.ev-feed-body { flex: 1; min-width: 0; }

.ev-feed-title {
    font-size: 13px;
    font-weight: 500;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.ev-feed-meta {
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 2px;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 3px;
}

@media (max-width: 768px) {
    .db-split-side { max-width: 100%; }
    .db-map-container { height: 260px; }
}

.card {
    background: var(--bg-card);
    padding: 20px;
    border-radius: 8px;
    box-shadow: var(--shadow-card);
    margin-bottom: 20px;
}

.card h3 {
    color: var(--text-heading);
    margin-bottom: 15px;
}

.table {
    width: 100%;
    border-collapse: collapse;
}

.table th,
.table td {
    padding: 12px;
    text-align: left;
    border-bottom: 1px solid var(--border-color);
}

.table th {
    background-color: var(--bg-table-header);
    font-weight: 600;
    color: var(--text-secondary);
}

.table td {
    color: var(--text-primary);
}

.status-badge {
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 12px;
    font-weight: 500;
}

.status-active {
    background-color: #d4edda;
    color: #155724;
}

.status-inactive {
    background-color: #e9ecef;
    color: #495057;
}

.status-offline {
    background-color: #f8d7da;
    color: #721c24;
}

.status-warning {
    background-color: #fff3cd;
    color: #856404;
}

.severity-critical {
    background-color: #f8d7da;
    color: #721c24;
}

.severity-high {
    background-color: #fff3cd;
    color: #856404;
}

.severity-medium {
    background-color: #d1ecf1;
    color: #0c5460;
}

.severity-low {
    background-color: #d4edda;
    color: #155724;
}

.loading {
    text-align: center;
    padding: 40px;
    color: var(--text-category);
}

.empty-state {
    text-align: center;
    padding: 40px;
    color: var(--text-category);
}

.map-container {
    height: 600px;
    background-color: var(--bg-map);
    border-radius: 8px;
    position: relative;
    z-index: 1;
}

#worksite-map {
    width: 100%;
    height: 100%;
    border-radius: 8px;
}

.map-legend {
    margin-top: 20px;
    padding: 15px;
    background-color: var(--bg-legend);
    border-radius: 4px;
}

.map-legend h4 {
    color: var(--text-heading);
    font-size: 14px;
    margin-bottom: 10px;
}

.legend-item {
    display: flex;
    align-items: center;
    margin-bottom: 8px;
    font-size: 13px;
    color: var(--text-secondary);
}

.legend-marker {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    margin-right: 8px;
    display: inline-block;
}

.legend-marker.worksite {
    background-color: #e74c3c;
    border: 2px solid #c0392b;
}

.legend-marker.personnel {
    background-color: #2196F3;
    border: 2px solid #1976D2;
}

.legend-marker.device {
    background-color: #4CAF50;
    border: 2px solid #388E3C;
}

.legend-marker.asset {
    background-color: #FF9800;
    border: 2px solid #F57C00;
}

.legend-marker.worker {
    background-color: #2196F3;
    border: 2px solid #1976D2;
}

.legend-marker.perimeter {
    background-color: transparent;
    border: 2px solid #ff7800;
}

.legend-marker.weather {
    background-color: #3498db;
    border: 2px solid #2980b9;
}

.legend-marker.ble-tag {
    background-color: #8e44ad;
    border: 2px solid #6c3483;
}

/* ==========================================
   PRIVACY OBFUSCATION (worker data)
   ========================================== */
.privacy-banner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
    padding: 10px 16px;
    border-radius: 8px;
    margin-bottom: 16px;
    font-size: 14px;
}
.privacy-banner--locked {
    background: var(--bg-card, #fff);
    border: 1px solid #e0c36a;
    background-color: #fff8e1;
    color: #7a5c00;
}
.privacy-banner--open {
    background-color: #e8f5e9;
    border: 1px solid #a5d6a7;
    color: #1b5e20;
}
[data-theme="dark"] .privacy-banner--locked {
    background-color: #3a2e00;
    border-color: #6b5800;
    color: #ffd95a;
}
[data-theme="dark"] .privacy-banner--open {
    background-color: #1b4332;
    border-color: #2d6a4f;
    color: #95d5b2;
}

.privacy-pill {
    display: inline-block;
    padding: 2px 9px;
    border-radius: 12px;
    font-size: 12px;
    font-weight: 600;
}
.privacy-pill--locked { background: #fff3cd; color: #856404; }
.privacy-pill--open   { background: #d4edda; color: #155724; }

.privacy-modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10000;
}
.privacy-modal {
    background: var(--bg-card, #fff);
    color: var(--text-primary, #222);
    border-radius: 10px;
    padding: 24px;
    width: 90%;
    max-width: 420px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
}
.privacy-modal h3 { color: var(--text-heading, #222); }

/* ==========================================
   ADVANCED PERMISSIONS MATRIX (user create/edit)
   ========================================== */
.adv-perm-card {
    margin-top: 16px;
    border-left: 4px solid var(--color-primary, #3498db);
}
.adv-perm-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    cursor: pointer;
    user-select: none;
}
.adv-perm-hint {
    color: var(--text-muted, #888);
    font-size: 13px;
    margin: 4px 0 0;
}
.adv-perm-card table.table th,
.adv-perm-card table.table td {
    vertical-align: middle;
}
.adv-perm-card input[type="checkbox"] {
    width: 17px;
    height: 17px;
    cursor: pointer;
}
.adv-perm-card input[type="checkbox"]:disabled {
    cursor: not-allowed;
    opacity: 0.4;
}
.adv-expand {
    display: inline-block;
    width: 16px;
    cursor: pointer;
    color: var(--text-muted, #888);
    font-size: 11px;
    user-select: none;
}
.adv-children-wrap > td { background: var(--bg-subtle, rgba(0,0,0,0.02)); }
.adv-child td { border-top: none; font-size: 13px; }

/* BLE tag markers on the map */
.ble-tag-marker {
    background-color: transparent;
    border: none;
}

.ble-tag-marker .ble-dot {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: #8e44ad;
    border: 2px solid #ffffff;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 11px;
    line-height: 1;
}

/* "Too far" BLE tag: amber, dashed border, weaker signal */
.ble-tag-marker.too-far .ble-dot {
    background: #e67e22;
    border-style: dashed;
    opacity: 0.85;
}

/* Weather markers on map */
.weather-marker,
.weather-marker-mini {
    background: transparent !important;
    border: none !important;
}

/* Weather alert banner */
#weather-alert-banner {
    margin-bottom: 10px;
}

#weather-alert-banner > div {
    animation: weatherAlertFadeIn 0.3s ease-in;
}

@keyframes weatherAlertFadeIn {
    from { opacity: 0; transform: translateY(-5px); }
    to { opacity: 1; transform: translateY(0); }
}

.custom-marker {
    background-color: transparent;
    border: none;
}

.custom-marker .marker-pin {
    width: 0;
    height: 0;
    border-left: 8px solid transparent;
    border-right: 8px solid transparent;
    position: relative;
}

.worksite-marker .marker-pin {
    border-bottom: 24px solid #e74c3c;
}

.worksite-marker .marker-pin::before {
    content: '';
    position: absolute;
    width: 12px;
    height: 12px;
    background-color: #e74c3c;
    border-radius: 50%;
    top: 24px;
    left: -6px;
    border: 2px solid white;
}

.worksite-marker .marker-label {
    position: absolute;
    top: -20px;
    left: 50%;
    transform: translateX(-50%);
    background-color: var(--bg-card);
    padding: 2px 6px;
    border-radius: 3px;
    font-size: 11px;
    font-weight: bold;
    color: #e74c3c;
    white-space: nowrap;
    box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}

.personnel-marker .marker-pin,
.worker-marker .marker-pin {
    border-bottom: 20px solid #2196F3;
}

.personnel-marker .marker-pin::before,
.worker-marker .marker-pin::before {
    content: '';
    position: absolute;
    width: 8px;
    height: 8px;
    background-color: #2196F3;
    border-radius: 50%;
    top: 20px;
    left: -4px;
    border: 2px solid white;
}

.worker-marker.outside .marker-pin {
    border-bottom: 20px solid #dc3545;
}

.worker-marker.outside .marker-pin::before {
    background-color: #dc3545;
}

.device-marker .marker-pin {
    border-bottom: 16px solid #4CAF50;
}

.device-marker .marker-pin::before {
    content: '';
    position: absolute;
    width: 6px;
    height: 6px;
    background-color: #4CAF50;
    border-radius: 50%;
    top: 16px;
    left: -3px;
    border: 2px solid white;
}

.asset-marker .marker-pin {
    border-bottom: 20px solid #FF9800;
}

.asset-marker .marker-pin::before {
    content: '';
    position: absolute;
    width: 8px;
    height: 8px;
    background-color: #FF9800;
    border-radius: 50%;
    top: -4px;
    left: -4px;
    border: 2px solid white;
}

.leaflet-tile-pane {
    filter: var(--map-filter);
    -webkit-filter: var(--map-filter);
}

.leaflet-popup-content {
    margin: 8px 12px;
    font-size: 13px;
    line-height: 1.4;
}

.leaflet-popup-content b {
    color: var(--text-popup);
}

.data-form {
    max-width: 800px;
}

.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
    margin-bottom: 20px;
}

.form-group {
    margin-bottom: 20px;
}

.form-group label {
    display: block;
    margin-bottom: 5px;
    color: var(--text-secondary);
    font-weight: 500;
    font-size: 14px;
}

.form-group input,
.form-group select,
.form-group textarea {
    width: 100%;
    padding: 10px;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    font-size: 14px;
    font-family: inherit;
    background-color: var(--bg-input);
    color: var(--text-primary);
}

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

.form-group textarea {
    resize: vertical;
    min-height: 80px;
}

.form-actions {
    margin-top: 30px;
    padding-top: 20px;
    border-top: 1px solid var(--border-light);
}

.success-message {
    background-color: #d4edda;
    color: #155724;
    padding: 12px;
    border-radius: 4px;
    margin-top: 15px;
    border: 1px solid #c3e6cb;
}

#form-message,
#start-message {
    margin-top: 15px;
}

/* ==========================================
   NAVBAR LEFT GROUP
   ========================================== */

/* Hamburger toggle button - hidden on desktop */
.sidebar-toggle {
    display: none;
    flex-direction: column;
    justify-content: center;
    gap: 5px;
    background: none;
    border: none;
    cursor: pointer;
    padding: 4px;
    z-index: 1001;
}

.sidebar-toggle span {
    display: block;
    width: 24px;
    height: 2px;
    background-color: white;
    border-radius: 2px;
    transition: transform 0.3s, opacity 0.3s;
}

/* Sidebar overlay - hidden on desktop */
.sidebar-overlay {
    display: none;
}

/* Tables horizontal scroll wrapper */
.table-responsive {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

/* ==========================================
   THEME TOGGLE BUTTON
   ========================================== */
.theme-toggle {
    background: none;
    border: 1px solid rgba(255,255,255,0.3);
    border-radius: 20px;
    padding: 4px 10px;
    cursor: pointer;
    font-size: 16px;
    line-height: 1;
    color: var(--text-navbar);
    transition: background-color 0.2s, border-color 0.2s;
    display: flex;
    align-items: center;
    gap: 4px;
}

.theme-toggle:hover {
    background: rgba(255,255,255,0.1);
    border-color: rgba(255,255,255,0.5);
}

/* ==========================================
   RESPONSIVE: TABLET (max-width: 1024px)
   ========================================== */
@media (max-width: 1024px) {
    .content {
        padding: 20px;
    }

    .stats-grid {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    .form-row {
        gap: 15px;
    }

    .map-container,
    #worksite-map {
        height: 450px;
    }
}

/* ==========================================
   RESPONSIVE: MOBILE (max-width: 768px)
   ========================================== */
@media (max-width: 768px) {
    /* Navbar */
    .navbar {
        padding: 10px 15px;
        flex-wrap: wrap;
        gap: 8px;
    }

    .navbar-brand-text {
        display: none;
    }

    .navbar-user {
        gap: 8px;
        font-size: 13px;
    }

    .navbar-user #user-name {
        display: none;
    }

    /* Show hamburger */
    .sidebar-toggle {
        display: flex;
    }

    /* Sidebar as slide-over drawer */
    .sidebar {
        position: fixed;
        top: 0;
        left: -280px;
        width: 280px;
        height: 100vh;
        z-index: 1000;
        transition: left 0.3s ease;
        box-shadow: none;
        padding-top: 60px;
        overflow-y: auto;
    }

    .sidebar.open {
        left: 0;
        box-shadow: var(--shadow-sidebar);
    }

    /* Overlay when sidebar is open */
    .sidebar-overlay {
        display: block;
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: var(--shadow-overlay);
        z-index: 999;
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.3s, visibility 0.3s;
    }

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

    /* Main content takes full width */
    .main-content {
        flex-direction: column;
    }

    .content {
        padding: 15px;
        min-height: calc(100vh - 60px);
    }

    /* Form rows stack vertically */
    .form-row {
        grid-template-columns: 1fr !important;
        gap: 10px;
    }

    /* Stats grid single column */
    .stats-grid {
        grid-template-columns: 1fr !important;
        gap: 15px;
    }

    /* Cards */
    .card {
        padding: 15px;
        margin-bottom: 15px;
    }

    /* Tables */
    .table th,
    .table td {
        padding: 8px 6px;
        font-size: 12px;
    }

    .table {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        white-space: nowrap;
    }

    /* Dashboard header */
    .dashboard-header {
        margin-bottom: 15px;
    }

    .dashboard-header h2 {
        font-size: 20px;
    }

    /* Maps smaller on mobile */
    .map-container,
    #worksite-map,
    #worksites-map,
    #areas-overview-map,
    #area-draw-map,
    #edit-area-draw-map,
    #perimeterEditorMap,
    #perimeterMap {
        height: 300px !important;
    }

    /* Stat cards compact */
    .stat-card .value {
        font-size: 24px;
    }

    .stat-card h3 {
        font-size: 12px;
    }

    /* Login box */
    .login-box {
        padding: 25px 20px;
        margin: 10px;
    }

    /* Buttons */
    .btn {
        font-size: 13px;
        padding: 8px 16px;
    }

    /* Form group inputs */
    .form-group input,
    .form-group select,
    .form-group textarea {
        font-size: 16px; /* Prevents iOS zoom on focus */
    }

    /* Leaflet popups */
    .leaflet-popup-content {
        max-width: 220px;
        font-size: 12px;
    }

    /* Data form max width */
    .data-form {
        max-width: 100%;
    }

    /* Legend */
    .map-legend {
        padding: 10px;
    }

    .legend-item {
        font-size: 12px;
        margin-bottom: 5px;
    }
}

/* ==========================================
   RESPONSIVE: SMALL MOBILE (max-width: 480px)
   ========================================== */
@media (max-width: 480px) {
    .navbar {
        padding: 8px 10px;
    }

    .content {
        padding: 10px;
    }

    .card {
        padding: 12px;
        border-radius: 6px;
    }

    .stat-card {
        padding: 12px;
    }

    .stat-card .value {
        font-size: 20px;
    }

    .map-container,
    #worksite-map,
    #worksites-map,
    #areas-overview-map,
    #area-draw-map,
    #edit-area-draw-map,
    #perimeterEditorMap,
    #perimeterMap {
        height: 250px !important;
    }

    .table th,
    .table td {
        padding: 6px 4px;
        font-size: 11px;
    }

    .login-box {
        padding: 20px 15px;
    }

    .login-box h1 {
        font-size: 22px;
    }

    .login-tabs {
        gap: 5px;
    }

    .tab-btn {
        padding: 8px;
        font-size: 12px;
    }

    .badge {
        font-size: 10px;
        padding: 3px 8px;
    }
}

/* ============================================================
   Device History Map — Timeline & Markers
   ============================================================ */

.timeline-item {
    padding: 10px 12px;
    border-left: 3px solid var(--border-timeline);
    margin-bottom: 6px;
    transition: background-color 0.2s, border-left-color 0.2s;
    border-radius: 0 4px 4px 0;
}

.timeline-item:hover {
    background-color: var(--bg-timeline-hover);
    border-left-color: #3388ff;
}

.timeline-item[style*="cursor:pointer"]:hover {
    background-color: var(--bg-timeline-active);
}

.timeline-time {
    font-size: 11px;
    color: var(--text-hint);
    margin-bottom: 3px;
}

.timeline-content {
    font-size: 13px;
    line-height: 1.4;
}

.history-marker {
    background: transparent !important;
    border: none !important;
}

.history-summary {
    padding: 8px 12px;
    background: var(--bg-summary);
    border-radius: 6px;
    margin-bottom: 10px;
    font-size: 13px;
}

/* ============================================================
   TOAST NOTIFICATIONS
   ============================================================ */
#toast-container {
    position: fixed;
    top: 75px;
    right: 20px;
    z-index: 9999;
    display: flex;
    flex-direction: column;
    gap: 10px;
    pointer-events: none;
    max-width: 380px;
    width: calc(100% - 40px);
}

.toast {
    pointer-events: all;
    background: var(--bg-card);
    border-radius: 8px;
    box-shadow: 0 4px 24px rgba(0,0,0,0.22);
    padding: 13px 14px;
    display: flex;
    align-items: flex-start;
    gap: 11px;
    border-left: 5px solid #adb5bd;
    animation: toast-slide-in 0.3s cubic-bezier(.21,1.02,.73,1) forwards;
    position: relative;
    overflow: hidden;
}

.toast.removing {
    animation: toast-slide-out 0.25s ease forwards;
}

@keyframes toast-slide-in {
    from { transform: translateX(110%); opacity: 0; }
    to   { transform: translateX(0);    opacity: 1; }
}

@keyframes toast-slide-out {
    from { transform: translateX(0);    opacity: 1; max-height: 120px; margin-bottom: 0; }
    to   { transform: translateX(110%); opacity: 0; max-height: 0;    margin-bottom: -10px; }
}

.toast.toast-critical { border-left-color: #dc3545; }
.toast.toast-high     { border-left-color: #fd7e14; }
.toast.toast-medium   { border-left-color: #0dcaf0; }
.toast.toast-low      { border-left-color: #198754; }
.toast.toast-info     { border-left-color: #6c757d; }

.toast-icon  { font-size: 22px; flex-shrink: 0; line-height: 1.1; }
.toast-body  { flex: 1; min-width: 0; }
.toast-title { font-weight: 700; font-size: 14px; margin-bottom: 2px; color: var(--text-primary); }
.toast-desc  { font-size: 12px; color: var(--text-secondary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.toast-meta  { font-size: 11px; color: var(--text-hint); margin-top: 4px; }
.toast-close {
    background: none; border: none; cursor: pointer;
    font-size: 17px; color: var(--text-secondary);
    padding: 0; line-height: 1; flex-shrink: 0;
    opacity: 0.7; transition: opacity 0.15s;
}
.toast-close:hover { opacity: 1; }

.toast-progress {
    position: absolute;
    bottom: 0; left: 0;
    height: 3px;
    background: currentColor;
    opacity: 0.35;
    border-radius: 0 2px 2px 0;
    animation: toast-progress-shrink linear forwards;
}

@keyframes toast-progress-shrink {
    from { width: 100%; }
    to   { width: 0%; }
}

/* ============================================================
   NOTIFICATION BELL & PANEL
   ============================================================ */
.notification-bell {
    position: relative;
    cursor: pointer;
    font-size: 20px;
    padding: 4px 7px;
    border-radius: 6px;
    transition: background 0.15s;
    user-select: none;
    line-height: 1;
}

.notification-bell:hover { background: rgba(255,255,255,0.12); }

.notification-badge {
    position: absolute;
    top: -3px; right: -5px;
    background: #dc3545;
    color: #fff;
    border-radius: 10px;
    font-size: 10px;
    font-weight: 700;
    min-width: 17px;
    height: 17px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 3px;
    pointer-events: none;
}

.notification-panel {
    position: absolute;
    top: 60px;
    right: 10px;
    width: 360px;
    max-height: 500px;
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.18);
    z-index: 8000;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: panel-fade-in 0.18s ease;
}

@keyframes panel-fade-in {
    from { opacity: 0; transform: translateY(-6px); }
    to   { opacity: 1; transform: translateY(0); }
}

.notification-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border-color);
    font-weight: 700;
    font-size: 14px;
    flex-shrink: 0;
}

.sse-dot {
    width: 8px; height: 8px;
    border-radius: 50%;
    background: #adb5bd;
    display: inline-block;
    margin-right: 7px;
    vertical-align: middle;
    transition: background 0.4s, box-shadow 0.4s;
}

.sse-dot.connected {
    background: #28a745;
    box-shadow: 0 0 0 3px rgba(40,167,69,0.25);
    animation: sse-pulse 2s infinite;
}

@keyframes sse-pulse {
    0%, 100% { box-shadow: 0 0 0 3px rgba(40,167,69,0.25); }
    50%       { box-shadow: 0 0 0 6px rgba(40,167,69,0.05); }
}

.notification-panel-clear {
    font-size: 12px;
    color: var(--color-primary);
    cursor: pointer;
    background: none;
    border: none;
    padding: 0;
    font-weight: 600;
}

.notification-panel-body {
    overflow-y: auto;
    flex: 1;
}

.notification-item {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    padding: 10px 16px;
    border-bottom: 1px solid var(--border-color);
    cursor: pointer;
    transition: background 0.15s;
}

.notification-item:last-child { border-bottom: none; }
.notification-item:hover { background: var(--bg-hover, rgba(0,0,0,0.04)); }
.notification-item.unread { background: rgba(52,152,219,0.07); }

.notification-item-icon { font-size: 18px; flex-shrink: 0; padding-top: 1px; }
.notification-item-body { flex: 1; min-width: 0; }
.notification-item-title { font-weight: 600; font-size: 13px; color: var(--text-primary); }
.notification-item-desc  { font-size: 12px; color: var(--text-secondary); margin-top: 1px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.notification-item-time  { font-size: 11px; color: var(--text-hint); margin-top: 3px; }

.notification-empty {
    padding: 36px 16px;
    text-align: center;
    color: var(--text-secondary);
    font-size: 13px;
}

/* ============================================================
   EVENTS VIEW — improved
   ============================================================ */
.events-toolbar {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
    margin-bottom: 14px;
}

.events-live-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    color: #28a745;
    font-weight: 600;
    background: rgba(40,167,69,0.1);
    padding: 4px 10px;
    border-radius: 20px;
}

.events-live-dot {
    width: 7px; height: 7px;
    border-radius: 50%;
    background: #28a745;
    animation: pulse-dot 1.5s infinite;
}

@keyframes pulse-dot {
    0%, 100% { opacity: 1; }
    50%       { opacity: 0.3; }
}

.event-row-critical { background: rgba(220,53,69,0.05); }
.event-row-high     { background: rgba(253,126,20,0.05); }

.btn-xs {
    padding: 2px 8px;
    font-size: 11px;
    border-radius: 4px;
}

@media (max-width: 600px) {
    .notification-panel { width: calc(100vw - 20px); right: 10px; }
    #toast-container { right: 10px; max-width: calc(100vw - 20px); }
}

/* ==========================================
   SMART SEARCH COMPONENT
   ========================================== */
.smart-search {
    position: relative;
    display: block;
    width: 100%;
}

.ss-input-wrap {
    position: relative;
    display: flex;
    align-items: center;
}

/* Dropdown chevron — always visible, sits at the right edge */
.ss-input-wrap::after {
    content: '▾';
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-muted);
    font-size: 12px;
    pointer-events: none;
    transition: color 0.1s, transform 0.15s;
}
.smart-search.ss-open .ss-input-wrap::after {
    color: var(--color-primary);
    transform: translateY(-50%) rotate(180deg);
}

.ss-input-wrap .ss-input.form-control {
    width: 100%;
    padding-right: 28px;
    cursor: pointer;
}
.ss-input-wrap .ss-input.form-control:focus { cursor: text; }

.ss-clear-btn {
    position: absolute;
    right: 22px; /* sits left of the chevron */
    top: 50%;
    transform: translateY(-50%);
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 16px;
    line-height: 1;
    padding: 0;
    z-index: 1;
    opacity: 0.6;
}
.ss-clear-btn:hover { opacity: 1; color: var(--text-primary); }

.ss-dropdown {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    z-index: 9999;
    background: var(--bg-card);
    border: 1px solid var(--color-primary);
    border-top: none;
    border-radius: 0 0 6px 6px;
    max-height: 220px;
    overflow-y: auto;
    box-shadow: 0 6px 18px rgba(0,0,0,0.15);
}

.ss-option {
    padding: 7px 12px;
    cursor: pointer;
    font-size: 13px;
    color: var(--text-primary);
    line-height: 1.4;
    transition: background 0.08s;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.ss-option:hover { background: var(--bg-hover); }
.ss-option.ss-selected {
    background: var(--color-primary-light);
    color: var(--color-primary);
    font-weight: 500;
}
.ss-option.ss-selected:hover { background: var(--color-primary-light); }

.ss-check { color: var(--color-primary); font-weight: bold; margin-right: 2px; }

.ss-hint {
    padding: 7px 12px;
    font-size: 12px;
    color: var(--text-muted);
    font-style: italic;
}

/* Open state: remove bottom radius so input and dropdown connect */
.smart-search.ss-open .ss-input-wrap .ss-input.form-control,
.smart-search.ss-open .ss-tags-area.form-control {
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
    border-color: var(--color-primary);
}

/* Multi-select tags area */
.ss-multi .ss-tags-area {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    align-items: center;
    min-height: 38px;
    padding: 4px 8px;
    cursor: text;
    height: auto;
}
.ss-multi .ss-input {
    border: none;
    outline: none;
    background: transparent;
    padding: 2px 4px;
    flex: 1;
    min-width: 80px;
    font-size: 13px;
    color: var(--text-primary);
}
.ss-tags-display { display: contents; }

.ss-tag {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    background: var(--color-primary);
    color: #fff;
    font-size: 12px;
    padding: 2px 6px 2px 8px;
    border-radius: 12px;
    white-space: nowrap;
    max-width: 180px;
    overflow: hidden;
    text-overflow: ellipsis;
}
.ss-tag-rm {
    background: none;
    border: none;
    color: rgba(255,255,255,0.8);
    cursor: pointer;
    font-size: 15px;
    line-height: 1;
    padding: 0;
    flex-shrink: 0;
}
.ss-tag-rm:hover { color: #fff; }

/* ==========================================
   PRINT — Registro Cantiere (export PDF)
   ========================================== */

/* Repeated page header/footer. The report is wrapped in table.print-sheet so the
   browser repeats thead/tfoot on every printed page and, crucially, reserves
   their height in the page flow — position:fixed repeats but reserves nothing,
   so content printed underneath the bands. On screen the bands are hidden. */
.print-sheet { width: 100%; border-collapse: collapse; }
.print-sheet > thead,
.print-sheet > tfoot { display: none; }
.print-sheet > tbody > tr > td { padding: 0; border: none; }
.print-header, .print-footer { display: none; }

@media print {
    /* ── Hide every piece of app chrome ─────────────────────────────── */
    .no-print,
    body.printing-registry .sidebar,
    body.printing-registry .navbar,
    body.printing-registry .privacy-banner,
    body.printing-registry #registry-form,
    body.printing-registry .dashboard-header,
    body.printing-registry .view-header {
        display: none !important;
    }

    /* ── Neutralise the wrappers so the report flows freely ──────────
       These must NOT get break-inside:avoid — the outer card wraps the whole
       report, and telling the browser to keep it on one page is what left the
       first pages nearly blank. */
    body.printing-registry,
    body.printing-registry .main-content,
    body.printing-registry #dashboard-content,
    body.printing-registry #registry-result,
    body.printing-registry .card,
    body.printing-registry #registry-report {
        background: #fff !important;
        color: #000 !important;
        margin: 0 !important;
        padding: 0 !important;
        width: auto !important;
        max-width: none !important;
        border: none !important;
        border-radius: 0 !important;
        box-shadow: none !important;
        overflow: visible !important;
        break-inside: auto !important;
        page-break-inside: auto !important;
    }

    @page { margin: 12mm; }

    /* Repeat the bands on every page. table-header-group / table-footer-group
       also reserve their height on each page, so nothing prints underneath. */
    body.printing-registry .print-sheet > thead { display: table-header-group; }
    body.printing-registry .print-sheet > tfoot { display: table-footer-group; }

    /* The print-sheet is layout scaffolding, not a data table: no borders, no
       padding, and it must be free to break across pages. */
    body.printing-registry .print-sheet,
    body.printing-registry .print-sheet > thead > tr,
    body.printing-registry .print-sheet > tfoot > tr,
    body.printing-registry .print-sheet > tbody > tr,
    body.printing-registry .print-sheet > thead > tr > td,
    body.printing-registry .print-sheet > tfoot > tr > td,
    body.printing-registry .print-sheet > tbody > tr > td {
        border: none !important;
        padding: 0 !important;
        font-size: inherit !important;
        break-inside: auto !important;
        page-break-inside: auto !important;
    }

    body.printing-registry .print-header,
    body.printing-registry .print-footer {
        display: flex !important;
        justify-content: space-between;
        align-items: center;
        font-size: 9px;
        color: #444 !important;
        gap: 12px;
    }
    body.printing-registry .print-header {
        padding: 0 0 3px;
        margin-bottom: 6px;
        border-bottom: 1px solid #bbb;
    }
    body.printing-registry .print-footer {
        padding: 3px 0 0;
        margin-top: 6px;
        border-top: 1px solid #bbb;
    }

    /* The printable width (~703px on A4) is narrower than the mobile
       breakpoints, so the responsive one-column rules would otherwise kick in
       and stack the 8 KPI cards vertically — which pushed them onto page 2. */
    body.printing-registry .stats-grid {
        grid-template-columns: repeat(4, minmax(0, 1fr)) !important;
        gap: 8px !important;
        margin: 10px 0 !important;
        break-inside: auto;
        page-break-inside: auto;
    }
    body.printing-registry .stat-card {
        padding: 8px !important;
    }
    body.printing-registry .stat-card h4 { font-size: 10px !important; }
    body.printing-registry .stat-card .value { font-size: 16px !important; }

    /* ── Only small, self-contained blocks avoid page breaks ─────────── */
    body.printing-registry .stat-card,
    body.printing-registry .registry-chart-card {
        border: 1px solid #ccc !important;
        background: #fff !important;
        color: #000 !important;
        box-shadow: none !important;
        break-inside: avoid;
        page-break-inside: avoid;
    }

    /* One chart per row when printing, so each is full page width */
    body.printing-registry .registry-charts {
        grid-template-columns: 1fr !important;
        gap: 10px !important;
    }

    /* ── Tables: flow across pages, repeat the header row ────────────── */
    body.printing-registry table {
        border-collapse: collapse;
        width: 100%;
        break-inside: auto;
        page-break-inside: auto;
    }
    body.printing-registry thead { display: table-header-group; }
    body.printing-registry tr {
        break-inside: avoid;
        page-break-inside: avoid;
    }
    body.printing-registry th,
    body.printing-registry td {
        border: 1px solid #ccc !important;
        color: #000 !important;
        font-size: 10px;
        padding: 3px 5px !important;
    }

    body.printing-registry h3,
    body.printing-registry h4 {
        color: #000 !important;
        break-after: avoid;
        page-break-after: avoid;
    }

    body.printing-registry canvas { max-width: 100% !important; }
}

/* ==========================================
   REGISTRO CANTIERE — grafici
   ========================================== */
.registry-charts {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 16px;
    margin: 24px 0;
}

/* Time series read better across the full width */
.registry-charts .registry-chart-wide {
    grid-column: 1 / -1;
}

.registry-chart-card {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 14px;
    min-width: 0;          /* let the grid shrink the card instead of overflowing */
}

.registry-chart-card h4 {
    margin: 0 0 10px;
    font-size: 15px;
}

/* Chart.js with maintainAspectRatio:false needs a sized, relative parent.
   Without it the canvas grows on every resize tick and the page never settles. */
.registry-chart-box {
    position: relative;
    width: 100%;
}

.registry-chart-box > canvas {
    position: absolute;
    inset: 0;
    width: 100% !important;
    height: 100% !important;
}

@media (max-width: 900px) {
    .registry-charts { grid-template-columns: minmax(0, 1fr); }
}
