* { box-sizing: border-box; }
:root {
    --bg-body: #f5f7fb;
    --text-main: #1f2933;
    --header-bg: #101828;
    --header-text: #ffffff;
    --nav-link-bg: rgba(255,255,255,0.08);
    --nav-link-color: rgba(255,255,255,0.88);
    --nav-link-active-bg: #ffffff;
    --nav-link-active-color: #101828;
    --card-bg: #ffffff;
    --card-shadow: 0 12px 24px rgba(0,0,0,0.05);
    --muted-color: #5f6b7c;
    --error-bg: #fde5e1;
    --error-text: #f82836;
    --success-bg: #d1fadf;
    --success-text: #05603a;
    --table-border: #e5e7eb;
    --table-head-bg: #f8fafc;
    --code-bg: #f1f5f9;
    --stat-bg: #eef2ff;
    --badge-bg: #eef2ff;
    --badge-text: #3730a3;
    --icon-btn-bg: #e0e7ff;
    --icon-btn-hover: #c7d2fe;
    --modal-bg: #ffffff;
    --modal-text: #475467;
    --log-bg: #0f172a;
    --log-text: #e2e8f0;
    --auth-bg: #101828;
    --auth-card-bg: #ffffff;
    --auth-card-shadow: 0 20px 45px rgba(15,23,42,0.25);
    --input-bg: #ffffff;
    --input-border: #d0d5dd;
    --svg-icon-color: #1f2933;
}
:root.theme-dark {
    --bg-body: #0f172a;
    --text-main: #e2e8f0;
    --header-bg: #0b1220;
    --header-text: #e2e8f0;
    --nav-link-bg: rgba(255,255,255,0.05);
    --nav-link-color: rgba(255,255,255,0.85);
    --nav-link-active-bg: #1d4ed8;
    --nav-link-active-color: #ffffff;
    --card-bg: #182235;
    --card-shadow: 0 12px 24px rgba(0,0,0,0.4);
    --muted-color: #94a3b8;
    --error-bg: rgba(180,35,24,0.18);
    --error-text: #fca5a5;
    --success-bg: rgba(5,96,58,0.25);
    --success-text: #bbf7d0;
    --table-border: rgba(148,163,184,0.3);
    --table-head-bg: rgba(148,163,184,0.12);
    --code-bg: rgba(15,23,42,0.6);
    --stat-bg: rgba(99,102,241,0.2);
    --badge-bg: rgba(59,130,246,0.15);
    --badge-text: #dbeafe;
    --icon-btn-bg: rgba(99,102,241,0.35);
    --icon-btn-hover: rgba(99,102,241,0.55);
    --modal-bg: #1f2937;
    --modal-text: #d1d5db;
    --log-bg: #050a15;
    --log-text: #e2e8f0;
    --auth-bg: #050a15;
    --auth-card-bg: #111827;
    --auth-card-shadow: 0 20px 45px rgba(0,0,0,0.6);
    --input-bg: #0f172a;
    --input-border: #1f2937;
    --svg-icon-color: #e2e8f0;
}
html {
    scroll-behavior: smooth;
    font-size: 16px;
    background: var(--header-bg);
}
body {
    margin: 0;
    padding: 0;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: var(--bg-body);
    color: var(--text-main);
    transition: background 0.2s ease, color 0.2s ease;
}
body.is-modal-open {
    overflow: hidden;
}
.admin-header {
    background: var(--header-bg);
    color: var(--header-text);
    padding: 16px 20px;
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    gap: 16px;
    align-items: center;
}
.admin-header h1 {
    margin: 0;
    font-size: 20px;
    white-space: nowrap;
}
.admin-header nav {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
    min-width: 0;
}
.admin-header nav a {
    display: inline-flex;
    align-items: center;
    color: var(--nav-link-color);
    text-decoration: none;
    padding: 6px 12px;
    border-radius: 8px;
    background: var(--nav-link-bg);
    transition: background 0.15s ease;
}
.admin-header nav a.active {
    background: var(--nav-link-active-bg);
    color: var(--nav-link-active-color);
}
.header-actions {
    display: flex;
    align-items: center;
    gap: 12px;
    justify-self: end;
    flex-wrap: wrap;
    justify-content: flex-end;
}
.user-meta {
    display: flex;
    align-items: center;
    gap: 12px;
    white-space: nowrap;
}
.logout-btn {
    background: none;
    border: none;
    color: inherit;
    padding: 4px 8px;
    border-radius: 8px;
    cursor: pointer;
}
.logout-btn:hover {
    background: rgba(255,255,255,0.1);
}
.theme-toggle {
    background: none;
    border: 1px solid rgba(255,255,255,0.4);
    color: inherit;
    border-radius: 999px;
    width: 40px;
    height: 40px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.15s ease, border-color 0.15s ease;
}
.theme-toggle:hover,
.theme-toggle:focus-visible {
    border-color: rgba(255,255,255,0.7);
    background: rgba(255,255,255,0.08);
}
.theme-toggle-icon {
    font-size: 18px;
    line-height: 1;
}
main {
    width: 100%;
    max-width: 1180px;
    margin: 0 auto;
    padding: 32px 20px 64px;
}
.card {
    background: var(--card-bg);
    border-radius: 12px;
    padding: 24px;
    margin-top: 24px;
    box-shadow: var(--card-shadow);
    transition: background 0.2s ease, box-shadow 0.2s ease;
}
.card h2 {
    margin-top: 0;
    margin-bottom: 14px;
    font-size: 20px;
}
.card-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 16px;
    flex-wrap: wrap;
}
.card-header form {
    margin-left: auto;
}
.stat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    gap: 16px;
    margin-top: 12px;
}
.stat {
    padding: 16px;
    border-radius: 10px;
    background: var(--stat-bg);
}
.stat .label {
    display: block;
    font-size: 14px;
    color: var(--muted-color);
    margin-bottom: 6px;
}
.stat .value {
    font-size: 26px;
    font-weight: 600;
}
.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
    gap: 32px;
}
.form {
    display: flex;
    flex-direction: column;
    gap: 12px;
}
label {
    display: flex;
    flex-direction: column;
    font-size: 14px;
    font-weight: 600;
    color: var(--text-main);
    gap: 6px;
}
label.checkbox {
    flex-direction: row;
}
input,
textarea,
select {
    padding: 10px 12px;
    border: 1px solid var(--input-border);
    border-radius: 8px;
    font-size: 14px;
    background: var(--input-bg);
    color: inherit;
}
input:focus,
textarea:focus,
select:focus {
    outline: 2px solid #6366f1;
    border-color: transparent;
}
button {
    padding: 11px 16px;
    border: none;
    border-radius: 8px;
    font-weight: 600;
    background: #1d4ed8;
    color: #fff;
    cursor: pointer;
}
.muted {
    color: var(--muted-color);
    font-size: 14px;
}
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}
.error {
    color: var(--error-text);
    font-size: 14px;
    background: var(--error-bg);
    padding: 12px;
    border-radius: 8px;
}
.success {
    color: var(--success-text);
    background: var(--success-bg);
    padding: 12px;
    border-radius: 8px;
    font-size: 14px;
}
.flash-messages {
    position: fixed;
    top: 50px;
    right: 20px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    z-index: 120;
    pointer-events: none;
}
.flash-message {
    min-width: 280px;
    max-width: min(380px, calc(100vw - 32px));
    background: var(--card-bg);
    color: var(--text-main);
    border-radius: 12px;
    padding: 14px 18px;
    box-shadow: 0 15px 35px rgba(15, 23, 42, 0.25);
    display: flex;
    align-items: flex-start;
    gap: 12px;
    pointer-events: auto;
    opacity: 1;
    transform: translateY(0);
    transition: opacity 0.3s ease, transform 0.3s ease;
}
.flash-message.flash-error {
    border-left: 8px solid var(--error-text);
}
.flash-message.flash-success {
    border-left: 8px solid var(--success-text);
}
.flash-message.is-hiding {
    opacity: 0;
    transform: translateY(-10px);
}
.flash-message-text {
    flex: 1;
    font-size: 14px;
}
.flash-close {
    background: none;
    border: none;
    color: inherit;
    font-size: 20px;
    line-height: 1;
    cursor: pointer;
    padding: 0;
}
.flash-close:hover,
.flash-close:focus-visible {
    color: var(--nav-link-active-color);
}
.table-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}
.table-wrapper table {
    min-width: 640px;
}
table {
    width: 100%;
    border-collapse: collapse;
}
th,
td {
    text-align: left;
    padding: 10px 12px;
    border-bottom: 1px solid var(--table-border);
    font-size: 14px;
    vertical-align: middle;
}
th {
    background: var(--table-head-bg);
    font-weight: 600;
}
code {
    font-size: 13px;
    background: var(--code-bg);
    padding: 2px 4px;
    border-radius: 4px;
}
.firmware-block {
    border: 1px solid var(--table-border);
    border-radius: 10px;
    padding: 16px;
    margin-top: 18px;
}
.firmware-heading {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
}
.badge {
    display: inline-block;
    padding: 2px 10px;
    border-radius: 999px;
    background: var(--badge-bg);
    color: var(--badge-text);
    font-weight: 600;
    font-size: 13px;
    margin-right: 8px;
}
.actions {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}
.chip-info {
    display: flex;
    flex-direction: column;
    gap: 4px;
}
.inline-form { display: inline; }
.link-btn {
    background: none;
    border: none;
    color: #2563eb;
    padding: 0;
    cursor: pointer;
    font-weight: 600;
}
button.icon-btn {
    width: 38px;
    height: 38px;
    border-radius: 999px;
    background: var(--icon-btn-bg);
    color: #1d4ed8;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    padding: 0;
    border: none;
    transition: background 0.15s ease, transform 0.15s ease;
    position: relative;
}
button.icon-btn:hover {
    background: var(--icon-btn-hover);
    transform: translateY(-1px);
}
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.45);
    z-index: 40;
}
.modal {
    position: fixed;
    top: calc(100vh / 2);
    left: calc(100vw / 2);
    transform: translate(-50%, -50%);
    background: var(--modal-bg);
    color: var(--text-main);
    border-radius: 12px;
    width: calc(100% - 32px);
    max-height: 80vh;
    overflow-y: auto;
    padding: 8px 24px 24px;
    box-shadow: 0 20px 45px rgba(15, 23, 42, 0.35);
    z-index: 50;
}
.modal-body {
    max-height: 60vh;
    overflow: auto;
}
.modal.full-width { min-width: 80%; }
.modal.hidden,
.modal-overlay.hidden { display: none; }
.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    margin-bottom: 12px;
}
.modal-table-wrapper {
    margin-top: 12px;
}
.modal-status {
    font-size: 14px;
    color: var(--muted-color);
    margin-bottom: 12px;
}
#upload-firmware-modal {
    max-width: 600px;
}
.close-btn {
    border: none;
    background: transparent;
    font-size: 36px;
    line-height: 1;
    cursor: pointer;
    color: var(--modal-text);
}
.status {
    font-weight: 600;
    font-size: 14px;
    color: var(--muted-color);
    display: inline-block;
    padding: 2px 6px;
    border-radius: 4px;
    background: var(--code-bg);
}
.status.blocked { background: #b42318; color: #ffffff; }
.status.active { background: #05603a; color: #ffffff; }
.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
    margin-top: 16px;
}
.log-output {
    background: var(--log-bg);
    color: var(--log-text);
    padding: 16px;
    border-radius: 8px;
    overflow-x: auto;
    max-height: 60vh;
    white-space: pre;
    font-family: 'SFMono-Regular', Consolas, Menlo, monospace;
    font-size: 13px;
    margin-top: 12px;
}

@media (max-width: 1024px) {
    .admin-header {
        grid-template-columns: 1fr;
        gap: 14px;
    }
    .header-actions {
        justify-self: stretch;
        justify-content: space-between;
    }
    main {
        max-width: none;
        width: 100%;
    }
}

@media (max-width: 640px) {
    .admin-header {
        display: flex;
        flex-direction: column;
        align-items: stretch;
    }
    .admin-header h1 {
        font-size: 18px;
        text-align: left;
    }
    .admin-header nav {
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        padding: 4px 0;
        width: 100%;
    }
    .admin-header nav::-webkit-scrollbar {
        display: none;
    }
    .admin-header nav a {
        flex: 0 0 auto;
    }
    .header-actions {
        width: 100%;
        flex-direction: column;
        align-items: flex-start;
        justify-content: flex-start;
        gap: 8px;
    }
    .user-meta {
        width: 100%;
        flex-direction: column;
        align-items: flex-start;
        gap: 6px;
        white-space: normal;
    }
    .user-meta form {
        width: 100%;
    }
    .user-meta form button {
        width: 100%;
    }
    main {
        padding: 24px 16px 48px;
    }
    .card {
        padding: 20px;
        margin-top: 18px;
    }
    .card-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 8px;
    }
    .card-header form,
    .card-header button {
        width: 100%;
    }
    .card-header form button {
        width: 100%;
    }
    .actions {
        justify-content: flex-start;
    }
    .modal {
        width: calc(100vw - 24px);
        min-width: 0;
        max-height: 90vh;
        padding: 16px 18px 20px;
    }
    .modal.full-width {
        min-width: 0;
    }
    .modal-body {
        max-height: 65vh;
    }
    .theme-toggle {
        width: 36px;
        height: 36px;
        align-self: flex-start;
    }
    .stat-grid,
    .form-grid {
        grid-template-columns: 1fr;
        gap: 16px;
    }
}

@media (max-width: 480px) {
    .auth-card {
        padding: 24px;
    }
}
.auth-page {
    background: var(--auth-bg);
    min-height: 100vh;
    margin: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}
.auth-container {
    width: 100%;
    max-width: 420px;
    padding: 32px 16px;
}
.auth-card {
    background: var(--auth-card-bg);
    border-radius: 16px;
    padding: 32px;
    box-shadow: var(--auth-card-shadow);
    display: flex;
    flex-direction: column;
    gap: 16px;
}
.auth-form { display: flex; flex-direction: column; gap: 16px; }
.auth-form label { gap: 8px; }

svg {
    display: block;
}
svg .svg-stroke {
    stroke: var(--svg-icon-color);
}
svg .svg-fill {
    fill: var(--svg-icon-color);
}
header svg .svg-stroke {
    stroke: var(--nav-link-color);
}
header svg .svg-fill {
    fill: var(--nav-link-color);
}
