:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --ink: #17181a;
  --muted: #5f6670;
  --line: #dde1e6;
  --accent: #bb0a30;
  --accent-dark: #8e0825;
  --green: #157347;
  --shadow: 0 1px 2px rgba(23, 24, 26, 0.06), 0 6px 18px rgba(23, 24, 26, 0.06);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  line-height: 1.45;
  min-height: 100vh;
}

button, input, select {
  font: inherit;
  color: inherit;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }

.brand-mark {
  flex: 0 0 auto;
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 15px;
  border-radius: 8px;
  letter-spacing: 0;
}

.brand h1 { font-size: 18px; font-weight: 650; line-height: 1.2; }
.brand p { font-size: 12px; color: var(--muted); margin-top: 2px; }

.filter-line {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 60vw;
}

.topbar-actions { display: flex; gap: 8px; flex: 0 0 auto; }

.note-pill {
  font-size: 12px;
  padding: 5px 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--muted);
  white-space: nowrap;
}

.note-pill.alt { border-color: var(--accent); color: var(--accent); background: #fdeef1; }

#change-btn {
  cursor: pointer;
  font: inherit;
}

#change-btn:hover {
  background: #fbd9df;
  border-color: var(--accent);
}

.check {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 40px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  font-size: 13px;
  white-space: nowrap;
  cursor: pointer;
}

.check input { accent-color: var(--accent); }

.toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 40px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  font-size: 13px;
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
}

.toggle input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.switch {
  position: relative;
  flex: 0 0 auto;
  width: 34px;
  height: 20px;
  border-radius: 999px;
  background: #c9ced4;
  transition: background 0.15s ease;
}

.switch::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.15s ease;
}

.toggle input:checked + .switch {
  background: var(--accent);
}

.toggle input:checked + .switch::after {
  transform: translateX(14px);
}

.toggle-label { color: var(--ink); }

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  padding: 16px 24px;
  max-width: 1440px;
  margin: 0 auto;
}

.controls input,
.controls select {
  height: 40px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  outline: none;
}

.controls input:focus,
.controls select:focus { border-color: var(--accent); }

.search-wrap { flex: 1 1 240px; min-width: 200px; }
.search-wrap input { width: 100%; }

.controls select { min-width: 150px; }
.range-wrap { min-width: 140px; }
.range-wrap input { width: 100%; }

.ghost-btn {
  height: 40px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  cursor: pointer;
  font-size: 14px;
}

.ghost-btn:hover { border-color: var(--accent); color: var(--accent); }

.grid {
  max-width: 1440px;
  margin: 0 auto;
  padding: 8px 24px 48px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 18px;
}

.card {
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  box-shadow: var(--shadow);
  min-width: 0;
}

.thumb {
  position: relative;
  aspect-ratio: 16 / 10;
  background: #e9ebee;
  overflow: hidden;
}

.thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.top-left-tags,
.top-right-tags {
  position: absolute;
  top: 10px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  z-index: 3;
  max-width: calc(100% - 20px);
}

.top-left-tags { left: 10px; }
.top-right-tags { right: 10px; }

.year-badge {
  background: rgba(23, 24, 26, 0.82);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  padding: 4px 9px;
  border-radius: 6px;
}

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

.card-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }

.card-title { font-size: 17px; font-weight: 650; line-height: 1.25; }

.cert-badge {
  font-size: 11px;
  font-weight: 700;
  color: #fff;
  background: var(--green);
  border-radius: 6px;
  padding: 3px 7px;
}

.cert-badge[style*="none"] { display: none; }

.rating-badge {
  flex: 0 0 auto;
  font-size: 11px;
  font-weight: 700;
  color: #8a5a00;
  background: #fdf3dd;
  border-radius: 6px;
  padding: 3px 7px;
}

.rating-badge.good { color: var(--green); background: #e5f4ec; }
.rating-badge:empty { display: none; }

.plus-badge {
  flex: 0 0 auto;
  font-size: 11px;
  font-weight: 700;
  color: #fff;
  background: var(--accent);
  border-radius: 6px;
  padding: 3px 7px;
}

.accident-badge {
  flex: 0 0 auto;
  font-size: 11px;
  font-weight: 700;
  color: #6b4f00;
  background: #ffe680;
  border-radius: 6px;
  padding: 3px 7px;
}

.owner-badge {
  flex: 0 0 auto;
  font-size: 11px;
  font-weight: 700;
  color: #6b4f00;
  background: #ffe680;
  border-radius: 6px;
  padding: 3px 7px;
}

.new-badge {
  flex: 0 0 auto;
  font-size: 11px;
  font-weight: 700;
  color: #fff;
  background: #1565c0;
  border-radius: 6px;
  padding: 3px 7px;
}

.change-badge {
  flex: 0 0 auto;
  font-size: 11px;
  font-weight: 700;
  color: #fff;
  background: #e8860a;
  border-radius: 6px;
  padding: 3px 7px;
}

.change-badge.down { background: var(--green); }
.change-badge.up { background: #c62828; }

.sold-badge {
  font-size: 11px;
  font-weight: 700;
  color: #fff;
  background: #6b7280;
  border-radius: 6px;
  padding: 3px 7px;
}

.sold-badge[style*="none"] { display: none; }

.color-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  min-height: 24px;
}

.mileage-tag,
.days-tag {
  position: absolute;
  bottom: 10px;
  background: rgba(23, 24, 26, 0.82);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 6px;
  z-index: 3;
}

.mileage-tag { left: 10px; }
.days-tag { right: 10px; }

.mileage-tag:empty,
.days-tag:empty { display: none; }

.color-tag {
  font-size: 11px;
  font-weight: 700;
  border-radius: 6px;
  padding: 3px 7px;
  white-space: nowrap;
}

.card.new { box-shadow: 0 0 0 2px #1565c0, var(--shadow); }
.card.changed { box-shadow: 0 0 0 2px #e8860a, var(--shadow); }

.card.sold {
  opacity: 0.72;
  filter: grayscale(0.55);
  border-style: dashed;
}

.price-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  padding: 9px 0;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}

.spec { font-size: 14px; }
.spec.price { font-weight: 700; font-size: 16px; }
.spec .was { color: var(--muted); font-weight: 400; font-size: 13px; }

.value-tag {
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
}

.value-tag.below { color: var(--green); }
.value-tag:not(.below) { color: #c98a00; }
.value-tag[style*="none"] { display: none; }

.meta-row {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-size: 12px;
  color: var(--muted);
}

.vin { font-variant-numeric: tabular-nums; }

.dealer-link,
.town-link {
  color: var(--ink);
  text-decoration: none;
}

.dealer-link:hover,
.town-link:hover {
  color: var(--accent);
  text-decoration: underline;
}

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

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 38px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  text-decoration: none;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  min-width: 0;
  white-space: nowrap;
}

.btn svg { width: 15px; height: 15px; flex: 0 0 auto; }

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

.btn.carfax { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.carfax:hover { background: var(--accent-dark); }

.btn.pdf { color: var(--green); border-color: #b7dac8; }
.btn.pdf:hover { background: #e5f4ec; }

.btn[hidden] { display: none; }

.empty {
  grid-column: 1 / -1;
  text-align: center;
  color: var(--muted);
  padding: 80px 20px;
  font-size: 15px;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(23, 24, 26, 0.55);
  display: grid;
  place-items: center;
  z-index: 50;
  padding: 24px;
}

.modal-backdrop.hidden { display: none; }

.modal {
  position: relative;
  width: min(920px, 100%);
  max-height: 88vh;
  overflow: auto;
  background: var(--surface);
  border-radius: 8px;
  padding: 26px 28px 30px;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.25);
}

.icon-btn {
  position: sticky;
  float: right;
  top: 4px;
  right: 0;
  width: 28px;
  height: 28px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  cursor: pointer;
  z-index: 5;
  margin-left: 10px;
}

.icon-btn svg { width: 14px; height: 14px; }

.viewer-modal {
  width: min(1080px, 100%);
  height: 92vh;
  display: flex;
  flex-direction: column;
  padding: 16px 18px 18px;
}

.viewer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
  padding-right: 44px;
}

.viewer-head h3 {
  font-size: 16px;
  font-weight: 650;
}

#viewer-frame {
  flex: 1;
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.detail-hero { display: flex; gap: 22px; align-items: flex-start; flex-wrap: wrap; }

.detail-media {
  flex: 0 1 420px;
  min-width: 260px;
  width: 100%;
  max-width: 480px;
}

.detail-main-img {
  width: 100%;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #e9ebee;
  aspect-ratio: 16 / 10;
  object-fit: cover;
}

.detail-gallery {
  max-height: 58vh;
  overflow-y: auto;
  display: grid;
  gap: 10px;
  padding-right: 4px;
}

.detail-gallery img {
  width: 100%;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #e9ebee;
  aspect-ratio: 16 / 10;
  object-fit: cover;
}

.detail-info { flex: 1 1 320px; min-width: 260px; }

.detail-title { font-size: 22px; font-weight: 700; margin-bottom: 4px; }
.detail-subtitle { color: var(--muted); font-size: 14px; margin-bottom: 14px; }

.detail-trim {
  font-size: 15px;
  font-weight: 600;
  color: var(--ink);
  margin: 0 0 10px;
}

.detail-address {
  font-size: 13px;
  color: var(--muted);
  margin: 0 0 12px;
}

.detail-address .address-link {
  color: var(--accent);
  text-decoration: underline;
}

.detail-address .address-link:hover {
  color: var(--accent-dark);
}

.detail-price { font-size: 26px; font-weight: 750; margin-bottom: 12px; }

.detail-list { list-style: none; display: grid; gap: 6px; font-size: 14px; }
.detail-list li { display: flex; justify-content: space-between; gap: 16px; padding-bottom: 6px; border-bottom: 1px dashed var(--line); }
.detail-list dt { color: var(--muted); }
.detail-list dd { text-align: right; font-weight: 550; }

.detail-actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 18px; }
.detail-actions .btn { height: 42px; padding: 0 16px; }

.section-block { margin-top: 22px; }
.section-block h3 {
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 0;
  color: var(--muted);
  margin-bottom: 8px;
}

.section-block p, .section-block li { font-size: 14px; }
.options-list {
  list-style: none;
  display: grid;
  gap: 4px;
  max-height: 260px;
  overflow: auto;
  padding-right: 6px;
}

.options-list li { padding: 3px 0; border-bottom: 1px dashed var(--line); }

.tech-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 8px;
}

.tech-item {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 12px;
}

.tech-item strong { display: block; font-size: 13px; }

.desc-text { white-space: pre-wrap; color: #3a4046; }

.recon-value {
  display: inline-block;
  margin-left: 8px;
  font-size: 12px;
  font-weight: 700;
  color: var(--green);
  background: #e5f4ec;
  border-radius: 6px;
  padding: 2px 8px;
  vertical-align: middle;
}

.drop-badge {
  display: inline-block;
  margin-left: 8px;
  font-size: 13px;
  font-weight: 700;
  color: var(--green);
  background: #e5f4ec;
  border-radius: 6px;
  padding: 3px 8px;
  vertical-align: middle;
}

.deal-line { font-size: 14px; font-weight: 600; color: var(--green); margin-bottom: 12px; }

.history-list {
  list-style: none;
  display: grid;
  gap: 4px;
  max-height: 240px;
  overflow: auto;
  padding-right: 6px;
}

.history-list li {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  font-size: 13px;
  padding: 4px 0;
  border-bottom: 1px dashed var(--line);
}

.history-list li span:last-child { font-weight: 600; font-variant-numeric: tabular-nums; }

.modal-title { font-size: 20px; font-weight: 700; margin-bottom: 4px; padding-right: 40px; }
.modal-sub { font-size: 13px; color: var(--muted); margin-bottom: 14px; }

.change-list {
  list-style: none;
  display: grid;
  gap: 8px;
  max-height: 60vh;
  overflow-y: auto;
  padding-right: 6px;
}

.change-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.change-type {
  flex: 0 0 auto;
  font-size: 11px;
  font-weight: 700;
  border-radius: 6px;
  padding: 3px 8px;
  text-transform: uppercase;
  min-width: 52px;
  text-align: center;
}

.change-type.new { background: #1565c0; color: #fff; }
.change-type.sold { background: #6b7280; color: #fff; }
.change-type.price { background: #e8860a; color: #fff; }

.change-info {
  flex: 1 1 auto;
  min-width: 0;
  display: grid;
  gap: 2px;
}

.change-title {
  font-size: 14px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.change-meta {
  font-size: 12px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.change-delta {
  flex: 0 0 auto;
  font-size: 14px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.change-delta.down { color: var(--green); }
.change-delta.up { color: #c62828; }

.change-at {
  flex: 0 0 auto;
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.change-empty {
  text-align: center;
  color: var(--muted);
  padding: 40px 20px;
  font-size: 14px;
}

@media (max-width: 720px) {
  .topbar { flex-wrap: wrap; padding: 12px 16px; }
  .controls { padding: 12px 16px; }
  .grid { padding: 8px 16px 40px; grid-template-columns: 1fr; }
  .actions { grid-template-columns: 1fr 1fr; }
  .actions .btn.pdf { grid-column: 1 / -1; }
  .modal { padding: 20px 18px 24px; }
  .detail-actions .btn { flex: 1 1 auto; }
  .detail-media { max-width: none; }
  .detail-gallery {
    display: flex;
    gap: 10px;
    overflow-x: auto;
    overflow-y: hidden;
    max-height: none;
    scroll-snap-type: x mandatory;
    padding-bottom: 6px;
  }
  .detail-gallery img {
    flex: 0 0 86vw;
    scroll-snap-align: start;
    aspect-ratio: 16 / 10;
  }
}
