:root {
  --bg: #09090b;
  --bg-panel: #141415;
  --bg-hover: #27272a;
  --bg-active: #3f3f46;
  --border: #27272a;
  --border-strong: #3f3f46;
  --text: #fafafa;
  --text-muted: #a1a1aa;
  --text-dim: #71717a;
  --primary: #ededed;
  --primary-hover: #ffffff;
  --primary-text: #09090b;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --success: #10b981;
  --warning: #f59e0b;
  --radius-sm: 6px;
  --radius: 8px;
  --radius-lg: 12px;
  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
  --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
  --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.5), 0 4px 6px -4px rgb(0 0 0 / 0.5);
  --shadow-modal: 0 0 0 1px var(--border), 0 20px 25px -5px rgb(0 0 0 / 0.5), 0 8px 10px -6px rgb(0 0 0 / 0.5);
  
  /* Badge Colors */
  --badge-ok-bg: rgba(16, 185, 129, 0.1);
  --badge-ok-text: #34d399;
  --badge-ok-border: rgba(16, 185, 129, 0.2);
  
  --badge-off-bg: rgba(239, 68, 68, 0.1);
  --badge-off-text: #f87171;
  --badge-off-border: rgba(239, 68, 68, 0.2);
  
  --badge-warn-bg: rgba(245, 158, 11, 0.1);
  --badge-warn-text: #fbbf24;
  --badge-warn-border: rgba(245, 158, 11, 0.2);
  
  --badge-neutral-bg: rgba(255, 255, 255, 0.06);
  --badge-neutral-text: #a1a1aa;
  --badge-neutral-border: rgba(255, 255, 255, 0.1);

  --badge-paid-bg: rgba(168, 85, 247, 0.1);
  --badge-paid-text: #c084fc;
  --badge-paid-border: rgba(168, 85, 247, 0.2);

  --badge-free-bg: rgba(59, 130, 246, 0.1);
  --badge-free-text: #60a5fa;
  --badge-free-border: rgba(59, 130, 246, 0.2);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font-sans);
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  font-size: 14px;
  line-height: 1.5;
}
button, input, select, textarea {
  font: inherit;
  color: inherit;
}
.hidden { display: none !important; }

/* Login */
.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-shell {
  width: min(380px, 100%);
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 40px 32px;
  box-shadow: var(--shadow-modal);
}
.login-brand {
  display: flex;
  flex-direction: column;
  gap: 16px;
  align-items: center;
  margin-bottom: 32px;
  text-align: center;
}
.login-brand h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
  letter-spacing: -0.02em;
}
.login-brand p {
  margin: 4px 0 0;
  color: var(--text-muted);
  font-size: 14px;
}
.brand-mark {
  width: 48px;
  height: 48px;
  border-radius: var(--radius-lg);
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 24px;
  color: var(--bg);
  background: var(--primary);
  box-shadow: 0 4px 14px rgba(255, 255, 255, 0.1);
}
.brand-mark.sm {
  width: 32px;
  height: 32px;
  border-radius: var(--radius);
  font-size: 16px;
  box-shadow: none;
}
.login-form {
  display: grid;
  gap: 20px;
}
.login-foot {
  margin: 24px 0 0;
  text-align: center;
  color: var(--text-dim);
  font-size: 12px;
}

/* App Shell */
.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 240px 1fr;
}
.sidebar {
  background: var(--bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 16px 12px;
  position: sticky;
  top: 0;
  height: 100vh;
}
.sidebar-brand {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 8px 12px 24px;
}
.sidebar-brand strong {
  display: block;
  font-size: 14px;
  font-weight: 600;
}
.sidebar-brand span {
  display: block;
  font-size: 12px;
  color: var(--text-muted);
}
.nav {
  display: grid;
  gap: 4px;
  flex: 1;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  border: 0;
  background: transparent;
  color: var(--text-muted);
  text-align: left;
  padding: 8px 12px;
  border-radius: var(--radius);
  cursor: pointer;
  transition: 0.15s ease;
  font-size: 14px;
  font-weight: 500;
}
.nav-item:hover {
  background: var(--bg-hover);
  color: var(--text);
}
.nav-item.active {
  background: var(--bg-hover);
  color: var(--text);
}
.nav-ico {
  width: 16px;
  height: 16px;
  opacity: 0.8;
}
.sidebar-foot {
  border-top: 1px solid var(--border);
  padding-top: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.admin-chip {
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 4px;
}
.admin-chip small {
  color: var(--text-muted);
  font-size: 12px;
}
#admin-name {
  font-size: 13px;
  font-weight: 500;
}
.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--bg-hover);
  border: 1px solid var(--border);
  color: var(--text);
  font-weight: 500;
  font-size: 13px;
}
.main {
  min-width: 0;
  display: flex;
  flex-direction: column;
}
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  padding: 32px 40px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.topbar h2 {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
  letter-spacing: -0.02em;
}
.page-desc {
  margin: 6px 0 0;
  color: var(--text-muted);
  font-size: 14px;
}
.content {
  padding: 32px 40px;
  display: grid;
  gap: 24px;
  max-width: 1280px;
  width: 100%;
}

/* Stats */
.stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 16px;
}
.stat {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px 24px;
  box-shadow: var(--shadow-sm);
}
.stat .label {
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 500;
}
.stat .value {
  display: block;
  margin-top: 12px;
  font-size: 32px;
  font-weight: 600;
  letter-spacing: -0.02em;
}
.stat .hint {
  display: block;
  margin-top: 4px;
  color: var(--text-dim);
  font-size: 12px;
}

/* Panels */
.panel {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}
.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  padding: 20px 24px;
  border-bottom: 1px solid var(--border);
}
.panel-head h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 500;
}
.panel-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}
.search-input {
  width: 200px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  font-size: 13px;
  outline: none;
}
.search-input:focus {
  border-color: var(--border-strong);
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.06);
}
.purchase-list {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  min-height: 48px;
  max-height: 220px;
  overflow: auto;
  background: var(--bg);
}
.purchase-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.purchase-item:last-child {
  border-bottom: 0;
}
.purchase-empty {
  padding: 16px 12px;
  color: var(--text-muted);
  font-size: 13px;
  text-align: center;
}
.muted {
  color: var(--text-muted);
  font-size: 13px;
  margin: 4px 0 0;
}
.grid-2 {
  display: grid;
  grid-template-columns: 3fr 2fr;
  gap: 24px;
}
.hint-list {
  margin: 0;
  padding: 24px 24px 24px 44px;
  color: var(--text-muted);
  font-size: 14px;
  line-height: 1.6;
}
.hint-list li {
  margin-bottom: 8px;
}
.hint-list li:last-child {
  margin-bottom: 0;
}
.hint-list b {
  color: var(--text);
  font-weight: 500;
}
.quick-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  padding: 24px;
}

/* Table */
.table-wrap {
  overflow-x: auto;
}
.table-wrap.compact {
  max-height: 320px;
}
table {
  width: 100%;
  border-collapse: collapse;
  min-width: 720px;
  text-align: left;
}
th {
  color: var(--text-muted);
  font-weight: 500;
  font-size: 12px;
  padding: 12px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 10;
}
td {
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
  vertical-align: top;
}
tbody tr {
  transition: background 0.15s ease;
}
tbody tr:hover {
  background: var(--bg-hover);
}
tbody tr:last-child td {
  border-bottom: 0;
}
.col-actions {
  width: 1%;
  white-space: nowrap;
  text-align: right;
}
th.col-actions {
  text-align: right;
}
.cell-title {
  font-weight: 500;
  color: var(--text);
  display: block;
}
.cell-sub {
  display: block;
  margin-top: 4px;
  color: var(--text-muted);
  font-size: 13px;
}
.mono {
  font-family: var(--font-mono);
  font-size: 13px;
}
.empty-row td {
  text-align: center;
  color: var(--text-muted);
  padding: 64px 20px;
}

/* Badges */
.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 2px 8px;
  border-radius: 9999px;
  font-size: 12px;
  font-weight: 500;
  border: 1px solid transparent;
  white-space: nowrap;
}
.badge.ok {
  background: var(--badge-ok-bg);
  color: var(--badge-ok-text);
  border-color: var(--badge-ok-border);
}
.badge.off {
  background: var(--badge-off-bg);
  color: var(--badge-off-text);
  border-color: var(--badge-off-border);
}
.badge.warn {
  background: var(--badge-warn-bg);
  color: var(--badge-warn-text);
  border-color: var(--badge-warn-border);
}
.badge.neutral {
  background: var(--badge-neutral-bg);
  color: var(--badge-neutral-text);
  border-color: var(--badge-neutral-border);
}
.badge.paid {
  background: var(--badge-paid-bg);
  color: var(--badge-paid-text);
  border-color: var(--badge-paid-border);
}
.badge.free {
  background: var(--badge-free-bg);
  color: var(--badge-free-text);
  border-color: var(--badge-free-border);
}

/* Buttons */
.actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 8px 16px;
  cursor: pointer;
  font-weight: 500;
  font-size: 14px;
  transition: all 0.15s ease;
  white-space: nowrap;
  outline: none;
}
.btn:hover {
  background: var(--bg-hover);
  border-color: var(--border-strong);
}
.btn:focus-visible {
  box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--text-muted);
}
.btn:active {
  transform: translateY(1px);
}
.btn-primary {
  background: var(--primary);
  border: 1px solid var(--primary);
  color: var(--primary-text);
}
.btn-primary:hover {
  background: var(--primary-hover);
  border-color: var(--primary-hover);
}
.btn-secondary {
  background: var(--bg-hover);
  border-color: var(--border);
}
.btn-ghost {
  background: transparent;
  border-color: transparent;
}
.btn-ghost:hover {
  background: var(--bg-hover);
}
.btn-danger {
  background: var(--danger);
  border-color: var(--danger);
  color: white;
}
.btn-danger:hover {
  background: var(--danger-hover);
  border-color: var(--danger-hover);
}
.btn-sm {
  padding: 6px 12px;
  font-size: 13px;
}
.btn-block {
  width: 100%;
  padding: 10px 16px;
}
.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  font-size: 18px;
  color: var(--text-muted);
  background: transparent;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.15s ease;
  outline: none;
}
.btn-icon:hover {
  color: var(--text);
  background: var(--bg-hover);
}
.btn-icon:focus-visible {
  box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--text-muted);
}

/* Forms */
.field {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.field label {
  font-size: 13px;
  color: var(--text);
  font-weight: 500;
}
.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}
input, select, textarea {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 14px;
  transition: all 0.15s ease;
  outline: none;
}
input::placeholder, textarea::placeholder {
  color: var(--text-dim);
}
input:hover, select:hover, textarea:hover {
  border-color: var(--border-strong);
}
input:focus, select:focus, textarea:focus {
  border-color: var(--text-muted);
  box-shadow: 0 0 0 1px var(--text-muted);
}
textarea {
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.5;
  resize: vertical;
  min-height: 100px;
}
.form-panel {
  max-width: 600px;
}
.form-grid {
  display: grid;
  gap: 20px;
  padding: 24px;
}
.form-actions {
  padding-top: 8px;
}
.form-error {
  margin: 0;
  color: var(--danger);
  font-size: 13px;
  min-height: 20px;
}
.form-error.ok {
  color: var(--success);
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  z-index: 100;
}
/* Confirm above nested modals (user detail → revoke, etc.) */
#modal-confirm {
  z-index: 400;
}
.modal-card {
  width: 100%;
  max-width: 560px;
  max-height: 90vh;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-modal);
  display: flex;
  flex-direction: column;
}
.modal-sm {
  max-width: 420px;
}
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px;
  border-bottom: 1px solid var(--border);
}
.modal-head h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 500;
}
.modal-body {
  padding: 24px;
  display: grid;
  gap: 16px;
  overflow-y: auto;
}
.modal-foot {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
  padding: 20px 24px;
  border-top: 1px solid var(--border);
  background: var(--bg);
  border-bottom-left-radius: var(--radius-lg);
  border-bottom-right-radius: var(--radius-lg);
}
.spacer {
  flex: 1;
}
.confirm-msg {
  margin: 0;
  color: var(--text-muted);
  line-height: 1.6;
  font-size: 14px;
}
.preview-box {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  margin-top: 8px;
}
.preview-head {
  padding: 10px 16px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.nodes-panel {
  margin-top: 0;
}

/* Toast */
#toast-root {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 200;
  display: grid;
  gap: 12px;
  pointer-events: none;
}
.toast {
  pointer-events: auto;
  min-width: 280px;
  max-width: 400px;
  padding: 14px 16px;
  border-radius: var(--radius);
  background: var(--bg-panel);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-lg);
  font-size: 14px;
  color: var(--text);
  animation: toast-in 0.2s cubic-bezier(0.16, 1, 0.3, 1);
  display: flex;
  align-items: center;
  gap: 12px;
}
.toast::before {
  content: '';
  display: block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.toast.success::before {
  background: var(--success);
}
.toast.error::before {
  background: var(--danger);
}
@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateY(12px) scale(0.96);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

/* Responsive */
@media (max-width: 1024px) {
  .app-shell {
    grid-template-columns: 200px 1fr;
  }
  .stats {
    grid-template-columns: repeat(2, 1fr);
  }
}
@media (max-width: 768px) {
  .app-shell {
    grid-template-columns: 1fr;
  }
  .sidebar {
    height: auto;
    position: relative;
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 16px;
  }
  .sidebar-brand {
    padding-bottom: 16px;
  }
  .nav {
    grid-auto-flow: column;
    grid-auto-columns: max-content;
    overflow-x: auto;
    padding-bottom: 4px;
  }
  .sidebar-foot {
    display: none;
  }
  .topbar {
    padding: 24px 24px 16px;
  }
  .content {
    padding: 24px;
  }
  .grid-2 {
    grid-template-columns: 1fr;
  }
}
@media (max-width: 640px) {
  .stats, .quick-actions, .field-row {
    grid-template-columns: 1fr;
  }
  .topbar {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }
}
