:root {
  --primary: #1976d2;
  --primary-hover: #1565c0;
  --primary-light: #e3f2fd;
  --bg: #f5f6f8;
  --sidebar: #f8f9fa;
  --surface: #ffffff;
  --border: #e8eaed;
  --border-light: #f0f1f3;
  --text: #1a1a2e;
  --text-secondary: #6b7280;
  --text-muted: #9ca3af;
  --success: #22c55e;
  --danger: #ef4444;
  --warning: #f59e0b;
  --radius: 8px;
  --radius-lg: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,.06);
  --shadow-lg: 0 8px 30px rgba(0,0,0,.08);
  --sidebar-w: 260px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }
.mono { font-family: "SF Mono", "Cascadia Code", Consolas, monospace; font-size: 13px; }
.muted { color: var(--text-muted); }

/* ===== Brand icon ===== */
.brand-icon {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: var(--primary);
  color: #fff;
  font-weight: 700;
  font-size: 13px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.brand-icon.sm { width: 32px; height: 32px; border-radius: 8px; font-size: 12px; }

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 16px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all .15s;
  white-space: nowrap;
}
.btn:hover { opacity: .9; }
.btn-block { width: 100%; }
.btn-sm { padding: 5px 10px; font-size: 12px; }

.btn-primary {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
.btn-primary:hover { background: var(--primary-hover); }

.btn-outline {
  background: var(--surface);
  border-color: var(--border);
  color: var(--text);
}
.btn-outline:hover { background: var(--bg); border-color: #d1d5db; }

.btn-danger-outline {
  background: var(--surface);
  border-color: #fecaca;
  color: var(--danger);
}
.btn-danger-outline:hover { background: #fef2f2; }

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  border: none;
}
.btn-ghost:hover { color: var(--text); background: var(--border-light); }

/* ===== Login ===== */
.login-wrap {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  background: var(--bg);
}
.login-brand {
  text-align: center;
  margin-bottom: 2rem;
}
.login-brand .brand-icon {
  width: 48px;
  height: 48px;
  font-size: 16px;
  margin: 0 auto 12px;
  border-radius: 12px;
}
.login-brand h1 { font-size: 1.5rem; font-weight: 700; margin-bottom: 4px; }
.login-brand p { color: var(--text-secondary); font-size: 14px; }

.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 2rem;
  box-shadow: var(--shadow-lg);
}

label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 6px;
  margin-top: 14px;
}
label:first-child { margin-top: 0; }

input[type="text"], input[type="password"], input[type="number"], textarea {
  width: 100%;
  padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 14px;
  transition: border-color .15s, box-shadow .15s;
}
input:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-light);
}
textarea { resize: vertical; min-height: 80px; }

.error { color: var(--danger); font-size: 13px; margin-top: 8px; }
.hint { color: var(--text-muted); font-size: 12px; display: block; margin-top: 4px; }

#login-form .btn { margin-top: 20px; }

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

/* ===== Sidebar ===== */
.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100vh;
  position: sticky;
  top: 0;
}

.sidebar-top {
  padding: 20px 16px 16px;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 16px;
}
.brand-title {
  font-weight: 700;
  font-size: 15px;
  color: var(--text);
  line-height: 1.2;
}
.brand-sub {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 1px;
}

.sidebar-divider {
  height: 1px;
  background: var(--border);
  margin: 0 16px;
}

.sidebar-section {
  flex: 1;
  overflow-y: auto;
  padding: 12px 8px;
}

.section-label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .06em;
  color: var(--text-muted);
  padding: 8px 8px 6px;
}

.instance-list {
  list-style: none;
}

.instance-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background .12s;
  font-size: 14px;
  color: var(--text);
}
.instance-item:hover { background: rgba(0,0,0,.04); }
.instance-item.active {
  background: var(--primary-light);
  color: var(--primary);
  font-weight: 500;
}

.instance-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
  min-width: 0;
}

.instance-status {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
  margin-left: 8px;
}

.status-link {
  color: var(--success);
  font-size: 14px;
  line-height: 1;
}
.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #d1d5db;
}
.status-dot.running { background: var(--success); }
.status-dot.error { background: var(--danger); }

.sidebar-empty {
  padding: 16px 12px;
  color: var(--text-muted);
  font-size: 13px;
}

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.user-badge {
  font-size: 12px;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ===== Main ===== */
.main {
  flex: 1;
  background: var(--surface);
  min-height: 100vh;
  overflow-y: auto;
}

.detail-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 60vh;
  color: var(--text-muted);
  text-align: center;
  padding: 2rem;
}
.empty-icon { font-size: 48px; margin-bottom: 12px; opacity: .5; }
.detail-empty p { margin: 4px 0; }

.detail-panel {
  padding: 32px 40px;
  max-width: 1100px;
}

/* ===== Tabs ===== */
.detail-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 20px;
  border-bottom: 1px solid var(--border);
}
.tab {
  padding: 10px 16px;
  border: none;
  background: none;
  color: var(--text-secondary);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}
.tab:hover { color: var(--text); }
.tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

.tab-panel { animation: fadeIn .15s ease; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* ===== Panel card / table ===== */
.panel-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px 24px;
}
.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}
.panel-head h2 { font-size: 16px; font-weight: 600; }
.count-badge {
  font-weight: 400;
  color: var(--text-muted);
  font-size: 14px;
}
.info-banner {
  background: #e8f4fd;
  border: 1px solid #bee3f8;
  border-radius: var(--radius);
  padding: 10px 14px;
  font-size: 13px;
  color: #2c5282;
  margin-bottom: 14px;
  line-height: 1.5;
}
.search-input {
  width: 100%;
  margin-bottom: 14px;
}
.table-wrap { overflow-x: auto; }
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.data-table th {
  text-align: left;
  padding: 10px 12px;
  background: var(--bg);
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .04em;
  border-bottom: 1px solid var(--border);
}
.data-table td {
  padding: 12px;
  border-bottom: 1px solid var(--border-light);
  vertical-align: middle;
}
.data-table tr:hover td { background: #fafbfc; }
.source-tag {
  display: inline-block;
  padding: 2px 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 11px;
  color: var(--text-secondary);
}
.dm-yes { color: var(--success); font-weight: 600; }
.dm-no { color: var(--text-muted); }
.table-empty {
  text-align: center;
  padding: 32px;
  color: var(--text-muted);
}
.label-hint { font-weight: 400; color: var(--text-muted); font-size: 12px; }

/* Toggle */
.toggle-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 16px 0 20px;
  cursor: pointer;
  font-size: 14px;
  color: var(--text);
}
.toggle-row input { display: none; }
.toggle-ui {
  width: 40px;
  height: 22px;
  background: #d1d5db;
  border-radius: 11px;
  position: relative;
  transition: background .2s;
  flex-shrink: 0;
}
.toggle-ui::after {
  content: "";
  position: absolute;
  width: 18px;
  height: 18px;
  background: #fff;
  border-radius: 50%;
  top: 2px;
  left: 2px;
  transition: transform .2s;
  box-shadow: 0 1px 3px rgba(0,0,0,.15);
}
.toggle-row input:checked + .toggle-ui {
  background: var(--primary);
}
.toggle-row input:checked + .toggle-ui::after {
  transform: translateX(18px);
}

#tab-broadcast textarea {
  margin-top: 8px;
}

/* DM modal */
.modal-wide { max-width: 560px; }
.modal-sub { font-size: 13px; color: var(--text-muted); margin-top: 4px; }
.dm-body { display: flex; flex-direction: column; gap: 12px; }
.dm-messages {
  max-height: 320px;
  overflow-y: auto;
  background: var(--bg);
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  padding: 12px;
  min-height: 120px;
}
.dm-msg {
  margin-bottom: 10px;
  max-width: 85%;
}
.dm-msg.out { margin-left: auto; text-align: right; }
.dm-msg .bubble {
  display: inline-block;
  padding: 8px 12px;
  border-radius: 12px;
  font-size: 13px;
  line-height: 1.5;
  text-align: left;
}
.dm-msg.in .bubble { background: var(--surface); border: 1px solid var(--border); }
.dm-msg.out .bubble { background: var(--primary-light); color: var(--text); }
.dm-msg .time {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 4px;
}
.dm-compose {
  display: flex;
  gap: 8px;
  align-items: flex-end;
}
.dm-compose textarea { flex: 1; }
.dm-empty { color: var(--text-muted); font-size: 13px; text-align: center; padding: 24px; }


.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 24px;
}
.detail-header h1 {
  font-size: 24px;
  font-weight: 700;
  margin-bottom: 4px;
}
.detail-sub {
  color: var(--text-secondary);
  font-size: 14px;
}

.status-pill {
  display: inline-block;
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
}
.status-pill.running { background: #dcfce7; color: #16a34a; }
.status-pill.stopped { background: #f3f4f6; color: var(--text-secondary); }
.status-pill.error { background: #fee2e2; color: var(--danger); }

.detail-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 28px;
  padding-bottom: 24px;
  border-bottom: 1px solid var(--border-light);
}

.detail-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.info-card {
  background: var(--bg);
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.info-card.full { grid-column: 1 / -1; }
.info-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-muted);
  margin-bottom: 6px;
}
.info-value {
  font-size: 14px;
  color: var(--text);
  word-break: break-all;
}
.error-text { color: var(--danger); }

.settings-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 20px;
}
.tag {
  padding: 4px 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 20px;
  font-size: 12px;
  color: var(--text-secondary);
}
.tag.on { background: var(--primary-light); border-color: #bbdefb; color: var(--primary); }

/* ===== Modal ===== */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,.35);
  backdrop-filter: blur(2px);
}
.modal-box {
  position: relative;
  background: var(--surface);
  border-radius: var(--radius-lg);
  width: 100%;
  max-width: 520px;
  max-height: 90vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-lg);
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px 0;
}
.modal-header h2 { font-size: 18px; font-weight: 700; }
.modal-close {
  background: none;
  border: none;
  font-size: 24px;
  color: var(--text-muted);
  cursor: pointer;
  line-height: 1;
  padding: 4px;
}
.modal-close:hover { color: var(--text); }
.modal-body {
  padding: 20px 24px 24px;
  overflow-y: auto;
}
.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--border-light);
}

.form-group { margin-bottom: 4px; }
.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.checkbox-col .check {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
  font-weight: 400;
}
.checkbox-col .check input { width: auto; }

.opt-item { margin-bottom: 12px; }
.opt-desc {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.5;
  margin: 4px 0 0 24px;
}
.broadcast-desc { margin: 8px 0 0 0; }

/* ===== Toast ===== */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  padding: 12px 20px;
  background: var(--text);
  color: #fff;
  border-radius: var(--radius);
  font-size: 14px;
  z-index: 200;
  box-shadow: var(--shadow-lg);
}
.toast.success { background: #16a34a; }
.toast.error { background: var(--danger); }

/* ===== Responsive ===== */
@media (max-width: 768px) {
  .layout { flex-direction: column; }
  .sidebar {
    width: 100%;
    min-width: 100%;
    height: auto;
    position: relative;
  }
  .detail-panel { padding: 20px 16px; }
  .detail-grid { grid-template-columns: 1fr; }
  .form-row { grid-template-columns: 1fr; }
}
