:root {
  --brand: #2f5560;
  --brand-dark: #24434c;
  --accent: #1f8a70;
  --accent-soft: #e3f4ee;
  --bg: #f4f6f7;
  --panel: #ffffff;
  --text: #1d2a30;
  --muted: #6b7b83;
  --line: #dfe5e8;
  --danger: #c0392b;
  --warn-bg: #fff6e0;
  --warn-line: #f0d08a;
  --ok-bg: #e7f6ef;
  --ok-line: #a9dcc5;
  --mono: ui-monospace, 'SF Mono', Menlo, Consolas, 'Liberation Mono', monospace;
  --radius: 8px;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
}

* { box-sizing: border-box; }
body { margin: 0; }
a { color: var(--accent); }
code { font-family: var(--mono); font-size: 12.5px; background: #eef2f4; padding: 1px 5px; border-radius: 4px; word-break: break-all; }
h1 { font-size: 20px; margin: 0; }
h2 { font-size: 15px; margin: 0 0 8px; }
h3 { font-size: 13px; margin: 16px 0 6px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.sub { color: var(--muted); font-size: 12.5px; }
.spacer { flex: 1; }
.nowrap { white-space: nowrap; }
.boot { padding: 40px; color: var(--muted); }
[hidden] { display: none !important; }

/* ---------- buttons & inputs ---------- */
button {
  font: inherit; cursor: pointer; border-radius: 6px; padding: 7px 14px;
  border: 1px solid var(--line); background: var(--panel); color: var(--text);
}
button:disabled { opacity: .5; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button.primary:hover:not(:disabled) { background: #187560; }
button.ghost { background: transparent; }
button.ghost:hover { background: #eef2f4; }
button.ghost.active { background: var(--accent-soft); border-color: var(--ok-line); }
button.small { padding: 4px 10px; font-size: 12.5px; }
button.block { width: 100%; padding: 10px; }
button.danger { color: var(--danger); }
.icon-btn { border: none; background: none; font-size: 20px; line-height: 1; padding: 2px 8px; color: var(--muted); }
input, select, textarea {
  font: inherit; padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; background: #fff; color: var(--text); width: 100%;
}
input:focus, select:focus, button:focus-visible { outline: 2px solid #7cc7b1; outline-offset: 1px; }
.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 14px; }
.label { font-size: 12.5px; font-weight: 600; color: #3b4b52; }
.hint { font-size: 12px; color: var(--muted); }
.form-error { color: var(--danger); font-size: 13px; min-height: 18px; margin-bottom: 6px; }
.actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.check { display: inline-flex; align-items: center; gap: 4px; font-size: 12.5px; color: var(--muted); }
.check input { width: auto; }

/* ---------- login ---------- */
.login-page { min-height: 100vh; display: grid; place-items: center; background: linear-gradient(160deg, var(--brand) 0%, var(--brand-dark) 100%); padding: 20px; }
.login-card { width: 100%; max-width: 360px; background: #fff; border-radius: 12px; padding: 28px; box-shadow: 0 20px 50px rgba(0,0,0,.25); }
.login-brand { display: flex; gap: 12px; align-items: center; margin-bottom: 22px; }
.login-brand img { width: 42px; height: 42px; }
.login-brand strong { display: block; font-size: 18px; }
.login-brand span { color: var(--muted); }

/* ---------- shell ---------- */
.shell { display: grid; grid-template-columns: 210px 1fr; grid-template-rows: 54px 1fr; height: 100vh; }
.topbar { grid-column: 1 / -1; background: var(--brand); color: #fff; display: flex; align-items: center; padding: 0 16px; gap: 16px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; }
.brand img { width: 28px; height: 28px; }
.brand em { font-style: normal; font-weight: 400; opacity: .75; }
.user-menu { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.user-menu .who { display: flex; align-items: center; gap: 8px; margin-right: 6px; }
.user-menu button { color: #fff; border-color: rgba(255,255,255,.25); }
.user-menu button:hover { background: rgba(255,255,255,.1); }
.sidebar { background: #fff; border-right: 1px solid var(--line); padding: 12px 8px; display: flex; flex-direction: column; gap: 2px; }
.sidebar a { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 6px; color: var(--text); text-decoration: none; }
.sidebar a:hover { background: #f0f3f4; }
.sidebar a.active { background: var(--accent-soft); color: #11604d; font-weight: 600; }
.nav-icon { width: 18px; text-align: center; opacity: .8; }
.content { overflow: auto; padding: 20px 24px; min-width: 0; display: flex; flex-direction: column; }
.page-head { display: flex; align-items: center; gap: 14px; margin-bottom: 14px; flex-wrap: wrap; }
.page-head .sub { margin: 0; }

.role-badge { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 10px; text-transform: uppercase; letter-spacing: .03em; background: #e8edf0; color: #40525a; }
.role-badge.super_admin { background: #fde9d7; color: #9a4a0c; }
.role-badge.admin { background: #dfeefe; color: #1f5f9a; }
.topbar .role-badge { background: rgba(255,255,255,.18); color: #fff; }

.pill { display: inline-block; font-size: 11.5px; font-weight: 600; padding: 2px 9px; border-radius: 10px; }
.pill.ok { background: var(--ok-bg); color: #16734f; }
.pill.bad { background: #fbe9e7; color: #a5341f; }

.notice { border: 1px solid var(--warn-line); background: var(--warn-bg); padding: 10px 14px; border-radius: var(--radius); margin-bottom: 14px; font-size: 13px; line-height: 1.45; }
.notice.ok { border-color: var(--ok-line); background: var(--ok-bg); }

/* ---------- cards & tables ---------- */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 16px; }
.card-head { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.card-head h2 { margin: 0; }
.cards { margin-bottom: 0; }
.table-card { padding: 0; overflow: auto; }
table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 10px 12px; border-bottom: 1px solid var(--line); background: #fafbfb; white-space: nowrap; }
td { padding: 10px 12px; border-bottom: 1px solid #eef1f2; vertical-align: top; }
tr:last-child td { border-bottom: none; }
tr.muted td { opacity: .55; }
.row-actions { white-space: nowrap; text-align: right; }
.row-actions button { margin-left: 4px; }
td.details { max-width: 360px; word-break: break-word; font-family: var(--mono); font-size: 11.5px; }
.rule { display: flex; gap: 6px; align-items: center; margin: 2px 0; }
.access { font-size: 10.5px; font-weight: 700; text-transform: uppercase; padding: 1px 6px; border-radius: 4px; background: #e8edf0; color: #40525a; }
.access.readwrite { background: #e3f4ee; color: #16734f; }
.access.write { background: #fdf0dc; color: #94570b; }
.empty { color: var(--muted); padding: 24px; text-align: center; }
ul.plain { list-style: none; margin: 0; padding: 0; }
ul.plain li { padding: 6px 0; border-bottom: 1px solid #eef1f2; display: flex; gap: 8px; align-items: center; }

.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; margin-bottom: 16px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; display: flex; flex-direction: column; gap: 4px; }
.stat .label { color: var(--muted); font-weight: 500; }
.stat strong { font-size: 18px; }

.meta-grid { display: grid; grid-template-columns: max-content 1fr; gap: 6px 14px; margin: 10px 0; font-size: 13px; }
.meta-grid dt { color: var(--muted); }
.meta-grid dd { margin: 0; word-break: break-all; }
.meta-grid.secret code { font-size: 14px; }

pre.log, pre.code { background: #1d2a30; color: #d7e3e8; font-family: var(--mono); font-size: 12px; padding: 12px; border-radius: 6px; overflow: auto; margin: 0; }
pre.log { height: 380px; white-space: pre-wrap; word-break: break-all; }

/* ---------- explorer ---------- */
.scope-note { color: var(--muted); font-size: 12.5px; }
.explorer-toolbar { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.search-box { display: flex; align-items: center; gap: 6px; background: #fff; border: 1px solid var(--line); border-radius: 6px; padding: 0 10px; width: 320px; }
.search-box input { border: none; padding: 7px 0; outline: none; }
.counters { color: var(--muted); font-size: 12.5px; }
.explorer { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(0, 1fr) minmax(320px, 40%); gap: 14px; }
.tree-wrap { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: auto; min-height: 300px; }
.tree { padding: 6px 0; font-size: 14px; }
.row { display: flex; align-items: baseline; padding: 3px 10px; cursor: pointer; white-space: nowrap; border-left: 3px solid transparent; }
.row:hover { background: #f2f6f7; }
.row.selected { background: #e1efeb; border-left-color: var(--accent); }
.row.fresh .name { animation: flash 1.4s ease-out; }
@keyframes flash { from { background: #c8eedd; } to { background: transparent; } }
.twisty { width: 16px; font-size: 10px; color: #1d2a30; flex: none; }
.twisty.none { visibility: hidden; }
.row .name { font-weight: 700; border-radius: 3px; padding: 0 2px; }
.row .meta { color: var(--muted); margin-left: 2px; }
.row .value { color: #4f6168; margin-left: 4px; overflow: hidden; text-overflow: ellipsis; font-family: var(--mono); font-size: 12.5px; }
.detail { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; overflow: auto; min-height: 300px; }
.detail-head { display: flex; gap: 10px; align-items: flex-start; }
.detail-topic { font-family: var(--mono); font-weight: 600; word-break: break-all; flex: 1; font-size: 13.5px; }
.value-head { display: flex; align-items: center; justify-content: space-between; }
pre.payload { background: #f6f8f9; border: 1px solid var(--line); border-radius: 6px; padding: 10px; font-family: var(--mono); font-size: 12.5px; max-height: 320px; overflow: auto; white-space: pre-wrap; word-break: break-all; margin: 0; }
pre.payload.binary { color: #7a5a1a; }
ol.history { list-style: none; margin: 0; padding: 0; font-size: 12.5px; }
ol.history li { display: flex; gap: 10px; padding: 5px 6px; border-bottom: 1px solid #eef1f2; cursor: pointer; }
ol.history li:hover { background: #f2f6f7; }
ol.history li.active { background: #e1efeb; }
ol.history time { color: var(--muted); flex: none; font-variant-numeric: tabular-nums; }
ol.history span { font-family: var(--mono); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- dialogs ---------- */
.overlay { position: fixed; inset: 0; background: rgba(20,30,35,.45); display: grid; place-items: center; z-index: 50; padding: 20px; }
.dialog { background: #fff; border-radius: 10px; width: 100%; max-width: 520px; max-height: 90vh; overflow: auto; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.dialog.wide { max-width: 760px; }
.dialog-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.dialog-head h2 { margin: 0; font-size: 16px; }
.dialog-body { padding: 18px; }
.rules { display: flex; flex-direction: column; gap: 6px; margin: 6px 0; }
.rule-row { display: grid; grid-template-columns: 1fr 120px 32px; gap: 6px; }
.rule-row input { font-family: var(--mono); font-size: 13px; }

.toasts { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 60; }
.toast { background: #1d2a30; color: #fff; padding: 10px 16px; border-radius: 6px; box-shadow: 0 6px 20px rgba(0,0,0,.2); max-width: 420px; }
.toast.error { background: var(--danger); }

@media (max-width: 900px) {
  .shell { grid-template-columns: 1fr; grid-template-rows: 54px auto 1fr; }
  .sidebar { flex-direction: row; overflow-x: auto; border-right: none; border-bottom: 1px solid var(--line); }
  .explorer { grid-template-columns: 1fr; }
  .grid-2 { grid-template-columns: 1fr; }
  .user-menu .who { display: none; }
}
