:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1e222b;
  --border: #2a2f3a;
  --text: #e6e9ef;
  --muted: #9aa3b2;
  --accent: #4f8cff;
  --danger: #ff5f56;
  --warn: #f0b429;
  --ok: #38b26a;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f6f7f9;
    --panel: #ffffff;
    --panel-2: #f0f2f5;
    --border: #d8dce3;
    --text: #14171c;
    --muted: #5c6472;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

.app { min-height: 100vh; display: flex; flex-direction: column; }
.loading { padding: 3rem; text-align: center; color: var(--muted); }

/* ---- auth ---- */
.auth-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 1rem;
}
.auth-card {
  width: min(420px, 100%);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2rem;
}
.auth-card h1 { margin: 0 0 .25rem; font-size: 1.4rem; }
.auth-card .sub { color: var(--muted); margin: 0 0 1.5rem; font-size: .9rem; }

.auth-card input,
.auth-card select.auth-select {
  width: 100%;
  padding: .65rem .75rem;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font: inherit;
  transition: border-color .15s ease, box-shadow .15s ease;
}
.auth-card input::placeholder { color: var(--muted); opacity: .85; }
.auth-card input:focus,
.auth-card select.auth-select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 28%, transparent);
}
.auth-select,
select.auth-select {
  appearance: none;
  -webkit-appearance: none;
  background-image:
    linear-gradient(45deg, transparent 50%, var(--muted) 50%),
    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position:
    calc(100% - 18px) calc(50% + 2px),
    calc(100% - 12px) calc(50% + 2px);
  background-size: 6px 6px, 6px 6px;
  background-repeat: no-repeat;
  padding-right: 2.25rem;
  cursor: pointer;
}
.auth-select option,
select.auth-select option {
  background: var(--panel);
  color: var(--text);
}
.field-lang { margin-bottom: 1rem; }
.field-hint {
  margin: .35rem 0 0;
  font-size: .75rem;
  color: var(--muted);
  line-height: 1.4;
}
.auth-actions { margin-top: .25rem; }
.auth-actions .full { margin-top: .25rem; }
.auth-footer {
  margin-top: 1.25rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
  text-align: center;
}

label { display: block; font-size: .8rem; color: var(--muted); margin-bottom: .35rem; font-weight: 500; }
input, textarea, select {
  width: 100%;
  padding: .6rem .7rem;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font: inherit;
}
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.field { margin-bottom: 1rem; }

button {
  padding: .6rem 1rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: .5; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button.danger { border-color: var(--danger); color: var(--danger); }
button.full { width: 100%; }
button.link { background: none; border: none; color: var(--accent); padding: .25rem; text-decoration: underline; }

.msg { padding: .6rem .75rem; border-radius: 8px; margin-bottom: 1rem; font-size: .875rem; }
.msg.error { background: rgba(255,95,86,.12); border: 1px solid var(--danger); color: var(--danger); }
.msg.warn  { background: rgba(240,180,41,.12); border: 1px solid var(--warn); color: var(--warn); }
.msg.ok    { background: rgba(56,178,106,.12); border: 1px solid var(--ok); color: var(--ok); }
.msg.info  { background: var(--panel-2); border: 1px solid var(--border); color: var(--muted); }

/* ---- shell ---- */
.topbar {
  display: flex; align-items: center; gap: 1rem;
  padding: .6rem 1rem;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.topbar .brand { font-weight: 700; letter-spacing: .02em; }
.topbar .spacer { flex: 1; }
.badge {
  font-size: .72rem; text-transform: uppercase; letter-spacing: .06em;
  padding: .15rem .5rem; border-radius: 999px;
  border: 1px solid var(--border); color: var(--muted);
}
.badge.admin { border-color: var(--warn); color: var(--warn); }
.badge.editor { border-color: var(--accent); color: var(--accent); }
.lang-select {
  width: auto;
  min-width: 7.5rem;
  padding: .35rem .5rem; font-size: .78rem;
  background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 6px; color: var(--text);
  appearance: none;
  -webkit-appearance: none;
  padding-right: 1.75rem;
  background-image:
    linear-gradient(45deg, transparent 50%, var(--muted) 50%),
    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position:
    calc(100% - 14px) calc(50% + 2px),
    calc(100% - 8px) calc(50% + 2px);
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  cursor: pointer;
}
.lang-select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 25%, transparent);
}

.tabs { display: flex; gap: .25rem; }
.tabs button { border: none; background: none; padding: .4rem .8rem; border-radius: 6px; color: var(--muted); }
.tabs button.active { background: var(--panel-2); color: var(--text); }

.main { flex: 1; display: flex; min-height: 0; }
.sidebar {
  width: 280px; flex-shrink: 0;
  border-right: 1px solid var(--border);
  background: var(--panel);
  overflow-y: auto;
  padding: .75rem;
}
.content { flex: 1; display: flex; flex-direction: column; min-width: 0; padding: 1rem; gap: 1rem; overflow: auto; }

.tree-item { padding: .3rem .4rem; border-radius: 6px; cursor: pointer; font-size: .82rem; }
.tree-item:hover { background: var(--panel-2); }
.tree-cols { padding-left: .9rem; font-size: .76rem; color: var(--muted); }
.tree-cols div { padding: .12rem 0; display: flex; justify-content: space-between; gap: .5rem; }
.tree-cols .t { color: var(--accent); opacity: .75; }

.editor { display: flex; flex-direction: column; gap: .5rem; }
textarea.sql { min-height: 160px; font-family: var(--mono); font-size: 13px; resize: vertical; }
.toolbar { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.toolbar .spacer { flex: 1; }
.hint { color: var(--muted); font-size: .78rem; }

.result-meta { color: var(--muted); font-size: .8rem; display: flex; gap: 1rem; flex-wrap: wrap; }
.table-wrap { overflow: auto; border: 1px solid var(--border); border-radius: 8px; max-height: 55vh; }
table { border-collapse: collapse; width: 100%; font-family: var(--mono); font-size: 12.5px; }
th, td { text-align: left; padding: .35rem .6rem; border-bottom: 1px solid var(--border); white-space: pre; }
th { position: sticky; top: 0; background: var(--panel-2); z-index: 1; }
th .type { color: var(--muted); font-weight: 400; font-size: .72rem; }
td.null { color: var(--muted); font-style: italic; }
tbody tr:hover { background: var(--panel-2); }

.card { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 1rem; }
.card h2 { margin: 0 0 .75rem; font-size: 1rem; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; }
.row { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }

.list { border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.list-row {
  display: flex; gap: .75rem; align-items: center;
  padding: .5rem .75rem; border-bottom: 1px solid var(--border); font-size: .85rem;
}
.list-row:last-child { border-bottom: none; }
.list-row .grow { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.mono { font-family: var(--mono); font-size: 12px; }
.dim { color: var(--muted); }

.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.6);
  display: grid; place-items: center; padding: 1rem; z-index: 50;
}
.modal { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 1.5rem; width: min(520px, 100%); }
.modal h2 { margin: 0 0 .75rem; font-size: 1.05rem; }
.modal pre { background: var(--panel-2); padding: .75rem; border-radius: 8px; overflow-x: auto; font-size: 12px; }

.codes { display: grid; grid-template-columns: 1fr 1fr; gap: .4rem; font-family: var(--mono); font-size: 13px; }
.codes span { background: var(--panel-2); padding: .35rem .5rem; border-radius: 6px; text-align: center; }

/* ---- vault / database-as-backend ---- */
.vault-workspace {
  flex: 1;
  display: grid;
  grid-template-columns: 240px 1fr 220px;
  min-height: 0;
  overflow: hidden;
}
.vault-sidebar, .vault-audit {
  background: var(--panel);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: .75rem;
}
.vault-audit { border-right: none; border-left: 1px solid var(--border); }
.vault-main { overflow-y: auto; padding: 1rem; display: flex; flex-direction: column; gap: .75rem; }
.vault-panel-head { display: flex; align-items: center; justify-content: space-between; gap: .5rem; flex-wrap: wrap; }
.vault-panel-head h1, .vault-panel-head h2 { margin: 0; font-size: 1rem; }
.vault-actions { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.template-tabs { display: flex; gap: .35rem; flex-wrap: wrap; }
.template-tab {
  padding: .35rem .75rem; border-radius: 999px; border: 1px solid var(--border);
  background: var(--panel-2); font-size: .8rem;
}
.template-tab.active { border-color: var(--accent); color: var(--accent); }
.sql-preview { border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.sql-preview-head {
  display: flex; justify-content: space-between; padding: .4rem .6rem;
  background: var(--panel-2); font-size: .75rem; color: var(--muted);
}
.sql-code {
  margin: 0; padding: .75rem; font-family: var(--mono); font-size: 12px;
  background: var(--panel); overflow-x: auto; white-space: pre-wrap;
}
.badge-safe {
  font-size: .68rem; padding: .1rem .45rem; border-radius: 999px;
  border: 1px solid var(--ok); color: var(--ok);
}
.params-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: .75rem; }
.param-field label { display: block; font-size: .75rem; color: var(--muted); margin-bottom: .25rem; }
.security-strip {
  display: flex; gap: 1rem; flex-wrap: wrap; font-size: .72rem; color: var(--muted);
  padding: .5rem .75rem; border: 1px dashed var(--border); border-radius: 8px;
}
.vault-run-row { display: flex; gap: .5rem; }
.vault-results { min-height: 120px; }
.audit-list { list-style: none; padding: 0; margin: 0; font-size: .78rem; }
.audit-item { padding: .45rem 0; border-bottom: 1px solid var(--border); display: flex; flex-direction: column; gap: .15rem; }
.audit-time { color: var(--muted); font-family: var(--mono); font-size: .7rem; }
.btn-ghost { background: transparent; border: 1px solid var(--border); }

.qr { display: block; margin: 0 auto 1rem; border-radius: 8px; background: #fff; padding: 8px; }
.secret { font-family: var(--mono); font-size: 12px; word-break: break-all; background: var(--panel-2); padding: .5rem; border-radius: 6px; }

.status-ok { color: var(--ok); }
.status-denied { color: var(--danger); }
.status-error { color: var(--warn); }
