:root {
    color-scheme: light;
    --bg: #f6f7f9;
    --panel: #ffffff;
    --text: #18202a;
    --muted: #667085;
    --line: #d9dee7;
    --blue: #2563eb;
    --blue-dark: #1d4ed8;
    --green: #12805c;
    --red: #b42318;
    --amber: #b54708;
    --shadow: 0 10px 28px rgba(24, 32, 42, .07);
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    letter-spacing: 0;
}

button,
input,
select {
    font: inherit;
}

.unit-input {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    overflow: hidden;
    background: #fff;
    border: 1px solid #cbd5e1;
    border-radius: 6px;
}

.unit-input:focus-within {
    border-color: var(--blue);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, .12);
}

.unit-input input {
    border: 0;
    border-radius: 0;
    box-shadow: none;
}

.unit-input input:focus {
    box-shadow: none;
}

.unit-input b {
    padding: 0 11px;
    color: var(--muted);
    font-size: 12px;
    font-weight: 900;
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 5;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 18px clamp(16px, 4vw, 44px);
    background: rgba(246, 247, 249, .92);
    border-bottom: 1px solid var(--line);
    backdrop-filter: blur(12px);
}

.topnav {
    display: flex;
    align-items: center;
    gap: 8px;
}

.topnav a {
    min-height: 36px;
    display: inline-flex;
    align-items: center;
    padding: 7px 11px;
    color: var(--muted);
    border-radius: 6px;
    text-decoration: none;
    font-weight: 800;
}

.topnav a:hover {
    background: #eef2f7;
    color: var(--text);
}

.topbar h1,
.login-box h1 {
    margin: 0;
    font-size: 26px;
    line-height: 1.2;
}

.eyebrow {
    margin: 0 0 4px;
    color: var(--blue);
    font-size: 12px;
    font-weight: 800;
    text-transform: uppercase;
}

.workspace {
    width: min(1600px, 100%);
    margin: 0 auto;
    padding: 24px clamp(16px, 4vw, 44px) 44px;
}

.app-shell {
    display: grid;
    grid-template-columns: 300px minmax(0, 1fr);
    grid-template-areas:
        "metrics metrics"
        "create create"
        "sidebar detail";
    gap: 16px;
    align-items: start;
}

.shell-metrics {
    grid-area: metrics;
}

.metrics {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 12px;
    margin-bottom: 16px;
}

.metrics div,
.panel,
.upstream-card,
.login-box {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 8px;
    box-shadow: var(--shadow);
}

.metrics div {
    padding: 16px;
}

.metrics span,
label span,
.stat-row span {
    display: block;
    color: var(--muted);
    font-size: 12px;
    font-weight: 700;
}

.metrics strong {
    display: block;
    margin-top: 6px;
    font-size: 28px;
}

.panel {
    padding: 16px;
    margin-bottom: 16px;
}

.section-head {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 16px;
}

.section-head h2 {
    margin: 0;
    font-size: 19px;
}

.section-head > span {
    color: var(--muted);
    font-size: 13px;
    font-weight: 700;
}

.source-sidebar {
    position: sticky;
    top: 92px;
    grid-area: sidebar;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 8px;
    box-shadow: var(--shadow);
    overflow: hidden;
}

.side-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px;
    border-bottom: 1px solid var(--line);
}

.side-head span {
    color: var(--muted);
    font-size: 12px;
    font-weight: 800;
}

.source-list {
    display: grid;
    gap: 4px;
    padding: 8px;
}

.source-item {
    display: grid;
    gap: 5px;
    padding: 12px;
    color: var(--text);
    border: 1px solid transparent;
    border-radius: 6px;
    text-decoration: none;
}

.source-item span {
    color: var(--muted);
    font-size: 12px;
    font-weight: 700;
}

.source-item:hover,
.source-item.active {
    background: #f8fafc;
    border-color: #cbd5e1;
}

.source-item.active {
    box-shadow: inset 3px 0 0 var(--blue);
}

.source-create {
    grid-area: create;
}

.detail-pane {
    grid-area: detail;
    min-width: 0;
}

.upstream-form,
.derive-form {
    display: grid;
    grid-template-columns: 180px 180px minmax(260px, 1fr) auto;
    gap: 12px;
    align-items: end;
}

.derive-form {
    display: grid;
    grid-template-columns: 1fr;
    align-items: stretch;
    gap: 12px;
    margin: 16px 0;
    padding: 16px;
    background: #f8fafc;
    border: 1px solid #dbe3ee;
    border-radius: 8px;
}

.derive-form > * {
    grid-column: 1 / -1;
}

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

.form-head h2 {
    margin: 0;
    font-size: 18px;
}

.form-head > span {
    color: var(--muted);
    font-size: 12px;
    font-weight: 800;
}

.derive-fields {
    display: grid;
    grid-template-columns: minmax(180px, 1.2fr) minmax(150px, .9fr) minmax(120px, .7fr) minmax(160px, .9fr) minmax(170px, .9fr) auto;
    gap: 12px;
    align-items: end;
}

.derive-fields > button {
    min-height: 40px;
}

.derive-fields .advanced-limit {
    min-height: 40px;
}

label {
    display: grid;
    gap: 6px;
}

input,
select {
    width: 100%;
    min-height: 40px;
    padding: 9px 10px;
    color: var(--text);
    background: #fff;
    border: 1px solid #cbd5e1;
    border-radius: 6px;
    outline: none;
}

input:focus,
select:focus {
    border-color: var(--blue);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, .12);
}

button,
.ghost {
    min-height: 40px;
    padding: 9px 13px;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: #fff;
    color: var(--text);
    text-decoration: none;
    cursor: pointer;
    white-space: nowrap;
}

.primary {
    border-color: var(--blue);
    background: var(--blue);
    color: #fff;
    font-weight: 800;
}

.primary:hover {
    background: var(--blue-dark);
}

.ghost:hover,
.icon-button:hover {
    border-color: var(--blue);
    color: var(--blue);
}

.danger {
    min-height: 40px;
    padding: 9px 13px;
    border: 1px solid #fecaca;
    border-radius: 6px;
    background: #fff;
    color: var(--red);
    cursor: pointer;
    font-weight: 800;
    white-space: nowrap;
}

.danger:hover {
    background: #fee2e2;
    border-color: #fca5a5;
}

.small {
    min-height: 34px;
    padding: 6px 10px;
}

.icon-button {
    width: 40px;
    min-width: 40px;
    padding: 0;
    font-size: 22px;
    line-height: 1;
}

.upstream-grid {
    display: grid;
    gap: 16px;
}

.upstream-card {
    padding: 18px;
}

.detail-card {
    margin: 0;
}

.card-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 14px;
}

.card-head h2 {
    margin: 0;
    font-size: 21px;
}

.card-head p {
    margin: 8px 0 0;
    color: var(--muted);
    overflow-wrap: anywhere;
}

.title-line {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
}

.client-badge {
    display: inline-flex;
    align-items: center;
    min-height: 24px;
    padding: 3px 8px;
    border-radius: 999px;
    background: #eff6ff;
    color: #1d4ed8;
    font-size: 12px;
    font-weight: 800;
}

.card-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: 8px;
}

.detail-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 10px;
    margin: 14px 0;
}

.detail-grid div {
    min-width: 0;
    padding: 12px;
    background: #fbfcfe;
    border: 1px solid #e6ebf2;
    border-radius: 6px;
}

.detail-grid span {
    display: block;
    margin-bottom: 6px;
    color: var(--muted);
    font-size: 12px;
    font-weight: 800;
}

.detail-grid strong {
    display: block;
    font-size: 14px;
}

.upstream-edit {
    margin: 14px 0;
    padding: 12px;
    background: #f8fafc;
    border: 1px solid #dbe3ee;
    border-radius: 8px;
}

.upstream-edit summary {
    color: var(--text);
    cursor: pointer;
    font-size: 13px;
    font-weight: 900;
}

.upstream-edit-form {
    display: grid;
    grid-template-columns: 180px 180px minmax(260px, 1fr) auto;
    gap: 12px;
    align-items: end;
    margin-top: 12px;
}

.upstream-edit-form .wide {
    min-width: 0;
}

.stat-row {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 10px;
    margin-bottom: 12px;
}

.stat-row div {
    padding: 12px;
    background: #f8fafc;
    border: 1px solid #e6ebf2;
    border-radius: 6px;
}

.stat-row strong {
    display: block;
    margin-top: 5px;
    font-size: 20px;
}

.sync-line {
    margin: 8px 0 12px;
    font-size: 13px;
    font-weight: 700;
}

.sync-line.ok {
    color: var(--green);
}

.sync-line.bad,
.flash.bad {
    color: var(--red);
}

.node-panel {
    margin-bottom: 14px;
    padding: 14px;
    background: #fbfcfe;
    border: 1px solid #e6ebf2;
    border-radius: 6px;
}

.node-list {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    max-height: 260px;
    overflow: auto;
    padding-right: 4px;
}

.node-list span,
.pill {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    min-height: 26px;
    padding: 4px 9px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 800;
    background: #eef2ff;
    color: #3730a3;
}

.node-list b {
    color: inherit;
    font-size: 11px;
}

.node-list .latency-good {
    background: #dcfce7;
    color: #166534;
}

.node-list .latency-mid {
    background: #fef3c7;
    color: #92400e;
}

.node-list .latency-slow {
    background: #ffedd5;
    color: #9a3412;
}

.node-list .latency-timeout {
    background: #fee2e2;
    color: #991b1b;
}

.node-list .latency-unknown {
    background: #eef2ff;
    color: #3730a3;
}

.pill.on {
    background: #dcfce7;
    color: #166534;
}

.pill.off {
    background: #fee2e2;
    color: #991b1b;
}

.sub-table-wrap {
    padding-top: 14px;
}

.section-head.compact {
    margin-bottom: 10px;
}

.sub-table {
    display: grid;
    gap: 10px;
}

.sub-table > form[id^="toggle-"],
.sub-table > form[id^="delete-"] {
    display: none;
}

.sub-row {
    display: grid;
    gap: 14px;
    padding: 14px;
    background: #fbfcfe;
    border: 1px solid #e6ebf2;
    border-radius: 6px;
}

.sub-detail-line,
.sub-edit-line,
.sub-fields,
.sub-actions {
    min-width: 0;
}

.sub-detail-line {
    display: grid;
    grid-template-columns: minmax(220px, 320px) minmax(0, 1fr);
    gap: 12px;
    align-items: start;
    padding-bottom: 12px;
    border-bottom: 1px solid #e6ebf2;
}

.sub-title {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 6px;
}

.sub-title input {
    min-height: 34px;
    font-weight: 800;
}

.copy-line {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
    align-items: center;
}

.sub-detail-line small {
    grid-column: 1 / -1;
    margin-top: 0;
}

.traffic-box {
    grid-column: 1 / -1;
    display: grid;
    gap: 6px;
    padding: 10px;
    background: #fff;
    border: 1px solid #e6ebf2;
    border-radius: 6px;
}

.traffic-head,
.traffic-foot {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}

.traffic-head strong {
    font-size: 14px;
}

.traffic-head span,
.traffic-foot {
    color: var(--muted);
    font-size: 12px;
    font-weight: 800;
}

.traffic-bar {
    height: 8px;
    overflow: hidden;
    background: #e6ebf2;
    border-radius: 999px;
}

.traffic-bar i {
    display: block;
    height: 100%;
    background: var(--blue);
    border-radius: inherit;
}

.sub-edit-line {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 12px;
    align-items: end;
}

.copy-button.copied {
    border-color: var(--green);
    color: var(--green);
}

.sub-fields {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 8px;
}

.sub-fields input {
    min-height: 34px;
}

.advanced-limit {
    position: relative;
    min-width: 0;
    padding: 8px 10px;
    border: 1px solid #dbe3ee;
    border-radius: 6px;
    background: #fff;
}

.advanced-limit summary {
    color: var(--muted);
    cursor: pointer;
    font-size: 12px;
    font-weight: 800;
    list-style-position: outside;
}

.advanced-limit label {
    margin-top: 8px;
}

.derive-fields .advanced-limit[open] {
    z-index: 20;
}

.derive-fields .advanced-limit[open] summary {
    color: var(--blue);
}

.derive-fields .advanced-limit[open] label {
    position: absolute;
    left: 0;
    right: 0;
    z-index: 25;
    min-width: 260px;
    padding: 10px;
    background: #fff;
    border-left: 1px solid #dbe3ee;
    border-right: 1px solid #dbe3ee;
    box-shadow: 0 18px 32px rgba(24, 32, 42, .14);
}

.derive-fields .advanced-limit[open] label:nth-of-type(1) {
    top: calc(100% + 8px);
    border-top: 1px solid #dbe3ee;
    border-radius: 8px 8px 0 0;
}

.derive-fields .advanced-limit[open] label:nth-of-type(2) {
    top: calc(100% + 78px);
}

.derive-fields .advanced-limit[open] label:nth-of-type(3) {
    top: calc(100% + 148px);
    border-bottom: 1px solid #dbe3ee;
    border-radius: 0 0 8px 8px;
}

.derive-form .advanced-limit label + label {
    margin-top: 8px;
}

.sub-advanced {
    grid-column: 1 / -1;
}

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

code {
    display: block;
    color: #1d4ed8;
    overflow-wrap: anywhere;
    font-size: 12px;
}

small {
    display: block;
    margin-top: 6px;
    color: var(--muted);
}

.empty {
    padding: 18px;
    color: var(--muted);
    text-align: center;
    border: 1px dashed var(--line);
    border-radius: 6px;
}

.flash {
    padding: 12px 14px;
    margin-bottom: 14px;
    background: #fff7ed;
    border: 1px solid #fed7aa;
    border-radius: 6px;
    font-weight: 700;
}

.flash-bar {
    padding-bottom: 0;
}

.login-shell {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 18px;
}

.login-box {
    width: min(420px, 100%);
    display: grid;
    gap: 18px;
    padding: 24px;
}

.footer {
    width: min(1600px, 100%);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin: 0 auto;
    padding: 0 clamp(16px, 4vw, 44px) 28px;
    color: var(--muted);
    font-size: 12px;
    font-weight: 700;
}

@media (max-width: 980px) {
    .app-shell {
        grid-template-columns: 1fr;
        grid-template-areas:
            "metrics"
            "create"
            "sidebar"
            "detail";
    }

    .source-create,
    .source-sidebar,
    .detail-pane {
        grid-column: auto;
        grid-row: auto;
    }

    .source-sidebar {
        position: static;
    }

    .upstream-form,
    .upstream-edit-form,
    .derive-form {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .derive-form {
        grid-template-columns: 1fr;
    }

    .upstream-form .wide,
    .upstream-edit-form .wide,
    .upstream-edit-form button {
        grid-column: 1 / -1;
    }

    .section-head {
        align-items: flex-start;
        flex-direction: column;
    }

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

    .form-head {
        align-items: stretch;
        flex-direction: column;
    }

    .detail-grid,
    .sub-row,
    .sub-detail-line,
    .sub-edit-line {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 680px) {
    .metrics,
    .stat-row,
    .detail-grid,
    .upstream-edit-form,
    .upstream-form,
    .derive-form,
    .derive-fields,
    .sub-fields,
    .copy-line {
        grid-template-columns: 1fr;
    }

    .topbar,
    .topnav,
    .footer {
        align-items: flex-start;
        flex-direction: column;
    }

    .card-head,
    .card-actions {
        align-items: stretch;
        flex-direction: column;
    }

    .card-actions form,
    .card-actions button {
        width: 100%;
    }
}
