:root {
  --bg: #f5f7fb;
  --surface: #ffffff;
  --surface-soft: #f9fbff;
  --line: #d9e1ee;
  --line-strong: #b9c6d8;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #2563eb;
  --primary-strong: #1d4ed8;
  --success: #047857;
  --success-soft: #dcfce7;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --warning: #b45309;
  --warning-soft: #fef3c7;
  --shadow: 0 10px 26px rgba(15, 23, 42, 0.06);
  font-family: Inter, "Segoe UI", Arial, sans-serif;
}

* { box-sizing: border-box; }
html { min-width: 320px; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  letter-spacing: 0;
}

button, input, select, textarea { font: inherit; }
button {
  min-height: 34px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  padding: 7px 11px;
  cursor: pointer;
}
button:hover { border-color: #94a3b8; background: #f8fafc; }
button.primary { border-color: var(--primary); background: var(--primary); color: #fff; }
button.primary:hover { background: var(--primary-strong); }
button.danger { border-color: #fecaca; color: var(--danger); }
button.success { border-color: #bbf7d0; color: var(--success); }
button.compact { min-height: 30px; padding: 5px 9px; font-size: 12px; }
.update-btn {
  min-width: 130px;
  border-color: #bbf7d0;
  color: var(--success);
  background: #f0fdf4;
}
.update-btn.update-available {
  border-color: #fed7aa;
  color: var(--warning);
  background: #fff7ed;
}

input, select, textarea {
  width: 100%;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  padding: 8px 10px;
}
textarea { resize: vertical; min-height: 80px; }

h1, h2, h3, p { margin: 0; }
h1 { font-size: 22px; line-height: 1.15; }
h2 { font-size: 16px; line-height: 1.25; }
h3 { font-size: 14px; }
p, .small, label { color: var(--muted); font-size: 12px; line-height: 1.45; }
label { display: grid; gap: 5px; font-weight: 650; }

.title-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.build-badge {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  border: 1px solid #bfdbfe;
  border-radius: 999px;
  background: #eff6ff;
  color: #1d4ed8;
  padding: 4px 10px;
  font-size: 12px;
  font-weight: 800;
  line-height: 1.2;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 18px 22px;
  background: rgba(255, 255, 255, 0.94);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
  backdrop-filter: blur(14px);
}
.top-actions { display: flex; align-items: end; gap: 10px; flex-wrap: wrap; justify-content: flex-end; }
.api-field { display: grid; gap: 4px; }
.api-field input { width: min(360px, 48vw); }

.tabbar {
  position: sticky;
  top: 77px;
  z-index: 19;
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 10px 16px;
  background: rgba(245, 247, 251, 0.92);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(14px);
}
.tabbar button {
  white-space: nowrap;
  border-color: var(--line);
  background: #fff;
  font-weight: 750;
}
.tabbar button.active {
  border-color: var(--primary);
  background: #eff6ff;
  color: var(--primary-strong);
}

.layout { display: grid; gap: 16px; padding: 16px; }
.view { display: none; }
.view.active { display: block; }

.summary-band {
  display: grid;
  grid-template-columns: repeat(5, minmax(130px, 1fr));
  gap: 10px;
}
.metric {
  min-height: 82px;
  display: grid;
  align-content: center;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  box-shadow: var(--shadow);
}
.metric span { color: var(--muted); font-size: 12px; }
.metric strong { font-size: 24px; line-height: 1; }
.metric.urgent { border-color: #fecaca; background: linear-gradient(180deg, #fff, var(--danger-soft)); }
.metric.warn { border-color: #fde68a; background: linear-gradient(180deg, #fff, var(--warning-soft)); }

.content-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.45fr) minmax(360px, 0.75fr);
  gap: 16px;
  align-items: start;
}
.left-stack { display: grid; gap: 16px; }
.editor-grid {
  display: grid;
  grid-template-columns: minmax(360px, 0.78fr) minmax(0, 1.22fr);
  gap: 16px;
  align-items: start;
}
.editor-grid.log-only-grid { grid-template-columns: 1fr; }

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.panel-head {
  min-height: 52px;
  padding: 13px 14px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.table-wrap { overflow: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px 12px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { font-size: 12px; color: var(--muted); font-weight: 750; background: var(--surface-soft); }
td { line-height: 1.35; }

.form-panel { position: sticky; top: 132px; }
.form-body {
  padding: 14px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.form-body .wide { grid-column: 1 / -1; }
.form-actions {
  padding: 12px 14px;
  border-top: 1px solid var(--line);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.status { display: inline-flex; align-items: center; gap: 7px; color: var(--muted); }
.dot { width: 9px; height: 9px; border-radius: 999px; background: #94a3b8; }
.dot.online { background: var(--success); }
.dot.error { background: var(--danger); }
.dot.starting, .dot.updating { background: var(--warning); }

.pill {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  border-radius: 999px;
  padding: 3px 9px;
  font-size: 12px;
  font-weight: 750;
  background: #eef2ff;
  color: #3730a3;
}
.pill.send_failed, .pill.send_error, .pill.rejected { background: var(--danger-soft); color: var(--danger); }
.pill.open, .pill.in_progress, .pill.not_sent, .pill.planned { background: var(--warning-soft); color: var(--warning); }
.pill.online, .pill.active, .pill.completed, .pill.sent { background: var(--success-soft); color: var(--success); }
.pill.paused, .pill.offline, .pill.disabled { background: #f1f5f9; color: var(--muted); }

.actions { display: flex; align-items: center; gap: 7px; flex-wrap: wrap; }
.card-list { display: grid; }
.record-card, .alert-row {
  padding: 13px 14px;
  border-bottom: 1px solid var(--line);
  display: grid;
  gap: 9px;
}
.record-card:last-child, .alert-row:last-child { border-bottom: 0; }
.record-main, .alert-main {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: start;
}
.record-meta {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 8px;
}
.kv {
  border: 1px solid var(--line);
  background: var(--surface-soft);
  border-radius: 8px;
  padding: 8px;
  min-width: 0;
}
.kv span { display: block; color: var(--muted); font-size: 11px; margin-bottom: 3px; }
.kv strong { display: block; font-size: 12px; overflow-wrap: anywhere; white-space: pre-wrap; }
.process-log-card { gap: 12px; }
.process-summary-row {
  display: grid;
  grid-template-columns: repeat(4, minmax(140px, 1fr));
  gap: 8px;
}
.process-log-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.15fr) minmax(300px, 0.85fr);
  gap: 10px;
}
.debug-section {
  border: 1px solid var(--line);
  background: var(--surface-soft);
  border-radius: 8px;
  padding: 10px;
  display: grid;
  align-content: start;
  gap: 8px;
  min-width: 0;
}
.debug-section h4 {
  margin: 0;
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
}
.series-list { display: grid; gap: 8px; }
.series-card {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 8px;
  padding: 9px;
  min-width: 0;
}
.latest-series { border-color: #93c5fd; background: #eff6ff; }
.series-head {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: start;
  margin-bottom: 6px;
}
.series-head strong { font-size: 12px; }
.series-head span { color: var(--muted); font-size: 11px; text-align: right; }
.debug-text {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  color: var(--text);
  font-size: 12px;
  line-height: 1.45;
}
.debug-note {
  border-left: 3px solid var(--warning);
  color: var(--muted);
  background: #fff;
  padding: 8px;
  border-radius: 6px;
  overflow-wrap: anywhere;
}
.action-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.action-chip {
  border-radius: 999px;
  background: #dbeafe;
  color: #1d4ed8;
  padding: 4px 8px;
  font-size: 11px;
  font-weight: 750;
}
.action-chip.warning { background: var(--warning-soft); color: var(--warning); }
.debug-reply {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 9px;
}

.avatar {
  width: 56px;
  height: 56px;
  border-radius: 8px;
  object-fit: cover;
  border: 1px solid var(--line);
  background: #f8fafc;
}
.image-preview {
  min-height: 76px;
  border: 1px dashed var(--line-strong);
  border-radius: 8px;
  background: var(--surface-soft);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  color: var(--muted);
}
.image-preview img { width: 72px; height: 72px; border-radius: 8px; object-fit: cover; border: 1px solid var(--line); }
.image-preview strong,
.image-preview span {
  display: block;
  overflow-wrap: anywhere;
}
.image-preview strong { color: var(--text); font-size: 13px; }
.image-preview span { font-size: 12px; line-height: 1.4; }
.avatar-empty {
  display: inline-block;
  background: linear-gradient(135deg, #f8fafc, #e2e8f0);
}

.selfie-manager {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
  padding: 12px;
  display: grid;
  gap: 12px;
}
.selfie-manager.is-disabled { opacity: 0.76; }
.selfie-manager-head {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: start;
}
.selfie-head-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}
.selfie-workspace {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  align-items: start;
}
.selfie-section-label {
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  margin-bottom: 7px;
  text-transform: uppercase;
}
.selfie-strip {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 8px;
}
.selfie-chip {
  justify-content: flex-start;
  display: flex;
  gap: 8px;
  align-items: center;
  text-align: left;
  background: #fff;
  min-height: 62px;
  padding: 8px;
  border-color: var(--line);
}
.selfie-chip.is-selected {
  border-color: var(--primary);
  background: #eff6ff;
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.10);
}
.selfie-chip .avatar {
  width: 34px;
  height: 34px;
  flex: 0 0 auto;
}
.selfie-chip span {
  min-width: 0;
  display: grid;
  gap: 2px;
}
.selfie-chip strong,
.selfie-chip small {
  overflow-wrap: anywhere;
}
.selfie-chip strong { color: var(--text); font-size: 12px; }
.selfie-chip small { color: var(--muted); font-size: 11px; line-height: 1.25; }
.selfie-editor-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  overflow: hidden;
}
.selfie-editor-head {
  padding: 11px 12px;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, #fff, #f8fbff);
}
.selfie-editor-head h4 {
  margin: 0;
  font-size: 13px;
}
.selfie-editor-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  padding: 12px;
}
.selfie-editor-grid .wide { grid-column: 1 / -1; }
.selfie-file-field input { padding: 7px; }
.file-note {
  display: block;
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
}
.selfie-preview {
  min-height: 98px;
  background: #fbfdff;
}
.selfie-preview img {
  width: 88px;
  height: 88px;
}
.selfie-actions {
  display: flex;
  justify-content: flex-end;
}
.selfie-gallery {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
  gap: 8px;
}
.selfie-thumb {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
  padding: 8px;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 8px;
  align-items: start;
}
.selfie-thumb .avatar { width: 48px; height: 48px; }
.selfie-thumb strong,
.selfie-thumb span {
  display: block;
  overflow-wrap: anywhere;
}
.selfie-thumb strong { font-size: 12px; }
.selfie-thumb span { color: var(--muted); font-size: 11px; line-height: 1.35; }
.selfie-thumb-actions {
  grid-column: 1 / -1;
  display: flex;
  gap: 6px;
}
.empty-selfies,
.compact-empty {
  color: var(--muted);
  border: 1px dashed var(--line-strong);
  border-radius: 8px;
  padding: 10px;
  background: #fff;
}
.compact-empty { min-width: 190px; }

.alert-panel {
  max-height: calc(100vh - 164px);
  overflow: hidden;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
}
.alert-list { overflow: auto; }
.alert-row.send_failed { border-left: 4px solid var(--danger); }
.reply-box {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
  padding: 9px;
  display: grid;
  gap: 6px;
}
.reply-part { white-space: pre-wrap; line-height: 1.45; }
.error-text { color: var(--danger); overflow-wrap: anywhere; }
.muted { color: var(--muted); }
.empty { padding: 18px 14px; color: var(--muted); }

.toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  max-width: min(420px, calc(100vw - 36px));
  background: #111827;
  color: #fff;
  border-radius: 8px;
  padding: 11px 13px;
  box-shadow: 0 14px 34px rgba(15, 23, 42, 0.24);
  opacity: 0;
  transform: translateY(12px);
  pointer-events: none;
  transition: opacity 160ms ease, transform 160ms ease;
}
.toast.show { opacity: 1; transform: translateY(0); }

@media (max-width: 1100px) {
  .summary-band { grid-template-columns: repeat(3, minmax(130px, 1fr)); }
  .content-grid, .editor-grid, .selfie-workspace, .process-log-grid { grid-template-columns: 1fr; }
  .process-summary-row { grid-template-columns: repeat(2, minmax(140px, 1fr)); }
  .form-panel { position: static; }
  .alert-panel { max-height: none; }
}

@media (max-width: 720px) {
  .topbar, .top-actions, .panel-head, .record-main, .alert-main {
    align-items: stretch;
    flex-direction: column;
  }
  .api-field input { width: 100%; }
  .summary-band { grid-template-columns: 1fr 1fr; }
  .process-summary-row { grid-template-columns: 1fr; }
  .form-body, .selfie-editor-grid { grid-template-columns: 1fr; }
  .metric strong { font-size: 20px; }
}
