/* ------------------------------------------------------------------ *
 * Digitakt II LFO — визуальный справочник.
 * Мобильный приоритет: iPhone внутри Telegram (WKWebView).
 * ------------------------------------------------------------------ */

:root {
  --bg: #0b1117;
  --text: #f3f7ff;
  --text-muted: #a9bbd4;
  --surface-border: #27364a;
  --accent: #5aa2ff;

  /* Поверхности выводятся из --bg/--text, чтобы светлая тема Telegram
     не давала чёрное на чёрном. */
  --surface-1: color-mix(in srgb, var(--text) 6%, var(--bg));
  --surface-2: color-mix(in srgb, var(--text) 11%, var(--bg));
  --surface-3: color-mix(in srgb, var(--accent) 22%, var(--bg));
  --on-accent: color-mix(in srgb, var(--bg) 88%, var(--text));
  --press: color-mix(in srgb, var(--accent) 30%, transparent);

  --radius: 14px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Arial, sans-serif;
}

/* Запасной вариант для движков без color-mix (iOS < 16.2). */
@supports not (color: color-mix(in srgb, red, blue)) {
  :root {
    --surface-1: #101a24;
    --surface-2: #0e1620;
    --surface-3: #152f47;
    --on-accent: #07111d;
    --press: rgba(90, 162, 255, 0.3);
  }
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

/* Страница не скроллится вообще: единственная прокручиваемая область —
   таблица периодов. Всё остальное всегда на экране. */
body {
  margin: 0;
  height: 100svh;
  height: var(--tg-vh, 100svh);
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
}

.app {
  height: 100%;
  max-width: 900px;
  margin: 0 auto;
  display: grid;
  /* шапка · график · окно · параметры · результат · таблица · статус */
  grid-template-rows: auto auto auto auto auto minmax(0, 1fr) auto;
  gap: 6px;
  min-height: 0;
  padding:
    max(6px, env(safe-area-inset-top))
    max(8px, env(safe-area-inset-right))
    calc(6px + env(safe-area-inset-bottom, 0px))
    max(8px, env(safe-area-inset-left));
}

/* ---------------------------------------------------------- header -- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 0 2px;
}

.topbar h1 {
  margin: 0;
  font-size: 0.95rem;
  line-height: 1.2;
  letter-spacing: 0.01em;
}

.topbar-btn {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 1px solid var(--surface-border);
  background: var(--surface-1);
  color: var(--text-muted);
  font-size: 0.85rem;
  line-height: 1;
  flex: 0 0 auto;
}

.badge {
  border: 1px solid var(--surface-border);
  border-radius: 999px;
  padding: 3px 9px;
  font-size: 0.68rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* ------------------------------------------- компактные параметры -- */

.par-row {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 5px;
}

.par {
  display: grid;
  justify-items: center;
  align-content: center;
  gap: 1px;
  min-height: 52px;
  padding: 4px 2px;
  border: 1px solid var(--surface-border);
  border-radius: 10px;
  background: var(--surface-1);
  color: var(--text);
  -webkit-tap-highlight-color: var(--press);
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-appearance: none;
  appearance: none;
  cursor: pointer;
  touch-action: manipulation;
  transition: background-color 0.1s ease, transform 0.1s ease;
}

.par:active,
.par.is-pressed {
  background: var(--accent);
  border-color: var(--accent);
  transform: scale(0.95);
  transition: none;
}

.par:active .par-name,
.par.is-pressed .par-name,
.par:active .par-val,
.par.is-pressed .par-val,
.par:active .par-big,
.par.is-pressed .par-big { color: var(--on-accent); }

.par:active .glyph-wave,
.par.is-pressed .glyph-wave { stroke: var(--on-accent); }

.par-name {
  font-size: 0.6rem;
  letter-spacing: 0.07em;
  color: var(--text-muted);
}

.par-icon { display: block; width: 100%; line-height: 0; }
.par-icon svg { width: 100%; height: auto; display: block; }

.par-val {
  font-size: 0.62rem;
  font-weight: 500;
  color: var(--text);
}

.par-big {
  font-size: 1.02rem;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  line-height: 1.15;
}

/* ------------------------------------------------------- результат -- */

.result {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  width: 100%;
  padding: 6px 10px;
  border: 1px solid var(--surface-border);
  border-radius: 10px;
  background: var(--surface-1);
  color: var(--text);
  text-align: left;
  -webkit-tap-highlight-color: var(--press);
  -webkit-appearance: none;
  appearance: none;
  cursor: pointer;
  touch-action: manipulation;
}

.result:active, .result.is-pressed { background: var(--surface-2); }

.result-addr {
  font-size: 0.76rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.result-main { display: flex; align-items: baseline; gap: 5px; }

.result-main strong {
  font-size: 1.25rem;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}

.result-main span { font-size: 0.68rem; color: var(--text-muted); }

.result.is-audio .result-main strong { color: var(--warn); }

.result-detail {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  margin-top: 8px;
  font-size: 0.8rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

/* ---------------------------------------------------------- шторки -- */

.scrim {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 40;
  border: 0;
  padding: 0;
  width: 100%;
}

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 50;
  max-height: 82svh;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--surface-2);
  border-top: 1px solid var(--surface-border);
  border-radius: 16px 16px 0 0;
  padding: 12px 12px calc(16px + env(safe-area-inset-bottom, 0px));
  animation: sheet-up 0.16s ease-out;
}

@keyframes sheet-up { from { transform: translateY(14px); } to { transform: none; } }

@media (prefers-reduced-motion: reduce) {
  .sheet { animation: none; }
}

.sheet-head {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}

.sheet-head h2 {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 500;
  flex: 1 1 auto;
}

.sheet-close {
  width: 32px;
  height: 32px;
  flex: 0 0 auto;
  border-radius: 50%;
  border: 1px solid var(--surface-border);
  background: var(--surface-1);
  color: var(--text-muted);
  font-size: 0.8rem;
  line-height: 1;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
  touch-action: manipulation;
}

.sheet-note {
  margin: 8px 0 0;
  font-size: 0.8rem;
  line-height: 1.45;
  color: var(--text-muted);
}

.sheet-note.dim { font-size: 0.74rem; opacity: 0.85; }

/* ---------------------------------------------------------- panels -- */

.panel {
  background: var(--surface-1);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius);
  padding: 10px;
  min-width: 0;
}

.panel h2 {
  margin: 0 0 8px;
  font-size: 0.95rem;
  line-height: 1.3;
}

.panel-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.panel-title h2 { margin: 0; }

/* ------------------------------------------------------- feedback --- *
 * Каждый интерактивный элемент обязан показывать нажатие.
 * :active на iOS снимается при малейшем движении пальца, поэтому
 * дублируем классом .is-pressed из JS.                                  */

.btn,
.chip,
.icon-btn,
.table-cell {
  -webkit-tap-highlight-color: var(--press);
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-appearance: none;
  appearance: none;
  cursor: pointer;
  touch-action: manipulation;
  transition: background-color 0.1s ease, transform 0.1s ease, border-color 0.1s ease;
}

.btn:active,
.chip:active,
.icon-btn:active,
.table-cell:active,
.btn.is-pressed,
.chip.is-pressed,
.icon-btn.is-pressed,
.table-cell.is-pressed {
  background: var(--accent);
  color: var(--on-accent);
  border-color: var(--accent);
  transform: scale(0.94);
  transition: none;
}

.btn:focus-visible,
.chip:focus-visible,
.icon-btn:focus-visible,
.table-cell:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.btn {
  min-height: 44px;
  padding: 8px 12px;
  border: 1px solid var(--surface-border);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text);
  font-family: inherit;
  font-size: 0.9rem;
}

.btn-sign {
  min-height: 36px;
  padding: 4px 12px;
  font-variant-numeric: tabular-nums;
  font-weight: 700;
}

.btn-sign[aria-pressed="true"] {
  background: var(--surface-3);
  border-color: var(--accent);
  color: var(--text);
}

/* ----------------------------------------------------------- chips -- */

.chips-label {
  margin: 8px 0 5px;
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.chips {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  scroll-snap-type: x proximity;
  padding: 2px 0 6px;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.chips::-webkit-scrollbar { display: none; }

.chip {
  flex: 0 0 auto;
  min-width: 52px;
  min-height: 46px;
  padding: 0 10px;
  border: 1px solid var(--surface-border);
  border-radius: 11px;
  background: var(--surface-2);
  color: var(--text);
  font-family: inherit;
  font-size: 0.92rem;
  font-variant-numeric: tabular-nums;
  scroll-snap-align: center;
}

.chip[aria-checked="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
  font-weight: 700;
}

/* ---------------------------------------------------------- result -- */

.result {
  margin: 4px 0 10px;
  padding: 10px 12px;
  border: 1px solid var(--surface-border);
  border-radius: 12px;
  background: var(--surface-2);
}

.result-main {
  display: flex;
  align-items: baseline;
  gap: 8px;
  flex-wrap: wrap;
}

.result-main strong {
  font-size: 1.75rem;
  line-height: 1.05;
  font-variant-numeric: tabular-nums;
}

.result-main span {
  font-size: 0.82rem;
  color: var(--text-muted);
}

.result-sub {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 14px;
  margin-top: 6px;
  font-size: 0.84rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.result-bars {
  margin: 6px 0 0;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.result.is-audio {
  border-color: var(--accent);
}

/* ---------------------------------------------------------- fields -- */

.field {
  display: grid;
  gap: 6px;
  margin-top: 10px;
}

.field-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  font-size: 0.82rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.field-head strong {
  color: var(--text);
  font-size: 1.05rem;
  font-variant-numeric: tabular-nums;
  text-transform: none;
  letter-spacing: 0;
}

.stepper {
  display: grid;
  grid-template-columns: 52px 1fr 52px;
  gap: 8px;
  align-items: center;
}

.stepper .step {
  font-size: 24px;
  line-height: 1;
  padding: 0;
  min-height: 46px;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  min-height: 44px;
  background: transparent;
  margin: 0;
  touch-action: pan-y;
}

input[type="range"]::-webkit-slider-runnable-track {
  height: 6px;
  border-radius: 3px;
  background: var(--surface-1);
  border: 1px solid var(--surface-border);
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 28px;
  height: 28px;
  margin-top: -12px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--bg);
}

input[type="range"]::-moz-range-track {
  height: 6px;
  border-radius: 3px;
  background: var(--surface-1);
  border: 1px solid var(--surface-border);
}

input[type="range"]::-moz-range-thumb {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--bg);
}

.field.is-disabled {
  opacity: 0.45;
}

/* ----------------------------------------------------------- icons -- */

.icon-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
}

.icon-btn {
  display: grid;
  align-content: center;
  justify-items: center;
  row-gap: 3px;
  min-height: 62px;
  padding: 7px 4px;
  border: 1px solid var(--surface-border);
  border-radius: 11px;
  background: var(--surface-2);
  color: var(--text);
  font-family: inherit;
}

.icon-btn[aria-checked="true"] {
  background: var(--surface-3);
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
}

.icon-btn svg {
  width: 100%;
  height: auto;
  pointer-events: none;
}

.icon-caption {
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.icon-btn[aria-checked="true"] .icon-caption { color: var(--text); }

/* Глифы в стиле иллюстраций Elektron: толстая линия, тонкая ось. */
.glyph-wave {
  fill: none;
  stroke: currentColor;
  stroke-width: 3;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.glyph-bg {
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  opacity: 0.4;
  stroke-dasharray: 2 3;
}

.glyph-axis {
  fill: none;
  stroke: currentColor;
  stroke-width: 1;
  opacity: 0.35;
}

.glyph-trig-line {
  fill: none;
  stroke: currentColor;
  stroke-width: 1;
  opacity: 0.55;
}

.glyph-trig-head {
  fill: currentColor;
  opacity: 0.75;
}

/* ------------------------------------------------------------ help -- */

.help-text {
  margin: 8px 0 0;
  font-size: 0.83rem;
  line-height: 1.45;
  color: var(--text-muted);
}

.help-note {
  margin: 6px 0 0;
  font-size: 0.75rem;
  line-height: 1.4;
  color: var(--text-muted);
  opacity: 0.85;
}

.help {
  margin-top: 10px;
  border-top: 1px solid var(--surface-border);
}

.help summary {
  list-style: none;
  display: flex;
  align-items: center;
  min-height: 44px;
  font-size: 0.85rem;
  color: var(--text-muted);
  cursor: pointer;
  -webkit-tap-highlight-color: var(--press);
}

.help summary::-webkit-details-marker { display: none; }

.help summary::after {
  content: "▾";
  margin-left: auto;
  transition: transform 0.15s ease;
}

.help[open] summary::after { transform: rotate(180deg); }

/* ----------------------------------------------------------- table -- */

/* Единственная прокручиваемая область приложения — и вбок, и вниз. */
.table-wrap {
  overflow: auto;
  min-height: 0;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  border: 1px solid var(--surface-border);
  border-radius: 10px;
}

#speed-table {
  border-collapse: separate;
  border-spacing: 0;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

#speed-table th,
#speed-table td {
  padding: 0;
  margin: 0;
  text-align: center;
  white-space: nowrap;
  box-shadow: inset -1px -1px 0 var(--surface-border);
}

#speed-table th {
  background: var(--surface-1);
  font-size: 0.74rem;
  color: var(--text-muted);
  min-width: 44px;
  height: 34px;
}

#speed-table thead th {
  position: sticky;
  top: 0;
  z-index: 1;
}

#speed-table tbody th {
  position: sticky;
  left: 0;
  z-index: 2;
}

/* Угловая ячейка липнет по обеим осям, иначе при диагональной прокрутке
   она уезжает и колонка SPD остаётся без заголовка. */
#speed-table thead th:first-child {
  left: 0;
  z-index: 3;
}

.table-cell {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-width: 46px;
  min-height: 44px;
  padding: 2px;
  border: 0;
  border-radius: 0;
  background: var(--surface-2);
  color: var(--text);
  font-family: inherit;
  font-size: 0.78rem;
  font-variant-numeric: tabular-nums;
}

.table-cell[aria-checked="true"] {
  background: var(--accent);
  color: var(--on-accent);
  font-weight: 700;
  box-shadow: inset 0 0 0 2px var(--text);
}

.sub-title {
  margin: 14px 0 6px;
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
  font-weight: 600;
}

/* ----------------------------------------------------------- chart -- */

.window-switch {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
}

.win {
  display: grid;
  gap: 1px;
  justify-items: center;
  align-content: center;
  min-height: 44px;
  padding: 4px 2px;
  border: 1px solid var(--surface-border);
  border-radius: 10px;
  background: var(--surface-1);
  color: var(--text);
  -webkit-tap-highlight-color: var(--press);
  -webkit-user-select: none;
  user-select: none;
  -webkit-appearance: none;
  appearance: none;
  cursor: pointer;
  touch-action: manipulation;
  transition: background-color 0.1s ease, transform 0.1s ease;
}

.win:active, .win.is-pressed { transform: scale(0.96); transition: none; }

.win-name { font-size: 0.8rem; font-weight: 500; }

.win-sub {
  font-size: 0.64rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.win[aria-checked="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
}

.win[aria-checked="true"] .win-sub { color: var(--on-accent); opacity: 0.8; }

.chart-wrap {
  background: var(--surface-2);
  border: 1px solid var(--surface-border);
  border-radius: 10px;
  overflow: hidden;
}

.chart {
  display: block;
  width: 100%;
  height: clamp(150px, 27svh, 260px);
}

.dt2-gridline { stroke: var(--surface-border); stroke-width: 1; }
.dt2-zero { stroke: var(--text-muted); stroke-width: 1; opacity: 0.7; }

.dt2-shape {
  fill: none;
  stroke: var(--text-muted);
  stroke-width: 1.5;
  stroke-dasharray: 3 4;
  opacity: 0.55;
  stroke-linejoin: round;
}

.dt2-output {
  fill: none;
  stroke: var(--accent);
  stroke-width: 3;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.dt2-trigger { stroke: var(--text-muted); stroke-width: 1; stroke-dasharray: 3 3; opacity: 0.8; }
.dt2-trigger-head { fill: var(--text-muted); opacity: 0.9; }
.dt2-axis-text { fill: var(--text-muted); font-size: 12px; font-family: var(--font); }
.dt2-label { fill: var(--text-muted); font-size: 11px; font-family: var(--font); }

.legend {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 10px;
  font-size: 0.74rem;
  color: var(--text-muted);
}

.legend .line {
  width: 14px;
  height: 3px;
  display: inline-block;
  vertical-align: middle;
  margin-right: 5px;
  border-radius: 2px;
}

.line-main { background: var(--accent); }
.line-shape { background: var(--text-muted); }

.status {
  display: block;
  margin-top: 8px;
  font-size: 0.76rem;
  color: var(--text-muted);
}

.status.is-error { color: #ff8a8a; }

.app-footer {
  padding: 4px 2px 0;
  font-size: 0.68rem;
  line-height: 1.5;
  color: var(--text-muted);
  opacity: 0.7;
}

/* --------------------------------------------------------- desktop -- */

@media (min-width: 760px) {
  .app-shell { gap: 14px; }
  .panel { padding: 14px; }
  .app-header h1 { font-size: 1.3rem; }
  .icon-grid { grid-template-columns: repeat(7, minmax(0, 1fr)); }
  .chart { height: 380px; }
  .chart-wrap {
    width: 100%;
    margin-inline: 0;
    border: 1px solid var(--surface-border);
    border-radius: 12px;
  }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
