:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #f4f4f5);
  --text: var(--tg-theme-text-color, #111111);
  --hint: var(--tg-theme-hint-color, #8e8e93);
  --link: var(--tg-theme-link-color, #2481cc);
  --button: var(--tg-theme-button-color, #2481cc);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --accent: var(--button);
  --danger: #e5484d;
  --success: #2fae4e;
  --border: rgba(127, 127, 127, 0.2);
  --radius-sm: 12px;
  --radius-md: 16px;
  --radius-lg: 20px;
  --radius-pill: 999px;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.06), 0 1px 6px rgba(0, 0, 0, 0.06);
  --shadow-btn: 0 4px 12px rgba(0, 0, 0, 0.16);
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--secondary-bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
}

#app { min-height: 100vh; display: flex; flex-direction: column; }

#screen-header {
  position: sticky;
  top: env(safe-area-inset-top, 0px);
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
#screen-header h1 { font-size: 17px; margin: 0; font-weight: 600; }

.icon-btn {
  background: var(--secondary-bg);
  border: none;
  border-radius: var(--radius-pill);
  font-size: 18px;
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--text);
  transition: transform 0.12s ease, opacity 0.12s ease;
}
.icon-btn:active { transform: scale(0.92); opacity: 0.8; }
.icon-btn:disabled { opacity: 0.35; cursor: default; }
#back-btn { background: none; }

#screen-content {
  flex: 1;
  padding: 12px 16px 90px;
  overflow-y: auto;
}

#tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  display: flex;
  background: var(--bg);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  z-index: 20;
}
.tab-btn {
  flex: 1;
  background: none;
  border: none;
  padding: 8px 4px 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  font-size: 11px;
  color: var(--hint);
  cursor: pointer;
  transition: color 0.12s ease, transform 0.12s ease;
}
.tab-btn:active { transform: scale(0.94); }
.tab-btn.active { color: var(--accent); }
.tab-icon { font-size: 20px; transition: transform 0.12s ease; }
.tab-btn.active .tab-icon { transform: translateY(-1px); }
.tab-badge {
  display: inline-block;
  margin-top: 2px;
  min-width: 15px;
  height: 15px;
  padding: 0 4px;
  border-radius: var(--radius-pill);
  background: var(--danger);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  line-height: 15px;
  text-align: center;
}

.field { margin-bottom: 12px; }
.field label { display: block; font-size: 13px; color: var(--hint); margin-bottom: 4px; }
.field input, .field select, .field textarea {
  width: 100%;
  padding: 11px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  font-family: inherit;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.field input:focus, .field select:focus, .field textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(36, 129, 204, 0.22);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent);
}
.field textarea { resize: vertical; min-height: 80px; }
.field-hint { font-size: 12px; color: var(--hint); margin-top: 4px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  width: 100%;
  padding: 13px 18px;
  border-radius: var(--radius-pill);
  border: none;
  background: var(--button);
  color: var(--button-text);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  margin-bottom: 8px;
  box-shadow: var(--shadow-btn);
  transition: transform 0.12s ease, box-shadow 0.12s ease, opacity 0.12s ease;
}
.btn:active { transform: scale(0.98); box-shadow: none; }
.btn.secondary {
  background: var(--secondary-bg);
  color: var(--text);
  border: 1px solid var(--border);
  box-shadow: none;
}
.btn.danger { background: var(--danger); color: #fff; box-shadow: 0 4px 12px rgba(229, 72, 77, 0.28); }
.btn.small { width: auto; padding: 9px 16px; font-size: 13px; }
.btn:disabled { opacity: 0.5; cursor: default; transform: none; box-shadow: none; }

.row { display: flex; gap: 8px; }
.row > * { flex: 1; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.chip {
  padding: 7px 14px;
  border-radius: var(--radius-pill);
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
  transition: transform 0.12s ease, background 0.12s ease, border-color 0.12s ease;
}
.chip:active { transform: scale(0.95); }
.chip.active { background: var(--accent); color: var(--button-text); border-color: var(--accent); }

.ad-list { display: flex; flex-direction: column; gap: 10px; }
.ad-card {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--bg);
  border-radius: var(--radius-md);
  padding: 10px;
  position: relative;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}
.ad-card:active { transform: scale(0.985); box-shadow: none; }
.ad-card-photo {
  width: 64px; height: 64px;
  border-radius: var(--radius-sm);
  background: var(--secondary-bg) center/cover no-repeat;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 22px;
}
.ad-card-body { flex: 1; min-width: 0; }
.ad-card-badges { font-size: 11px; color: var(--accent); margin-bottom: 2px; min-height: 14px; }
.ad-card-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ad-card-price { color: var(--accent); font-weight: 600; font-size: 14px; }
.ad-card-city { color: var(--hint); font-size: 12px; }
.ad-card-fav {
  position: absolute;
  top: 8px;
  right: 8px;
  font-size: 15px;
  width: 28px;
  height: 28px;
  border-radius: var(--radius-pill);
  background: var(--secondary-bg);
  transition: transform 0.12s ease;
}
.ad-card-fav:active { transform: scale(0.88); }

.empty-state { text-align: center; color: var(--hint); padding: 48px 16px; }
.empty-state .emoji { font-size: 40px; display: block; margin-bottom: 8px; }

.pager { display: flex; justify-content: center; gap: 12px; align-items: center; margin-top: 12px; color: var(--hint); }

.photo-gallery { display: flex; gap: 8px; overflow-x: auto; margin-bottom: 12px; }
.photo-gallery img { width: 100%; max-width: 340px; border-radius: var(--radius-md); aspect-ratio: 1 / 1; object-fit: cover; flex-shrink: 0; }

.detail-title { font-size: 19px; font-weight: 700; margin: 4px 0; }
.detail-price { font-size: 22px; font-weight: 700; color: var(--accent); margin-bottom: 8px; }
.detail-meta { color: var(--hint); font-size: 13px; margin-bottom: 12px; }
.detail-section { margin-bottom: 16px; }
.detail-section h3 { font-size: 13px; color: var(--hint); margin: 0 0 6px; text-transform: uppercase; letter-spacing: .02em; }
.badge-line { color: var(--accent); font-size: 13px; margin-bottom: 6px; }

.photo-upload-grid { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.photo-thumb { width: 72px; height: 72px; border-radius: var(--radius-sm); object-fit: cover; position: relative; }
.photo-thumb-wrap { position: relative; }
.photo-thumb-remove {
  position: absolute; top: -6px; right: -6px;
  width: 20px; height: 20px; border-radius: var(--radius-pill);
  background: var(--danger); color: #fff; border: none; font-size: 12px; line-height: 1;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: transform 0.12s ease;
}
.photo-thumb-remove:active { transform: scale(0.85); }
.photo-add-btn {
  width: 72px; height: 72px; border-radius: var(--radius-sm);
  border: 1px dashed var(--border);
  background: none; color: var(--hint); font-size: 24px; cursor: pointer;
  transition: transform 0.12s ease, border-color 0.12s ease;
}
.photo-add-btn:active { transform: scale(0.94); border-color: var(--accent); }

.phone-row {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--secondary-bg);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
}
.phone-link {
  flex: 1;
  font-size: 18px;
  font-weight: 700;
  color: var(--accent);
  text-decoration: none;
}
.phone-row .icon-btn { flex-shrink: 0; background: var(--bg); }

.status-line { font-size: 13px; margin-bottom: 8px; }
.stars-grid { display: flex; flex-direction: column; gap: 8px; margin-bottom: 16px; }

.spinner { text-align: center; padding: 40px; color: var(--hint); }
.error-banner {
  background: rgba(229, 72, 77, 0.12);
  color: var(--danger);
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  margin-bottom: 12px;
  font-size: 13px;
}

.chat-list { display: flex; flex-direction: column; gap: 10px; }
.chat-list-item {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg);
  border-radius: var(--radius-md);
  padding: 10px;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}
.chat-list-item:active { transform: scale(0.985); box-shadow: none; }
.chat-list-photo {
  width: 48px; height: 48px;
  border-radius: var(--radius-sm);
  background: var(--secondary-bg) center/cover no-repeat;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px;
}
.chat-list-body { flex: 1; min-width: 0; }
.chat-list-top { display: flex; align-items: center; gap: 6px; }
.chat-list-name { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.chat-list-role { font-size: 11px; color: var(--hint); flex-shrink: 0; }
.chat-list-ad { font-size: 12px; color: var(--hint); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 1px; }
.chat-list-last { font-size: 13px; color: var(--hint); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 3px; }
.chat-list-last.unread { color: var(--text); font-weight: 600; }
.chat-list-time {
  flex-shrink: 0;
  font-size: 11px;
  color: var(--hint);
}
.chat-list-unread {
  flex-shrink: 0;
  min-width: 20px; height: 20px;
  padding: 0 6px;
  border-radius: var(--radius-pill);
  background: var(--accent);
  color: var(--button-text);
  font-size: 11px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
}

.chat-thread { display: flex; flex-direction: column; gap: 8px; }
.chat-bubble-row { display: flex; }
.chat-bubble-row.mine { justify-content: flex-end; }
.chat-bubble {
  max-width: 78%;
  padding: 8px 12px;
  border-radius: var(--radius-md);
  background: var(--secondary-bg);
  font-size: 14px;
  line-height: 1.35;
  word-wrap: break-word;
  white-space: pre-wrap;
}
.chat-bubble-row.mine .chat-bubble { background: var(--accent); color: var(--button-text); }
.chat-bubble-time { font-size: 10px; opacity: 0.65; margin-top: 4px; text-align: right; }

#screen-content.chat-open { padding-bottom: 150px; }
.chat-input-bar {
  position: fixed;
  left: 0; right: 0;
  bottom: calc(56px + env(safe-area-inset-bottom, 0px));
  background: var(--bg);
  border-top: 1px solid var(--border);
  padding: 8px 12px;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  z-index: 15;
}
.chat-input-bar textarea {
  flex: 1;
  resize: none;
  max-height: 90px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--secondary-bg);
  color: var(--text);
  padding: 9px 12px;
  font-family: inherit;
  font-size: 14px;
}
.chat-input-bar textarea:focus { outline: none; border-color: var(--accent); }
.chat-input-bar button {
  flex-shrink: 0;
  width: auto;
  padding: 0 16px;
  height: 40px;
  margin-bottom: 0;
}

@media (prefers-color-scheme: dark) {
  :root { --border: rgba(255,255,255,0.14); }
}
