:root {
  color-scheme: light;
  --surface-1: #fcfcfb;
  --surface-2: #ffffff;
  --surface-3: #f3f3f0;
  --border: #dedcd5;
  --border-strong: #c4c2b8;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #78766f;
  --accent: #2a78d6;
  --good: #0ca30c;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --radius: 8px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --surface-1: #1a1a19;
    --surface-2: #232322;
    --surface-3: #2c2c2a;
    --border: #3a3a37;
    --border-strong: #4d4d49;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #96958c;
    --accent: #3987e5;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-1: #1a1a19;
  --surface-2: #232322;
  --surface-3: #2c2c2a;
  --border: #3a3a37;
  --border-strong: #4d4d49;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #96958c;
  --accent: #3987e5;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--surface-1);
  color: var(--text-primary);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------------------------------------------------------------- nav --- */

header.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 12px 24px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
  flex-wrap: wrap;
}

.brand {
  font-weight: 650;
  font-size: 15px;
  color: var(--text-primary);
  letter-spacing: -0.01em;
}
.brand:hover { text-decoration: none; }
.brand span { color: var(--text-muted); font-weight: 400; }

nav.links { display: flex; gap: 4px; }

nav.links a {
  padding: 5px 11px;
  border-radius: 6px;
  color: var(--text-secondary);
  font-weight: 500;
}
nav.links a:hover { background: var(--surface-3); text-decoration: none; }
nav.links a.active { background: var(--surface-3); color: var(--text-primary); }

.topbar-right {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 12px;
  color: var(--text-muted);
}

main { padding: 24px; max-width: 1500px; margin: 0 auto; }

h1 {
  font-size: 20px;
  font-weight: 650;
  margin: 0 0 4px;
  letter-spacing: -0.01em;
}

.subtitle { color: var(--text-muted); font-size: 13px; margin: 0 0 20px; }

/* --------------------------------------------------------- stat tiles --- */

.kpi-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
  margin-bottom: 20px;
}

.stat-tile {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}

.stat-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  font-weight: 600;
  margin-bottom: 6px;
}

.stat-value {
  font-size: 26px;
  font-weight: 600;
  letter-spacing: -0.02em;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}

.stat-sub { font-size: 12px; color: var(--text-muted); margin-top: 3px; }

/* ------------------------------------------------------------ filters --- */

.filters {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 16px;
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: flex-end;
}

.field { display: flex; flex-direction: column; gap: 4px; }
.field.grow { flex: 1 1 220px; }

.field label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}

input[type="text"], input[type="date"], input[type="number"], select {
  background: var(--surface-1);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  padding: 6px 9px;
  font: inherit;
  font-size: 13px;
  color: var(--text-primary);
  min-width: 0;
}

input:focus, select:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  border-color: var(--accent);
}

.btn {
  background: var(--accent);
  color: #fff;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 7px 14px;
  font: inherit;
  font-size: 13px;
  font-weight: 550;
  cursor: pointer;
}
.btn:hover { filter: brightness(1.08); text-decoration: none; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }

.btn.secondary {
  background: var(--surface-1);
  color: var(--text-secondary);
  border-color: var(--border-strong);
}

/* -------------------------------------------------------------- table --- */

.table-wrap {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow-x: auto;
}

table { width: 100%; border-collapse: collapse; font-size: 13px; }

thead th {
  text-align: left;
  padding: 10px 14px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  font-weight: 600;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  background: var(--surface-2);
  position: sticky;
  top: 0;
}

tbody td {
  padding: 11px 14px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--surface-3); }

td.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
td.addr { font-weight: 550; min-width: 200px; }
.party {
  color: var(--text-secondary);
  font-size: 12px;
  max-width: 260px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* ------------------------------------------------------------- badges --- */

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 8px;
  border-radius: 100px;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
  border: 1px solid var(--border-strong);
  color: var(--text-secondary);
  background: var(--surface-1);
}

.badge .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
  flex: none;
}

.badge.open { color: var(--good); border-color: color-mix(in srgb, var(--good) 40%, transparent); }
.badge.closed { color: var(--text-muted); }
.badge.adjourned { color: var(--serious); border-color: color-mix(in srgb, var(--serious) 45%, transparent); }
.badge.sold { color: var(--accent); border-color: color-mix(in srgb, var(--accent) 40%, transparent); }
.badge.cancelled { color: var(--critical); border-color: color-mix(in srgb, var(--critical) 40%, transparent); }

.soon { color: var(--critical); font-weight: 600; }

/* ------------------------------------------------------------- detail --- */

.detail-grid {
  display: grid;
  grid-template-columns: minmax(0, 2fr) minmax(260px, 1fr);
  gap: 16px;
  align-items: start;
}

@media (max-width: 900px) {
  .detail-grid { grid-template-columns: 1fr; }
}

.card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  margin-bottom: 16px;
}

.card h2 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin: 0 0 14px;
  font-weight: 600;
}

dl.fields { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: 9px 20px; }
dl.fields dt { color: var(--text-muted); font-size: 12px; white-space: nowrap; }
dl.fields dd { margin: 0; font-size: 13px; overflow-wrap: anywhere; }

.hero-value {
  font-size: 30px;
  font-weight: 600;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}

pre.note {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font: 12px/1.6 ui-monospace, SFMono-Regular, Menlo, monospace;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px;
  margin: 0;
  color: var(--text-secondary);
  max-height: 320px;
  overflow-y: auto;
}

/* ------------------------------------------------------------ history --- */

ol.timeline { list-style: none; margin: 0; padding: 0 0 0 18px; position: relative; }
ol.timeline::before {
  content: "";
  position: absolute;
  left: 3px;
  top: 5px;
  bottom: 5px;
  width: 2px;
  background: var(--border);
}
ol.timeline li { position: relative; padding: 0 0 14px; font-size: 13px; }
ol.timeline li:last-child { padding-bottom: 0; }
ol.timeline li::before {
  content: "";
  position: absolute;
  left: -18px;
  top: 5px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--border-strong);
  border: 2px solid var(--surface-2);
}
ol.timeline li:last-child::before { background: var(--accent); }
ol.timeline .when { color: var(--text-muted); font-size: 12px; }

/* ---------------------------------------------------------------- map --- */

#map {
  height: calc(100vh - 190px);
  min-height: 420px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.leaflet-popup-content { font: 13px/1.45 -apple-system, BlinkMacSystemFont, sans-serif; margin: 12px 14px; }
.leaflet-popup-content b { display: block; margin-bottom: 3px; }
.leaflet-popup-content .meta { color: #52514e; font-size: 12px; }

/* -------------------------------------------------------------- misc --- */

.empty { padding: 48px 24px; text-align: center; color: var(--text-muted); }
.empty p { margin: 0 0 14px; }

.flash {
  padding: 10px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border-strong);
  background: var(--surface-3);
  margin-bottom: 16px;
  font-size: 13px;
}
.flash.running { border-color: color-mix(in srgb, var(--accent) 45%, transparent); }
.flash.error { border-color: color-mix(in srgb, var(--critical) 50%, transparent); color: var(--critical); }

.count-note { color: var(--text-muted); font-size: 12px; margin: 10px 2px 0; }

.spinner {
  display: inline-block;
  width: 11px;
  height: 11px;
  border: 2px solid color-mix(in srgb, var(--accent) 30%, transparent);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: -1px;
  margin-right: 5px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* --------------------------------------------------------------- tabs --- */

.tabs {
  display: flex;
  gap: 2px;
  border-bottom: 1px solid var(--border);
  margin: 0 0 18px;
}

.tab {
  padding: 8px 15px;
  border: none;
  background: none;
  font: inherit;
  font-size: 13px;
  font-weight: 550;
  color: var(--text-secondary);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  border-radius: 6px 6px 0 0;
}

.tab:hover { background: var(--surface-3); color: var(--text-primary); }
.tab[aria-selected="true"] { color: var(--accent); border-bottom-color: var(--accent); }
.tab:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

/* -------------------------------------------------------- street view --- */

.sv-bar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}

.sv-addr { font-weight: 600; font-size: 14px; }

.sv-sub {
  color: var(--text-muted);
  font-size: 12px;
  margin-top: 4px;
  font-variant-numeric: tabular-nums;
}

.sv-actions { display: flex; gap: 8px; flex: none; flex-wrap: wrap; }

.sv-frame-wrap {
  position: relative;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
  background: var(--surface-3);
  height: min(62vh, 560px);
  min-height: 340px;
}

.sv-frame-wrap iframe {
  position: relative;
  z-index: 1;
  width: 100%;
  height: 100%;
  border: 0;
  display: block;
}

.sv-placeholder {
  position: absolute;
  inset: 0;
  z-index: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: var(--text-muted);
  font-size: 13px;
  text-align: center;
  padding: 24px;
}

.sv-placeholder .spinner { margin: 0; width: 15px; height: 15px; }

.sv-note {
  color: var(--text-muted);
  font-size: 12px;
  line-height: 1.6;
  margin: 12px 0 0;
  max-width: 78ch;
}

.sv-empty {
  text-align: center;
  padding: 46px 20px;
  color: var(--text-muted);
}

.sv-empty p { font-size: 13px; line-height: 1.7; margin: 0; }

.sv-icon {
  width: 36px;
  height: 36px;
  color: var(--border-strong);
  margin-bottom: 12px;
}

/* ------------------------------------------------------------ favorites --- */

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.star {
  background: none;
  border: 0;
  padding: 2px;
  cursor: pointer;
  line-height: 0;
  border-radius: 4px;
  color: var(--border-strong);
}

.star svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 1.7; }
.star:hover { color: var(--warning); }
.star.on { color: var(--warning); }
.star.on svg { fill: currentColor; stroke: currentColor; }
.star:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.star.star-error { color: var(--critical); }

th.star-col, td.star-col { width: 30px; padding-right: 0; }

.favorites { margin-bottom: 22px; }

.section-head {
  display: flex;
  align-items: baseline;
  gap: 9px;
  margin-bottom: 10px;
  flex-wrap: wrap;
}

.section-head h2 {
  font-size: 13px;
  font-weight: 650;
  margin: 0;
  letter-spacing: -0.01em;
}

.section-count {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  background: var(--surface-3);
  border: 1px solid var(--border);
  border-radius: 100px;
  padding: 1px 8px;
  font-variant-numeric: tabular-nums;
}

.section-note { font-size: 12px; color: var(--text-muted); }

h1.detail-title {
  display: flex;
  align-items: center;
  gap: 9px;
}
h1.detail-title .star svg { width: 21px; height: 21px; }

/* -------------------------------------------------------- notes & files --- */

.tab-count {
  display: inline-block;
  font-size: 10px;
  font-weight: 600;
  background: var(--surface-3);
  border: 1px solid var(--border);
  border-radius: 100px;
  padding: 0 5px;
  color: var(--text-muted);
  vertical-align: 1px;
}

.notes-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.35fr) minmax(300px, 1fr);
  gap: 16px;
  align-items: start;
}

@media (max-width: 900px) {
  .notes-grid { grid-template-columns: 1fr; }
}

#notes-field {
  width: 100%;
  background: var(--surface-1);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  padding: 12px;
  font: 13px/1.65 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--text-primary);
  resize: vertical;
  min-height: 200px;
}

#notes-field:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  border-color: var(--accent);
}

.notes-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 12px;
}

.notes-hint { font-size: 12px; color: var(--text-muted); }

.save-status { font-size: 12px; color: var(--text-muted); margin-left: auto; }
.save-status.ok { color: var(--good); }
.save-status.pending { color: var(--serious); }
.save-status.err { color: var(--critical); }

/* upload */

.upload-form { margin-bottom: 16px; }
.upload-form input[type="file"] {
  position: absolute;
  width: 1px; height: 1px;
  opacity: 0;
  pointer-events: none;
}

.upload-drop {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  text-align: center;
  padding: 22px 16px;
  border: 1.5px dashed var(--border-strong);
  border-radius: 8px;
  cursor: pointer;
  background: var(--surface-1);
  margin-bottom: 10px;
}

.upload-drop:hover { border-color: var(--accent); background: var(--surface-3); }
.upload-drop svg { width: 24px; height: 24px; color: var(--text-muted); }
.upload-label { font-size: 13px; font-weight: 550; }
.upload-name { font-size: 11.5px; color: var(--text-muted); overflow-wrap: anywhere; }

.file-list { list-style: none; margin: 0; padding: 0; }

.file-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 0;
  border-top: 1px solid var(--border);
}

.file-meta { min-width: 0; flex: 1; }
.file-meta > a { font-size: 13px; font-weight: 550; overflow-wrap: anywhere; }
.file-sub { font-size: 11.5px; color: var(--text-muted); margin-top: 2px; }

.file-empty { font-size: 12.5px; color: var(--text-muted); margin: 0; }

.btn-icon {
  background: none;
  border: 0;
  padding: 4px;
  cursor: pointer;
  color: var(--text-muted);
  border-radius: 4px;
  line-height: 0;
  flex: none;
}
.btn-icon svg { width: 15px; height: 15px; }
.btn-icon:hover { color: var(--critical); background: var(--surface-3); }
.btn-icon:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.sv-modes { display: inline-flex; gap: 2px; margin-bottom: 12px; background: var(--surface-3);
  border: 1px solid var(--border); border-radius: 7px; padding: 2px; }
.mode-btn { border: 0; background: none; font: inherit; font-size: 12px; font-weight: 550;
  color: var(--text-secondary); padding: 5px 12px; border-radius: 5px; cursor: pointer; }
.mode-btn:hover { color: var(--text-primary); }
.mode-btn.on { background: var(--surface-2); color: var(--text-primary);
  box-shadow: 0 1px 2px rgba(0,0,0,.08); }
.mode-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.sv-note strong { color: var(--text-secondary); font-weight: 600; }

/* ------------------------------------------------------------ ai summary --- */

.ai-summary { font-size: 13.5px; line-height: 1.65; max-width: 78ch; }
.ai-summary.empty { color: var(--text-muted); font-size: 13px; }
.ai-summary h3 { font-size: 13px; font-weight: 650; margin: 20px 0 7px;
  letter-spacing: -0.01em; text-transform: none; color: var(--text-primary); }
.ai-summary h4 { font-size: 12.5px; font-weight: 620; margin: 16px 0 6px; }
.ai-summary h3:first-child, .ai-summary h4:first-child { margin-top: 0; }
.ai-summary p { margin: 0 0 11px; }
.ai-summary ul { margin: 0 0 12px; padding-left: 20px; }
.ai-summary li { margin-bottom: 5px; }
.ai-summary strong { font-weight: 620; }
.ai-waiting { color: var(--text-muted); }
#system-prompt { width: 100%; background: var(--surface-1); border: 1px solid var(--border-strong);
  border-radius: 6px; padding: 12px; color: var(--text-primary); resize: vertical;
  font: 12.5px/1.6 ui-monospace, SFMono-Regular, Menlo, monospace; }
#system-prompt:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }

/* --------------------------------------------------------------- diff --- */

td.was { color: var(--text-muted); text-decoration: line-through; }
td.now { font-weight: 600; }

.schedule-form { display: flex; align-items: flex-end; gap: 16px; flex-wrap: wrap; }
.check { display: flex; align-items: center; gap: 8px; font-size: 13px; padding-bottom: 7px; cursor: pointer; }
.check input { width: 15px; height: 15px; accent-color: var(--accent); }

/* ------------------------------------------------------------- market --- */

.market-form { display: flex; align-items: flex-end; gap: 16px; flex-wrap: wrap; }
.market-est { font-size: 12px; color: var(--text-secondary); padding-bottom: 7px; }
.market-est .notes-hint { display: block; margin-top: 2px; }
#market-prompt { width: 100%; background: var(--surface-1); border: 1px solid var(--border-strong);
  border-radius: 6px; padding: 12px; color: var(--text-primary); resize: vertical;
  font: 12.5px/1.6 ui-monospace, SFMono-Regular, Menlo, monospace; }
#market-prompt:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }

.md-table { overflow-x: auto; margin: 0 0 14px; }
.md-table table { width: auto; min-width: 60%; border-collapse: collapse; font-size: 12.5px; }
.md-table th, .md-table td { border: 1px solid var(--border); padding: 6px 11px; text-align: left; }
.md-table th { background: var(--surface-3); font-weight: 620; font-size: 11px;
  text-transform: uppercase; letter-spacing: .04em; color: var(--text-muted); }
.md-table td { font-variant-numeric: tabular-nums; }

/* -------------------------------------------------------------- login --- */

body.login-page { display: flex; align-items: center; justify-content: center;
  min-height: 100vh; padding: 24px; }
.login-card { background: var(--surface-2); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 26px 28px; width: 100%; max-width: 340px; }
.login-card .field { display: block; }
.login-card input[type="password"] { width: 100%; }
