:root {
  --bg: #0f1419;
  --surface: #1a2332;
  --surface-hover: #1f2b3d;
  --border: #2d3a4d;
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #58a6ff;
  --accent-dim: rgba(88, 166, 255, 0.12);
  --danger: #f85149;
  --flag: #d29922;
  --notice-bg: #1c2b1c;
  --sidebar-width: 260px;
  --topbar-height: 48px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

a { color: var(--accent); }

/* ── Top Bar ─────────────────────────────────────────────── */

.top-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  height: var(--topbar-height);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1rem;
}

.top-bar-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.top-bar-left strong a {
  color: var(--text);
  text-decoration: none;
  font-size: 0.95rem;
}

.top-bar-right {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.sidebar-toggle {
  display: none;
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 0.25rem;
}

.sidebar-toggle:hover { color: var(--text); }

/* ── App Layout ──────────────────────────────────────────── */

.app-layout {
  display: flex;
  margin-top: var(--topbar-height);
  min-height: calc(100vh - var(--topbar-height));
}

/* ── Sidebar ─────────────────────────────────────────────── */

.sidebar {
  position: fixed;
  top: var(--topbar-height);
  left: 0;
  bottom: 0;
  width: var(--sidebar-width);
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  overflow-x: hidden;
  z-index: 90;
  display: flex;
  flex-direction: column;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

.sidebar::-webkit-scrollbar { width: 6px; }
.sidebar::-webkit-scrollbar-track { background: transparent; }
.sidebar::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

.sidebar-backdrop {
  display: none;
}

/* ── Soul Selector ───────────────────────────────────────── */

.soul-selector {
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
}

.soul-selector label {
  display: block;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: 0.35rem;
}

.soul-selector select {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.4rem 0.6rem;
  border-radius: 6px;
  font-size: 0.85rem;
}

/* ── Sidebar Navigation ──────────────────────────────────── */

.sidebar-nav {
  padding: 0.5rem 0;
  flex: 1;
}

.nav-icon {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  stroke-width: 2;
}

/* Top-level nav item (Dashboard) */
.nav-item {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.45rem 1rem;
  color: var(--muted);
  text-decoration: none;
  font-size: 0.85rem;
  border-left: 3px solid transparent;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.nav-item:hover {
  background: var(--surface-hover);
  color: var(--text);
}

.nav-item.active {
  color: var(--accent);
  background: var(--accent-dim);
  border-left-color: var(--accent);
}

.nav-item.active .nav-icon { stroke: var(--accent); }

/* ── Nav Group ───────────────────────────────────────────── */

.nav-group {
  margin-top: 0.15rem;
}

.nav-group-header {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  width: 100%;
  padding: 0.5rem 1rem;
  background: none;
  border: none;
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  cursor: pointer;
  transition: color 0.15s;
}

.nav-group-header:hover { color: var(--text); }

.nav-group-header .nav-icon {
  width: 14px;
  height: 14px;
}

.nav-group-header span { flex: 1; text-align: left; }

.nav-chevron {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
  transition: transform 0.2s;
}

.nav-group:not(.open) .nav-chevron {
  transform: rotate(-90deg);
}

.nav-group-items {
  display: none;
  padding-bottom: 0.25rem;
}

.nav-group.open .nav-group-items {
  display: block;
}

.nav-group-items .nav-item {
  padding-left: 2.4rem;
  font-size: 0.83rem;
}

/* ── Content Area ────────────────────────────────────────── */

.content {
  flex: 1;
  padding: 1.5rem 2rem;
  max-width: 960px;
  margin-left: var(--sidebar-width);
  width: calc(100% - var(--sidebar-width));
}

/* No sidebar (login, no soul) */
.app-layout:not(:has(.sidebar)) .content {
  margin-left: 0;
  width: 100%;
  margin: 0 auto;
}

/* ── Responsive (mobile) ─────────────────────────────────── */

@media (max-width: 768px) {
  .sidebar-toggle { display: block; }

  .sidebar {
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: none;
  }

  .sidebar-open .sidebar {
    transform: translateX(0);
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.4);
  }

  .sidebar-backdrop {
    position: fixed;
    inset: 0;
    z-index: 85;
    background: rgba(0, 0, 0, 0.5);
    display: none;
  }

  .sidebar-open .sidebar-backdrop {
    display: block;
  }

  .content {
    margin-left: 0;
    width: 100%;
    padding: 1.25rem 1rem;
  }
}

/* ── Common UI Components ────────────────────────────────── */

.login-page { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.login-box { background: var(--surface); padding: 2rem; border-radius: 8px; border: 1px solid var(--border); width: 100%; max-width: 400px; }

h1 { font-size: 1.5rem; margin-top: 0; }
h2 { font-size: 1.1rem; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 1rem;
  margin-bottom: 1rem;
}

.card-meta { font-size: 0.85rem; color: var(--muted); margin-bottom: 0.5rem; }
.card-body { margin: 0.5rem 0; white-space: pre-wrap; }
.card-actions { display: flex; gap: 0.5rem; margin-top: 0.75rem; }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 1rem;
  margin-bottom: 2rem;
}

.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 1rem;
  text-align: center;
}

.stat-value { display: block; font-size: 1.75rem; font-weight: 600; }
.stat-label { font-size: 0.8rem; color: var(--muted); }

table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
th, td { border: 1px solid var(--border); padding: 0.5rem 0.75rem; text-align: left; }
th { background: var(--surface); }

button, .button {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 0.4rem 0.85rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.9rem;
}

button.danger { background: var(--danger); }
button.small { padding: 0.2rem 0.5rem; font-size: 0.8rem; }

.btn-logout {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
  padding: 0.2rem 0.6rem;
  border-radius: 4px;
  font-size: 0.8rem;
  cursor: pointer;
}

.btn-logout:hover { color: var(--text); border-color: var(--accent); }

input, select, textarea {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.4rem 0.6rem;
  border-radius: 4px;
}

.inline-form, .search-form {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
  margin-bottom: 1.5rem;
}

.flag {
  display: inline-block;
  background: rgba(210, 153, 34, 0.2);
  color: var(--flag);
  padding: 0.15rem 0.5rem;
  border-radius: 4px;
  font-size: 0.8rem;
  margin-bottom: 0.5rem;
}

.error { color: var(--danger); }
.notice { background: var(--notice-bg); border: 1px solid var(--border); padding: 0.75rem 1rem; border-radius: 6px; margin-bottom: 1rem; }
.token-reveal code { display: block; margin-top: 0.5rem; word-break: break-all; }

.codemirror-host {
  border: 1px solid var(--border);
  border-radius: 4px;
  margin-bottom: 1rem;
  min-height: 400px;
}

.memory-content {
  white-space: pre-wrap;
  background: var(--bg);
  padding: 1rem;
  border-radius: 4px;
  overflow-x: auto;
}

.preview { max-width: 40rem; }

label { display: block; margin-bottom: 0.75rem; }

.role-badge {
  padding: 0.1rem 0.5rem;
  border-radius: 999px;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.lang-toggle { margin-left: 0; }
.lang-btn {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
  padding: 0.15rem 0.5rem;
  border-radius: 4px;
  font-size: 0.8rem;
  cursor: pointer;
}
.lang-btn:hover { color: var(--text); border-color: var(--accent); }

.pagination {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-top: 1rem;
}
