:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --border: #e5e7eb;
  --primary: #2563eb;
  --primary-soft: #eff6ff;
  --danger: #ef4444;
  --danger-soft: #fef2f2;
  --success: #059669;
  --success-soft: #ecfdf5;
  --amber: #d97706;
  --amber-soft: #fffbeb;
  --radius: 16px;
  --shadow: 0 2px 12px rgba(15, 23, 42, 0.06);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

.hidden {
  display: none !important;
}

/* Header */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border);
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
}

.header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  max-width: 960px;
  margin: 0 auto;
}

.brand h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.subtitle {
  margin: 2px 0 0;
  font-size: 12px;
  color: var(--muted);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.icon-btn {
  width: 38px;
  height: 38px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.icon-btn.spinning .refresh-icon {
  display: inline-block;
  animation: spin 0.9s linear infinite;
}

.icon-btn.notify-on {
  background: var(--success-soft);
  border-color: #6ee7b7;
  color: var(--success);
}

.new-badge {
  position: absolute;
  top: 10px;
  right: 10px;
  background: linear-gradient(135deg, #2563eb, #0ea5e9);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  padding: 4px 9px;
  border-radius: 999px;
  box-shadow: 0 2px 8px rgba(37, 99, 235, 0.35);
}

.primary-pill-btn {
  height: 38px;
  padding: 0 14px;
  border-radius: 999px;
  border: none;
  background: var(--primary);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.feed-controls {
  max-width: 960px;
  margin: 10px auto 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
}

.criteria-summary {
  font-size: 12px;
  color: var(--muted);
  flex: 1;
  min-width: 180px;
}

.sort-select {
  font-size: 13px;
  padding: 6px 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}

/* Main */
.app-main {
  max-width: 960px;
  margin: 0 auto;
  padding: 12px 14px calc(84px + env(safe-area-inset-bottom));
}

.crawl-banner {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--primary-soft);
  color: var(--primary);
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 12px;
}

.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(37, 99, 235, 0.25);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

.listing-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 14px;
}

@media (min-width: 720px) {
  .listing-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* Listing Card */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  border: 1px solid rgba(229, 231, 235, 0.8);
  display: flex;
  flex-direction: column;
}

.card.reappeared {
  border: 2px solid #f97316;
  box-shadow: 0 4px 18px rgba(249, 115, 22, 0.16);
}

.reappear-banner {
  background: linear-gradient(90deg, #ea580c, #f97316);
  color: #fff;
  padding: 8px 12px;
  font-size: 13px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.photo-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 10;
  background: #e5e7eb;
}

.photo-carousel {
  display: flex;
  width: 100%;
  height: 100%;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.photo-carousel::-webkit-scrollbar {
  display: none;
}

.photo-carousel img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  flex-shrink: 0;
  scroll-snap-align: start;
}

.photo-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 14px;
}

.source-badge {
  position: absolute;
  top: 10px;
  left: 10px;
  background: rgba(17, 24, 39, 0.72);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  padding: 4px 8px;
  border-radius: 999px;
  backdrop-filter: blur(6px);
}

.photo-count {
  position: absolute;
  bottom: 10px;
  right: 10px;
  background: rgba(17, 24, 39, 0.65);
  color: #fff;
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 999px;
}

.card-body {
  padding: 12px 14px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1;
}

.price-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}

.price-main {
  font-size: 22px;
  font-weight: 800;
  color: #dc2626;
  letter-spacing: -0.02em;
}

.price-unit {
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
  margin-left: 2px;
}

.price-old {
  font-size: 13px;
  color: var(--muted);
  text-decoration: line-through;
  margin-left: 6px;
}

.district-pill {
  font-size: 12px;
  font-weight: 600;
  color: var(--primary);
  background: var(--primary-soft);
  padding: 3px 8px;
  border-radius: 8px;
  white-space: nowrap;
}

.card-title {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.meta-line {
  font-size: 13px;
  color: #374151;
  display: flex;
  flex-wrap: wrap;
  gap: 6px 10px;
}

.address-line {
  font-size: 12px;
  color: var(--muted);
}

.tag-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.tag {
  font-size: 11px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 6px;
  background: #f3f4f6;
  color: #4b5563;
}

.tag.unfurnished {
  background: var(--success-soft);
  color: var(--success);
}

.tag.cook {
  background: var(--amber-soft);
  color: var(--amber);
}

.tag.subsidy {
  background: var(--primary-soft);
  color: var(--primary);
}

.note-box {
  margin-top: 2px;
  display: flex;
  gap: 6px;
}

.note-input {
  flex: 1;
  font-size: 13px;
  padding: 7px 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: #f9fafb;
}

.note-save-btn {
  padding: 0 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.hidden-info {
  font-size: 12px;
  color: var(--muted);
  background: #f9fafb;
  padding: 6px 10px;
  border-radius: 8px;
}

.card-actions {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  margin-top: auto;
  padding-top: 6px;
}

.btn {
  height: 40px;
  padding: 0 4px;
  border-radius: 11px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 3px;
  text-decoration: none;
}

.btn:active {
  transform: scale(0.97);
}

.btn-dislike {
  color: #6b7280;
}

.btn-dislike:hover {
  background: var(--danger-soft);
  color: var(--danger);
  border-color: #fecaca;
}

.btn-fav.active {
  background: var(--danger-soft);
  color: var(--danger);
  border-color: #fecaca;
}

.btn-nav {
  background: var(--success-soft);
  color: var(--success);
  border-color: #a7f3d0;
}

.btn-open {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

.btn-restore {
  background: var(--success-soft);
  color: var(--success);
  border-color: #a7f3d0;
}

/* Empty State */
.empty-state {
  text-align: center;
  padding: 56px 20px;
  color: var(--muted);
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 10px;
}

.empty-state h3 {
  margin: 0 0 6px;
  color: var(--text);
  font-size: 17px;
}

.empty-state p {
  margin: 0;
  font-size: 14px;
}

/* Bottom Tab Bar */
.bottom-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 25;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  background: rgba(255, 255, 255, 0.94);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-top: 1px solid var(--border);
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
}

.nav-item {
  border: none;
  background: transparent;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  position: relative;
  padding: 4px 0;
  color: var(--muted);
  cursor: pointer;
}

.nav-item.active {
  color: var(--primary);
}

.nav-icon {
  font-size: 20px;
  line-height: 1;
}

.nav-label {
  font-size: 11px;
  font-weight: 600;
}

.nav-badge {
  position: absolute;
  top: 0;
  right: calc(50% - 28px);
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 999px;
  background: #e5e7eb;
  color: #374151;
  font-size: 10px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.nav-item.active .nav-badge {
  background: var(--primary);
  color: #fff;
}

/* Settings Modal (Bottom Sheet on iPhone) */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(15, 23, 42, 0.48);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

@media (min-width: 640px) {
  .modal-backdrop {
    align-items: center;
    padding: 20px;
  }
}

.modal-sheet {
  width: 100%;
  max-width: 540px;
  max-height: 88dvh;
  background: var(--surface);
  border-radius: 22px 22px 0 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

@media (min-width: 640px) {
  .modal-sheet {
    border-radius: 20px;
  }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}

.modal-header h2 {
  margin: 0;
  font-size: 17px;
}

.close-btn {
  width: 34px;
  height: 34px;
  border-radius: 999px;
  border: none;
  background: #f3f4f6;
  font-size: 16px;
  cursor: pointer;
}

.modal-body {
  padding: 14px 18px calc(20px + env(safe-area-inset-bottom));
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.form-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.two-col {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.section-label, .field-label {
  font-size: 13px;
  font-weight: 700;
  color: #374151;
}

.form-section input[type="number"],
.form-section select {
  width: 100%;
  height: 42px;
  padding: 0 12px;
  border-radius: 11px;
  border: 1px solid var(--border);
  font-size: 16px; /* Prevents iOS Safari auto-zoom */
  background: #f9fafb;
  color: var(--text);
}

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

.mt-8 {
  margin-top: 6px;
}

.chip {
  padding: 7px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: #f9fafb;
  color: #374151;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
}

.chip.selected {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 6px 0;
}

.toggle-title {
  font-size: 14px;
  font-weight: 600;
}

.toggle-desc {
  font-size: 12px;
  color: var(--muted);
}

/* iOS Toggle Switch */
.switch {
  position: relative;
  display: inline-block;
  width: 48px;
  height: 28px;
  flex-shrink: 0;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  inset: 0;
  background: #d1d5db;
  border-radius: 999px;
  transition: 0.2s;
  cursor: pointer;
}

.slider::before {
  content: "";
  position: absolute;
  width: 22px;
  height: 22px;
  left: 3px;
  top: 3px;
  background: #fff;
  border-radius: 50%;
  transition: 0.2s;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}

.switch input:checked + .slider {
  background: var(--primary);
}

.switch input:checked + .slider::before {
  transform: translateX(20px);
}

.modal-footer {
  padding-top: 6px;
}

.save-btn {
  width: 100%;
  height: 46px;
  border-radius: 13px;
  border: none;
  background: var(--primary);
  color: #fff;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
}

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(76px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 60;
  background: rgba(17, 24, 39, 0.92);
  color: #fff;
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: calc(100vw - 32px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
}

.toast-undo {
  border: none;
  background: rgba(255, 255, 255, 0.18);
  color: #93c5fd;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 8px;
  cursor: pointer;
}
