:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel2: #1e2330;
  --border: #2a3140;
  --text: #e8eaed;
  --muted: #9aa3b2;
  --green: #3d9a6a;
  --yellow: #c4922a;
  --red: #c44b4b;
  --blue: #4c7fd4;
  --radius: 8px;
  --font: "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.4;
  overflow-x: hidden;
}
a { color: inherit; text-decoration: none; }
.wrap {
  max-width: 1180px;
  margin: 0 auto;
  padding: 16px 20px 40px;
  min-width: 0;
}

.top {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
  min-width: 0;
}
.top-head {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  min-width: 0;
}
.brand { min-width: 0; }
.brand h1 {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 650;
  line-height: 1.25;
}
.brand .sub { color: var(--muted); font-size: 0.85rem; margin-top: 2px; }
.brand-short { display: none; }
.nav-short { display: none; }

nav.tabs {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  min-width: 0;
}
nav.tabs a {
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
  font-size: 0.85rem;
  white-space: nowrap;
}
nav.tabs a.active,
nav.tabs a:hover {
  color: var(--text);
  border-color: #3b465c;
  background: var(--panel);
}

.actions {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  min-width: 0;
}
button {
  background: var(--panel2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
  font: inherit;
}
button:hover { border-color: #46526a; }
.meta { color: var(--muted); font-size: 0.8rem; }

.grid { display: grid; gap: 12px; min-width: 0; }
.grid.cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }

@media (max-width: 900px) {
  .grid.cols-4, .grid.cols-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 560px) {
  .grid.cols-4, .grid.cols-3, .grid.cols-2 { grid-template-columns: 1fr; }
}

.stat, .card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  min-width: 0;
  overflow: hidden;
}
.stat .v {
  font-size: 1.45rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  word-break: break-word;
}
.stat .l { color: var(--muted); font-size: 0.8rem; margin-top: 2px; }
.card h2, .card h3 { margin: 0 0 10px; font-size: 0.95rem; font-weight: 650; }

.signal {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.signal:last-child { border-bottom: 0; padding-bottom: 0; }
.dot {
  width: 10px; height: 10px; border-radius: 50%;
  margin-top: 5px; flex: 0 0 auto;
}
.dot.green { background: var(--green); }
.dot.yellow { background: var(--yellow); }
.dot.red { background: var(--red); }
.signal .ttl { font-weight: 600; }
.signal .val { color: var(--text); }
.signal .det { color: var(--muted); font-size: 0.82rem; }

.table-scroll,
.card > div:has(> table),
#ffCore, #pnlMonths, #purchKeys, #reportTable,
#whTable, #priceTable, #usersTable,
#salesTop, #salesSku, #salesA, #salesArBuckets, #salesAr, #salesDaily,
#planCalendar, #cashPayments, #cashStructure, #cashPayables, #cashWeekly,
#purchTop, #purchTotal, #whSurplus, #whProduce, #whKinds, #whGaps, #whAll, #whCalendar,
#priceAlerts, #priceTables, #pricePie {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
}

table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
th, td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-weight: 600; }
td.n, th.n { text-align: right; font-variant-numeric: tabular-nums; }
tr.red td { background: rgba(196, 75, 75, 0.12); }
tr.yellow td { background: rgba(196, 146, 42, 0.1); }

.pill {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.75rem;
  border: 1px solid var(--border);
}
.pill.green { color: var(--green); border-color: #2f6b4c; }
.pill.yellow { color: var(--yellow); border-color: #7a5c1f; }
.pill.red { color: var(--red); border-color: #7a3030; }

.banner {
  margin-bottom: 12px;
  padding: 10px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--panel2);
  color: var(--muted);
  font-size: 0.85rem;
}
.banner.err { border-color: #7a3030; color: #f0b4b4; }
.banner.ok { border-color: #2d5a3d; color: #b4f0c8; }
.hidden { display: none !important; }
.page { display: none; }
.page.active { display: block; min-width: 0; }
.section { margin-top: 14px; }

.report-form {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 10px 12px;
  align-items: end;
}
.report-form label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.82rem;
  color: var(--muted);
}
.report-form label.chk {
  flex-direction: row;
  align-items: center;
  gap: 8px;
  padding-bottom: 8px;
}
.report-form input,
.report-form select,
.card select,
.meta select {
  background: var(--panel2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 7px 8px;
  font-size: 0.9rem;
  max-width: 100%;
}
.report-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  grid-column: 1 / -1;
}
.chat-toolbar {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 8px;
}
.chat-clear {
  font-size: 0.78rem;
  padding: 4px 8px;
  color: var(--muted);
}
.chat-log {
  min-height: 280px;
  max-height: min(70vh, 640px);
  overflow: auto;
  background: var(--panel2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 12px;
  font-size: 0.9rem;
  word-break: break-word;
}
.chat-bubble {
  margin-bottom: 14px;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--panel);
}
.chat-bubble.chat-user {
  background: transparent;
  border-color: transparent;
  padding-left: 4px;
  padding-right: 4px;
}
.chat-bubble .who {
  color: var(--muted);
  font-size: 0.75rem;
  margin-bottom: 6px;
  letter-spacing: 0.02em;
  text-transform: uppercase;
}
.chat-bubble .txt {
  line-height: 1.45;
}
.chat-bubble .txt .chat-p {
  margin: 0 0 6px;
}
.chat-bubble .txt .chat-p:last-child {
  margin-bottom: 0;
}
.chat-list {
  margin: 6px 0 8px;
  padding-left: 1.15rem;
}
.chat-list li {
  margin: 3px 0;
}
.chat-list li::marker {
  color: var(--muted);
}
.chat-table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin: 10px 0 4px;
  max-width: 100%;
  border: 1px solid var(--border);
  border-radius: 6px;
}
.chat-md-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
  margin: 0;
}
.chat-md-table th,
.chat-md-table td {
  padding: 7px 10px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.chat-md-table th {
  background: var(--panel2);
  position: sticky;
  top: 0;
  z-index: 1;
}
.chat-md-table tbody tr:nth-child(even) td {
  background: rgba(255, 255, 255, 0.02);
}
.chat-md-table tbody tr:hover td {
  background: rgba(76, 127, 212, 0.08);
}
.chat-md-table td:first-child,
.chat-md-table th:first-child {
  white-space: normal;
  min-width: 120px;
  max-width: 280px;
}
.chat-chart {
  margin: 12px 0 8px;
  padding: 10px 8px 6px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel2);
}
.chat-chart-grouped svg {
  max-height: 520px;
}
.chat-pre {
  overflow-x: auto;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 0.78rem;
  margin: 8px 0;
}
.chat-h {
  font-weight: 650;
  margin: 10px 0 6px;
  font-size: 0.95rem;
}
.chat-chart-title {
  font-size: 0.78rem;
  color: var(--muted);
  margin: 0 0 8px 4px;
  font-weight: 600;
}
.chat-chart svg {
  display: block;
  max-width: 100%;
  overflow: visible;
}
.chat-chart-lbl {
  fill: var(--text);
  font-size: 11px;
}
.chat-chart-val {
  fill: var(--muted);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}
.chat-bubble .txt code {
  font-size: 0.85em;
  background: var(--panel2);
  padding: 1px 5px;
  border-radius: 3px;
}
.chat-form {
  display: flex;
  gap: 8px;
  align-items: stretch;
}
.chat-form input {
  flex: 1;
  min-width: 0;
  background: var(--panel2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 10px 12px;
  font-size: 1rem;
}

#usersTable input,
#usersTable select {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 4px 6px;
  font: inherit;
  max-width: 160px;
}
#usersTable .user-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}
#usersTable .user-actions button {
  font-size: 0.78rem;
  padding: 4px 7px;
}

/* —— Mobile —— */
@media (max-width: 720px) {
  .wrap {
    padding: 0 0 calc(20px + env(safe-area-inset-bottom, 0px));
  }

  .top {
    position: sticky;
    top: 0;
    z-index: 50;
    margin: 0 0 12px;
    padding: 0;
    gap: 0;
    border-bottom: 0;
    background: rgba(15, 17, 21, 0.96);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
  }

  .top-head {
    flex-wrap: nowrap;
    gap: 8px;
    padding: calc(8px + env(safe-area-inset-top, 0px)) 12px 8px;
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
    border-bottom: 1px solid var(--border);
  }

  .brand-full { display: none; }
  .brand-short { display: inline; }
  .brand h1 { font-size: 1.05rem; }
  .brand .sub { display: none; }

  .nav-long { display: none; }
  .nav-short { display: inline; }

  .actions {
    flex: 0 0 auto;
    flex-wrap: nowrap;
    gap: 6px;
  }
  .actions .meta {
    display: none;
  }
  .actions button {
    min-height: 36px;
    padding: 6px 10px;
    font-size: 0.82rem;
  }

  /* Горизонтальный скролл вкладок — критично min-width:0 */
  nav.tabs {
    display: flex;
    flex-wrap: nowrap;
    gap: 6px;
    width: 100%;
    max-width: 100%;
    min-width: 0;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
    scroll-snap-type: x proximity;
    scrollbar-width: none;
    padding: 8px 12px 10px;
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
    border-bottom: 1px solid var(--border);
    mask-image: linear-gradient(90deg, transparent 0, #000 12px, #000 calc(100% - 12px), transparent);
    -webkit-mask-image: linear-gradient(90deg, transparent 0, #000 12px, #000 calc(100% - 12px), transparent);
  }
  nav.tabs::-webkit-scrollbar { display: none; height: 0; }
  nav.tabs a {
    flex: 0 0 auto;
    scroll-snap-align: start;
    font-size: 0.84rem;
    padding: 8px 14px;
    min-height: 40px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }
  nav.tabs a.active {
    background: var(--panel2);
    border-color: #4a5670;
  }

  .page.active,
  #banner {
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
  }

  .grid.cols-4,
  .grid.cols-3,
  .grid.cols-2 {
    grid-template-columns: 1fr !important;
  }

  .stat .v { font-size: 1.28rem; }
  .card, .stat { padding: 12px; }
  .card h2 { font-size: 0.9rem; }

  table { font-size: 0.76rem; }
  th, td { padding: 6px 6px; }

  .report-form {
    grid-template-columns: 1fr;
  }
  .report-form input,
  .report-form select,
  .card select,
  .meta select {
    width: 100%;
    min-height: 42px;
    font-size: 16px; /* без автозума iOS */
  }

  #usersTable .user-actions {
    flex-direction: column;
  }
  #usersTable input,
  #usersTable select {
    max-width: 100%;
    width: 100%;
    min-height: 40px;
    font-size: 16px;
  }

  .chat-log {
    max-height: 50vh;
    min-height: 160px;
  }
  .chat-form {
    flex-direction: row;
  }
  .chat-form input {
    min-height: 44px;
    font-size: 16px;
  }
  .chat-form button {
    min-height: 44px;
    min-width: 72px;
    flex: 0 0 auto;
  }

  .banner { font-size: 0.82rem; }
}

@media (display-mode: standalone) {
  body {
    overscroll-behavior-y: contain;
  }
}

