:root {
  color-scheme: light;
  --surface-1: #fcfcfb;
  --surface-2: #ffffff;
  --page: #f9f9f7;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --gridline: #e1e0d9;
  --border: rgba(11, 11, 11, 0.10);
  --series-telegram: #2a78d6;
  --series-max: #eb6834;
  --status-good: #0ca30c;
  --status-good-text: #006300;
  --status-critical: #d03b3b;
  --focus: #2a78d6;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --surface-1: #1a1a19;
    --surface-2: #202020;
    --page: #0d0d0d;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --gridline: #2c2c2a;
    --border: rgba(255, 255, 255, 0.10);
    --series-telegram: #3987e5;
    --series-max: #d95926;
    --status-good: #0ca30c;
    --status-good-text: #0ca30c;
    --status-critical: #e66767;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--page);
  color: var(--text-primary);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}

#app { height: 100%; }

button, input, textarea { font-family: inherit; font-size: inherit; }

a { color: var(--series-telegram); }

/* ---- Login ---- */

.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.login-card {
  width: 100%;
  max-width: 340px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 28px 24px;
}

.login-card h1 {
  font-size: 18px;
  margin: 0 0 4px;
}

.login-card p.sub {
  color: var(--text-secondary);
  font-size: 13px;
  margin: 0 0 20px;
}

.field {
  margin-bottom: 14px;
}

.field label {
  display: block;
  font-size: 12px;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.field input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-1);
  color: var(--text-primary);
}

.field input:focus {
  outline: 2px solid var(--focus);
  outline-offset: 1px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: none;
  border-radius: 8px;
  padding: 10px 16px;
  background: var(--series-telegram);
  color: #fff;
  cursor: pointer;
  font-weight: 600;
}

.btn:disabled { opacity: 0.6; cursor: default; }
.btn.secondary { background: transparent; color: var(--text-secondary); border: 1px solid var(--border); }
.btn.full { width: 100%; }

.error-text {
  color: var(--status-critical);
  font-size: 13px;
  margin: 8px 0 0;
}

/* ---- Shell ---- */

.shell {
  display: grid;
  grid-template-columns: 220px 1fr;
  height: 100vh;
}

.sidebar {
  background: var(--surface-1);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 16px 12px;
}

.brand {
  font-weight: 700;
  font-size: 14px;
  padding: 0 8px 20px;
  color: var(--text-primary);
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 10px;
  border-radius: 8px;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 14px;
  margin-bottom: 2px;
  border: none;
  background: transparent;
  width: 100%;
  text-align: left;
}

.nav-item:hover { background: var(--gridline); }
.nav-item.active { background: var(--series-telegram); color: #fff; }

.nav-badge {
  margin-left: auto;
  background: var(--status-critical);
  color: #fff;
  font-size: 11px;
  border-radius: 999px;
  min-width: 18px;
  height: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 5px;
}

.sidebar-footer {
  margin-top: auto;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-muted);
}

.sidebar-footer .whoami {
  margin-bottom: 8px;
  color: var(--text-secondary);
}

.main {
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* ---- Conversations ---- */

.conv-layout {
  display: grid;
  grid-template-columns: 320px 1fr;
  height: 100%;
  min-height: 0;
}

.conv-list-pane {
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.conv-filters {
  display: flex;
  gap: 6px;
  padding: 12px;
  border-bottom: 1px solid var(--border);
}

.chip {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-secondary);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 12px;
  cursor: pointer;
}

.chip.active {
  background: var(--text-primary);
  color: var(--surface-1);
  border-color: var(--text-primary);
}

.conv-list {
  overflow-y: auto;
  flex: 1;
}

.conv-card {
  display: flex;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--gridline);
  cursor: pointer;
}

.conv-card:hover { background: var(--surface-2); }
.conv-card.active { background: var(--surface-2); box-shadow: inset 3px 0 0 var(--series-telegram); }

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 999px;
  background: var(--gridline);
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 13px;
  flex-shrink: 0;
}

.conv-card-body { min-width: 0; flex: 1; }

.conv-card-top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}

.conv-name {
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-time { font-size: 11px; color: var(--text-muted); flex-shrink: 0; }

.conv-preview {
  font-size: 12px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.conv-meta { display: flex; align-items: center; gap: 6px; margin-top: 4px; }

.badge-channel {
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 4px;
  font-weight: 700;
  color: #fff;
}

.badge-channel.telegram { background: var(--series-telegram); }
.badge-channel.max { background: var(--series-max); }

.unread-dot {
  min-width: 16px;
  height: 16px;
  border-radius: 999px;
  background: var(--status-critical);
  color: #fff;
  font-size: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 4px;
  margin-left: auto;
}

.empty-state {
  padding: 40px 20px;
  text-align: center;
  color: var(--text-muted);
  font-size: 13px;
}

/* ---- Thread ---- */

.thread-pane {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.thread-header {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
}

.thread-title { font-size: 14px; font-weight: 700; }
.thread-sub { font-size: 12px; color: var(--text-muted); }

.thread-body {
  flex: 1;
  overflow-y: auto;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.bubble {
  max-width: 60%;
  padding: 8px 12px;
  border-radius: 12px;
  font-size: 13px;
  line-height: 1.4;
  white-space: pre-wrap;
  word-break: break-word;
}

.bubble.in {
  align-self: flex-start;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-bottom-left-radius: 2px;
}

.bubble.out {
  align-self: flex-end;
  background: var(--series-telegram);
  color: #fff;
  border-bottom-right-radius: 2px;
}

.bubble-meta {
  font-size: 10px;
  opacity: 0.7;
  margin-top: 4px;
}

.thread-composer {
  border-top: 1px solid var(--border);
  padding: 12px;
  display: flex;
  gap: 8px;
  align-items: flex-end;
}

.thread-composer textarea {
  flex: 1;
  resize: none;
  min-height: 40px;
  max-height: 140px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-1);
  color: var(--text-primary);
}

/* ---- Stats ---- */

.stats-view { padding: 24px; overflow-y: auto; }
.stats-view h2 { font-size: 18px; margin: 0 0 4px; }
.stats-view .sub { color: var(--text-secondary); font-size: 13px; margin: 0 0 20px; }

.tile-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 12px;
  margin-bottom: 28px;
}

.tile {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
}

.tile .tile-label { font-size: 12px; color: var(--text-secondary); margin-bottom: 6px; }
.tile .tile-value { font-size: 26px; font-weight: 700; font-variant-numeric: proportional-nums; }
.tile .tile-value.good { color: var(--status-good-text); }
.tile .tile-value.critical { color: var(--status-critical); }

.split-section {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px 18px;
  margin-bottom: 16px;
}

.split-section h3 { font-size: 13px; margin: 0 0 14px; color: var(--text-secondary); }

.split-row { display: flex; align-items: center; gap: 12px; margin-bottom: 10px; font-size: 12px; }
.split-row:last-child { margin-bottom: 0; }

.split-label { width: 90px; flex-shrink: 0; display: flex; align-items: center; gap: 6px; }
.split-dot { width: 8px; height: 8px; border-radius: 999px; flex-shrink: 0; }
.split-dot.telegram { background: var(--series-telegram); }
.split-dot.max { background: var(--series-max); }

.split-bar-track {
  flex: 1;
  height: 10px;
  border-radius: 4px;
  background: var(--gridline);
  overflow: hidden;
  display: flex;
}

.split-bar-fill.telegram { background: var(--series-telegram); }
.split-bar-fill.max { background: var(--series-max); margin-left: 2px; }

.split-value { width: 40px; text-align: right; flex-shrink: 0; font-variant-numeric: tabular-nums; }

.legend-row { display: flex; gap: 16px; font-size: 12px; color: var(--text-secondary); margin-bottom: 16px; }
.legend-row .legend-item { display: flex; align-items: center; gap: 6px; }

/* ---- Mobile ---- */

/* ---- Generic page (leads/orders/broadcast/reflinks/seminars/cosmetologists/banned/admins) ---- */

.page-view { padding: 24px; overflow-y: auto; height: 100%; }
.page-view h2 { font-size: 18px; margin: 0 0 4px; }
.page-view .sub { color: var(--text-secondary); font-size: 13px; margin: 0 0 20px; }

.toolbar {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
  margin-bottom: 16px;
}

.data-table-wrap {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  margin-bottom: 24px;
}

table.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

table.data-table th {
  text-align: left;
  padding: 10px 14px;
  color: var(--text-muted);
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

table.data-table td {
  padding: 10px 14px;
  border-bottom: 1px solid var(--gridline);
  vertical-align: top;
}

table.data-table tr:last-child td { border-bottom: none; }
table.data-table tr:hover td { background: var(--surface-1); }

.badge-temp {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
}

.form-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 18px 20px;
  margin-bottom: 24px;
  max-width: 520px;
}

.form-card h3 { font-size: 13px; margin: 0 0 14px; color: var(--text-secondary); }

.form-row { display: flex; gap: 10px; }
.form-row .field { flex: 1; }

.field textarea {
  width: 100%;
  min-height: 90px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-1);
  color: var(--text-primary);
  resize: vertical;
}

.segment-picker { display: flex; gap: 8px; margin-bottom: 14px; flex-wrap: wrap; }

.segment-toggle {
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-secondary);
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 13px;
  cursor: pointer;
}

.segment-toggle.selected {
  background: var(--series-telegram);
  border-color: var(--series-telegram);
  color: #fff;
}

.recipients-hint { font-size: 12px; color: var(--text-muted); margin: -6px 0 14px; }

.btn.small { padding: 6px 10px; font-size: 12px; }
.btn.danger { background: var(--status-critical); }
.btn.good { background: var(--status-good); }

.table-actions { display: flex; gap: 6px; }

.copy-link {
  font-size: 12px;
  color: var(--text-secondary);
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 8px;
  cursor: pointer;
  font-family: monospace;
}

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text-primary);
  color: var(--surface-1);
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 13px;
  z-index: 50;
}

@media (max-width: 860px) {
  .data-table-wrap { overflow-x: auto; }
  table.data-table { min-width: 640px; }
}

@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar {
    flex-direction: row;
    align-items: center;
    padding: 10px 12px;
    overflow-x: auto;
  }
  .brand { display: none; }
  .sidebar-footer { display: none; }
  .nav-item { width: auto; white-space: nowrap; }
  .conv-layout { grid-template-columns: 1fr; }
  .conv-list-pane.has-active { display: none; }
  .thread-pane.no-active { display: none; }
  .bubble { max-width: 82%; }
}
