:root {
  --bg: #f4f6f5;
  --surface: #ffffff;
  --surface-2: #eef2f1;
  --text: #15201d;
  --muted: #5d6b67;
  --line: #dde4e2;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --accent-soft: #d7efe9;
  --good: #13795b;
  --good-soft: #dcf3e8;
  --warn: #9a5b00;
  --warn-soft: #fbecd2;
  --bad: #b42318;
  --bad-soft: #fde4e1;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 24, 22, .06), 0 4px 14px rgba(16, 24, 22, .06);
  --tabbar-h: 62px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1513;
    --surface: #161f1c;
    --surface-2: #1d2925;
    --text: #e7efec;
    --muted: #98a8a3;
    --line: #2a3833;
    --accent: #2bb3a3;
    --accent-ink: #062320;
    --accent-soft: #143a35;
    --good: #43c59a;
    --good-soft: #13382b;
    --warn: #f0b354;
    --warn-soft: #3b2c12;
    --bad: #ff8a7a;
    --bad-soft: #3f1b17;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 12px);
}
a { color: var(--accent); text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
img { max-width: 100%; }
h1, h2, h3 { line-height: 1.2; margin: 0; }
h1 { font-size: 22px; }
h2 { font-size: 18px; }
h3 { font-size: 15px; }
small, .muted { color: var(--muted); }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(10px);
  -webkit-backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; color: var(--text); font-weight: 700; font-size: 17px; }
.brand img { border-radius: 7px; }
.status {
  display: inline-flex; align-items: center; gap: 6px; max-width: 60%;
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: 4px 10px; font-size: 12.5px; color: var(--muted); cursor: pointer;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.ok { background: var(--good); }
.dot.busy { background: var(--warn); animation: pulse 1.2s infinite; }
.dot.err { background: var(--bad); }
@keyframes pulse { 50% { opacity: .35; } }

main { max-width: 760px; margin: 0 auto; padding: 14px 16px 0; }

/* Tab bar */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(5, minmax(0, 1fr));
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
}
.tabbar a {
  position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); font-size: 11.5px; font-weight: 600;
}
.tabbar svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tabbar a.active { color: var(--accent); }
.tab-badge {
  position: absolute; top: 6px; left: calc(50% + 6px);
  min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px;
  background: var(--bad); color: #fff; font-size: 11px; line-height: 18px; text-align: center;
}

/* Generic building blocks */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-bottom: 14px; box-shadow: var(--shadow); }
.panel > h2, .panel > h3 { margin-bottom: 10px; }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.spread { justify-content: space-between; }
.stack > * + * { margin-top: 10px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 8px 14px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); color: var(--text); font-weight: 600; cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--bad); }
.btn.small { min-height: 32px; padding: 4px 10px; font-size: 13px; border-radius: 8px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .55; cursor: default; }
.icon-btn { background: none; border: 0; padding: 6px; cursor: pointer; color: var(--muted); border-radius: 8px; }
.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn.on { color: var(--warn); }
.icon-btn.on svg { fill: currentColor; }

label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; font-weight: 600; }
input[type=text], input[type=number], input[type=email], input[type=tel], input[type=password], input[type=url], select, textarea {
  width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface-2);
}
textarea { min-height: 160px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 55%, transparent); outline-offset: 1px; }
input[type=range] { width: 100%; accent-color: var(--accent); }
.hint { font-size: 12.5px; color: var(--muted); margin-top: 4px; }

.switch { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; border-top: 1px solid var(--line); }
.switch:first-of-type { border-top: 0; }
.switch input { width: 46px; height: 28px; appearance: none; -webkit-appearance: none; background: var(--line); border-radius: 14px; position: relative; cursor: pointer; flex: none; }
.switch input::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; transition: transform .15s; box-shadow: 0 1px 2px rgba(0,0,0,.25); }
.switch input:checked { background: var(--accent); }
.switch input:checked::after { transform: translateX(18px); }

.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 12px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface);
  font-size: 13.5px; font-weight: 600; color: var(--muted); cursor: pointer; user-select: none;
}
.chip.active { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.chip small { font-weight: 500; opacity: .75; color: inherit; }
.chip .count { font-weight: 700; opacity: .85; }
.chip-grid { display: flex; flex-wrap: wrap; gap: 8px; }

/* House cards */
.toolbar { display: grid; gap: 10px; margin-bottom: 12px; }
.cards { display: grid; gap: 14px; }
@media (min-width: 640px) { .cards { grid-template-columns: 1fr 1fr; } }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); cursor: pointer; }
.card.dim { opacity: .62; }
.photo { position: relative; aspect-ratio: 16 / 10; background: var(--surface-2) center / cover no-repeat; }
.photo.placeholder { display: grid; place-items: center; color: var(--muted); font-size: 13px; }
.score {
  position: absolute; top: 10px; left: 10px; min-width: 44px; padding: 4px 8px; border-radius: 10px;
  font-weight: 800; font-size: 15px; text-align: center; background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
.score small { display: block; font-size: 9.5px; font-weight: 700; letter-spacing: .03em; color: var(--muted); text-transform: uppercase; }
.score.high { background: var(--good); color: #fff; }
.score.high small { color: rgba(255,255,255,.85); }
.score.mid { background: var(--warn-soft); color: var(--warn); }
.badges { position: absolute; top: 10px; right: 10px; display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.badge { padding: 3px 8px; border-radius: 8px; font-size: 11.5px; font-weight: 700; background: var(--surface); color: var(--text); }
.badge.new { background: var(--bad); color: #fff; }
.badge.offer { background: var(--warn-soft); color: var(--warn); }
.badge.sold { background: var(--bad-soft); color: var(--bad); }
.card .body { padding: 12px 14px 10px; }
.price { font-size: 19px; font-weight: 800; }
.price small { font-size: 12px; font-weight: 600; }
.addr { font-weight: 600; margin: 1px 0 8px; }
.facts { display: flex; flex-wrap: wrap; gap: 6px; }
.fact { font-size: 12.5px; padding: 3px 8px; border-radius: 7px; background: var(--surface-2); color: var(--text); }
.fact.good { background: var(--good-soft); color: var(--good); font-weight: 700; }
.fact.warn { background: var(--warn-soft); color: var(--warn); }
.fact.bad { background: var(--bad-soft); color: var(--bad); }
.card .foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 4px 8px 8px 14px; font-size: 12.5px; color: var(--muted); }
.reasons { font-size: 12.5px; color: var(--bad); margin-top: 8px; }

.empty { text-align: center; padding: 40px 16px; color: var(--muted); }
.empty h2 { color: var(--text); margin-bottom: 6px; }

/* Detail page */
.back { display: inline-flex; align-items: center; gap: 4px; font-weight: 600; margin-bottom: 10px; }
.hero { border-radius: var(--radius); overflow: hidden; margin-bottom: 12px; }
.hero .photo { aspect-ratio: 16 / 9; }
.kv { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.kv div { background: var(--surface-2); border-radius: 10px; padding: 8px 10px; }
.kv dt { font-size: 12px; color: var(--muted); }
.kv dd { margin: 0; font-weight: 700; }
.bars { display: grid; gap: 6px; }
.bar { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: center; font-size: 14px; }
.bar b { font-variant-numeric: tabular-nums; }
.bar b.neg { color: var(--bad); }
.list-plain { margin: 0; padding-left: 18px; }
.list-plain li { margin: 2px 0; }
.pairs { display: grid; grid-template-columns: minmax(110px, 40%) 1fr; gap: 4px 12px; font-size: 13.5px; }
.pairs dt { color: var(--muted); }
.pairs dd { margin: 0; }
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 12px 0 14px; }
.actions .btn { width: 100%; }

/* Brokers */
.broker { display: grid; grid-template-columns: auto 1fr auto; gap: 10px; align-items: center; padding: 10px 0; border-top: 1px solid var(--line); }
.broker:first-child { border-top: 0; }
.broker .name { font-weight: 600; }
.broker .meta { font-size: 12.5px; color: var(--muted); }
.broker .meta.err { color: var(--bad); }
.group-title { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 16px 0 4px; font-weight: 700; }

/* Sheet (bottom drawer) */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 40; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 41; max-height: 88vh; overflow: auto;
  background: var(--surface); border-radius: 18px 18px 0 0; padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
  box-shadow: 0 -8px 30px rgba(0,0,0,.2); max-width: 760px; margin: 0 auto;
}
.sheet .grabber { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: -4px auto 12px; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 50;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 14px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-weight: 600; font-size: 14px;
  max-width: calc(100% - 32px); box-shadow: var(--shadow);
}

/* Login */
.login { max-width: 380px; margin: 12vh auto 0; text-align: center; }
.login img { width: 72px; height: 72px; border-radius: 18px; margin-bottom: 12px; }
.login form { margin-top: 18px; text-align: left; }
body.logged-out .tabbar, body.logged-out .status { display: none; }

.log { font-size: 12.5px; max-height: 280px; overflow: auto; }
.log div { padding: 4px 0; border-top: 1px solid var(--line); }
.log time { color: var(--muted); margin-right: 6px; font-variant-numeric: tabular-nums; }
.log .warning, .log .error { color: var(--bad); }
.notice { background: var(--accent-soft); border-radius: 12px; padding: 10px 12px; font-size: 13.5px; margin-bottom: 12px; }

[hidden] { display: none !important; }

/* Viewings calendar */
.cal { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 4px; margin-top: 10px; }
.cal-head { text-align: center; font-size: 12px; color: var(--muted); padding: 2px 0; }
.cal-day { position: relative; aspect-ratio: 1; min-width: 0; padding: 0; border: 0; border-radius: 8px; background: transparent; color: var(--text); font: inherit; cursor: pointer; }
.cal-day.empty { cursor: default; }
.cal-day.today { outline: 1px solid var(--line); }
.cal-day.has { background: var(--accent); color: #fff; font-weight: 700; }
.cal-day.picked { box-shadow: 0 0 0 2px var(--text); }
.cal-day i { position: absolute; top: 2px; right: 4px; font-size: 10px; font-style: normal; }
.appt { display: flex; flex-direction: column; align-items: flex-start; gap: 2px; width: 100%; text-align: left; padding: 10px 0; border: 0; border-top: 1px solid var(--line); background: none; color: var(--text); font: inherit; cursor: pointer; }
.appt:first-of-type { border-top: 0; }
.appt-when { font-weight: 700; }
.appt.past { opacity: .55; }
