:root {
    --bg: #eef1f5;
    --surface: #ffffff;
    --surface-2: #f7f8fa;
    --text: #171a21;
    --muted: #6b7280;
    --border: #e2e5ea;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --primary-soft: #e6effe;
    --success: #16a34a;
    --success-soft: #dcfce7;
    --danger: #dc2626;
    --danger-soft: #fee2e2;
    --warning: #b45309;
    --warning-soft: #fef3c7;
    --radius: 12px;
    --radius-sm: 8px;
    --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .1);
    --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    --mono: ui-monospace, "SF Mono", "SFMono-Regular", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

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

/* ---------------------------- App shell ---------------------------- */
.app { display: flex; min-height: 100vh; }

.sidebar {
    width: 250px;
    flex-shrink: 0;
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    padding: 20px 14px;
    position: sticky;
    top: 0;
    height: 100vh;
}

.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 20px; }
.brand-mark {
    width: 34px; height: 34px; border-radius: 9px;
    background: var(--primary); color: #fff;
    display: grid; place-items: center; font-weight: 700; font-size: 14px;
}
.brand-name { font-weight: 650; font-size: 16px; }

.nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav-link {
    display: flex; align-items: center; gap: 10px;
    padding: 10px 12px; border-radius: var(--radius-sm);
    color: var(--muted); font-weight: 500; font-size: 14px;
}
.nav-link:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.nav-link.active { background: var(--primary-soft); color: var(--primary); }
.nav-ico { font-size: 15px; width: 18px; text-align: center; }

.sidebar-foot { border-top: 1px solid var(--border); padding-top: 14px; display: flex; flex-direction: column; gap: 10px; }
.user-chip { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.user-meta { display: flex; flex-direction: column; gap: 2px; font-size: 13px; overflow: hidden; }
.user-meta strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar {
    display: flex; align-items: center; gap: 12px;
    padding: 16px 28px; background: var(--surface);
    border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 20;
}
.topbar-title { font-size: 18px; font-weight: 650; margin: 0; }
#menuToggle { display: none; }
.content { padding: 28px; max-width: 1200px; width: 100%; }

/* ---------------------------- Auth ---------------------------- */
.auth-shell {
    min-height: 100vh; display: flex; flex-direction: column;
    align-items: center; justify-content: center; padding: 24px; gap: 14px;
}
.auth-card {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); box-shadow: var(--shadow);
    padding: 32px; width: 100%; max-width: 380px;
}
.auth-card h1 { margin: 0 0 4px; font-size: 22px; }
.auth-card .sub { color: var(--muted); margin: 0 0 24px; font-size: 14px; }
.auth-brand { display: flex; align-items: center; gap: 10px; justify-content: center; margin-bottom: 18px; }

/* ---------------------------- Cards / layout ---------------------------- */
.card {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); box-shadow: var(--shadow);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}
.card:hover {
    box-shadow: 0 6px 20px rgba(16, 24, 40, 0.04);
}
.card-pad { padding: 20px 22px; }
.card + .card { margin-top: 18px; }
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.stack { display: flex; flex-direction: column; gap: 16px; }
.row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.between { justify-content: space-between; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head h2 { margin: 0; font-size: 20px; }
.muted { color: var(--muted); }
.mono { font-family: var(--mono); font-size: 13px; }

/* ---------------------------- Buttons ---------------------------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 7px;
    font-family: inherit; font-size: 14px; font-weight: 550; line-height: 1;
    padding: 10px 16px; border-radius: var(--radius-sm);
    border: 1px solid transparent; cursor: pointer; transition: background .15s ease, border-color .15s ease, transform .15s ease, box-shadow .15s ease;
    text-decoration: none;
}
.btn:hover { text-decoration: none; transform: translateY(-1px); box-shadow: 0 4px 10px rgba(0,0,0,0.06); }
.btn:active { transform: translateY(0); box-shadow: none; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn-secondary:hover { background: var(--surface-2); }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { filter: brightness(.94); }
.btn-danger-soft { background: var(--danger-soft); color: var(--danger); }
.btn-success { background: var(--success); color: #fff; }
.btn-success:hover { filter: brightness(.94); }
.btn-sm { padding: 7px 11px; font-size: 13px; }
.btn-icon { padding: 8px 10px; }
.btn-block { width: 100%; }

/* ---------------------------- Forms ---------------------------- */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 16px; }
.field label { font-size: 13px; font-weight: 550; }
.field .hint { font-size: 12px; color: var(--muted); }
.input, .select, .textarea {
    font-family: inherit; font-size: 14px; color: var(--text);
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius-sm); padding: 10px 12px; width: 100%;
}
.input:focus, .select:focus, .textarea:focus {
    outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft);
}
.textarea { min-height: 90px; resize: vertical; font-family: var(--mono); }
.checkline { display: flex; align-items: center; gap: 9px; font-size: 14px; }
.checkline input { width: 16px; height: 16px; }

/* ---------------------------- Tables ---------------------------- */
.table-wrap { overflow-x: auto; border-radius: var(--radius); border: 1px solid var(--border); }
table { width: 100%; border-collapse: collapse; background: var(--surface); }
th, td { text-align: left; padding: 12px 16px; font-size: 14px; border-bottom: 1px solid var(--border); }
th { background: var(--surface-2); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
tr { transition: background-color 0.15s ease, opacity 0.15s ease; }
tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--surface-2); }
.row-link { cursor: pointer; }

/* ---------------------------- Badges / tags ---------------------------- */
.tag { display: inline-flex; align-items: center; gap: 5px; padding: 3px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.tag-admin { background: var(--primary-soft); color: var(--primary); }
.tag-user { background: var(--surface-2); color: var(--muted); }
.tag-new { background: var(--primary-soft); color: var(--primary); }
.tag-archived { background: var(--surface-2); color: var(--muted); }
.tag-active { background: var(--success-soft); color: var(--success); }
.tag-inactive { background: var(--danger-soft); color: var(--danger); }
.tag-ok { background: var(--success-soft); color: var(--success); }
.tag-failed { background: var(--danger-soft); color: var(--danger); }
.tag-pending { background: var(--warning-soft); color: var(--warning); }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }

/* ---------------------------- Alerts ---------------------------- */
.alert { padding: 12px 16px; border-radius: var(--radius-sm); font-size: 14px; margin-bottom: 18px; border: 1px solid transparent; }
.alert-success { background: var(--success-soft); color: var(--success); }
.alert-error { background: var(--danger-soft); color: var(--danger); }
.alert-info { background: var(--primary-soft); color: var(--primary); }

/* ---------------------------- Stats ---------------------------- */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 22px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); }
.stat .n { font-size: 26px; font-weight: 700; }
.stat .l { font-size: 13px; color: var(--muted); }

/* ---------------------------- Filters ---------------------------- */
.filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; margin-bottom: 18px; }
.filters .field { margin-bottom: 0; }
.filters .field { min-width: 150px; }

/* ---------------------------- Detail / kv ---------------------------- */
.kv { display: grid; grid-template-columns: 200px 1fr; gap: 0; }
.kv > div { padding: 12px 16px; border-bottom: 1px solid var(--border); font-size: 14px; }
.kv > div:nth-child(odd) { background: var(--surface-2); font-weight: 550; color: var(--muted); }
.kv > div:nth-child(even) { word-break: break-word; }
.kv > div:nth-last-child(-n+2) { border-bottom: none; }

.channel-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 0; border-bottom: 1px solid var(--border); }
.channel-row:last-child { border-bottom: none; }
.channel-icon { width: 36px; height: 36px; border-radius: 9px; background: var(--surface-2); display: grid; place-items: center; font-size: 15px; }

.empty { text-align: center; padding: 48px 20px; color: var(--muted); }
.empty .big { font-size: 15px; color: var(--text); font-weight: 550; margin-bottom: 4px; }

.token-box { display: flex; gap: 8px; align-items: center; }
.token-box code { background: var(--surface-2); padding: 8px 12px; border-radius: var(--radius-sm); border: 1px solid var(--border); flex: 1; overflow-x: auto; }

pre.code { background: #0f1117; color: #d4d7de; padding: 16px; border-radius: var(--radius-sm); overflow-x: auto; font-size: 12.5px; line-height: 1.6; }
pre.code .c { color: #6b7280; }

hr.sep { border: none; border-top: 1px solid var(--border); margin: 22px 0; }

/* ---------------------------- Page head / actions ---------------------------- */
.page-head h1 { margin: 0 0 4px; font-size: 22px; }
.page-head p { margin: 0; }
.head-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.back-link { font-size: 13px; color: var(--muted); display: inline-block; margin-bottom: 8px; }
.card-title { margin: 0 0 16px; font-size: 15px; font-weight: 650; }
.card { padding: 20px 22px; }
.card-title:first-child { margin-top: 0; }

/* ---------------------------- Filters ---------------------------- */
.filter-grid { display: grid; grid-template-columns: 2fr repeat(4, 1fr); gap: 12px; align-items: end; }
.filters .field, .filter-grid .field { margin-bottom: 0; }
.filter-grid .field span { font-size: 13px; font-weight: 550; margin-bottom: 6px; display: block; }
.filter-grid input, .filter-grid select {
    font-family: inherit; font-size: 14px; color: var(--text);
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius-sm); padding: 9px 11px; width: 100%;
}
.filter-grid input:focus, .filter-grid select:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.filter-actions { display: flex; gap: 8px; margin-top: 14px; }

/* Generic field span-label form fields (admin/user forms) */
.field > span { font-size: 13px; font-weight: 550; }
.field input:not([type=checkbox]), .field select, .field textarea {
    font-family: inherit; font-size: 14px; color: var(--text);
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius-sm); padding: 10px 12px; width: 100%;
}
.field textarea { min-height: 80px; resize: vertical; }
.field input:focus, .field select:focus, .field textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
fieldset.field { border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 12px 14px; }
fieldset.field legend { font-size: 13px; font-weight: 550; padding: 0 6px; }
.check-list { display: flex; flex-direction: column; gap: 8px; }
.check { display: flex; align-items: center; gap: 8px; font-size: 14px; font-weight: 400; }
.check input { width: 16px; height: 16px; }

/* ---------------------------- Table extras ---------------------------- */
.table-card { padding: 0; overflow: hidden; }
.table-card .table { border-radius: var(--radius); }
.nowrap { white-space: nowrap; }
.preview { color: var(--muted); max-width: 380px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
tr.is-archived td { opacity: .6; }
.row-actions { display: flex; gap: 6px; align-items: center; justify-content: flex-end; }
.row-actions form { margin: 0; }

/* ---------------------------- Badges & chips ---------------------------- */
.badge { display: inline-flex; align-items: center; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 600; background: var(--surface-2); color: var(--muted); text-transform: capitalize; }
.badge-accent { background: var(--primary-soft); color: var(--primary); }
.chip { display: inline-flex; align-items: center; padding: 3px 9px; border-radius: 999px; font-size: 11px; font-weight: 600; margin: 1px; text-transform: capitalize; }
.chip-ok { background: var(--success-soft); color: var(--success); }
.chip-bad { background: var(--danger-soft); color: var(--danger); }
.chip-warn { background: var(--warning-soft); color: var(--warning); }

/* ---------------------------- Data / meta lists ---------------------------- */
.data-list { margin: 0; }
.data-row { display: grid; grid-template-columns: 160px 1fr; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.data-row:last-child { border-bottom: none; }
.data-row dt { font-weight: 600; color: var(--muted); font-size: 13px; word-break: break-word; }
.data-row dd { margin: 0; word-break: break-word; }
.meta-list { margin: 0; display: flex; flex-direction: column; gap: 10px; }
.meta-list > div { display: flex; justify-content: space-between; gap: 12px; font-size: 14px; }
.meta-list dt { color: var(--muted); }
.meta-list dd { margin: 0; text-align: right; }
.small { font-size: 12px; }

.delivery-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 14px; }
.delivery-list > li { padding-bottom: 12px; border-bottom: 1px solid var(--border); }
.delivery-list > li:last-child { border-bottom: none; padding-bottom: 0; }
.delivery-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 3px; }
.history { margin-top: 8px; font-size: 13px; }
.history summary { cursor: pointer; color: var(--muted); }
.history ul { margin: 8px 0 0; padding-left: 16px; }
.history li.ok { color: var(--success); }
.history li.bad { color: var(--danger); }

/* ---------------------------- Endpoint / token ---------------------------- */
.endpoint-box { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.endpoint-box code { background: var(--surface-2); padding: 8px 12px; border-radius: var(--radius-sm); border: 1px solid var(--border); flex: 1; overflow-x: auto; font-size: 13px; }

.pager { display: flex; align-items: center; justify-content: center; gap: 16px; margin-top: 18px; }

.channel-card { border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 16px; margin-bottom: 12px; }
.channel-card.disabled { opacity: .65; }
.channel-card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; }

/* ---------------------------- Responsive ---------------------------- */
@media (max-width: 860px) {
    .sidebar { position: fixed; z-index: 50; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow); }
    .sidebar.open { transform: translateX(0); }
    #menuToggle { display: inline-flex; }
    .stats { grid-template-columns: repeat(2, 1fr); }
    .grid-2, .grid-3 { grid-template-columns: 1fr; }
    .kv { grid-template-columns: 1fr; }
    .kv > div:nth-child(odd) { padding-bottom: 2px; }
    .content { padding: 18px; }
    .filter-grid { grid-template-columns: 1fr 1fr; }
    .data-row { grid-template-columns: 1fr; gap: 2px; }
}
