/* ── Telegram theme variables ── */
:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #1a1a1a);
  --hint: var(--tg-theme-hint-color, #8e8e93);
  --link: var(--tg-theme-link-color, #007aff);
  --accent: var(--tg-theme-button-color, #007aff);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #f2f2f7);
  --section-bg: var(--tg-theme-section-bg-color, #ffffff);
  --subtitle: var(--tg-theme-subtitle-text-color, #6c6c70);
  --destructive: var(--tg-theme-destructive-text-color, #ff3b30);
  --border: rgba(60, 60, 67, 0.08);
  --shadow: rgba(0, 0, 0, 0.04);
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
}

/* ── Base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
  padding-bottom: 24px;
}

/* ── Spacing scale ── */
:root {
  --space-2: 2px;
  --space-4: 4px;
  --space-6: 6px;
  --space-8: 8px;
  --space-12: 12px;
  --space-16: 16px;
  --space-20: 20px;
  --space-24: 24px;
}

/* ── Typography ── */
h1 { font-size: 20px; font-weight: 700; letter-spacing: -0.3px; }
h2 { font-size: 17px; font-weight: 600; }
h3 { font-size: 14px; font-weight: 600; color: var(--subtitle); text-transform: uppercase; letter-spacing: 0.5px; }

/* ── Header ── */
#app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-12) var(--space-16);
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.header-left {
  display: flex;
  align-items: center;
  gap: var(--space-8);
}
.header-logo { color: var(--accent); flex-shrink: 0; }
#app-header h1 { font-size: 20px; font-weight: 700; }
#user-name { font-size: 13px; color: var(--hint); }

/* ── Tab bar ── */
#tab-bar {
  display: flex;
  background: var(--secondary-bg);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 49px;
  z-index: 9;
}
.tab {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-6);
  padding: 10px var(--space-8);
  border: none;
  background: transparent;
  font-size: 14px;
  font-weight: 600;
  color: var(--hint);
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
  border-bottom: 2px solid transparent;
}
.tab svg { flex-shrink: 0; }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }
.tab:focus { outline: none; }

/* ── Tab panels ── */
.tab-panel { display: none; padding: var(--space-12) var(--space-16); }
.tab-panel.active { display: block; }

.section-title { margin-bottom: var(--space-12); }
.subsection-title {
  font-size: 12px;
  font-weight: 600;
  margin-bottom: var(--space-8);
  padding: 0;
}

/* ── Search bar ── */
.search-bar {
  display: flex;
  align-items: center;
  gap: var(--space-8);
  margin-bottom: var(--space-16);
  padding: 0;
  position: relative;
}
.search-icon {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--hint);
  pointer-events: none;
  z-index: 1;
}
.search-bar input {
  flex: 1;
  padding: 10px 12px 10px 36px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--secondary-bg);
  color: var(--text);
  font-size: 15px;
  transition: border-color 0.15s;
}
.search-bar input::placeholder { color: var(--hint); }
.search-bar input:focus { outline: none; border-color: var(--accent); }
.search-bar input.search-loading {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%238e8e93' stroke-width='2.5'%3E%3Ccircle cx='12' cy='12' r='10' stroke-dasharray='31.4 31.4' stroke-linecap='round' opacity='0.3'/%3E%3Cpath d='M12 2a10 10 0 0 1 10 10' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
}

/* ── Card grid ── */
.card-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--space-12);
}
@media (min-width: 480px) { .card-grid { grid-template-columns: 1fr 1fr; } }
@media (min-width: 768px) { .card-grid { grid-template-columns: 1fr 1fr 1fr; } }

/* ── Listing card ── */
.listing-card {
  background: var(--section-bg);
  border-radius: var(--radius-lg);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  box-shadow: 0 1px 3px var(--shadow);
}
.card-img-wrap {
  position: relative;
  width: 100%;
  height: 180px;
  overflow: hidden;
  background: var(--secondary-bg);
}
.card-img-wrap img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  transition: transform 0.2s;
}
.listing-card:hover .card-img-wrap img { transform: scale(1.03); }
.card-img-wrap.img-missing { display: none; }

.listing-card .card-body {
  padding: var(--space-12);
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}
.listing-card .card-title {
  font-weight: 700;
  font-size: 15px;
  line-height: 1.35;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.listing-card .card-price {
  font-size: 15px;
  font-weight: 600;
  color: var(--accent);
}
.listing-card .card-address { font-size: 13px; color: var(--subtitle); }
.listing-card .card-phone {
  display: flex;
  align-items: center;
  gap: var(--space-4);
  font-size: 13px;
  color: var(--accent);
  margin-top: var(--space-2);
}
.listing-card .card-phone svg { flex-shrink: 0; }
.listing-card .card-note {
  font-size: 12px;
  color: var(--link);
  font-style: italic;
  padding: var(--space-4) var(--space-8);
  background: var(--secondary-bg);
  border-radius: var(--radius-sm);
  margin-top: var(--space-2);
}
.listing-card .card-date {
  font-size: 12px;
  color: var(--hint);
  margin-top: auto;
  padding-top: var(--space-4);
}
.listing-card .card-actions {
  display: flex;
  gap: var(--space-6);
  margin-top: var(--space-8);
}
.listing-card .card-actions button {
  flex: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-4);
  padding: 8px 4px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s, transform 0.1s;
  white-space: nowrap;
}
.listing-card .card-actions button:active { transform: scale(0.96); }
.listing-card .card-actions button:disabled { opacity: 0.5; cursor: default; transform: none; }
.btn-pdf { background: var(--accent); color: var(--accent-text); }
.btn-agent { background: transparent; color: var(--accent); border: 1px solid var(--accent) !important; }
.btn-delete {
  flex: 0 0 auto !important;
  width: 38px;
  padding: 8px 0 !important;
  background: transparent;
  color: var(--destructive);
}

/* ── Pagination ── */
.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: var(--space-12);
  margin-top: var(--space-20);
}
.pagination button {
  display: inline-flex;
  align-items: center;
  gap: var(--space-4);
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--section-bg);
  color: var(--text);
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
  transition: border-color 0.15s;
}
.pagination button:hover { border-color: var(--accent); }
.pagination button:disabled { opacity: 0.3; cursor: default; border-color: var(--border); }
.pagination span { font-size: 14px; color: var(--hint); }

/* ── PDF list ── */
.pdf-list { margin-bottom: var(--space-16); }
.pdf-empty-sub {
  font-size: 13px;
  color: var(--hint);
  padding: var(--space-8) 0;
}
.pdf-list .pdf-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: var(--space-12);
  background: var(--section-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  margin-bottom: var(--space-8);
}
.pdf-item .pdf-info { flex: 1; min-width: 0; }
.pdf-item .pdf-title { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pdf-item .pdf-meta {
  display: flex;
  align-items: center;
  gap: var(--space-4);
  font-size: 12px;
  color: var(--hint);
  margin-top: var(--space-2);
}
.pdf-item .pdf-meta svg { flex-shrink: 0; }
.pdf-item .pdf-download {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--secondary-bg);
  color: var(--accent);
  cursor: pointer;
  transition: background 0.15s;
  flex-shrink: 0;
  margin-left: var(--space-8);
}
.pdf-item .pdf-download:hover { background: var(--accent); color: var(--accent-text); }
.pdf-item .pdf-download:active { transform: scale(0.94); }

/* ── Subsection headers in PDF tab ── */
#pdfs-client-section, #pdfs-agent-section { margin-bottom: var(--space-16); }
#pdfs-client-section h3, #pdfs-agent-section h3 {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--subtitle);
  margin-bottom: var(--space-8);
  padding: 0;
}

/* ── Empty state ── */
.empty-msg {
  text-align: center;
  padding: 48px var(--space-16);
  color: var(--hint);
  font-size: 15px;
  line-height: 1.6;
  display: none;
}
.empty-msg:only-child { display: block; }

/* ── Toast ── */
#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 20px;
  border-radius: var(--radius-md);
  font-size: 14px;
  font-weight: 500;
  opacity: 0;
  transition: opacity 0.25s;
  pointer-events: none;
  z-index: 100;
  white-space: nowrap;
}
#toast.show { opacity: 0.9; }
#toast.toast-error { background: var(--destructive); color: #fff; }

/* ── Modal ── */
#modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  backdrop-filter: blur(2px);
}
#modal-overlay.hidden { display: none; }
#modal-box {
  background: var(--bg);
  padding: var(--space-24);
  border-radius: var(--radius-lg);
  max-width: 320px;
  width: 90%;
  text-align: center;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
}
#modal-text { margin-bottom: var(--space-16); font-size: 15px; line-height: 1.5; }
#modal-actions { display: flex; gap: var(--space-8); justify-content: center; }
#modal-actions button {
  padding: 10px 24px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s;
}
#modal-actions button:active { opacity: 0.8; }
#modal-actions .btn-confirm { background: var(--destructive); color: #fff; }
#modal-actions .btn-cancel { background: var(--secondary-bg); color: var(--text); }

/* ── Spinner ── */
.spinner {
  border: 3px solid var(--secondary-bg);
  border-top-color: var(--accent);
  border-radius: 50%;
  width: 28px;
  height: 28px;
  animation: spin 0.7s linear infinite;
  margin: 32px auto;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Inline SVG spinner */
.spin-svg {
  animation: spin 0.8s linear infinite;
}

/* ── Skeleton loaders ── */
.listing-card.skeleton .skeleton-img {
  width: 100%;
  height: 180px;
  background: var(--secondary-bg);
  animation: shimmer 1.5s infinite;
}
.listing-card.skeleton .skeleton-line {
  height: 14px;
  border-radius: 6px;
  background: var(--secondary-bg);
  margin-bottom: 8px;
  animation: shimmer 1.5s infinite;
}
.listing-card.skeleton .skeleton-line.w-70 { width: 70%; }
.listing-card.skeleton .skeleton-line.w-40 { width: 40%; }
.listing-card.skeleton .skeleton-line.w-50 { width: 50%; }

@keyframes shimmer {
  0% { opacity: 0.6; }
  50% { opacity: 1; }
  100% { opacity: 0.6; }
}

.hidden { display: none !important; }
