/* === Theme (Telegram WebApp vars with fallbacks) === */
: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, #2481cc);
  --btn: var(--tg-theme-button-color, #2481cc);
  --btn-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);
  --header-bg: var(--tg-theme-header-bg-color, #2481cc);
  --divider: var(--tg-theme-section-separator-color, #e5e5ea);
  --danger: #ff3b30;
  --success: #34c759;
  --radius: 12px;
  --radius-sm: 8px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

body {
  font-family: -apple-system, 'SF Pro', 'Helvetica Neue', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

button {
  font: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

input {
  font: inherit;
  border: none;
  outline: none;
  background: none;
  color: var(--text);
}

/* === Layout === */
#app {
  max-width: 480px;
  margin: 0 auto;
  padding-bottom: calc(16px + var(--safe-bottom));
}

/* === Header === */
#header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  background: var(--section-bg);
  border-bottom: 1px solid var(--divider);
  position: sticky;
  top: 0;
  z-index: 100;
}

#header h1 {
  flex: 1;
  font-size: 18px;
  font-weight: 700;
}

.header-btn {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background 0.15s;
  color: var(--link);
}

.header-btn:active {
  background: var(--secondary-bg);
}

/* === Limit Bar === */
#limit-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 16px;
  background: var(--secondary-bg);
  font-size: 12px;
  color: var(--hint);
}

#limit-track {
  flex: 1;
  height: 4px;
  background: var(--divider);
  border-radius: 2px;
  overflow: hidden;
}

#limit-fill {
  height: 100%;
  background: var(--btn);
  border-radius: 2px;
  transition: width 0.3s ease;
  width: 100%;
}

#limit-fill.warn { background: #ff9500; }
#limit-fill.danger { background: var(--danger); }

/* === Screens === */
.screen {
  display: none;
  padding: 16px;
  animation: fadeIn 0.2s ease;
}

.screen.active { display: block; }

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* === Search === */
.search-wrap {
  position: relative;
  margin-bottom: 16px;
}

.search-icon {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--hint);
  pointer-events: none;
}

#search {
  width: 100%;
  padding: 10px 12px 10px 40px;
  background: var(--secondary-bg);
  border-radius: var(--radius);
  font-size: 15px;
}

#search::placeholder { color: var(--hint); }

/* === Places Grid === */
#places-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
}

.place-card {
  background: var(--section-bg);
  border: 1px solid var(--divider);
  border-radius: var(--radius);
  padding: 14px 12px;
  text-align: center;
  transition: transform 0.15s, box-shadow 0.15s;
  cursor: pointer;
}

.place-card:active {
  transform: scale(0.97);
}

.place-card .place-emoji {
  font-size: 28px;
  margin-bottom: 6px;
}

.place-card .place-name {
  font-size: 14px;
  font-weight: 600;
  line-height: 1.3;
}

.place-card .place-region {
  font-size: 11px;
  color: var(--hint);
  margin-top: 2px;
}

/* === Place Hero (config screen) === */
.place-hero {
  background: linear-gradient(135deg, var(--btn), #6366f1);
  color: #fff;
  border-radius: var(--radius);
  padding: 20px 16px;
  margin-bottom: 20px;
  text-align: center;
}

.place-hero .hero-name {
  font-size: 22px;
  font-weight: 700;
}

.place-hero .hero-region {
  font-size: 13px;
  opacity: 0.85;
  margin-top: 4px;
}

.place-hero .hero-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
  margin-top: 10px;
}

.place-hero .hero-tag {
  background: rgba(255,255,255,0.2);
  border-radius: 20px;
  padding: 3px 10px;
  font-size: 11px;
}

/* === Config === */
.config-group {
  margin-bottom: 18px;
}

.config-label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--hint);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 8px;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  padding: 8px 14px;
  border-radius: 20px;
  background: var(--secondary-bg);
  font-size: 13px;
  font-weight: 500;
  transition: all 0.15s;
  white-space: nowrap;
}

.chip.selected {
  background: var(--btn);
  color: var(--btn-text);
}

.chip:active {
  transform: scale(0.95);
}

/* === Buttons === */
.btn-primary {
  display: block;
  width: 100%;
  padding: 14px;
  background: var(--btn);
  color: var(--btn-text);
  border-radius: var(--radius);
  font-size: 16px;
  font-weight: 600;
  margin-top: 12px;
  transition: opacity 0.15s;
}

.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-primary:active:not(:disabled) {
  opacity: 0.85;
}

.btn-secondary {
  display: block;
  width: 100%;
  padding: 12px;
  background: var(--secondary-bg);
  color: var(--link);
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 600;
  margin-top: 12px;
}

/* === Result === */
.result-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 14px;
}

.result-meta .meta-tag {
  background: var(--secondary-bg);
  border-radius: 20px;
  padding: 4px 10px;
  font-size: 11px;
  color: var(--hint);
}

/* === Result Image === */
.result-image-wrap {
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: 12px;
  background: var(--secondary-bg);
  position: relative;
}

.result-image {
  display: block;
  width: 100%;
  height: auto;
  border-radius: var(--radius);
}

.result-prompt-label {
  font-size: 11px;
  color: var(--hint);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 4px;
  margin-top: 8px;
}

.result-content {
  background: var(--section-bg);
  border: 1px solid var(--divider);
  border-radius: var(--radius);
  padding: 16px;
  font-size: 14px;
  line-height: 1.65;
  white-space: pre-wrap;
  word-wrap: break-word;
  max-height: 55vh;
  overflow-y: auto;
}

.result-content.compact {
  max-height: 20vh;
  font-size: 12px;
  color: var(--hint);
}

.result-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 14px;
}

.action-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px;
  background: var(--secondary-bg);
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  transition: background 0.15s;
}

.action-btn:active {
  background: var(--divider);
}

.action-btn.favorited {
  background: #fff3cd;
  color: #b8860b;
}

.action-btn.favorited svg path {
  fill: #ffc107;
  stroke: #ffc107;
}

/* === History / Favorites cards === */
.screen-title {
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 14px;
}

.card-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.history-card {
  background: var(--section-bg);
  border: 1px solid var(--divider);
  border-radius: var(--radius);
  padding: 12px 14px;
  cursor: pointer;
  transition: transform 0.15s;
}

.history-card:active {
  transform: scale(0.98);
}

.history-card .hc-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
}

.history-card .hc-place {
  font-size: 15px;
  font-weight: 600;
}

.history-card .hc-type {
  font-size: 11px;
  color: var(--btn);
  background: var(--secondary-bg);
  padding: 2px 8px;
  border-radius: 10px;
}

.history-card .hc-preview {
  font-size: 13px;
  color: var(--hint);
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.history-card .hc-date {
  font-size: 11px;
  color: var(--hint);
  margin-top: 6px;
}

/* === Empty state === */
.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--hint);
  font-size: 14px;
}

/* === Loading === */
.loading {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 999;
  gap: 16px;
  color: #fff;
  font-size: 15px;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid rgba(255,255,255,0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* === Toast === */
.toast {
  position: fixed;
  bottom: calc(24px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: var(--bg);
  padding: 10px 20px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 500;
  opacity: 0;
  transition: all 0.3s ease;
  z-index: 1000;
  pointer-events: none;
  white-space: nowrap;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* === Utility === */
.hidden { display: none !important; }

/* === Scrollbar (webkit) === */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--hint); border-radius: 2px; }
