:root {
  --bg: var(--tg-theme-secondary-bg-color, #111622);
  --surface: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, #1a2133));
  --surface-elev: var(--tg-theme-bg-color, #161d2e);
  --text: var(--tg-theme-text-color, #f1f5f9);
  --hint: var(--tg-theme-hint-color, #8d9ab0);
  --accent: var(--tg-theme-button-color, #38bdf8);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: var(--tg-theme-destructive-text-color, #ef4444);
  --sep: var(--tg-theme-section-separator-color, rgba(255, 255, 255, 0.08));
  --ok: #22c55e;
  --warn: #f59e0b;
  --radius: 14px;
  --radius-sm: 10px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.4;
  min-height: 100vh;
  overflow-x: hidden;
}

.hidden { display: none !important; }

.app-shell {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  max-width: 640px;
  margin: 0 auto;
}

/* Top Bar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--surface-elev);
  border-bottom: 1px solid var(--sep);
  backdrop-filter: blur(12px);
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  object-fit: cover;
  box-shadow: 0 2px 8px rgba(56, 189, 248, 0.25);
  flex-shrink: 0;
}

.brand-meta { min-width: 0; }

.brand-title {
  font-weight: 700;
  font-size: 16px;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.brand-sub {
  font-size: 12px;
  color: var(--hint);
}

.lang-pill {
  border: 1px solid var(--sep);
  background: var(--surface);
  color: var(--text);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

/* Main Content */
.main-view {
  flex: 1;
  padding: 14px 14px 88px;
}

/* BotFather-style Section Headers & Grouped Inset Lists */
.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 6px 6px;
  margin-top: 16px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--hint);
}

.section-header:first-child { margin-top: 2px; }

.card {
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--sep);
  margin-bottom: 12px;
}

.card-pad {
  padding: 14px;
}

.list-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--sep);
  background: transparent;
  width: 100%;
  color: var(--text);
  text-align: left;
  border-left: 0;
  border-right: 0;
  border-top: 0;
  font-family: inherit;
  font-size: 15px;
}

.list-row:last-child { border-bottom: 0; }

.list-row.clickable {
  cursor: pointer;
  transition: background 0.12s ease;
}

.list-row.clickable:active {
  background: rgba(128, 128, 128, 0.12);
}

.row-left {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
  flex: 1;
}

.row-icon {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  background: rgba(56, 189, 248, 0.13);
  flex-shrink: 0;
}

.row-icon.admin { background: rgba(168, 85, 247, 0.16); }
.row-icon.channel { background: rgba(245, 158, 11, 0.16); }
.row-icon.muted { background: rgba(148, 163, 184, 0.14); }
.row-icon.danger { background: rgba(239, 68, 68, 0.15); }

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

.row-title {
  font-weight: 600;
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.row-sub {
  font-size: 12.5px;
  color: var(--hint);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 1px;
}

.row-chevron {
  color: var(--hint);
  font-size: 18px;
  font-weight: 500;
  flex-shrink: 0;
}

/* Quick Add Chips */
.quick-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 14px;
}

.quick-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 5px;
  padding: 11px 8px;
  background: var(--surface);
  border: 1px solid var(--sep);
  border-radius: var(--radius);
  color: var(--text);
  font-family: inherit;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
}

.quick-btn:active { transform: scale(0.97); }
.quick-btn .q-ico { font-size: 20px; }

/* Native iOS Toggle Switch */
.switch {
  position: relative;
  display: inline-block;
  width: 46px;
  height: 27px;
  flex-shrink: 0;
}

.switch input { opacity: 0; width: 0; height: 0; }

.slider {
  position: absolute;
  inset: 0;
  background: rgba(148, 163, 184, 0.35);
  border-radius: 999px;
  transition: background 0.18s ease;
  cursor: pointer;
}

.slider::before {
  content: "";
  position: absolute;
  width: 21px;
  height: 21px;
  left: 3px;
  top: 3px;
  border-radius: 50%;
  background: #ffffff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  transition: transform 0.18s ease;
}

.switch input:checked + .slider { background: var(--accent); }
.switch input:checked + .slider::before { transform: translateX(19px); }

/* Segmented Control */
.seg-group {
  display: flex;
  background: var(--bg);
  border-radius: 10px;
  padding: 3px;
  gap: 3px;
  border: 1px solid var(--sep);
}

.seg-btn {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--hint);
  padding: 8px 6px;
  border-radius: 8px;
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
}

.seg-btn.active {
  background: var(--accent);
  color: var(--accent-text);
}

/* Native Inputs & Selects */
.select-inline {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--sep);
  border-radius: 9px;
  padding: 6px 10px;
  font-family: inherit;
  font-size: 13.5px;
  font-weight: 500;
  max-width: 175px;
}

.field {
  margin-bottom: 12px;
}

.field label {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--hint);
  margin-bottom: 5px;
}

.input, .textarea, .select-full {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--sep);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-family: inherit;
  font-size: 14.5px;
  outline: none;
}

.input:focus, .textarea:focus, .select-full:focus {
  border-color: var(--accent);
}

.textarea {
  min-height: 92px;
  resize: vertical;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  width: 100%;
  padding: 12px 14px;
  border-radius: 11px;
  border: 0;
  font-family: inherit;
  font-size: 14.5px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s ease, transform 0.1s ease;
}

.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }

.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-secondary { background: var(--bg); color: var(--text); border: 1px solid var(--sep); }
.btn-danger { background: rgba(239, 68, 68, 0.14); color: var(--danger); border: 1px solid rgba(239, 68, 68, 0.3); }

.btn-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 8px;
}

.btn-sm {
  padding: 6px 11px;
  font-size: 12.5px;
  width: auto;
  border-radius: 8px;
}

/* Badges & Status */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11.5px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 999px;
}

.badge.ok { background: rgba(34, 197, 94, 0.15); color: var(--ok); }
.badge.warn { background: rgba(245, 158, 11, 0.16); color: var(--warn); }
.badge.accent { background: rgba(56, 189, 248, 0.16); color: var(--accent); }

/* Preview Box */
.preview-box {
  background: var(--bg);
  border: 1px solid var(--sep);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius-sm);
  padding: 12px;
  font-size: 13.5px;
  line-height: 1.48;
  white-space: pre-wrap;
  margin: 10px 0;
  word-break: break-word;
}

/* Category Chips */
.chip-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
}

.chip {
  padding: 7px 11px;
  border-radius: 999px;
  border: 1px solid var(--sep);
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
}

.chip.active {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
}

/* Stats Grid */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.kpi-tile {
  background: var(--bg);
  border: 1px solid var(--sep);
  border-radius: 10px;
  padding: 10px 8px;
  text-align: center;
}

.kpi-val {
  font-size: 20px;
  font-weight: 700;
}

.kpi-lbl {
  font-size: 11px;
  color: var(--hint);
  margin-top: 2px;
}

/* Hero Privacy Box */
.hero-box {
  background: linear-gradient(135deg, rgba(56, 189, 248, 0.14), rgba(99, 102, 241, 0.12));
  border: 1px solid rgba(56, 189, 248, 0.25);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 14px;
}

.hero-title { font-weight: 700; font-size: 15.5px; margin-bottom: 4px; }
.hero-desc { font-size: 13px; color: var(--hint); }

/* Upload Preview */
.upload-box {
  border: 1.5px dashed var(--sep);
  border-radius: var(--radius-sm);
  padding: 12px;
  text-align: center;
  cursor: pointer;
  background: var(--bg);
}

.upload-thumb {
  max-width: 100%;
  max-height: 150px;
  border-radius: 8px;
  margin-top: 8px;
}

/* Empty State */
.empty-box {
  text-align: center;
  padding: 28px 18px;
  color: var(--hint);
}

.empty-ico { font-size: 38px; margin-bottom: 8px; }
.empty-title { color: var(--text); font-weight: 700; font-size: 16px; margin-bottom: 6px; }

/* Bottom Tab Bar */
.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  max-width: 640px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  background: var(--surface-elev);
  border-top: 1px solid var(--sep);
  padding: 6px 8px calc(8px + env(safe-area-inset-bottom, 0px));
  z-index: 30;
}

.tab-item {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  background: transparent;
  border: 0;
  color: var(--hint);
  font-family: inherit;
  font-size: 11px;
  font-weight: 600;
  padding: 4px 0;
  cursor: pointer;
}

.tab-item.active { color: var(--accent); }
.tab-ico { font-size: 20px; line-height: 1.1; }

.tab-badge {
  position: absolute;
  top: 0;
  right: calc(50% - 22px);
  background: var(--danger);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  padding: 1px 5px;
  border-radius: 999px;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 78px;
  left: 50%;
  transform: translateX(-50%);
  background: #0f172a;
  color: #f8fafc;
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 13.5px;
  font-weight: 600;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  border: 1px solid rgba(255, 255, 255, 0.12);
  z-index: 50;
  max-width: calc(100vw - 32px);
  text-align: center;
}

.toast.err { background: #7f1d1d; }
.toast.ok { background: #14532d; }

/* Skeletons */
.skeleton-wrap { display: flex; flex-direction: column; gap: 12px; }
.skeleton-card {
  height: 82px;
  border-radius: var(--radius);
  background: linear-gradient(90deg, var(--surface) 25%, rgba(255,255,255,0.06) 50%, var(--surface) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.2s infinite;
}
.skeleton-card.short { height: 54px; }
@keyframes shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }
