:root {
    --body: #0d1520;
    --ink: #101c2c;
    --panel: #162032;
    --panel-soft: #1e2f47;
    --line: #253a56;
    --text: #e8f2fb;
    --muted: #8faec8;
    --soft-muted: #617d99;
    --field: #f7fafc;
    --field-text: #0f1e30;
    --field-line: #bccad8;
    --brand: #ffffff;
    --pre-bg: #06101b;
    --pre-text: #c9eeff;
    --flash-bg: #0f3322;
    --flash-line: #1e6644;
    --alert-bg: #3a1520;
    --accent: #f08a20;
    --accent-hover: #e87800;
    --blue: #1a6fbf;
    --blue-hover: #1560a8;
    --danger: #d44;
    font-family: system-ui, -apple-system, "Segoe UI", Arial, sans-serif;
}

[data-theme="light"] {
    --body: #edf2f7;
    --ink: #f5f8fc;
    --panel: #ffffff;
    --panel-soft: #e5ecf4;
    --line: #c5d3e0;
    --text: #0f1e30;
    --muted: #4a6278;
    --soft-muted: #637d96;
    --field: #ffffff;
    --field-text: #0f1e30;
    --field-line: #b2c4d4;
    --brand: #0f2240;
    --pre-bg: #0f2240;
    --pre-text: #eef8ff;
    --flash-bg: #e3f5ec;
    --flash-line: #7dc49a;
    --alert-bg: #fcedf0;
    --danger: #c03040;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    min-height: 100vh;
    color: var(--text);
    background: var(--body);
}

a {
    color: inherit;
    text-decoration: none;
}

/* ── Topbar ─────────────────────────────────── */

.topbar {
    position: sticky;
    top: 0;
    z-index: 200;
    display: flex;
    align-items: center;
    gap: 20px;
    min-height: 56px;
    padding: 0 24px;
    background: var(--ink);
    border-bottom: 1px solid var(--line);
    box-shadow: 0 2px 12px rgba(0,0,0,.25);
}

.brand {
    font-size: 15px;
    font-weight: 700;
    letter-spacing: .5px;
    color: var(--brand);
    white-space: nowrap;
}

/* ── Navigation ─────────────────────────────── */

.nav {
    display: flex;
    flex: 1;
    align-items: center;
    gap: 2px;
}

.nav-group {
    position: relative;
}

.nav-toggle {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    min-height: 34px;
    padding: 0 13px;
    color: var(--muted);
    background: transparent;
    border: 0;
    border-radius: 6px;
    font: inherit;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    white-space: nowrap;
    transition: color .15s, background .15s;
}

.nav-toggle:hover,
.nav-group.is-open .nav-toggle {
    color: var(--text);
    background: var(--panel-soft);
}

.nav-caret {
    font-size: 9px;
    opacity: .55;
    transition: opacity .15s, transform .15s;
}

.nav-group.is-open .nav-caret {
    opacity: 1;
    transform: rotate(180deg);
}

.nav-dropdown {
    display: none;
    position: absolute;
    top: calc(100% + 8px);
    left: 0;
    min-width: 210px;
    padding: 6px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 10px;
    box-shadow: 0 8px 28px rgba(0,0,0,.28);
    z-index: 300;
}

.nav-dropdown-right {
    left: auto;
    right: 0;
}

.nav-group.is-open .nav-dropdown {
    display: block;
}

.nav-dropdown-item {
    display: block;
    width: 100%;
    padding: 8px 12px;
    border-radius: 6px;
    color: var(--text);
    font: inherit;
    font-size: 14px;
    text-align: left;
    background: transparent;
    border: 0;
    cursor: pointer;
    white-space: nowrap;
    transition: background .12s;
}

.nav-dropdown-item:hover {
    background: var(--panel-soft);
}

.nav-dropdown-divider {
    margin: 5px 0;
    border-top: 1px solid var(--line);
}

.nav-dropdown-empty {
    display: block;
    padding: 8px 12px;
    color: var(--soft-muted);
    font-size: 13px;
}

.nav-dropdown-theme {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 6px 12px;
}

.nav-dropdown-label {
    color: var(--muted);
    font-size: 13px;
    font-weight: 500;
    white-space: nowrap;
}

.theme-select {
    min-height: 30px;
    padding: 4px 8px;
    width: auto;
    font-size: 13px;
}

.nav-dropdown-logout {
    color: var(--danger) !important;
}

.nav-dropdown-logout:hover {
    background: color-mix(in srgb, var(--danger) 12%, transparent) !important;
}

/* ── Page layout ─────────────────────────────── */

.page {
    width: min(1200px, calc(100% - 32px));
    margin: 0 auto;
    padding: 36px 0 64px;
}

/* ── Login ─────────────────────────────────── */

.login-shell {
    display: grid;
    min-height: calc(100vh - 56px);
    place-items: center;
}

.login-panel {
    width: min(400px, 100%);
    padding: 36px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 12px;
    box-shadow: 0 16px 48px rgba(0,0,0,.28);
}

/* ── Cards & panels ─────────────────────────── */

.panel,
.result,
.user-row,
.module-card {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 10px;
}

.panel,
.result {
    padding: 22px;
    margin-bottom: 18px;
}

/* ── Typography ─────────────────────────────── */

.eyebrow {
    margin: 0 0 6px;
    color: var(--accent);
    font-size: 11px;
    font-weight: 700;
    letter-spacing: .8px;
    text-transform: uppercase;
}

h1, h2 { margin: 0; }
h1 { font-size: 28px; font-weight: 700; }
h2 { font-size: 17px; font-weight: 600; }

/* ── Form elements ──────────────────────────── */

.stack,
.admin-form,
.user-row {
    display: grid;
    gap: 16px;
}

.stack { margin-top: 28px; }

label {
    display: grid;
    gap: 6px;
    color: var(--muted);
    font-size: 12px;
    font-weight: 600;
    letter-spacing: .2px;
}

label small {
    color: var(--soft-muted);
    font-size: 11px;
    font-weight: 600;
}

label em {
    color: var(--muted);
    font-size: 12px;
    font-style: normal;
    font-weight: 400;
    line-height: 1.35;
}

label[title],
label span[title] {
    cursor: help;
}

input,
select,
textarea {
    width: 100%;
    min-height: 40px;
    padding: 8px 11px;
    color: var(--field-text);
    background: var(--field);
    border: 1px solid var(--field-line);
    border-radius: 7px;
    font: inherit;
    font-size: 14px;
    transition: border-color .15s, box-shadow .15s;
}

input:focus,
select:focus,
textarea:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent);
}

textarea {
    min-height: 80px;
    resize: vertical;
}

.text-upper { text-transform: uppercase; }
.text-lower { text-transform: lowercase; }

input[type="checkbox"] {
    width: auto;
    min-height: 0;
}

/* ── Buttons ────────────────────────────────── */

.primary-button,
.secondary-button,
.ghost-button,
.danger-button {
    min-height: 38px;
    padding: 0 18px;
    border: 0;
    border-radius: 7px;
    cursor: pointer;
    font: inherit;
    font-size: 14px;
    font-weight: 600;
    transition: background .15s, box-shadow .15s;
}

.primary-button {
    color: #0d1520;
    background: var(--accent);
}

.primary-button:hover { background: var(--accent-hover); }

.secondary-button {
    color: #fff;
    background: var(--blue);
}

.secondary-button:hover { background: var(--blue-hover); }

.ghost-button {
    color: var(--muted);
    background: transparent;
    border: 1px solid var(--line);
}

.ghost-button:hover {
    color: var(--text);
    background: var(--panel-soft);
}

.danger-button {
    color: #fff;
    background: var(--danger);
}

/* ── Section header ─────────────────────────── */

.section-head { margin-bottom: 24px; }

/* ── Dashboard module cards ─────────────────── */

.module-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 16px;
}

.module-card {
    display: grid;
    gap: 8px;
    padding: 20px;
    border-radius: 10px;
    transition: box-shadow .15s;
}

.module-card:hover { box-shadow: 0 4px 16px rgba(0,0,0,.18); }

.module-card span { font-weight: 700; }

.module-card small,
.empty,
.hint {
    color: var(--muted);
}

.hint {
    margin: 12px 0 0;
    font-size: 13px;
}

/* ── Admin forms ─────────────────────────────── */

.admin-form {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    margin-top: 18px;
}

.profile-form {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 16px;
}

.link-button {
    display: inline-flex;
    align-items: center;
}

/* ── Module form (cliente-servidor) ──────────── */

.load-form {
    display: grid;
    grid-template-columns: minmax(260px, 1fr) auto;
    gap: 14px;
    align-items: end;
    margin-top: 18px;
}

.form-options {
    display: grid;
    gap: 16px;
}

.form-options-row {
    display: flex;
    flex-wrap: wrap;
    gap: 16px;
    align-items: end;
    justify-content: space-between;
}

.segmented-control {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    min-width: min(100%, 280px);
    margin: 0;
    padding: 0;
    border: 0;
}

.segmented-control legend {
    width: 100%;
    margin-bottom: 4px;
    color: var(--muted);
    font-size: 12px;
    font-weight: 600;
}

.segmented-control label { display: block; }

.segmented-control input {
    position: absolute;
    inline-size: 1px;
    block-size: 1px;
    min-height: 0;
    opacity: 0;
}

.segmented-control span {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 34px;
    min-width: 96px;
    padding: 0 14px;
    color: var(--muted);
    border: 1px solid var(--line);
    border-radius: 7px;
    font-size: 13px;
    cursor: pointer;
    transition: background .12s, color .12s, border-color .12s;
}

.segmented-control input:checked + span {
    color: #0d1520;
    background: var(--accent);
    border-color: var(--accent);
    font-weight: 600;
}

.font-size-control { width: min(100%, 220px); }

.checks,
.check {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 14px;
}

.checks { grid-column: 1 / -1; }

/* ── User list ──────────────────────────────── */

.user-list { display: grid; gap: 14px; }

.user-row {
    grid-template-columns: 1fr 1fr auto;
    padding: 18px;
    border-radius: 10px;
}

/* ── Field admin ─────────────────────────────── */

.field-admin-form {
    display: grid;
    gap: 18px;
    margin-top: 18px;
}

.field-form-grid,
.option-form,
.option-edit-form {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 16px;
}

.wide-field { grid-column: 1 / -1; }

.field-list,
.option-list { display: grid; gap: 12px; }

.field-row,
.option-row {
    display: grid;
    gap: 16px;
    padding: 16px 18px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 8px;
}

.field-row {
    grid-template-columns: minmax(0, 1fr) auto auto;
    align-items: center;
}

.field-row div:first-child {
    display: grid;
    gap: 4px;
}

.field-row small { color: var(--muted); font-size: 12px; }

.status-pill {
    display: inline-flex;
    align-items: center;
    min-height: 26px;
    padding: 0 10px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 700;
    letter-spacing: .3px;
}

.status-pill.is-on { color: #0a2718; background: #7ddba0; }
.status-pill.is-off { color: #3a0e14; background: #f0a8b2; }

.row-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    justify-content: flex-end;
}

/* ── Config form (main module form) ─────────── */

.config-form {
    display: grid;
    gap: 16px;
}

.config-form.font-compact { font-size: 12px; }
.config-form.font-default { font-size: 13px; }
.config-form.font-large   { font-size: 15px; }

.config-form label,
.config-form input,
.config-form select {
    font-size: inherit;
}

.config-group {
    display: grid;
    gap: 12px;
    margin: 0;
    padding: 20px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 10px;
}

.config-group legend {
    padding: 0 8px;
    color: var(--text);
    font-size: 14px;
    font-weight: 700;
    letter-spacing: .2px;
}

.config-group-fields { display: grid; gap: 12px; }

.config-form.is-grouped .config-group-fields {
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
}

.config-form.is-list .config-group {
    padding: 0;
    background: transparent;
    border: 0;
}

.config-form.is-list .config-group legend { display: none; }

.form-actions {
    grid-column: 1 / -1;
    display: flex;
    gap: 10px;
    justify-content: flex-end;
}

/* ── Result block ───────────────────────────── */

.result { display: grid; gap: 10px; }

pre {
    max-height: 340px;
    margin: 0;
    padding: 14px 16px;
    overflow: auto;
    color: var(--pre-text);
    background: var(--pre-bg);
    border: 1px solid var(--line);
    border-radius: 8px;
    font-size: 13px;
    line-height: 1.5;
}

/* ── Flash & alert ──────────────────────────── */

.flash,
.alert {
    margin-bottom: 18px;
    padding: 13px 16px;
    border-radius: 8px;
    font-size: 14px;
}

.flash {
    background: var(--flash-bg);
    border: 1px solid var(--flash-line);
}

.alert {
    background: var(--alert-bg);
    border: 1px solid var(--danger);
}

.alert-title {
    font-weight: 700;
    margin-bottom: 6px;
}

.field-error {
    display: block;
    margin-top: 4px;
    color: var(--danger);
    font-size: 12px;
    font-weight: 600;
}

.config-form label.has-error > span {
    color: var(--danger);
}

.config-form label.has-error input,
.config-form label.has-error select {
    border-color: var(--danger);
    box-shadow: 0 0 0 1px var(--danger);
}

/* ── Dashboard hero ─────────────────────────── */

.dashboard-hero {
    display: grid;
    place-items: center;
    min-height: calc(100vh - 56px);
}

.dashboard-wordmark {
    color: var(--muted);
    font-size: clamp(18px, 3vw, 28px);
    font-weight: 700;
    letter-spacing: 4px;
    text-transform: uppercase;
    user-select: none;
}

/* ── Execution overlay ──────────────────────── */

.exec-overlay {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 999;
    background: rgba(0, 0, 0, .72);
    place-items: center;
    backdrop-filter: blur(3px);
}

.exec-overlay.is-active {
    display: grid;
}

.exec-overlay-box {
    display: grid;
    gap: 14px;
    padding: 40px 52px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 14px;
    text-align: center;
    max-width: 360px;
    box-shadow: 0 24px 60px rgba(0,0,0,.5);
}

.exec-spinner {
    width: 48px;
    height: 48px;
    border: 4px solid var(--line);
    border-top-color: var(--accent);
    border-radius: 50%;
    margin: 0 auto;
    animation: spin .8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.exec-overlay-msg {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
    color: var(--text);
}

.exec-overlay-sub {
    margin: 0;
    font-size: 13px;
    color: var(--muted);
    min-height: 1em;
}

/* ── Responsive ─────────────────────────────── */

@media (max-width: 760px) {
    .topbar {
        flex-wrap: wrap;
        padding: 12px 16px;
        gap: 12px;
    }

    .nav { flex-wrap: wrap; }

    .nav-dropdown { position: static; box-shadow: none; border: 0; padding: 0 0 0 12px; }
    .nav-group.is-open .nav-dropdown { display: block; }

    .admin-form,
    .field-form-grid,
    .load-form,
    .option-edit-form,
    .option-form,
    .profile-form,
    .user-row {
        grid-template-columns: 1fr;
    }

    .field-row { grid-template-columns: 1fr; }
    .row-actions { justify-content: flex-start; }
    h1 { font-size: 24px; }
}
