:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #1a1a1a);
  --hint: var(--tg-theme-hint-color, #999999);
  --link: var(--tg-theme-link-color, #2678b6);
  --btn: var(--tg-theme-button-color, #2678b6);
  --btn-text: var(--tg-theme-button-text-color, #ffffff);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #f0f0f0);
  --section-bg: var(--tg-theme-section-bg-color, #ffffff);
  --section-header: var(--tg-theme-section-header-text-color, #6d6d72);
  --accent: var(--tg-theme-accent-text-color, var(--link));
  --destructive: var(--tg-theme-destructive-text-color, #e53935);
  --radius: 12px;
  --nav-height: 56px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, sans-serif;
  background: var(--secondary-bg);
  color: var(--text);
  padding-bottom: calc(var(--nav-height) + env(safe-area-inset-bottom, 0px) + 16px);
  -webkit-text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
}

#app {
  padding: 12px;
  min-height: 80vh;
}

/* === Navigation === */
#nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: calc(var(--nav-height) + env(safe-area-inset-bottom, 0px));
  background: var(--section-bg);
  display: flex;
  border-top: 1px solid var(--secondary-bg);
  z-index: 100;
  padding-bottom: env(safe-area-inset-bottom, 0);
}

.nav-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border: none;
  background: none;
  color: var(--hint);
  font-size: 10px;
  cursor: pointer;
  transition: color 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.nav-btn.active { color: var(--accent); }
.nav-icon { font-size: 18px; }
.nav-label { font-weight: 500; }

/* === Cards === */
.card {
  background: var(--section-bg);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 10px;
}

.card-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--section-header);
  text-transform: uppercase;
  letter-spacing: 0.4px;
  margin-bottom: 10px;
}

.card-subtitle {
  font-size: 12px;
  color: var(--hint);
  margin-top: -6px;
  margin-bottom: 10px;
}

/* === Stat grid === */
.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.stat-item {
  background: var(--section-bg);
  border-radius: var(--radius);
  padding: 14px;
  text-align: center;
}

.stat-value {
  font-size: 28px;
  font-weight: 700;
  color: var(--text);
  line-height: 1.1;
}

.stat-label {
  font-size: 12px;
  color: var(--hint);
  margin-top: 4px;
}

.stat-item.accent .stat-value { color: var(--accent); }
.stat-item.danger .stat-value { color: var(--destructive); }

/* === Tables === */
.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

th {
  text-align: left;
  font-weight: 600;
  color: var(--section-header);
  padding: 8px 10px;
  border-bottom: 1px solid var(--secondary-bg);
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
}

th.sorted::after {
  content: " ↓";
  font-size: 11px;
}

th.sorted.asc::after {
  content: " ↑";
}

td {
  padding: 10px;
  border-bottom: 1px solid var(--secondary-bg);
  vertical-align: middle;
}

tr:last-child td { border-bottom: none; }

/* === Search === */
.search-bar {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.search-input {
  flex: 1;
  padding: 10px 14px;
  border-radius: var(--radius);
  border: none;
  background: var(--section-bg);
  color: var(--text);
  font-size: 14px;
  outline: none;
}

.search-input::placeholder { color: var(--hint); }
.search-input:focus {
  box-shadow: 0 0 0 2px var(--accent);
}

/* === Buttons === */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 18px;
  border-radius: var(--radius);
  border: none;
  background: var(--btn);
  color: var(--btn-text);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: opacity 0.15s, transform 0.1s;
  -webkit-tap-highlight-color: transparent;
  text-decoration: none;
}

.btn:active { opacity: 0.7; transform: scale(0.97); }
.btn:disabled {
  opacity: 0.4;
  pointer-events: none;
}
.btn-outline {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--accent);
}
.btn-sm { padding: 6px 12px; font-size: 12px; }
.btn-block { width: 100%; }
.btn-danger {
  background: var(--destructive);
  color: #fff;
}

/* === Filter chips === */
.chips {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 4px;
  margin-bottom: 12px;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.chips::-webkit-scrollbar { display: none; }

.chip {
  padding: 6px 14px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  border: 1px solid var(--hint);
  color: var(--hint);
  background: none;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: all 0.15s;
}

.chip.active {
  background: var(--accent);
  color: var(--btn-text);
  border-color: var(--accent);
}

/* === Feedback list === */
.fb-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--secondary-bg);
  cursor: pointer;
  transition: opacity 0.15s;
}

.fb-item:active { opacity: 0.6; }
.fb-item:last-child { border-bottom: none; }

.fb-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--secondary-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
}

.fb-info { flex: 1; min-width: 0; }

.fb-name {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.fb-preview {
  font-size: 12px;
  color: var(--hint);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.fb-meta {
  font-size: 12px;
  color: var(--hint);
  margin-top: 2px;
}

.fb-badge {
  padding: 3px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
  flex-shrink: 0;
}

.fb-badge.new { background: #ffebee; color: var(--destructive); }
.fb-badge.replied { background: #e8f5e9; color: #2e7d32; }

/* === Chat === */
.chat {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 60vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 4px 0;
}

.chat-msg {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.4;
  word-wrap: break-word;
}

.chat-msg.user {
  align-self: flex-start;
  background: var(--section-bg);
  border-bottom-left-radius: 4px;
}

.chat-msg.admin {
  align-self: flex-end;
  background: var(--btn);
  color: var(--btn-text);
  border-bottom-right-radius: 4px;
}

.chat-time {
  font-size: 11px;
  color: var(--hint);
  margin-top: 4px;
}

.chat-type {
  font-size: 10px;
  color: var(--accent);
  font-weight: 600;
  text-transform: uppercase;
  margin-bottom: 4px;
}

.chat-msg.admin .chat-time { color: rgba(255,255,255,0.7); }

.chat-img {
  max-width: 220px;
  max-height: 180px;
  border-radius: 8px;
  margin-top: 6px;
  cursor: pointer;
  display: block;
  object-fit: cover;
}
.chat-doc {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 6px;
  padding: 6px 10px;
  border-radius: 8px;
  background: rgba(0,0,0,0.06);
  font-size: 13px;
  color: var(--accent);
  text-decoration: none;
}
.chat-msg.admin .chat-doc {
  background: rgba(255,255,255,0.15);
  color: var(--btn-text);
}
.chat-img-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0,0,0,0.92);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 99999;
  cursor: pointer;
  animation: fadeIn 0.15s ease;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.chat-img-overlay img {
  max-width: 94vw;
  max-height: 88vh;
  border-radius: 8px;
  object-fit: contain;
  animation: zoomIn 0.2s ease;
  user-select: none;
  -webkit-user-drag: none;
}
@keyframes zoomIn { from { transform: scale(0.85); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.chat-img-overlay .img-close-hint {
  position: absolute;
  top: 12px;
  right: 16px;
  font-size: 28px;
  color: rgba(255,255,255,0.7);
  pointer-events: none;
}

.reply-bar {
  display: flex;
  gap: 8px;
  padding: 12px;
  background: var(--section-bg);
  border-radius: var(--radius);
  margin-top: 12px;
  position: sticky;
  bottom: 0;
}

.reply-input {
  flex: 1;
  border: none;
  background: var(--secondary-bg);
  border-radius: 20px;
  padding: 10px 14px;
  font-size: 14px;
  color: var(--text);
  outline: none;
  resize: none;
  max-height: 100px;
  font-family: inherit;
}

.reply-input:focus {
  box-shadow: 0 0 0 2px var(--accent);
}

.send-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: var(--btn);
  color: var(--btn-text);
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: opacity 0.15s, transform 0.1s;
}

.send-btn:active { transform: scale(0.9); }
.send-btn:disabled { opacity: 0.4; }

.attach-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: transparent;
  font-size: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  opacity: 0.6;
  transition: opacity 0.15s;
}
.attach-btn:hover { opacity: 1; }
.attach-btn:disabled { opacity: 0.3; }

.attach-preview {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--section-bg);
  border-radius: var(--radius);
  margin-top: 4px;
}
.attach-thumb {
  width: 60px;
  height: 60px;
  object-fit: cover;
  border-radius: 8px;
}
.attach-remove {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: none;
  background: var(--danger, #e53935);
  color: #fff;
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

/* === Chart === */
.chart-container {
  position: relative;
  height: 200px;
  margin-top: 8px;
}

/* === Pagination === */
.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-top: 12px;
}

.page-info { font-size: 13px; color: var(--hint); }

/* === Loading === */
.loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px;
  gap: 12px;
  color: var(--hint);
}

.spinner {
  width: 28px;
  height: 28px;
  border: 3px solid var(--secondary-bg);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.loading-text {
  font-size: 13px;
  color: var(--hint);
}

/* === Skeleton === */
.skeleton {
  background: linear-gradient(90deg, var(--secondary-bg) 25%, var(--section-bg) 50%, var(--secondary-bg) 75%);
  background-size: 200% 100%;
  animation: skeleton-pulse 1.5s infinite;
  border-radius: 6px;
}

@keyframes skeleton-pulse {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* === Back button header === */
.page-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
}

.back-btn {
  border: none;
  background: none;
  font-size: 20px;
  cursor: pointer;
  color: var(--accent);
  padding: 4px;
  -webkit-tap-highlight-color: transparent;
}

.page-title {
  font-size: 17px;
  font-weight: 600;
}

/* === Export section === */
.export-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 10px;
}

.export-item {
  background: var(--section-bg);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 8px;
  cursor: pointer;
  transition: opacity 0.15s;
}

.export-item:active { opacity: 0.6; }

.export-icon {
  width: 44px;
  height: 44px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  flex-shrink: 0;
}

.export-icon.blue { background: rgba(38, 120, 182, 0.12); }
.export-icon.green { background: rgba(46, 125, 50, 0.12); }
.export-icon.orange { background: rgba(239, 108, 0, 0.12); }
.export-icon.purple { background: rgba(142, 36, 170, 0.12); }

.export-info { flex: 1; }

.export-name {
  font-size: 15px;
  font-weight: 600;
}

.export-desc {
  font-size: 12px;
  color: var(--hint);
  margin-top: 2px;
}

.export-arrow {
  color: var(--hint);
  font-size: 16px;
}

/* === Poll options === */
.poll-option {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
}

.poll-bar-bg {
  flex: 1;
  height: 28px;
  background: var(--secondary-bg);
  border-radius: 6px;
  overflow: hidden;
  position: relative;
}

.poll-bar-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 6px;
  transition: width 0.4s ease;
}

.poll-bar-label {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
}

.poll-pct {
  font-size: 13px;
  font-weight: 600;
  width: 45px;
  text-align: right;
}

/* === Empty state === */
.empty-state {
  padding: 40px 20px;
  text-align: center;
  color: var(--hint);
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 12px;
}

.empty-text {
  font-size: 15px;
  font-weight: 500;
  margin-bottom: 4px;
  color: var(--text);
}

.empty-hint {
  font-size: 13px;
}

/* === Toast === */
.toast {
  position: fixed;
  bottom: calc(var(--nav-height) + env(safe-area-inset-bottom, 0px) + 20px);
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  padding: 10px 20px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 500;
  z-index: 200;
  transition: transform 0.3s ease;
  white-space: nowrap;
  pointer-events: none;
}

.toast.show { transform: translateX(-50%) translateY(0); }
.toast.success { background: #2e7d32; color: #fff; }
.toast.error { background: var(--destructive); color: #fff; }

/* === Broadcast detail === */
.bc-preview {
  background: var(--secondary-bg);
  border-radius: var(--radius);
  padding: 12px 14px;
  font-size: 13px;
  line-height: 1.5;
  color: var(--text);
  white-space: pre-wrap;
  word-wrap: break-word;
  margin-top: 8px;
}

/* === Section divider === */
.section-gap {
  height: 8px;
}

/* === Pull to refresh indicator === */
.refresh-hint {
  text-align: center;
  padding: 8px;
  font-size: 12px;
  color: var(--hint);
  cursor: pointer;
}

.refresh-hint:active {
  color: var(--accent);
}

/* === Broadcast Create Wizard === */
.bc-types {
  display: flex;
  gap: 8px;
  margin-top: 8px;
  flex-wrap: wrap;
}

.bc-type-btn {
  flex: 1 1 calc(33% - 8px);
  min-width: 70px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 14px 6px;
  border: 2px solid var(--border);
  border-radius: 12px;
  background: var(--section-bg);
  color: var(--text);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: all .15s;
}

.bc-type-btn.active {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 10%, transparent);
}

.bc-type-btn:active {
  transform: scale(0.97);
}

.bc-type-icon {
  font-size: 28px;
}

.field-label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--hint);
  margin-bottom: 4px;
}

.field-input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--section-bg);
  color: var(--text);
  font-size: 14px;
  box-sizing: border-box;
  outline: none;
  transition: border-color .15s;
}

.field-input:focus {
  border-color: var(--accent);
}

.field-textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--section-bg);
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  box-sizing: border-box;
  outline: none;
  resize: vertical;
  min-height: 120px;
  transition: border-color .15s;
}

.field-textarea:focus {
  border-color: var(--accent);
}

.format-toolbar {
  display: flex;
  gap: 4px;
  margin-bottom: 8px;
  padding: 6px;
  background: var(--section-bg);
  border-radius: 10px;
  flex-wrap: wrap;
}

.fmt-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
  transition: all .15s;
  -webkit-tap-highlight-color: transparent;
}

.fmt-btn:hover, .fmt-btn:active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.field-editor {
  width: 100%;
  min-height: 160px;
  max-height: 320px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.55;
  outline: none;
  overflow-y: auto;
  word-wrap: break-word;
  white-space: pre-wrap;
  -webkit-user-modify: read-write-plaintext-only;
  -webkit-user-modify: read-write;
  box-sizing: border-box;
}

.field-editor:focus {
  border-color: var(--accent);
}

.field-editor:empty::before {
  content: attr(data-placeholder);
  color: var(--hint);
  pointer-events: none;
  display: block;
}

.field-editor code {
  background: var(--section-bg);
  padding: 1px 4px;
  border-radius: 3px;
  font-family: monospace;
  font-size: 13px;
}

.field-editor a {
  color: var(--accent);
  text-decoration: underline;
}

.bc-preview code {
  background: var(--section-bg);
  padding: 1px 5px;
  border-radius: 4px;
  font-family: monospace;
  font-size: 13px;
}

.bc-preview pre {
  background: var(--section-bg);
  padding: 8px 10px;
  border-radius: 6px;
  font-family: monospace;
  font-size: 13px;
  overflow-x: auto;
}

.bc-preview a {
  color: var(--accent);
  text-decoration: underline;
}

.field-counter {
  font-size: 11px;
  color: var(--hint);
  text-align: right;
  margin-top: 4px;
}

.btn-icon {
  background: none;
  border: none;
  font-size: 16px;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 6px;
  color: var(--hint);
  transition: all .15s;
}

.btn-icon:hover, .btn-icon:active {
  background: var(--border);
  color: var(--danger, #ef4444);
}

.btn-sm {
  font-size: 12px;
  padding: 6px 12px;
}

/* === Media Upload Zone === */
.media-upload-zone {
  border: 2px dashed var(--hint);
  border-radius: var(--radius);
  padding: 24px 16px;
  cursor: pointer;
  transition: all 0.2s;
  text-align: center;
  min-height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.media-upload-zone:active {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 5%, transparent);
}

.media-upload-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.media-upload-icon {
  font-size: 36px;
}

.media-upload-text {
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
}

.media-upload-hint {
  font-size: 12px;
  color: var(--hint);
}

.media-upload-status {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 16px;
  font-size: 13px;
  color: var(--hint);
}

.media-preview {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
}

.media-preview-img {
  max-width: 100%;
  max-height: 240px;
  border-radius: 8px;
  object-fit: contain;
}

/* === Responsive === */
@media (max-width: 360px) {
  .stat-value { font-size: 24px; }
  .stat-item { padding: 10px; }
  .nav-label { font-size: 9px; }
  .bc-types { flex-direction: column; }
}
