:root {
  --bg: #14171b;
  --panel: #1b1f25;
  --panel-raised: #232832;
  --border: #2c323c;
  --text: #e7e9ec;
  --text-muted: #8b94a0;
  --accent: #e09e4a;
  --accent-dim: #b87f38;
  --accent-soft: rgba(224, 158, 74, 0.14);
  --danger: #e0644a;
  --danger-soft: rgba(224, 100, 74, 0.14);
  --success: #7fb86b;
  --mono: "SFMono-Regular", ui-monospace, "JetBrains Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --radius: 10px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
}

body { min-height: 100vh; min-height: 100dvh; }

button, input, textarea { font-family: inherit; color: inherit; }
a { color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- layout shell ---------- */

#app { min-height: 100vh; min-height: 100dvh; }

.center-screen {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.shell { display: flex; min-height: 100vh; min-height: 100dvh; }

.sidebar {
  width: 220px;
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
  padding: 20px 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 20px 8px; }

.brand-mark { display: inline-flex; gap: 3px; align-items: flex-end; height: 20px; }
.brand-mark span { display: block; width: 4px; background: var(--accent); border-radius: 2px; }
.brand-mark span:nth-child(1) { height: 40%; }
.brand-mark span:nth-child(2) { height: 70%; }
.brand-mark span:nth-child(3) { height: 100%; }
.brand-mark span:nth-child(4) { height: 55%; }

.brand-name { font-weight: 600; letter-spacing: 0.01em; font-size: 15px; }

.nav-item {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px;
  border-radius: var(--radius); color: var(--text-muted); cursor: pointer;
  border: none; background: transparent; text-align: left; font-size: 14px; width: 100%;
}
.nav-item:hover { background: var(--panel-raised); color: var(--text); }
.nav-item.active { background: var(--accent-soft); color: var(--accent); }

.sidebar-footer { margin-top: auto; padding-top: 12px; border-top: 1px solid var(--border); }

.main { flex: 1; min-width: 0; padding: 28px 32px 60px; overflow-y: auto; }

.page-header {
  display: flex; align-items: baseline; justify-content: space-between;
  gap: 16px; margin-bottom: 22px; flex-wrap: wrap;
}
.page-header h1 { font-size: 20px; margin: 0; font-weight: 600; }
.page-sub { color: var(--text-muted); font-size: 13px; margin-top: 4px; }

/* ---------- primitives ---------- */

.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; }

.btn {
  display: inline-flex; align-items: center; gap: 6px; padding: 9px 14px;
  border-radius: 8px; border: 1px solid var(--border); background: var(--panel-raised);
  color: var(--text); font-size: 13.5px; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: #3a4250; }
.btn:disabled { opacity: 0.5; cursor: default; }

.btn-primary { background: var(--accent); border-color: var(--accent); color: #1a1206; font-weight: 600; }
.btn-primary:hover { background: #eaad60; }

.btn-danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn-danger:hover { background: var(--danger-soft); }

.btn-sm { padding: 6px 10px; font-size: 12.5px; }

input[type="text"], input[type="password"], input[type="number"] {
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px;
  padding: 10px 12px; font-size: 14px; width: 100%; color: var(--text);
}
input[type="text"]:focus, input[type="password"]:focus, input[type="number"]:focus { border-color: var(--accent); }

label { display: block; font-size: 12.5px; color: var(--text-muted); margin-bottom: 5px; }
.field { margin-bottom: 14px; }
.mono { font-family: var(--mono); }

.pill {
  display: inline-flex; align-items: center; gap: 6px; padding: 3px 9px;
  border-radius: 999px; font-size: 11.5px; background: var(--panel-raised);
  border: 1px solid var(--border); color: var(--text-muted);
}

/* ---------- toggle switch ---------- */

.switch {
  display: inline-flex; align-items: center; gap: 8px;
  font-size: 12.5px; color: var(--text-muted); cursor: pointer; user-select: none;
}
.switch input[type="checkbox"] {
  appearance: none; -webkit-appearance: none;
  width: 34px; height: 20px; flex-shrink: 0;
  background: var(--panel-raised); border: 1px solid var(--border); border-radius: 999px;
  position: relative; cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.switch input[type="checkbox"]::before {
  content: ""; position: absolute; top: 1px; left: 1px;
  width: 16px; height: 16px; border-radius: 50%; background: var(--text-muted);
  transition: transform 0.15s ease, background 0.15s ease;
}
.switch input[type="checkbox"]:checked { background: var(--danger-soft); border-color: var(--danger); }
.switch input[type="checkbox"]:checked::before { transform: translateX(14px); background: var(--danger); }
.switch input[type="checkbox"]:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.error-banner {
  background: var(--danger-soft); border: 1px solid rgba(224, 100, 74, 0.4);
  color: #ffb9a6; padding: 10px 14px; border-radius: 8px; font-size: 13.5px; margin-bottom: 16px;
}

.empty-state {
  text-align: center; color: var(--text-muted); padding: 50px 20px;
  border: 1px dashed var(--border); border-radius: var(--radius); font-size: 14px;
}

/* ---------- login ---------- */

.login-card { width: 100%; max-width: 360px; padding: 32px 28px; }
.login-card .brand { padding: 0 0 26px 0; justify-content: center; }
.login-card h1 { font-size: 17px; text-align: center; margin: 0 0 22px; font-weight: 500; color: var(--text-muted); }
.login-card button[type="submit"] { width: 100%; justify-content: center; margin-top: 6px; }

/* ---------- groups & accounts ---------- */

.group-card {
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--panel); margin-bottom: 14px; overflow: hidden;
}
.group-card-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; border-bottom: 1px solid var(--border); gap: 10px; flex-wrap: wrap;
}
.group-name { font-weight: 600; font-size: 15px; }
.group-meta { color: var(--text-muted); font-size: 12.5px; margin-top: 2px; }

.account-row, .grant-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; border-bottom: 1px solid var(--border); gap: 10px; flex-wrap: wrap;
}
.account-row:last-child, .grant-row:last-child { border-bottom: none; }
.account-name { font-family: var(--mono); font-size: 13.5px; }
.account-id { color: var(--text-muted); font-size: 11.5px; }

.inline-form { display: flex; gap: 8px; padding: 12px 16px; flex-wrap: wrap; background: var(--panel-raised); }
.inline-form input { flex: 1; min-width: 140px; }

.row-actions { display: flex; gap: 6px; }

/* ---------- access terminal (matrix) ---------- */

.section-toolbar { display: flex; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; align-items: center; }

.search-input-wrap { position: relative; flex: 1; min-width: 180px; max-width: 280px; }
.search-input-wrap input[type="text"] { padding-left: 32px; }
.search-input-wrap svg { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); opacity: 0.5; pointer-events: none; }

.matrix-legend { display: flex; align-items: center; gap: 6px; color: var(--text-muted); font-size: 12px; margin-left: auto; }
.matrix-legend .matrix-toggle { margin: 0; pointer-events: none; }

.matrix-wrap {
  border: 1px solid var(--border); border-radius: var(--radius);
  overflow: auto; max-width: 100%; background: var(--panel);
}
.matrix-table { border-collapse: separate; border-spacing: 0; width: max-content; min-width: 100%; }
.matrix-table th, .matrix-table td { border-bottom: 1px solid var(--border); border-right: 1px solid var(--border); }
.matrix-table tr:last-child td { border-bottom: none; }
.matrix-table th:last-child, .matrix-table td:last-child { border-right: none; }

.matrix-corner { position: sticky; top: 0; left: 0; z-index: 3; background: var(--panel-raised); min-width: 168px; }

.matrix-col-head {
  position: sticky; top: 0; z-index: 2; background: var(--panel-raised);
  padding: 10px 16px; font-size: 12.5px; font-weight: 600; white-space: nowrap;
  text-align: center; position: relative;
}
.matrix-col-head .channel-path { display: block; font-weight: 400; margin-top: 2px; }

.col-hide-btn {
  position: absolute; top: 4px; right: 4px; width: 16px; height: 16px; line-height: 14px;
  padding: 0; border: none; border-radius: 4px; background: transparent; color: var(--text-muted);
  cursor: pointer; font-size: 13px; opacity: 0; transition: opacity 0.1s;
}
.matrix-col-head:hover .col-hide-btn { opacity: 1; }
.col-hide-btn:hover { background: var(--danger-soft); color: var(--danger); }

.matrix-row-head {
  position: sticky; left: 0; z-index: 1; background: var(--panel);
  padding: 10px 16px; font-family: var(--mono); font-size: 13px;
  white-space: nowrap; text-align: left; min-width: 168px;
}
.matrix-row-head .group-meta { font-family: var(--sans); }

.matrix-cell { padding: 0; text-align: center; min-width: 64px; }

.matrix-toggle {
  width: 32px; height: 32px; margin: 8px auto; border-radius: 8px;
  border: 1px solid var(--border); background: var(--bg); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.matrix-toggle:hover { border-color: var(--accent-dim); }
.matrix-toggle:disabled { opacity: 0.45; cursor: default; }
.matrix-toggle.granted { background: var(--accent-soft); border-color: var(--accent); }
.matrix-toggle.granted::after { content: ""; width: 12px; height: 12px; border-radius: 3px; background: var(--accent); display: block; }

.matrix-empty-row { color: var(--text-muted); padding: 14px 16px; font-size: 13.5px; }

.grant-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.grant-chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 4px 6px 4px 10px;
  border-radius: 999px; background: var(--accent-soft); color: var(--accent);
  font-size: 12px; border: 1px solid rgba(224, 158, 74, 0.3);
}
.grant-chip button { background: none; border: none; color: inherit; cursor: pointer; font-size: 13px; line-height: 1; padding: 2px; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--panel-raised); border: 1px solid var(--border);
  padding: 10px 18px; border-radius: 999px; font-size: 13px; z-index: 50;
}
.toast.error { border-color: rgba(224,100,74,0.5); color: #ffb9a6; }

/* ---------- mobile / iPad ---------- */

@media (max-width: 860px) {
  .shell { flex-direction: column; }
  .sidebar {
    width: 100%; flex-direction: row; align-items: center; padding: 10px 14px;
    border-right: none; border-bottom: 1px solid var(--border); overflow-x: auto;
  }
  .brand { padding: 0 14px 0 0; }
  .sidebar-footer { margin-top: 0; padding-top: 0; border-top: none; margin-left: auto; }
  .nav-item { width: auto; }
  .main { padding: 20px 16px 60px; }
}
