/* ============================================================
   Sage OS — design system
   One stylesheet for every page. Tokens first, then the app shell
   (sidebar, mobile header, sign-in gate), then shared components.
   Pages should only add layout that is specific to them; colors,
   type, buttons, cards, chips, fields, tabs, tables and modals
   all come from here.

   Direction: modern SaaS. One sans family (Geist), neutral cool
   grays, Sage green reserved for primary actions, the active nav
   item and progress. Borders instead of shadows. Two radii.
   ============================================================ */

@import url('https://fonts.googleapis.com/css2?family=Geist:wght@400;500;600;700&family=Geist+Mono:wght@400;500&display=swap');

:root{
  /* surfaces */
  --canvas:#F5F6F4;
  --surface:#FFFFFF;
  --surface-sunken:#F0F2EF;
  --surface-hover:#F3F5F2;
  /* lines */
  --line:#E3E6E2;
  --line-subtle:#EEF0ED;
  --line-strong:#D3D8D2;
  /* text */
  --ink:#17201B;
  --ink-2:#59625C;
  --ink-3:#8B938E;
  /* brand (Sage logo green) */
  --brand:#062817;
  --brand-hover:#0E3A24;
  --brand-tint:#E9F0EB;
  --brand-tint-2:#D6E4DA;
  --brand-mid:#4F7D63;
  /* status */
  --ok:#157347;      --ok-tint:#E7F4EC;
  --warn:#B45309;    --warn-tint:#FEF3E2;  --warn-ink:#8A3D0C;
  --danger:#B42318;  --danger-tint:#FDECEA;
  --info:#2557A7;    --info-tint:#EAF1FB;
  /* shape */
  --r-sm:8px; --r-lg:12px; --r-pill:999px;
  --shadow-pop:0 12px 32px -8px rgba(23,32,27,.18), 0 2px 6px rgba(23,32,27,.06);
  /* type */
  --font:'Geist', ui-sans-serif, system-ui, -apple-system, 'Segoe UI', sans-serif;
  --font-mono:'Geist Mono', ui-monospace, SFMono-Regular, Menlo, monospace;
  --t-xs:12px; --t-sm:13px; --t-md:14px; --t-lg:15px; --t-xl:18px; --t-2xl:22px; --t-3xl:28px;
  --sidebar-w:240px;

  /* legacy names still used by page-level CSS — mapped onto the system */
  --bg:var(--canvas); --bg-2:var(--surface-sunken);
  --surface-2:var(--surface-sunken); --surface-3:var(--line-subtle);
  --glass:rgba(255,255,255,.9);
  --border:var(--line); --border-strong:var(--line-strong);
  --accent:var(--brand); --accent-ink:var(--brand);
  --accent-soft:var(--brand-tint); --accent-line:var(--brand-mid);
  --good:var(--ok); --warning:var(--warn); --serious:#C2410C; --critical:var(--danger);
  --radius-sm:var(--r-sm); --radius-md:var(--r-lg); --radius-lg:var(--r-lg); --radius-pill:var(--r-pill);
  --shadow:none; --shadow-lg:var(--shadow-pop);
  --grade-a:#14532D; --grade-b:#4F7D63; --grade-c:#9AB3A3;
  --type-land:#A16207; --type-industrial:#2557A7; --type-other:#8B938E;
  --type-retail:#B42318; --type-office:#0F766E; --type-hospitality:#7E22CE; --type-multifamily:#4D7C0F;
  color-scheme:light;
}

/* ---------- base ---------- */
*{ box-sizing:border-box; }
html,body{ height:100%; }
body{
  margin:0; background:var(--canvas); color:var(--ink);
  font-family:var(--font); font-size:var(--t-md); line-height:1.5;
  -webkit-font-smoothing:antialiased; text-rendering:optimizeLegibility;
  font-feature-settings:"ss01","cv11";
}
h1,h2,h3,h4{ font-family:var(--font); margin:0; color:var(--ink); text-wrap:balance; letter-spacing:-.011em; }
h1{ font-size:var(--t-2xl); font-weight:600; line-height:1.25; }
h2{ font-size:var(--t-lg); font-weight:600; line-height:1.35; }
h3{ font-size:var(--t-md); font-weight:600; line-height:1.4; }
p{ margin:0; }
a{ color:var(--brand); text-underline-offset:2px; }
button,input,select,textarea{ font:inherit; color:inherit; }
.num,.mono{ font-variant-numeric:tabular-nums; }
.mono{ font-family:var(--font-mono); font-size:.92em; }
.muted{ color:var(--ink-2); }
.subtle{ color:var(--ink-3); }
.sr-only{ position:absolute; width:1px; height:1px; overflow:hidden; clip:rect(0 0 0 0); white-space:nowrap; }
:focus-visible{ outline:2px solid var(--brand); outline-offset:2px; }
::selection{ background:var(--brand-tint-2); }
@media (prefers-reduced-motion:reduce){ *{ transition:none!important; animation:none!important; } }

/* ---------- sign-in gate ---------- */
#gate{
  position:fixed; inset:0; z-index:1000; background:var(--canvas);
  display:flex; align-items:center; justify-content:center; padding:24px;
}
#gate.hidden{ display:none; }
/* Signed-in brokers moving between pages: keep the gate hidden while the
   session is confirmed (set by the inline head script; auth.js removes it
   if there turns out to be no valid session). */
html.sage-signed-in #gate{ display:none; }
#gate .box{
  width:100%; max-width:380px; background:var(--surface); border:1px solid var(--line);
  border-radius:var(--r-lg); padding:32px; text-align:left;
}
#gate .box img{ display:block; width:200px; max-width:100%; height:auto; margin:0 0 28px; }
#gate h2{ font-size:var(--t-xl); margin-bottom:6px; }
#gate p{ font-size:var(--t-sm); color:var(--ink-2); margin-bottom:20px; }
#gate input{
  display:block; width:100%; font-size:var(--t-md); padding:0 12px; height:40px; margin-bottom:10px;
  border:1px solid var(--line-strong); border-radius:var(--r-sm); background:var(--surface);
}
#gate input:focus{ outline:none; border-color:var(--brand); box-shadow:0 0 0 3px var(--brand-tint); }
#gate button{
  width:100%; height:40px; border:0; border-radius:var(--r-sm); background:var(--brand); color:#fff;
  font-size:var(--t-md); font-weight:500; cursor:pointer; margin-top:4px;
}
#gate button:hover{ background:var(--brand-hover); }
#gate .err{ color:var(--danger); font-size:var(--t-sm); margin-top:10px; min-height:1.2em; }
#gate .authpanel{ display:none; }
#gate .authpanel.active{ display:block; }
#gate a.link{ display:inline-block; margin-top:16px; font-size:var(--t-sm); color:var(--brand); cursor:pointer; text-decoration:none; }
#gate a.link:hover{ text-decoration:underline; }

/* ---------- app shell ---------- */
.app-shell{ display:flex; align-items:stretch; min-height:100vh; background:var(--canvas); }
.sidebar{
  flex:0 0 var(--sidebar-w); width:var(--sidebar-w); position:sticky; top:0; height:100vh; overflow-y:auto;
  background:var(--surface); border-right:1px solid var(--line);
  padding:18px 12px 12px; display:flex; flex-direction:column; scrollbar-width:none; z-index:60;
}
.sidebar::-webkit-scrollbar{ display:none; }
.sb-brand{ display:block; padding:4px 10px 18px; text-decoration:none; }
.sb-brand img{ display:block; width:168px; max-width:100%; height:auto; }
.sb-group{ display:flex; flex-direction:column; gap:1px; margin-bottom:14px; }
.sb-group-label{ font-size:var(--t-xs); font-weight:500; color:var(--ink-3); padding:0 10px 4px; }
.sb-item{
  display:flex; align-items:center; gap:10px; height:34px; padding:0 10px; border-radius:var(--r-sm);
  color:var(--ink-2); text-decoration:none; font-size:var(--t-sm); font-weight:500; white-space:nowrap;
}
.sb-item svg{ width:16px; height:16px; flex:0 0 auto; stroke-width:1.75; }
.sb-item:hover{ background:var(--surface-hover); color:var(--ink); }
.sb-item.active{ background:var(--brand-tint); color:var(--brand); font-weight:600; }
.sb-item.soon{ color:var(--ink-3); cursor:default; }
.sb-item.soon:hover{ background:none; }
.sb-item .sb-tag{ margin-left:auto; font-size:11px; font-weight:500; color:var(--ink-3); border:1px solid var(--line); border-radius:var(--r-pill); padding:0 7px; line-height:18px; }
.sb-item .sb-count{ margin-left:auto; font-size:11px; font-weight:600; color:#fff; background:var(--warn); border-radius:var(--r-pill); padding:0 6px; line-height:18px; }
.sb-spacer{ flex:1; min-height:12px; }
.sb-sync{ font-size:var(--t-xs); color:var(--ink-3); padding:0 10px 8px; }
.sb-sync:empty{ display:none; }
.sb-sync.live::before{ content:""; display:inline-block; width:6px; height:6px; border-radius:50%; background:var(--ok); margin-right:6px; vertical-align:1px; }
.sb-sync.off::before{ content:""; display:inline-block; width:6px; height:6px; border-radius:50%; background:var(--warn); margin-right:6px; vertical-align:1px; }
.sb-profile{ display:flex; align-items:center; gap:10px; padding:10px; border-top:1px solid var(--line-subtle); margin:0 -2px; }
.avatar{
  width:30px; height:30px; border-radius:50%; background:var(--brand); color:#fff; flex:0 0 auto;
  display:flex; align-items:center; justify-content:center; font-size:11px; font-weight:600; letter-spacing:.02em;
}
.sb-profile .who{ min-width:0; flex:1; }
.sb-profile .name{ font-size:var(--t-sm); font-weight:600; color:var(--ink); white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
.sb-profile .org{ font-size:var(--t-xs); color:var(--ink-3); }
.sb-signout{
  width:30px; height:30px; border:0; background:none; color:var(--ink-3); border-radius:var(--r-sm); cursor:pointer;
  display:flex; align-items:center; justify-content:center; flex:0 0 auto;
}
.sb-signout svg{ width:16px; height:16px; }
.sb-signout:hover{ background:var(--surface-hover); color:var(--ink); }

/* mobile header + drawer (injected by shared/sidebar.js) */
.mobile-bar{ display:none; }
.sb-scrim{ display:none; }

/* main column */
.main{ flex:1; min-width:0; padding:28px 32px 64px; }
.page{ max-width:1200px; margin:0 auto; }
.page.wide{ max-width:none; }

/* page header: title + one line of context + actions */
.page-header{ display:flex; flex-wrap:wrap; align-items:flex-end; justify-content:space-between; gap:12px 24px; margin-bottom:24px; }
.page-header h1{ font-size:var(--t-2xl); }
.page-header .page-sub{ margin-top:4px; color:var(--ink-2); font-size:var(--t-md); max-width:70ch; }
.page-actions{ display:flex; flex-wrap:wrap; gap:8px; align-items:center; }

@media (max-width:900px){
  .app-shell{ flex-direction:column; }
  .mobile-bar{
    display:flex; align-items:center; justify-content:space-between; gap:12px; position:sticky; top:0; z-index:70;
    height:56px; padding:0 12px 0 16px; background:var(--surface); border-bottom:1px solid var(--line);
  }
  .mobile-bar img{ height:26px; width:auto; display:block; }
  .mobile-bar button{
    width:40px; height:40px; border:0; background:none; border-radius:var(--r-sm); color:var(--ink); cursor:pointer;
    display:flex; align-items:center; justify-content:center;
  }
  .mobile-bar button svg{ width:22px; height:22px; }
  .sidebar{
    position:fixed; left:0; top:0; bottom:0; height:100vh; width:min(84vw,300px); flex-basis:auto;
    transform:translateX(-100%); transition:transform .2s ease, visibility .2s; visibility:hidden; z-index:90;
  }
  body.nav-open .sidebar{ transform:none; visibility:visible; box-shadow:var(--shadow-pop); }
  body.nav-open .sb-scrim{ display:block; position:fixed; inset:0; background:rgba(23,32,27,.32); z-index:80; }
  .main{ padding:20px 16px 88px; }
  .page-header{ margin-bottom:18px; }
}

/* ---------- buttons ---------- */
.btn{
  display:inline-flex; align-items:center; justify-content:center; gap:6px; height:36px; padding:0 14px;
  border-radius:var(--r-sm); border:1px solid var(--line-strong); background:var(--surface); color:var(--ink);
  font-size:var(--t-sm); font-weight:500; line-height:1; text-decoration:none; white-space:nowrap; cursor:pointer;
  transition:background .12s, border-color .12s;
}
.btn:hover{ background:var(--surface-hover); }
.btn:disabled{ opacity:.55; cursor:not-allowed; }
.btn svg{ width:16px; height:16px; flex:0 0 auto; }
.btn.primary,.btn-primary{ background:var(--brand); border-color:var(--brand); color:#fff; }
.btn.primary:hover,.btn-primary:hover{ background:var(--brand-hover); border-color:var(--brand-hover); }
.btn.ghost{ background:transparent; border-color:transparent; color:var(--ink-2); }
.btn.ghost:hover{ background:var(--surface-hover); color:var(--ink); }
.btn.danger{ color:var(--danger); }
.btn.danger:hover{ background:var(--danger-tint); border-color:#F3C7C2; }
.btn.sm{ height:30px; padding:0 10px; font-size:var(--t-xs); }
.btn.lg{ height:44px; padding:0 18px; font-size:var(--t-md); }
.icon-btn{
  width:36px; height:36px; flex:0 0 auto; display:inline-flex; align-items:center; justify-content:center;
  border-radius:var(--r-sm); border:1px solid var(--line-strong); background:var(--surface); color:var(--ink-2);
  cursor:pointer; text-decoration:none;
}
.icon-btn:hover{ background:var(--surface-hover); color:var(--ink); }
.icon-btn.brand{ border-color:transparent; background:var(--brand-tint); color:var(--brand); }
.icon-btn.brand:hover{ background:var(--brand-tint-2); }
.icon-btn svg{ width:16px; height:16px; }

/* ---------- cards ---------- */
.card{ background:var(--surface); border:1px solid var(--line); border-radius:var(--r-lg); }
.card-header{ display:flex; align-items:center; justify-content:space-between; gap:12px; padding:16px 20px; }
.card-header h2{ font-size:var(--t-lg); }
.card-header .meta{ font-size:var(--t-sm); color:var(--ink-3); }
.card-header a{ font-size:var(--t-sm); font-weight:500; text-decoration:none; }
.card-header a:hover{ text-decoration:underline; }
.card-body{ padding:0 20px 20px; }
.card-pad{ padding:20px; }
.card + .card{ margin-top:0; }

/* ---------- stats ---------- */
.stat-row{ display:grid; grid-template-columns:repeat(auto-fit,minmax(170px,1fr)); background:var(--surface); border:1px solid var(--line); border-radius:var(--r-lg); }
.stat{ padding:16px 20px; border-left:1px solid var(--line-subtle); min-width:0; }
.stat:first-child{ border-left:0; }
.stat .stat-label{ font-size:var(--t-sm); color:var(--ink-2); }
.stat .stat-value{ font-size:var(--t-2xl); font-weight:600; letter-spacing:-.02em; font-variant-numeric:tabular-nums; margin-top:2px; line-height:1.2; }
.stat .stat-note{ font-size:var(--t-xs); color:var(--ink-3); margin-top:2px; }
@media (max-width:620px){
  .stat-row{ grid-template-columns:repeat(2,minmax(0,1fr)); }
  .stat{ border-left:0; border-top:1px solid var(--line-subtle); padding:14px 16px; }
  .stat:nth-child(-n+2){ border-top:0; }
  .stat:nth-child(even){ border-left:1px solid var(--line-subtle); }
  .stat .stat-value{ font-size:var(--t-xl); }
}

/* ---------- chips / badges ---------- */
.chip,.badge{
  display:inline-flex; align-items:center; gap:5px; height:22px; padding:0 8px; border-radius:var(--r-pill);
  font-size:var(--t-xs); font-weight:500; white-space:nowrap; background:var(--surface-sunken); color:var(--ink-2);
}
.chip.brand{ background:var(--brand-tint); color:var(--brand); }
.chip.ok{ background:var(--ok-tint); color:var(--ok); }
.chip.warn{ background:var(--warn-tint); color:var(--warn-ink); }
.chip.danger{ background:var(--danger-tint); color:var(--danger); }
.chip.info{ background:var(--info-tint); color:var(--info); }
.chip.dot::before{ content:""; width:6px; height:6px; border-radius:50%; background:currentColor; }

/* ---------- form fields ---------- */
.field{ display:flex; flex-direction:column; gap:6px; min-width:0; }
.field > label,.field-label{ font-size:var(--t-sm); font-weight:500; color:var(--ink-2); }
.input,.select,.textarea,
.field input:not([type=checkbox]):not([type=radio]),.field select,.field textarea{
  width:100%; height:36px; padding:0 12px; border:1px solid var(--line-strong); border-radius:var(--r-sm);
  background:var(--surface); color:var(--ink); font-size:var(--t-md);
}
.textarea,.field textarea{ height:auto; min-height:88px; padding:10px 12px; resize:vertical; line-height:1.5; }
.input:focus,.select:focus,.textarea:focus,
.field input:focus,.field select:focus,.field textarea:focus{ outline:none; border-color:var(--brand); box-shadow:0 0 0 3px var(--brand-tint); }
.search{
  position:relative; display:flex; align-items:center; min-width:0;
}
.search svg{ position:absolute; left:11px; width:16px; height:16px; color:var(--ink-3); pointer-events:none; }
.search input{ width:100%; height:36px; padding:0 12px 0 34px; border:1px solid var(--line-strong); border-radius:var(--r-sm); background:var(--surface); font-size:var(--t-md); }
.search input:focus{ outline:none; border-color:var(--brand); box-shadow:0 0 0 3px var(--brand-tint); }
input[type=checkbox],input[type=radio]{ accent-color:var(--brand); }

/* ---------- segmented control / tabs ---------- */
.segmented{ display:inline-flex; padding:3px; background:var(--surface-sunken); border-radius:var(--r-sm); gap:2px; }
.segmented button,.segmented a{
  height:30px; padding:0 12px; border:0; border-radius:6px; background:none; color:var(--ink-2);
  font-size:var(--t-sm); font-weight:500; cursor:pointer; text-decoration:none; display:inline-flex; align-items:center;
}
.segmented .active,.segmented [aria-selected=true]{ background:var(--surface); color:var(--ink); box-shadow:0 1px 2px rgba(23,32,27,.08); }
.tabs{ display:flex; gap:20px; border-bottom:1px solid var(--line); overflow-x:auto; scrollbar-width:none; }
.tabs::-webkit-scrollbar{ display:none; }
.tab{
  height:40px; padding:0; border:0; border-bottom:2px solid transparent; margin-bottom:-1px; background:none;
  color:var(--ink-2); font-size:var(--t-md); font-weight:500; cursor:pointer; white-space:nowrap;
}
.tab:hover{ color:var(--ink); }
.tab.active{ color:var(--ink); border-bottom-color:var(--brand); }

/* ---------- lists & tables ---------- */
.list-row{ display:flex; align-items:center; gap:12px; padding:12px 20px; border-top:1px solid var(--line-subtle); min-width:0; }
.list-row .grow{ flex:1; min-width:0; }
.list-row .title{ font-size:var(--t-md); font-weight:500; color:var(--ink); white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
.list-row .sub{ font-size:var(--t-sm); color:var(--ink-2); white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
.table-wrap{ overflow-x:auto; }
.table{ width:100%; border-collapse:collapse; font-size:var(--t-sm); }
.table th{ text-align:left; font-weight:500; color:var(--ink-3); padding:10px 12px; border-bottom:1px solid var(--line); white-space:nowrap; }
.table td{ padding:12px; border-bottom:1px solid var(--line-subtle); vertical-align:middle; }
.table tr:hover td{ background:var(--surface-hover); }

/* ---------- progress ---------- */
.progress{ height:6px; border-radius:var(--r-pill); background:var(--line-subtle); overflow:hidden; }
.progress > span,.progress > i{ display:block; height:100%; background:var(--brand); border-radius:inherit; }

/* ---------- empty states ---------- */
.empty{ padding:20px; color:var(--ink-2); font-size:var(--t-sm); line-height:1.55; }
.empty strong{ color:var(--ink); font-weight:600; }

/* ---------- modal ---------- */
.modal{ position:fixed; inset:0; z-index:200; background:rgba(23,32,27,.36); display:none; align-items:center; justify-content:center; padding:16px; }
.modal.open{ display:flex; }
.sheet{
  width:100%; max-width:480px; max-height:calc(100vh - 32px); overflow:auto; background:var(--surface);
  border-radius:var(--r-lg); box-shadow:var(--shadow-pop); padding:24px; display:flex; flex-direction:column; gap:16px;
}
.sheet h2{ font-size:var(--t-xl); }
.sheet-actions{ display:flex; justify-content:flex-end; gap:8px; padding-top:4px; }
.form-error{ color:var(--danger); font-size:var(--t-sm); min-height:1em; }

/* ---------- toast ---------- */
.toast{
  position:fixed; left:50%; bottom:24px; z-index:300; transform:translate(-50%,12px); opacity:0; pointer-events:none;
  background:var(--ink); color:#fff; padding:10px 16px; border-radius:var(--r-sm); font-size:var(--t-sm);
  transition:opacity .18s, transform .18s;
}
.toast.show{ opacity:1; transform:translate(-50%,0); }

/* The hidden attribute always wins over component display rules. */
[hidden]{ display:none !important; }
