:root {
    --bg: #f4f6fa;
    --surface: #ffffff;
    --surface-2: #f8fafc;
    --border: #e3e8ef;
    --text: #1f2937;
    --muted: #6b7280;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --primary-soft: #eff4ff;
    --danger: #dc2626;
    --danger-hover: #b91c1c;
    --ok: #15803d;
    --warn: #b45309;
    --radius: 10px;
    --radius-sm: 6px;
    --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 1px 3px rgba(16, 24, 40, .06);
    --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif;
    font-size: 14px;
    line-height: 1.5;
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

h1, h2, h3 { margin: 0; color: #111827; line-height: 1.25; }
h1 { font-size: 22px; font-weight: 650; }
h2 { font-size: 15px; font-weight: 600; }

code, .mono { font-family: var(--mono); font-size: 12.5px; }
code {
    background: #f1f4f9;
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 1px 6px;
    color: #374151;
}
kbd {
    font-family: var(--mono);
    font-size: 11px;
    padding: 1px 5px;
    border-radius: 4px;
    background: rgba(255, 255, 255, .2);
    margin-left: 6px;
}

[hidden] { display: none !important; }

.muted { color: var(--muted); }
.small { font-size: 12px; }
.strong { font-weight: 600; }
.right { text-align: right; }
.ellipsis { max-width: 340px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- Top bar ---------- */
.topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    background: #0f172a;
    box-shadow: 0 1px 0 rgba(255, 255, 255, .04);
}
.topbar-inner {
    max-width: 1240px;
    margin: 0 auto;
    padding: 0 24px;
    height: 56px;
    display: flex;
    align-items: center;
    gap: 28px;
}
.brand { color: #fff; font-weight: 700; font-size: 16px; letter-spacing: .2px; }
.brand span { color: #60a5fa; }
.brand:hover { text-decoration: none; }
.nav { display: flex; gap: 4px; flex: 1; overflow-x: auto; }
.nav a, .nav-logout {
    color: #cbd5e1;
    padding: 7px 12px;
    border-radius: var(--radius-sm);
    font-weight: 500;
    white-space: nowrap;
}
.nav a:hover, .nav-logout:hover { color: #fff; background: rgba(255, 255, 255, .06); text-decoration: none; }
.nav a.active { color: #fff; background: rgba(96, 165, 250, .18); }

/* ---------- Layout ---------- */
.page {
    max-width: 1240px;
    margin: 0 auto;
    padding: 28px 24px 96px;
}
.page-header {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 20px;
    flex-wrap: wrap;
}
.page-header p { margin: 6px 0 0; }
.back { display: inline-block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }

.grid-2 {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 20px;
    margin-bottom: 20px;
}
.grid-2 > .card { margin-bottom: 0; }

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 20px;
    margin-bottom: 20px;
}
.card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 16px;
}
.count {
    display: inline-block;
    min-width: 22px;
    padding: 0 7px;
    margin-left: 6px;
    border-radius: 999px;
    background: #eef2f7;
    color: var(--muted);
    font-size: 12px;
    font-weight: 600;
    text-align: center;
}
.collapsible { display: none; }
.collapsible.open { display: block; animation: slide .18s ease-out; }
@keyframes slide { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: none; } }

/* ---------- Stats ---------- */
.stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 16px;
    margin-bottom: 20px;
}
.stat {
    display: flex;
    flex-direction: column;
    gap: 4px;
    padding: 18px 20px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    color: inherit;
    transition: border-color .15s, transform .15s;
}
.stat:hover { border-color: #bfd0f5; text-decoration: none; transform: translateY(-1px); }
.stat-label { color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .5px; }
.stat-value { font-size: 30px; font-weight: 700; color: #0f172a; }
.stat-sub { color: var(--muted); font-size: 12.5px; }

.list { list-style: none; margin: 0; padding: 0; }
.list li {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 9px 0;
    border-bottom: 1px dashed var(--border);
}
.list li:last-child { border-bottom: none; }

.kv { display: grid; grid-template-columns: max-content 1fr; gap: 10px 20px; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-all; }

.empty {
    margin: 0;
    padding: 24px;
    text-align: center;
    color: var(--muted);
    background: var(--surface-2);
    border: 1px dashed var(--border);
    border-radius: var(--radius-sm);
}
.hint { margin: 6px 0 0; color: var(--muted); font-size: 12.5px; }

/* ---------- Forms ---------- */
.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 0 20px;
}
.span-2 { grid-column: 1 / -1; }
.form-group { margin-bottom: 16px; }
.form-actions { display: flex; justify-content: flex-end; gap: 10px; }

label {
    display: block;
    font-weight: 500;
    font-size: 13px;
    margin-bottom: 6px;
    color: #374151;
}
label.check { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; margin: 0; }

input[type="text"], input[type="password"], input[type="date"], input[type="url"],
input[type="search"], select, textarea {
    width: 100%;
    padding: 8px 11px;
    font: inherit;
    color: var(--text);
    background: #fff;
    border: 1px solid #d1d9e4;
    border-radius: var(--radius-sm);
    transition: border-color .15s, box-shadow .15s;
}
input.mono { font-family: var(--mono); font-size: 12.5px; }
input[readonly] { background: var(--surface-2); color: #374151; }
input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, .15);
}
input[type="checkbox"] { width: 16px; height: 16px; accent-color: var(--primary); margin: 0; }
.search { max-width: 260px; padding: 6px 10px; }

.input-row { display: flex; gap: 8px; align-items: center; }
.input-row > input, .input-row > select { flex: 1; min-width: 0; }

.sticky-actions {
    position: sticky;
    bottom: 0;
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    padding: 14px 0;
    margin-top: 4px;
    background: linear-gradient(to top, var(--bg) 70%, rgba(244, 246, 250, 0));
}

/* ---------- Buttons ---------- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 8px 14px;
    font: inherit;
    font-weight: 500;
    line-height: 1.2;
    color: #1f2937;
    background: #fff;
    border: 1px solid #d1d9e4;
    border-radius: var(--radius-sm);
    cursor: pointer;
    white-space: nowrap;
    transition: background .15s, border-color .15s, color .15s;
}
.btn:hover { background: #f3f6fa; text-decoration: none; }
.btn:disabled { opacity: .6; cursor: progress; }
.btn-primary { color: #fff; background: var(--primary); border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-danger { color: var(--danger); border-color: #f3c4c4; background: #fff; }
.btn-danger:hover { color: #fff; background: var(--danger); border-color: var(--danger); }
.btn-ghost { background: transparent; border-color: transparent; color: #374151; }
.btn-ghost:hover { background: #eaeef4; }
.btn-sm { padding: 5px 10px; font-size: 12.5px; }
.btn.loading::after {
    content: "";
    width: 12px;
    height: 12px;
    border: 2px solid currentColor;
    border-right-color: transparent;
    border-radius: 50%;
    animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.btn-row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.btn-row.end { justify-content: flex-end; }
.btn-row form { margin: 0; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; margin: 0 -20px -20px; border-top: 1px solid var(--border); }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 12px 20px; text-align: left; vertical-align: middle; border-bottom: 1px solid #eef1f5; }
th {
    background: var(--surface-2);
    color: var(--muted);
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .4px;
    white-space: nowrap;
}
th.right, td.right { text-align: right; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: #fafbfd; }
.token {
    display: inline-block;
    max-width: 180px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    vertical-align: middle;
    margin-top: 3px;
    color: var(--muted);
}

/* ---------- Badges / chips ---------- */
.badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 500;
    white-space: nowrap;
    border: 1px solid transparent;
}
.badge-ok { color: var(--ok); background: #ecfdf3; border-color: #c6f0d6; }
.badge-warn { color: var(--warn); background: #fffaeb; border-color: #fde5b3; }
.badge-danger { color: var(--danger); background: #fef2f2; border-color: #fbd0d0; }
.badge-info { color: #1d4ed8; background: var(--primary-soft); border-color: #cddcfd; }
.badge-purple { color: #6d28d9; background: #f5f3ff; border-color: #ddd6fe; }
.badge-muted { color: var(--muted); background: #f3f4f6; border-color: #e5e7eb; }

.chips { display: flex; flex-wrap: wrap; gap: 4px; max-width: 320px; }
.chip {
    padding: 1px 8px;
    border-radius: 999px;
    font-size: 12px;
    background: #f1f4f9;
    border: 1px solid var(--border);
    color: #374151;
}

/* ---------- Upstream picker ---------- */
.picker { border: 1px solid #d1d9e4; border-radius: var(--radius-sm); overflow: hidden; }
.picker-toolbar {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px;
    background: var(--surface-2);
    border-bottom: 1px solid var(--border);
    flex-wrap: wrap;
}
.picker-count { margin-left: auto; font-size: 12px; padding-right: 4px; }
.picker-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 6px;
    padding: 8px;
    max-height: 280px;
    overflow-y: auto;
}
.picker-item {
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 0;
    padding: 7px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-weight: 400;
    transition: border-color .12s, background .12s;
}
.picker-item:hover { border-color: #bfd0f5; }
.picker-item.checked { background: var(--primary-soft); border-color: #a9c1f8; }
.picker-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- Alerts / toast ---------- */
.alert {
    padding: 11px 14px;
    margin-bottom: 18px;
    border-radius: var(--radius-sm);
    border: 1px solid;
    transition: opacity .4s, margin .4s, padding .4s, max-height .4s;
    max-height: 120px;
    overflow: hidden;
}
.alert-success { color: #166534; background: #f0fdf4; border-color: #bbf7d0; }
.alert-error { color: #991b1b; background: #fef2f2; border-color: #fecaca; }
.alert.fade { opacity: 0; max-height: 0; padding-top: 0; padding-bottom: 0; margin: 0; border-width: 0; }

.toast {
    position: fixed;
    left: 50%;
    bottom: 28px;
    transform: translate(-50%, 20px);
    padding: 9px 16px;
    border-radius: 999px;
    background: #0f172a;
    color: #fff;
    font-size: 13px;
    opacity: 0;
    pointer-events: none;
    transition: opacity .18s, transform .18s;
    z-index: 50;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- CodeMirror ---------- */
.CodeMirror {
    border-radius: var(--radius-sm);
    font-family: var(--mono);
    font-size: 13px;
    line-height: 1.55;
}

/* ---------- Login / home ---------- */
.login-page {
    min-height: 100vh;
    padding: 24px;
    background:
        linear-gradient(rgba(15, 23, 42, .35), rgba(15, 23, 42, .5)),
        url('/static/bg.jpg') center center / cover no-repeat;
    display: flex;
    align-items: center;
    justify-content: center;
}
.home-page {
    min-height: 100vh;
    background: url('/static/bg.jpg') center center / cover no-repeat;
}
.login-wrapper { width: 100%; max-width: 400px; }
.login-card {
    margin: 0;
    padding: 28px;
    background: rgba(255, 255, 255, .9);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border: 1px solid rgba(255, 255, 255, .6);
    border-radius: 14px;
    box-shadow: 0 20px 50px rgba(0, 0, 0, .25);
}
.login-card h1 { margin-bottom: 20px; }
.login-btn { width: 100%; padding: 10px 12px; }
.error-msg {
    color: #991b1b;
    background: #fef2f2;
    border: 1px solid #fecaca;
    border-radius: var(--radius-sm);
    padding: 8px 12px;
}

/* ---------- Responsive ---------- */
@media (max-width: 860px) {
    .grid-2 { grid-template-columns: 1fr; }
    .topbar-inner { gap: 12px; padding: 0 14px; }
    .page { padding: 20px 14px 96px; }
    .card { padding: 16px; }
    .table-wrap { margin: 0 -16px -16px; }
    th, td { padding: 10px 14px; }
    .search { max-width: 160px; }
}
@media (max-width: 560px) {
    .brand { display: none; }
    .card-header { flex-wrap: wrap; }
    .search { max-width: none; }
}
