/* ── Telegram theme variables with sensible fallbacks ─────────────────── */
:root {
  --bg:           var(--tg-theme-bg-color,           #ffffff);
  --text:         var(--tg-theme-text-color,          #000000);
  --hint:         var(--tg-theme-hint-color,          #707579);
  --link:         var(--tg-theme-link-color,          #2481cc);
  --btn:          var(--tg-theme-button-color,        #2481cc);
  --btn-text:     var(--tg-theme-button-text-color,   #ffffff);
  --sec-bg:       var(--tg-theme-secondary-bg-color,  #f1f1f1);
  --header-bg:    var(--tg-theme-header-bg-color,     #ffffff);
  --section-sep:  var(--tg-theme-section-separator-color, #e0e0e0);

  --source:  #27ae60;
  --network: #2980b9;
  --brand:   #e74c3c;

  --radius: 10px;
  --gap:    12px;
  --font:   -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

/* ── Reset ──────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: var(--font);
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
  overflow-x: hidden;
}

/* ── Header ─────────────────────────────────────────────────────────────── */
#app-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  background: var(--header-bg);
  border-bottom: 1px solid var(--section-sep);
  position: sticky;
  top: 0;
  z-index: 100;
}
#logo { width: 32px; height: 32px; object-fit: contain; border-radius: 6px; }
#company-name { font-size: 16px; font-weight: 700; color: var(--text); }

/* ── Tab bar ─────────────────────────────────────────────────────────────── */
#tab-bar {
  display: flex;
  background: var(--header-bg);
  border-bottom: 1px solid var(--section-sep);
  overflow-x: auto;
  position: sticky;
  top: 53px;
  z-index: 99;
}
.tab-btn {
  flex: 1;
  padding: 10px 6px;
  font-size: 12px;
  font-weight: 500;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--hint);
  cursor: pointer;
  white-space: nowrap;
  transition: color .15s, border-color .15s;
}
.tab-btn.active {
  color: var(--btn);
  border-bottom-color: var(--btn);
}
.tab-btn:hover:not(.active) { color: var(--text); }

/* ── Tab sections ────────────────────────────────────────────────────────── */
.tab-section { display: none; padding: var(--gap); }
.tab-section.active { display: block; }

/* ── Generic card ─────────────────────────────────────────────────────────── */
.card {
  background: var(--sec-bg);
  border-radius: var(--radius);
  padding: var(--gap);
  margin-bottom: var(--gap);
}
.card h3 { font-size: 14px; font-weight: 600; margin-bottom: 8px; }

/* ── Inputs / textareas / selects ─────────────────────────────────────────── */
input, textarea, select {
  display: block;
  width: 100%;
  padding: 8px 10px;
  margin-bottom: 8px;
  font-size: 14px;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--section-sep);
  border-radius: 8px;
  outline: none;
  transition: border-color .15s;
}
input:focus, textarea:focus, select:focus { border-color: var(--btn); }
input[disabled], textarea[disabled], select[disabled] {
  opacity: .5;
  cursor: not-allowed;
}

/* ── Buttons ─────────────────────────────────────────────────────────────── */
button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  padding: 8px 14px;
  font-size: 13px;
  font-family: var(--font);
  font-weight: 500;
  background: var(--btn);
  color: var(--btn-text);
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: opacity .15s;
}
button:hover  { opacity: .85; }
button:active { opacity: .7; }
button.btn-secondary {
  background: var(--sec-bg);
  color: var(--text);
  border: 1px solid var(--section-sep);
}
button.btn-danger { background: #e74c3c; }
button.btn-sm { padding: 4px 10px; font-size: 12px; }
button.btn-icon {
  background: none;
  color: var(--hint);
  padding: 4px 6px;
  border-radius: 6px;
  border: none;
}
button.btn-icon:hover { background: var(--sec-bg); color: var(--text); }

/* ── Search row ──────────────────────────────────────────────────────────── */
.search-row {
  display: flex;
  gap: 8px;
  margin-bottom: var(--gap);
  align-items: flex-start;
}
.search-row input,
.search-row select { margin-bottom: 0; flex: 1; }
.search-row button { white-space: nowrap; }

/* ── Partner cards (colored by type) ─────────────────────────────────────── */
.partner-card {
  background: var(--bg);
  border-radius: var(--radius);
  border-left: 4px solid var(--hint);
  padding: var(--gap);
  margin-bottom: var(--gap);
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
}
.partner-card.card-source  { border-left-color: var(--source);  }
.partner-card.card-network { border-left-color: var(--network); }
.partner-card.card-brand   { border-left-color: var(--brand);   }

.card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}
.partner-type-badge {
  font-size: 10px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 20px;
  color: #fff;
}
.card-source  .partner-type-badge { background: var(--source);  }
.card-network .partner-type-badge { background: var(--network); }
.card-brand   .partner-type-badge { background: var(--brand);   }

.partner-name { font-weight: 600; font-size: 14px; flex: 1; }
.partner-notes { font-size: 12px; color: var(--hint); margin: 4px 0 6px; }

.sub-section { margin-top: 8px; }
.sub-section > strong {
  display: block;
  font-size: 11px;
  font-weight: 600;
  color: var(--hint);
  text-transform: uppercase;
  letter-spacing: .5px;
  margin-bottom: 4px;
}
.item-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 5px 0;
  border-bottom: 1px solid var(--sec-bg);
  font-size: 13px;
  gap: 6px;
}
.item-row:last-child { border-bottom: none; }
.item-row span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ── Match cards ─────────────────────────────────────────────────────────── */
.match-card {
  background: var(--bg);
  border: 1px solid var(--section-sep);
  border-radius: var(--radius);
  padding: var(--gap);
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 10px;
}
.match-info { flex: 1; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.match-geo {
  font-size: 12px; font-weight: 700; padding: 2px 8px;
  background: var(--btn); color: var(--btn-text);
  border-radius: 20px;
}
.match-vertical { font-size: 12px; color: var(--hint); }
.match-flow { font-size: 13px; font-weight: 500; width: 100%; }
.match-contacts { font-size: 11px; color: var(--hint); width: 100%; }

/* ── Buffer ──────────────────────────────────────────────────────────────── */
.buffer-item {
  background: var(--bg);
  border: 1px solid var(--section-sep);
  border-radius: var(--radius);
  padding: var(--gap);
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 10px;
}
.buffer-item-text {
  flex: 1;
  font-size: 13px;
  color: var(--hint);
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
}
.buffer-item-date { font-size: 11px; color: var(--hint); white-space: nowrap; }

/* ── Normalized items table ──────────────────────────────────────────────── */
.norm-items { display: flex; flex-direction: column; gap: 8px; margin: 8px 0; }
.norm-item {
  background: var(--sec-bg);
  border-radius: 8px;
  padding: 10px;
  position: relative;
}
.norm-item .field-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}
.norm-item label { font-size: 11px; color: var(--hint); display: block; margin-bottom: 2px; }
.norm-item input, .norm-item select { margin-bottom: 0; font-size: 13px; padding: 5px 8px; }
.norm-item .item-header {
  display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px;
}
.norm-item .item-num { font-size: 12px; font-weight: 600; color: var(--hint); }
.unresolved { border: 1px solid #f39c12; }
.unresolved::after {
  content: '⚠ unresolved';
  font-size: 10px; color: #f39c12;
  position: absolute; top: 8px; right: 10px;
}

/* ── Modals ──────────────────────────────────────────────────────────────── */
.modal {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.45);
  z-index: 200;
  align-items: flex-end;
  justify-content: center;
}
.modal.show { display: flex; }
.modal-content {
  background: var(--bg);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 16px;
  width: 100%;
  max-width: 600px;
  max-height: 92vh;
  overflow-y: auto;
  position: relative;
}
.modal-content h2 { font-size: 16px; margin-bottom: 12px; padding-right: 30px; }
.modal-close {
  position: absolute;
  top: 12px; right: 12px;
  background: var(--sec-bg);
  color: var(--hint);
  border: none;
  width: 28px; height: 28px;
  border-radius: 50%;
  font-size: 14px;
  cursor: pointer;
  padding: 0;
}
.modal-close:hover { background: var(--section-sep); }

/* ── Edit sections ────────────────────────────────────────────────────────── */
.edit-section {
  margin-bottom: 14px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--sec-bg);
}
.edit-section:last-child { border-bottom: none; }
.edit-section h4 { font-size: 13px; font-weight: 600; margin-bottom: 8px; color: var(--hint); }
.add-row { display: flex; gap: 8px; align-items: flex-start; }
.add-row input { margin-bottom: 0; }

/* ── Settings tab ────────────────────────────────────────────────────────── */
.participant-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 7px 0;
  border-bottom: 1px solid var(--sec-bg);
  gap: 8px;
  font-size: 13px;
}
.participant-row:last-child { border-bottom: none; }
.participant-info { flex: 1; }
.participant-info .p-name { font-weight: 500; }
.participant-info .p-meta { font-size: 11px; color: var(--hint); }
.role-badge {
  font-size: 10px; padding: 2px 6px; border-radius: 20px;
  background: var(--sec-bg); color: var(--hint);
}
.role-badge.admin { background: #e8f4ff; color: var(--network); }
.role-badge.super { background: #fff3cd; color: #856404; }

/* ── Merge mode radios ────────────────────────────────────────────────────── */
.radio-group { display: flex; gap: 12px; margin-bottom: 8px; flex-wrap: wrap; }
.radio-group label {
  display: flex; align-items: center; gap: 5px;
  font-size: 13px; cursor: pointer; width: auto; margin-bottom: 0;
}
.radio-group input[type=radio] { width: auto; display: inline; margin-bottom: 0; }

/* ── Toast notification ──────────────────────────────────────────────────── */
.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: #333;
  color: #fff;
  padding: 9px 18px;
  border-radius: 20px;
  font-size: 13px;
  z-index: 300;
  pointer-events: none;
  white-space: nowrap;
}
.toast.success { background: var(--source); }
.toast.error   { background: var(--brand);  }
.toast.warn    { background: #e67e22;        }

/* ── State helpers ───────────────────────────────────────────────────────── */
.loading { color: var(--hint); padding: 20px; text-align: center; font-size: 13px; }
.empty   { color: var(--hint); padding: 20px; text-align: center; font-size: 13px; }
.error   { color: var(--brand); padding: 12px; text-align: center; font-size: 13px; }

/* ── Divider ─────────────────────────────────────────────────────────────── */
hr { border: none; border-top: 1px solid var(--sec-bg); margin: 12px 0; }
