:root {
  --bg: #f4f4f1;
  --panel: #ffffff;
  --ink: #1d1f21;
  --muted: #6b6f74;
  --line: #e2e2dc;
  --asphalt: #23262a;
  --asphalt-2: #33373c;
  --accent: #f5b700;
  --accent-ink: #1d1f21;
  --ok: #2e8b57;
  --watch: #d98e04;
  --bad: #c8372d;
  --info: #2f6fd6;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 2px 8px rgba(0,0,0,.05);
  --crew-0: #2f6fd6; --crew-1: #8e44ad; --crew-2: #16a085; --crew-3: #d35400; --crew-4: #7f8c8d;
  color-scheme: light;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--ink);
}
a { color: var(--info); }
h1, h2, h3 { margin: 0 0 .5rem; line-height: 1.2; }
h2 { font-size: 1.35rem; }
h3 { font-size: 1.05rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.right { text-align: right; }
.row { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.hidden { display: none !important; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 1000;
  background: var(--asphalt); color: #fff;
  display: flex; align-items: center; gap: 1rem; padding: 0 1rem; height: 56px;
  border-bottom: 4px solid var(--accent);
}
.brand { display: flex; align-items: center; gap: .55rem; font-weight: 700; white-space: nowrap; }
.brand .logo {
  width: 30px; height: 30px; border-radius: 6px; background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center; font-weight: 900;
  background-image: repeating-linear-gradient(90deg, transparent 0 10px, rgba(0,0,0,.18) 10px 14px);
}
.nav { display: flex; gap: .25rem; overflow-x: auto; }
.nav a {
  color: #d9dadc; text-decoration: none; padding: .45rem .75rem; border-radius: 8px; white-space: nowrap; font-weight: 500;
}
.nav a:hover { background: var(--asphalt-2); color: #fff; }
.nav a.active { background: var(--accent); color: var(--accent-ink); }
.demo-pill { font-size: .72rem; background: #3c4046; color: #c9cbce; padding: .15rem .5rem; border-radius: 99px; }

main { padding: 1.25rem; max-width: 1400px; margin: 0 auto; }

/* ---------- Panels / cards ---------- */
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1rem; }
.grid { display: grid; gap: 1rem; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 1rem; margin-bottom: 1rem; }
.kpi .label { color: var(--muted); font-size: .82rem; text-transform: uppercase; letter-spacing: .04em; }
.kpi .value { font-size: 1.7rem; font-weight: 700; margin-top: .2rem; }
.kpi .sub { color: var(--muted); font-size: .85rem; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
@media (max-width: 900px) { .two-col { grid-template-columns: 1fr; } }

/* ---------- Forms ---------- */
label { display: block; font-size: .82rem; font-weight: 600; color: var(--muted); margin-bottom: .2rem; }
input, select, textarea {
  width: 100%; font: inherit; padding: .55rem .65rem; border: 1px solid #cfcfc8; border-radius: 8px; background: #fff; color: var(--ink);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: var(--accent); }
textarea { min-height: 70px; resize: vertical; }
.field { margin-bottom: .7rem; }
.fields-2 { display: grid; grid-template-columns: 1fr 1fr; gap: .6rem; }
.fields-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: .6rem; }
.check { display: flex; align-items: center; gap: .45rem; font-weight: 500; color: var(--ink); font-size: .92rem; }
.check input { width: auto; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  font: inherit; font-weight: 600; padding: .55rem .95rem; border-radius: 8px; cursor: pointer;
  border: 1px solid #cfcfc8; background: #fff; color: var(--ink); text-decoration: none; white-space: nowrap;
}
.btn:hover { background: #f2f2ee; }
.btn.primary { background: var(--accent); border-color: #d9a200; color: var(--accent-ink); }
.btn.primary:hover { background: #ffc61a; }
.btn.dark { background: var(--asphalt); border-color: var(--asphalt); color: #fff; }
.btn.dark:hover { background: var(--asphalt-2); }
.btn.danger { color: var(--bad); }
.btn.sm { padding: .3rem .6rem; font-size: .85rem; }
.btn.on { background: var(--asphalt); color: #fff; border-color: var(--asphalt); }
.btn:disabled { opacity: .5; cursor: default; }

/* ---------- Tables ---------- */
table { width: 100%; border-collapse: collapse; }
th, td { padding: .55rem .5rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
th { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
tr.click { cursor: pointer; }
tr.click:hover td { background: #fafaf6; }
.items input { padding: .35rem .45rem; }
.items td.amt { white-space: nowrap; text-align: right; font-variant-numeric: tabular-nums; }
.totals { margin-left: auto; width: min(320px, 100%); }
.totals div { display: flex; justify-content: space-between; padding: .25rem 0; }
.totals .grand { font-size: 1.25rem; font-weight: 800; border-top: 2px solid var(--ink); margin-top: .3rem; padding-top: .45rem; }

/* ---------- Badges ---------- */
.badge { display: inline-block; font-size: .75rem; font-weight: 700; padding: .15rem .55rem; border-radius: 99px; background: #ececea; color: #444; text-transform: capitalize; }
.badge.draft { background: #ececea; color: #555; }
.badge.sent { background: #e3ecfb; color: #2457b0; }
.badge.viewed { background: #efe6fa; color: #6d34a7; }
.badge.accepted { background: #ddf3e6; color: #1f7046; }
.badge.scheduled { background: #fff2c9; color: #8a6400; }
.badge.declined { background: #fbe2df; color: #a12b22; }
.risk { display: inline-flex; gap: .25rem; align-items: center; font-size: .75rem; font-weight: 700; padding: .1rem .45rem; border-radius: 6px; }
.risk.ok { background: #ddf3e6; color: #1f7046; }
.risk.watch { background: #fff0cf; color: #8a5a00; }
.risk.bad { background: #fbe2df; color: #a12b22; }
.risk.unknown { background: #ececea; color: #666; }
.chips { display: flex; gap: .35rem; flex-wrap: wrap; }
.chip { border: 1px solid #cfcfc8; background: #fff; border-radius: 99px; padding: .25rem .7rem; cursor: pointer; font: inherit; font-size: .85rem; }
.chip.on { background: var(--asphalt); color: #fff; border-color: var(--asphalt); }

/* ---------- Quote builder ---------- */
.builder { display: grid; grid-template-columns: 340px 1fr; gap: 1rem; align-items: start; }
.builder .map-wrap { position: relative; }
#map { height: 520px; border-radius: var(--radius); border: 1px solid var(--line); }
.map-tools {
  position: absolute; z-index: 500; top: 10px; left: 50%; transform: translateX(-50%);
  background: rgba(35,38,42,.92); padding: .4rem; border-radius: 10px; display: flex; gap: .35rem; flex-wrap: wrap; justify-content: center;
}
.map-tools .btn { background: #3a3e44; color: #fff; border-color: #4a4f56; }
.map-tools .btn.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.map-hint { position: absolute; z-index: 500; bottom: 10px; left: 10px; right: 10px; text-align: center; pointer-events: none; }
.map-hint span { background: rgba(35,38,42,.88); color: #fff; padding: .35rem .7rem; border-radius: 8px; font-size: .88rem; }
.measure-sum { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: .6rem; margin: .8rem 0; }
.measure-sum .m { background: #f7f7f3; border: 1px solid var(--line); border-radius: 8px; padding: .5rem .7rem; }
.measure-sum .m b { display: block; font-size: 1.2rem; }
.shape-list { list-style: none; padding: 0; margin: 0; }
.shape-list li { display: flex; align-items: center; gap: .5rem; padding: .3rem 0; border-bottom: 1px dashed var(--line); font-size: .9rem; }
.geo-results { border: 1px solid var(--line); border-radius: 8px; margin-top: .3rem; max-height: 180px; overflow: auto; background: #fff; }
.geo-results div { padding: .45rem .6rem; cursor: pointer; font-size: .88rem; border-bottom: 1px solid var(--line); }
.geo-results div:hover { background: #fff7d6; }
.shape-label { background: rgba(35,38,42,.85); color: #fff; border: 0; border-radius: 6px; font-weight: 700; box-shadow: none; }
.shape-label::before { display: none; }
@media (max-width: 1000px) { .builder { grid-template-columns: 1fr; } #map { height: 420px; } }

/* ---------- Schedule ---------- */
.sched { display: grid; grid-template-columns: 260px 1fr; gap: 1rem; align-items: start; }
@media (max-width: 1000px) { .sched { grid-template-columns: 1fr; } }
.cal { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); border-top: 1px solid var(--line); border-left: 1px solid var(--line); background: #fff; border-radius: var(--radius); overflow: hidden; }
.cal .dow { background: var(--asphalt); color: #fff; font-size: .78rem; text-transform: uppercase; padding: .4rem; text-align: center; letter-spacing: .05em; }
.cal .day { min-height: 118px; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); padding: .3rem; display: flex; flex-direction: column; gap: .25rem; transition: background .1s; }
.cal .day.other { background: #fafaf7; color: #aaa; }
.cal .day.today { box-shadow: inset 0 0 0 2px var(--accent); }
.cal .day.drop { background: #fff7d6; }
.cal .day.wx-bad { background-image: linear-gradient(180deg, rgba(200,55,45,.07), transparent 60%); }
.cal .day.wx-watch { background-image: linear-gradient(180deg, rgba(217,142,4,.08), transparent 60%); }
.day-head { display: flex; justify-content: space-between; align-items: center; font-size: .8rem; }
.day-head .d { font-weight: 700; }
.wx { font-size: .72rem; color: var(--muted); white-space: nowrap; }
.wx .rain { color: var(--info); font-weight: 600; }
.job-chip {
  font-size: .78rem; line-height: 1.25; padding: .25rem .4rem; border-radius: 6px; color: #fff; cursor: grab;
  border-left: 4px solid rgba(0,0,0,.25); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; position: relative;
}
.job-chip.cont { opacity: .75; font-style: italic; }
.job-chip.done { opacity: .5; text-decoration: line-through; }
.job-chip .flag { position: absolute; right: 3px; top: 2px; font-size: .7rem; }
.job-chip.conflict { outline: 2px dashed var(--bad); outline-offset: 1px; }
.job-chip.wbad { box-shadow: inset -4px 0 0 var(--bad); }
.job-chip.wwatch { box-shadow: inset -4px 0 0 var(--watch); }
.ready-card { border: 1px solid var(--line); border-left: 4px solid var(--accent); border-radius: 8px; padding: .5rem .6rem; margin-bottom: .5rem; background: #fff; cursor: grab; font-size: .88rem; }
.ready-card b { display: block; }
.legend { display: flex; gap: .7rem; flex-wrap: wrap; font-size: .8rem; color: var(--muted); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: .3rem; vertical-align: -1px; }
.alert-item { border-left: 4px solid var(--watch); padding: .45rem .6rem; background: #fffaf0; border-radius: 6px; margin-bottom: .5rem; font-size: .88rem; }
.alert-item.bad { border-color: var(--bad); background: #fff4f3; }

/* ---------- Forecast strip ---------- */
.forecast { display: grid; grid-template-columns: repeat(7, 1fr); gap: .5rem; }
.forecast .f { text-align: center; background: #f7f7f3; border-radius: 8px; padding: .5rem .2rem; border: 1px solid var(--line); }
.forecast .f .i { font-size: 1.4rem; }
.forecast .f.bad { border-color: #f0b9b3; background: #fff4f3; }
.forecast .f.watch { border-color: #f3d79b; background: #fffaf0; }

/* ---------- Modal / toast ---------- */
.modal-bg { position: fixed; inset: 0; background: rgba(20,22,24,.55); z-index: 2000; display: grid; place-items: center; padding: 1rem; }
.modal { background: #fff; border-radius: 12px; width: min(520px, 100%); max-height: 90vh; overflow: auto; padding: 1.2rem; box-shadow: 0 10px 40px rgba(0,0,0,.3); }
.modal h3 { margin-bottom: .8rem; }
.modal .actions { display: flex; gap: .5rem; justify-content: flex-end; margin-top: 1rem; flex-wrap: wrap; }
.toast { position: fixed; bottom: 1rem; left: 50%; transform: translateX(-50%); background: var(--asphalt); color: #fff; padding: .6rem 1rem; border-radius: 8px; z-index: 3000; box-shadow: var(--shadow); }
.link-box { display: flex; gap: .4rem; }
.link-box input { font-family: ui-monospace, monospace; font-size: .85rem; }
.empty { text-align: center; color: var(--muted); padding: 2rem 1rem; }
.list-item { display: flex; gap: .6rem; align-items: center; padding: .55rem 0; border-bottom: 1px solid var(--line); }
.list-item:last-child { border-bottom: 0; }
.list-item .grow { flex: 1; min-width: 0; }
.list-item .grow b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

@media (max-width: 640px) {
  main { padding: .8rem; }
  .fields-3 { grid-template-columns: 1fr 1fr; }
  .brand .name { display: none; }
  .cal .day { min-height: 84px; }
  .wx { display: none; }
}

/* ================= Phone / tablet ================= */
.builder > .grid, .sched > *, .two-col > * { min-width: 0; }
input, select, textarea { font-size: 16px; } /* stops iPhone auto-zoom when tapping a field */
.tabbar { display: none; }
.agenda { display: grid; gap: .5rem; }
.ag-day { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: .55rem .65rem; display: grid; gap: .4rem; }
.ag-day.today { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.ag-day.empty-day { padding: .35rem .65rem; background: transparent; border-style: dashed; }
.ag-day.empty-day .ag-head b { font-weight: 500; color: var(--muted); }
.ag-head { display: flex; align-items: center; gap: .5rem; }
.wx-m { font-size: .8rem; padding: .1rem .45rem; border-radius: 6px; background: #f2f2ee; white-space: nowrap; }
.wx-m.bad { background: #fbe2df; color: #a12b22; }
.wx-m.watch { background: #fff0cf; color: #8a5a00; }
.agenda .job-chip { white-space: normal; font-size: .95rem; padding: .55rem .7rem; padding-right: 1.8rem; cursor: pointer; }
.chip-sub { font-size: .78rem; opacity: .9; margin-top: .15rem; }
.agenda .job-chip .flag { font-size: 1rem; top: .5rem; right: .5rem; }
.ready-card { cursor: pointer; }

@media (max-width: 760px) {
  .topbar { height: 50px; padding: 0 .8rem; padding-top: env(safe-area-inset-top); height: calc(50px + env(safe-area-inset-top)); }
  .topbar .nav { display: none; }
  .brand .name { display: inline; }
  main { padding: .8rem .8rem calc(84px + env(safe-area-inset-bottom)); }
  .tabbar {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 1500;
    background: var(--asphalt); border-top: 3px solid var(--accent);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .tabbar a {
    flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
    color: #c9cbce; text-decoration: none; font-size: .7rem; font-weight: 600; padding: .45rem 0 .5rem;
  }
  .tabbar a span { font-size: 1.25rem; line-height: 1; filter: grayscale(.4); }
  .tabbar a.active { color: var(--accent); }
  .tabbar a.active span { filter: none; }
  .toast { bottom: calc(90px + env(safe-area-inset-bottom)); width: max-content; max-width: calc(100% - 2rem); text-align: center; }

  h2 { font-size: 1.2rem; }
  .btn { padding: .65rem .9rem; } /* bigger tap targets */
  .btn.sm { padding: .45rem .65rem; }
  .kpis { grid-template-columns: 1fr 1fr; gap: .6rem; }
  .kpi .value { font-size: 1.3rem; }
  .panel { padding: .85rem; }
  .forecast { grid-template-columns: none; grid-auto-flow: column; grid-auto-columns: 78px; overflow-x: auto; padding-bottom: .3rem; }

  /* Quote builder: customer → map → job → price */
  .builder { display: flex; flex-direction: column; gap: .8rem; align-items: stretch; }
  .builder > .grid { display: contents; }
  .p-customer { order: 1; } .p-map { order: 2; } .p-job { order: 3; } .p-items { order: 4; }
  #map { height: 62vh; min-height: 340px; }
  .map-tools { top: auto; bottom: 46px; left: 8px; right: 8px; transform: none; }
  .map-tools .btn.sm { padding: .55rem .7rem; font-size: .9rem; }
  .map-hint { bottom: 6px; }
  .map-hint span { font-size: .8rem; }
  .fields-3 { grid-template-columns: 1fr 1fr; }

  /* line items become cards */
  .items thead { display: none; }
  .items, .items tbody { display: block; }
  .items tr { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: .4rem; padding: .7rem 0; border-bottom: 1px solid var(--line); }
  .items td { display: block; border: 0; padding: 0; }
  .items td:first-child { grid-column: 1 / -1; }
  .items td[colspan] { grid-column: 1 / -1; }
  .items td[data-l]::before { content: attr(data-l); display: block; font-size: .7rem; font-weight: 600; color: var(--muted); text-transform: uppercase; margin-bottom: .15rem; }
  .items td.amt { grid-column: 1 / 3; text-align: left; font-weight: 700; font-size: 1.05rem; align-self: end; }
  .items td.rm { text-align: right; align-self: end; }
  .hide-sm { display: none; }

  .sched-bar h2 { width: 100%; }
  .sched-bar .spacer { display: none; }
  .sched-title { flex: 1; text-align: center; font-size: .95rem; }
  .sched { display: flex; flex-direction: column; }
  .sched > div:last-child { order: -1; } /* agenda first, side panels below */
  .modal-bg { align-items: end; padding: 0; }
  .modal { width: 100%; border-radius: 16px 16px 0 0; max-height: 92vh; padding-bottom: calc(1.2rem + env(safe-area-inset-bottom)); }
}
@media (display-mode: standalone) { .demo-pill { display: none; } }
