/* ============================================================
   Инетометр — стили
   Mobile-first, 4 брейкпоинта:
     · 480  — phone landscape / small phone portrait
     · 768  — tablet portrait
     · 1024 — small desktop / tablet landscape
     · 1440 — large desktop
   Без transform: scale — адаптив реализован через clamp() и grid.
   ============================================================ */

:root {
  /* Цвета (light) */
  --bg: #ffffff;
  --bg-2: #f6f6f6;
  --surface: #ffffff;
  --surface-2: #f6f6f6;
  --border: #e6e6e6;
  --text: #1a1a1a;          /* 17.4:1 на --bg (WCAG AAA) */
  --text-dim: #666666;      /*  5.7:1 на --bg (WCAG AA) */
  --text-mute: #767676;     /*  4.5:1 на --bg (WCAG AA) */
  --accent: #fc3f1d;        /* бренд-цвет, 3.6:1 — допустим для крупного/жирного текста (≥18pt) и UI-элементов */
  --accent-2: #ff7a3d;
  --accent-text: #c22600;   /*  5.9:1 — для текстовых ссылок */
  --accent-hover: #e63817;
  --accent-soft: rgba(252, 63, 29, 0.08);  /* подсветка при hover */

  /* Радиусы и тени */
  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 2px 8px rgba(0,0,0,0.04);
  --shadow-btn: 0 4px 14px rgba(252, 63, 29, 0.3);
  --focus-ring: 0 0 0 3px rgba(252, 63, 29, 0.45);

  /* Типографика (fluid: min → preferred → max) */
  /* font-size: clamp(min, fluid, max) */
  --fs-metric:  clamp(34px, 5.7vw, 44px);   /* цифры скорости +5 */
  --fs-metric-small: clamp(32px, 5.2vw, 40px);
  --fs-metric-head: clamp(12px, 2.4vw, 14px);
  --fs-unit: clamp(11px, 2.2vw, 13px);
  --fs-logo: clamp(18px, 4.2vw, 22px);
  --fs-btn: clamp(15px, 3.4vw, 18px);
  --fs-status: clamp(13px, 2.8vw, 15px);
  --fs-chart-title: clamp(12px, 2.6vw, 14px);
  --fs-chart-current: clamp(14px, 3vw, 16px);

  /* Отступы */
  --gap-app: clamp(12px, 2.5vw, 20px);
  --gap-section: clamp(20px, 4vw, 32px);
}

/* ============================================================
   Reset
   ============================================================ */
* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
html { -webkit-text-size-adjust: 100%; } /* отключаем авто-зум iOS на повороте */
body {
  font-family: 'YS Display', 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh; /* dynamic viewport — для mobile браузеров с адресной строкой */
  -webkit-font-smoothing: antialiased;
  font-size: 16px;
  line-height: 1.4;
  overflow-x: hidden;
}

/* ============================================================
   App layout — mobile-first
   ============================================================ */
.app {
  max-width: 920px;
  margin: 0 auto;
  padding: max(env(safe-area-inset-top), 12px)
           max(env(safe-area-inset-right), 12px)
           max(env(safe-area-inset-bottom), 16px)
           max(env(safe-area-inset-left), 12px);
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.content {
  display: flex;
  flex-direction: column;
}

/* ============================================================
   Topbar
   ============================================================ */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 0;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 20px;
}

.server-selector {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: var(--text-dim);
}

.server-selector select {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 4px;
  font-size: 13px;
  cursor: pointer;
}

.logo {
  font-size: var(--fs-logo);
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text);
}
.icon-btn {
  /* WCAG AAA: touch-target ≥ 44×44 px */
  min-width: 44px;
  min-height: 44px;
  width: 44px;
  height: 44px;
  padding: 0;
  border-radius: 50%;
  border: none;
  background: var(--surface-2);
  color: var(--text-dim);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--border); color: var(--text); }

/* ============================================================
   Gauge section (шкала + метрики)
   SVG: viewBox="0 0 800 460", cx=400, cy=460, rx=ry=378
   Метрики позиционируются абсолютно поверх SVG.
   ============================================================ */
/* Метрики, статус и кнопка — ВНУТРИ полусферы (absolute поверх SVG).
   Геометрия viewBox 0..800 × 0..460:
   - cy=460, rx=ry=378 — нижние точки шкалы на y=460
   - метрики   top: 70%  (y≈322)
   - status    top: 86%  (y≈395)
   - кнопка    bottom: 2% (y≈451) — вровень с нижними точками шкалы
   .gauge-section имеет ту же aspect-ratio что и SVG, чтобы absolute-дети
   позиционировались относительно низа полусферы, а не body. */
.gauge-section {
  position: relative;
  background: var(--bg);
  padding: 0;
  width: 100%;
  aspect-ratio: 800 / 460;
  max-height: clamp(420px, 60vh, 620px);
  margin: 0 auto;
  flex: 0 0 auto;
  align-self: center;
}
.gauge-svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

/* Тики */
.tick {
  stroke: var(--accent);
  stroke-width: 1.4;
  stroke-linecap: round;
}
.progress-arc {
  fill: none;
  stroke: var(--accent);
  stroke-width: 3;
  stroke-linecap: round;
  opacity: 0;
  filter: drop-shadow(0 0 6px rgba(252, 63, 29, 0.5));
  transition: opacity 0.3s;
}
.progress-arc.visible { opacity: 0.95; }

.indicator {
  fill: var(--accent);
  stroke: white;
  stroke-width: 2.5;
  filter: drop-shadow(0 0 8px rgba(252, 63, 29, 0.7));
  transition: cx 0.4s cubic-bezier(0.22, 1, 0.36, 1), cy 0.4s cubic-bezier(0.22, 1, 0.36, 1);
  opacity: 0;
}
.indicator.visible { opacity: 1; }

/* Метрики — ВНУТРИ полусферы, в верхней половине, выше кнопки.
   Симметрично: 3 равные колонки с маленьким gap. */
.gauge-metrics {
  position: absolute;
  left: 0;
  right: 0;
  top: calc(50% + 19px);
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  align-items: end;
  gap: 0;
  padding: 0 clamp(8px, 4vw, 80px);
  z-index: 2;
  pointer-events: none;
}
.g-metric {
  text-align: center;
  pointer-events: auto;
  min-width: 0; /* grid item: allow shrink */
}
.g-metric-head {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-size: var(--fs-metric-head);
  color: var(--text-dim);
  margin-bottom: clamp(2px, 1vw, 8px);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.g-metric-arrow {
  display: inline-flex;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--text);
  color: white;
  font-size: 11px;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  flex-shrink: 0;
}
.g-metric-value {
  font-size: var(--fs-metric);
  font-weight: 700;
  letter-spacing: -0.03em;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  line-height: 1.0;
  margin-bottom: 2px;
  /* Защита от переполнения на маленьких экранах */
  white-space: nowrap;
  overflow: hidden;
  text-overflow: clip;
}
.g-metric-unit {
  font-size: var(--fs-unit);
  color: var(--text-dim);
  line-height: 1.0;
}

.gauge-status {
  position: absolute;
  left: 0;
  right: 0;
  top: calc(75% + 10px);
  text-align: center;
  font-size: var(--fs-status);
  color: var(--text-dim);
  min-height: 24px;
  margin: 0;
  z-index: 2;
  pointer-events: none;
  display: block;
}

/* ============================================================
   Primary button — ВНУТРИ полусферы, нижний край ВРОВЕНЬ с нижними точками.
   SVG viewBox 0 0 800 460, cy=460 — низ полусферы = 100% секции.
   top: 88% (405px), height: 48px → bottom ≈ 453 (7px от низа).
   ============================================================ */
.primary-btn {
  position: absolute;
  top: calc(90% + 4px);
  left: 25%;
  right: 25%;
  width: auto;
  min-height: 53px;
  padding: clamp(10px, 2.2vw, 16px);
  background: var(--accent);
  border: none;
  border-radius: var(--radius);
  color: white;
  font-family: inherit;
  font-size: var(--fs-btn);
  font-weight: 500;
  cursor: pointer;
  overflow: hidden;
  box-shadow: var(--shadow-btn);
  transition: background 0.2s, transform 0.1s;
  z-index: 3;
}
.primary-btn:hover { background: var(--accent-hover); }
.primary-btn:active { transform: scale(0.99); }
.primary-btn:disabled {
  background: var(--surface-2);
  color: var(--text-dim);
  box-shadow: none;
  cursor: not-allowed;
}
.primary-btn.small {
  padding: 12px 24px;
  font-size: 15px;
  margin-top: 12px;
  width: auto;
  min-height: 44px;
}
.btn-label { position: relative; z-index: 1; }
.btn-progress {
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
  transform: translateX(-100%);
}
.primary-btn.running .btn-progress { animation: shimmer 1.5s linear infinite; }
@keyframes shimmer { to { transform: translateX(100%); } }

/* ============================================================
   Chart card
   ============================================================ */
.chart-card {
  background: var(--bg);
  padding: 4px 0;
  margin-top: var(--gap-section);
}
.chart-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  margin-bottom: 8px;
}
.chart-head h2 {
  font-size: var(--fs-chart-title);
  font-weight: 500;
  color: var(--text-dim);
}
.chart-current {
  font-size: var(--fs-chart-current);
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  white-space: nowrap;
}
#chart-canvas {
  width: 100%;
  height: clamp(120px, 22vw, 180px);
  display: block;
  border-radius: var(--radius-sm);
}
.chart-stats {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
  font-size: 13px;
  color: var(--text-dim);
}
.chart-stats b {
  color: var(--text);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* ============================================================
   Result actions (после замера)
   ============================================================ */
.result-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}
.result-actions[hidden] { display: none; }

/* Группа кнопок внутри result-actions (копировать + скачать картинку) */
.result-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.secondary-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px; /* WCAG AAA touch target */
  padding: 8px 16px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-family: inherit;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s, transform 0.05s;
}
.secondary-btn:hover {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent-text);
}
.secondary-btn:active { transform: scale(0.97); }
.secondary-btn.copied {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}
.secondary-btn:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px rgba(252, 63, 29, 0.45);
}

.server-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-mute);
  font-variant-numeric: tabular-nums;
}
.server-badge svg { flex-shrink: 0; opacity: 0.7; }

/* ============================================================
   Dialog
   ============================================================ */
.dialog {
  max-width: 460px;
  width: calc(100% - 32px);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  padding: 24px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.18);
}
.dialog::backdrop { background: rgba(0, 0, 0, 0.4); backdrop-filter: blur(4px); }
.dialog h2 { font-size: 20px; margin-bottom: 12px; }
.dialog p { margin-bottom: 8px; line-height: 1.5; }
.dialog a { color: var(--accent-text); text-decoration: underline; }
.dialog a:hover { color: var(--accent); }
.info-list { list-style: none; padding: 0; margin: 12px 0; }
.info-list li { padding: 8px 0; font-size: 15px; color: var(--text-dim); border-bottom: 1px solid var(--border); }
.info-list li:last-child { border-bottom: none; }
.info-list li b { color: var(--text); }
.dialog .hint { font-size: 13px; color: var(--text-mute); margin-top: 16px; }

/* ============================================================
   Toast
   ============================================================ */
.toast {
  position: fixed;
  bottom: max(24px, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%) translateY(80px);
  background: var(--text);
  color: white;
  border-radius: var(--radius-sm);
  padding: 12px 20px;
  font-size: 14px;
  opacity: 0;
  transition: all 0.3s cubic-bezier(0.22, 1, 0.36, 1);
  pointer-events: none;
  z-index: 1000;
  max-width: calc(100vw - 32px);
  text-align: center;
}
.toast.show { transform: translateX(-50%) translateY(0); opacity: 1; }

/* ============================================================
   Breakpoint: ≥ 480 — small phone landscape / large phone portrait
   Чуть больше воздуха, метрики читаемее.
   ============================================================ */
@media (min-width: 480px) {
  /* .primary-btn без max-width: должна быть на всю ширину внутри полусферы */
}

/* ============================================================
   Breakpoint: ≥ 768 — tablet portrait
   Появляется возможность для 4-колоночных вариаций.
   ============================================================ */
@media (min-width: 768px) {
  .app {
    padding: max(env(safe-area-inset-top), 24px) 32px
             max(env(safe-area-inset-bottom), 32px) 32px;
  }
  #chart-canvas { height: 180px; }
  .result-actions { flex-wrap: nowrap; }
}

/* ============================================================
   Breakpoint: ≥ 1024 — small desktop / tablet landscape
   Кнопка замера — не на всю ширину, а 480px по центру.
   Метрики получают больше паддинга.
   ============================================================ */
@media (min-width: 1024px) {
  .app { padding: max(env(safe-area-inset-top), 16px) 32px
                   max(env(safe-area-inset-bottom), 24px) 32px; }
  .gauge-metrics { padding: 0 80px; gap: 24px; }
  /* .primary-btn без max-width: должна быть на всю ширину внутри полусферы */
  .chart-card { margin-top: 24px; }
}

/* ============================================================
   Breakpoint: ≥ 1440 — large desktop
   Cap максимальной ширины контента.
   ============================================================ */
@media (min-width: 1440px) {
  .app { max-width: 1040px; }
  .gauge-metrics { padding: 0 100px; }
}

/* ============================================================
   Compact-режим для низких десктоп-окон (< 900px по высоте)
   Уменьшаем паддинги и отступы, чтобы вписаться.
   ============================================================ */
@media (min-width: 768px) and (max-height: 899px) {
  .app { padding: 12px 24px 16px 24px; }
  .gauge-svg { max-height: 340px; }
  .gauge-metrics { padding: 0 60px; gap: 14px; }
  .g-metric-value { font-size: 40px; }
  .primary-btn { padding: 10px; min-height: 44px; }
  .chart-card { margin-top: 16px; }
  #chart-canvas { height: 120px; }
}

/* ============================================================
   Breakpoint: < 360 — очень узкие экраны (iPhone SE 1st gen)
   Метрики в 3 колонки не помещаются — переходим на 1×3 вертикально.
   ============================================================ */
@media (max-width: 359px) {
  .gauge-metrics {
    grid-template-columns: 1fr;
    gap: 8px;
    padding: 0 12px;
  }
  .g-metric-value { font-size: 28px; }
}

/* ============================================================
   Landscape: мобильный альбомный режим (≤ 920×500)
   Шкала занимает меньше места, кнопка компактнее.
   ============================================================ */
@media (orientation: landscape) and (max-height: 500px) {
  .app { padding: 6px 16px 8px 16px; }
  .topbar { padding: 0; }
  .icon-btn { width: 32px; height: 32px; min-width: 32px; min-height: 32px; }
  .gauge-svg { max-height: 130px; aspect-ratio: 800 / 460; }
  .gauge-metrics { padding: 0 16px; gap: 8px; }
  .g-metric-value { font-size: 20px; }
  .g-metric-head { font-size: 10px; margin-bottom: 0; }
  .g-metric-unit { font-size: 9px; }
  .gauge-status { font-size: 11px; min-height: 1em; }
  .primary-btn { padding: 6px; font-size: 13px; min-height: 32px; }
  .chart-card { margin-top: 8px; }
  .chart-head { margin-bottom: 4px; }
  #chart-canvas { height: 60px; }
  .chart-stats { font-size: 11px; margin-top: 4px; }
}

/* ============================================================
   History card (v1.6.0)
   ============================================================ */
.history-card {
  margin-top: var(--gap-section);
  padding-top: 16px;
  border-top: 1px solid var(--border);
}
.history-card[hidden] { display: none; }
.history-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}
.history-title {
  font-size: var(--fs-chart-title);
  font-weight: 500;
  color: var(--text-dim);
  margin: 0;
}
.history-deltas {
  display: flex;
  gap: 12px;
  font-size: 13px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}
.history-delta { display: inline-flex; align-items: center; gap: 4px; }
.history-delta--up { color: #2ea44f; }
.history-delta--down { color: #d92d20; }
.history-delta--neutral { color: var(--text-mute); }
.history-canvas {
  width: 100%;
  height: 120px;
  display: block;
  border-radius: var(--radius-sm);
}
.history-legend {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-top: 8px;
  font-size: 12px;
  color: var(--text-mute);
  flex-wrap: wrap;
}
.legend-item { display: inline-flex; align-items: center; gap: 6px; }
.legend-dot {
  width: 10px; height: 10px; border-radius: 50%;
  display: inline-block;
}
.legend-dot--dl { background: #fc3f1d; }
.legend-dot--ul { background: #ff7a3d; }
.history-clear {
  margin-left: auto;
  background: none;
  border: none;
  font-family: inherit;
  font-size: 12px;
  color: var(--text-mute);
  cursor: pointer;
  padding: 6px 10px;
  min-height: 32px;
  border-radius: 8px;
  transition: background 0.15s, color 0.15s;
}
.history-clear:hover { background: var(--surface-2); color: var(--text); }
.history-clear:focus-visible { box-shadow: var(--focus-ring); }

/* ============================================================
   Dark mode (auto по системной теме)
   ============================================================ */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121212;
    --bg-2: #1a1a1a;
    --surface: #1e1e1e;
    --surface-2: #2a2a2a;
    --border: #2f2f2f;
    --text: #ececec;
    --text-dim: #9a9a9a;
    --text-mute: #888888;     /*  5.3:1 на --bg (WCAG AA) */
    --accent-soft: rgba(252, 63, 29, 0.18);
    --shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
    --shadow-btn: 0 4px 14px rgba(252, 63, 29, 0.25);
  }
  .tick { stroke-opacity: 0.85; }
  .icon-btn { color: var(--text-dim); }
  .g-metric-arrow { background: var(--text); color: var(--bg); }
}

/* ============================================================
   Skip link (доступность)
   ============================================================ */
.skip-link {
  position: absolute;
  top: -40px;
  left: 12px;
  padding: 8px 14px;
  background: var(--text);
  color: var(--bg);
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  text-decoration: none;
  z-index: 1001;
  transition: top 0.15s;
}
.skip-link:focus-visible { top: 12px; }

/* ============================================================
   Focus visible (клавиатура)
   ============================================================ */
:focus { outline: none; }
:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
  border-radius: 8px;
}
.primary-btn:focus-visible { box-shadow: var(--focus-ring); }
.icon-btn:focus-visible    { box-shadow: var(--focus-ring); }

/* ============================================================
   prefers-reduced-motion
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
  .primary-btn.running .btn-progress { animation: none !important; }
}

/* ============================================================
   color-scheme — нативные контролы (scrollbar, form)
   ============================================================ */
:root { color-scheme: light dark; }
