/* style.css — стилі форми Mini App
 * Використовує CSS-змінні Telegram WebApp:
 *   --tg-theme-bg-color, --tg-theme-text-color, --tg-theme-hint-color,
 *   --tg-theme-button-color, --tg-theme-button-text-color, --tg-theme-link-color
 * Це робить форму автоматично адаптивною до dark/light теми Telegram.
 */

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  /* Fallback-кольори, якщо CSS-змінні Telegram недоступні (відкрито в звичайному браузері) */
  --bg: var(--tg-theme-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #1d1d1f);
  --hint: var(--tg-theme-hint-color, #999999);
  --link: var(--tg-theme-link-color, #2481cc);
  --btn-bg: var(--tg-theme-button-color, #2481cc);
  --btn-text: var(--tg-theme-button-text-color, #ffffff);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #f4f4f5);
  --section-bg: var(--tg-theme-section-bg-color, #ffffff);
  --section-header: var(--tg-theme-section-header-text-color, #6e6e72);
  --accent: var(--tg-theme-accent-text-color, #2481cc);
  --destructive: var(--tg-theme-destructive-text-color, #d93535);

  /* Власні */
  --border: rgba(0, 0, 0, 0.08);
  --disabled-bg: rgba(128, 128, 128, 0.1);
  --disabled-text: rgba(128, 128, 128, 0.6);
  --success: #34c759;
  --warning: #ff9500;
}

@media (prefers-color-scheme: dark) {
  :root {
    --border: rgba(255, 255, 255, 0.1);
    --disabled-bg: rgba(255, 255, 255, 0.05);
    --disabled-text: rgba(255, 255, 255, 0.3);
  }
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.4;
  padding: 12px 16px 80px;
  min-height: 100vh;
}

h1, h2, h3 {
  font-weight: 600;
}

h1 {
  font-size: 18px;
  margin-bottom: 4px;
}

h2 {
  font-size: 14px;
  color: var(--section-header);
  text-transform: uppercase;
  letter-spacing: 0.4px;
  margin: 16px 0 8px;
  padding-left: 4px;
}

.subtitle {
  color: var(--hint);
  font-size: 13px;
  margin-bottom: 16px;
}

/* ============================================================
   Секція форми (картка)
   ============================================================ */
.section {
  background: var(--section-bg);
  border-radius: 12px;
  padding: 12px 14px;
  margin-bottom: 8px;
  border: 1px solid var(--border);
}

.section.disabled-section {
  background: var(--disabled-bg);
  border-color: transparent;
}

.section.disabled-section h2 {
  color: var(--disabled-text);
}

/* ============================================================
   Поля форми
   ============================================================ */
.field {
  display: flex;
  flex-direction: column;
  margin-bottom: 12px;
}
.field:last-child { margin-bottom: 0; }

.field-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.field-label {
  font-size: 13px;
  color: var(--section-header);
  margin-bottom: 4px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.field-label .ref {
  color: var(--hint);
  font-size: 11px;
  font-weight: normal;
}

.info-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--hint);
  color: var(--bg);
  font-size: 10px;
  font-weight: bold;
  cursor: pointer;
  user-select: none;
  flex-shrink: 0;
}

.info-icon:hover {
  opacity: 0.8;
}

input[type="text"],
input[type="number"],
select {
  font-family: inherit;
  font-size: 16px; /* 16px — щоб iOS не зумив при фокусі */
  background: var(--secondary-bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  width: 100%;
  appearance: none;
  -webkit-appearance: none;
}

input:focus,
select:focus {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
  border-color: transparent;
}

input:disabled,
select:disabled {
  background: var(--disabled-bg);
  color: var(--disabled-text);
  cursor: not-allowed;
}

select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%23999' d='M6 8L0 0h12z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 32px;
}

select:disabled {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%23bbb' d='M6 8L0 0h12z'/%3E%3C/svg%3E");
}

/* ============================================================
   Checkbox як toggle-блок
   ============================================================ */
.toggle-field {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 0;
}

.toggle-field.disabled {
  opacity: 0.5;
}

.toggle-label {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1;
}

.toggle-label .main {
  font-size: 15px;
}

.toggle-label .ref {
  color: var(--hint);
  font-size: 11px;
}

.switch {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 26px;
  flex-shrink: 0;
}
.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}
.slider {
  position: absolute;
  cursor: pointer;
  inset: 0;
  background: var(--hint);
  border-radius: 26px;
  transition: 0.2s;
}
.slider:before {
  content: "";
  position: absolute;
  height: 22px;
  width: 22px;
  left: 2px;
  bottom: 2px;
  background: white;
  border-radius: 50%;
  transition: 0.2s;
}
input:checked + .slider {
  background: var(--btn-bg);
}
input:checked + .slider:before {
  transform: translateX(18px);
}
input:disabled + .slider {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ============================================================
   Сегменти маршруту (картки)
   ============================================================ */
.segments-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.segment-card {
  background: var(--secondary-bg);
  border-radius: 10px;
  padding: 10px 12px;
  position: relative;
}

.segment-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.segment-card-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--section-header);
}

.segment-remove-btn {
  background: transparent;
  color: var(--destructive);
  border: none;
  font-size: 18px;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 4px;
  line-height: 1;
}
.segment-remove-btn:hover {
  background: rgba(217, 53, 53, 0.1);
}

.add-segment-btn {
  width: 100%;
  background: transparent;
  border: 1.5px dashed var(--accent);
  color: var(--accent);
  font-size: 14px;
  font-weight: 500;
  padding: 12px;
  border-radius: 10px;
  cursor: pointer;
  margin-top: 4px;
}
.add-segment-btn:hover {
  background: rgba(36, 129, 204, 0.05);
}

.segment-info-line {
  font-size: 12px;
  color: var(--hint);
  margin-top: 6px;
  font-style: italic;
}

/* ============================================================
   Кнопка розрахунку (велика)
   ============================================================ */
.btn-calculate {
  width: 100%;
  background: var(--btn-bg);
  color: var(--btn-text);
  border: none;
  border-radius: 12px;
  padding: 16px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  margin-top: 16px;
}
.btn-calculate:hover { opacity: 0.9; }
.btn-calculate:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ============================================================
   Блок результату
   ============================================================ */
.result-block {
  background: var(--section-bg);
  border-radius: 12px;
  padding: 14px 16px;
  margin-top: 16px;
  border: 1px solid var(--border);
}

.result-block h2 {
  margin-top: 0;
}

.result-content {
  font-size: 14px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}

.result-content b {
  font-weight: 600;
}

.result-content i {
  color: var(--hint);
}

.result-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 16px;
}

.btn {
  flex: 1;
  min-width: 100px;
  border: none;
  border-radius: 10px;
  padding: 12px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
}

.btn-primary {
  background: var(--btn-bg);
  color: var(--btn-text);
}

.btn-secondary {
  background: var(--secondary-bg);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-secondary:hover {
  background: var(--disabled-bg);
}

/* ============================================================
   Помилка валідації
   ============================================================ */
.field-error {
  color: var(--destructive);
  font-size: 12px;
  margin-top: 4px;
}

input.has-error,
select.has-error {
  border-color: var(--destructive);
  outline: 2px solid rgba(217, 53, 53, 0.3);
  outline-offset: -2px;
}

/* ============================================================
   Popup (для tooltip)
   ============================================================ */
.popup-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
}

.popup-overlay.open {
  display: flex;
}

.popup {
  background: var(--section-bg);
  color: var(--text);
  border-radius: 12px;
  padding: 16px 20px;
  max-width: 400px;
  width: 100%;
}

.popup h3 {
  font-size: 15px;
  margin-bottom: 8px;
}

.popup p {
  font-size: 14px;
  color: var(--hint);
  margin-bottom: 12px;
}

.popup-close {
  background: var(--btn-bg);
  color: var(--btn-text);
  border: none;
  border-radius: 8px;
  padding: 8px 16px;
  font-size: 14px;
  cursor: pointer;
  width: 100%;
}

/* ============================================================
   Loading-стан
   ============================================================ */
.loading {
  text-align: center;
  padding: 40px 20px;
  color: var(--hint);
  font-size: 14px;
}

/* ============================================================
   Маленькі утилітки
   ============================================================ */
.muted { color: var(--hint); }
.divider {
  height: 1px;
  background: var(--border);
  margin: 12px 0;
}

.hint-text {
  font-size: 12px;
  color: var(--hint);
  margin-top: 4px;
}
