:root {
  --bg: #0c0d12;
  --bg-elev: #14161e;
  --bg-elev-2: #1c1f2a;
  --border: #2a2e3c;
  --text: #e7e9ee;
  --text-muted: #8b91a3;
  --accent: #6f8bff;
  --accent-2: #a577ff;
  --green: #4dd6a3;
  --red: #ff6b81;
  --yellow: #ffc94d;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }

html {
  margin: 0;
  padding: 0;
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
}
body {
  margin: 0;
  padding: 0;
  max-width: 100%;
  overflow-x: hidden;
  font-family: 'DM Sans', system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.45;
}
img, svg, video { max-width: 100%; height: auto; }

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.92em; background: rgba(255,255,255,0.06); padding: 2px 6px; border-radius: 4px; }

.muted { color: var(--text-muted); }
.small { font-size: 12px; }
.error { color: var(--red); margin-top: 8px; }
.alert { background: rgba(255,201,77,0.12); border: 1px solid rgba(255,201,77,0.4); padding: 12px; border-radius: var(--radius-sm); }

/* ------- AUTH PAGE ------- */
.page-auth {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  position: relative;
  padding: 32px 16px;
  overflow-x: hidden;
}
.bg-glow {
  position: fixed; inset: 0;
  background: radial-gradient(60% 50% at 30% 30%, rgba(111,139,255,0.18), transparent 60%),
              radial-gradient(60% 50% at 70% 70%, rgba(165,119,255,0.15), transparent 60%);
  z-index: -1;
  pointer-events: none;
}
.shell {
  display: grid;
  grid-template-columns: minmax(260px, 1fr) minmax(320px, 460px);
  gap: 48px;
  max-width: 1100px;
  width: 100%;
  align-items: center;
}
.brand .logo { font-size: 38px; font-weight: 700; }
.brand .logo span { color: var(--accent); }
.brand .tagline { color: var(--text-muted); margin: 16px 0 24px; }
.brand .features { padding: 0; list-style: none; }
.brand .features li { padding: 8px 0; border-top: 1px solid var(--border); }
.brand .features li:first-child { border-top: 0; }

.card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
  box-shadow: var(--shadow);
}
.card h1 { margin: 0 0 8px; }
.tg-widget { margin-top: 16px; min-height: 50px; }
.tg-fallback-btn {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  margin-top: 12px;
  padding: 12px 20px;
  border: none;
  border-radius: 12px;
  background: linear-gradient(180deg, #2aabee 0%, #229ed9 100%);
  color: #fff;
  font: 600 15px/1 'DM Sans', system-ui, sans-serif;
  cursor: pointer;
  box-shadow: 0 6px 16px rgba(34,158,217,.25);
  transition: transform .1s ease, box-shadow .15s ease, filter .15s ease;
}
.tg-fallback-btn:hover { filter: brightness(1.05); box-shadow: 0 8px 22px rgba(34,158,217,.35); }
.tg-fallback-btn:active { transform: translateY(1px); }
.tg-fallback-btn .tg-icon { display: inline-flex; align-items: center; justify-content: center; }

@media (max-width: 800px) {
  .page-auth { padding: 24px 14px; place-items: start center; }
  .shell { grid-template-columns: 1fr; gap: 24px; }
  .brand .logo { font-size: 30px; }
  .brand .tagline { margin: 12px 0 16px; }
  .card { padding: 22px; }
  .card h1 { font-size: 22px; }
}

/* ------- APP LAYOUT ------- */
.page-app {
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr);
  min-height: 100vh;
  min-height: 100dvh;
  width: 100%;
  max-width: 100%;
}
.sidebar {
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 24px;
  position: sticky;
  top: 0;
  height: 100vh;
  height: 100dvh;
  min-width: 0;
}
.brand-mini { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 18px; }
.logo-dot { width: 10px; height: 10px; background: linear-gradient(135deg, var(--accent), var(--accent-2)); border-radius: 50%; }
.nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav a {
  color: var(--text);
  padding: 10px 12px;
  border-radius: var(--radius-sm);
}
.nav a:hover { background: var(--bg-elev-2); text-decoration: none; }
.nav a.active { background: var(--bg-elev-2); color: var(--accent); }
.me { font-size: 13px; }
.me-name { font-weight: 600; }
.me-role { text-transform: uppercase; font-size: 11px; letter-spacing: 0.5px; }
.me .btn { width: 100%; margin-top: 8px; }

.content {
  padding: 24px 32px 64px;
  max-width: 1280px;
  min-width: 0;
  width: 100%;
  box-sizing: border-box;
}
.page { min-width: 0; max-width: 100%; }
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 24px;
}
.page-title { margin: 0; font-size: 24px; flex: 1 1 auto; min-width: 0; }
.topbar-actions {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  flex: 1 1 auto;
  min-width: 0;
  max-width: 100%;
}
.topbar .filters {
  flex: 1 1 100%;
  max-width: 100%;
}

/* ------- BUTTONS / FORMS ------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
  color: var(--text);
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-family: inherit;
  font-size: 14px;
  transition: background 120ms, border-color 120ms;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: linear-gradient(135deg, var(--accent), var(--accent-2)); border-color: transparent; color: white; }
.btn.primary:hover { filter: brightness(1.1); }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 10px; font-size: 12px; }
.btn.active { background: var(--bg-elev-2); border-color: var(--accent); color: var(--accent); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.form { display: flex; flex-direction: column; gap: 12px; min-width: min(360px, 100%); }
.form .field { display: flex; flex-direction: column; gap: 4px; }
.form .field span { color: var(--text-muted); font-size: 12px; }
.form .field input, .form .field select, .form .field textarea {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  font-family: inherit;
  font-size: 14px;
  resize: vertical;
}
.form .field input:focus, .form .field select:focus, .form .field textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.check { display: flex; align-items: center; gap: 8px; }
.form-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 4px; }

.filters {
  display: flex;
  gap: 8px;
  align-items: stretch;
  flex-wrap: wrap;
  max-width: 100%;
}
.filters select, .filters input[type="search"] {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 7px 10px;
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 13px;
  max-width: 100%;
  min-width: 0;
}
.filters input[type="search"] { flex: 1 1 140px; min-width: 0; }
.filters select { flex: 1 1 120px; min-width: 0; }
.filters .btn { flex: 0 0 auto; }

/* ------- CARDS / STATS ------- */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; margin-bottom: 24px; }
.stat {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 20px;
}
.stat-num { font-size: 28px; font-weight: 700; line-height: 1.1; }
.stat-label { color: var(--text-muted); font-size: 12px; text-transform: uppercase; letter-spacing: 0.5px; margin-top: 4px; }

.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 16px; margin-bottom: 16px; }
.card-block {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}
.card-block h2 { margin: 0 0 12px; font-size: 16px; }
.list { padding-left: 20px; margin: 0; }
.list li { padding: 4px 0; }
.list li .num { color: var(--text-muted); font-size: 12px; margin-left: 6px; }

/* ------- TABLES ------- */
.table {
  width: 100%;
  border-collapse: collapse;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.table th, .table td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
.table th { color: var(--text-muted); font-weight: 500; font-size: 12px; text-transform: uppercase; letter-spacing: 0.4px; background: var(--bg-elev-2); }
.table tr:last-child td { border-bottom: 0; }
.table .num { text-align: right; }
.table td.actions { white-space: nowrap; text-align: right; }
.table .feed { font-family: ui-monospace, monospace; font-size: 12px; color: var(--text-muted); word-break: break-all; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; line-height: 1.6; border: 1px solid var(--border); }
.badge.on { color: var(--green); border-color: rgba(77,214,163,0.4); }
.badge.off { color: var(--text-muted); }
.badge.role-admin { color: var(--accent); border-color: rgba(111,139,255,0.4); }
.badge.role-member { color: var(--text-muted); }
.badge.status-blocked { color: var(--red); border-color: rgba(255,107,129,0.4); }

/* ------- ARTICLES ------- */
.articles { display: flex; flex-direction: column; gap: 8px; }
.article {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.article h3 { margin: 0 0 4px; font-size: 15px; }
.article h3 a { color: var(--text); }
.article h3 a:hover { color: var(--accent); }
.article .meta { color: var(--text-muted); font-size: 12px; display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 4px; }
.article .meta .seg { color: var(--accent); }
.article p { margin: 0; color: var(--text-muted); font-size: 13px; word-break: break-word; overflow-wrap: anywhere; }
.article h3 a { word-break: break-word; }

.pagination { display: flex; gap: 12px; align-items: center; justify-content: center; margin-top: 16px; }

/* ------- TRENDS ------- */
.trends-meta { margin-bottom: 12px; }
.period-toggle { display: inline-flex; border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 2px; gap: 2px; background: var(--bg-elev); }
.period-toggle .btn { border: 0; padding: 6px 12px; font-size: 13px; background: transparent; }
.period-toggle .btn.active { background: var(--bg-elev-2); color: var(--accent); }

.trends { display: flex; flex-direction: column; gap: 12px; }
.trend {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
}
.trend-head { display: flex; gap: 12px; align-items: baseline; flex-wrap: wrap; }
.trend-head h3 { margin: 0; font-size: 16px; }
.trend-scores { display: flex; gap: 10px; color: var(--text-muted); font-size: 12px; }
.trend-scores .hot { color: var(--yellow); }
.trend-scores .rel { color: var(--accent); }
.trend p { margin: 8px 0 6px; }
.trend .cats { color: var(--text-muted); font-size: 12px; margin-bottom: 6px; }
.trend .src-list { padding-left: 18px; margin: 0; }
.trend .src-list li { font-size: 13px; padding: 2px 0; word-break: break-word; }
.trend-head h3 { word-break: break-word; min-width: 0; }

/* ------- MODAL / TOAST ------- */
.modal {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-elev);
  color: var(--text);
  padding: 24px;
  max-width: min(520px, calc(100vw - 24px));
  width: calc(100vw - 24px);
  margin: auto;
  max-height: calc(100dvh - 24px);
  overflow-y: auto;
  box-sizing: border-box;
}
.table-wrap {
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.modal::backdrop { background: rgba(0,0,0,0.6); }
.modal h2 { margin: 0 0 12px; }

.toast {
  position: fixed; bottom: 24px; right: 24px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  font-size: 13px;
  box-shadow: var(--shadow);
  z-index: 10;
}
.toast.error { border-color: rgba(255,107,129,0.5); color: var(--red); }
.toast.ok { border-color: rgba(77,214,163,0.5); color: var(--green); }

/* ------- RESPONSIVE: APP LAYOUT ON MOBILE ------- */
@media (max-width: 900px) {
  .page-app {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }
  .sidebar {
    position: static;
    height: auto;
    flex-direction: column;
    gap: 10px;
    padding: 12px 14px;
    border-right: 0;
    border-bottom: 1px solid var(--border);
    align-items: stretch;
  }
  .sidebar .brand-mini { margin-right: 0; }
  .nav {
    flex: none;
    flex-direction: row;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    gap: 4px;
    padding-bottom: 2px;
    max-width: 100%;
  }
  .nav a { padding: 8px 12px; font-size: 13px; white-space: nowrap; flex-shrink: 0; }
  .me {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
    padding-top: 4px;
    border-top: 1px solid var(--border);
  }
  .me .btn { width: auto; margin-top: 0; padding: 6px 10px; margin-left: auto; }
  .me-name, .me-role { display: inline; }
  .content { padding: 16px 12px 48px; max-width: 100%; }
  .topbar {
    flex-direction: column;
    align-items: stretch;
    margin-bottom: 16px;
  }
  .topbar-actions {
    width: 100%;
    justify-content: flex-start;
  }
  .page-title { font-size: 20px; }
  .filters {
    flex-direction: column;
    align-items: stretch;
    width: 100%;
  }
  .filters select,
  .filters input[type="search"],
  .filters .btn {
    width: 100%;
    flex: none;
  }
  .period-toggle { flex-wrap: wrap; max-width: 100%; }
  .cards { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-2 { grid-template-columns: 1fr; }
  .table { font-size: 13px; min-width: 520px; }
  .table th, .table td { padding: 8px; }
  .table td.actions {
    white-space: normal;
  }
  .table td.actions .btn {
    display: inline-block;
    margin: 2px 0;
  }
  .modal { padding: 18px; width: calc(100vw - 20px); }
  .form { min-width: 0; width: 100%; }
  .form-actions { flex-wrap: wrap; }
  .form-actions .btn { flex: 1 1 calc(50% - 4px); min-width: 0; }
  .toast {
    left: 12px;
    right: 12px;
    bottom: 12px;
    max-width: none;
  }
}

@media (max-width: 400px) {
  .cards { grid-template-columns: 1fr; }
}
