:root, .theme-dark {
  --bg: #0f151d;
  --panel: #161f2a;
  --panel-alt: #1c2733;
  --border: #263241;
  --text: #e9eef3;
  --text-muted: #8fa0b0;
  --accent: #f2a541;
  --accent-soft: rgba(242,165,65,0.15);
  --teal: #3ec9a7;
  --danger: #e5484d;
  --owner: #3ea6ff;
  --radius-lg: 14px;
  --radius-md: 10px;
  --radius-sm: 8px;
  --font: 'Vazirmatn', Tahoma, sans-serif;
}

.theme-light {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --panel-alt: #eef1f5;
  --border: #d8dee6;
  --text: #1a2330;
  --text-muted: #6b7a8d;
  --accent: #e09a2e;
  --accent-soft: rgba(224,154,46,0.18);
  --teal: #1a9e7a;
  --danger: #d32f2f;
  --owner: #1976d2;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
.hidden { display: none !important; }
button { font-family: inherit; cursor: pointer; }
input, textarea, select {
  font-family: inherit;
  background: var(--panel-alt);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 11px 12px;
  font-size: 15px;
  width: 100%;
}
input:focus, textarea:focus, select:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
label { display: block; font-size: 13px; color: var(--text-muted); margin-bottom: 12px; }
label input, label textarea, label select { margin-top: 6px; }
hr { border: none; border-top: 1px solid var(--border); margin: 16px 0; }

.btn-primary {
  background: linear-gradient(135deg, var(--accent), #d98a2b);
  color: #1a1200;
  border: none;
  font-weight: 700;
  padding: 11px 18px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  width: 100%;
}
.btn-primary:hover { filter: brightness(1.08); }
.btn-secondary {
  background: var(--panel-alt);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 9px 14px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 13px;
}
.icon-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 18px;
  padding: 6px 8px;
  border-radius: var(--radius-sm);
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--panel-alt); color: var(--text); }

/* Auth */
.auth-screen {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background:
    radial-gradient(400px 300px at 20% 10%, rgba(242,165,65,0.1), transparent),
    radial-gradient(400px 300px at 80% 90%, rgba(62,201,167,0.08), transparent),
    var(--bg);
}
.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 24px;
}
.auth-brand { display: flex; align-items: center; gap: 12px; margin-bottom: 22px; }
.brand-mark {
  width: 42px; height: 42px; border-radius: 12px;
  background: linear-gradient(135deg, var(--accent), var(--teal));
  color: #10151b; font-weight: 700; font-size: 20px;
  display: flex; align-items: center; justify-content: center;
}
.brand-mark.small { width: 30px; height: 30px; font-size: 14px; border-radius: 9px; }
.brand-mark-img { width: 30px; height: 30px; border-radius: 9px; object-fit: cover; }
.brand-name { font-weight: 700; font-size: 18px; }
.brand-name.small { font-size: 14px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.auth-tabs { display: flex; gap: 4px; margin-bottom: 18px; background: var(--panel-alt); border-radius: var(--radius-sm); padding: 4px; }
.auth-tab { flex: 1; padding: 10px; border: none; background: transparent; color: var(--text-muted); font-weight: 600; font-size: 14px; border-radius: var(--radius-sm); }
.auth-tab.active { background: var(--accent); color: #1a1200; }
.auth-form { display: flex; flex-direction: column; }
.auth-error, .modal-error { color: var(--danger); font-size: 13px; min-height: 18px; margin: -4px 0 10px; }

/* App layout - mobile first */
.app { display: flex; height: 100vh; height: 100dvh; position: relative; }
.sidebar {
  position: fixed; inset: 0; z-index: 40;
  width: 100%; max-width: 100%;
  background: var(--panel);
  display: flex; flex-direction: column;
  overflow: hidden;
  transform: translateX(0);
  transition: transform 0.25s ease;
}
.sidebar.closed { transform: translateX(100%); }
.sidebar-header { display: flex; align-items: center; gap: 8px; padding: 12px 14px; border-bottom: 1px solid var(--border); }
.sidebar-section { padding: 10px 10px 4px; overflow-y: auto; flex-shrink: 1; }
.sidebar-section-title {
  display: flex; justify-content: space-between; align-items: center;
  font-size: 11px; letter-spacing: 0.04em; text-transform: uppercase;
  color: var(--text-muted); font-weight: 700; padding: 0 6px 6px;
}
.list { display: flex; flex-direction: column; gap: 2px; max-height: 160px; overflow-y: auto; }
.list-item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 8px; border-radius: var(--radius-sm);
  color: var(--text); font-size: 14px; cursor: pointer;
}
.list-item:hover, .list-item:active { background: var(--panel-alt); }
.list-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.list-item .hash { color: var(--text-muted); }

.avatar {
  width: 34px; height: 34px; border-radius: 50%;
  background: var(--panel-alt); border: 1px solid var(--border);
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 700; color: var(--teal);
  background-size: cover; background-position: center;
  flex-shrink: 0; position: relative;
}
.online-list .avatar::after {
  content: ''; position: absolute; bottom: -1px; left: -1px;
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--teal); border: 2px solid var(--panel);
}

.sidebar-footer {
  margin-top: auto; display: flex; align-items: center; gap: 10px;
  padding: 12px 14px; border-top: 1px solid var(--border);
}
.me-info { flex: 1; min-width: 0; }
.me-name { font-size: 13.5px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.me-role { font-size: 11.5px; color: var(--text-muted); }

.chat {
  flex: 1; display: flex; flex-direction: column; min-width: 0;
  width: 100%; background: var(--bg);
}
.chat-header {
  display: flex; align-items: center; gap: 8px;
  padding: 12px 14px; border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.chat-title { font-weight: 700; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-sub { font-size: 12px; color: var(--text-muted); margin-top: 1px; }
.typing-indicator { font-size: 12px; color: var(--accent); height: 16px; }

.pinned-bar {
  background: var(--accent-soft); border-bottom: 1px solid var(--border);
  padding: 8px 14px; font-size: 13px; color: var(--accent);
}

.messages {
  flex: 1; overflow-y: auto; padding: 14px;
  display: flex; flex-direction: column; gap: 12px;
  -webkit-overflow-scrolling: touch;
}
.msg-row { display: flex; gap: 8px; max-width: 92%; }
.msg-row.own { align-self: flex-end; flex-direction: row-reverse; }
.msg-body { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.msg-meta { display: flex; align-items: center; gap: 5px; font-size: 12px; flex-wrap: wrap; }
.msg-name { font-weight: 700; }
.msg-title-badge {
  font-size: 10px; background: var(--panel-alt); border: 1px solid var(--border);
  padding: 1px 6px; border-radius: 999px; color: var(--teal); font-weight: 600;
}
.owner-badge {
  font-size: 10px; background: rgba(62,166,255,0.15); border: 1px solid rgba(62,166,255,0.4);
  padding: 1px 6px; border-radius: 999px; color: var(--owner); font-weight: 700;
}
.tick-blue { color: #3ea6ff; font-size: 12px; }
.msg-time { color: var(--text-muted); font-size: 11px; }
.msg-bubble {
  background: var(--panel-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 8px 12px;
  font-size: 14.5px;
  line-height: 1.5;
  word-wrap: break-word;
  white-space: pre-wrap;
}
.msg-row.own .msg-bubble { background: var(--accent-soft); border-color: rgba(242,165,65,0.35); }
.msg-row.manager .msg-bubble {
  border-right: 3px solid var(--danger);
  box-shadow: 0 0 0 1px rgba(229,72,77,0.12);
}
.msg-bubble img { max-width: 100%; border-radius: var(--radius-sm); display: block; cursor: pointer; }
.msg-file {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 0; font-size: 13.5px;
}
.msg-file a { color: var(--accent); text-decoration: none; font-weight: 600; }
.msg-actions { display: flex; gap: 8px; margin-top: 2px; }
.msg-actions button, .msg-report-link {
  font-size: 11px; color: var(--text-muted); background: none; border: none; padding: 0; cursor: pointer;
}
.msg-actions button:hover, .msg-report-link:hover { color: var(--danger); }

.message-form {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px; border-top: 1px solid var(--border);
  background: var(--panel);
}
.message-form input[type=text] { flex: 1; }
.message-form .btn-primary { width: auto; padding: 10px 16px; }

/* Modal */
.modal {
  position: fixed; inset: 0; background: rgba(6,9,13,0.65);
  display: flex; align-items: flex-end; justify-content: center; z-index: 60;
  padding: 0;
}
.modal-card {
  width: 100%; max-height: 90vh; overflow-y: auto;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 16px 16px 0 0;
}
.modal-card.wide { width: 100%; }
.modal-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 16px; border-bottom: 1px solid var(--border);
  font-weight: 700; font-size: 15px; position: sticky; top: 0; background: var(--panel); z-index: 1;
}
.modal-body { padding: 16px; }
.modal-tabs {
  display: flex; gap: 2px; padding: 8px 12px 0; border-bottom: 1px solid var(--border);
  overflow-x: auto; -webkit-overflow-scrolling: touch;
}
.modal-tab {
  border: none; background: transparent; color: var(--text-muted);
  padding: 8px 10px; font-weight: 600; font-size: 12.5px; border-bottom: 2px solid transparent;
  white-space: nowrap;
}
.modal-tab.active { color: var(--accent); border-bottom-color: var(--accent); }
.settings-panel h4 { margin: 0 0 10px; }
.rules-text { white-space: pre-wrap; line-height: 1.7; font-size: 14px; }
.checkbox-row { display: flex; align-items: center; gap: 8px; }
.checkbox-row input { width: auto; }
.perm-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 8px; margin: 10px 0 14px; }

.data-table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.data-table th, .data-table td { text-align: right; padding: 8px 4px; border-bottom: 1px solid var(--border); }
.data-table th { color: var(--text-muted); font-weight: 600; font-size: 11px; }
.pill { font-size: 10px; padding: 2px 7px; border-radius: 999px; font-weight: 700; }
.pill.super_admin { background: rgba(229,72,77,0.18); color: var(--danger); }
.pill.admin { background: var(--accent-soft); color: var(--accent); }
.pill.user { background: var(--panel-alt); color: var(--text-muted); }
.row-actions { display: flex; gap: 4px; flex-wrap: wrap; }
.row-actions button { font-size: 11px; padding: 4px 8px; }

.img-viewer {
  position: fixed; inset: 0; z-index: 80; background: rgba(0,0,0,0.92);
  display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 16px;
}
.img-viewer img { max-width: 100%; max-height: 80vh; object-fit: contain; border-radius: 8px; }
.img-viewer #img-viewer-close { position: absolute; top: 12px; left: 12px; color: #fff; font-size: 22px; }
.img-viewer a { margin-top: 12px; }

.mobile-only { display: inline-flex; }

@media (min-width: 768px) {
  .sidebar {
    position: relative; width: 300px; max-width: 300px;
    transform: none !important; border-left: 1px solid var(--border);
  }
  .sidebar.closed { transform: none !important; }
  .mobile-only { display: none; }
  .modal { align-items: center; padding: 20px; }
  .modal-card { width: 420px; border-radius: var(--radius-lg); max-height: 86vh; }
  .modal-card.wide { width: 640px; }
}

.unread-badge {
  background: var(--danger); color: #fff; font-size: 10px; font-weight: 700;
  min-width: 18px; height: 18px; border-radius: 999px;
  display: inline-flex; align-items: center; justify-content: center;
  padding: 0 5px; margin-right: auto;
}
.msg-failed { color: var(--danger); font-size: 11px; margin-top: 2px; }
.msg-read { color: var(--teal); font-size: 11px; }
.msg-reply-preview {
  border-right: 3px solid var(--accent); padding: 4px 8px; margin-bottom: 4px;
  background: var(--panel); border-radius: 6px; font-size: 12px; color: var(--text-muted);
  cursor: pointer;
}
.channel-link { color: var(--accent); cursor: pointer; font-weight: 600; }
.msg-caption { font-size: 13px; margin-top: 4px; color: var(--text); }
.no-send-placeholder {
  flex: 1; text-align: center; color: var(--text-muted); font-size: 13px; padding: 10px;
}

.ad-banner {
  background: linear-gradient(90deg, rgba(242,165,65,0.14), rgba(62,201,167,0.1));
  border-bottom: 1px solid var(--border);
  padding: 8px 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  max-height: 64px;
  overflow: hidden;
  flex-shrink: 0;
}
.ad-banner.hidden { display: none !important; }
.ad-banner-img { max-height: 44px; max-width: 100px; border-radius: 8px; object-fit: cover; }
.ad-banner-text { font-size: 13px; font-weight: 600; color: var(--text); }
.ad-banner-link { display: flex; align-items: center; gap: 10px; text-decoration: none; color: inherit; justify-content: center; width: 100%; }
.delete-warning-box {
  background: rgba(229,72,77,0.08);
  border: 1px solid rgba(229,72,77,0.35);
  border-radius: var(--radius-md);
  padding: 14px;
  margin-bottom: 12px;
}
.delete-warning-list {
  margin: 0;
  padding-right: 18px;
  font-size: 13px;
  line-height: 1.7;
  color: var(--text);
}

.member-count {
  margin-right: auto;
  font-size: 10px;
  color: var(--text-muted);
  background: var(--panel-alt);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 1px 7px;
}
.edit-msg-card {
  border: 1px solid var(--accent);
  box-shadow: 0 12px 40px rgba(0,0,0,0.25);
}
.edit-msg-card textarea {
  min-height: 100px;
  resize: vertical;
  line-height: 1.5;
}
