/*
 * Application theme. Every color, size, radius and spacing used by screens
 * comes from the tokens below (layered on Bootstrap's own CSS variables).
 * Screens never type a literal: if a screen needs a new style, the theme
 * changes here. Both light and dark themes are defined and kept at WCAG AA.
 */
:root,
[data-bs-theme="light"] {
    --app-bg: #f4f6f8;
    --app-surface: #ffffff;
    --app-surface-muted: #eef1f4;
    --app-border: #d5dbe1;
    --app-text: #1c2530;
    --app-text-muted: #55606c;
    --app-primary: #0b5cad;
    --app-primary-hover: #094a8c;
    --app-primary-contrast: #ffffff;
    --app-sidebar-bg: #0f2a44;
    --app-sidebar-text: #d9e4ef;
    --app-sidebar-active-bg: #1d4f80;
    --app-sidebar-active-text: #ffffff;
    --app-focus: #f2a900;

    --app-status-ok-bg: #d8f0e0;
    --app-status-ok-text: #13582d;
    --app-status-warn-bg: #fff0c7;
    --app-status-warn-text: #6b4a00;
    --app-status-bad-bg: #fbdcdc;
    --app-status-bad-text: #8a1c1c;
    --app-status-neutral-bg: #e4e8ec;
    --app-status-neutral-text: #3a4550;

    --app-radius: 0.5rem;
    --app-radius-sm: 0.375rem;
    --app-space-1: 0.25rem;
    --app-space-2: 0.5rem;
    --app-space-3: 1rem;
    --app-space-4: 1.5rem;
    --app-space-5: 2.5rem;
    --app-sidebar-width: 15rem;
    --app-topbar-height: 3.5rem;
    --app-font-size-sm: 0.875rem;
    --app-font-size-title: 1.5rem;
    --app-font-size-kpi: 1.75rem;
    --app-icon-size: 1.1em;
    --app-icon-size-lg: 2.5rem;
    --app-shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.1);
    --app-map-height: 70vh;
    --app-map-height-phone: 55vh;
    --app-map-min-height: 20rem;
    --app-map-min-height-phone: 16rem;
    --app-photo-height: 12rem;
    --app-photo-height-phone: 10rem;
    --app-form-narrow-width: 48rem;
    --app-touch-target: 2.75rem;
    --app-kpi-min: 11rem;
    --app-kpi-max-share: 18%;

    --bs-body-bg: var(--app-bg);
    --bs-body-color: var(--app-text);
    --bs-secondary-color: var(--app-text-muted);
    --bs-border-color: var(--app-border);
    --bs-primary: var(--app-primary);
    --bs-primary-rgb: 11, 92, 173;
    --bs-link-color: var(--app-primary);
    --bs-link-color-rgb: 11, 92, 173;
    --bs-link-hover-color: var(--app-primary-hover);
    --bs-border-radius: var(--app-radius);
    --bs-card-bg: var(--app-surface);
}

[data-bs-theme="dark"] {
    --app-bg: #10161d;
    --app-surface: #18212b;
    --app-surface-muted: #202b37;
    --app-border: #334252;
    --app-text: #e6edf3;
    --app-text-muted: #a6b3c0;
    --app-primary: #5aa9f5;
    --app-primary-hover: #82bff8;
    --app-primary-contrast: #0b1520;
    --app-sidebar-bg: #0b1219;
    --app-sidebar-text: #c9d6e2;
    --app-sidebar-active-bg: #1d3b5a;
    --app-sidebar-active-text: #ffffff;
    --app-focus: #ffc94a;

    --app-status-ok-bg: #143d25;
    --app-status-ok-text: #b4ebc8;
    --app-status-warn-bg: #4a3800;
    --app-status-warn-text: #ffe6a3;
    --app-status-bad-bg: #4d1a1a;
    --app-status-bad-text: #ffc9c9;
    --app-status-neutral-bg: #2a3540;
    --app-status-neutral-text: #d3dce5;
    --app-shadow: 0 1px 2px rgba(0, 0, 0, 0.4);

    --bs-body-bg: var(--app-bg);
    --bs-body-color: var(--app-text);
    --bs-secondary-color: var(--app-text-muted);
    --bs-border-color: var(--app-border);
    --bs-primary: var(--app-primary);
    --bs-primary-rgb: 90, 169, 245;
    --bs-link-color: var(--app-primary);
    --bs-link-color-rgb: 90, 169, 245;
    --bs-link-hover-color: var(--app-primary-hover);
    --bs-card-bg: var(--app-surface);
}

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

/* The hidden attribute must always win: Bootstrap display utilities (.d-flex etc.) are
   !important and would otherwise keep JS-toggled banners and buttons visible. */
[hidden] { display: none !important; }

.bi {
    width: var(--app-icon-size);
    height: var(--app-icon-size);
    fill: currentColor;
    vertical-align: -0.125em;
    flex-shrink: 0;
}

:focus-visible { outline: 3px solid var(--app-focus); outline-offset: 2px; }

.skip-link {
    position: absolute;
    z-index: 2000;
    padding: var(--app-space-2) var(--app-space-3);
    background: var(--app-surface);
    color: var(--app-text);
}

/* Buttons --------------------------------------------------------------- */
.btn-primary {
    --bs-btn-bg: var(--app-primary);
    --bs-btn-border-color: var(--app-primary);
    --bs-btn-color: var(--app-primary-contrast);
    --bs-btn-hover-bg: var(--app-primary-hover);
    --bs-btn-hover-border-color: var(--app-primary-hover);
    --bs-btn-hover-color: var(--app-primary-contrast);
    --bs-btn-active-bg: var(--app-primary-hover);
    --bs-btn-disabled-bg: var(--app-primary);
    --bs-btn-disabled-border-color: var(--app-primary);
    --bs-btn-disabled-color: var(--app-primary-contrast);
}
.btn-outline-primary {
    --bs-btn-color: var(--app-primary);
    --bs-btn-border-color: var(--app-primary);
    --bs-btn-hover-bg: var(--app-primary);
    --bs-btn-hover-border-color: var(--app-primary);
    --bs-btn-hover-color: var(--app-primary-contrast);
}
.btn-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: var(--app-space-1) var(--app-space-2);
    color: var(--app-text);
    border: 1px solid transparent;
}
.btn-icon:hover { background: var(--app-surface-muted); }
.btn-user { color: var(--app-text); border: 1px solid var(--app-border); }
.row-actions { display: inline-flex; gap: var(--app-space-1); }
.row-actions .btn { padding: var(--app-space-1) var(--app-space-2); line-height: 1; }

/* Shell ------------------------------------------------------------------ */
.app-topbar {
    position: sticky;
    top: 0;
    z-index: 1030;
    min-height: var(--app-topbar-height);
    background: var(--app-surface);
}
.app-topbar .navbar-brand { color: var(--app-text); }
.role-badge { background: var(--app-surface-muted); color: var(--app-text); border: 1px solid var(--app-border); font-weight: 500; }

.app-shell { display: flex; min-height: calc(100vh - var(--app-topbar-height)); }

.app-sidebar {
    --bs-offcanvas-bg: var(--app-sidebar-bg);
    --bs-offcanvas-color: var(--app-sidebar-text);
    --bs-offcanvas-width: var(--app-sidebar-width);
    background: var(--app-sidebar-bg);
    color: var(--app-sidebar-text);
}
.app-sidebar .offcanvas-body { display: flex; flex-direction: column; padding: var(--app-space-3) var(--app-space-2); }
.app-sidebar .btn-close { filter: invert(1); }
.app-sidebar .nav-link {
    display: flex;
    align-items: center;
    gap: var(--app-space-2);
    color: var(--app-sidebar-text);
    border-radius: var(--app-radius-sm);
    padding: var(--app-space-2) var(--app-space-3);
}
.app-sidebar .nav-link:hover { background: var(--app-sidebar-active-bg); color: var(--app-sidebar-active-text); }
.app-sidebar .nav-link.active { background: var(--app-sidebar-active-bg); color: var(--app-sidebar-active-text); font-weight: 600; }
.app-sidebar .app-version { color: var(--app-sidebar-text); opacity: 0.85; padding: 0 var(--app-space-3); }

@media (min-width: 992px) {
    /* Bootstrap's .offcanvas-lg forces a transparent background at this width; restore the token. */
    .app-sidebar.offcanvas-lg { background-color: var(--app-sidebar-bg) !important; }
    .app-sidebar {
        width: var(--app-sidebar-width);
        flex-shrink: 0;
        position: sticky;
        top: var(--app-topbar-height);
        height: calc(100vh - var(--app-topbar-height));
        overflow-y: auto;
    }
}

.app-main { flex: 1; min-width: 0; padding: var(--app-space-4); }
.app-main:focus { outline: none; }
@media (max-width: 575.98px) { .app-main { padding: var(--app-space-3); } }

/* Top bar on phones: the brand text truncates instead of pushing the user menu off-screen. */
.app-topbar .container-fluid { flex-wrap: nowrap; min-width: 0; }
.app-topbar .navbar-brand { min-width: 0; margin-right: 0; }
.app-brand-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.app-topbar .dropdown-header { white-space: normal; }

/* Touch screens: icon buttons and pager links get a 44px hit area. */
@media (pointer: coarse) {
    .btn-icon,
    .row-actions .btn,
    .btn-touch,
    .btn[aria-label],
    .btn-close,
    .pagination .page-link,
    .app-topbar .btn-user {
        min-width: var(--app-touch-target);
        min-height: var(--app-touch-target);
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }
    .app-sidebar .nav-link { min-height: var(--app-touch-target); }
    .form-check:not(.form-check-inline):not(.form-switch) { min-height: var(--app-touch-target); display: flex; align-items: center; gap: var(--app-space-2); padding-left: 0; }
    .form-check:not(.form-check-inline):not(.form-switch) .form-check-input { float: none; margin: 0; flex-shrink: 0; }
}

/* Page header ------------------------------------------------------------ */
.page-header { margin-bottom: var(--app-space-4); }
.page-title { font-size: var(--app-font-size-title); font-weight: 600; margin: 0; }
.page-subtitle { font-size: var(--app-font-size-sm); }
.breadcrumb { font-size: var(--app-font-size-sm); }

/* Cards, tables, forms ------------------------------------------------ */
.card { background: var(--app-surface); border-color: var(--app-border); box-shadow: var(--app-shadow); border-radius: var(--app-radius); }
/* Create/edit forms fill the content width; fields are laid out with the grid
   (row g-3 > col-12 col-md-6 col-xl-4). Small confirmation pages stay narrow. */
.form-card { width: 100%; }
.form-card-narrow { max-width: var(--app-form-narrow-width); }
.form-field { margin-bottom: var(--app-space-3); }
.row > * > .form-field { margin-bottom: 0; }
/* "Use my current location" under a coordinates field (partials/geolocate_button.php). */
.geolocate { margin-top: var(--app-space-2); }
.geolocate-status:empty { display: none; }
.table { --bs-table-bg: var(--app-surface); --bs-table-color: var(--app-text); margin-bottom: 0; }
.table thead th { background: var(--app-surface-muted); font-size: var(--app-font-size-sm); white-space: nowrap; }
.table td { vertical-align: middle; }
.sort-link { color: inherit; text-decoration: none; }
.sort-link:hover { text-decoration: underline; }
.required-mark { color: var(--app-status-bad-text); margin-left: var(--app-space-1); }
.form-actions { margin-top: var(--app-space-3); }
/* Phones: form buttons stack full width, the primary action on top. */
@media (max-width: 575.98px) {
    .form-actions { flex-direction: column-reverse; align-items: stretch; }
    .form-actions > .btn { width: 100%; justify-content: center; }
}
.filter-bar { margin-bottom: var(--app-space-3); }

/* Filter panel: a <details> that is always open on md+ (the summary is hidden there) and
   collapsible on phones. Rendered open, so it works without JS; app.js collapses it on
   phones when no filter is applied. */
.filter-summary {
    display: flex;
    align-items: center;
    gap: var(--app-space-2);
    min-height: var(--app-touch-target);
    padding: var(--app-space-2) var(--app-space-3);
    font-weight: 600;
    cursor: pointer;
    list-style: none;
}
.filter-summary::-webkit-details-marker { display: none; }
.filter-panel[open] > .filter-summary { border-bottom: 1px solid var(--app-border); }
.filter-chevron { margin-left: auto; }
.filter-chevron .bi { transition: transform 0.15s ease-in-out; }
.filter-panel:not([open]) .filter-chevron .bi { transform: rotate(-90deg); }
@media (min-width: 768px) {
    .filter-summary { display: none; }
    .filter-panel::details-content { content-visibility: visible; }
}.flash-stack { margin-bottom: var(--app-space-3); }
.pagination { flex-wrap: wrap; row-gap: var(--app-space-1); }
/* Below lg the row actions stay pinned to the right edge while the other columns scroll. */
@media (max-width: 991.98px) {
    .table-sticky-actions tr > :last-child {
        position: sticky;
        right: 0;
        z-index: 1;
        background-color: var(--app-surface);
        box-shadow: inset 1px 0 0 var(--app-border);
    }
    .table-sticky-actions thead tr > :last-child { background-color: var(--app-surface-muted); }
}
.text-pre-line { white-space: pre-line; }

/* Status chips: color + icon + text, never color alone ------------------ */
.status-chip {
    display: inline-flex;
    align-items: center;
    gap: var(--app-space-1);
    padding: 0.15rem var(--app-space-2);
    border-radius: 999px;
    font-size: var(--app-font-size-sm);
    font-weight: 500;
    white-space: nowrap;
    text-decoration: none;
    border: 1px solid transparent;
}
a.status-chip:hover { border-color: currentColor; }
.status-ok { background: var(--app-status-ok-bg); color: var(--app-status-ok-text); }
.status-warn { background: var(--app-status-warn-bg); color: var(--app-status-warn-text); }
.status-bad { background: var(--app-status-bad-bg); color: var(--app-status-bad-text); }
.status-neutral { background: var(--app-status-neutral-bg); color: var(--app-status-neutral-text); }
.status-chip.is-active { border-color: currentColor; box-shadow: inset 0 0 0 1px currentColor; }

/* KPI tiles and proportion bar -------------------------------------------- */
.kpi-tile { padding: var(--app-space-3); }
.kpi-value { font-size: var(--app-font-size-kpi); font-weight: 700; line-height: 1.1; }
.kpi-label { font-size: var(--app-font-size-sm); color: var(--app-text-muted); }
.proportion-bar { display: flex; height: 0.75rem; border-radius: 999px; overflow: hidden; background: var(--app-surface-muted); }
.proportion-bar > span { display: block; height: 100%; }
.proportion-bar .seg-ok { background: var(--app-status-ok-text); }
.proportion-bar .seg-warn { background: var(--app-status-warn-text); }
.proportion-bar .seg-bad { background: var(--app-status-bad-text); }
.proportion-bar .seg-neutral { background: var(--app-status-neutral-text); }

/* Empty / error states ------------------------------------------------------ */
.empty-state { padding: var(--app-space-5) var(--app-space-3); color: var(--app-text-muted); }
.empty-state-icon .bi, .error-icon .bi { width: var(--app-icon-size-lg); height: var(--app-icon-size-lg); }
.empty-state-icon, .error-icon { margin-bottom: var(--app-space-3); color: var(--app-text-muted); }
.error-page { padding: var(--app-space-5) var(--app-space-3); }

/* Photos ------------------------------------------------------------------- */
.photo-tile {
    border: 1px solid var(--app-border);
    border-radius: var(--app-radius);
    overflow: hidden;
    background: var(--app-surface-muted);
}
.photo-tile img { display: block; width: 100%; height: var(--app-photo-height); object-fit: cover; }
/* "Instalar app" page (views/profile/install.php): the current device's steps stand out. */
.install-icon { border-radius: var(--app-radius); flex-shrink: 0; }
.install-badge { display: none; background: var(--app-primary); color: var(--app-primary-contrast); font-weight: 500; }
.is-current-platform .install-card { border-color: var(--app-primary); box-shadow: 0 0 0 1px var(--app-primary); }
.is-current-platform .install-badge { display: inline-block; }
/* Installed app: respect the iPhone notch / home indicator areas. */
.is-standalone .app-topbar { padding-top: env(safe-area-inset-top); }
.is-standalone .app-main { padding-bottom: calc(var(--app-space-4) + env(safe-area-inset-bottom)); }
/* "Tomar foto" / "Elegir de la galería" buttons under each photo slot (js/photo-picker.js). */
.photo-picker { display: flex; flex-wrap: wrap; gap: var(--app-space-1); }
.photo-picker .btn { flex: 1 1 auto; justify-content: center; min-height: var(--app-touch-target); }
.photo-picker-status:empty { display: none; }
.photo-missing {
    height: var(--app-photo-height);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--app-space-2);
    color: var(--app-text-muted);
    padding: var(--app-space-3);
    text-align: center;
    font-size: var(--app-font-size-sm);
}

/* Map ------------------------------------------------------------------- */
.map-canvas { height: var(--app-map-height); min-height: var(--app-map-min-height); border-radius: var(--app-radius); border: 1px solid var(--app-border); }
@media (max-width: 575.98px) {
    .map-canvas { height: var(--app-map-height-phone); min-height: var(--app-map-min-height-phone); }
    .photo-tile img, .photo-missing { height: var(--app-photo-height-phone); }
}
.map-legend { display: flex; flex-wrap: wrap; gap: var(--app-space-2); margin-top: var(--app-space-2); }

/* Auth -------------------------------------------------------------------- */
.auth-body { min-height: 100vh; }
.auth-main {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: var(--app-space-3);
}
.auth-card { width: 100%; max-width: 26rem; }
.auth-brand .bi { width: 2rem; height: 2rem; color: var(--app-primary); }

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { transition: none !important; animation-duration: 0.01ms !important; }
}

/* Wide lists: secondary text columns, column visibility (js/table-columns.js). */
:root {
    --app-cell-text-min: 12rem;
    --app-cell-text-max: 20rem;
    --app-menu-min-width: 14rem;
}
.cell-text { min-width: var(--app-cell-text-min); max-width: var(--app-cell-text-max); }
.col-hidden { display: none; }
.column-toggle-menu { min-width: var(--app-menu-min-width); }

/* Screenshot deterrence (partials/screen_guard.php). */
.screen-watermark {
    position: fixed;
    inset: -50%;
    z-index: 2000;
    display: flex;
    flex-wrap: wrap;
    align-content: center;
    justify-content: center;
    gap: 3.5rem 3rem;
    transform: rotate(-30deg);
    overflow: hidden;
    pointer-events: none;
    user-select: none;
}
.screen-watermark span {
    font-size: 0.8rem;
    white-space: nowrap;
    color: var(--bs-body-color);
    /* Barely visible on purpose: stays in captures and shows up when contrast is raised. */
    opacity: 0.02;
}
.screen-shield {
    position: fixed;
    inset: 0;
    z-index: 2010;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    text-align: center;
    background: var(--bs-body-bg);
}
.screen-shield .bi { width: 3rem; height: 3rem; margin-bottom: 0.75rem; color: var(--app-primary); }
.is-shielded .screen-shield { display: flex; }
