/* ===== Токены: сайт (светлая/тёмная) ===== */
:root {
  --bg: #f5f4f0;
  --surface: #ffffff;
  --surface-2: #efeee9;
  --text: #1b1d1c;
  --muted: #6b706d;
  --line: #e2e0d9;
  --accent: #0f6b5f;
  --accent-text: #ffffff;
  --accent-soft: #dcefe9;
  --danger: #b4372f;
  --badge-rent: #e6eefb;
  --badge-rent-text: #2a4f8f;
  --badge-sale: #dcefe9;
  --badge-sale-text: #0f5a50;
  --shadow: 0 1px 2px rgba(20, 24, 22, .06), 0 4px 16px rgba(20, 24, 22, .06);
  --radius: 14px;
  --radius-sm: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111413;
    --surface: #1a1e1d;
    --surface-2: #232826;
    --text: #eef0ee;
    --muted: #9aa29e;
    --line: #2c3230;
    --accent: #3fb9a5;
    --accent-text: #08201c;
    --accent-soft: #173a34;
    --danger: #e0675e;
    --badge-rent: #1f2c44;
    --badge-rent-text: #a9c3f2;
    --badge-sale: #173a34;
    --badge-sale-text: #9fe0d3;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #111413;
  --surface: #1a1e1d;
  --surface-2: #232826;
  --text: #eef0ee;
  --muted: #9aa29e;
  --line: #2c3230;
  --accent: #3fb9a5;
  --accent-text: #08201c;
  --accent-soft: #173a34;
  --danger: #e0675e;
  --badge-rent: #1f2c44;
  --badge-rent-text: #a9c3f2;
  --badge-sale: #173a34;
  --badge-sale-text: #9fe0d3;
  color-scheme: dark;
}

/* ===== Токены: внутри Telegram — цвета темы пользователя ===== */
body.in-telegram {
  --bg: var(--tg-theme-secondary-bg-color, var(--tg-theme-bg-color, #f5f4f0));
  --surface: var(--tg-theme-bg-color, #ffffff);
  --surface-2: var(--tg-theme-secondary-bg-color, #efeee9);
  --text: var(--tg-theme-text-color, #1b1d1c);
  --muted: var(--tg-theme-hint-color, #6b706d);
  --line: var(--tg-theme-section-separator-color, rgba(127, 127, 127, .2));
  --accent: var(--tg-theme-button-color, #0f6b5f);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: var(--tg-theme-destructive-text-color, #b4372f);
}

/* ===== База ===== */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
button, input, select { font: inherit; color: inherit; }
img { display: block; max-width: 100%; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ===== Шапка ===== */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 16px;
  padding: 10px max(16px, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; color: var(--text); }
.brand-mark {
  width: 30px; height: 30px; border-radius: 9px; background: var(--accent);
  display: grid; place-items: center;
}
.brand-mark svg { width: 18px; height: 18px; fill: var(--accent-text); }
.brand-name { font-weight: 700; font-size: 17px; letter-spacing: -.01em; }
.brand-city { color: var(--muted); font-size: 13px; }
.tabs { display: flex; gap: 4px; margin-left: auto; }
.tabs a {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 12px; border-radius: 999px; color: var(--muted); font-weight: 500;
}
.tabs a svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linejoin: round; }
.tabs a:hover { background: var(--surface-2); color: var(--text); }
.tabs a.active { background: var(--accent-soft); color: var(--accent); }
body.in-telegram .tabs a.active { background: var(--surface-2); }
.account { display: flex; align-items: center; gap: 8px; }
.account .who { color: var(--muted); font-size: 13px; max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Внутри Telegram и на телефоне — вкладки снизу */
@media (max-width: 720px) {
  /* backdrop-filter у шапки сделал бы её контейнером для fixed-панели вкладок — панель оказалась бы вверху */
  .topbar { backdrop-filter: none; background: var(--surface); }
  .account { margin-left: auto; }
  .brand-city { display: none; }
  .tabs {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 25;
    justify-content: space-around; margin: 0; gap: 0;
    padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
    background: var(--surface); border-top: 1px solid var(--line);
  }
  .tabs a { flex-direction: column; gap: 2px; font-size: 11px; padding: 6px 12px; border-radius: 12px; }
  .tabs a.active { background: none; }
  .view { padding-bottom: 90px !important; }
}
body.in-telegram .brand-city { display: none; }
body.in-telegram .account { display: none; }

/* ===== Контент ===== */
.view { max-width: 1120px; margin: 0 auto; padding: 16px; }
.page-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin: 4px 0 14px; }
.page-head h1 { font-size: 22px; line-height: 1.2; margin: 0; letter-spacing: -.01em; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* Сегменты типа сделки */
.segments { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 12px; scrollbar-width: none; }
.segments::-webkit-scrollbar { display: none; }
.chip {
  flex: none; border: 1px solid var(--line); background: var(--surface);
  padding: 7px 14px; border-radius: 999px; cursor: pointer; white-space: nowrap; font-size: 14px;
}
.chip:hover { border-color: var(--muted); }
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

.toolbar { display: flex; gap: 8px; align-items: center; margin-bottom: 14px; flex-wrap: wrap; }
.toolbar .count { margin-right: auto; color: var(--muted); font-size: 14px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  padding: 9px 14px; border-radius: var(--radius-sm); cursor: pointer; font-weight: 500;
  min-height: 40px;
}
.btn:hover { border-color: var(--muted); }
.btn svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 1.8; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.05); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.danger { color: var(--danger); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .55; cursor: default; }
.badge-dot {
  min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px;
  background: var(--accent); color: var(--accent-text); font-size: 11px; display: inline-grid; place-items: center;
}

/* Карточки объявлений */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 14px; }
@media (max-width: 560px) { .grid { grid-template-columns: 1fr; } }
.card {
  display: flex; flex-direction: column; background: var(--surface); border-radius: var(--radius);
  overflow: hidden; box-shadow: var(--shadow); color: var(--text); transition: transform .15s ease;
}
.card:hover { transform: translateY(-2px); }
.card-photo { position: relative; aspect-ratio: 4 / 3; background: var(--surface-2); }
.card-photo img { width: 100%; height: 100%; object-fit: cover; }
.card-photo .placeholder { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); }
.card-photo .placeholder svg { width: 40px; height: 40px; fill: none; stroke: currentColor; stroke-width: 1.4; opacity: .6; }
.card-photo .photos-count {
  position: absolute; right: 8px; bottom: 8px; background: rgba(0, 0, 0, .55); color: #fff;
  font-size: 12px; padding: 2px 8px; border-radius: 999px;
}
.card-body { padding: 12px 14px 14px; display: flex; flex-direction: column; gap: 4px; }
.card-price { font-size: 18px; font-weight: 700; letter-spacing: -.01em; }
.card-title { font-weight: 500; }
.card-meta { color: var(--muted); font-size: 13px; }
.badge {
  position: absolute; left: 8px; top: 8px; font-size: 12px; font-weight: 600;
  padding: 3px 9px; border-radius: 999px; background: var(--badge-sale); color: var(--badge-sale-text);
}
.badge.rent { background: var(--badge-rent); color: var(--badge-rent-text); }
.inline-badge { position: static; display: inline-block; }

.more { display: flex; justify-content: center; margin: 18px 0 8px; }

/* Пустые состояния и загрузка */
.empty {
  background: var(--surface); border-radius: var(--radius); padding: 28px 20px; text-align: center;
  color: var(--muted); box-shadow: var(--shadow);
}
.empty h2 { color: var(--text); font-size: 17px; margin: 0 0 6px; }
.empty .btn { margin-top: 14px; }
.skeleton { background: linear-gradient(90deg, var(--surface-2), var(--surface), var(--surface-2)); background-size: 200% 100%; animation: shimmer 1.2s infinite; border-radius: var(--radius); }
@keyframes shimmer { to { background-position: -200% 0; } }
@media (prefers-reduced-motion: reduce) { .skeleton { animation: none; } .card { transition: none; } }

/* ===== Страница объявления ===== */
.ad-layout {
  display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr); gap: 16px 20px; align-items: start;
  grid-template-areas: "gallery info" "desc info";
}
.ad-layout .gallery { grid-area: gallery; }
.ad-layout .ad-info { grid-area: info; position: sticky; top: 76px; }
.ad-layout .ad-desc { grid-area: desc; }
@media (max-width: 860px) {
  /* На телефоне цена и кнопки сразу под фото, описание — ниже */
  .ad-layout { grid-template-columns: 1fr; grid-template-areas: "gallery" "info" "desc"; }
  .ad-layout .ad-info { position: static; }
}
.gallery { position: relative; border-radius: var(--radius); overflow: hidden; background: var(--surface-2); }
.gallery-track { display: flex; overflow-x: auto; scroll-snap-type: x mandatory; scrollbar-width: none; }
.gallery-track::-webkit-scrollbar { display: none; }
.gallery-track img { flex: none; width: 100%; aspect-ratio: 4 / 3; object-fit: cover; scroll-snap-align: start; }
.gallery .counter {
  position: absolute; right: 10px; bottom: 10px; background: rgba(0, 0, 0, .55); color: #fff;
  font-size: 12px; padding: 3px 9px; border-radius: 999px;
}
.gallery .nav {
  position: absolute; top: 50%; transform: translateY(-50%); width: 36px; height: 36px; border-radius: 50%;
  border: 0; background: rgba(255, 255, 255, .85); color: #1b1d1c; cursor: pointer; display: grid; place-items: center;
}
.gallery .nav.prev { left: 10px; } .gallery .nav.next { right: 10px; }
@media (hover: none) { .gallery .nav { display: none; } }
.gallery-empty { aspect-ratio: 4 / 3; display: grid; place-items: center; color: var(--muted); }

.panel { background: var(--surface); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.panel + .panel { margin-top: 14px; }
.ad-price { font-size: 26px; font-weight: 750; letter-spacing: -.02em; margin: 6px 0 2px; }
.ad-title { font-size: 18px; margin: 0 0 6px; line-height: 1.3; }
.facts { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 12px 0 0; font-size: 14px; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; font-weight: 500; }
.description { white-space: pre-wrap; overflow-wrap: anywhere; margin: 0; }
.actions { display: grid; gap: 8px; margin-top: 14px; }
.actions .row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.back-link { display: inline-flex; align-items: center; gap: 4px; margin-bottom: 12px; color: var(--muted); }
body.in-telegram .back-link { display: none; }

/* ===== Подписки ===== */
.alert-card { display: flex; gap: 12px; align-items: flex-start; }
.alert-card .info { flex: 1; min-width: 0; }
.alert-card h3 { margin: 0 0 4px; font-size: 15px; }
.alert-card .conditions { color: var(--muted); font-size: 13px; }
.alert-card .controls { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.status { font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); }
.status.off { background: var(--surface-2); color: var(--muted); }
.stack { display: grid; gap: 12px; }

/* ===== Вход ===== */
.login-box { text-align: center; }
.login-box p { margin: 0 0 14px; }
#tg-login { display: flex; justify-content: center; min-height: 44px; }

/* ===== Панель фильтров ===== */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, .4); z-index: 40; }
.sheet {
  position: fixed; z-index: 41; left: 50%; transform: translateX(-50%);
  bottom: 0; width: min(560px, 100%); max-height: 88vh; overflow-y: auto;
  background: var(--surface); border-radius: 18px 18px 0 0; box-shadow: 0 -8px 30px rgba(0, 0, 0, .2);
  padding: 18px 16px calc(16px + env(safe-area-inset-bottom));
}
@media (min-width: 721px) { .sheet { bottom: auto; top: 6vh; border-radius: 18px; } }
.sheet h2 { font-size: 18px; margin: 0 0 14px; }
.field { margin-bottom: 16px; }
.field > label, .field > .label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; font-weight: 500; }
.input, .select {
  width: 100%; border: 1px solid var(--line); background: var(--bg); border-radius: var(--radius-sm);
  padding: 10px 12px; min-height: 42px;
}
.input:focus, .select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips .chip { padding: 6px 12px; font-size: 13px; }
.sheet-actions { position: sticky; bottom: -16px; display: grid; grid-template-columns: 1fr 2fr; gap: 8px; background: var(--surface); padding: 12px 0 4px; }
.sheet-note { font-size: 13px; color: var(--muted); margin: -6px 0 14px; }

/* ===== Уведомление ===== */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(84px + env(safe-area-inset-bottom)); z-index: 60;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px;
  max-width: calc(100% - 32px); box-shadow: var(--shadow);
}
@media (min-width: 721px) { .toast { bottom: 24px; } }
