:root {
  --bg: #fff3e4;
  --bg-2: #ffe3c1;
  --surface: #ffffff;
  --surface-2: #fff7ef;
  --surface-3: #ffeccc;
  --text: #1f1b16;
  --muted: #5c5349;
  --faint: #8a7b6c;
  --border: rgba(31, 27, 22, 0.1);
  --border-strong: rgba(31, 27, 22, 0.22);
  --nav: #2b1a08;
  --nav-2: #3d2509;
  --accent: #f38020;
  --accent-soft: #ffe0b0;
  --accent-light: #ffb35c;
  --green: #047857;
  --green-soft: #d1fae5;
  --red: #dc2626;
  --red-soft: #fee2e2;
  --amber: #b45309;
  --amber-soft: #fef3c7;
  --shadow: 0 12px 32px rgba(243, 128, 32, 0.14);
  --shadow-soft: 0 4px 16px rgba(243, 128, 32, 0.08);
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

body {
  margin: 0;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: radial-gradient(circle at 10% 10%, rgba(255,179,92,0.38), transparent 45%),
              radial-gradient(circle at 90% 5%, rgba(243,128,32,0.22), transparent 50%),
              linear-gradient(120deg, var(--bg), #fffaf4 55%, var(--bg-2));
  background-attachment: fixed;
  color: var(--text);
}

button, input, textarea, select { font: inherit; }
button { cursor: pointer; }

#appContent { display: none; }
#appContent.visible { display: block; }

/* Login */
.login-screen {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: center; justify-content: center; padding: 24px;
  background: radial-gradient(circle at 30% 30%, rgba(255,179,92,0.5), transparent 50%),
              radial-gradient(circle at 80% 70%, rgba(243,128,32,0.28), transparent 50%),
              linear-gradient(140deg, var(--bg), #fff9f2 50%, var(--bg-2));
}

.login-box {
  width: min(390px, 100%);
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  box-shadow: 0 18px 48px rgba(243,128,32,0.22);
  padding: 32px 28px;
}

.login-title { margin: 0; font-size: 22px; line-height: 1.2; color: var(--text); }
.login-sub { margin: 6px 0 22px; color: var(--muted); font-size: 14px; }
.login-hint { margin-top: 14px; color: var(--faint); font-size: 12px; text-align: center; }
.login-error { display: none; margin-top: 10px; color: var(--red); font-size: 13px; }

/* Layout */
.app-layout {
  display: flex;
  min-height: 100vh;
  height: 100vh;
  overflow: hidden;
}

/* Sidebar */
.sidebar {
  width: 264px;
  flex: 0 0 264px;
  background: var(--nav);
  color: #f5e6d0;
  display: flex;
  flex-direction: column;
  border-right: 1px solid rgba(255,179,92,0.12);
  box-shadow: 4px 0 24px rgba(243,128,32,0.1);
  height: 100vh;
  overflow: hidden;
}

.brand { padding: 22px 18px 18px; border-bottom: 1px solid rgba(255,179,92,0.12); }
.brand-name { font-weight: 800; font-size: 16px; color: #ffd9a8; }
.brand-sub { margin-top: 4px; color: #a07840; font-size: 12px; }

.nav {
  flex: 1;
  padding: 14px 10px;
  overflow-y: auto;
  overflow-x: hidden;
  min-height: 0;
}
.nav-label { padding: 14px 10px 7px; color: #a07840; font-size: 11px; font-weight: 800; letter-spacing: 0.08em; text-transform: uppercase; }

.nav-item {
  width: 100%; min-height: 40px;
  display: flex; align-items: center; gap: 10px; padding: 9px 10px;
  border: 1px solid transparent; border-radius: 10px;
  background: transparent; color: #d4b896; text-align: left; font-weight: 650;
  transition: background 0.18s, color 0.18s, border-color 0.18s;
}
.nav-item:hover { background: var(--nav-2); color: #ffd9a8; border-color: rgba(255,179,92,0.15); }
.nav-item.active {
  background: linear-gradient(130deg, var(--accent), var(--accent-light));
  color: #2b1402; border-color: transparent;
  box-shadow: 0 8px 20px rgba(243,128,32,0.3);
}

.nav-icon {
  width: 24px; height: 24px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 7px; background: rgba(255,255,255,0.08);
  font-size: 12px; font-weight: 800; transition: background 0.18s;
}
.nav-item.active .nav-icon { background: rgba(43,20,2,0.2); color: #2b1402; }

.sidebar-footer { padding: 12px; border-top: 1px solid rgba(255,179,92,0.12); }
.user-card { display: grid; grid-template-columns: 34px 1fr auto; gap: 10px; align-items: center; }
.avatar {
  width: 34px; height: 34px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 10px; background: rgba(255,179,92,0.15); color: #ffd9a8;
  font-size: 12px; font-weight: 800;
}

/* Main */
.main {
  flex: 1;
  min-width: 0;
  padding: 28px;
  overflow-y: auto;
  height: 100vh;
  box-sizing: border-box;
}

.page-section {
  display: none;
  max-width: 1280px;
  margin: 0 auto;
  padding-bottom: 40px;
}
.page-section.show {
  display: block;
  animation: slideUp 0.5s ease both;
}

@keyframes slideUp {
  from { opacity: 0; transform: translateY(16px); }
  to { opacity: 1; transform: translateY(0); }
}

.page-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 18px; }
.page-title { margin: 0; font-size: 24px; line-height: 1.2; letter-spacing: 0; }
.page-desc { margin: 6px 0 0; color: var(--muted); font-size: 14px; }

/* Panel */
.panel {
  border: 1px solid var(--border); border-radius: 14px;
  background: var(--surface); box-shadow: var(--shadow-soft);
  padding: 18px; margin-bottom: 16px;
  transition: box-shadow 0.2s;
}
.panel:hover { box-shadow: var(--shadow); }
.panel-title { margin: 0 0 14px; font-size: 15px; font-weight: 800; }

/* Grid */
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, minmax(0,1fr)); gap: 16px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field.full { grid-column: 1 / -1; }
.field label, .field-label { color: var(--muted); font-size: 12px; font-weight: 800; letter-spacing: 0.04em; text-transform: uppercase; }

/* Input */
.input, input, textarea, select {
  width: 100%; min-height: 40px;
  border: 1px solid var(--border); border-radius: 10px;
  background: #fffcf8; color: var(--text); padding: 9px 11px; outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
textarea { min-height: 160px; resize: vertical; font-family: "JetBrains Mono", Consolas, monospace; font-size: 12px; line-height: 1.5; }
input:focus, textarea:focus, select:focus { border-color: rgba(243,128,32,0.55); box-shadow: 0 0 0 3px rgba(243,128,32,0.12); }

/* Checkbox */
.check-row { display: flex; flex-wrap: wrap; gap: 12px 18px; }
.check-item { display: inline-flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; font-weight: 650; }
.check-item input { width: 16px; min-height: 16px; accent-color: var(--accent); }

/* Actions */
.action-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }

/* Buttons */
.btn {
  min-height: 36px; display: inline-flex; align-items: center; justify-content: center;
  gap: 7px; border: 1px solid transparent; border-radius: 999px;
  padding: 8px 16px; font-size: 13px; font-weight: 800; text-decoration: none;
  transition: transform 0.18s, box-shadow 0.18s, background 0.18s, border-color 0.18s;
}
.btn:disabled { cursor: not-allowed; opacity: 0.55; }

.btn-primary {
  background: linear-gradient(130deg, var(--accent), var(--accent-light));
  color: #2b1402; box-shadow: 0 8px 22px rgba(243,128,32,0.28);
}
.btn-primary:hover { transform: translateY(-1px); box-shadow: 0 12px 28px rgba(243,128,32,0.35); }

.btn-secondary { background: var(--surface); color: var(--text); border-color: var(--border-strong); box-shadow: 0 2px 8px rgba(31,27,22,0.06); }
.btn-secondary:hover { background: var(--surface-2); transform: translateY(-1px); }

.btn-green { background: var(--green-soft); color: var(--green); border-color: #a7f3d0; }
.btn-green:hover { background: #a7f3d0; transform: translateY(-1px); }

.btn-red { background: var(--red-soft); color: var(--red); border-color: #fecaca; }
.btn-ghost { background: transparent; color: #d4b896; border-color: rgba(255,179,92,0.14); }
.btn-ghost:hover { background: var(--nav-2); color: #ffd9a8; }
.btn-sm { min-height: 30px; padding: 5px 10px; font-size: 12px; }
.file-input { display: none; }

/* Stats */
.stats-row { display: grid; grid-template-columns: repeat(4, minmax(0,1fr)); gap: 10px; }
.stat { border: 1px solid var(--border); border-radius: 12px; background: var(--surface-2); padding: 12px; }
.stat-number { font-size: 22px; font-weight: 850; color: var(--accent); }
.stat-label { margin-top: 2px; color: var(--muted); font-size: 12px; font-weight: 750; }

/* Progress */
.progress { height: 10px; overflow: hidden; border-radius: 999px; background: var(--surface-3); }
.progress-bar {
  width: 0%; height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--accent-light));
  border-radius: 999px; transition: width 0.3s ease;
}
.status-line { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 10px 0; color: var(--muted); font-size: 13px; }

/* Results */
.results-grid { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 16px; }
.nc-results-grid { align-items: start; }
.nc-available-box {
  min-height: 180px;
  height: 540px;
  max-height: 540px;
  overflow-y: auto;
  resize: vertical;
}
.nc-results-table-wrap {
  max-height: 540px;
  overflow-y: auto;
}
.nc-results-table-wrap table {
  min-width: 100%;
}
.nc-results-table-wrap thead th {
  position: sticky;
  top: 0;
  z-index: 1;
}

/* Table */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 12px; }
table { width: 100%; min-width: 720px; border-collapse: collapse; font-size: 13px; }
th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { background: var(--surface-2); color: var(--muted); font-size: 11px; font-weight: 850; letter-spacing: 0.06em; text-transform: uppercase; white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: #fffcf8; }

/* Badge */
.badge { display: inline-flex; align-items: center; min-height: 22px; border-radius: 999px; padding: 3px 10px; font-size: 11px; font-weight: 850; white-space: nowrap; }
.badge-green { background: var(--green-soft); color: var(--green); }
.badge-red { background: var(--red-soft); color: var(--red); }
.badge-blue { background: var(--accent-soft); color: #9a5108; }
.badge-amber { background: var(--amber-soft); color: var(--amber); }
.badge-gray { background: var(--surface-3); color: var(--muted); }

/* Empty state */
.empty-state {
  display: flex; flex-direction: column; min-height: 280px; align-items: center; justify-content: center;
  border: 1px dashed var(--border-strong); border-radius: 12px;
  color: var(--muted); text-align: center; padding: 24px; background: var(--surface-2);
}
.empty-state .empty-icon { font-size: 48px; margin-bottom: 12px; }
.empty-state h3 { margin: 0 0 6px 0; color: var(--text); }

/* Permissions */
.permission-list { display: grid; gap: 10px; }
.permission-row {
  display: grid; grid-template-columns: 34px 1fr auto; align-items: center;
  gap: 12px; border: 1px solid var(--border); border-radius: 12px;
  padding: 12px; background: var(--surface);
}
.permission-row.granted { border-color: #a7f3d0; background: #f0fdf4; }
.feature-mark {
  width: 34px; height: 34px; display: inline-flex; align-items: center; justify-content: center;
  border-radius: 10px; background: var(--accent-soft); color: #9a5108; font-weight: 850;
}
.feature-name { font-weight: 850; }
.feature-desc { margin-top: 3px; color: var(--muted); font-size: 12px; }
.feature-meta { margin-top: 4px; color: var(--green); font-size: 12px; }

/* Modal */
.modal-overlay {
  position: fixed; inset: 0; z-index: 40; display: none;
  align-items: center; justify-content: center; padding: 24px;
  background: rgba(43,26,8,0.45); backdrop-filter: blur(4px);
}
.modal-overlay.show { display: flex; }
.modal {
  width: min(420px, 100%); border-radius: 16px; border: 1px solid var(--border);
  background: var(--surface); box-shadow: 0 24px 60px rgba(243,128,32,0.18); padding: 22px;
}
.modal h3 { margin: 0 0 16px; font-size: 18px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.assign-user-list {
  display: grid;
  gap: 8px;
  max-height: 220px;
  overflow-y: auto;
  margin-top: 8px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-2);
}
.assign-user-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 10px;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
}
.assign-user-item .check-item {
  flex: 1;
  font-weight: 700;
}
.assign-user-meta {
  font-size: 12px;
  color: var(--muted);
}

/* Toast */
.toast {
  position: fixed; right: 22px; bottom: 22px; z-index: 60;
  max-width: min(420px, calc(100vw - 44px));
  border: 1px solid var(--border); border-left-width: 4px; border-radius: 10px;
  background: var(--surface); box-shadow: 0 12px 36px rgba(31,27,22,0.12);
  padding: 12px 14px; color: var(--text); font-size: 13px; font-weight: 750;
  transform: translateY(130%); transition: transform 0.25s ease;
}
.toast.show { transform: translateY(0); }
.toast-success { border-left-color: var(--green); }
.toast-error { border-left-color: var(--red); }
.toast-info { border-left-color: var(--accent); }

.muted { color: var(--muted); }
.nowrap { white-space: nowrap; }

/* Responsive */
@media (max-width: 1020px) {
  .app-layout {
    flex-direction: column;
    height: auto;
    overflow: visible;
  }
  .sidebar {
    width: 100%;
    flex-basis: auto;
    height: auto;
    overflow: visible;
  }
  .brand { padding: 16px; }
  .nav {
    display: flex;
    gap: 8px;
    padding: 10px;
    overflow-x: auto;
    max-height: none;
  }
  .nav-label { display: none; }
  .nav-item { width: auto; white-space: nowrap; }
  .sidebar-footer { padding: 8px 10px 10px; }
  .user-card { grid-template-columns: 34px 1fr auto; }
  .main {
    padding: 18px;
    height: auto;
    overflow: visible;
  }
}

@media (max-width: 760px) {
  .grid-2, .grid-3, .results-grid, .stats-row { grid-template-columns: 1fr; }
  .page-header { flex-direction: column; align-items: stretch; }
  .permission-row { grid-template-columns: 34px 1fr; }
  .permission-row .action-row { grid-column: 1 / -1; margin-top: 0; }
}

/* ── Online Users Panel ── */
.online-panel {
  padding: 10px 12px;
  border-top: 1px solid rgba(255,179,92,0.12);
}

.online-toggle {
  margin: 0;
}

.online-list {
  display: none;
  flex-direction: column;
  gap: 4px;
  max-height: 140px;
  overflow-y: auto;
  margin-top: 8px;
}

.online-panel.is-open .online-list {
  display: flex;
}

.online-item {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 4px 6px;
  border-radius: 7px;
  transition: background 0.15s;
}

.online-item:hover {
  background: rgba(255,179,92,0.08);
}

.online-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #4ade80;
  flex-shrink: 0;
  box-shadow: 0 0 4px rgba(74,222,128,0.6);
}

.online-dot.idle {
  background: #fbbf24;
  box-shadow: 0 0 4px rgba(251,191,36,0.6);
}

.online-avatar {
  width: 22px;
  height: 22px;
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 9px;
  font-weight: 800;
  flex-shrink: 0;
}

.online-avatar.admin {
  background: rgba(243,128,32,0.25);
  color: #ffd9a8;
}

.online-avatar.user {
  background: rgba(255,255,255,0.08);
  color: #d4b896;
}

.online-name {
  font-size: 12px;
  font-weight: 700;
  color: #d4b896;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.online-role {
  font-size: 10px;
  color: #a07840;
  margin-left: auto;
  flex-shrink: 0;
}

.online-empty {
  font-size: 11px;
  color: #a07840;
  text-align: center;
  padding: 4px 0;
}

.online-refresh {
  margin-top: 6px;
  font-size: 10px;
  color: #a07840;
  text-align: center;
  opacity: 0.7;
}

/* Info box */
.info-box {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-radius: 7px;
  font-size: 13px;
  background: var(--surface-2);
  border: 1px solid var(--border);
}
.info-box.warn {
  background: var(--amber-soft);
  border-color: rgba(180,83,9,0.2);
  color: var(--amber);
}
.info-box.error {
  background: var(--red-soft);
  border-color: rgba(220,38,38,0.2);
  color: var(--red);
}
.info-box.success {
  background: var(--green-soft);
  border-color: rgba(4,120,87,0.2);
  color: var(--green);
}
.info-icon { font-size: 16px; }

/* API selector in namecheck */
.nc-api-selector {
  margin-top: 10px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--surface-2);
}
.nc-api-selector label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

/* Modal large */
.modal-lg .modal {
  max-width: 640px;
}
