:root {
  --bg: #f4f6f9;
  --card: #ffffff;
  --ink: #1f2a37;
  --muted: #5b6775;
  --line: #d9e1ea;
  --brand: #1f4e79;
  --brand-2: #c45911;
  --ok: #548235;
  --bad: #c00000;
  --soon: #8a94a0;
  --page-pad: 20px;
  --gap: 12px;
  --radius: 10px;
  --touch: 44px;
}

* { box-sizing: border-box; }
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}
html, body { margin: 0; padding: 0; }
body {
  font-family: "Segoe UI", Calibri, system-ui, sans-serif;
  background: var(--bg);
  color: var(--ink);
  min-height: 100vh;
  min-height: 100dvh;
  font-size: 15px;
  line-height: 1.35;
}

.top {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  padding-top: max(10px, env(safe-area-inset-top));
  padding-left: max(12px, env(safe-area-inset-left));
  padding-right: max(12px, env(safe-area-inset-right));
  background: var(--brand);
  color: #fff;
  position: sticky;
  top: 0;
  z-index: 40;
}
.brand { display: flex; align-items: center; gap: 8px; min-width: 0; flex: 1; }
.brand-mark {
  width: 32px; height: 32px; border-radius: 8px; flex: 0 0 auto;
  background: #fff; color: var(--brand);
  display: grid; place-items: center; font-weight: 700; font-size: 13px;
}
.brand-title {
  font-weight: 700; line-height: 1.15; font-size: 15px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.brand-sub { font-size: 11px; opacity: 0.8; line-height: 1.2; }
.top-actions { display: flex; align-items: center; gap: 4px; flex: 0 0 auto; }
.nav { display: flex; gap: 6px; flex: 1; flex-wrap: wrap; }
.nav-link {
  color: #fff; text-decoration: none;
  padding: 8px 10px; border-radius: 6px; opacity: 0.85;
  min-height: 40px; display: inline-flex; align-items: center;
  font-size: 14px;
}
.nav-link.is-active { background: rgba(255,255,255,0.16); opacity: 1; }
.nav-link.is-soon { opacity: 0.65; }
.userbox { display: flex; gap: 4px; align-items: center; font-size: 13px; }
.userbox a {
  color: #fff; padding: 8px 8px; min-height: 40px;
  display: inline-flex; align-items: center;
}
.nav-toggle {
  display: none;
  width: 40px; height: 40px;
  padding: 9px 8px;
  background: transparent;
  border: 1px solid rgba(255,255,255,0.35);
  border-radius: 8px;
  cursor: pointer;
}
.nav-toggle span {
  display: block; height: 2px; margin: 5px 0; background: #fff; border-radius: 1px;
}

.page {
  padding: var(--page-pad);
  padding-left: max(var(--page-pad), env(safe-area-inset-left));
  padding-right: max(var(--page-pad), env(safe-area-inset-right));
  padding-bottom: max(28px, env(safe-area-inset-bottom));
  max-width: 1400px; margin: 0 auto;
}
h1 {
  margin: 0 0 10px;
  font-size: clamp(1.15rem, 4.5vw, 1.6rem);
  line-height: 1.2;
  color: var(--brand);
}
h2 {
  margin: 0;
  font-size: 0.95rem;
  line-height: 1.25;
  color: var(--brand);
}
.panel > h2 { margin: 0 0 10px; }
.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px; margin-bottom: 10px; flex-wrap: wrap;
}
.grain { display: flex; gap: 4px; }
.grain button {
  background: #fff; color: var(--brand); border: 1px solid var(--line);
  padding: 6px 8px; cursor: pointer; font-size: 13px;
}
.grain button.is-active { background: var(--brand); color: #fff; border-color: var(--brand); }

.filters { margin-bottom: 4px; }
.filters-row {
  display: flex; flex-wrap: wrap; gap: 8px; align-items: end;
}
.filter-actions { display: flex; gap: 6px; align-items: end; }
.filter-actions button { min-height: 42px; }
label { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted); }
input, select, button {
  font: inherit; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px;
  font-size: 16px; /* iOS: не зумит поле */
}
button {
  background: var(--brand); color: #fff; border: 0; cursor: pointer; font-size: 14px;
  font-weight: 600;
}
button.secondary { background: #fff; color: var(--brand); border: 1px solid var(--brand); }

.sources {
  display: flex; flex-wrap: wrap; gap: 6px;
  margin: 8px 0;
}
.source {
  font-size: 12px; padding: 5px 9px; border-radius: 999px;
  background: #e8f0e4; color: var(--ok);
  line-height: 1.3;
  max-width: 100%;
  overflow-wrap: anywhere;
}
.source.is-bad { background: #f8e4e4; color: var(--bad); }

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 8px;
  margin: 8px 0 14px;
}
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 10px 12px;
  min-width: 0;
}
.card .val {
  font-size: clamp(1.15rem, 5vw, 1.65rem);
  font-weight: 700; color: var(--brand);
  overflow-wrap: anywhere; line-height: 1.15;
}
.card .lbl {
  color: var(--muted); font-size: 12px; margin-top: 2px; line-height: 1.25;
}

.grid { display: grid; grid-template-columns: 1fr 1fr; gap: var(--gap); }
.panel {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px;
}
.panel.wide { grid-column: 1 / -1; }
.panel canvas { max-height: 260px; width: 100% !important; }
.panel.wide canvas { max-height: 300px; }

.heatmap { overflow: auto; -webkit-overflow-scrolling: touch; }
.heat-table { border-collapse: collapse; font-size: 11px; }
.heat-table th, .heat-table td {
  min-width: 22px; height: 18px; text-align: center; border: 1px solid #eee;
}
.heat-table th { color: var(--muted); font-weight: 600; }
.heat-table td.day { min-width: 78px; text-align: left; padding: 0 6px; color: var(--muted); }

.placeholder {
  background: var(--card); border: 1px dashed var(--line); border-radius: 12px; padding: 28px;
  color: var(--muted);
}

.login-body {
  min-height: 100vh; min-height: 100dvh; display: grid; place-items: center;
  padding: 16px;
  padding-top: max(16px, env(safe-area-inset-top));
  background: linear-gradient(180deg, #1f4e79, #163a5a);
}
.login-card {
  width: min(360px, 100%); background: #fff; padding: 24px 20px; border-radius: 12px;
  display: flex; flex-direction: column; gap: 10px;
}
.login-card .muted { color: var(--muted); margin: 0 0 6px; font-size: 14px; }
.alert { background: #f8e4e4; color: var(--bad); padding: 8px 10px; border-radius: 6px; font-size: 14px; }

.tabs {
  display: flex;
  flex-wrap: nowrap;
  gap: 6px;
  margin: 6px 0 2px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  padding-bottom: 2px;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  background: #fff;
  color: var(--brand);
  border: 1px solid var(--line);
  min-height: 40px;
  flex: 1 1 0;
  white-space: nowrap;
  font-size: 13px;
  padding: 8px 10px;
}
.tab.is-active {
  background: var(--brand);
  color: #fff;
  border-color: var(--brand);
}
.tab .tab-short { display: none; }
.tab-panel { display: none; }
.tab-panel.is-active { display: block; }

.data-table-wrap,
.mini-table-wrap {
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  margin: 0 -2px;
  padding-bottom: 2px;
}
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
  min-width: 640px;
}
.mini-table-wrap .data-table { min-width: 0; width: 100%; font-size: 12px; }
.data-table th,
.data-table td {
  border: 1px solid var(--line);
  padding: 6px 7px;
  text-align: center;
  vertical-align: middle;
  line-height: 1.25;
}
.data-table th {
  background: #eef1f5;
  color: var(--ink);
  font-weight: 600;
  position: sticky;
  top: 0;
  z-index: 1;
  font-size: 11px;
}
.data-table td:first-child,
.data-table th:first-child {
  text-align: left;
  max-width: 160px;
  position: sticky;
  left: 0;
  background: #fff;
  z-index: 2;
  box-shadow: 1px 0 0 var(--line);
}
.data-table th:first-child {
  background: #eef1f5;
  z-index: 3;
}
.data-table tr.is-total td {
  font-weight: 700;
  background: #f3f6f9;
}
.data-table tr.is-total td:first-child { background: #f3f6f9; }

.export-bar {
  margin: 18px 0 4px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
  display: flex;
  justify-content: flex-end;
}
html.exporting-pdf,
body.exporting-pdf {
  background: #fff;
}
body.exporting-pdf .top,
body.exporting-pdf .export-bar,
body.exporting-pdf .nav-toggle,
body.exporting-pdf #refresh {
  display: none !important;
}
body.exporting-pdf .page {
  background: #fff;
}
/* html2canvas ломается на overflow/sticky — раскрываем и убираем sticky */
body.exporting-pdf .heatmap,
body.exporting-pdf .data-table-wrap,
body.exporting-pdf .mini-table-wrap {
  overflow: visible !important;
  max-height: none !important;
  height: auto !important;
}
body.exporting-pdf .data-table th,
body.exporting-pdf .data-table td:first-child,
body.exporting-pdf .data-table th:first-child {
  position: static !important;
  box-shadow: none !important;
}
/* не avoid-all на всех панелях: высокие heatmap дают пустые страницы */
body.exporting-pdf .panel:not(.wide) {
  break-inside: avoid;
  page-break-inside: avoid;
}
body.exporting-pdf .pdf-chart-img {
  display: block;
  width: 100% !important;
  max-width: 100% !important;
  height: auto !important;
  object-fit: contain;
}
body.exporting-pdf .grid {
  grid-template-columns: 1fr 1fr !important;
}
body.exporting-pdf .panel.wide {
  grid-column: 1 / -1 !important;
}
body.exporting-pdf .panel {
  overflow: visible !important;
}
#pdf-export-holder,
#pdf-export-root {
  box-sizing: border-box;
}
#pdf-export-root .panel {
  overflow: visible !important;
}
#pdf-export-root .pdf-chart-img {
  width: 100% !important;
  max-width: 100% !important;
  height: auto !important;
}

@media print {
  .top, .export-bar, #refresh { display: none !important; }
  .page { max-width: none; padding: 0; }
  .heatmap,
  .data-table-wrap,
  .mini-table-wrap { overflow: visible; }
  .data-table th,
  .data-table td:first-child,
  .data-table th:first-child { position: static !important; }
  .panel:not(.wide) { break-inside: avoid; }
}

/* Планшет / телефон */
@media (max-width: 900px) {
  :root { --page-pad: 12px; --gap: 10px; }

  .grid { grid-template-columns: 1fr; gap: 10px; }
  .panel.wide { grid-column: auto; }
  .top {
    display: grid;
    grid-template-columns: 1fr auto;
    align-items: center;
    gap: 6px 8px;
    padding: 8px 10px;
    padding-top: max(8px, env(safe-area-inset-top));
  }
  .brand { grid-column: 1; min-width: 0; }
  .brand-mark { width: 30px; height: 30px; font-size: 12px; }
  .brand-title { font-size: 14px; }
  .top-actions { grid-column: 2; margin-left: 0; }
  .nav-toggle { display: block; }
  .nav {
    display: none;
    grid-column: 1 / -1;
    flex-direction: column;
    gap: 2px;
    padding-top: 4px;
  }
  .top.is-open .nav { display: flex; }
  .nav-link {
    justify-content: flex-start;
    min-height: var(--touch);
    padding: 10px 12px;
    font-size: 15px;
    border-radius: 8px;
  }
  .userbox a { min-height: 40px; padding: 8px; font-size: 13px; }

  .page { padding-top: 10px; padding-bottom: max(20px, env(safe-area-inset-bottom)); }
  h1 { margin-bottom: 8px; font-size: 1.2rem; }
  h2 { font-size: 0.9rem; }

  .filters-row { gap: 8px; }
  .filters-row label { flex: 1 1 calc(50% - 8px); min-width: 0; }
  .filters-row input, .filters-row select { width: 100%; min-height: 42px; }
  .filter-actions { flex: 1 1 100%; }
  .filter-actions button { flex: 1; min-height: var(--touch); }

  .sources { margin: 6px 0; gap: 5px; }
  .source { font-size: 11px; padding: 4px 8px; }

  .cards {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    margin: 6px 0 12px;
  }
  .card { padding: 9px 10px; }
  .card .val { font-size: 1.25rem; }
  .card .lbl { font-size: 11px; }

  .panel { padding: 10px; }
  .panel canvas { max-height: 210px; }
  .panel.wide canvas { max-height: 230px; }
  .panel-head { margin-bottom: 8px; }

  .export-bar { justify-content: stretch; margin-top: 14px; }
  .export-bar button { width: 100%; min-height: var(--touch); }

  .grain { width: 100%; }
  .grain button {
    flex: 1; min-height: 38px; padding: 6px 4px; font-size: 12px;
  }

  .tabs { gap: 5px; margin: 4px 0; }
  .tab {
    flex: 1 1 0;
    min-width: 0;
    min-height: 42px;
    font-size: 12px;
    padding: 8px 6px;
    white-space: normal;
    line-height: 1.2;
    text-align: center;
  }
  .tab .tab-full { display: none; }
  .tab .tab-short { display: inline; }

  .data-table { min-width: 560px; font-size: 11px; }
  .data-table th, .data-table td { padding: 5px 6px; }
  .data-table td:first-child,
  .data-table th:first-child { max-width: 120px; font-size: 11px; }
}

@media (max-width: 520px) {
  :root { --page-pad: 10px; --gap: 8px; }

  .brand-sub { display: none; }
  .brand-title { font-size: 13px; max-width: 58vw; }
  .brand-mark { width: 28px; height: 28px; }

  /* 2 колонки KPI — меньше пустоты по вертикали */
  .cards { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; }
  .card { padding: 8px; border-radius: 8px; }
  .card .val { font-size: 1.15rem; }
  .card .lbl { font-size: 10.5px; }

  .filters-row label { flex: 1 1 100%; }
  .filters-row label[for],
  .filters-row > label:nth-child(1),
  .filters-row > label:nth-child(2) {
    flex: 1 1 calc(50% - 8px);
  }

  .heat-table { font-size: 10px; }
  .heat-table th, .heat-table td { min-width: 16px; height: 15px; }
  .login-card { padding: 20px 16px; }

  .panel canvas { max-height: 190px; }
  .panel.wide canvas { max-height: 210px; }

  .tab { font-size: 11.5px; padding: 7px 4px; min-height: 40px; }
  .data-table { min-width: 480px; }
  .mini-table-wrap .data-table th:nth-child(n+3),
  .mini-table-wrap .data-table td:nth-child(n+3) {
    font-size: 11px;
  }
}

/* Очень узкие экраны */
@media (max-width: 360px) {
  .cards { gap: 5px; }
  .card .val { font-size: 1.05rem; }
  .userbox { display: none; }
  .top.is-open .userbox {
    display: flex;
    grid-column: 1 / -1;
    justify-content: flex-end;
  }
}
