:root {
  --bg:          #090908;
  --bg2:         #111110;
  --bg3:         #1c1c1a;
  --bg-glass:    rgba(17, 17, 16, 0.92);
  --bg-glass2:   rgba(28, 28, 26, 0.96);
  --surface-soft: rgba(255, 255, 255, 0.01);
  --surface-hover: rgba(255, 255, 255, 0.05);
  --border:      #222220;
  --border-hi:   #333330;
  --border2:     rgba(255, 255, 255, 0.04);
  --text:        #ffffff;
  --text2:       #d4d4d0;
  --text3:       #8b8b84;
  --blue:        #40a9ff;
  --blue-dim:    rgba(64, 169, 255, 0.1);
  --blue-glow:   0 0 0 3px rgba(64, 169, 255, 0.12);
  --green:       #40a9ff;
  --green-dim:   rgba(64, 169, 255, 0.15);
  --red:         #ff4d4f;
  --red-dim:     rgba(255, 77, 79, 0.1);
  --yellow:      #ffc069;
  --yellow-dim:  rgba(255, 192, 105, 0.1);
  --purple:      #b37feb;
  --purple-dim:  rgba(179, 127, 235, 0.12);
  --radius:      16px;
  --radius-sm:   12px;
  --shadow:      0 22px 52px rgba(0, 0, 0, 0.34);
  --shadow-sm:   0 12px 30px rgba(0, 0, 0, 0.2);
  --backdrop:    none;
  --font-mono:   "DM Mono", "Consolas", "Courier New", monospace;
  --font-sans:   "Space Grotesk", system-ui, -apple-system, sans-serif;
  --transition:  all 0.16s ease;
  --top-glow:    rgba(64, 169, 255, 0.12);
  --bottom-glow: rgba(64, 169, 255, 0.08);
  color-scheme:  dark;
}

body[data-theme="light"] {
  --bg:          #edf2e8;
  --bg2:         #f8faf6;
  --bg3:         #eef2ea;
  --bg-glass:    rgba(248, 250, 246, 0.92);
  --bg-glass2:   rgba(238, 242, 234, 0.96);
  --surface-soft: rgba(24, 26, 23, 0.02);
  --surface-hover: rgba(24, 26, 23, 0.05);
  --border:      #dbe2d3;
  --border-hi:   #c7d0bd;
  --border2:     rgba(24, 26, 23, 0.08);
  --text:        #181a17;
  --text2:       #485043;
  --text3:       #76806f;
  --shadow:      0 20px 44px rgba(53, 69, 45, 0.12);
  --shadow-sm:   0 10px 24px rgba(53, 69, 45, 0.08);
  --top-glow:    rgba(64, 169, 255, 0.14);
  --bottom-glow: rgba(64, 169, 255, 0.10);
  color-scheme:  light;
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }
body {
  margin: 0;
  background:
    radial-gradient(circle at top left, var(--top-glow), transparent 28%),
    radial-gradient(circle at bottom right, var(--bottom-glow), transparent 24%),
    var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 13.5px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}
body::after {
  content: "";
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background-image:
    linear-gradient(var(--border2) 1px, transparent 1px),
    linear-gradient(90deg, var(--border2) 1px, transparent 1px);
  background-size: 36px 36px;
  opacity: .35;
}
body > * { position: relative; z-index: 1; }

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 999px; }
::-webkit-scrollbar-thumb:hover { background: var(--text3); }

header {
  display: flex; align-items: center; gap: 20px;
  padding: 0 28px; height: 64px;
  background: var(--bg-glass);
  border-bottom: 1.5px solid var(--border);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
  position: sticky; top: 0; z-index: 20;
}
.brand-link {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: inherit;
  text-decoration: none;
  min-width: 0;
}
.brand-icon {
  width: 32px;
  height: 32px;
  flex: 0 0 32px;
  border-radius: 8px;
  object-fit: cover;
  box-shadow: 0 0 16px rgba(64, 169, 255, 0.22);
}
.logo { font-size: 19px; font-weight: 700; color: var(--text); letter-spacing: -0.03em; }
.logo span { color: var(--green); }

.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 4px 12px;
  border: 1.5px solid var(--border);
  border-radius: 999px;
  background: var(--surface-soft);
  font-size: 11px;
  font-weight: 700;
  color: var(--text3);
  letter-spacing: 0.08em;
}
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--green); box-shadow: 0 0 0 3px rgba(64,169,255,0.12); }

.toolbar, .actions, .proxy-actions, .tg-ws-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.theme-toggle {
  width: 42px;
  height: 42px;
  margin-left: auto;
  border: 1.5px solid var(--border);
  border-radius: 999px;
  background: linear-gradient(180deg, var(--surface-hover), var(--surface-soft));
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: var(--transition);
  box-shadow: var(--shadow-sm);
}
.theme-toggle:hover {
  transform: translateY(-1px) rotate(8deg);
  border-color: var(--border-hi);
  background: linear-gradient(180deg, var(--surface-hover), rgba(255,255,255,0.01));
}
.theme-icon { font-size: 17px; line-height: 1; }

.app-shell {
  display: flex;
  min-height: calc(100vh - 64px);
}
.sidebar {
  width: 244px;
  flex: 0 0 244px;
  padding: 20px 14px;
  background: linear-gradient(180deg, rgba(255,255,255,0.02), rgba(255,255,255,0)), var(--bg2);
  border-right: 1.5px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 18px;
  box-shadow: inset -1px 0 0 rgba(255,255,255,0.03);
}
.sidebar-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-top: 10px;
}
.sidebar-group + .sidebar-group {
  border-top: 1px solid rgba(255,255,255,0.05);
  padding-top: 18px;
}
.sidebar-label {
  padding: 0 10px;
  color: var(--text3);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .12em;
  text-transform: uppercase;
}
.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.sidebar-nav button {
  min-height: 42px;
  padding: 10px 14px;
  border: 1.5px solid color-mix(in srgb, var(--border) 50%, transparent);
  border-radius: 12px;
  background: var(--surface-soft);
  color: var(--text3);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  text-align: left;
  cursor: pointer;
  transition: var(--transition);
}
.sidebar-nav button:hover {
  background: var(--surface-hover);
  color: var(--text2);
  border-color: color-mix(in srgb, var(--border) 65%, transparent);
  transform: translateX(2px);
}
.sidebar-nav button.active {
  background: linear-gradient(90deg, rgba(64,169,255,0.16), rgba(255,255,255,0.04));
  color: var(--text);
  border-color: rgba(64,169,255,0.18);
  box-shadow: inset 2px 0 0 var(--green), 0 10px 22px rgba(0,0,0,0.14);
}

.shell {
  flex: 1;
  min-width: 0;
  padding: 32px;
  overflow: auto;
}
.tab-panel { display: none; animation: fadeIn .24s ease; }
.tab-panel.active { display: block; }
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.section-head, .proxy-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 24px;
}
h1 {
  margin: 0;
  font-size: 30px;
  line-height: 1.05;
  font-weight: 700;
  letter-spacing: -.04em;
  color: var(--text);
}
h2 { margin: 0; font-size: 18px; letter-spacing: -.02em; }
p { margin: 8px 0 0; color: var(--text3); font-size: 14px; max-width: 720px; }
.right-note {
  min-width: 144px;
  padding: 10px 14px;
  border: 1.5px solid var(--border);
  border-radius: 999px;
  background: var(--bg2);
  color: var(--text3);
  text-align: right;
  box-shadow: var(--shadow-sm);
}
.right-note strong {
  display: block;
  color: var(--green);
  font-size: 14px;
  line-height: 1.1;
}
.right-note span { font-size: 11px; }

.toolbar {
  flex-wrap: wrap;
  margin-bottom: 20px;
}
.btn, .link-btn, .icon-btn, select, input, textarea {
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg3);
  color: var(--text);
  font: inherit;
  outline: none;
  transition: var(--transition);
}
.btn {
  min-height: 38px;
  padding: 0 16px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 700;
}
.btn.primary {
  background: var(--green);
  border-color: var(--green);
  color: #fff;
  box-shadow: 0 10px 22px rgba(64,169,255,.18);
}
.btn.ghost { background: var(--bg2); color: var(--text2); }
.btn:hover, .link-btn:hover, .icon-btn:hover {
  transform: translateY(-1px);
  border-color: var(--border-hi);
}
.link-btn {
  min-height: 36px;
  padding: 0 12px;
  cursor: pointer;
  background: var(--bg2);
  color: var(--text2);
  font-weight: 600;
}
.icon-btn {
  width: 38px;
  height: 38px;
  cursor: pointer;
  display: grid;
  place-items: center;
  background: var(--bg2);
}
select, input { height: 38px; padding: 0 12px; }
textarea { min-height: 84px; padding: 12px; resize: vertical; }
select:focus, input:focus, textarea:focus {
  border-color: var(--green);
  box-shadow: 0 0 0 3px rgba(64,169,255,.1);
}
.search { min-width: 260px; }
.pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 34px;
  padding: 0 14px;
  border: 1.5px solid var(--border);
  border-radius: 999px;
  background: var(--bg3);
  color: var(--text2);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .04em;
}
.pill span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: currentColor;
}
.pill.online {
  color: var(--green);
  border-color: rgba(64,169,255,.25);
  background: rgba(64,169,255,.12);
}

.metric-strip, .usage-grid, .settings-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 16px;
  margin: 20px 0;
}
.account-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 16px;
  margin: 20px 0;
  align-items: start;
}
.account-card, .proxy-row, .table-card, .metric, .setting-card, .route-card, .tg-ws-panel, .assignment-row {
  background: var(--bg2);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  position: relative;
  overflow: hidden;
}
.account-card::before, .metric::before, .setting-card::before, .route-card::before, .tg-ws-panel::before {
  content: "";
  position: absolute;
  top: 0;
  left: 20%;
  right: 20%;
  height: 1px;
  background: linear-gradient(90deg, transparent, rgba(24,26,23,.16), transparent);
}
.account-card {
  padding: 18px;
  transition: var(--transition);
  display: flex;
  flex-direction: column;
  min-height: 220px;
}
.account-card:hover, .metric:hover, .setting-card:hover, .table-card:hover, .tg-ws-panel:hover {
  border-color: var(--border-hi);
  box-shadow: var(--shadow);
}
.card-head, .row, .proxy-row, .assignment-row, .tg-ws-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.card-head {
  flex-wrap: wrap;
  min-height: 44px;
}
.identity { display: flex; align-items: center; gap: 12px; min-width: 0; flex: 1 1 auto; }
.avatar {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  flex: 0 0 40px;
  font-weight: 800;
  background: var(--blue-dim);
  color: var(--blue);
  border: 1.5px solid rgba(37,99,235,.16);
}
.email, .truncate {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.email { max-width: 220px; font-weight: 700; }
.subline {
  color: var(--text3);
  font-size: 11px;
  margin-top: 3px;
}
.platform {
  color: var(--blue);
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: .05em;
}
.card-body {
  margin-top: 14px;
  display: grid;
  gap: 9px;
  flex: 1;
  align-content: start;
}
.row span:first-child { color: var(--text3); font-size: 12px; }
.row strong { font-size: 14px; }
.divider { height: 1px; background: var(--border2); margin: 4px 0; }
.bar {
  height: 8px;
  background: var(--bg3);
  border-radius: 999px;
  overflow: hidden;
}
.bar span { display: block; height: 100%; background: var(--green); width: 0; }

.dialog-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 5px 8px; margin-top: 4px;
  background: var(--surface-soft);
  border-radius: 8px;
  font-size: 12px;
}
.dialog-name {
  color: var(--text2);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.dialog-badge {
  color: var(--green);
  font-weight: 700;
  font-size: 11px;
  flex-shrink: 0;
}

.metric, .setting-card { padding: 18px; }
.metric span, .setting-card span {
  color: var(--text3);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
}
.metric strong {
  display: block;
  margin-top: 8px;
  color: var(--text);
  font-size: 28px;
  line-height: 1;
  letter-spacing: -.04em;
}
.metric p { font-size: 12px; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--green-dim);
  color: var(--green);
  border: 1.5px solid rgba(64,169,255,.22);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .04em;
}
.badge.red { background: var(--red-dim); border-color: rgba(201,54,62,.25); color: var(--red); }
.badge.amber { background: var(--yellow-dim); border-color: rgba(183,121,31,.25); color: var(--yellow); }

.spacer { height: 24px; }
.proxy-head { margin-top: 8px; }
.proxy-actions, .proxy-health { display: flex; align-items: center; flex-wrap: wrap; gap: 10px; }
.proxy-row, .assignment-row { padding: 16px; margin-top: 10px; }
.proxy-row code, .key-code, .route-card code, .tg-ws-panel code {
  font-family: var(--font-mono);
  color: var(--text2);
  word-break: break-all;
}
.proxy-health { color: var(--text3); }
.tg-ws-panel { padding: 18px; }
.tg-ws-actions { flex-wrap: wrap; justify-content: flex-end; }
.tg-ws-row h1 { font-size: 18px; letter-spacing: -.02em; }

.table-card {
  overflow: auto;
  margin-bottom: 16px;
}
table {
  width: 100%;
  border-collapse: collapse;
  text-align: left;
}
thead { background: var(--bg3); }
th {
  padding: 14px 16px;
  color: var(--text2);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
  border-bottom: 1.5px solid var(--border);
  white-space: nowrap;
}
td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border2);
  color: var(--text);
  vertical-align: middle;
}
tr:last-child td { border-bottom: 0; }
tbody tr { transition: background .16s ease; }
tbody tr:hover { background: rgba(24,26,23,.035); }
.actions { flex-wrap: wrap; }
.mini-btn {
  border: 1px solid transparent;
  background: transparent;
  color: var(--text2);
  cursor: pointer;
  padding: 5px 8px;
  border-radius: 8px;
  font: inherit;
  font-size: 12px;
  font-weight: 700;
  transition: var(--transition);
}
.mini-btn:hover {
  background: var(--surface-hover);
  border-color: var(--border);
}
.mini-btn.danger {
  color: var(--text3);
}
.mini-btn.danger:hover {
  color: var(--red);
  background: var(--red-dim);
  border-color: rgba(255, 77, 79, 0.25);
  transform: scale(1.08);
}
.assignment-list { display: grid; gap: 10px; }
.route-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 12px;
}
.route-card { padding: 16px; }

.inline-form {
  display: grid;
  grid-template-columns: 140px repeat(4, minmax(120px, 1fr)) 130px;
  gap: 10px;
  align-items: start;
  margin-bottom: 18px;
}
.inline-form textarea { grid-column: span 5; }
.inline-form.compact { grid-template-columns: repeat(3, minmax(140px, 1fr)) 100px 140px; }
.inline-form.compact textarea { grid-column: auto; }
.checkline {
  min-height: 38px;
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text3);
  font-weight: 600;
}
.checkline input { width: auto; height: auto; accent-color: var(--green); }

.modal-overlay {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(9, 9, 8, .58);
  z-index: 50;
}
.modal-overlay.open { display: flex; }
.modal {
  width: min(560px, 100%);
  background: var(--bg2);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.modal-head, .modal-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-bottom: 1px solid var(--border);
}
.modal-actions {
  border-top: 1px solid var(--border);
  border-bottom: 0;
  justify-content: flex-end;
  gap: 10px;
}
.modal-body {
  padding: 16px;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}
.modal-body label {
  display: grid;
  gap: 6px;
  color: var(--text3);
  font-weight: 600;
}
.modal-body input, .modal-body select, .modal-body textarea { width: 100%; }
.modal-body .wide { grid-column: 1 / -1; }
.modal-body .hidden { display: none; }
.input-action {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
}
.form-actions-row {
  display: grid;
  grid-template-columns: 1fr 120px 1fr;
  gap: 8px;
  align-items: center;
}

#toast {
  position: fixed;
  right: 22px;
  bottom: 22px;
  min-width: 240px;
  max-width: 380px;
  padding: 13px 15px;
  border-radius: var(--radius-sm);
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text);
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translateY(8px);
  pointer-events: none;
  transition: .18s ease;
  z-index: 80;
}
#toast.show { opacity: 1; transform: translateY(0); }

@media (max-width: 1100px) {
  .topbar { padding: 0 18px; }
  .sidebar { width: 210px; flex-basis: 210px; }
  .shell { padding: 24px; }
  h1 { font-size: 26px; }
}

@media (max-width: 760px) {
  .topbar {
    height: auto;
    min-height: 64px;
    align-items: stretch;
    flex-direction: column;
    padding: 12px;
  }
  .header-actions, .toolbar { flex-wrap: wrap; }
  .app-shell { flex-direction: column; }
  .sidebar {
    width: 100%;
    flex-basis: auto;
    border-right: 0;
    border-bottom: 1.5px solid var(--border);
    padding: 12px;
  }
  .sidebar-nav {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .shell { padding: 18px 12px 64px; }
  .section-head, .proxy-head, .proxy-row, .assignment-row, .tg-ws-row {
    align-items: stretch;
    flex-direction: column;
  }
  .modal-body, .form-actions-row, .inline-form, .inline-form.compact {
    grid-template-columns: 1fr;
  }
  .inline-form textarea { grid-column: auto; }
  .right-note { text-align: left; }
}
