:root {
  --bg: var(--tg-theme-bg-color, #f7f8fa);
  --surface: var(--tg-theme-secondary-bg-color, #ffffff);
  --surface-2: #eef1f4;
  --text: var(--tg-theme-text-color, #18212b);
  --muted: var(--tg-theme-hint-color, #6c7785);
  --accent: var(--tg-theme-button-color, #1f6d54);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --line: rgba(24, 33, 43, 0.12);
  --danger: #b63b3b;
  --warning: #9a6500;
  --success: #16784b;
  --sidebar: #17221f;
  --sidebar-text: #f4f7f6;
  --radius: 8px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: var(--tg-theme-bg-color, #111716);
    --surface: var(--tg-theme-secondary-bg-color, #19201f);
    --surface-2: #242c2a;
    --text: var(--tg-theme-text-color, #f2f5f4);
    --muted: var(--tg-theme-hint-color, #9ba7a3);
    --line: rgba(255, 255, 255, 0.12);
    --sidebar: #0c1211;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; background: var(--bg); color: var(--text); font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; }
button, input, select, textarea { font: inherit; letter-spacing: 0; }
button { cursor: pointer; }
.hidden { display: none !important; }

.auth-screen { min-height: 100vh; display: grid; place-content: center; justify-items: center; gap: 8px; padding: 24px; text-align: center; }
.auth-screen h1 { margin: 8px 0 0; font-size: 28px; }
.auth-screen p { margin: 0; color: var(--muted); }
.auth-mark, .brand-mark { display: grid; place-items: center; width: 44px; height: 44px; background: #b79045; color: #101614; font-weight: 850; border-radius: 6px; }

.app-shell { min-height: 100vh; display: grid; grid-template-columns: 248px minmax(0, 1fr); }
.sidebar { position: sticky; top: 0; height: 100vh; padding: 20px 14px; background: var(--sidebar); color: var(--sidebar-text); display: flex; flex-direction: column; }
.brand { display: flex; align-items: center; gap: 12px; padding: 0 8px 22px; }
.brand strong, .brand span { display: block; }
.brand strong { font-size: 16px; }
.brand span { margin-top: 2px; color: rgba(255,255,255,.58); font-size: 12px; }
.side-nav { display: grid; gap: 4px; }
.nav-item { position: relative; width: 100%; height: 44px; display: flex; align-items: center; gap: 11px; padding: 0 12px; border: 0; border-radius: 6px; background: transparent; color: rgba(255,255,255,.68); text-align: left; }
.nav-item svg { width: 19px; height: 19px; }
.nav-item.active, .nav-item:hover { background: rgba(255,255,255,.1); color: #fff; }
.nav-item .badge { margin-left: auto; min-width: 22px; height: 22px; padding: 0 6px; display: grid; place-items: center; border-radius: 11px; background: #d55a46; color: #fff; font-size: 11px; }
.admin-profile { margin-top: auto; padding: 14px 8px 2px; border-top: 1px solid rgba(255,255,255,.12); display: flex; align-items: center; gap: 10px; }
.admin-profile > span { width: 34px; height: 34px; display: grid; place-items: center; border-radius: 50%; background: #b79045; color: #111; font-weight: 800; }
.admin-profile strong, .admin-profile small { display: block; }
.admin-profile small { color: rgba(255,255,255,.55); margin-top: 2px; }

.workspace { min-width: 0; padding: 0 32px 48px; }
.topbar { min-height: 94px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--line); }
.topbar p, .topbar h1 { margin: 0; }
.topbar p { color: var(--muted); font-size: 12px; text-transform: uppercase; font-weight: 700; }
.topbar h1 { margin-top: 3px; font-size: 26px; }
.top-actions, .heading-actions, .dialog-actions { display: flex; align-items: center; gap: 8px; }
.view { display: none; padding-top: 26px; }
.view.active { display: block; }

.primary-button, .secondary-button, .text-button, .icon-button { border: 0; border-radius: 6px; min-height: 40px; display: inline-flex; align-items: center; justify-content: center; gap: 8px; }
.primary-button { padding: 0 15px; background: var(--accent); color: var(--accent-text); font-weight: 720; }
.secondary-button { padding: 0 14px; background: var(--surface); border: 1px solid var(--line); color: var(--text); font-weight: 650; }
.text-button { padding: 0 6px; background: transparent; color: var(--accent); font-weight: 700; }
.icon-button { width: 40px; background: var(--surface); border: 1px solid var(--line); color: var(--text); }
button svg { width: 18px; height: 18px; }
button:disabled { opacity: .5; cursor: wait; }

.stats-grid { display: grid; grid-template-columns: repeat(4, minmax(150px, 1fr)); gap: 12px; }
.stat-block { min-height: 112px; padding: 16px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); display: flex; flex-direction: column; justify-content: space-between; }
.stat-block span { color: var(--muted); font-size: 13px; }
.stat-block strong { font-size: 30px; line-height: 1; }
.stat-block small { color: var(--muted); }
.section-heading { margin: 30px 0 12px; display: flex; align-items: end; justify-content: space-between; gap: 16px; }
.section-heading h2, .section-heading p { margin: 0; }
.section-heading h2 { font-size: 18px; }
.section-heading p { margin-top: 4px; color: var(--muted); font-size: 13px; }

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; min-width: 720px; }
th, td { padding: 12px 14px; border-bottom: 1px solid var(--line); text-align: left; font-size: 13px; vertical-align: middle; }
th { color: var(--muted); font-size: 11px; text-transform: uppercase; font-weight: 750; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: color-mix(in srgb, var(--accent) 4%, transparent); }
tbody tr[data-user-id], .user-open { cursor: pointer; }
.empty-list { padding: 24px; color: var(--muted); text-align: center; }
.user-cell { display: flex; align-items: center; gap: 10px; }
.avatar { flex: 0 0 auto; width: 34px; height: 34px; display: grid; place-items: center; border-radius: 50%; background: var(--surface-2); font-weight: 800; }
.user-cell strong, .user-cell small { display: block; max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-cell small { color: var(--muted); margin-top: 2px; }
.status-pill { display: inline-flex; align-items: center; min-height: 24px; padding: 0 8px; border-radius: 12px; background: var(--surface-2); color: var(--muted); font-size: 11px; font-weight: 750; }
.status-pill.paid, .status-pill.reachable, .status-pill.answered { background: rgba(22,120,75,.13); color: var(--success); }
.status-pill.blocked, .status-pill.open { background: rgba(182,59,59,.13); color: var(--danger); }
.status-pill.pending { background: rgba(154,101,0,.14); color: var(--warning); }
.row-button { border: 0; background: transparent; color: inherit; width: 100%; text-align: left; padding: 0; }

.filters-row { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; }
.search-field { height: 42px; min-width: 220px; flex: 1; display: flex; align-items: center; gap: 8px; padding: 0 12px; background: var(--surface); border: 1px solid var(--line); border-radius: 6px; }
.search-field svg { width: 17px; color: var(--muted); }
.search-field input { min-width: 0; width: 100%; border: 0; outline: 0; background: transparent; color: var(--text); }
select, input, textarea { background: var(--surface); color: var(--text); border: 1px solid var(--line); border-radius: 6px; }
select, input { min-height: 42px; padding: 0 11px; }
textarea { width: 100%; padding: 10px 11px; resize: vertical; }
.segmented { display: flex; padding: 3px; background: var(--surface-2); border-radius: 7px; }
.segmented button { min-height: 34px; padding: 0 12px; border: 0; border-radius: 5px; background: transparent; color: var(--muted); }
.segmented button.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.08); }

.inbox-layout { height: calc(100vh - 178px); min-height: 520px; display: grid; grid-template-columns: minmax(280px, 36%) 1fr; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.conversation-list { overflow-y: auto; border-right: 1px solid var(--line); }
.conversation-item { width: 100%; padding: 14px; border: 0; border-bottom: 1px solid var(--line); background: transparent; color: var(--text); text-align: left; }
.conversation-item:hover, .conversation-item.active { background: color-mix(in srgb, var(--accent) 7%, transparent); }
.conversation-item header { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.conversation-item p { margin: 8px 0 0; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conversation-item time { color: var(--muted); font-size: 11px; }
.conversation-panel { min-width: 0; display: grid; grid-template-rows: auto 1fr auto; }
.conversation-panel.empty-state { place-content: center; justify-items: center; text-align: center; color: var(--muted); padding: 24px; }
.empty-state svg { width: 34px; height: 34px; }
.empty-state h3, .empty-state p { margin: 8px 0 0; }
.conversation-header { padding: 14px 18px; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.message-thread { overflow-y: auto; padding: 18px; display: flex; flex-direction: column; gap: 10px; }
.message-bubble { max-width: min(78%, 620px); padding: 10px 12px; border-radius: 8px; background: var(--surface-2); align-self: flex-start; white-space: pre-wrap; word-break: break-word; }
.message-bubble.outgoing { align-self: flex-end; background: color-mix(in srgb, var(--accent) 16%, var(--surface)); }
.message-bubble small { display: block; margin-top: 6px; color: var(--muted); font-size: 10px; }
.reply-composer { padding: 12px; border-top: 1px solid var(--line); display: grid; grid-template-columns: 1fr auto; gap: 8px; }
.reply-composer textarea { min-height: 44px; max-height: 130px; }

.pagination { margin-top: 12px; display: flex; align-items: center; justify-content: space-between; color: var(--muted); font-size: 13px; }
.pagination > div { display: flex; gap: 6px; }
.broadcast-list { display: grid; gap: 8px; }
.broadcast-row { padding: 14px 16px; display: grid; grid-template-columns: minmax(180px, 1fr) repeat(4, minmax(72px, auto)) auto; gap: 18px; align-items: center; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.broadcast-row h3, .broadcast-row p { margin: 0; }
.broadcast-row p, .broadcast-metric span { color: var(--muted); font-size: 12px; }
.broadcast-metric strong, .broadcast-metric span { display: block; }
.broadcast-metric strong { margin-bottom: 3px; }

.product-list { display: grid; gap: 8px; }
.product-row { padding: 14px 16px; display: grid; grid-template-columns: minmax(220px, 1fr) repeat(3, minmax(90px, auto)) auto; gap: 18px; align-items: center; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.product-row h3, .product-row p { margin: 0; }
.product-row p, .product-metric span { color: var(--muted); font-size: 12px; }
.product-row p { margin-top: 4px; }
.product-metric strong, .product-metric span { display: block; }
.product-metric strong { margin-bottom: 3px; }

.media-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; }
.media-item { min-height: 150px; padding: 14px; display: flex; flex-direction: column; justify-content: space-between; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.media-item svg { width: 28px; height: 28px; color: var(--accent); }
.media-item h3, .media-item p { margin: 0; }
.media-item h3 { margin-top: 18px; font-size: 14px; word-break: break-word; }
.media-item p { margin-top: 5px; color: var(--muted); font-size: 12px; }
.file-button input { display: none; }
.progress-line { height: 3px; margin-bottom: 12px; overflow: hidden; background: var(--surface-2); }
.progress-line span { display: block; width: 35%; height: 100%; background: var(--accent); animation: progress 1s infinite alternate ease-in-out; }
@keyframes progress { to { transform: translateX(185%); } }

.admin-list { display: grid; gap: 8px; }
.admin-row { padding: 13px 15px; display: flex; align-items: center; justify-content: space-between; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.admin-row strong, .admin-row span { display: block; }
.admin-row span { margin-top: 3px; color: var(--muted); font-size: 12px; }
.audit-heading { margin-top: 34px; }

dialog { padding: 0; color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: 0 24px 70px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(9,14,13,.58); backdrop-filter: blur(2px); }
.modal-dialog { width: min(680px, calc(100% - 24px)); max-height: calc(100vh - 24px); overflow: auto; }
.broadcast-dialog { width: min(820px, calc(100% - 24px)); }
.drawer-dialog { width: min(620px, 100%); height: 100%; max-height: none; margin: 0 0 0 auto; border-radius: 0; border-width: 0 0 0 1px; }
.dialog-header { padding: 18px 20px; border-bottom: 1px solid var(--line); display: flex; align-items: center; justify-content: space-between; }
.dialog-header p, .dialog-header h2 { margin: 0; }
.dialog-header p { color: var(--muted); font-size: 12px; }
.dialog-header h2 { margin-top: 3px; font-size: 20px; }
.form-grid { padding: 18px 20px; display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.field { display: grid; gap: 6px; }
.field.full { grid-column: 1 / -1; }
.field > span { color: var(--muted); font-size: 12px; font-weight: 650; }
.toggle-field { min-height: 42px; display: flex; align-items: center; gap: 9px; color: var(--text); }
.toggle-field input { min-height: 0; width: 18px; height: 18px; padding: 0; }
.audience-count-field strong { min-height: 42px; display: flex; align-items: center; font-size: 22px; }
.editor-toolbar { min-height: 38px; display: flex; align-items: center; gap: 3px; padding: 4px; background: var(--surface-2); border: 1px solid var(--line); border-bottom: 0; border-radius: 6px 6px 0 0; }
.editor-toolbar button { width: 30px; height: 28px; border: 0; background: transparent; color: var(--text); border-radius: 4px; }
.editor-toolbar button:hover { background: var(--surface); }
.rich-editor { min-height: 150px; padding: 11px; outline: 0; border: 1px solid var(--line); border-radius: 0 0 6px 6px; line-height: 1.5; }
.rich-editor:empty::before { content: attr(data-placeholder); color: var(--muted); }
.dialog-actions { padding: 14px 20px; border-top: 1px solid var(--line); justify-content: flex-end; flex-wrap: wrap; }
.form-error { margin: 0 20px 12px; padding: 10px 12px; background: rgba(182,59,59,.1); color: var(--danger); border-radius: 6px; }
.user-drawer { min-height: 100%; display: grid; grid-template-rows: auto auto 1fr auto; }
.user-summary { padding: 18px; border-bottom: 1px solid var(--line); }
.user-summary h2, .user-summary p { margin: 0; }
.user-summary p { color: var(--muted); margin-top: 5px; }
.user-facts { padding: 14px 18px; display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; border-bottom: 1px solid var(--line); }
.fact { min-height: 60px; padding: 10px; background: var(--surface-2); border-radius: 6px; }
.fact span, .fact strong { display: block; }
.fact span { color: var(--muted); font-size: 11px; }
.fact strong { margin-top: 5px; font-size: 13px; }
.drawer-messages { overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 8px; }
.drawer-actions { padding: 14px 18px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 8px; }
.toast-region { position: fixed; z-index: 100; right: 16px; bottom: 16px; display: grid; gap: 8px; }
.toast { max-width: 360px; padding: 12px 14px; border-radius: 6px; background: #16211e; color: #fff; box-shadow: 0 10px 30px rgba(0,0,0,.24); }
.toast.error { background: #7f2929; }
.bottom-nav { display: none; }

@media (max-width: 1020px) {
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .broadcast-row { grid-template-columns: minmax(160px, 1fr) repeat(2, auto); }
  .broadcast-row .broadcast-metric:nth-of-type(n+3) { display: none; }
  .product-row { grid-template-columns: minmax(160px, 1fr) repeat(2, auto); }
  .product-row .product-metric:nth-of-type(n+3) { display: none; }
}

@media (max-width: 760px) {
  .app-shell { display: block; padding-bottom: calc(68px + env(safe-area-inset-bottom)); }
  .sidebar { display: none; }
  .workspace { padding: 0 12px 24px; }
  .topbar { min-height: 76px; }
  .topbar h1 { font-size: 22px; }
  #new-broadcast-top span { display: none; }
  .view { padding-top: 16px; }
  .stats-grid { grid-template-columns: 1fr 1fr; gap: 8px; }
  .stat-block { min-height: 96px; padding: 13px; }
  .stat-block strong { font-size: 25px; }
  .section-heading { margin-top: 22px; align-items: center; }
  .filters-row, .users-filters { flex-wrap: wrap; }
  .search-field { min-width: 100%; }
  .users-filters select { flex: 1; min-width: 140px; }
  .inbox-layout { height: calc(100vh - 168px); display: block; min-height: 440px; }
  .conversation-list { height: 100%; border-right: 0; }
  .conversation-panel { display: none; position: fixed; z-index: 20; inset: 0 0 calc(66px + env(safe-area-inset-bottom)) 0; background: var(--surface); }
  .conversation-panel.mobile-open { display: grid; }
  .message-bubble { max-width: 88%; }
  .broadcast-row { grid-template-columns: 1fr auto; gap: 10px; }
  .broadcast-row .broadcast-metric { display: none; }
  .product-row { grid-template-columns: 1fr auto; gap: 10px; }
  .product-row .product-metric { display: none; }
  .media-grid { grid-template-columns: 1fr 1fr; }
  .heading-actions { flex-wrap: wrap; justify-content: flex-end; }
  .heading-actions .secondary-button span, .heading-actions .primary-button span { display: none; }
  .bottom-nav { position: fixed; z-index: 30; left: 0; right: 0; bottom: 0; height: calc(62px + env(safe-area-inset-bottom)); padding: 4px 5px env(safe-area-inset-bottom); display: grid; grid-template-columns: repeat(7, 1fr); background: var(--surface); border-top: 1px solid var(--line); }
  .bottom-nav button { border: 0; background: transparent; color: var(--muted); display: grid; justify-items: center; align-content: center; gap: 2px; font-size: 10px; }
  .bottom-nav button.active { color: var(--accent); }
  .bottom-nav svg { width: 20px; height: 20px; }
  .form-grid { grid-template-columns: 1fr; padding: 14px; }
  .field.full { grid-column: 1; }
  .dialog-actions { padding: 12px 14px; }
  .dialog-actions button { flex: 1; }
  .drawer-dialog { width: 100%; }
  .toast-region { left: 12px; right: 12px; bottom: calc(74px + env(safe-area-inset-bottom)); }
  .toast { max-width: none; }
}

@media (max-width: 430px) {
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .media-grid { grid-template-columns: 1fr; }
  .segmented { width: 100%; }
  .segmented button { flex: 1; }
}
