:root {
    --primary: #6366f1;
    --primary-hover: #4f46e5;
    --primary-light: #eef2ff;
    --green: #10b981;
    --green-light: #ecfdf5;
    --green-border: #a7f3d0;
    --red: #ef4444;
    --red-light: #fef2f2;
    --red-border: #fecaca;
    --amber: #f59e0b;
    --amber-light: #fffbeb;
    --amber-border: #fde68a;
    --blue: #3b82f6;
    --blue-light: #eff6ff;
    --blue-border: #bfdbfe;
    --text: #0f172a;
    --text-secondary: #64748b;
    --text-muted: #94a3b8;
    --bg: #f1f5f9;
    --surface: #ffffff;
    --border: #e2e8f0;
    --radius: 12px;
    --radius-sm: 8px;
    --shadow: 0 1px 3px rgba(0,0,0,.06);
    --shadow-md: 0 4px 12px rgba(0,0,0,.08);
    --shadow-lg: 0 10px 24px rgba(0,0,0,.1);
    --transition: 180ms ease;
}

*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; min-height: 100vh; }
body {
    font-family: "Inter", "Noto Sans SC", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
    font-size: 14px;
    line-height: 1.5;
    color: var(--text);
    background: var(--bg);
    -webkit-font-smoothing: antialiased;
}
input, select, textarea, button { font: inherit; }
.hidden { display: none !important; }

/* ── Layout ── */
.layout {
    display: grid;
    grid-template-columns: 250px minmax(0, 1fr);
    min-height: 100vh;
}

/* ── Sidebar ── */
.sidebar {
    background: #0f172a;
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
    z-index: 20;
}

.sidebar-header { padding: 20px 16px 12px; }

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    text-decoration: none;
    color: inherit;
}

.sidebar-logo {
    width: 34px;
    height: 34px;
    background: var(--primary);
    border-radius: 9px;
    display: grid;
    place-items: center;
    color: #fff;
    font-weight: 800;
    font-size: 16px;
    flex-shrink: 0;
}

.sidebar-title { color: #f1f5f9; font-size: 15px; font-weight: 700; }
.sidebar-sub { color: var(--text-muted); font-size: 11px; margin-top: 2px; }

.sidebar-nav { padding: 8px 10px; flex: 1; }

.nav-section {
    padding: 16px 8px 6px;
    font-size: 10px;
    font-weight: 700;
    letter-spacing: .12em;
    text-transform: uppercase;
    color: rgba(148,163,184,.5);
}

.nav-section:first-child { padding-top: 8px; }

.nav-item {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    border: none;
    background: transparent;
    color: #94a3b8;
    text-align: left;
    font-size: 13px;
    font-weight: 500;
    padding: 9px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all var(--transition);
    position: relative;
    margin-bottom: 1px;
}

.nav-item svg { width: 18px; height: 18px; flex-shrink: 0; opacity: .65; }
.nav-item:hover { background: rgba(99,102,241,.08); color: #e2e8f0; }
.nav-item:hover svg { opacity: 1; }

.nav-item.is-active {
    background: rgba(99,102,241,.15);
    color: #a5b4fc;
    font-weight: 600;
}

.nav-item.is-active svg { opacity: 1; }

.nav-item.is-active::before {
    content: "";
    position: absolute;
    left: 0;
    top: 6px;
    bottom: 6px;
    width: 3px;
    border-radius: 0 3px 3px 0;
    background: var(--primary);
}

.sidebar-footer {
    padding: 12px 18px 16px;
    border-top: 1px solid rgba(148,163,184,.12);
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.sidebar-link {
    color: #a5b4fc;
    font-size: 12px;
    font-weight: 500;
    text-decoration: none;
    transition: color var(--transition);
}

.sidebar-link:hover { color: #c7d2fe; }

.sidebar-origin {
    font-size: 11px;
    color: var(--text-muted);
    background: rgba(148,163,184,.08);
    border: 1px solid rgba(148,163,184,.1);
    border-radius: 4px;
    padding: 5px 8px;
    font-family: "JetBrains Mono", "Consolas", monospace;
    margin-top: 4px;
}

/* ── Mobile ── */
.mobile-toggle {
    display: none;
    position: fixed;
    top: 14px;
    left: 14px;
    z-index: 50;
    width: 38px;
    height: 38px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow-md);
}

.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(15,23,42,.55);
    z-index: 15;
}

.sidebar-overlay.is-open { display: block; }

/* ── Auth Screen ── */
.auth-screen {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 24px;
    background: linear-gradient(135deg, #0f172a 0%, #1e293b 50%, #312e81 100%);
}

.auth-card {
    width: 100%;
    max-width: 380px;
    background: var(--surface);
    border-radius: var(--radius);
    padding: 40px 32px 32px;
    box-shadow: var(--shadow-lg);
    text-align: center;
}

.auth-card .auth-logo {
    width: 56px;
    height: 56px;
    background: var(--primary);
    border-radius: 14px;
    display: grid;
    place-items: center;
    color: #fff;
    font-weight: 800;
    font-size: 24px;
    margin: 0 auto 16px;
}

.auth-card h1 { margin: 0 0 4px; font-size: 22px; font-weight: 700; }
.auth-card > p { margin: 0 0 28px; color: var(--text-secondary); font-size: 14px; }

.auth-form {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.auth-form input {
    text-align: center;
    padding: 12px 14px;
}

.btn-block { width: 100%; }

.auth-back {
    display: inline-block;
    margin-top: 20px;
    font-size: 13px;
    color: var(--text-secondary);
    text-decoration: none;
}

.auth-back:hover { color: var(--primary); }

/* ── Main ── */
.main {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

/* ── Top Bar ── */
.top-bar {
    position: sticky;
    top: 0;
    z-index: 10;
    background: rgba(255,255,255,.92);
    backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border);
    padding: 10px 24px;
}

.top-bar-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
}

.top-bar-status {
    display: flex;
    align-items: center;
    gap: 12px;
}

.auth-chip {
    display: inline-flex;
    align-items: center;
    border-radius: 999px;
    padding: 6px 14px;
    font-size: 12px;
    font-weight: 600;
    white-space: nowrap;
    border: 1px solid var(--amber-border);
    background: var(--amber-light);
    color: #92400e;
    flex-shrink: 0;
}

.auth-chip-ok { border-color: var(--green-border); background: var(--green-light); color: #065f46; }
.auth-chip-err { border-color: var(--red-border); background: var(--red-light); color: #991b1b; }
.auth-chip-idle { border-color: var(--amber-border); background: var(--amber-light); color: #92400e; }

/* ── Content ── */
.content {
    padding: 24px;
    max-width: 1400px;
    width: 100%;
}

/* ── Inputs ── */
input, select, textarea {
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 8px 12px;
    background: var(--surface);
    color: var(--text);
    transition: border-color var(--transition), box-shadow var(--transition);
    font-size: 13px;
}

input::placeholder, textarea::placeholder { color: var(--text-muted); }

input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(99,102,241,.1);
}

.w-80 { width: 80px; }

/* ── Buttons ── */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    border-radius: var(--radius-sm);
    padding: 8px 14px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: all var(--transition);
    white-space: nowrap;
    font-family: inherit;
}

.btn:hover { background: var(--bg); border-color: #cbd5e1; }
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); box-shadow: 0 4px 14px rgba(99,102,241,.2); }

.btn-danger { background: var(--red); border-color: var(--red); color: #fff; }
.btn-danger:hover { background: #dc2626; border-color: #dc2626; }

.btn-ghost { background: transparent; border-color: transparent; color: var(--text-secondary); }
.btn-ghost:hover { background: var(--bg); color: var(--text); }

.btn-sm { padding: 6px 10px; font-size: 12px; }

.btn-group { display: flex; gap: 6px; }

.btn.is-loading { pointer-events: none; opacity: .7; }

/* ── Views ── */
.view { display: none; animation: fadeIn 200ms ease; }
.view.is-active { display: flex; flex-direction: column; gap: 16px; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; } }

.view-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
}

.view-head h2 { margin: 0; font-size: 20px; font-weight: 700; }

/* ── Cards ── */
.cards {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
    gap: 12px;
}

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 18px;
    position: relative;
    overflow: hidden;
    transition: box-shadow var(--transition);
}

.card::before {
    content: "";
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 3px;
    background: var(--primary);
    opacity: .5;
}

.card:nth-child(2)::before { background: var(--green); }
.card:nth-child(3)::before { background: var(--blue); }
.card:nth-child(4)::before { background: var(--amber); }
.card:nth-child(5)::before { background: #8b5cf6; }
.card:nth-child(6)::before { background: #ec4899; }
.card:nth-child(7)::before { background: #14b8a6; }
.card:nth-child(8)::before { background: #f97316; }

.card:hover { box-shadow: var(--shadow-md); }

.card h4 {
    margin: 0;
    color: var(--text-secondary);
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .04em;
}

.card strong {
    display: block;
    margin-top: 6px;
    font-size: 22px;
    font-weight: 700;
}

.card small {
    display: block;
    margin-top: 4px;
    color: var(--text-secondary);
    font-size: 12px;
}

/* ── Panel ── */
.panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 18px;
    display: flex;
    flex-direction: column;
    gap: 14px;
}

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

.panel-head h3 { margin: 0; font-size: 14px; font-weight: 600; }

.panel-stack { display: flex; flex-direction: column; gap: 14px; }

/* ── Grid ── */
.grid-2 {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 14px;
}

/* ── Filter Bar ── */
.filter-bar {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    align-items: center;
}

.filter-bar input, .filter-bar select {
    min-width: 0;
}

.filter-bar input:not(.w-80) { flex: 1; min-width: 140px; }

.filter-bar-wide { flex-wrap: wrap; }

/* ── Forms ── */
.form-stack {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.form-stack h4 { margin: 0; font-size: 14px; font-weight: 600; }

.form-group {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.form-group label {
    font-size: 11px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: .04em;
}

.form-inline {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    align-items: flex-end;
}

.form-inline .form-group { flex: 1; min-width: 140px; }
.form-inline .form-action { flex: 0 0 auto; min-width: auto; }

.form-row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.form-row-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }

/* ── Tables ── */
.table-wrap {
    overflow-x: auto;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
}

table { width: 100%; border-collapse: collapse; min-width: 800px; }

thead th {
    text-align: left;
    font-size: 11px;
    font-weight: 600;
    letter-spacing: .04em;
    text-transform: uppercase;
    color: var(--text-secondary);
    border-bottom: 1px solid var(--border);
    padding: 10px 12px;
    background: #f8fafc;
    white-space: nowrap;
}

tbody td {
    padding: 10px 12px;
    border-bottom: 1px solid #f1f5f9;
    font-size: 13px;
    vertical-align: middle;
}

tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: #f8fafc; }

.action-group { display: flex; gap: 4px; }

.mini-btn {
    border: 1px solid var(--border);
    background: var(--surface);
    border-radius: 6px;
    padding: 4px 10px;
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition);
    white-space: nowrap;
    font-family: inherit;
}

.mini-btn:hover { background: var(--bg); border-color: #cbd5e1; }
.mini-btn.primary { border-color: var(--primary); color: var(--primary); }
.mini-btn.primary:hover { background: var(--primary-light); }
.mini-btn.danger { border-color: var(--red-border); color: var(--red); }
.mini-btn.danger:hover { background: var(--red-light); }

.pagination { font-size: 12px; color: var(--text-secondary); padding: 4px 0; }

/* ── Badges ── */
.badge {
    display: inline-flex;
    align-items: center;
    padding: 2px 10px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 600;
    white-space: nowrap;
    border: 1px solid transparent;
}

.badge-active, .badge-normal, .badge-ok { background: var(--green-light); color: #065f46; border-color: var(--green-border); }
.badge-inactive, .badge-disabled, .badge-draft { background: #f1f5f9; color: #475569; border-color: var(--border); }
.badge-error { background: var(--red-light); color: #991b1b; border-color: var(--red-border); }
.badge-expired, .badge-quota_exhausted, .badge-archived { background: var(--amber-light); color: #92400e; border-color: var(--amber-border); }
.badge-unused { background: var(--blue-light); color: #1e40af; border-color: var(--blue-border); }
.badge-used { background: #f5f3ff; color: #5b21b6; border-color: #ddd6fe; }
.badge-admin { background: #fdf4ff; color: #86198f; border-color: #f0abfc; }
.badge-user { background: var(--blue-light); color: #1e40af; border-color: var(--blue-border); }

/* ── Copy ── */
.copy-wrap { display: inline-flex; align-items: center; gap: 4px; max-width: 100%; }

.copy-wrap code {
    font-size: 12px;
    background: #f1f5f9;
    padding: 3px 8px;
    border-radius: 4px;
    font-family: "JetBrains Mono", "Consolas", monospace;
    color: var(--text);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 160px;
    display: inline-block;
}

.copy-btn {
    flex-shrink: 0;
    border: none;
    background: none;
    cursor: pointer;
    color: var(--text-muted);
    padding: 2px;
    border-radius: 4px;
    transition: all var(--transition);
    display: inline-flex;
    align-items: center;
}

.copy-btn:hover { color: var(--primary); background: rgba(99,102,241,.08); }
.copy-btn.is-copied { color: var(--green); }

/* ── Editable cells ── */
.editable {
    cursor: pointer;
    padding: 2px 8px;
    border-radius: 4px;
    border: 1px dashed transparent;
    transition: all var(--transition);
}

.editable:hover {
    border-color: var(--primary);
    background: var(--primary-light);
    color: var(--primary);
}

/* ── JSON / Log ── */
.json-box, .log-box {
    margin: 0;
    white-space: pre-wrap;
    word-break: break-word;
    background: #1e293b;
    color: #e2e8f0;
    border-radius: var(--radius-sm);
    padding: 14px;
    min-height: 100px;
    max-height: 380px;
    overflow: auto;
    font-family: "JetBrains Mono", "Consolas", monospace;
    font-size: 12px;
    line-height: 1.6;
    border: 1px solid #334155;
}

.json-sm { min-height: 60px; max-height: 200px; }

.code-area {
    width: 100%;
    min-height: 120px;
    resize: vertical;
    font-family: "JetBrains Mono", "Consolas", monospace;
    font-size: 12px;
    line-height: 1.6;
}

/* ── Playground ── */
.playground-row {
    display: grid;
    grid-template-columns: 100px 1fr auto;
    gap: 8px;
}

.checkbox {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    color: var(--text-secondary);
    cursor: pointer;
}

.checkbox input[type="checkbox"] { width: 16px; height: 16px; accent-color: var(--primary); }

/* ── Empty State ── */
.empty-state {
    text-align: center;
    padding: 40px 24px;
    color: var(--text-secondary);
}

.empty-state svg { width: 40px; height: 40px; margin: 0 auto 12px; opacity: .3; display: block; }
.empty-state p { margin: 0; font-size: 13px; font-weight: 500; }

/* ── Toast ── */
.toast-container {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 200;
    display: flex;
    flex-direction: column-reverse;
    gap: 8px;
    max-width: 380px;
    pointer-events: none;
}

.toast {
    pointer-events: auto;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 16px;
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text);
    font-size: 13px;
    font-weight: 500;
    box-shadow: 0 8px 24px rgba(0,0,0,.12);
    border: 1px solid var(--border);
    border-left: 4px solid var(--blue);
    animation: toastIn 250ms ease;
    word-break: break-word;
}

.toast-ok { border-left-color: var(--green); }
.toast-error { border-left-color: var(--red); }

.toast-close {
    margin-left: auto;
    border: none;
    background: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 16px;
    padding: 0 2px;
    opacity: .6;
    transition: opacity var(--transition);
}

.toast-close:hover { opacity: 1; }
.toast.is-leaving { animation: toastOut 200ms ease forwards; }

@keyframes toastIn { from { opacity: 0; transform: translateX(30px); } to { opacity: 1; transform: none; } }
@keyframes toastOut { from { opacity: 1; } to { opacity: 0; transform: translateX(30px); } }

/* ── Modal ── */
.modal {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding: 6vh 16px;
}

.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(15,23,42,.55);
    backdrop-filter: blur(4px);
}

.modal-card {
    position: relative;
    z-index: 1;
    width: min(960px, 100%);
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 20px;
    animation: modalIn 200ms ease;
}

@keyframes modalIn { from { opacity: 0; transform: translateY(-8px) scale(.98); } to { opacity: 1; transform: none; } }

.modal-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal-head h3 { margin: 0; font-size: 15px; font-weight: 600; }

/* ── Confirm Dialog ── */
.confirm-overlay {
    position: fixed;
    inset: 0;
    z-index: 150;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
}

.confirm-overlay .modal-backdrop { position: fixed; inset: 0; background: rgba(15,23,42,.5); backdrop-filter: blur(4px); }

.confirm-card {
    position: relative;
    z-index: 1;
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border);
    padding: 24px;
    max-width: 420px;
    width: 100%;
    animation: modalIn 200ms ease;
}

.confirm-card h3 { margin: 0 0 8px; font-size: 16px; font-weight: 600; }
.confirm-card p { margin: 0 0 20px; color: var(--text-secondary); font-size: 14px; }
.confirm-actions { display: flex; gap: 8px; justify-content: flex-end; }
.confirm-card input.prompt-input { width: 100%; margin-bottom: 16px; }

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }
.json-box::-webkit-scrollbar-thumb, .log-box::-webkit-scrollbar-thumb { background: #475569; }

/* ── Responsive ── */
@media (max-width: 1024px) {
    .layout { grid-template-columns: 1fr; }

    .sidebar {
        position: fixed;
        inset: 0;
        width: 260px;
        transform: translateX(-100%);
        transition: transform 250ms ease;
        z-index: 20;
    }

    .sidebar.is-open { transform: translateX(0); }
    .mobile-toggle { display: flex; }

    .content { padding: 16px; }
    .auth-bar { padding: 12px 16px; }
    .auth-bar-inner { flex-direction: column; align-items: stretch; }
    .auth-input-row { flex-wrap: wrap; }
    .auth-chip { align-self: flex-start; }

    .grid-2 { grid-template-columns: 1fr; }
    .form-row-2, .form-row-3 { grid-template-columns: 1fr; }
    .form-inline { flex-direction: column; }
    .form-inline .form-group { min-width: 0; }
    .playground-row { grid-template-columns: 1fr; }

    .cards { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
}

@media (max-width: 640px) {
    .filter-bar { flex-direction: column; }
    .filter-bar input, .filter-bar select { width: 100%; min-width: 0; }
    .filter-bar .w-80 { width: 100%; }
    .cards { grid-template-columns: 1fr 1fr; }
}
