:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f3f2ee;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-ink: #1c5cab;
  --accent-wash: rgba(42, 120, 214, 0.10);
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --good-ink: #006300;
  --bad-ink: #b42f2f;
  --warn-bg: #fdf3dc;
  --div-strong-low: #1c5cab;
  --div-low: #6da7ec;
  --div-mid: #a3a29b;
  --div-high: #ec8f8e;
  --div-strong-high: #c23b3a;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232321;
    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --accent: #3987e5;
    --accent-ink: #86b6ef;
    --accent-wash: rgba(57, 135, 229, 0.16);
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --good-ink: #2fbf2f;
    --bad-ink: #ee7b7b;
    --warn-bg: #3a2f14;
    --div-strong-low: #3987e5;
    --div-low: #86b6ef;
    --div-mid: #6f6e68;
    --div-high: #e89a99;
    --div-strong-high: #e66767;
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232321; --ink: #ffffff; --ink-2: #c3c2b7; --muted: #898781;
  --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10); --accent: #3987e5; --accent-ink: #86b6ef;
  --accent-wash: rgba(57, 135, 229, 0.16); --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; --good-ink: #2fbf2f;
  --bad-ink: #ee7b7b; --warn-bg: #3a2f14; --div-strong-low: #3987e5; --div-low: #86b6ef; --div-mid: #6f6e68;
  --div-high: #e89a99; --div-strong-high: #e66767; --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--page);
  color: var(--ink);
  font: 14px/1.45 var(--font);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent-ink); }
.muted { color: var(--muted); }
.num, .tnum { font-variant-numeric: tabular-nums; }

/* Top bar */
.topbar {
  display: flex; align-items: center; gap: 24px;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; flex-direction: column; line-height: 1.2; }
.brand strong { font-size: 16px; }
.brand span { color: var(--ink-2); font-size: 12px; }
.tabs { display: flex; gap: 4px; }
.tabs button {
  border: 0; background: none; padding: 8px 12px; border-radius: 8px; cursor: pointer; color: var(--ink-2);
}
.tabs button[aria-selected="true"] { background: var(--accent-wash); color: var(--accent-ink); font-weight: 600; }
.tabs button:hover { background: var(--surface-2); }
.status { margin-left: auto; display: flex; align-items: center; gap: 10px; font-size: 13px; }
.btn {
  border: 1px solid var(--border); background: var(--surface); padding: 6px 12px; border-radius: 8px; cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn:disabled { opacity: 0.55; cursor: progress; }

/* Tabs */
.tab { display: none; flex: 1; min-height: 0; flex-direction: column; position: relative; }
.tab[data-active] { display: flex; }

/* Filters */
.filters {
  display: flex; flex-wrap: wrap; gap: 8px 12px; align-items: center;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: none; padding: 5px 10px; cursor: pointer; color: var(--ink-2); }
.seg button + button { border-left: 1px solid var(--border); }
.seg button[aria-pressed="true"] { background: var(--accent-wash); color: var(--accent-ink); font-weight: 600; }
.range { display: inline-flex; align-items: center; gap: 4px; color: var(--ink-2); }
.range span { font-size: 12px; margin-right: 2px; }
.range i { font-style: normal; color: var(--muted); }
.range input, .range select, .search {
  width: 84px; padding: 5px 8px; border: 1px solid var(--border); border-radius: 8px; background: var(--page);
}
.range select { width: auto; }
.search { width: 220px; }
.chips { display: inline-flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.chips .lbl { font-size: 12px; color: var(--ink-2); margin-right: 2px; }
.chips button {
  border: 1px solid var(--border); background: none; border-radius: 999px; padding: 3px 10px; cursor: pointer;
  color: var(--ink-2); font-size: 13px;
}
.chips button[aria-pressed="true"] { background: var(--accent-wash); border-color: transparent; color: var(--accent-ink); font-weight: 600; }
.chips button.link { border: 0; text-decoration: underline; color: var(--muted); }
.toggles { flex-basis: 100%; }

/* Split layout */
.split { position: relative; flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(380px, 520px) 1fr; }
.list-pane { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--border); }
.list-head { display: flex; justify-content: space-between; align-items: center; padding: 8px 16px; font-size: 13px; }
.sort select { margin-left: 6px; padding: 3px 6px; border-radius: 6px; border: 1px solid var(--border); background: var(--surface); }
.cards { overflow-y: auto; padding: 0 12px 24px; display: flex; flex-direction: column; gap: 8px; }
.map-pane { position: relative; min-height: 0; }
.map { position: absolute; inset: 0; background: var(--surface-2); }

/* Listing card */
.lcard {
  display: grid; grid-template-columns: 128px 1fr; gap: 12px; padding: 8px; border-radius: var(--radius);
  background: var(--surface); border: 1px solid var(--border); cursor: pointer; position: relative;
}
.lcard:hover, .lcard.hl { border-color: var(--accent); }
.lcard.sel { box-shadow: 0 0 0 2px var(--accent); }
.lcard.removed { opacity: 0.6; }
.lcard.rejected { opacity: 0.45; }
.lcard .ph { width: 128px; height: 100px; border-radius: 6px; object-fit: cover; background: var(--surface-2); }
.lcard .body { min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.lcard .row1 { display: flex; align-items: baseline; gap: 8px; }
.lcard .price { font-size: 17px; font-weight: 650; }
.lcard .m2 { color: var(--ink-2); font-size: 13px; }
.lcard .title { font-size: 13px; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lcard .facts { font-size: 13px; display: flex; flex-wrap: wrap; gap: 4px 10px; }
.lcard .meta { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lcard .badges { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 2px; }
.lcard .acts { position: absolute; top: 6px; right: 6px; display: flex; gap: 2px; }

.badge {
  display: inline-flex; align-items: center; gap: 4px; font-size: 12px; padding: 1px 7px; border-radius: 999px;
  background: var(--surface-2); color: var(--ink-2); white-space: nowrap;
}
.badge.good { color: var(--good-ink); }
.badge.bad { color: var(--bad-ink); }
.badge.new { background: var(--accent-wash); color: var(--accent-ink); font-weight: 600; }
.badge.warn { background: var(--warn-bg); color: var(--ink); }
.badge .dot { width: 8px; height: 8px; border-radius: 50%; }
.dpe {
  display: inline-grid; place-items: center; width: 18px; height: 18px; border-radius: 4px; font-size: 11px;
  font-weight: 700; color: #fff;
}
.dpe-A { background: #009c6d; } .dpe-B { background: #52b153; } .dpe-C { background: #78bd76; color: #0b0b0b; }
.dpe-D { background: #f4e70f; color: #0b0b0b; } .dpe-E { background: #f0b40f; color: #0b0b0b; }
.dpe-F { background: #eb8235; color: #0b0b0b; } .dpe-G { background: #d7221f; }
.dpe-NS, .dpe-VI { background: var(--surface-2); color: var(--ink-2); width: auto; padding: 0 4px; }

.icon-btn {
  width: 28px; height: 28px; display: inline-grid; place-items: center; border-radius: 6px; border: 0;
  background: var(--surface); color: var(--muted); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--ink); }
.icon-btn[aria-pressed="true"].fav { color: #d89a00; }
.icon-btn[aria-pressed="true"].visit { color: var(--accent); }
.icon-btn[aria-pressed="true"].rejected { color: var(--bad-ink); }
.icon-btn svg { width: 16px; height: 16px; }

/* Map */
.map-legend {
  position: absolute; left: 12px; bottom: 22px; z-index: 500; background: var(--surface); border-radius: 8px;
  padding: 8px 10px; font-size: 12px; box-shadow: var(--shadow); color: var(--ink-2); max-width: calc(100% - 24px);
}
.map-legend .ttl { color: var(--ink); font-weight: 600; margin-bottom: 4px; }
.map-legend .items { display: flex; flex-wrap: wrap; gap: 4px 10px; }
.map-legend .it { display: inline-flex; align-items: center; gap: 5px; }
.map-legend .sw { width: 10px; height: 10px; border-radius: 50%; border: 2px solid var(--surface); box-shadow: 0 0 0 1px var(--border); }
.basemap { filter: saturate(0.35) contrast(0.95); }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .basemap { filter: invert(1) hue-rotate(180deg) saturate(0.3) brightness(0.85) contrast(0.9); }
}
:root[data-theme="dark"] .basemap { filter: invert(1) hue-rotate(180deg) saturate(0.3) brightness(0.85) contrast(0.9); }
.mobile-toggle { display: none; }
.leaflet-container { font: 13px/1.4 var(--font); background: var(--surface-2); }
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--surface); color: var(--ink); }
.leaflet-popup-content { margin: 10px 12px; }
.pop b { font-size: 14px; }
.pop .muted { font-size: 12px; }

/* Detail panel */
.detail {
  position: absolute; top: 0; right: 0; bottom: 0; width: min(560px, 100%); z-index: 1000; background: var(--surface);
  border-left: 1px solid var(--border); box-shadow: var(--shadow); overflow-y: auto; padding: 0 0 32px;
}
.detail .close { position: sticky; top: 8px; float: right; margin: 8px 8px 0 0; z-index: 2; background: var(--surface); box-shadow: var(--shadow); }
.gallery { display: flex; gap: 4px; overflow-x: auto; scroll-snap-type: x mandatory; height: 260px; background: var(--surface-2); }
.gallery img { height: 100%; width: auto; max-width: 92%; object-fit: cover; scroll-snap-align: start; flex: none; }
.gallery .empty { margin: auto; color: var(--muted); }
.detail .sec { padding: 14px 20px 0; }
.detail h2 { font-size: 18px; margin: 0 0 4px; }
.detail h3 { font-size: 13px; text-transform: none; color: var(--ink-2); margin: 0 0 8px; font-weight: 600; }
.detail .bigprice { font-size: 26px; font-weight: 650; }
.detail .sub { color: var(--ink-2); }
.factgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px 16px; font-size: 13px; }
.factgrid div span { display: block; color: var(--muted); font-size: 12px; }
.verdict { padding: 12px 14px; border-radius: var(--radius); background: var(--surface-2); }
.verdict .big { font-size: 18px; font-weight: 650; }
.verdict p { margin: 6px 0 0; color: var(--ink-2); font-size: 13px; }
.desc { white-space: pre-line; font-size: 13px; color: var(--ink-2); max-height: 9em; overflow: hidden; position: relative; }
.desc.open { max-height: none; }
.markbar { display: flex; gap: 6px; flex-wrap: wrap; }
.markbar .btn[aria-pressed="true"] { background: var(--accent-wash); border-color: transparent; color: var(--accent-ink); font-weight: 600; }
.markbar .btn.rej[aria-pressed="true"] { background: var(--surface-2); color: var(--bad-ink); }
textarea.note { width: 100%; min-height: 64px; margin-top: 8px; padding: 8px; border-radius: 8px; border: 1px solid var(--border); background: var(--page); }
.history-list { font-size: 13px; margin: 6px 0 0; padding-left: 18px; }

/* Tables */
.table-pane { overflow: auto; min-height: 0; border-left: 1px solid var(--border); }
table.data { width: 100%; border-collapse: collapse; font-size: 13px; }
table.data th {
  position: sticky; top: 0; background: var(--surface); text-align: left; font-weight: 600; color: var(--ink-2);
  padding: 8px 10px; border-bottom: 1px solid var(--axis); cursor: pointer; white-space: nowrap; z-index: 1;
}
table.data td { padding: 6px 10px; border-bottom: 1px solid var(--grid); }
table.data .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.data tbody tr:hover { background: var(--accent-wash); cursor: pointer; }
table.data th[aria-sort]::after { content: " ↓"; color: var(--muted); }
table.data th[aria-sort="ascending"]::after { content: " ↑"; }
.btn.more { margin: 12px auto; display: block; }
.sales-split { grid-template-columns: 1fr minmax(420px, 46%); }

/* Tiles / market */
.tiles { display: flex; gap: 12px; padding: 12px 16px; flex-wrap: wrap; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 14px; min-width: 150px; }
.tile .lbl { font-size: 12px; color: var(--ink-2); }
.tile .val { font-size: 22px; font-weight: 650; }
.tile .delta { font-size: 12px; color: var(--ink-2); }
.market { overflow-y: auto; padding: 4px 16px 32px; display: flex; flex-direction: column; gap: 16px; }
.market .tiles { padding: 12px 0 0; }
.tiles.wide .group { display: flex; flex-direction: column; gap: 8px; }
.tiles.wide .group h3 { margin: 0; font-size: 13px; display: flex; align-items: center; gap: 6px; }
.tiles.wide .group .row { display: flex; gap: 8px; flex-wrap: wrap; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; }
.card h2 { margin: 0; font-size: 15px; }
.card .sub { margin: 2px 0 8px; color: var(--ink-2); font-size: 13px; }
.card.note ul { margin: 8px 0 0; padding-left: 18px; color: var(--ink-2); font-size: 13px; }
.card.note li { margin-bottom: 4px; }
.table-scroll { overflow-x: auto; }
.table-view summary { cursor: pointer; color: var(--ink-2); font-size: 13px; margin-top: 8px; }
.chart-legend { display: flex; gap: 16px; font-size: 12px; color: var(--ink-2); margin-bottom: 8px; }
.chart-legend span { display: inline-flex; align-items: center; gap: 6px; }
.key { display: inline-block; width: 14px; height: 2px; border-radius: 1px; vertical-align: middle; }

/* Buying plan */
.plan { flex: 1; min-height: 0; display: grid; grid-template-columns: 340px 1fr; }
.plan-form { overflow-y: auto; padding: 12px 16px 32px; border-right: 1px solid var(--border); background: var(--surface); }
.plan-form h3 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); margin: 18px 0 8px; }
.plan-form h3:first-child { margin-top: 4px; }
.pf-row { display: grid; grid-template-columns: 1fr 118px; align-items: center; gap: 8px; margin-bottom: 6px; font-size: 13px; }
.pf-row label { color: var(--ink-2); line-height: 1.25; }
.pf-row label small { display: block; color: var(--muted); font-size: 11px; }
.pf-in { display: flex; align-items: center; border: 1px solid var(--border); border-radius: 8px; background: var(--page); }
.pf-in input { width: 100%; min-width: 0; border: 0; background: none; padding: 5px 8px; text-align: right; font-variant-numeric: tabular-nums; }
.pf-in input:focus { outline: none; }
.pf-in:focus-within { border-color: var(--accent); }
.pf-in span { padding-right: 8px; color: var(--muted); font-size: 11px; white-space: nowrap; }
.pf-row.check { grid-template-columns: auto 1fr; }
.pf-row .seg { justify-self: end; }
.pf-row.segrow { grid-template-columns: auto 1fr; }
.pf-row.segblock { display: flex; flex-direction: column; align-items: stretch; gap: 4px; }
.pf-row.segblock .seg { justify-self: stretch; display: flex; }
.pf-row.segblock .seg button { flex: 1; }
.plan-form .seg button { white-space: nowrap; padding: 4px 9px; font-size: 13px; }
.plan-body { overflow-y: auto; padding: 16px 20px 40px; display: flex; flex-direction: column; gap: 16px; }
.plan-head { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: center; justify-content: space-between; }
.plan-head h1 { font-size: 20px; margin: 0; }
.plan-head .sub { color: var(--ink-2); font-size: 13px; }
.plan-head select { padding: 5px 8px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); max-width: 320px; }
.callout { padding: 10px 14px; border-radius: var(--radius); background: var(--warn-bg); font-size: 13px; }
.callout + .callout { margin-top: -8px; }
.scen { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0 4px; }
.scen .btn { font-size: 12px; }
.scen .btn[aria-pressed="true"] { background: var(--accent-wash); border-color: transparent; color: var(--accent-ink); font-weight: 600; }
.card-sub { font-size: 14px; margin: 20px 0 6px; color: var(--ink); }
.card td small.muted { font-size: 11px; }
.verdict-line { font-size: 16px; font-weight: 600; margin: 4px 0 8px; }
table.data.compact td, table.data.compact th { padding: 5px 10px; }
table.data tr.total td { font-weight: 650; border-top: 1px solid var(--axis); }
table.data tr.sub td { color: var(--ink-2); font-size: 12px; }
table.data td.hl { background: var(--accent-wash); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.steps { margin: 8px 0 0; padding-left: 20px; font-size: 13px; }
.steps li { margin-bottom: 8px; }
.steps li b { color: var(--ink); }
.steps li span { color: var(--ink-2); }
.muted-note { color: var(--muted); font-size: 12px; margin-top: 8px; }
.good-ink { color: var(--good-ink); }
.bad-ink { color: var(--bad-ink); }

/* Charts */
.chart { position: relative; width: 100%; height: 300px; }
.chart.small { height: 180px; }
.chart svg { display: block; width: 100%; height: 100%; overflow: visible; }
.chart .grid line { stroke: var(--grid); stroke-width: 1; }
.chart .axis text, .chart .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .baseline { stroke: var(--axis); }
.chart .endlabel { fill: var(--ink-2); font-size: 12px; }
.chart .endlabel tspan.v { fill: var(--ink); font-weight: 600; }
.chart .cross { stroke: var(--axis); stroke-width: 1; }
.chart:focus-visible { outline: 2px solid var(--accent); outline-offset: 4px; border-radius: 4px; }
.tooltip {
  position: fixed; z-index: 5000; pointer-events: none; background: var(--surface); color: var(--ink);
  border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow); padding: 8px 10px; font-size: 12px; min-width: 140px;
}
.tooltip .t { color: var(--ink-2); margin-bottom: 4px; }
.tooltip .r { display: flex; align-items: center; gap: 8px; }
.tooltip .r b { font-size: 13px; font-variant-numeric: tabular-nums; }
.tooltip .r span { color: var(--ink-2); }

.empty-state { padding: 40px 16px; text-align: center; color: var(--muted); }

/* Mobile */
@media (max-width: 860px) {
  body { overflow: auto; }
  .topbar { flex-wrap: wrap; gap: 8px 16px; }
  .status { margin-left: 0; width: 100%; justify-content: space-between; }
  .split, .sales-split { display: flex; flex-direction: column; }
  .map-pane { height: 46vh; flex: none; order: -1; }
  .list-pane, .table-pane { border: 0; }
  .cards { overflow: visible; }
  .search { width: 100%; }
  .lcard { grid-template-columns: 96px 1fr; }
  .lcard .ph { width: 96px; height: 84px; }
  .detail { position: fixed; inset: 0; width: 100%; }
  .plan { display: flex; flex-direction: column; }
  .plan-form { border-right: 0; border-bottom: 1px solid var(--border); overflow: visible; }
  .plan-body { overflow: visible; padding: 16px; }
  .grid2 { grid-template-columns: 1fr; }
  .mobile-toggle { display: inline-block; }
  .filters:not(.open) > :not(.seg):not(.search):not(.mobile-toggle) { display: none; }
}
