/* ==========================================================================
   TG Panel — dark theme
   ========================================================================== */
:root {
  --bg:            #0d0f12;
  --bg-elevated:   #15181d;
  --bg-input:      #1c2027;
  --border:        #262b33;
  --text:          #e6e8eb;
  --text-muted:    #8b929c;
  --accent:        #2f9fe3;
  --success:       #22c55e;
  --danger:        #ef4444;
  --warning:       #f59e0b;
  --info:          #38bdf8;
  --radius:        10px;
  --topbar-h:      58px;
  --sidebar-w:     236px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
}

/* ------------------------------ Topbar ---------------------------------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 1040;
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: .5rem;
  padding: 0 1rem;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: .5rem;
  font-weight: 700;
  font-size: 1.15rem;
  color: var(--accent);
  text-decoration: none;
}
.brand:hover { color: var(--accent); opacity: .85; }

.user-chip {
  display: inline-flex;
  align-items: center;
  gap: .45rem;
  padding: .3rem .7rem;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: .85rem;
  color: var(--text-muted);
}

/* ------------------------------ Layout ---------------------------------- */
.layout { display: flex; min-height: calc(100vh - var(--topbar-h)); }

.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--bg-elevated);
  border-right: 1px solid var(--border);
  padding: 1rem 0;
  position: sticky;
  top: var(--topbar-h);
  height: calc(100vh - var(--topbar-h));
  overflow-y: auto;
}

.sidebar a {
  display: flex;
  align-items: center;
  gap: .65rem;
  padding: .6rem 1.1rem;
  color: var(--text-muted);
  text-decoration: none;
  border-left: 3px solid transparent;
  font-size: .93rem;
  transition: background .15s, color .15s, border-color .15s;
}
.sidebar a:hover { background: rgba(255,255,255,.04); color: var(--text); }
.sidebar a.active {
  background: rgba(47,159,227,.12);
  color: var(--accent);
  border-left-color: var(--accent);
  font-weight: 500;
}
.sidebar a i { width: 18px; text-align: center; }

.nav-section {
  padding: 1rem 1.1rem .4rem;
  font-size: .7rem;
  font-weight: 700;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: #5a616c;
}

.sidebar-backdrop { display: none; }

.content { flex: 1; padding: 1.6rem; max-width: 100%; overflow-x: hidden; }
.content-full { padding: 0; }

.page-header h1 { font-size: 1.6rem; font-weight: 700; margin: 0 0 .25rem; }
.page-header p  { color: var(--text-muted); font-size: .92rem; }

/* ------------------------------ Cards ----------------------------------- */
.card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
}
.card-header { background: rgba(255,255,255,.02); border-bottom: 1px solid var(--border); }

.stat-card { text-align: center; padding: 1.25rem .75rem; }
.stat-card .stat-icon { font-size: 1.6rem; opacity: .85; }
.stat-card .stat-value { font-size: 1.9rem; font-weight: 700; line-height: 1.15; margin: .4rem 0 .1rem; }
.stat-card .stat-label { color: var(--text-muted); font-size: .8rem; text-transform: uppercase; letter-spacing: .05em; }

/* ------------------------------ Forms ----------------------------------- */
.form-control, .form-select, textarea.form-control {
  background: var(--bg-input);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: .6rem .8rem;
  font-size: .95rem;
}
.form-control:focus, .form-select:focus {
  background: var(--bg-input);
  border-color: var(--accent);
  color: var(--text);
  box-shadow: 0 0 0 .18rem rgba(47,159,227,.2);
}
.form-control::placeholder { color: #5c636e; }
.form-control:disabled, .form-select:disabled { background: #14171c; opacity: .6; }
.form-label { font-size: .87rem; font-weight: 500; color: var(--text-muted); margin-bottom: .35rem; }

.char-count { font-size: .78rem; color: var(--text-muted); font-variant-numeric: tabular-nums; }
.char-count.warn   { color: var(--warning); }
.char-count.danger { color: var(--danger); }

/* --------------------------- Buttons & badges --------------------------- */
.btn { border-radius: 8px; font-weight: 500; }
.btn-send {
  background: var(--success);
  border-color: var(--success);
  color: #06210f;
  font-weight: 700;
  padding: .65rem 1rem;
}
.btn-send:hover:not(:disabled) { background: #1eb055; border-color: #1eb055; color: #06210f; }
.btn-send:disabled { opacity: .6; cursor: not-allowed; }

.badge { font-weight: 600; padding: .35em .65em; border-radius: 6px; font-size: .75rem; }
.badge-success { background: rgba(34,197,94,.16);  color: #4ade80; }
.badge-danger  { background: rgba(239,68,68,.16);  color: #f87171; }
.badge-warning { background: rgba(245,158,11,.16); color: #fbbf24; }
.badge-info    { background: rgba(56,189,248,.16); color: #7dd3fc; }
.badge-muted   { background: rgba(255,255,255,.07); color: var(--text-muted); }

/* ------------------------------ Tables ---------------------------------- */
.table { color: var(--text); --bs-table-bg: transparent; margin-bottom: 0; }
.table > :not(caption) > * > * { border-bottom-color: var(--border); padding: .7rem .6rem; }
.table thead th {
  color: var(--text-muted);
  font-size: .74rem;
  text-transform: uppercase;
  letter-spacing: .06em;
  font-weight: 700;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.table-hover > tbody > tr:hover > * { background: rgba(255,255,255,.035); color: var(--text); }
.table-responsive { border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg-elevated); }

/* --------------------------- Message preview ---------------------------- */
.message-preview {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: 8px;
  padding: .75rem .9rem;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: .87rem;
  line-height: 1.55;
  min-height: 78px;
  max-height: 260px;
  overflow-y: auto;
}
.message-preview .placeholder-text { color: #5c636e; font-style: italic; }
.message-preview strong { color: #fff; }

.note-body {
  white-space: pre-wrap;
  word-break: break-word;
  font-size: .9rem;
  color: var(--text-muted);
  max-height: 160px;
  overflow-y: auto;
}

/* ------------------------------ Modals ---------------------------------- */
.modal-content { background: var(--bg-elevated); border: 1px solid var(--border); border-radius: var(--radius); }
.modal-header, .modal-footer { border-color: var(--border); }
.btn-close { filter: invert(1) grayscale(1) brightness(1.8); }

/* ------------------------------ Alerts ---------------------------------- */
.alert { border-radius: var(--radius); border-width: 1px; }
.alert-success { background: rgba(34,197,94,.1);  border-color: rgba(34,197,94,.3);  color: #4ade80; }
.alert-danger  { background: rgba(239,68,68,.1);  border-color: rgba(239,68,68,.3);  color: #f87171; }
.alert-warning { background: rgba(245,158,11,.1); border-color: rgba(245,158,11,.3); color: #fbbf24; }
.alert-info    { background: rgba(56,189,248,.1); border-color: rgba(56,189,248,.3); color: #7dd3fc; }

/* ------------------------------ Toasts ---------------------------------- */
.toast-container {
  position: fixed;
  bottom: 1rem;
  right: 1rem;
  z-index: 1090;
  display: flex;
  flex-direction: column;
  gap: .5rem;
  max-width: min(360px, calc(100vw - 2rem));
}
.tg-toast {
  display: flex;
  align-items: flex-start;
  gap: .6rem;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  border-radius: 8px;
  padding: .75rem .9rem;
  box-shadow: 0 8px 24px rgba(0,0,0,.5);
  animation: toast-in .22s ease-out;
  font-size: .9rem;
}
.tg-toast.success { border-left-color: var(--success); }
.tg-toast.danger  { border-left-color: var(--danger); }
.tg-toast.warning { border-left-color: var(--warning); }
.tg-toast.hiding  { animation: toast-out .2s ease-in forwards; }

@keyframes toast-in  { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: none; } }
@keyframes toast-out { to { opacity: 0; transform: translateY(12px); } }

/* ---------------------------- Pagination -------------------------------- */
.pagination .page-link {
  background: var(--bg-elevated);
  border-color: var(--border);
  color: var(--text-muted);
}
.pagination .page-link:hover { background: var(--bg-input); color: var(--text); }
.pagination .page-item.active .page-link { background: var(--accent); border-color: var(--accent); color: #041a26; }
.pagination .page-item.disabled .page-link { background: var(--bg-elevated); color: #4a505a; }

/* ------------------------------ Misc ------------------------------------ */
code { color: #7dd3fc; background: rgba(56,189,248,.09); padding: .1rem .35rem; border-radius: 4px; }
hr { border-color: var(--border); }
.group-icon { width: 38px; height: 38px; border-radius: 9px; object-fit: cover; flex-shrink: 0; }
.group-icon-fallback {
  width: 38px; height: 38px; border-radius: 9px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  background: rgba(47,159,227,.14); color: var(--accent); font-size: 1.1rem;
}
::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: #333a44; border-radius: 5px; }
::-webkit-scrollbar-thumb:hover { background: #414a56; }

/* ---------------------------- Login page -------------------------------- */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.25rem;
  background: radial-gradient(circle at 50% 0%, rgba(47,159,227,.16), transparent 62%), var(--bg);
}
.login-card {
  width: 100%;
  max-width: 400px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 2.1rem 1.8rem;
  box-shadow: 0 22px 60px rgba(0,0,0,.55);
}

/* ---------------------------- Responsive -------------------------------- */
@media (max-width: 991.98px) {
  .sidebar {
    position: fixed;
    top: var(--topbar-h);
    left: 0;
    z-index: 1035;
    transform: translateX(-100%);
    transition: transform .22s ease;
    box-shadow: 4px 0 22px rgba(0,0,0,.5);
  }
  .sidebar.open { transform: translateX(0); }

  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: var(--topbar-h) 0 0 0;
    background: rgba(0,0,0,.55);
    z-index: 1030;
    opacity: 0;
    pointer-events: none;
    transition: opacity .22s ease;
  }
  .sidebar-backdrop.show { opacity: 1; pointer-events: auto; }

  .content { padding: 1rem; }
  .page-header h1 { font-size: 1.3rem; }
}

@media (max-width: 575.98px) {
  body { font-size: 14px; }
  .content { padding: .85rem; }
  .table > :not(caption) > * > * { padding: .55rem .45rem; }
  .toast-container { left: 1rem; right: 1rem; bottom: .75rem; max-width: none; }
  .btn-send { padding: .8rem 1rem; font-size: 1rem; }
  .form-control, .form-select { font-size: 16px; } /* prevents iOS zoom on focus */
}
