:root {
    color-scheme: light;

    --background: #f3f5f8;
    --surface: #ffffff;
    --surface-soft: #f8fafc;
    --border: #dfe4ea;

    --text: #202936;
    --muted: #687483;

    --ok: #198754;
    --ok-soft: #e6f5ec;

    --warning: #fd7e14;
    --warning-soft: #fff0e3;

    --alert: #dc3545;
    --alert-soft: #fdebed;

    --info: #0d6efd;
    --info-soft: #e8f1ff;

    --shadow:
        0 4px 14px rgba(31, 41, 55, 0.07);
}

html[data-theme="dark"] {
    color-scheme: dark;

    --background: #111827;
    --surface: #1f2937;
    --surface-soft: #273449;
    --border: #3b4759;

    --text: #e5e7eb;
    --muted: #a8b2c1;

    --ok: #4ade80;
    --ok-soft: #163c2b;

    --warning: #fb923c;
    --warning-soft: #472915;

    --alert: #f87171;
    --alert-soft: #4a2024;

    --info: #60a5fa;
    --info-soft: #193454;

    --shadow:
        0 5px 18px rgba(0, 0, 0, 0.28);
}

html,
body {
    background: var(--background);
    color: var(--text);
}

body,
input,
select,
button {
    color: var(--text);
}

.panel,
.card,
.summary-card,
.category-card,
.chart-card,
.server-card,
.url-card,
.database-card,
.trunk-server-card,
.toolbar,
.filter-toolbar,
.system-item,
.stat {
    background: var(--surface);
    border-color: var(--border);
}

.navigation a,
.filter-clear,
.pagination a,
.pagination span,
.collapse-button,
.filter-button,
.action-button {
    background: var(--surface);
    border-color: var(--border);
}

.navigation a:hover,
.filter-clear:hover,
.pagination a:hover,
.collapse-button:hover,
.filter-button:hover,
.action-button:hover {
    background: var(--surface-soft);
}

input,
select {
    background: var(--surface);
    border-color: var(--border);
}

th,
td {
    border-color: var(--border);
}

.progress {
    background: var(--surface-soft);
}

.server-footer {
    background: var(--surface-soft);
    border-color: var(--border);
}

.theme-toggle {
    position: fixed;
    right: 18px;
    bottom: 18px;
    z-index: 9999;

    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 7px;

    min-width: 112px;
    min-height: 40px;
    padding: 8px 13px;

    border: 1px solid var(--border);
    border-radius: 999px;

    background: var(--surface);
    color: var(--text);

    box-shadow: var(--shadow);

    font: inherit;
    font-size: 13px;
    font-weight: bold;

    cursor: pointer;
}

.theme-toggle:hover {
    background: var(--surface-soft);
}

.theme-toggle-icon {
    font-size: 16px;
}

table {
    min-width: 100%;
}

.panel {
    overflow-x: auto;
}

img,
canvas,
svg {
    max-width: 100%;
}

@media (max-width: 1100px) {
    .summary-grid,
    .category-grid {
        grid-template-columns:
            repeat(2, minmax(0, 1fr)) !important;
    }

    .servers-grid,
    .trunks-grid,
    .url-grid,
    .database-grid {
        grid-template-columns:
            repeat(
                auto-fit,
                minmax(320px, 1fr)
            ) !important;
    }
}

@media (max-width: 760px) {
    body,
    .page,
    .container {
        padding-left: 12px !important;
        padding-right: 12px !important;
    }

    .header,
    .topbar {
        flex-direction: column !important;
        align-items: stretch !important;
    }

    .navigation {
        width: 100%;
    }

    .navigation a {
        flex: 1 1 auto;
        text-align: center;
    }

    .summary-grid,
    .category-grid,
    .servers-grid,
    .trunks-grid,
    .url-grid,
    .database-grid {
        grid-template-columns: 1fr !important;
    }

    .filter-toolbar,
    .toolbar {
        align-items: stretch !important;
    }

    .filter-field,
    .search-box,
    .filter-group {
        width: 100% !important;
    }

    .filter-field input,
    .filter-field select,
    .search-box {
        width: 100% !important;
        min-width: 0 !important;
    }

    .filter-actions {
        width: 100%;
    }

    .filter-submit,
    .filter-clear {
        flex: 1;
    }

    .filter-summary,
    .visible-counter {
        width: 100%;
        margin-left: 0 !important;
    }

    .system-grid {
        grid-template-columns: 1fr !important;
    }

    .server-header {
        flex-direction: column;
    }

    .server-actions {
        justify-content: space-between;
        width: 100%;
    }

    .server-footer {
        grid-template-columns: 1fr !important;
    }

    .pagination {
        flex-direction: column !important;
        align-items: stretch !important;
    }

    .pagination-links {
        justify-content: center;
    }

    th,
    td {
        padding: 9px 10px !important;
        font-size: 12px !important;
    }

    .theme-toggle {
        right: 12px;
        bottom: 12px;
        min-width: 44px;
        width: 44px;
        padding: 0;
    }

    .theme-toggle-label {
        display: none;
    }
}
/* Infrastructure Watcher: complete dark-mode overrides */

:root {
    --na: #687483;
    --na-soft: #eceff2;
    --na-border: #cfd4da;

    --table-row-border: #edf0f3;
    --progress-track: #e8ecf1;

    --chart-text: #687483;
    --chart-grid: rgba(104, 116, 131, 0.20);
    --chart-tooltip-background: rgba(32, 41, 54, 0.96);
    --chart-tooltip-text: #ffffff;

    --scrollbar-track: #e8ecf1;
    --scrollbar-thumb: #aeb5bd;
    --scrollbar-thumb-hover: #7a838d;
}

html[data-theme="dark"] {
    --na: #a8b2c1;
    --na-soft: #2a3547;
    --na-border: #4a5668;

    --table-row-border: #374357;
    --progress-track: #334055;

    --chart-text: #c7d0dd;
    --chart-grid: rgba(168, 178, 193, 0.18);
    --chart-tooltip-background: rgba(9, 14, 24, 0.96);
    --chart-tooltip-text: #f3f4f6;

    --scrollbar-track: #1b2432;
    --scrollbar-thumb: #4b586b;
    --scrollbar-thumb-hover: #68768b;
}


/* Page surfaces */

html[data-theme="dark"] body,
html[data-theme="dark"] .page,
html[data-theme="dark"] .container {
    background: var(--background);
    color: var(--text);
}

html[data-theme="dark"] .panel,
html[data-theme="dark"] .card,
html[data-theme="dark"] .summary-card,
html[data-theme="dark"] .category-card,
html[data-theme="dark"] .chart-card,
html[data-theme="dark"] .server-card,
html[data-theme="dark"] .url-card,
html[data-theme="dark"] .database-card,
html[data-theme="dark"] .trunk-server-card,
html[data-theme="dark"] .toolbar,
html[data-theme="dark"] .filter-toolbar,
html[data-theme="dark"] .system-item,
html[data-theme="dark"] .stat {
    background: var(--surface) !important;
    border-color: var(--border) !important;
    color: var(--text);
}


/* Secondary surfaces */

html[data-theme="dark"] .server-footer,
html[data-theme="dark"] .progress,
html[data-theme="dark"] .filter-clear:hover,
html[data-theme="dark"] .filter-button:hover,
html[data-theme="dark"] .pagination a:hover,
html[data-theme="dark"] .navigation a:hover,
html[data-theme="dark"] tbody tr:hover {
    background: var(--surface-soft) !important;
}


/* Tables */

html[data-theme="dark"] table {
    background: var(--surface);
    color: var(--text);
}

html[data-theme="dark"] th {
    color: var(--muted);
    background: var(--surface);
}

html[data-theme="dark"] td,
html[data-theme="dark"] th {
    border-color: var(--table-row-border) !important;
}

html[data-theme="dark"] tr {
    background: var(--surface);
}


/* Forms and controls */

html[data-theme="dark"] input,
html[data-theme="dark"] select,
html[data-theme="dark"] textarea,
html[data-theme="dark"] button {
    color: var(--text);
    border-color: var(--border);
}

html[data-theme="dark"] input,
html[data-theme="dark"] select,
html[data-theme="dark"] textarea {
    background: var(--surface) !important;
}

html[data-theme="dark"] input::placeholder,
html[data-theme="dark"] textarea::placeholder {
    color: var(--muted);
    opacity: 1;
}

html[data-theme="dark"] .navigation a,
html[data-theme="dark"] .filter-clear,
html[data-theme="dark"] .pagination a,
html[data-theme="dark"] .pagination span,
html[data-theme="dark"] .collapse-button,
html[data-theme="dark"] .filter-button,
html[data-theme="dark"] .action-button,
html[data-theme="dark"] .button {
    background: var(--surface) !important;
    border-color: var(--border) !important;
}


/* Status backgrounds */

html[data-theme="dark"] .badge-ok {
    color: var(--ok) !important;
    background: var(--ok-soft) !important;
}

html[data-theme="dark"] .badge-warning {
    color: var(--warning) !important;
    background: var(--warning-soft) !important;
}

html[data-theme="dark"] .badge-alert,
html[data-theme="dark"] .error-message {
    color: var(--alert) !important;
    background: var(--alert-soft) !important;
}

html[data-theme="dark"] .not-applicable,
html[data-theme="dark"] .not-available,
html[data-theme="dark"] .status-not-available {
    color: var(--na) !important;
}


/* Dashboard NOT AVAILABLE states */

html[data-theme="dark"] .server-card.not-available,
html[data-theme="dark"] .url-card.not-available,
html[data-theme="dark"] .trunk-server-card.not-available {
    border-color: var(--na-border) !important;
}

html[data-theme="dark"] .status-badge.not-available,
html[data-theme="dark"] .status-badge.not-applicable {
    color: var(--na) !important;
    background: var(--na-soft) !important;
}


/* Footer */

html[data-theme="dark"] .application-footer {
    color: var(--muted);
    border-color: var(--border);
}


/* Theme button: preserve current visual design */

.theme-toggle {
    background: var(--surface) !important;
    color: var(--text) !important;
    border-color: var(--border) !important;
}

.theme-toggle:hover {
    background: var(--surface-soft) !important;
}


/* Native controls */

html[data-theme="dark"] input[type="date"] {
    color-scheme: dark;
}


/* Scrollbars */

* {
    scrollbar-width: thin;
    scrollbar-color:
        var(--scrollbar-thumb)
        var(--scrollbar-track);
}

*::-webkit-scrollbar {
    width: 11px;
    height: 11px;
}

*::-webkit-scrollbar-track {
    background: var(--scrollbar-track);
}

*::-webkit-scrollbar-thumb {
    background: var(--scrollbar-thumb);
    border: 2px solid var(--scrollbar-track);
    border-radius: 999px;
}

*::-webkit-scrollbar-thumb:hover {
    background: var(--scrollbar-thumb-hover);
}

/*
 * Infrastructure Watcher complete responsive layer
 *
 * Breakpoints:
 * - Desktop:    more than 1100px
 * - Tablet:     761px to 1100px
 * - Mobile:     up to 760px
 * - Small phone: up to 480px
 */

/* Prevent children from forcing horizontal overflow. */
.page,
.container,
.header,
.topbar,
.toolbar,
.filter-toolbar,
.summary-grid,
.category-grid,
.servers-grid,
.trunks-grid,
.url-grid,
.database-grid,
.chart-card,
.server-card,
.url-card,
.database-card,
.trunk-card,
.panel {
    min-width: 0;
}

.page,
.container {
    width: 100%;
}

input,
select,
button {
    max-width: 100%;
}

/*
 * Tables remain readable instead of compressing every
 * column until their contents become unusable.
 */
.panel,
.table-wrapper,
.table-container,
.trunk-card-body,
.server-content .subsection {
    max-width: 100%;
}

.panel,
.table-wrapper,
.table-container,
.trunk-card-body {
    overflow-x: auto;
    overscroll-behavior-inline: contain;
    -webkit-overflow-scrolling: touch;
}

table {
    width: 100%;
}

/*
 * Long infrastructure values such as URLs, hostnames,
 * certificate issuers and database details may wrap.
 */
.url-address,
.server-ip,
.server-os,
.trunk-server-meta,
.system-value,
.stat-value,
.error-message,
.summary-detail {
    overflow-wrap: anywhere;
    word-break: break-word;
}


/* ======================================================
 * TABLET
 * ====================================================== */

@media (min-width: 761px) and (max-width: 1100px) {
    .page,
    .container {
        padding-left: 16px !important;
        padding-right: 16px !important;
    }

    .header,
    .topbar {
        align-items: flex-start;
        gap: 16px;
    }

    .navigation {
        justify-content: flex-end;
    }

    .summary-grid,
    .category-grid {
        grid-template-columns:
            repeat(2, minmax(0, 1fr)) !important;
    }

    .summary-grid-secondary {
        grid-template-columns:
            repeat(3, minmax(0, 1fr)) !important;
    }

    .servers-grid,
    .url-grid,
    .database-grid,
    .trunks-grid {
        grid-template-columns:
            repeat(2, minmax(0, 1fr)) !important;
    }

    /*
     * Let toolbar controls wrap naturally on tablet,
     * overriding the desktop fixed-width alignment.
     */
    .card-filter-toolbar {
        flex-wrap: wrap !important;
    }

    .card-filter-toolbar .search-box {
        flex: 1 1 100% !important;
        width: 100% !important;
        min-width: 0 !important;
    }

    .card-filter-toolbar .issues-toggle,
    .card-filter-toolbar .action-button,
    .card-filter-toolbar .visible-counter {
        flex: 0 0 auto !important;
        width: auto !important;
    }

    .card-filter-toolbar .visible-counter {
        margin-left: auto !important;
    }

    .chart-header {
        flex-direction: column;
    }

    .chart-stats {
        width: 100%;
        display: grid;
        grid-template-columns:
            repeat(5, minmax(0, 1fr));
    }

    .chart-stat {
        min-width: 0;
        padding: 10px;
        border-radius: 7px;
        background: var(--surface-soft);
    }

    .chart-container {
        height: 390px;
    }

    .server-footer {
        gap: 8px 18px;
    }
}


/* ======================================================
 * MOBILE
 * ====================================================== */

@media (max-width: 760px) {
    html {
        overflow-x: hidden;
    }

    body {
        overflow-x: hidden;
    }

    body,
    .page,
    .container {
        width: 100%;
        max-width: 100%;
        padding-left: 10px !important;
        padding-right: 10px !important;
    }

    .page {
        padding-top: 14px !important;
        padding-bottom: 78px !important;
    }

    .header,
    .topbar {
        width: 100%;
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 14px !important;
        margin-bottom: 18px;
    }

    .title {
        font-size: 25px !important;
        line-height: 1.15;
    }

    .subtitle {
        font-size: 13px !important;
        line-height: 1.45;
    }

    /*
     * Navigation becomes a two-column button grid.
     */
    .navigation {
        display: grid !important;
        grid-template-columns:
            repeat(2, minmax(0, 1fr));
        width: 100%;
        gap: 8px !important;
    }

    .navigation a {
        display: flex;
        align-items: center;
        justify-content: center;
        min-width: 0;
        min-height: 42px;
        padding: 9px 8px !important;
        text-align: center;
        line-height: 1.2;
    }

    .incident-count {
        flex: 0 0 auto;
    }

    .section-title {
        margin-top: 22px !important;
        font-size: 19px !important;
    }

    .summary-grid,
    .summary-grid-secondary,
    .category-grid,
    .servers-grid,
    .trunks-grid,
    .url-grid,
    .database-grid {
        display: grid !important;
        grid-template-columns: 1fr !important;
        gap: 11px !important;
    }

    .summary-card,
    .category-card {
        min-height: 0 !important;
        padding: 14px !important;
    }

    .summary-value {
        font-size: 24px !important;
    }

    /*
     * All filter toolbars use a predictable mobile layout.
     */
    .toolbar,
    .filter-toolbar,
    .card-filter-toolbar {
        width: 100%;
        display: flex !important;
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 10px !important;
        padding: 11px !important;
    }

    .search-box,
    .toolbar .field,
    .filter-field {
        flex: none !important;
        width: 100% !important;
        min-width: 0 !important;
    }

    .search-box,
    .toolbar .field input,
    .toolbar .field select,
    .filter-field input,
    .filter-field select {
        width: 100% !important;
        min-width: 0 !important;
        min-height: 42px;
    }

    .filter-group {
        display: grid !important;
        grid-template-columns:
            repeat(4, minmax(0, 1fr));
        width: 100% !important;
        gap: 6px !important;
    }

    .filter-button {
        min-width: 0;
        width: 100%;
        padding-left: 5px !important;
        padding-right: 5px !important;
        font-size: 12px;
    }

    .issues-toggle {
        width: 100% !important;
        min-height: 42px;
        justify-content: center;
    }

    .card-filter-toolbar .issues-toggle,
    .card-filter-toolbar .action-button,
    .card-filter-toolbar .visible-counter {
        flex: none !important;
        width: 100% !important;
    }

    .action-button {
        width: 100% !important;
        min-height: 42px;
    }

    .visible-counter,
    .filter-summary,
    .summary {
        width: 100% !important;
        margin-left: 0 !important;
        text-align: center !important;
        white-space: normal !important;
        line-height: 1.45;
    }

    .filter-actions {
        display: flex;
        width: 100%;
        gap: 8px;
    }

    .filter-submit,
    .filter-clear {
        flex: 1 1 0;
        min-width: 0;
        text-align: center;
    }

    /*
     * URL and database cards.
     */
    .url-card,
    .database-card {
        width: 100%;
        padding: 13px !important;
    }

    .url-header {
        align-items: flex-start !important;
    }

    .url-name {
        min-width: 0;
        font-size: 16px !important;
        overflow-wrap: anywhere;
    }

    .status-badge {
        flex: 0 0 auto;
    }

    .url-stats {
        grid-template-columns: 1fr !important;
        gap: 8px !important;
    }

    .stat {
        min-width: 0;
    }

    /*
     * Servers.
     */
    .server-card {
        width: 100%;
    }

    .server-header {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 10px;
        padding: 13px !important;
    }

    .server-title-area {
        min-width: 0;
    }

    .server-name {
        overflow-wrap: anywhere;
    }

    .server-actions {
        display: flex;
        justify-content: space-between;
        width: 100%;
    }

    .collapse-button {
        min-width: 42px;
        min-height: 38px;
    }

    .server-body {
        padding: 13px !important;
    }

    .system-grid {
        grid-template-columns: 1fr !important;
        gap: 8px !important;
    }

    .system-item {
        min-width: 0;
    }

    .metric-header {
        gap: 8px;
    }

    /*
     * Footers stack vertically and remain readable.
     */
    .server-footer {
        display: grid !important;
        grid-template-columns: 1fr !important;
        gap: 5px !important;
        padding: 11px 13px !important;
        line-height: 1.4;
    }

    /*
     * Trunks.
     */
    .trunk-card,
    .trunk-server-card {
        width: 100%;
    }

    .trunk-card-header {
        flex-direction: column;
        align-items: stretch !important;
        gap: 9px;
    }

    .trunk-title-area {
        min-width: 0;
    }

    .trunks-table {
        min-width: 520px;
    }

    /*
     * General tables use horizontal scrolling.
     */
    .panel table,
    .table-wrapper table,
    .table-container table,
    .server-content table {
        min-width: 650px;
    }

    th,
    td {
        padding: 9px 10px !important;
        font-size: 12px !important;
        white-space: nowrap;
    }

    td:last-child {
        white-space: normal;
        min-width: 180px;
    }

    .sortable-table th.sortable-column {
        padding-right: 26px !important;
    }

    /*
     * Metrics.
     */
    .chart-card {
        width: 100%;
        padding: 13px !important;
    }

    .chart-header {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 12px;
    }

    .chart-title {
        font-size: 17px !important;
        overflow-wrap: anywhere;
    }

    .chart-stats {
        display: grid !important;
        grid-template-columns:
            repeat(2, minmax(0, 1fr)) !important;
        width: 100%;
        gap: 8px !important;
    }

    .chart-stat {
        min-width: 0 !important;
        padding: 10px !important;
        border-radius: 7px;
        background: var(--surface-soft);
    }

    .chart-stat:last-child {
        grid-column: 1 / -1;
    }

    .chart-stat-value {
        font-size: 16px !important;
        overflow-wrap: anywhere;
    }

    .chart-container {
        height: 320px !important;
        min-height: 320px;
    }

    /*
     * Pagination remains usable on narrow screens.
     */
    .pagination {
        display: flex !important;
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 10px;
    }

    .pagination-links {
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
        gap: 5px;
    }

    /*
     * Floating theme button becomes icon-only and leaves
     * sufficient space above the viewport edge.
     */
    .theme-toggle {
        right: 12px !important;
        bottom: 12px !important;
        width: 46px !important;
        min-width: 46px !important;
        height: 46px;
        min-height: 46px;
        padding: 0 !important;
    }

    .theme-toggle-label {
        display: none !important;
    }

    .application-footer {
        padding-left: 52px !important;
        padding-right: 52px !important;
        line-height: 1.5;
    }
}


/* ======================================================
 * SMALL PHONES
 * ====================================================== */

@media (max-width: 480px) {
    body,
    .page,
    .container {
        padding-left: 8px !important;
        padding-right: 8px !important;
    }

    .navigation {
        grid-template-columns: 1fr;
    }

    .navigation a {
        min-height: 40px;
    }

    .filter-group {
        grid-template-columns:
            repeat(2, minmax(0, 1fr));
    }

    .chart-stats {
        grid-template-columns: 1fr !important;
    }

    .chart-stat:last-child {
        grid-column: auto;
    }

    .chart-container {
        height: 280px !important;
        min-height: 280px;
    }

    .summary-card,
    .url-card,
    .database-card,
    .chart-card {
        border-radius: 8px !important;
    }

    .title {
        font-size: 23px !important;
    }
}
