/* ==========================================================================
   AquaCore -- design tokens (ocean/aqua palette, light + dark)
   ========================================================================== */

:root {
    --ocean-900: #052e3d;
    --ocean-800: #0b3d4c;
    --ocean-700: #0e4f61;
    --ocean-600: #12657a;
    --ocean-500: #178a9e;
    --ocean-400: #2ba9bd;
    --ocean-300: #6fc7d6;
    --ocean-100: #e3f4f6;

    --accent: var(--ocean-500);
    --accent-strong: var(--ocean-600);

    --danger: #d64545;
    --warning: #d99a2b;
    --success: #2f9e6e;
    --info: var(--ocean-400);

    --radius: 10px;
    --radius-lg: 14px;
    --sidebar-width: 240px;
    --topbar-height: 64px;
}

:root,
[data-theme="light"] {
    --bg-page: #eef4f5;
    --bg-surface: #ffffff;
    --bg-surface-alt: #f6fafb;
    --text-primary: #16262b;
    --text-secondary: #4b6067;
    --text-muted: #7d9298;
    --border-color: #dbe6e8;
    --shadow-sm: 0 1px 2px rgba(9, 41, 51, 0.06);
    --shadow-md: 0 4px 14px rgba(9, 41, 51, 0.08);
}

[data-theme="dark"] {
    --bg-page: #0d1b20;
    --bg-surface: #142a31;
    --bg-surface-alt: #17323b;
    --text-primary: #eaf4f6;
    --text-secondary: #a9c1c6;
    --text-muted: #75898f;
    --border-color: #23434c;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 6px 18px rgba(0, 0, 0, 0.35);
}

html {
    scroll-behavior: smooth;
    scroll-padding-top: calc(var(--topbar-height) + 12px);
}

body {
    font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
    background: var(--bg-page);
    color: var(--text-primary);
    transition: background-color .2s ease, color .2s ease;
}

/* ==========================================================================
   Shell layout: fixed sidebar + sticky topbar + content
   ========================================================================== */

.app-shell {
    display: flex;
    min-height: 100vh;
}

.app-sidebar {
    width: var(--sidebar-width);
    flex-shrink: 0;
    background: var(--ocean-900);
    display: flex;
    flex-direction: column;
    position: fixed;
    inset: 0 auto 0 0;
    height: 100vh;
    overflow-y: auto;
    z-index: 1030;
    transition: transform .25s ease;
}

.app-sidebar-brand {
    display: flex;
    align-items: center;
    gap: .6rem;
    padding: 1.25rem 1.25rem;
    font-size: 1.1rem;
    font-weight: 600;
    color: #fff;
}

.app-sidebar-brand i {
    font-size: 1.4rem;
    color: var(--ocean-300);
}

.app-nav {
    flex: 1;
    padding: 0 .75rem;
}

.app-nav-section {
    font-size: .7rem;
    text-transform: uppercase;
    letter-spacing: .06em;
    color: rgba(255, 255, 255, .45);
    padding: 1rem .75rem .35rem;
}

.app-nav-link {
    display: flex;
    align-items: center;
    gap: .65rem;
    padding: .55rem .75rem;
    border-radius: var(--radius);
    color: rgba(255, 255, 255, .75);
    text-decoration: none;
    font-size: .9rem;
    margin-bottom: .15rem;
}

.app-nav-link i {
    font-size: 1rem;
    width: 1.1rem;
    text-align: center;
}

.app-nav-link:hover {
    background: rgba(255, 255, 255, .08);
    color: #fff;
}

.app-nav-link.active {
    background: var(--ocean-600);
    color: #fff;
    font-weight: 500;
}

.app-nav-link.is-disabled {
    opacity: .45;
    cursor: not-allowed;
}

.app-nav-link.is-disabled:hover {
    background: transparent;
    color: rgba(255, 255, 255, .75);
}

.app-sidebar-footer {
    padding: 1rem 1.25rem;
    border-top: 1px solid rgba(255, 255, 255, .08);
}

.app-sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(5, 20, 25, .55);
    z-index: 1020;
}

.app-main {
    flex: 1;
    min-width: 0;
    margin-left: var(--sidebar-width);
    display: flex;
    flex-direction: column;
}

.app-topbar {
    position: sticky;
    top: 0;
    z-index: 1010;
    height: var(--topbar-height);
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0 1.5rem;
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border-color);
}

.app-sidebar-toggle {
    display: none;
    border: none;
    background: transparent;
    font-size: 1.4rem;
    color: var(--text-secondary);
}

.app-topbar-title h1 {
    font-size: 1.05rem;
    font-weight: 600;
    margin: 0;
    color: var(--text-primary);
}

.app-topbar-subtitle {
    font-size: .78rem;
    color: var(--text-muted);
}

.app-topbar-actions {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: .75rem;
}

.app-refresh-pill {
    display: flex;
    align-items: center;
    gap: .4rem;
    font-size: .78rem;
    color: var(--text-secondary);
    background: var(--bg-surface-alt);
    border: 1px solid var(--border-color);
    padding: .3rem .65rem;
    border-radius: 999px;
}

.app-refresh-pill.is-spinning i {
    animation: app-spin 1s linear;
}

@keyframes app-spin {
    from { transform: rotate(0); }
    to { transform: rotate(360deg); }
}

.app-theme-toggle {
    border: 1px solid var(--border-color);
    background: var(--bg-surface-alt);
    color: var(--text-secondary);
    width: 2.2rem;
    height: 2.2rem;
    border-radius: 999px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.app-theme-toggle:hover {
    color: var(--accent);
    border-color: var(--accent);
}

.app-content {
    padding: 1.5rem;
    flex: 1;
}

@media (max-width: 991.98px) {
    .app-sidebar {
        transform: translateX(-100%);
    }

    .app-sidebar.is-open {
        transform: translateX(0);
        box-shadow: var(--shadow-md);
    }

    .app-sidebar-backdrop.is-open {
        display: block;
    }

    .app-main {
        margin-left: 0;
    }

    .app-sidebar-toggle {
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }
}

@media (max-width: 575.98px) {
    .app-content {
        padding: 1rem;
    }

    .app-topbar {
        padding: 0 1rem;
    }

    .app-topbar-subtitle {
        display: none;
    }
}

/* ==========================================================================
   Section headers
   ========================================================================== */

.section-title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: 2rem 0 .9rem;
    scroll-margin-top: calc(var(--topbar-height) + 12px);
}

.section-title h2 {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-primary);
    margin: 0;
    display: flex;
    align-items: center;
    gap: .5rem;
}

.section-title h2 i {
    color: var(--accent);
}

.section-count {
    font-size: .78rem;
    color: var(--text-muted);
    background: var(--bg-surface-alt);
    border: 1px solid var(--border-color);
    padding: .15rem .6rem;
    border-radius: 999px;
}

/* ==========================================================================
   KPI stat cards
   ========================================================================== */

.stat-card {
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 1.1rem 1.25rem;
    box-shadow: var(--shadow-sm);
    display: flex;
    align-items: center;
    gap: .9rem;
    height: 100%;
}

.stat-card-icon {
    width: 2.6rem;
    height: 2.6rem;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2rem;
    background: var(--ocean-100);
    color: var(--ocean-600);
    flex-shrink: 0;
}

[data-theme="dark"] .stat-card-icon {
    background: rgba(43, 169, 189, .15);
    color: var(--ocean-300);
}

.stat-card-value {
    font-size: 1.4rem;
    font-weight: 600;
    line-height: 1.1;
    color: var(--text-primary);
}

.stat-card-label {
    font-size: .78rem;
    color: var(--text-muted);
}

/* ==========================================================================
   Alerts panel
   ========================================================================== */

.alert-item {
    display: flex;
    gap: .75rem;
    align-items: flex-start;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-left: 3px solid var(--border-color);
    border-radius: var(--radius);
    padding: .75rem .9rem;
    margin-bottom: .6rem;
    box-shadow: var(--shadow-sm);
}

.alert-item[data-severity="critical"] { border-left-color: var(--danger); }
.alert-item[data-severity="warning"]  { border-left-color: var(--warning); }
.alert-item[data-severity="info"]     { border-left-color: var(--info); }

.alert-item-icon { font-size: 1.05rem; margin-top: .1rem; }
.alert-item[data-severity="critical"] .alert-item-icon { color: var(--danger); }
.alert-item[data-severity="warning"] .alert-item-icon  { color: var(--warning); }
.alert-item[data-severity="info"] .alert-item-icon     { color: var(--info); }

.alert-item-body { flex: 1; min-width: 0; }
.alert-item-device { font-weight: 600; font-size: .85rem; color: var(--text-primary); }
.alert-item-message { font-size: .83rem; color: var(--text-secondary); }
.alert-item-time { font-size: .75rem; color: var(--text-muted); white-space: nowrap; }

/* Resolved entries in the "Recent activity" history list -- muted so the
   still-open alerts above stay visually dominant, but never fully
   hidden, which is the whole point of keeping this list at all. */
.alert-item.is-resolved {
    border-left-color: var(--border-color);
    opacity: .7;
}

.alert-item.is-resolved .alert-item-icon { color: var(--success); }

.alert-status-badge {
    display: inline-block;
    font-size: .68rem;
    font-weight: 500;
    color: var(--success);
    background: rgba(47, 158, 110, .12);
    padding: .05rem .45rem;
    border-radius: 999px;
    margin-left: .35rem;
    vertical-align: middle;
}

.alert-item-compact { padding: .55rem .8rem; margin-bottom: .4rem; }

/* Severity variants, reused on the Admin > Alert Rules table -- the base
   .alert-status-badge above defaults to the "resolved" green look. */
.alert-status-badge.severity-warning  { color: var(--warning); background: rgba(217, 154, 43, .14); }
.alert-status-badge.severity-critical { color: var(--danger);  background: rgba(214, 69, 69, .14); }
.alert-status-badge.severity-info     { color: var(--info);    background: rgba(43, 169, 189, .14); }

.btn-notify {
    display: inline-flex;
    align-items: center;
    gap: .35rem;
    font-size: .78rem;
    font-weight: 500;
    color: var(--text-secondary);
    background: var(--bg-surface-alt);
    border: 1px solid var(--border-color);
    padding: .3rem .7rem;
    border-radius: 999px;
}

.btn-notify:hover:not(:disabled) {
    border-color: var(--accent);
    color: var(--accent);
}

.btn-notify.is-active {
    background: rgba(47, 158, 110, .12);
    border-color: transparent;
    color: var(--success);
}

.btn-notify:disabled {
    opacity: .6;
    cursor: not-allowed;
}

.history-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 1.1rem;
    height: 1.1rem;
    padding: 0 .3rem;
    border-radius: 999px;
    background: var(--danger);
    color: #fff;
    font-size: .68rem;
    font-weight: 600;
    line-height: 1;
}

.empty-state {
    text-align: center;
    padding: 2rem 1rem;
    color: var(--text-muted);
    background: var(--bg-surface);
    border: 1px dashed var(--border-color);
    border-radius: var(--radius-lg);
}

.empty-state i {
    font-size: 1.6rem;
    display: block;
    margin-bottom: .4rem;
    color: var(--ocean-300);
}

/* ==========================================================================
   Device cards
   ========================================================================== */

.device-card {
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 1rem 1.1rem;
    box-shadow: var(--shadow-sm);
    height: 100%;
}

.device-card-top {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: .5rem;
}

.device-card-name { font-weight: 600; font-size: .95rem; color: var(--text-primary); }
.device-card-meta { font-size: .78rem; color: var(--text-muted); margin-top: .1rem; }

.device-card-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: .9rem;
    padding-top: .7rem;
    border-top: 1px solid var(--border-color);
    font-size: .8rem;
    color: var(--text-secondary);
}

.status-dot {
    width: .5rem;
    height: .5rem;
    border-radius: 50%;
    display: inline-block;
    margin-right: .35rem;
}

.status-dot.online  { background: var(--success); box-shadow: 0 0 0 3px rgba(47, 158, 110, .18); }
.status-dot.stale   { background: var(--warning); box-shadow: 0 0 0 3px rgba(217, 154, 43, .18); }
.status-dot.offline { background: var(--danger);  box-shadow: 0 0 0 3px rgba(214, 69, 69, .18); }
.status-dot.never   { background: var(--text-muted); }

.status-pill {
    display: inline-flex;
    align-items: center;
    font-size: .72rem;
    font-weight: 500;
    padding: .2rem .55rem;
    border-radius: 999px;
}

.status-pill.online  { background: rgba(47, 158, 110, .12); color: var(--success); }
.status-pill.stale   { background: rgba(217, 154, 43, .12); color: var(--warning); }
.status-pill.offline { background: rgba(214, 69, 69, .12); color: var(--danger); }
.status-pill.never   { background: var(--bg-surface-alt); color: var(--text-muted); }

/* ==========================================================================
   Chart cards -- one live trend line per (device, module), replacing
   what used to be a "latest reading" table row. The point of this
   dashboard is spotting drift over time, not a single snapshot value.
   ========================================================================== */

.chart-card {
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 1rem 1.1rem;
    box-shadow: var(--shadow-sm);
    height: 100%;
}

.chart-card-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: .5rem;
    margin-bottom: .6rem;
}

.chart-card-title { font-weight: 600; font-size: .9rem; color: var(--text-primary); text-transform: capitalize; }
.chart-card-title i { color: var(--accent); margin-right: .3rem; }
.chart-card-subtitle { font-size: .78rem; color: var(--text-muted); }

/* Deliberately the only clickable element on the card -- the canvas
   itself stays free for Chart.js's own legend-toggle/tooltip
   interactions, which a whole-card click handler used to intercept. */
.chart-card-expand {
    flex-shrink: 0;
    width: 1.8rem;
    height: 1.8rem;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: .85rem;
    color: var(--text-muted);
    background: var(--bg-surface-alt);
    border: 1px solid var(--border-color);
    border-radius: 8px;
}

.chart-card-expand:hover {
    color: var(--accent);
    border-color: var(--accent);
}

.chart-card canvas { max-height: 130px; }

.chart-card-footer {
    display: flex;
    flex-wrap: wrap;
    margin-top: .75rem;
    padding-top: .6rem;
    border-top: 1px solid var(--border-color);
}

/* ==========================================================================
   DataTables theming -- not used on the dashboard itself anymore (see
   chart cards above), kept here as shared infra for future admin pages
   (Devices, Alert rules) that the FE skill's Index convention expects to
   use DataTables. Bootstrap's own components only follow data-bs-theme
   (set alongside our data-theme, see layouts/main.php) for dark mode,
   not app.css's custom properties, so these force the ocean tokens
   explicitly with selectors specific enough to beat
   dataTables.bootstrap5.min.css's own (deeper-nested) rules.
   ========================================================================== */

.readings-card {
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 1rem 1.1rem 1.3rem;
    box-shadow: var(--shadow-sm);
}

table.dataTable,
table.dataTable tbody,
table.dataTable tbody tr {
    background-color: var(--bg-surface) !important;
    color: var(--text-primary);
}

table.dataTable {
    border-color: var(--border-color) !important;
}

table.dataTable thead th {
    background-color: var(--bg-surface) !important;
    border-bottom: 2px solid var(--border-color) !important;
    color: var(--text-muted) !important;
    font-size: .75rem;
    text-transform: uppercase;
    letter-spacing: .04em;
    font-weight: 600;
}

table.dataTable tbody td {
    border-top: 1px solid var(--border-color) !important;
    background-color: transparent !important;
    font-size: .85rem;
    vertical-align: middle;
}

table.dataTable.table-hover > tbody > tr:hover > * {
    background-color: var(--bg-surface-alt) !important;
    color: var(--text-primary) !important;
    box-shadow: none !important;
}

.data-chip {
    display: inline-flex;
    align-items: center;
    gap: .25rem;
    font-size: .72rem;
    background: var(--bg-surface-alt);
    border: 1px solid var(--border-color);
    padding: .1rem .45rem;
    border-radius: 999px;
    margin: 0 .2rem .2rem 0;
    color: var(--text-secondary);
}

.data-chip b { color: var(--text-primary); font-weight: 600; }

.dataTables_wrapper .dataTables_filter input,
.dataTables_wrapper .dataTables_length select {
    background-color: var(--bg-surface-alt) !important;
    border: 1px solid var(--border-color) !important;
    color: var(--text-primary) !important;
    border-radius: 6px;
}

.dataTables_wrapper .dataTables_filter input:focus,
.dataTables_wrapper .dataTables_length select:focus {
    border-color: var(--accent) !important;
    box-shadow: 0 0 0 .2rem rgba(23, 138, 158, .15) !important;
}

.dataTables_wrapper .dataTables_info,
.dataTables_wrapper .dataTables_length,
.dataTables_wrapper .dataTables_filter,
.dataTables_wrapper .dataTables_filter label {
    color: var(--text-secondary);
    font-size: .8rem;
}

.dataTables_wrapper .dataTables_paginate .page-link,
.dataTables_wrapper .pagination .page-link {
    background-color: var(--bg-surface) !important;
    border-color: var(--border-color) !important;
    color: var(--text-secondary) !important;
}

.dataTables_wrapper .dataTables_paginate .page-link:hover,
.dataTables_wrapper .pagination .page-link:hover {
    background-color: var(--bg-surface-alt) !important;
    border-color: var(--accent) !important;
    color: var(--accent) !important;
}

.dataTables_wrapper .dataTables_paginate .page-item.disabled .page-link,
.dataTables_wrapper .pagination .page-item.disabled .page-link {
    background-color: var(--bg-surface-alt) !important;
    border-color: var(--border-color) !important;
    color: var(--text-muted) !important;
}

.dataTables_wrapper .dataTables_paginate .page-item.active .page-link,
.dataTables_wrapper .pagination .page-item.active .page-link {
    background-color: var(--accent) !important;
    border-color: var(--accent) !important;
    color: #fff !important;
}

/* ==========================================================================
   History modal
   ========================================================================== */

[data-theme="dark"] .modal-content {
    background: var(--bg-surface);
    color: var(--text-primary);
    border-color: var(--border-color);
}

[data-theme="dark"] .btn-close {
    filter: invert(1) grayscale(100%) brightness(200%);
}

.history-stat { text-align: center; }
.history-stat-value { font-size: 1.1rem; font-weight: 600; color: var(--text-primary); }
.history-stat-label { font-size: .72rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: .03em; }

/* ==========================================================================
   Admin panels (Alert Rules / Devices management)
   ========================================================================== */

.panel-card {
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 1.1rem 1.25rem;
    box-shadow: var(--shadow-sm);
    margin-bottom: 1rem;
}

.panel-card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: .4rem;
}

.panel-card-header h2 {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-primary);
    margin: 0;
}

.panel-card-table {
    padding: 0;
    overflow: hidden;
}

.panel-card-table .table {
    color: var(--text-primary);
    margin-bottom: 0;
}

.panel-card-table .table > :not(caption) > * > * {
    border-bottom-color: var(--border-color);
    background-color: transparent;
}

.panel-card-table thead th {
    font-size: .72rem;
    text-transform: uppercase;
    letter-spacing: .03em;
    color: var(--text-muted);
    font-weight: 600;
    border-bottom-width: 1px;
}

.panel-card-table tbody tr:hover {
    background-color: var(--bg-surface-alt);
}

.panel-card-table td,
.panel-card-table th {
    padding: .65rem .9rem;
}
