/* Hapira — single stylesheet, no build step. */
:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --border: #e3e6eb;
  --text: #17202b;
  --muted: #667085;
  --primary: #1f5eff;
  --primary-ink: #ffffff;
  --primary-soft: #e8efff;
  --success: #0f8a5f;
  --success-soft: #e3f5ed;
  --warning: #a15c00;
  --warning-soft: #fff3df;
  --danger: #c8322b;
  --danger-soft: #fdeceb;
  --sidebar: #101828;
  --sidebar-ink: #c5cad3;
  --sidebar-active: #ffffff;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1117; --surface: #151b23; --surface-2: #1c232d; --border: #2a323d; --text: #e6e9ee;
    --muted: #8b95a5; --primary: #5b8cff; --primary-soft: #1b2a4d; --success: #3ccf91; --success-soft: #12322a;
    --warning: #f0b45a; --warning-soft: #3a2c14; --danger: #ff7a72; --danger-soft: #3d1c1a; --sidebar: #0a0e14;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { font-size: 14px; }
body { margin: 0; font-family: var(--font); background: var(--bg); color: var(--text); line-height: 1.45; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 1.55rem; font-weight: 650; }
h2 { font-size: 1.1rem; font-weight: 620; }
h3 { font-size: .95rem; font-weight: 600; }
.muted { color: var(--muted); }
.mono { font-family: var(--mono); font-size: .92em; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.neg { color: var(--danger); }
.pos { color: var(--success); }
.nowrap { white-space: nowrap; }
.small { font-size: .86rem; }

/* ---------- layout */
.shell { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }
.sidebar { background: var(--sidebar); color: var(--sidebar-ink); padding: 18px 12px; display: flex;
  flex-direction: column; gap: 4px; position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.brand { display: flex; align-items: center; gap: 10px; color: #fff; font-weight: 700; font-size: 1.1rem;
  padding: 4px 10px 14px; }
.brand-logo { display: block; height: 30px; width: auto; }
/* The logo is blue on transparent: render it white on the dark sidebar. */
.sidebar .brand-logo { filter: brightness(0) invert(1); }
.tenant-chip { margin: 0 6px 12px; padding: 8px 10px; border-radius: 8px; background: rgba(255,255,255,.06);
  font-size: .85rem; color: #fff; }
.tenant-chip small { display: block; color: var(--sidebar-ink); font-size: .75rem; }
.nav-label { text-transform: uppercase; font-size: .68rem; letter-spacing: .08em; color: #7a8394;
  padding: 14px 10px 4px; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 7px 10px; border-radius: 7px;
  color: var(--sidebar-ink); font-weight: 500; }
.nav a:hover { background: rgba(255,255,255,.06); text-decoration: none; color: #fff; }
.nav a.active { background: rgba(255,255,255,.1); color: var(--sidebar-active); }
.nav svg { width: 17px; height: 17px; flex: none; opacity: .85; }
.sidebar-foot { margin-top: auto; padding: 12px 6px 0; border-top: 1px solid rgba(255,255,255,.08);
  font-size: .85rem; }
.sidebar-foot button { background: none; border: 0; color: var(--sidebar-ink); padding: 6px 4px;
  cursor: pointer; font: inherit; }
.sidebar-foot button:hover { color: #fff; }
.main { padding: 26px 32px 60px; min-width: 0; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px;
  margin-bottom: 20px; flex-wrap: wrap; }
.page-head p { margin: 4px 0 0; color: var(--muted); }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.actions input, .actions select { width: auto; }

/* ---------- surfaces */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); }
.card-pad { padding: 18px 20px; }
.card-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px;
  border-bottom: 1px solid var(--border); gap: 12px; }
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.span-2 { grid-column: span 2; }
.stack { display: flex; flex-direction: column; gap: 16px; }
.stat { padding: 16px 18px; }
.stat .label { color: var(--muted); font-size: .82rem; font-weight: 500; }
.stat .value { font-size: 1.45rem; font-weight: 650; margin-top: 4px; font-variant-numeric: tabular-nums; }
.stat .sub { color: var(--muted); font-size: .8rem; margin-top: 2px; }
.kv { display: grid; grid-template-columns: 140px 1fr; gap: 8px 12px; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }

/* ---------- tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 9px 14px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: .76rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600;
  background: var(--surface-2); }
tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }
tfoot td { font-weight: 650; background: var(--surface-2); }
td.actions-cell { text-align: right; white-space: nowrap; }
.row-link { color: inherit; font-weight: 550; }

/* ---------- forms */
.form-grid { display: grid; grid-template-columns: repeat(12, 1fr); gap: 14px 16px; }
.field { display: flex; flex-direction: column; gap: 5px; grid-column: span 6; }
.field.full { grid-column: span 12; }
.field.third { grid-column: span 4; }
.field.quarter { grid-column: span 3; }
.field label { font-weight: 550; font-size: .88rem; }
.field .help { color: var(--muted); font-size: .78rem; }
.field .error { color: var(--danger); font-size: .8rem; }
.field.has-error input, .field.has-error select, .field.has-error textarea { border-color: var(--danger); }
input, select, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; width: 100%; min-height: 36px; }
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }
input[readonly] { background: var(--surface-2); font-weight: 650; }
.form-actions { display: flex; gap: 8px; justify-content: flex-end; padding-top: 6px; grid-column: span 12; }
.filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; padding: 14px 18px;
  border-bottom: 1px solid var(--border); }
.filters .field { grid-column: auto; min-width: 140px; }
.filters .grow { flex: 1; min-width: 220px; }
fieldset { border: 0; padding: 0; margin: 0; }
legend { font-weight: 650; margin-bottom: 10px; }
.section-title { grid-column: span 12; font-weight: 650; padding-top: 8px; border-top: 1px solid var(--border);
  margin-top: 4px; padding-top: 14px; }

/* ---------- buttons */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 550; cursor: pointer; min-height: 36px;
  white-space: nowrap; }
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn-primary:hover { background: var(--primary); filter: brightness(1.08); }
.btn-danger { color: var(--danger); border-color: var(--danger-soft); }
.btn-danger:hover { background: var(--danger-soft); }
.btn-sm { padding: 4px 10px; min-height: 28px; font-size: .85rem; }
.btn-link { background: none; border: 0; color: var(--primary); padding: 0; cursor: pointer; font: inherit; }
form.inline { display: inline; }

/* ---------- badges, flashes */
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: .75rem; font-weight: 600;
  background: var(--surface-2); color: var(--muted); }
.badge-success { background: var(--success-soft); color: var(--success); }
.badge-warning { background: var(--warning-soft); color: var(--warning); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }
.badge-primary { background: var(--primary-soft); color: var(--primary); }
.flashes { position: fixed; top: 16px; right: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 50;
  max-width: 420px; }
.flash { padding: 11px 14px; border-radius: 9px; box-shadow: var(--shadow); border: 1px solid var(--border);
  background: var(--surface); display: flex; gap: 10px; align-items: flex-start; }
.flash-success { border-left: 4px solid var(--success); }
.flash-warning { border-left: 4px solid var(--warning); }
.flash-error { border-left: 4px solid var(--danger); }
.flash button { margin-left: auto; background: none; border: 0; color: var(--muted); cursor: pointer; font-size: 1rem; }
.alert { padding: 10px 14px; border-radius: 8px; background: var(--danger-soft); color: var(--danger); margin-bottom: 12px; }
.empty { padding: 36px; text-align: center; color: var(--muted); }

/* ---------- tabs, pager */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 18px; }
.tabs a { padding: 9px 14px; color: var(--muted); font-weight: 550; border-bottom: 2px solid transparent;
  margin-bottom: -1px; }
.tabs a.active { color: var(--text); border-color: var(--primary); }
.tabs a:hover { text-decoration: none; color: var(--text); }
.pager { display: flex; justify-content: space-between; align-items: center; padding: 10px 18px;
  border-top: 1px solid var(--border); color: var(--muted); font-size: .85rem; }

/* ---------- chart */
.bars { display: flex; align-items: flex-end; gap: 8px; height: 190px; padding: 18px 18px 0; }
.bar { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 6px; height: 100%;
  justify-content: flex-end; min-width: 0; }
.bar-col { width: 100%; max-width: 42px; background: var(--primary-soft); border-radius: 6px 6px 2px 2px;
  position: relative; min-height: 2px; }
.bar-col .markup { position: absolute; bottom: 0; left: 0; right: 0; background: var(--primary);
  border-radius: 0 0 2px 2px; }
.bar small { color: var(--muted); font-size: .72rem; }
.bar:hover .bar-col { outline: 2px solid var(--primary); }
.legend { display: flex; gap: 14px; font-size: .8rem; color: var(--muted); padding: 10px 18px 16px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px;
  vertical-align: -1px; }

/* ---------- combobox */
.combobox { position: relative; }
.combobox ul { position: absolute; z-index: 20; top: 100%; left: 0; right: 0; margin: 4px 0 0; padding: 4px;
  list-style: none; background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: var(--shadow); max-height: 280px; overflow-y: auto; }
.combobox li { padding: 7px 10px; border-radius: 6px; cursor: pointer; }
.combobox li[aria-selected="true"], .combobox li:hover { background: var(--primary-soft); }

/* ---------- auth */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth .card { width: 100%; max-width: 380px; padding: 28px; }
.auth .brand { color: var(--text); padding: 0 0 18px; }
.auth .brand-logo { height: 40px; }
@media (prefers-color-scheme: dark) { .auth .brand-logo { filter: brightness(0) invert(1); } }

turbo-frame[busy] { opacity: .55; transition: opacity .15s; }
.turbo-progress-bar { background: var(--primary); height: 3px; }

@media (max-width: 1100px) { .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; flex-direction: row; flex-wrap: wrap; padding: 10px; }
  .nav-label, .tenant-chip, .sidebar-foot { display: none; }
  .nav { display: flex; flex-wrap: wrap; gap: 2px; }
  .main { padding: 18px 16px 40px; }
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
  .span-2 { grid-column: auto; }
  .field, .field.third, .field.quarter { grid-column: span 12; }
}

/* Permissions page */
.notice { background: var(--warning-soft); color: var(--text); border-radius: 10px; padding: 12px 16px; margin-bottom: 16px; }
.perm-toolbar { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.perm-groups { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 16px; }
.perm-list { display: flex; flex-direction: column; }
.perm-item { display: flex; gap: 12px; align-items: flex-start; padding: 12px 18px; border-top: 1px solid var(--border); cursor: pointer; }
.perm-item:first-child { border-top: 0; }
.perm-item input { width: auto; margin-top: 3px; flex: none; }
.perm-item span { display: flex; flex-direction: column; gap: 2px; }
.perm-item.is-sensitive strong::after { content: "sensitive"; margin-left: 8px; font-size: .7rem; font-weight: 600; padding: 1px 6px;
  border-radius: 999px; background: var(--warning-soft); color: var(--warning); vertical-align: 1px; }
fieldset:disabled .perm-item { cursor: default; opacity: .75; }
.perm-actions { position: sticky; bottom: 0; background: var(--bg); padding: 12px 0; margin-top: 8px; }
