:root {
    --bg-0: #080d13;
    --bg-1: #0d141c;
    --bg-2: #121c27;
    --bg-3: #192533;
    --bg-4: #1e2c3b;
    --border: #2b3a49;
    --border-strong: #3a4d60;
    --text: #eef4fa;
    --text-muted: #8da1b4;
    --faint: #6d8196;
    --green: #82d65b;
    --green-hover: #93e66a;
    --green-ink: #10210c;
    --blue: #168fd1;
    --blue-hover: #24a1e6;
    --blue-ink: #041820;
    --red: #df4040;
    --red-hover: #ef5555;
    --orange: #d79b22;
    --yellow: #d5b32d;
    --ok-bg: rgba(130, 214, 91, 0.12);
    --warn-bg: rgba(215, 155, 34, 0.14);
    --danger-bg: rgba(223, 64, 64, 0.12);
    --info-bg: rgba(22, 143, 209, 0.12);
    --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
    --radius: 4px;
    --font: "Segoe UI", ui-sans-serif, system-ui, sans-serif;
    --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    --nav-h: 52px;
    --control-h: 32px;
    --bg: var(--bg-1);
    --surface: var(--bg-2);
    --card: var(--bg-3);
    --card-2: var(--bg-4);
    --muted: var(--text-muted);
    --accent: var(--blue);
    --accent-2: var(--blue);
    --ok: var(--green);
    --warn: var(--orange);
    --danger: var(--red);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
    background: var(--bg-1);
    color: var(--text);
    font-family: var(--font);
    font-size: 13px;
    line-height: 1.4;
}
body.app-body { background: var(--bg-1); }
a { color: var(--blue); text-decoration: none; }
a:hover { color: var(--blue-hover); }

.icon { width: 14px; height: 14px; display: inline-block; vertical-align: -2px; fill: none; stroke: currentColor; stroke-width: 1.8; }

.app-shell { min-height: 100vh; display: flex; flex-direction: column; }
.app-top {
    display: flex; align-items: center; gap: 0.75rem;
    height: var(--nav-h); min-height: var(--nav-h);
    padding: 0 0.85rem;
    background: var(--bg-0);
    border-bottom: 1px solid var(--border);
    position: sticky; top: 0; z-index: 30;
}
.brand { display: flex; align-items: center; gap: 0.5rem; color: var(--text); text-decoration: none; flex-shrink: 0; }
.brand:hover { color: var(--text); }
.brand-mark {
    width: 22px; height: 22px; border-radius: 3px;
    background: linear-gradient(145deg, var(--green), #4ea82e);
    box-shadow: inset 0 0 0 1px rgba(255,255,255,0.12);
}
.brand-name { font-weight: 650; font-size: 13px; letter-spacing: 0.01em; }
.brand-sub { display: none; }
.app-nav { display: flex; align-items: center; gap: 1px; flex: 1; min-width: 0; overflow-x: auto; }
.app-nav a, .app-util a {
    color: var(--text-muted); text-decoration: none;
    padding: 0 0.55rem; height: 28px; display: inline-flex; align-items: center;
    border-radius: 3px; font-size: 12.5px; white-space: nowrap;
}
.app-nav a:hover, .app-util a:hover, .app-nav a.active, .app-util a.active {
    color: var(--text); background: var(--bg-3); text-decoration: none;
}
.app-util { display: flex; align-items: center; gap: 0.35rem; margin-left: auto; flex-shrink: 0; }
.top-meta { display: flex; align-items: center; gap: 0.5rem; color: var(--text-muted); font-size: 12px; }
.user-chip { color: var(--text-muted); padding: 0 0.35rem; }

.page { width: 100%; max-width: none; margin: 0; padding: 0.7rem 0.85rem 1.4rem; }
.page-head { display: flex; justify-content: space-between; align-items: center; gap: 0.7rem; margin-bottom: 0.55rem; }
.page-head h1 { margin: 0; font-size: 18px; font-weight: 650; }
.lede { color: var(--text-muted); margin: 0.15rem 0 0; font-size: 12px; }

.flash { border-radius: var(--radius); padding: 0.45rem 0.7rem; margin-bottom: 0.55rem; border: 1px solid; font-size: 12.5px; }
.flash-ok { background: var(--ok-bg); border-color: #3d7a2c; color: #c8f0b0; }
.flash-warn { background: var(--warn-bg); border-color: #8a6414; color: #f3d48a; }
.flash-err { background: var(--danger-bg); border-color: #8a2a2a; color: #f3b4b4; }

.card, .panel {
    background: var(--bg-3); border: 1px solid var(--border); border-radius: var(--radius);
    padding: 0.7rem 0.8rem; margin-bottom: 0.6rem;
}
.card h2, .panel h2 { margin: 0 0 0.35rem; font-size: 13px; font-weight: 650; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); }
.card .hint, .panel .hint { color: var(--text-muted); font-size: 11.5px; margin: 0 0 0.55rem; }

.btn, button.btn, input[type="submit"].btn, a.btn {
    appearance: none; border: 1px solid var(--border-strong); background: var(--bg-4);
    color: var(--text); border-radius: var(--radius);
    height: var(--control-h); padding: 0 0.7rem; font: inherit; font-size: 12.5px;
    cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 0.35rem;
    text-decoration: none; line-height: 1;
}
a.btn:hover { text-decoration: none; color: var(--text); }
.btn:hover { border-color: #4d6478; }
.btn.btn-primary, button.btn-primary, a.btn-primary, .btn.btn-success, button.btn-success {
    background: var(--green); border-color: #6fc24a; color: var(--green-ink); font-weight: 650;
}
.btn.btn-primary:hover, button.btn-primary:hover, a.btn-primary:hover, .btn.btn-success:hover {
    background: var(--green-hover); color: var(--green-ink);
}
.btn.btn-info, button.btn-info, a.btn-info, .btn.btn-secondary, button.btn-secondary {
    background: var(--blue); border-color: #127bb4; color: #fff; font-weight: 600;
}
.btn.btn-info:hover, button.btn-info:hover, a.btn-info:hover {
    background: var(--blue-hover); color: #fff;
}
.btn.btn-danger, button.btn-danger, a.btn-danger { background: transparent; border-color: var(--red); color: #f0a0a0; }
.btn.btn-danger:hover, button.btn-danger:hover { background: var(--danger-bg); }
.btn.btn-danger.solid { background: var(--red); border-color: #c43333; color: #fff; }
.btn.btn-ghost, button.btn-ghost, a.btn-ghost, .btn.btn-neutral { background: var(--bg-2); border-color: var(--border-strong); color: var(--text); }
.btn-small { height: 28px; padding: 0 0.55rem; font-size: 12px; }
.btn:disabled, .btn.is-disabled { opacity: 0.45; pointer-events: none; }
.btn-busy { opacity: 0.7; pointer-events: none; }

.table-wrap { overflow-x: auto; }
table.grid { width: 100%; border-collapse: collapse; font-size: 12.5px; }
table.grid th, table.grid td {
    text-align: left; padding: 0.38rem 0.45rem; border-bottom: 1px solid var(--border);
    vertical-align: middle;
}
table.grid thead th {
    position: sticky; top: 0; z-index: 2;
    background: var(--bg-2); color: var(--text-muted); font-weight: 600;
    font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em;
}
table.grid tbody tr:hover { background: rgba(255,255,255,0.025); }
table.grid tbody tr.is-selected { background: rgba(22, 143, 209, 0.12); }
table.grid .num { text-align: right; font-variant-numeric: tabular-nums; }
.empty { color: var(--text-muted); padding: 1rem 0.2rem; font-size: 12.5px; }

.badge {
    display: inline-flex; align-items: center; border-radius: 999px;
    padding: 0 0.45rem; height: 18px; font-size: 11px; border: 1px solid; line-height: 1; white-space: nowrap;
}
.badge-ok, .badge-active { color: #b8f09a; border-color: #3d7a2c; background: var(--ok-bg); }
.badge-warn, .badge-shadow, .badge-medium { color: #f3d48a; border-color: #8a6414; background: var(--warn-bg); }
.badge-danger, .badge-enforce, .badge-strict { color: #f3b4b4; border-color: #8a2a2a; background: var(--danger-bg); }
.badge-muted, .badge-inactive, .badge-off { color: var(--text-muted); border-color: var(--border-strong); background: transparent; }
.badge-info, .badge-light { color: #9ad4f0; border-color: #1d6f99; background: var(--info-bg); }

.toolbar {
    display: flex; flex-wrap: wrap; gap: 0.4rem; align-items: center;
    min-height: 44px; margin-bottom: 0.45rem;
}
.toolbar-left, .toolbar-right { display: flex; flex-wrap: wrap; gap: 0.35rem; align-items: center; }
.toolbar-right { margin-left: auto; }
form.toolbar-right { display: flex; flex-wrap: wrap; gap: 0.35rem; align-items: center; }
.toolbar input, .toolbar select, .toolbar-right input, .toolbar-right select {
    height: var(--control-h); max-width: 180px; width: auto;
}
.filters, form.toolbar { margin-bottom: 0.45rem; }

.field-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.55rem 0.7rem; }
.field { display: flex; flex-direction: column; gap: 0.18rem; }
.field.full { grid-column: 1 / -1; }
label { font-size: 11.5px; color: var(--text-muted); }
.req { color: #f3b4b4; }
input[type="text"], input[type="email"], input[type="password"], input[type="number"],
input[type="url"], input[type="search"], input[type="datetime-local"], select, textarea {
    width: 100%; background: #1c2633; color: var(--text);
    border: 1px solid var(--border-strong); border-radius: var(--radius);
    height: var(--control-h); padding: 0 0.5rem; font: inherit; font-size: 12.5px;
}
textarea { height: auto; min-height: 64px; padding: 0.4rem 0.5rem; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--blue); box-shadow: 0 0 0 1px rgba(22,143,209,0.25); }
.help { color: var(--faint); font-size: 11px; }
.error { color: #f3b4b4; font-size: 11.5px; }
.tip { border-bottom: 1px dotted var(--text-muted); cursor: help; }

.url-bar {
    display: flex; flex-wrap: wrap; align-items: center; gap: 0.45rem;
    background: #1c2633; border: 1px solid var(--border); border-radius: var(--radius); padding: 0.4rem 0.55rem;
}
.url-bar code { font-family: var(--mono); font-size: 12px; word-break: break-all; color: var(--text); }
.url-missing { color: var(--orange); }

.preset-row, .mode-row { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.4rem; }
.preset, .mode {
    border: 1px solid var(--border-strong); border-radius: var(--radius);
    padding: 0.4rem 0.5rem; cursor: pointer; background: #1c2633; text-align: left; color: inherit;
}
.preset strong, .mode strong { display: block; font-size: 12px; }
.preset small, .mode p { color: var(--text-muted); display: block; margin: 0.15rem 0 0; font-size: 11px; }
.preset.active, .mode.active { border-color: var(--blue); box-shadow: 0 0 0 1px var(--blue); }
.mode.enforce.active { border-color: var(--red); box-shadow: 0 0 0 1px var(--red); }

.effective { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 1rem; }
.effective div { display: flex; justify-content: space-between; gap: 0.6rem; border-bottom: 1px solid var(--border); padding: 0.22rem 0; font-size: 12px; }
.source { font-size: 11px; color: var(--faint); }

.stack { display: flex; flex-direction: column; gap: 0.4rem; }
.item-card { background: var(--bg-2); border: 1px solid var(--border); border-radius: var(--radius); padding: 0.5rem 0.55rem; }
.item-head { display: flex; justify-content: space-between; align-items: center; gap: 0.5rem; margin-bottom: 0.4rem; }
.inline { display: flex; gap: 0.4rem; flex-wrap: wrap; align-items: end; }
.inline .field { flex: 1; min-width: 120px; }

.builder { display: grid; grid-template-columns: 168px 1fr; gap: 0.7rem; align-items: start; }
.builder-nav {
    position: sticky; top: calc(var(--nav-h) + 8px);
    background: var(--bg-2); border: 1px solid var(--border);
    border-radius: var(--radius); padding: 0.3rem;
}
.builder-nav a {
    display: block; color: var(--text-muted); padding: 0.32rem 0.5rem; border-radius: 3px; text-decoration: none; font-size: 12px;
}
.builder-nav a:hover, .builder-nav a.active { background: var(--bg-4); color: var(--text); }
.section-anchor { scroll-margin-top: 68px; }

.editor-top {
    position: sticky; top: var(--nav-h); z-index: 20;
    display: flex; justify-content: space-between; align-items: center; gap: 0.6rem;
    background: var(--bg-1); border-bottom: 1px solid var(--border);
    margin: -0.7rem -0.85rem 0.7rem; padding: 0.45rem 0.85rem;
}
.editor-top h1 { font-size: 16px; margin: 0; }
.sticky-actions {
    position: sticky; bottom: 0; display: flex; justify-content: flex-end; align-items: center;
    gap: 0.45rem; padding: 0.5rem 0; background: linear-gradient(180deg, transparent, var(--bg-1) 28%);
}
.dirty { color: var(--orange); font-size: 12px; margin-right: auto; }

.modal-backdrop {
    position: fixed; inset: 0; background: rgba(4, 8, 14, 0.72); display: none; align-items: center; justify-content: center; z-index: 40; padding: 1rem;
}
.modal-backdrop.open { display: flex; }
.modal { width: min(480px, 100%); background: var(--bg-3); border: 1px solid var(--red); border-radius: var(--radius); padding: 0.85rem; }
.modal h3 { margin: 0 0 0.4rem; font-size: 15px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 0.4rem; margin-top: 0.8rem; }

.login-body { background: var(--bg-0); min-height: 100vh; display: grid; place-items: center; padding: 1.2rem; }
.login-card {
    width: min(360px, 100%); background: #141c26; border: 1px solid #304052;
    border-radius: 6px; padding: 1.35rem 1.3rem 1.2rem; box-shadow: var(--shadow);
}
.login-brand { display: flex; flex-direction: column; align-items: center; text-align: center; gap: 0.35rem; margin-bottom: 1rem; }
.login-brand .brand-mark { width: 36px; height: 36px; border-radius: 6px; }
.login-card h1 { margin: 0; font-size: 20px; }
.login-card .lede { margin: 0; }
.login-card .stack { margin-top: 0.85rem; }
.login-card .btn-primary { width: 100%; height: 34px; background: var(--green); border-color: #6fc24a; color: var(--green-ink); }
.remember { display: flex; align-items: center; gap: 0.4rem; color: var(--text-muted); font-size: 12px; }

.diag { font-family: var(--mono); font-size: 11.5px; background: #1c2633; border-radius: var(--radius); padding: 0.55rem; overflow: auto; max-height: 320px; white-space: pre-wrap; }
.pager { display: flex; justify-content: space-between; align-items: center; gap: 0.6rem; margin-top: 0.55rem; }
.row-actions { display: flex; flex-wrap: wrap; gap: 0.3rem 0.5rem; align-items: center; }
.row-actions form { display: inline; margin: 0; }
button.linkish {
    appearance: none; background: none; border: 0; padding: 0; color: var(--blue); font: inherit; cursor: pointer;
}
button.linkish:hover { text-decoration: underline; }
button.linkish.danger { color: #f0a0a0; }
.truncate { max-width: 280px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dest-preview { margin-top: 0.3rem; color: var(--text-muted); font-size: 11.5px; }
.dest-preview strong { color: var(--text); }
.mapping-table input { width: 100%; }
.mapping-preview { margin-top: 0.55rem; }
.macro-helper { margin-top: 0.4rem; color: var(--text-muted); font-size: 12px; }
.macro-helper summary { cursor: pointer; color: var(--text); }
.macro-cat { margin-top: 0.35rem; }
.macro-chips { display: flex; flex-wrap: wrap; gap: 0.25rem; margin-top: 0.2rem; }
.macro-chip {
    appearance: none; border: 1px solid var(--border); background: #121a24; color: var(--blue);
    border-radius: 999px; padding: 0.1rem 0.45rem; font: inherit; font-size: 11.5px; cursor: pointer;
}

table.grid.dense th, table.grid.dense td { padding: 0.32rem 0.4rem; font-size: 12px; }
.mono { font-family: var(--mono); font-size: 11.5px; word-break: break-all; }
.click-filters { display: flex; flex-wrap: wrap; gap: 0.4rem 0.55rem; align-items: end; }
.click-filters label { display: flex; flex-direction: column; gap: 0.15rem; min-width: 120px; }
.click-filters input, .click-filters select { min-width: 120px; }
.dl-grid { display: grid; grid-template-columns: 170px 1fr; gap: 0.28rem 0.8rem; margin: 0; }
.dl-grid dt { color: var(--text-muted); font-size: 12px; }
.dl-grid dd { margin: 0; }

.toggle-row { display: flex; align-items: center; justify-content: space-between; gap: 0.6rem; padding: 0.28rem 0; }
.switch { position: relative; width: 36px; height: 20px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span {
    position: absolute; inset: 0; background: #2a3848; border-radius: 999px; cursor: pointer;
}
.switch span:before {
    content: ""; position: absolute; width: 14px; height: 14px; left: 3px; top: 3px;
    background: #c5d0dc; border-radius: 50%; transition: transform .15s;
}
.switch input:checked + span { background: #2f7a22; }
.switch input:checked + span:before { transform: translateX(16px); background: var(--green); }

.sub { color: var(--text-muted); font-size: 11px; }
.check-col { width: 28px; }
.id-col { width: 52px; color: var(--text-muted); font-family: var(--mono); font-size: 11.5px; }

.table-panel {
    background: var(--bg-3); border: 1px solid var(--border); border-radius: var(--radius);
    overflow: hidden; margin-bottom: 0.6rem;
}
.table-panel .table-wrap { margin: 0; }
.table-panel table.grid th:first-child,
.table-panel table.grid td:first-child { padding-left: 0.65rem; }
.table-panel .empty { padding: 1.1rem 0.75rem; }

.editor-grid { display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(0, 0.9fr); gap: 0.65rem; align-items: start; }
.editor-col { min-width: 0; }
.editor-col .card { margin-bottom: 0.55rem; }
.editor-identity { display: flex; flex-direction: column; gap: 0.1rem; min-width: 0; }
.editor-identity .sub { font-family: var(--mono); }

.chip-list { display: flex; flex-wrap: wrap; gap: 0.35rem; min-height: 0; }
.chip {
    display: inline-flex; align-items: center; gap: 0.3rem;
    background: #1c2633; border: 1px solid var(--border-strong); border-radius: 999px;
    padding: 0.1rem 0.2rem 0.1rem 0.45rem; max-width: 100%;
}
.chip input, .chip select {
    width: auto; min-width: 110px; max-width: 220px; height: 26px;
    border: 0; background: transparent; padding: 0 0.2rem;
}
.chip .btn { height: 22px; width: 22px; padding: 0; border-radius: 999px; }

.protect-toggles { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.4rem; margin: 0.35rem 0 0.55rem; }
.protect-toggle {
    display: flex; align-items: center; justify-content: space-between; gap: 0.5rem;
    background: #1c2633; border: 1px solid var(--border); border-radius: var(--radius); padding: 0.35rem 0.5rem;
}
.protect-toggle label { color: var(--text); font-size: 12.5px; }
.protect-toggle select { width: auto; min-width: 72px; }

.ref-select { max-width: 280px; }
.dest-type { font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.04em; }

.login-errors { background: var(--danger-bg); border: 1px solid #8a2a2a; color: #f3b4b4; border-radius: var(--radius); padding: 0.4rem 0.55rem; font-size: 12.5px; margin-bottom: 0.7rem; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); border: 0; }

.stat-strip {
    display: grid;
    grid-template-columns: repeat(9, minmax(0, 1fr));
    gap: 0.45rem;
    margin: 0 0 0.7rem;
}
.stat-strip.protection-strip { grid-template-columns: repeat(7, minmax(0, 1fr)); }
.stat-strip > div {
    background: var(--bg-2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.45rem 0.55rem;
}
.stat-strip span { display: block; color: var(--text-muted); font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.04em; }
.stat-strip strong { font-size: 14px; }
.is-pos { color: var(--green); }
.is-neg { color: var(--red); }

.badge-unchecked { background: #2a3140; color: #c5cdd8; }
.badge-verified, .badge-valid, .health-healthy { color: var(--green); }
.badge-failed, .badge-invalid, .badge-error, .health-down { color: var(--red); }
.health-degraded { color: #e0b45a; }
.health-unknown, .health-unchecked { color: #c5cdd8; }
.health-cats {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.55rem;
    margin: 0 0 0.85rem;
}
.health-cat {
    background: var(--bg-2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.65rem 0.75rem;
}
.health-cat span { display: block; color: var(--text-muted); font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; }
.health-cat strong { font-size: 18px; }
.settings-tabs { display: flex; gap: 0.4rem; margin: 0 0 0.85rem; }
.settings-tabs a {
    padding: 0.35rem 0.7rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-muted);
}
.settings-tabs a.active { background: var(--bg-3); color: var(--text); }

@media (max-width: 1100px) {
    .app-top { flex-wrap: wrap; height: auto; min-height: var(--nav-h); padding-bottom: 0.4rem; }
    .app-nav { flex-basis: 100%; order: 3; }
    .editor-grid { grid-template-columns: 1fr; }
}
@media (max-width: 860px) {
    .builder { grid-template-columns: 1fr; }
    .builder-nav { position: static; display: flex; overflow-x: auto; gap: 0.2rem; }
    .builder-nav a { white-space: nowrap; }
    .field-grid, .preset-row, .mode-row, .effective, .protect-toggles { grid-template-columns: 1fr; }
    .page-head, .editor-top { flex-direction: column; align-items: stretch; }
    .toolbar-right { margin-left: 0; }
}
