/* Telegram injects --tg-theme-* variables for the user's theme. Layout follows
   Telegram's own settings screens: grey page, rounded section cards, small
   uppercase group titles. Fallbacks apply when opened in a normal browser. */
:root {
  --page: var(--tg-theme-secondary-bg-color, #efeff4);
  --card: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, #ffffff));
  --field: var(--tg-theme-secondary-bg-color, #f2f2f7);
  --text: var(--tg-theme-text-color, #111418);
  --hint: var(--tg-theme-hint-color, #8a8f98);
  --subtitle: var(--tg-theme-subtitle-text-color, var(--hint));
  --group-title: var(--tg-theme-section-header-text-color, var(--hint));
  --link: var(--tg-theme-link-color, #2481cc);
  --accent: var(--tg-theme-button-color, #2481cc);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: var(--tg-theme-destructive-text-color, #e5484d);
  --line: color-mix(in srgb, var(--hint) 22%, transparent);
  --good: #30a46c;
  --warn: #d4870b;
  --radius: 16px;
  /* Motion: one easing curve and three durations everywhere. */
  --ease: cubic-bezier(.2, .8, .2, 1);
  --fast: 140ms;
  --base: 240ms;
  --slow: 380ms;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-tg]) {
    --page: #101418; --card: #1b2026; --field: #252b33; --text: #eceef1; --hint: #8b939e; --link: #6ab3f3; --accent: #3e8ed0;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
/* `hidden` must win over any display rule (e.g. .field { display: block }). */
[hidden] { display: none !important; }
html, body { margin: 0; background: var(--page); color: var(--text); }
body { font: 16px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif; -webkit-text-size-adjust: 100%; }
main { max-width: 600px; margin: 0 auto; padding: 16px 16px calc(28px + env(safe-area-inset-bottom)); }
h1 { font-size: 22px; line-height: 1.2; margin: 0 0 4px; letter-spacing: -0.01em; }
h2 { font-size: 18px; margin: 0 0 10px; }
h3 { font-size: 15px; margin: 16px 0 6px; }
p { margin: 0 0 8px; }
a { color: var(--link); }
em { font-style: normal; font-weight: 400; color: var(--hint); }
.muted { color: var(--hint); }
.hint { color: var(--hint); font-size: 13px; }

/* ---------- hero ---------- */
.hero { display: flex; gap: 14px; align-items: center; padding: 6px 2px 12px; }
.hero p { color: var(--subtitle); font-size: 14px; margin: 0; }
/* Brand mark (public/logo.svg) carries its own colours, so it reads on every Telegram theme. */
.logo { flex: 0 0 56px; width: 56px; height: 56px; border-radius: 15px; box-shadow: 0 6px 18px rgba(14, 42, 71, .28); }
.covers { list-style: none; display: flex; flex-wrap: wrap; gap: 6px; margin: 0 0 6px; padding: 0; }
.covers li { font-size: 12.5px; padding: 5px 10px; border-radius: 999px; background: var(--card); color: var(--subtitle); }

/* ---------- grouped form ---------- */
.group-title {
  font-size: 13px; font-weight: 500; text-transform: uppercase; letter-spacing: 0.04em;
  color: var(--group-title); margin: 22px 14px 7px;
}
.group { background: var(--card); border-radius: var(--radius); padding: 4px 14px; }
.group-foot { font-size: 13px; color: var(--hint); margin: 7px 14px 0; }
.field { display: block; padding: 11px 0; }
.group > .field + .field, .group > .field-row + .field, .group > .field + .field-row { border-top: 1px solid var(--line); }
.field-row { display: flex; gap: 12px; }
.field-row > .field { flex: 1; min-width: 0; }
.label { display: block; font-size: 14px; font-weight: 600; margin-bottom: 7px; }
.label em { font-size: 12.5px; margin-inline-start: 4px; }

input, select, textarea {
  display: block; width: 100%; padding: 12px 14px; font: inherit; color: var(--text);
  background: var(--field); border: 1.5px solid transparent; border-radius: 12px; outline: none;
  transition: border-color var(--base) var(--ease), background-color var(--base) var(--ease), box-shadow var(--base) var(--ease);
}
input::placeholder, textarea::placeholder { color: var(--hint); opacity: 0.7; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 18%, transparent); }
input[aria-invalid="true"] { border-color: var(--danger); background: color-mix(in srgb, var(--danger) 8%, var(--field)); }
textarea { resize: vertical; min-height: 84px; }
/* No spinner arrows on number fields. */
input[type=number] { -moz-appearance: textfield; appearance: textfield; }
input[type=number]::-webkit-inner-spin-button, input[type=number]::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
select {
  -webkit-appearance: none; appearance: none; padding-inline-end: 38px; cursor: pointer;
  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 d='M1 1.5l5 5 5-5' fill='none' stroke='%238a8f98' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 14px center;
}

.affix { position: relative; display: block; }
.affix input { padding-inline-end: 44px; }
.affix > span { position: absolute; inset-inline-end: 14px; top: 50%; transform: translateY(-50%); color: var(--hint); pointer-events: none; }
.price { display: flex; gap: 8px; }
.price input { flex: 1; min-width: 0; }
.price select { flex: 0 0 96px; }

/* Chips = radio buttons styled as pills. */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chips.stacked { flex-direction: column; padding: 10px 0; }
.chip { position: relative; cursor: pointer; }
.chip input { position: absolute; opacity: 0; inset: 0; margin: 0; width: 100%; height: 100%; cursor: pointer; }
.chip span {
  display: flex; align-items: center; min-height: 42px; padding: 9px 16px; border-radius: 999px; font-size: 14.5px; font-weight: 500;
  background: var(--field); border: 1.5px solid transparent;
  transition: background-color var(--base) var(--ease), border-color var(--base) var(--ease), transform var(--fast) var(--ease);
}
.chip:active span { transform: scale(.96); }
.chips.stacked .chip span { border-radius: 12px; padding: 12px 14px; min-height: 48px; }
.chip input:checked + span {
  background: color-mix(in srgb, var(--accent) 16%, var(--field)); border-color: var(--accent); color: var(--text);
}
.chip input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

.error {
  margin: 16px 0 0; padding: 12px 14px; border-radius: 12px; font-size: 14px; font-weight: 500;
  color: var(--danger); background: color-mix(in srgb, var(--danger) 12%, var(--card));
}
.submit-bar {
  position: sticky; bottom: 0; margin: 18px -16px 0; padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--page) 70%, transparent);
}
button {
  width: 100%; padding: 15px; font: inherit; font-weight: 600; border: 0; border-radius: 14px;
  background: var(--accent); color: var(--accent-text); cursor: pointer;
}
button { transition: transform var(--fast) var(--ease), filter var(--fast) var(--ease), background-color var(--base) var(--ease); }
button:active { transform: scale(.98); filter: brightness(.94); }
button.with-icon { display: flex; align-items: center; justify-content: center; gap: 8px; }
button.secondary { background: var(--card); color: var(--link); }

/* ---------- loading ---------- */
#view-loading { text-align: center; padding-top: 14vh; }
.pulse-logo { display: block; width: 64px; height: 64px; margin: 0 auto 20px; border-radius: 17px; animation: breathe 2.4s var(--ease) infinite; }
@keyframes breathe {
  0%, 100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(21, 151, 160, .45); }
  50% { transform: scale(1.04); box-shadow: 0 0 0 14px rgba(21, 151, 160, 0); }
}
/* Indeterminate: shows activity without pretending to know the percentage. */
.progress { width: 180px; height: 4px; margin: 14px auto 0; border-radius: 2px; background: var(--card); overflow: hidden; }
.progress > span { display: block; width: 40%; height: 100%; border-radius: 2px; background: var(--accent); animation: slide 1.6s var(--ease) infinite; }
@keyframes slide { from { transform: translateX(-100%); } to { transform: translateX(260%); } }
.checklist {
  list-style: none; text-align: start; max-width: 340px; margin: 18px auto; padding: 6px 16px;
  background: var(--card); border-radius: var(--radius);
}
.checklist li { padding-block: 10px; padding-inline: 26px 0; position: relative; font-size: 14.5px; }
.checklist li + li { border-top: 1px solid var(--line); }
.checklist li { padding-inline-start: 20px; }
.checklist li::before { content: ""; position: absolute; inset-inline-start: 2px; top: 50%; width: 6px; height: 6px; margin-top: -3px; border-radius: 50%; background: var(--accent); opacity: .7; }

/* ---------- report ---------- */
.card { background: var(--card); border-radius: var(--radius); padding: 16px; margin-bottom: 12px; overflow-wrap: anywhere; }
.verdict { border-inline-start: 5px solid var(--hint); }
.verdict.good_buy, .verdict.fair_deal { border-inline-start-color: var(--good); }
.verdict.overpriced { border-inline-start-color: var(--warn); }
.verdict.high_risk { border-inline-start-color: var(--danger); }
.badge {
  display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600;
  background: color-mix(in srgb, var(--hint) 20%, transparent); color: var(--text); vertical-align: middle;
}
.badge.good, .badge.low { background: color-mix(in srgb, var(--good) 22%, transparent); }
.badge.medium, .badge.warn { background: color-mix(in srgb, var(--warn) 26%, transparent); }
.badge.high, .badge.bad { background: color-mix(in srgb, var(--danger) 24%, transparent); }
.place { color: var(--hint); font-size: 13px; margin-bottom: 8px; }
ul { margin: 4px 0 8px; padding-inline-start: 20px; }
li { margin-bottom: 4px; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 8px; margin: 8px 0; }
.stat { background: var(--field); border-radius: 12px; padding: 10px 12px; }
.stat .k { font-size: 12px; color: var(--hint); }
.stat .v { font-size: 17px; font-weight: 650; font-variant-numeric: tabular-nums; }

dl.kv { margin: 0; }
dl.kv dt { font-size: 12px; color: var(--hint); margin-top: 12px; text-transform: uppercase; letter-spacing: 0.03em; }
dl.kv dd { margin: 2px 0 0; }

.score { display: grid; grid-template-columns: 118px 1fr 36px; gap: 10px; align-items: center; margin: 7px 0; font-size: 14px; }
.bar { height: 8px; border-radius: 4px; background: var(--field); overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--accent); border-radius: 4px; }
.score .n { text-align: end; font-variant-numeric: tabular-nums; color: var(--hint); }

.risk { padding: 10px 0; border-top: 1px solid var(--line); }
.risk:first-of-type { border-top: 0; }
.risk .head { display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; font-weight: 600; margin-bottom: 3px; }
.risk .cat { font-size: 12px; color: var(--hint); font-weight: 400; }

.sources li { font-size: 13.5px; }
.disclaimer { font-size: 12px; color: var(--hint); }
.actions { display: flex; gap: 10px; margin-top: 4px; }

/* Collapsible rows in the report. */
.fold { border-top: 1px solid var(--line); }
.card > h2 + .fold, .card > .fold:first-child { border-top: 0; }
.fold > summary {
  list-style: none; cursor: pointer; padding-block: 12px; padding-inline: 0 26px; position: relative; font-weight: 600; font-size: 15px;
}
.fold > summary::-webkit-details-marker { display: none; }
.fold > summary::after {
  content: ""; position: absolute; inset-inline-end: 4px; top: 50%; width: 8px; height: 8px; margin-top: -6px;
  border-right: 2px solid var(--hint); border-bottom: 2px solid var(--hint); transform: rotate(45deg); transition: transform 0.15s;
}
.fold > summary::after { transition: transform var(--base) var(--ease), margin-top var(--base) var(--ease); }
.fold[data-open] > summary::after { transform: rotate(-135deg); margin-top: -2px; }
.fold-body { padding: 0 0 12px; }
.fold-body .cat { font-size: 12px; color: var(--hint); text-transform: uppercase; letter-spacing: 0.03em; margin-bottom: 4px; }

.close-note { max-width: 340px; margin: 14px auto 0; padding: 12px 14px; border-radius: 12px; background: var(--card); font-size: 14px; }
.pdf-bar { margin-bottom: 12px; }
.pdf-bar .hint { text-align: center; margin: 8px 0 0; }

/* "Your reports" list on the form page. */
.history-list { padding: 2px 14px; }
.history-item {
  display: flex; flex-direction: column; align-items: flex-start; gap: 4px; width: 100%; text-align: start;
  padding-block: 12px; padding-inline: 0 22px; background: none; color: var(--text); border-radius: 0; font-weight: 500; position: relative;
}
.history-item + .history-item { border-top: 1px solid var(--line); }
.history-item::after {
  content: ""; position: absolute; inset-inline-end: 4px; top: 50%; width: 7px; height: 7px; margin-top: -4px;
  border-right: 2px solid var(--hint); border-top: 2px solid var(--hint); transform: rotate(45deg);
}
.history-item .h-place { font-size: 15px; }
.history-item { transition: background-color var(--fast) var(--ease); }
.history-item:active { transform: none; background: color-mix(in srgb, var(--hint) 12%, transparent); }
.history-item .h-meta { font-size: 12.5px; color: var(--hint); font-weight: 400; }

/* Badges read as labels: "High", "Medium confidence". */
.badge::first-letter { text-transform: uppercase; }

/* Key figures under the verdict: a 2 × 2 grid on phones. */
.keyfigs { grid-template-columns: repeat(2, 1fr); margin: 12px 0 10px; }
.keyfigs .stat { background: var(--field); }

/* ---------- motion ---------- */
@keyframes enter-forward { from { opacity: 0; transform: translateX(24px); } to { opacity: 1; transform: none; } }
@keyframes enter-back { from { opacity: 0; transform: translateX(-24px); } to { opacity: 1; transform: none; } }
@keyframes rise { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: none; } }
.enter-forward { animation: enter-forward var(--slow) var(--ease); }
.enter-back { animation: enter-back var(--slow) var(--ease); }
/* Report cards arrive one after another (index set by report.js). */
.rise { animation: rise 480ms var(--ease) both; animation-delay: calc(var(--i, 0) * 55ms); }
#history:not([hidden]) { animation: rise var(--slow) var(--ease); }
.error:not([hidden]) { animation: rise var(--base) var(--ease); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 1ms !important; animation-iteration-count: 1 !important; transition-duration: 1ms !important; }
}

/* The headline is a sentence, not a title: lighter than section headings so the key figures lead. */
.verdict > h2 { font-size: 16.5px; font-weight: 600; line-height: 1.4; }

/* Residency routes: one block per route, badge on the right. */
.routes { display: grid; gap: 8px; margin: 4px 0 8px; }
.route { background: var(--field); border-radius: 12px; padding: 10px 12px; }
.route-head { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; }
.route-threshold { margin: 4px 0 2px; font-size: 14px; }
.verify { margin-top: 10px; }
.was { text-decoration: line-through; color: var(--hint); }
.trend { font-size: 14px; }
/* Badges never break mid-word (cards allow overflow-wrap: anywhere for long URLs). */
.badge { white-space: nowrap; overflow-wrap: normal; }
.route-head .badge { flex-shrink: 0; }

/* ---------- header: language button + tabs ---------- */
.hero-text { flex: 1; min-width: 0; }
.lang-button {
  width: auto; flex: 0 0 auto; align-self: flex-start; display: flex; align-items: center; gap: 6px;
  padding: 7px 11px; border-radius: 999px; font-size: 13px; font-weight: 600;
  background: var(--card); color: var(--text);
}
.tabs {
  position: relative; display: grid; grid-template-columns: 1fr 1fr; margin: 4px 0 14px; padding: 4px;
  background: var(--card); border-radius: 14px;
}
.tabs button {
  position: relative; z-index: 1; padding: 10px 8px; border-radius: 10px; background: none;
  color: var(--hint); font-size: 14.5px; transition: color var(--base) var(--ease);
}
.tabs button[aria-selected="true"] { color: var(--text); }
.tabs button:active { transform: none; }
/* The highlight slides between tabs instead of jumping. */
.tab-indicator {
  position: absolute; top: 4px; bottom: 4px; inset-inline-start: 4px; width: calc(50% - 4px); border-radius: 10px;
  background: var(--field); transition: transform var(--slow) var(--ease);
}
.tabs[data-active="reports"] .tab-indicator { transform: translateX(100%); }

/* ---------- my reports ---------- */
#view-reports .group-title { margin-top: 6px; }
.empty { text-align: center; padding: 28px 16px; color: var(--hint); }
.empty p { margin-bottom: 14px; }
.empty button { width: auto; padding: 12px 22px; }

/* ---------- language sheet ---------- */
.sheet { position: fixed; inset: 0; z-index: 20; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, .4); opacity: 0; transition: opacity var(--base) var(--ease); }
.sheet-panel {
  position: absolute; left: 0; right: 0; bottom: 0; max-width: 600px; margin: 0 auto;
  padding: 20px 16px calc(20px + env(safe-area-inset-bottom)); border-radius: 20px 20px 0 0; background: var(--card);
  transform: translateY(100%); transition: transform var(--slow) var(--ease);
}
.sheet.open .sheet-backdrop { opacity: 1; }
.sheet.open .sheet-panel { transform: none; }
.sheet-options { display: grid; gap: 8px; margin: 12px 0; }
.sheet-option { background: var(--field); color: var(--text); text-align: start; padding: 14px 16px; border: 1.5px solid transparent; }
.sheet-option.selected { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, var(--field)); }

/* Residential / Commercial switch: two equal halves, the selected one raised. */
.segmented { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; padding: 4px; background: var(--field); border-radius: 12px; }
.segmented label { position: relative; cursor: pointer; }
.segmented input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.segmented span {
  display: block; text-align: center; padding: 9px 8px; border-radius: 9px; font-weight: 600; font-size: 14.5px; color: var(--hint);
  transition: background-color var(--base) var(--ease), color var(--base) var(--ease), box-shadow var(--base) var(--ease);
}
.segmented input:checked + span { background: var(--card); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, .12); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
[data-segment]:not([hidden]) { animation: rise var(--base) var(--ease); }

/* ---------- step 1: residential or commercial ---------- */
.segment-cards { display: grid; gap: 10px; }
.segment-card {
  display: flex; align-items: center; gap: 14px; width: 100%; text-align: start; padding: 16px;
  background: var(--card); color: var(--text); border-radius: var(--radius); border: 1.5px solid transparent;
  transition: transform var(--fast) var(--ease), border-color var(--base) var(--ease);
}
.segment-card:active { transform: scale(.98); border-color: var(--accent); }
.segment-icon {
  flex: 0 0 52px; height: 52px; border-radius: 14px; display: grid; place-items: center;
  background: color-mix(in srgb, #1597A0 16%, var(--field)); color: #1597A0;
}
.segment-card[data-choose="commercial"] .segment-icon { background: color-mix(in srgb, #F4B740 20%, var(--field)); color: #C98A12; }
.segment-text { display: flex; flex-direction: column; gap: 3px; }
.segment-text strong { font-size: 17px; }
.segment-text span { font-size: 13.5px; color: var(--hint); font-weight: 400; }
#segment-chooser:not([hidden]) { animation: rise var(--slow) var(--ease); }
#segment-chooser + .covers { margin-top: 14px; }
.segment-pill { display: flex; align-items: center; justify-content: space-between; margin: 4px 2px 0; padding: 10px 14px; background: var(--card); border-radius: 12px; font-weight: 600; }
.link-button { width: auto; padding: 4px 6px; background: none; color: var(--link); font-weight: 600; }
.link-button:active { transform: none; opacity: .7; }
/* Report: which kind of property this is. */
.kind-badge { display: inline-block; margin-inline-start: 6px; font-size: 12px; color: var(--hint); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
#segment-chooser[hidden] + .covers { display: none; }

/* Seven languages: two columns in the picker. */
.sheet-options { grid-template-columns: 1fr 1fr; }
/* No divider above the first visible field when hidden blocks precede it. */
.group > [hidden]:first-child + .field, .group > [hidden]:first-child + [hidden] + .field { border-top: 0; }

/* Right-to-left languages (Arabic): mirror what logical properties cannot. */
[dir="rtl"] select { background-position: left 14px center; }
[dir="rtl"] .tabs[data-active="reports"] .tab-indicator { transform: translateX(-100%); }
[dir="rtl"] .history-item::after { transform: rotate(-135deg); }
[dir="rtl"] .enter-forward { animation-name: enter-back; }
[dir="rtl"] .enter-back { animation-name: enter-forward; }
/* Mixed content in a right-to-left report (place names, sources, text in another script):
   each paragraph takes the direction of its own first letter, so punctuation stays where it belongs. */
[dir="rtl"] :is(p, li, h1, h2, h3, td, summary, .headline) { unicode-bidi: plaintext; }

/* Report balance and promo-code link above the form; the Stars / promo-code sheet. */
.wallet { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 8px 2px 0; padding: 0 4px; font-size: 14px; }
.wallet #wallet-balance { font-weight: 600; }
.wallet .link-button { margin-inline-start: auto; }
#pay-sheet .sheet-panel { display: grid; gap: 12px; }
#pay-sheet h2 { margin: 0; }
#pay-sheet .hint { margin: 0; }
.promo-row { display: flex; align-items: flex-end; gap: 8px; }
.promo-row .field { flex: 1; min-width: 0; padding: 0; }
.promo-row input { text-transform: uppercase; letter-spacing: .06em; }
.promo-row input::placeholder { text-transform: none; letter-spacing: normal; }
.promo-row button { width: auto; padding-inline: 18px; }
.pay-note { margin: 0; color: var(--good, #1f8a4c); font-weight: 600; }
