* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Helvetica', 'Arial', sans-serif;
  background: linear-gradient(160deg, #1a3a3a 0%, #0d1f2d 50%, #152238 100%);
  min-height: 100vh;
  color: #e8eef2;
  line-height: 1.45;
  padding: 12px;
  font-size: clamp(0.9rem, 2.8vw, 1rem);
}

a {
  color: #7dd3c0;
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

.wrap {
  max-width: 820px;
  margin: 0 auto;
}

.back {
  display: inline-block;
  margin-bottom: 8px;
  font-size: 0.85rem;
}

.dev-banner {
  margin: 0 0 10px;
  padding: 6px 10px;
  font-size: 0.8rem;
  line-height: 1.35;
  color: #f2e8d4;
  background: rgba(180, 100, 30, 0.22);
  border: 1px solid rgba(230, 170, 80, 0.4);
  border-radius: 8px;
}

header.page-head {
  display: grid;
  grid-template-columns: 1fr auto;
  column-gap: 10px;
  row-gap: 8px;
  align-items: start;
  margin-bottom: 12px;
}

header.page-head h1 {
  grid-column: 1;
  grid-row: 1;
  font-size: clamp(1.1rem, 4vw, 1.65rem);
  font-weight: 600;
  letter-spacing: 0.02em;
  margin: 0;
  color: #f0f7f4;
  min-width: 0;
  line-height: 1.25;
}

/* Дети участвуют в сетке шапки: «?» остаётся в 1-й строке справа, панель — 2-й строкой на всю ширину (поток документа) */
.page-help {
  display: contents;
}

.page-help > .help-toggle {
  grid-column: 2;
  grid-row: 1;
  align-self: start;
  justify-self: end;
}

.page-help > .help-content {
  grid-column: 1 / -1;
  grid-row: 2;
}

.help-toggle {
  list-style: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.22);
  background: rgba(255, 255, 255, 0.08);
  color: #c5ebe0;
  font-size: 1.15rem;
  font-weight: 600;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  user-select: none;
  transition: background 0.15s, border-color 0.15s;
}

.help-toggle:hover {
  background: rgba(255, 255, 255, 0.14);
  border-color: rgba(125, 211, 192, 0.45);
}

.help-toggle::-webkit-details-marker {
  display: none;
}

.help-toggle::marker {
  content: '';
}

.page-help[open] .help-toggle {
  background: rgba(125, 211, 192, 0.2);
  border-color: rgba(125, 211, 192, 0.5);
  color: #f0f7f4;
}

.help-toggle:focus-visible {
  outline: 2px solid #7dd3c0;
  outline-offset: 3px;
}

.help-content {
  width: 100%;
  box-sizing: border-box;
  padding: 18px 20px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
  max-height: min(70vh, 28rem);
  overflow-y: auto;
}

.lead {
  color: #a8bcc8;
  font-size: 0.95rem;
  max-width: 65ch;
  margin: 0 0 14px;
}

.intro {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 10px;
  padding: 14px 16px;
}

.intro h2 {
  font-size: 0.95rem;
  font-weight: 600;
  margin-bottom: 10px;
  color: #9ee5d6;
}

.intro ul {
  margin-left: 1.2rem;
  color: #b8cad4;
  font-size: 0.88rem;
}

.intro li + li {
  margin-top: 6px;
}

.intro code {
  background: rgba(0, 0, 0, 0.35);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 0.88em;
  color: #c5ebe0;
}

.help-footnote {
  margin-top: 12px;
  font-size: 0.82rem;
  color: #8fa8b8;
}

#rr-calculator.calculator-section.url_synced {
  background: rgba(10, 22, 32, 0.85);
  border: 1px solid rgba(125, 211, 192, 0.25);
  border-radius: 12px;
  padding: 12px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
}

.calc-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin-bottom: 12px;
}

.share-hint {
  flex: 1 1 100%;
  min-width: 0;
  font-size: 0.88rem;
  color: #9ee5d6;
  line-height: 1.4;
}

.share-hint--error {
  color: #f0b0a8;
}

.legs-add {
  margin-top: 10px;
  padding-top: 2px;
}

.btn {
  border: none;
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 0.88rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s, transform 0.15s;
  min-height: 40px;
}

.btn:active {
  transform: scale(0.98);
}

.btn-primary {
  background: #2d8a78;
  color: #fff;
}

.btn-primary:hover {
  background: #3aa08e;
}

.btn-secondary {
  background: rgba(255, 255, 255, 0.12);
  color: #e8eef2;
}

.btn-secondary:hover {
  background: rgba(255, 255, 255, 0.18);
}

.btn-danger {
  background: rgba(180, 60, 60, 0.35);
  color: #ffc9c9;
}

.btn-danger:hover {
  background: rgba(200, 70, 70, 0.5);
}

.btn-sm {
  padding: 4px 10px;
  font-size: 0.75rem;
  min-height: 34px;
}

#share-hint {
  font-size: 0.85rem;
  color: #9ee5d6;
  min-height: 1.2em;
}

section.initial-point,
section.leg {
  background: rgba(255, 255, 255, 0.04);
  border-radius: 8px;
  padding: 10px 10px;
  margin-bottom: 10px;
  border: 1px solid rgba(255, 255, 255, 0.07);
}

/* Вставленная копия («Разделить»); снимается при первом input в полях промежутка */
section.leg.leg--highlight {
  border-color: rgba(125, 211, 192, 0.55);
  background: rgba(125, 211, 192, 0.1);
  box-shadow: 0 0 0 2px rgba(125, 211, 192, 0.22);
  transition:
    background 0.35s ease,
    border-color 0.35s ease,
    box-shadow 0.35s ease;
}

section.initial-point h2 {
  font-size: 0.95rem;
  margin-bottom: 8px;
  color: #c5ebe0;
  font-weight: 600;
}

/* Заголовок промежутка перенесён в .leg-foot внизу карточки */

.segment h4,
.end-point h4 {
  font-size: 0.62rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: rgba(125, 211, 192, 0.85);
  margin: 0 0 5px;
  font-weight: 600;
  line-height: 1.2;
}

.segment h4:first-child {
  margin-top: 0;
}

.end-point {
  margin-top: 10px;
}

/* Ввод + расчёты под подзаголовком «Перегон» / «Конечная точка» */
.leg-panel {
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  padding: 8px 10px;
  background: rgba(0, 0, 0, 0.14);
}

/* Ввод и расчёты — одна колонка, одинаковый шаг между строками (без двойного gap+margin) */
.field-grid,
.field-grid--dense,
.field-grid--ip,
.field-grid--seg,
.field-grid--ep,
.computed-cluster--seg,
.computed-cluster--end {
  display: flex;
  flex-direction: column;
  gap: 0;
  width: 100%;
  min-width: 0;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.field .label {
  font-size: 0.72rem;
  color: #8fa8b8;
  line-height: 1.2;
}

.inp {
  width: 100%;
  padding: 7px 9px;
  border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, 0.15);
  background: rgba(0, 0, 0, 0.25);
  color: #f0f7f4;
  font-size: 0.95rem;
  min-height: 40px;
}

.inp:focus {
  outline: 2px solid #3aa08e;
  outline-offset: 1px;
}

.inp::placeholder {
  color: #5a7080;
}

/*
 * Вводимые значения — тот же вид, что и .computed-cluster .computed-line (мелкий текст),
 * плюс карандаш. Значение как у <strong> в расчётах.
 */
.field-grid .computed-line--editable {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 6px;
  width: 100%;
  min-width: 0;
  margin: 0;
  padding: 0;
  border: none;
  font-size: 0.7rem;
  line-height: 1.25;
  color: #8fa8b0;
}

.field-grid .computed-line--editable + .computed-line--editable,
.computed-cluster .computed-line + .computed-line {
  margin-top: 1px;
}

/* Подсветка после модалки — inline-стили + rAF в JS (iOS не всегда рисует @keyframes на flex) */

.field-grid .computed-line--editable > input.inp.inp--hidden[type='hidden'] {
  display: none !important;
}

.field-grid .computed-line--editable .field-name {
  flex: 0 1 auto;
  min-width: 0;
  max-width: min(48%, 11rem);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: inherit;
  font-weight: 400;
}

.field-grid .computed-line--editable .field-value-text {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: #e8f4ef;
  font-weight: 600;
  cursor: pointer;
  -webkit-tap-highlight-color: rgba(125, 211, 192, 0.15);
}

.field-grid .computed-line--editable .field-value-text.field-value--empty,
.field-grid .computed-line--editable .field-value--empty {
  color: #6a8494;
  font-weight: 400;
}

.field-grid .computed-line--editable .field-edit-btn {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  /* Как у строки расчёта: высота строки = line-box текста, не фикс. 22px */
  width: 1.25em;
  height: 1.25em;
  min-width: 1.25em;
  margin: 0;
  padding: 0;
  border: none;
  border-radius: 4px;
  background: transparent;
  color: rgba(125, 211, 192, 0.55);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: color 0.15s, background 0.15s;
}

.field-grid .computed-line--editable .field-edit-btn:hover,
.field-grid .computed-line--editable .field-edit-btn:active {
  color: #7dd3c0;
  background: rgba(125, 211, 192, 0.12);
}

.field-grid .computed-line--editable .field-edit-btn:focus-visible {
  outline: 2px solid #3aa08e;
  outline-offset: 1px;
}

.field-grid .computed-line--editable .field-edit-icon {
  display: block;
  width: 0.72em;
  height: 0.72em;
}

.field-grid .computed-line--editable .field-now-btn {
  flex: 0 0 auto;
  margin: 0;
  padding: 0 0.2em;
  border: none;
  border-radius: 4px;
  background: transparent;
  color: rgba(125, 211, 192, 0.75);
  font: inherit;
  font-size: 0.68em;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: lowercase;
  line-height: 1.25;
  cursor: pointer;
  -webkit-tap-highlight-color: rgba(125, 211, 192, 0.15);
}

.field-grid .computed-line--editable .field-now-btn:hover,
.field-grid .computed-line--editable .field-now-btn:active {
  color: #7dd3c0;
  background: rgba(125, 211, 192, 0.12);
}

.field-grid .computed-line--editable .field-now-btn:focus-visible {
  outline: 2px solid #3aa08e;
  outline-offset: 1px;
}

/* Модалка редактирования поля */
.rrd-modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 0;
  box-sizing: border-box;
}

.rrd-modal[hidden] {
  display: none !important;
}

.rrd-modal__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
}

.rrd-modal__panel {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 440px;
  max-height: min(90vh, 28rem);
  overflow: auto;
  padding: 12px 14px 14px;
  border-radius: 14px 14px 0 0;
  background: linear-gradient(180deg, #1a3040 0%, #152238 100%);
  border: 1px solid rgba(125, 211, 192, 0.28);
  box-shadow: 0 -12px 48px rgba(0, 0, 0, 0.5);
}

.rrd-modal__title {
  font-size: 0.88rem;
  font-weight: 600;
  margin: 0 0 8px;
  color: #c5ebe0;
  line-height: 1.25;
}

.rrd-modal__input {
  width: 100%;
  padding: 7px 9px;
  margin-bottom: 16px;
  border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, 0.15);
  background: rgba(0, 0, 0, 0.25);
  color: #f0f7f4;
  font-size: 0.95rem;
  min-height: 38px;
  box-sizing: border-box;
}

.rrd-modal__input:focus {
  outline: 2px solid #3aa08e;
  outline-offset: 1px;
}

.rrd-modal__quick {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: -8px;
  margin-bottom: 12px;
}

.rrd-modal__quick[hidden] {
  display: none !important;
}

.rrd-modal__quick-btn {
  min-width: 2.5rem;
  min-height: 34px;
  padding: 4px 8px;
  font-size: 0.8rem;
}

.rrd-modal__actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: flex-end;
}

.rrd-modal__actions .btn {
  min-width: 100px;
}

body.rrd-modal-open {
  overflow: hidden;
}

@media (min-width: 520px) {
  .rrd-modal {
    align-items: center;
    padding: 16px;
  }

  .rrd-modal__panel {
    border-radius: 14px;
  }
}

/* Без margin для строк ввода (computed-line + computed-line--editable) — иначе лишний шаг между полями */
.computed-line:not(.computed-line--editable) {
  font-size: 0.88rem;
  color: #a8bcc8;
  margin-top: 8px;
}

.computed-cluster {
  margin-top: 4px;
  padding-top: 4px;
  border-top: 1px solid rgba(255, 255, 255, 0.06);
}

.computed-cluster--tight {
  margin-top: 2px;
  padding-top: 2px;
  border-top: none;
}

.computed-cluster .computed-line {
  font-size: 0.7rem;
  line-height: 1.25;
  color: #8fa8b0;
  margin: 0;
  padding: 0;
}

.computed-line strong {
  color: #e8f4ef;
  font-weight: 600;
}

.err {
  color: #f0a8a8;
  font-weight: 500;
}

.leg-foot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 6px;
  flex-wrap: nowrap;
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
}

.leg-actions {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.leg-foot h3 {
  margin: 0;
  font-size: 0.62rem;
  font-weight: 600;
  line-height: 1.2;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: rgba(125, 211, 192, 0.88);
  flex: 1;
  min-width: 0;
}

.leg-foot .btn-sm {
  font-size: 0.62rem;
  min-height: 28px;
  padding: 3px 8px;
}

footer.page-foot {
  margin-top: 16px;
  padding-top: 10px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  font-size: 0.75rem;
  line-height: 1.35;
  color: #7a909e;
}

@media (min-width: 900px) {
  body {
    padding: 16px;
  }

  #rr-calculator.calculator-section.url_synced {
    padding: 18px;
  }

  .dev-banner {
    margin-bottom: 14px;
    padding: 8px 12px;
    font-size: 0.85rem;
  }
}

@media (max-width: 520px) {
  .calc-actions {
    flex-direction: column;
    align-items: stretch;
  }

  .btn {
    width: 100%;
  }

  .leg-foot .btn-sm {
    width: auto;
  }

  .rrd-modal__quick .btn {
    width: auto;
  }

  .leg-foot {
    flex-wrap: wrap;
  }
}
