:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --panel-2: #f1f3f6;
  --border: #e2e5ea;
  --border-strong: #cfd4dc;
  --text: #1b1f27;
  --muted: #667085;
  --faint: #98a2b3;
  --accent: #3b67f0;
  --accent-soft: #e8eeff;
  --accent-text: #ffffff;
  --danger: #d92d20;
  --danger-soft: #fdecea;
  --ok: #12805c;
  --ok-soft: #e3f5ee;
  --warn: #b54708;
  --warn-soft: #fef3e2;
  --null: #a0a8b6;
  --row-hover: #f5f7fb;
  --row-sel: #eef2ff;
  --shadow: 0 10px 30px rgba(16, 24, 40, .12), 0 2px 6px rgba(16, 24, 40, .06);
  --mono: ui-monospace, "JetBrains Mono", "SF Mono", Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --radius: 8px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1116;
    --panel: #151920;
    --panel-2: #1b2029;
    --border: #262c37;
    --border-strong: #343c4a;
    --text: #e6e9ef;
    --muted: #97a1b3;
    --faint: #6b7588;
    --accent: #5b83ff;
    --accent-soft: #1d2744;
    --danger: #ff6b5e;
    --danger-soft: #3a1c1a;
    --ok: #3ccf91;
    --ok-soft: #14301f;
    --warn: #f5a524;
    --warn-soft: #33260f;
    --null: #5d6678;
    --row-hover: #1a1f28;
    --row-sel: #1c2440;
    --shadow: 0 10px 30px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 14px/1.45 var(--sans); -webkit-font-smoothing: antialiased; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
code, pre, .mono { font-family: var(--mono); font-size: 12.5px; }
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -.01em; }
h1 { font-size: 20px; }
h2 { font-size: 16px; }
h3 { font-size: 14px; }

/* ---------- layout */
.shell { display: grid; grid-template-columns: 264px 1fr; height: 100vh; }
.sidebar { background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.brand { display: flex; align-items: center; gap: 10px; padding: 16px 16px 12px; font-weight: 700; letter-spacing: -.01em; }
.brand svg { flex: none; }
.brand small { display: block; font-weight: 500; color: var(--muted); font-size: 11.5px; }
.nav { padding: 4px 8px 8px; display: flex; flex-direction: column; gap: 1px; border-bottom: 1px solid var(--border); }
.nav a { display: flex; align-items: center; gap: 9px; padding: 7px 10px; border-radius: 6px; color: var(--text); }
.nav a:hover { background: var(--panel-2); text-decoration: none; }
.nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav svg { width: 16px; height: 16px; flex: none; opacity: .85; }
.side-section { padding: 12px 12px 6px; display: flex; flex-direction: column; gap: 8px; }
.side-label { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--faint); font-weight: 600; display: flex; justify-content: space-between; align-items: center; }
.side-label button { text-transform: none; letter-spacing: 0; }
.table-list { flex: 1; overflow: auto; padding: 2px 8px 12px; min-height: 0; }
.table-list a { display: flex; align-items: center; gap: 8px; padding: 5px 8px; border-radius: 6px; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 13px; }
.table-list a:hover { background: var(--panel-2); text-decoration: none; }
.table-list a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.table-list a .count { margin-left: auto; color: var(--faint); font-size: 11px; font-variant-numeric: tabular-nums; }
.table-list .empty { color: var(--faint); padding: 8px; font-size: 13px; }
.side-foot { border-top: 1px solid var(--border); padding: 10px 12px; display: flex; align-items: center; justify-content: space-between; color: var(--muted); font-size: 12.5px; }

.main { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 14px 24px; border-bottom: 1px solid var(--border); background: var(--panel); min-height: 60px; flex-wrap: wrap; }
.crumbs { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; min-width: 0; flex-wrap: wrap; }
.crumbs a { color: var(--muted); }
.crumbs .sep { color: var(--faint); }
.crumbs strong { color: var(--text); font-size: 16px; font-weight: 650; }
.topbar .actions { margin-left: auto; display: flex; gap: 8px; flex-wrap: wrap; }
.content { flex: 1; overflow: auto; padding: 20px 24px 40px; min-height: 0; }
.content.flush { padding: 0; display: flex; flex-direction: column; overflow: hidden; }

.tabs { display: flex; gap: 2px; padding: 0 24px; border-bottom: 1px solid var(--border); background: var(--panel); }
.tabs a { padding: 10px 14px; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; font-weight: 550; }
.tabs a:hover { color: var(--text); text-decoration: none; }
.tabs a.active { color: var(--accent); border-bottom-color: var(--accent); }

/* ---------- controls */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; height: 32px; padding: 0 12px; border-radius: 6px; border: 1px solid var(--border-strong); background: var(--panel); cursor: pointer; font-weight: 550; font-size: 13px; white-space: nowrap; }
.btn:hover { background: var(--panel-2); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--danger); }
.btn.danger:hover { background: var(--danger-soft); }
.btn.danger.solid { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--panel-2); }
.btn.sm { height: 26px; padding: 0 8px; font-size: 12px; }
.btn.icon { width: 32px; padding: 0; }
.btn.sm.icon { width: 26px; }
.btn svg { width: 15px; height: 15px; }

.input, select.input, textarea.input { height: 32px; padding: 0 10px; border: 1px solid var(--border-strong); border-radius: 6px; background: var(--panel); width: 100%; outline: none; }
textarea.input { height: auto; padding: 8px 10px; resize: vertical; font-family: var(--mono); font-size: 12.5px; }
.input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.input.sm { height: 28px; font-size: 13px; }
.input.mono { font-family: var(--mono); font-size: 12.5px; }
label.field { display: flex; flex-direction: column; gap: 5px; font-size: 12.5px; font-weight: 600; color: var(--muted); }
label.field .hint { font-weight: 400; color: var(--faint); }
label.check { display: inline-flex; align-items: center; gap: 7px; font-size: 13px; cursor: pointer; user-select: none; }
label.check input { accent-color: var(--accent); width: 15px; height: 15px; margin: 0; }
.row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; }
.stack { display: flex; flex-direction: column; gap: 14px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
.right { text-align: right; }
.nowrap { white-space: nowrap; }

/* ---------- cards & tables */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 12px; margin-bottom: 20px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.card .k { color: var(--muted); font-size: 12px; font-weight: 600; }
.card .v { font-size: 22px; font-weight: 650; margin-top: 4px; font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.card .s { color: var(--faint); font-size: 12px; margin-top: 2px; }
.bar { height: 6px; border-radius: 3px; background: var(--panel-2); overflow: hidden; margin-top: 8px; }
.bar > i { display: block; height: 100%; background: var(--accent); border-radius: 3px; }

.section { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 18px; overflow: hidden; }
.section-head { display: flex; align-items: center; gap: 10px; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.section-head .actions { margin-left: auto; display: flex; gap: 6px; }
.section-body { padding: 16px; }
.section .empty { padding: 22px 16px; color: var(--muted); text-align: center; }

table.list { width: 100%; border-collapse: collapse; font-size: 13px; }
table.list th { text-align: left; font-weight: 600; color: var(--muted); font-size: 12px; padding: 8px 16px; background: var(--panel-2); border-bottom: 1px solid var(--border); white-space: nowrap; }
table.list td { padding: 9px 16px; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.list tr:last-child td { border-bottom: 0; }
table.list tr:hover td { background: var(--row-hover); }
table.list td.actions { text-align: right; white-space: nowrap; }
table.list td.actions .btn { margin-left: 4px; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

.badge { display: inline-flex; align-items: center; height: 20px; padding: 0 7px; border-radius: 10px; font-size: 11px; font-weight: 650; background: var(--panel-2); color: var(--muted); border: 1px solid var(--border); white-space: nowrap; }
.badge.accent { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.badge.ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge.danger { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.badges { display: inline-flex; gap: 4px; flex-wrap: wrap; }

pre.code { background: var(--panel-2); border: 1px solid var(--border); border-radius: 6px; padding: 12px 14px; margin: 0; overflow: auto; white-space: pre; line-height: 1.55; }

/* ---------- data grid */
.toolbar { display: flex; gap: 8px; align-items: center; padding: 10px 16px; border-bottom: 1px solid var(--border); background: var(--panel); flex-wrap: wrap; }
.toolbar .search { width: 240px; }
.filters { display: flex; flex-direction: column; gap: 6px; padding: 10px 16px; border-bottom: 1px solid var(--border); background: var(--panel); }
.filters .frow { display: flex; gap: 6px; align-items: center; }
.filters .frow select { width: auto; }
.filters .frow .input.val { width: 240px; }
.grid-wrap { flex: 1; overflow: auto; min-height: 0; background: var(--panel); }
table.grid { border-collapse: separate; border-spacing: 0; font-size: 12.5px; min-width: 100%; }
table.grid th { position: sticky; top: 0; z-index: 2; background: var(--panel-2); text-align: left; padding: 0; border-bottom: 1px solid var(--border-strong); border-right: 1px solid var(--border); white-space: nowrap; font-weight: 600; }
table.grid th .h { display: flex; flex-direction: column; padding: 6px 10px; cursor: pointer; user-select: none; }
table.grid th .h:hover { background: var(--row-hover); }
table.grid th .t { font-weight: 400; color: var(--faint); font-size: 11px; font-family: var(--mono); }
table.grid th .n { display: flex; align-items: center; gap: 5px; }
table.grid th .n .arrow { color: var(--accent); }
table.grid th .pk { color: var(--warn); font-size: 10px; }
table.grid td { padding: 5px 10px; border-bottom: 1px solid var(--border); border-right: 1px solid var(--border); max-width: 360px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: var(--mono); font-size: 12px; cursor: default; }
table.grid tr:hover td { background: var(--row-hover); }
table.grid tr.sel td { background: var(--row-sel); }
table.grid td.null { color: var(--null); font-style: italic; }
table.grid td.numv { text-align: right; }
table.grid td.bool-t { color: var(--ok); }
table.grid td.bool-f { color: var(--danger); }
table.grid th.chk, table.grid td.chk { width: 34px; min-width: 34px; text-align: center; padding: 0; position: sticky; left: 0; z-index: 1; background: var(--panel); }
table.grid th.chk { z-index: 3; background: var(--panel-2); }
table.grid td.chk input, table.grid th.chk input { accent-color: var(--accent); }
table.grid td.rn { color: var(--faint); text-align: right; font-size: 11px; width: 1%; }
.pager { display: flex; align-items: center; gap: 10px; padding: 8px 16px; border-top: 1px solid var(--border); background: var(--panel); font-size: 12.5px; color: var(--muted); flex-wrap: wrap; }
.pager .grow { flex: 1; }
.pager select { width: auto; }

/* ---------- SQL editor */
.sql-layout { display: flex; flex-direction: column; height: 100%; }
.editor-wrap { padding: 14px 16px 10px; background: var(--panel); border-bottom: 1px solid var(--border); display: flex; flex-direction: column; gap: 10px; }
.editor { width: 100%; min-height: 170px; height: 200px; resize: vertical; font: 13px/1.55 var(--mono); padding: 12px 14px; border-radius: 6px; border: 1px solid var(--border-strong); background: var(--panel-2); color: var(--text); outline: none; tab-size: 2; }
.editor:focus { border-color: var(--accent); }
.result-meta { padding: 8px 16px; font-size: 12.5px; color: var(--muted); border-bottom: 1px solid var(--border); background: var(--panel); display: flex; gap: 12px; align-items: center; }
.result-meta .ok { color: var(--ok); font-weight: 600; }
.error-box { margin: 14px 16px; padding: 12px 14px; border-radius: 6px; background: var(--danger-soft); color: var(--danger); font-family: var(--mono); font-size: 12.5px; white-space: pre-wrap; }
.history { max-height: 360px; overflow: auto; }
.history button { display: block; width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--border); background: none; padding: 8px 12px; cursor: pointer; font-family: var(--mono); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.history button:hover { background: var(--row-hover); }

/* ---------- modal */
.overlay { position: fixed; inset: 0; background: rgba(10, 14, 22, .45); display: flex; align-items: flex-start; justify-content: center; padding: 6vh 16px; z-index: 50; overflow: auto; animation: fade .12s ease-out; }
.modal { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow); width: 520px; max-width: 100%; animation: pop .14s ease-out; }
.modal.wide { width: 900px; }
.modal.mid { width: 680px; }
.modal-head { display: flex; align-items: center; padding: 16px 20px 0; }
.modal-head h2 { flex: 1; }
.modal-body { padding: 16px 20px; display: flex; flex-direction: column; gap: 14px; max-height: 70vh; overflow: auto; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 20px 16px; border-top: 1px solid var(--border); }
.modal-foot .left { margin-right: auto; }
@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { transform: translateY(6px) scale(.99); opacity: 0; } }

.coldefs { display: flex; flex-direction: column; gap: 6px; }
.coldef { display: grid; grid-template-columns: 1.1fr 1.1fr auto auto auto auto 1fr 30px; gap: 6px; align-items: center; }
.coldef-head { display: grid; grid-template-columns: 1.1fr 1.1fr auto auto auto auto 1fr 30px; gap: 6px; font-size: 11px; color: var(--faint); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.coldef label.check, .coldef-head span.c { width: 42px; justify-content: center; font-size: 11px; }
.coldef-head span.c { text-align: center; }

.edit-field { display: grid; grid-template-columns: 180px 1fr auto; gap: 10px; align-items: start; padding: 6px 0; border-bottom: 1px dashed var(--border); }
.edit-field:last-child { border-bottom: 0; }
.edit-field .fname { font-weight: 600; font-size: 13px; padding-top: 6px; word-break: break-all; }
.edit-field .fname small { display: block; color: var(--faint); font-weight: 400; font-family: var(--mono); font-size: 11px; }
.edit-field textarea.input { min-height: 32px; height: 32px; font-family: var(--mono); }
.edit-field .opts { display: flex; flex-direction: column; gap: 3px; padding-top: 4px; }
.edit-field.is-null textarea { opacity: .45; }

.secret { display: flex; gap: 6px; }
.secret .input { font-family: var(--mono); font-size: 12px; }
.callout { padding: 10px 12px; border-radius: 6px; background: var(--warn-soft); color: var(--warn); font-size: 13px; }
.callout.info { background: var(--accent-soft); color: var(--accent); }

/* ---------- toasts */
#toasts { position: fixed; right: 16px; top: 72px; display: flex; flex-direction: column; gap: 8px; z-index: 100; max-width: min(460px, calc(100vw - 32px)); }
.toast { background: var(--panel); border: 1px solid var(--border); border-left: 3px solid var(--ok); border-radius: 8px; padding: 10px 14px; box-shadow: var(--shadow); font-size: 13px; animation: pop .15s ease-out; white-space: pre-wrap; word-break: break-word; }
.toast.error { border-left-color: var(--danger); }
.toast .t { font-weight: 650; margin-bottom: 2px; }

/* ---------- login */
.login { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login form { width: 360px; max-width: 100%; background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 28px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 14px; }
.login .brand { padding: 0 0 6px; font-size: 17px; }
.login .err { color: var(--danger); font-size: 13px; min-height: 18px; }

.spinner { width: 16px; height: 16px; border: 2px solid var(--border-strong); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; display: inline-block; }
.loading { display: flex; align-items: center; gap: 10px; color: var(--muted); padding: 30px; justify-content: center; }
@keyframes spin { to { transform: rotate(360deg); } }

.menu-btn { display: none; }

@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 280px; z-index: 40; transform: translateX(-100%); transition: transform .18s; box-shadow: var(--shadow); }
  .shell.nav-open .sidebar { transform: none; }
  .menu-btn { display: inline-flex; }
  .topbar, .content { padding-left: 16px; padding-right: 16px; }
  .tabs { padding: 0 8px; overflow-x: auto; }
  .grid2 { grid-template-columns: 1fr; }
  .cards { grid-template-columns: 1fr 1fr; }
  .card .v { font-size: 18px; }
  .coldef, .coldef-head { grid-template-columns: 1fr 1fr; }
  .coldef-head { display: none; }
  .edit-field { grid-template-columns: 1fr; }
  .toolbar .search { width: 100%; }
  .filters .frow { flex-wrap: wrap; }
}
