:root {
    color-scheme: light dark;
    --sidebar-width: 220px;
    --page-bg: #eaf1ff;
    --surface-bg: #f8fbff;
    --surface-muted: #edf4ff;
    --text-main: #1f2937;
    --text-muted: #6b7280;
    --border-color: #c8d8f0;

    --sidebar-bg: #e4eeff;
    --sidebar-text: #0f172a;
    --sidebar-text-muted: #1e3a5f;
    --sidebar-hover-bg: #d6e6ff;
    --sidebar-hover-text: #0f172a;
    --sidebar-active: #0b5ed7;

    --link-color: #0b5ed7;
    --link-hover: #0846a9;

    --shadow-sm: 0 0.15rem 1.75rem rgba(58, 59, 69, 0.15);
    --shadow-lg: 0 10px 20px rgba(0, 0, 0, 0.1);
}

@media (prefers-color-scheme: dark) {
    :root {
        --page-bg: #0f141a;
        --surface-bg: #161d26;
        --surface-muted: #1d2631;
        --text-main: #e5e7eb;
        --text-muted: #9ca3af;
        --border-color: #2f3b4a;

        --sidebar-bg: #111820;
        --sidebar-text: #e5e7eb;
        --sidebar-text-muted: #c7ced8;
        --sidebar-hover-bg: #273445;
        --sidebar-hover-text: #ffffff;
        --sidebar-active: #ffd54d;

        --link-color: #7cb8ff;
        --link-hover: #a6ceff;

        --shadow-sm: 0 0.2rem 1.5rem rgba(0, 0, 0, 0.45);
        --shadow-lg: 0 12px 24px rgba(0, 0, 0, 0.45);
    }
}

:root[data-theme="light"] {
    --page-bg: #eaf1ff;
    --surface-bg: #f8fbff;
    --surface-muted: #edf4ff;
    --text-main: #1f2937;
    --text-muted: #6b7280;
    --border-color: #c8d8f0;
    --sidebar-bg: #e4eeff;
    --sidebar-text: #0f172a;
    --sidebar-text-muted: #1e3a5f;
    --sidebar-hover-bg: #d6e6ff;
    --sidebar-hover-text: #0f172a;
    --sidebar-active: #0b5ed7;
    --link-color: #0b5ed7;
    --link-hover: #0846a9;
    --shadow-sm: 0 0.15rem 1.75rem rgba(58, 59, 69, 0.15);
    --shadow-lg: 0 10px 20px rgba(0, 0, 0, 0.1);
}

:root[data-theme="dark"] {
    --page-bg: #0f141a;
    --surface-bg: #161d26;
    --surface-muted: #1d2631;
    --text-main: #e5e7eb;
    --text-muted: #9ca3af;
    --border-color: #2f3b4a;
    --sidebar-bg: #111820;
    --sidebar-text: #e5e7eb;
    --sidebar-text-muted: #c7ced8;
    --sidebar-hover-bg: #273445;
    --sidebar-hover-text: #ffffff;
    --sidebar-active: #ffd54d;
    --link-color: #7cb8ff;
    --link-hover: #a6ceff;
    --shadow-sm: 0 0.2rem 1.5rem rgba(0, 0, 0, 0.45);
    --shadow-lg: 0 12px 24px rgba(0, 0, 0, 0.45);
}

html,
body {
    height: 100%;
    margin: 0;
    background: var(--page-bg);
    color: var(--text-main);
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
}

.wrapper {
    display: flex;
    width: 100%;
    max-width: 100vw;
    overflow-x: hidden;
}

.container-fluid {
    width: 100% !important;
    max-width: 100% !important;
}

.sidebar {
    width: var(--sidebar-width);
    background: var(--sidebar-bg) !important;
    color: var(--sidebar-text) !important;
    min-height: 100vh;
    transition: all 0.3s ease-in-out;
    position: fixed;
    z-index: 1050;
}

#mainSidebarMobile {
    background: var(--sidebar-bg) !important;
    color: var(--sidebar-text) !important;
}

.sidebar-brand {
    padding: 1rem;
    text-align: center;
    border-bottom: 1px solid rgba(255, 255, 255, 0.12);
}

.sidebar .nav-link,
.sidebar-link {
    color: var(--sidebar-text-muted) !important;
    transition: all 0.2s ease-in-out;
}

#mainSidebarMobile .nav-link,
#mainSidebarMobile .sidebar-link {
    color: var(--sidebar-text-muted) !important;
}

.sidebar .nav-link:hover,
.sidebar-link:hover {
    background-color: var(--sidebar-hover-bg);
    color: var(--sidebar-hover-text) !important;
    border-radius: 4px;
}

#mainSidebarMobile .nav-link:hover,
#mainSidebarMobile .sidebar-link:hover {
    background-color: var(--sidebar-hover-bg);
    color: var(--sidebar-hover-text) !important;
    border-radius: 4px;
}

.sidebar .nav-link.active,
.sidebar .nav-link.active-parent {
    color: var(--sidebar-active) !important;
    font-weight: 600;
}

#mainSidebarMobile .nav-link.active,
#mainSidebarMobile .nav-link.active-parent {
    color: var(--sidebar-active) !important;
    font-weight: 600;
}

.main {
    margin-left: var(--sidebar-width);
    width: calc(100% - var(--sidebar-width));
    max-width: calc(100vw - var(--sidebar-width));
    box-sizing: border-box;
    padding: 20px;
    background: var(--page-bg);
    min-height: 100vh;
    overflow-x: hidden;
}

.topbar,
.navbar.topbar {
    background-color: var(--surface-bg) !important;
    color: var(--text-main) !important;
    border: 1px solid var(--border-color);
    overflow: visible;
}

.topbar h4,
.topbar .nav-link,
.topbar .dropdown-toggle,
.topbar .small {
    color: var(--text-main) !important;
}

.theme-option.active,
.theme-option:active {
    background: var(--link-color);
    color: #fff;
}

.dropdown-menu {
    background: var(--surface-bg);
    color: var(--text-main);
    border: 1px solid var(--border-color);
    z-index: 3000;
    width: min(360px, calc(100vw - 16px));
    max-width: calc(100vw - 16px);
    overflow-x: hidden;
    box-sizing: border-box;
}

.dropdown-item {
    color: var(--text-main);
    white-space: normal;
    overflow-wrap: anywhere;
    word-break: break-word;
}

.dropdown-item:hover,
.dropdown-item:focus {
    background: var(--surface-muted);
    color: var(--text-main);
}

.dropdown-divider {
    border-top-color: var(--border-color);
}

.dropdown-menu-end[data-bs-popper] {
    right: 0;
    left: auto;
}

.user-dropdown-menu {
    right: 0 !important;
    left: auto !important;
    transform-origin: top right;
}

.card,
.auth-card {
    background: var(--surface-bg);
    color: var(--text-main);
    border: 1px solid var(--border-color);
    border-radius: 0.35rem;
    box-shadow: var(--shadow-sm);
    transition: all 0.3s ease;
}

.card:hover {
    transform: translateY(-5px);
    box-shadow: var(--shadow-lg);
}

.card-header {
    border-bottom: 1px solid var(--border-color);
    background: var(--surface-muted);
}

.card-header h6,
.card-header .font-weight-bold,
.card-header .m-0 {
    color: var(--sidebar-active) !important;
}

.table,
.table-responsive {
    color: var(--text-main);
}

.table-responsive {
    overflow-x: auto;
}

/* DataTables global theme integration */
.dataTables_wrapper,
.dataTables_wrapper .dataTables_info,
.dataTables_wrapper .dataTables_length,
.dataTables_wrapper .dataTables_filter {
    color: var(--text-main) !important;
}

.dataTables_wrapper .dataTables_length label,
.dataTables_wrapper .dataTables_filter label {
    margin: 0;
    font-weight: 500;
}

.dataTables_wrapper .dataTables_length select,
.dataTables_wrapper .dataTables_filter input {
    background: var(--surface-bg) !important;
    color: var(--text-main) !important;
    border: 1px solid var(--border-color) !important;
    border-radius: 4px !important;
    min-height: 32px;
}

table.dataTable,
table.dataTable thead th,
table.dataTable tbody td {
    color: var(--text-main) !important;
    border-color: var(--border-color) !important;
}

.table,
table.dataTable {
    --bs-table-bg: var(--surface-bg);
    --bs-table-color: var(--text-main);
    --bs-table-border-color: var(--border-color);
    --bs-table-striped-bg: color-mix(in srgb, var(--surface-bg) 92%, var(--surface-muted) 8%);
    --bs-table-striped-color: var(--text-main);
    --bs-table-hover-bg: color-mix(in srgb, var(--surface-muted) 82%, var(--surface-bg) 18%);
    --bs-table-hover-color: var(--text-main);
}

.table > :not(caption) > * > *,
table.dataTable > :not(caption) > * > * {
    background-color: var(--bs-table-bg) !important;
    color: var(--bs-table-color) !important;
    border-color: var(--bs-table-border-color) !important;
    box-shadow: none !important;
}

.dataTables_wrapper .dataTables_paginate .paginate_button {
    color: var(--text-main) !important;
    border: 1px solid var(--border-color) !important;
    background: var(--surface-bg) !important;
    border-radius: 4px;
}

.dataTables_wrapper .dataTables_paginate .paginate_button.current,
.dataTables_wrapper .dataTables_paginate .paginate_button.current:hover {
    color: #fff !important;
    border-color: var(--link-color) !important;
    background: var(--link-color) !important;
}

.dataTables_wrapper .dataTables_paginate .paginate_button:hover {
    color: var(--text-main) !important;
    border-color: var(--link-color) !important;
    background: var(--surface-muted) !important;
}

.dataTables_wrapper .dataTables_paginate .paginate_button.disabled,
.dataTables_wrapper .dataTables_paginate .paginate_button.disabled:hover {
    color: var(--text-muted) !important;
    background: color-mix(in srgb, var(--surface-bg) 75%, transparent) !important;
    border-color: var(--border-color) !important;
}

.dataTables_wrapper .dataTables_paginate .pagination {
    margin: 0;
    gap: 0;
}

.dataTables_wrapper .dataTables_paginate .page-item .page-link {
    background: var(--surface-bg);
    color: var(--text-main);
    border-color: var(--border-color);
}

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

.dataTables_wrapper .dataTables_paginate .page-item .page-link:hover,
.dataTables_wrapper .dataTables_paginate .page-item .page-link:focus {
    background: var(--surface-muted);
    color: var(--text-main);
    border-color: var(--link-color);
    box-shadow: none;
}

.dataTables_wrapper .dataTables_paginate .page-item.disabled .page-link {
    background: color-mix(in srgb, var(--surface-bg) 75%, transparent);
    color: var(--text-muted);
    border-color: var(--border-color);
}

.badge {
    font-weight: 500;
}

.alert.alert-info {
    color: color-mix(in srgb, #0c4a6e 72%, var(--text-main) 28%);
    background: color-mix(in srgb, #0ea5e9 16%, var(--surface-bg) 84%);
    border: 1px solid color-mix(in srgb, #0ea5e9 38%, var(--border-color) 62%);
}

.diagnostic-page .diag-result {
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 12px 14px;
}

.diagnostic-page .diag-result.alert-success {
    color: color-mix(in srgb, #198754 28%, var(--text-main) 72%);
    background: color-mix(in srgb, #198754 12%, var(--surface-bg) 88%);
    border-color: color-mix(in srgb, #198754 30%, var(--border-color) 70%);
}

.diagnostic-page .diag-result.alert-warning {
    color: color-mix(in srgb, #f59e0b 32%, var(--text-main) 68%);
    background: color-mix(in srgb, #f59e0b 12%, var(--surface-bg) 88%);
    border-color: color-mix(in srgb, #f59e0b 32%, var(--border-color) 68%);
}

.diagnostic-page .diag-title {
    margin-bottom: 8px;
}

.diagnostic-page .diag-meta {
    color: var(--text-muted) !important;
    margin-top: 8px;
    margin-bottom: 8px;
    line-height: 1.4;
}

.diagnostic-page .diag-pre {
    white-space: pre-wrap;
    word-break: break-word;
    overflow-wrap: anywhere;
    background: color-mix(in srgb, var(--surface-muted) 78%, transparent);
    color: var(--text-main);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    padding: 10px 12px;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
    line-height: 1.4;
}

.diagnostic-page input:not([type="checkbox"]):not([type="radio"]),
.diagnostic-page select,
.diagnostic-page textarea,
.diagnostic-page .form-control,
.diagnostic-page .form-select {
    background-color: var(--surface-bg) !important;
    color: var(--text-main) !important;
    border-color: var(--border-color) !important;
}

.diagnostic-page input:not([type="checkbox"]):not([type="radio"]):focus,
.diagnostic-page select:focus,
.diagnostic-page textarea:focus,
.diagnostic-page .form-control:focus,
.diagnostic-page .form-select:focus {
    background-color: var(--surface-bg) !important;
    color: var(--text-main) !important;
    border-color: var(--link-color) !important;
}

.diagnostic-page input:-webkit-autofill,
.diagnostic-page input:-webkit-autofill:hover,
.diagnostic-page input:-webkit-autofill:focus,
.diagnostic-page textarea:-webkit-autofill,
.diagnostic-page select:-webkit-autofill {
    -webkit-text-fill-color: var(--text-main) !important;
    -webkit-box-shadow: 0 0 0px 1000px var(--surface-bg) inset !important;
    transition: background-color 9999s ease-out 0s;
}

a {
    color: var(--link-color);
}

a:hover {
    color: var(--link-hover);
}

.main-footer {
    margin-top: auto;
    margin-left: var(--sidebar-width);
    width: calc(100% - var(--sidebar-width));
    max-width: calc(100vw - var(--sidebar-width));
    box-sizing: border-box;
    background-color: var(--surface-muted);
    color: var(--text-main);
    padding: 8px 0;
    text-align: center;
    border-top: 1px solid var(--border-color);
}

.footer-content {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 10px;
}

.footer-section {
    flex: 1;
    min-width: 200px;
    margin-bottom: 6px;
}

.footer-section h3 {
    font-size: 0.95rem;
    font-weight: bold;
    margin: 2px 0 4px;
}

.footer-section p {
    margin: 2px 0;
    font-size: 0.85rem;
}

.footer-section ul {
    list-style-type: none;
    padding: 0;
}

.footer-section ul li {
    margin-bottom: 4px;
}

.footer-section ul li a {
    text-decoration: none;
    color: var(--link-color);
    font-size: 0.85rem;
}

.footer-section ul li a:hover {
    text-decoration: underline;
    color: var(--link-hover);
}

input:not([type="checkbox"]):not([type="radio"]),
select,
textarea,
.form-control,
.form-select {
    background-color: var(--surface-bg) !important;
    color: var(--text-main) !important;
    border-color: var(--border-color) !important;
    caret-color: var(--text-main);
}

input:not([type="checkbox"]):not([type="radio"]):focus,
select:focus,
textarea:focus,
.form-control:focus,
.form-select:focus {
    background-color: var(--surface-bg) !important;
    color: var(--text-main) !important;
    border-color: var(--link-color) !important;
    box-shadow: 0 0 0 0.2rem color-mix(in srgb, var(--link-color) 25%, transparent) !important;
}

input:not([type="checkbox"]):not([type="radio"]):disabled,
select:disabled,
textarea:disabled,
.form-control:disabled,
.form-select:disabled,
.form-control[readonly] {
    background-color: color-mix(in srgb, var(--surface-muted) 86%, var(--surface-bg) 14%) !important;
    color: color-mix(in srgb, var(--text-main) 78%, var(--text-muted) 22%) !important;
    border-color: var(--border-color) !important;
    opacity: 1;
}

.form-switch .form-check-input {
    width: 2.35rem;
    height: 1.2rem;
    border: 1px solid color-mix(in srgb, var(--border-color) 85%, var(--text-muted) 15%);
    background-color: color-mix(in srgb, var(--surface-muted) 80%, var(--text-muted) 20%);
}

.form-switch .form-check-input:checked {
    background-color: var(--link-color);
    border-color: var(--link-color);
}

.form-switch .form-check-input:focus {
    border-color: var(--link-color);
    box-shadow: 0 0 0 0.2rem color-mix(in srgb, var(--link-color) 28%, transparent);
}

:root {
    --placeholder-color: #a309eb;
}

input::placeholder,
textarea::placeholder,
.form-control::placeholder {
    color: var(--placeholder-color) !important;
    opacity: 1 !important;
}

input::-webkit-input-placeholder,
textarea::-webkit-input-placeholder,
.form-control::-webkit-input-placeholder {
    color: var(--placeholder-color) !important;
    opacity: 1 !important;
}

input::-moz-placeholder,
textarea::-moz-placeholder,
.form-control::-moz-placeholder {
    color: var(--placeholder-color) !important;
    opacity: 1 !important;
}

input:-ms-input-placeholder,
textarea:-ms-input-placeholder,
.form-control:-ms-input-placeholder {
    color: var(--placeholder-color) !important;
    opacity: 1 !important;
}

.modal-content,
.modal-header,
.modal-body,
.modal-footer {
    background: var(--surface-bg);
    color: var(--text-main);
    border-color: var(--border-color);
}

.modal-body canvas {
    width: 100% !important;
    height: 400px !important;
}

.system-card {
    width: 100%;
    margin-left: auto;
    margin-right: auto;
    padding: 12px;
    font-size: 0.92rem;
}

.system-card h2 {
    margin: 0 0 8px;
    font-size: 1.08rem;
}

.system-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(180px, 1fr));
    gap: 16px;
}

.metric-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

.metric-ring {
    --p: 0;
    --ring-track: color-mix(in srgb, var(--text-muted) 30%, transparent);
    --ring-fill: color-mix(in srgb, var(--link-color) 75%, #25d0a6 25%);
    width: 140px;
    height: 140px;
    border-radius: 50%;
    position: relative;
    background:
        radial-gradient(closest-side, var(--surface-bg) 78%, transparent 79% 100%),
        conic-gradient(var(--ring-fill) calc(var(--p) * 1%), var(--ring-track) 0);
    display: grid;
    place-items: center;
}

.metric-ring::before {
    content: '';
    position: absolute;
    inset: -10px;
    border-radius: 50%;
    border: 1px dashed color-mix(in srgb, var(--text-muted) 35%, transparent);
    opacity: 0.5;
}

.metric-value {
    font-size: 1.65rem;
    font-weight: 700;
    color: var(--text-main);
    z-index: 1;
}

.metric-label {
    margin-top: 10px;
    text-transform: uppercase;
    font-size: 0.72rem;
    letter-spacing: 0.04em;
    font-weight: 700;
    color: var(--text-muted);
}

.system-uptime {
    margin: 10px 0 0;
    font-size: 0.86rem;
    color: var(--text-main);
}

.system-chart-grid {
    width: 100%;
    margin-left: auto;
    margin-right: auto;
    display: grid;
    grid-template-columns: repeat(5, minmax(150px, 1fr));
    gap: 10px;
}

.metric-graph-card {
    padding: 8px;
}

.metric-graph-card h3 {
    margin: 0 0 6px;
    font-size: 0.78rem;
    font-weight: 700;
}

.metric-graph-card canvas {
    width: 100%;
    height: 80px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    background: color-mix(in srgb, var(--surface-muted) 65%, transparent);
}

.graph-value {
    margin: 5px 0 0;
    font-size: 0.7rem;
    color: var(--text-muted);
}

.graph-legend {
    margin-top: 6px;
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    font-size: 0.68rem;
    color: var(--text-muted);
}

.legend-item {
    display: inline-flex;
    align-items: center;
    gap: 5px;
}

.legend-color {
    width: 10px;
    height: 10px;
    border-radius: 2px;
    background: var(--legend-color, #0b5ed7);
    border: 1px solid color-mix(in srgb, var(--legend-color, #0b5ed7) 80%, #ffffff 20%);
}

.mini-graph-card {
    cursor: pointer;
}

.mini-graph-card:hover {
    transform: translateY(-3px);
}

#systemGraphModal .modal-content {
    border-radius: 0;
    border: none;
    background: color-mix(in srgb, var(--surface-bg) 86%, #000 14%);
    color: var(--text-main);
}

#systemGraphModal .modal-header {
    border-bottom: 1px solid color-mix(in srgb, var(--border-color) 65%, transparent);
    padding: 18px 26px 14px;
}

#systemGraphModal .modal-title {
    font-size: 2rem;
    font-weight: 700;
    letter-spacing: 0.01em;
    color: var(--text-main);
}

#systemGraphModal .modal-body {
    padding: 14px 26px 22px;
}

#systemGraphModal .btn-close {
    filter: none;
    opacity: 0.95;
    border: 1px solid color-mix(in srgb, var(--border-color) 80%, #fff 20%);
    border-radius: 10px;
    padding: 12px;
    box-shadow: 0 0 0 1px color-mix(in srgb, var(--border-color) 65%, transparent) inset;
}

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

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) #systemGraphModal .btn-close {
        filter: invert(1) grayscale(100%);
    }
}

#systemGraphModal .btn-close:hover {
    opacity: 1;
    border-color: color-mix(in srgb, var(--link-color) 70%, #fff 30%);
}

#systemGraphModal .modal-body canvas {
    width: 100% !important;
    height: calc(100vh - 165px) !important;
    max-height: calc(100vh - 165px);
    border: 1px solid color-mix(in srgb, var(--border-color) 70%, transparent);
    border-radius: 12px;
    background: color-mix(in srgb, var(--surface-muted) 70%, #000 30%);
}

#systemGraphModalCanvas {
    width: 100% !important;
    height: calc(100vh - 120px) !important;
    max-height: calc(100vh - 120px);
}

body.system-graph-open .modal-backdrop.show {
    opacity: 0.78;
    background: color-mix(in srgb, var(--page-bg) 25%, #000 75%);
}

.graph-legend-modal {
    margin-top: 12px;
    font-size: 0.9rem;
    color: var(--text-main);
}

.graph-value-modal {
    margin-top: 8px;
    font-size: 0.92rem;
    color: var(--text-main);
    font-weight: 600;
}


@media (max-width: 768px) {
    .sidebar {
        margin-left: calc(-1 * var(--sidebar-width));
    }

    .main {
        width: 100%;
        max-width: 100vw;
        margin-left: 0;
    }

    .sidebar.active {
        margin-left: 0;
    }

    .footer-content {
        flex-direction: column;
        align-items: center;
        gap: 6px;
    }

    .footer-section {
        min-width: auto;
        text-align: center;
        margin-bottom: 4px;
    }

    .main-footer {
        margin-left: 0;
        width: 100%;
        max-width: 100vw;
    }

    .system-grid {
        grid-template-columns: 1fr;
        gap: 16px;
    }

    .system-card,
    .system-chart-grid {
        width: 100%;
    }

    .system-chart-grid {
        grid-template-columns: repeat(2, minmax(140px, 1fr));
    }

    .metric-ring {
        width: 160px;
        height: 160px;
    }

    .metric-value {
        font-size: 1.7rem;
    }

}
