:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --line: #e3e6ea;
  --line-strong: #cbd1d9;
  --ink: #14161a;
  --ink-2: #4b525c;
  --ink-3: #838b96;
  --accent: #1f6feb;
  --accent-soft: #e8f0fe;
  --green: #17803d;
  --green-soft: #dcfce7;
  --amber: #92400e;
  --amber-soft: #fef3c7;
  --red: #b42318;
  --red-soft: #fee4e2;
  --purple: #6941c6;
  --purple-soft: #f4ebff;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.1);
  --shadow-lg: 0 12px 32px rgba(16, 24, 40, 0.18);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body { height: 100%; }

body { touch-action: manipulation; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0; letter-spacing: -0.02em; }
h1 { font-size: 22px; }
h2 { font-size: 17px; }
h3 { font-size: 14px; }

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

.muted { color: var(--ink-3); }
.small { font-size: 12px; }
.r { text-align: right; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; letter-spacing: 0.3px; }
.hide { display: none !important; }
.grow { flex: 1; }
.row { display: flex; gap: 12px; align-items: center; }
.wrap { flex-wrap: wrap; }

/* ---------- app shell ---------- */
#app { min-height: 100%; display: flex; flex-direction: column; }

header.topbar {
  background: var(--ink);
  color: #fff;
  padding: env(safe-area-inset-top) max(20px, env(safe-area-inset-right)) 0 max(20px, env(safe-area-inset-left));
  height: calc(56px + env(safe-area-inset-top));
  display: flex;
  align-items: center;
  gap: 4px;
  position: sticky;
  top: 0;
  z-index: 30;
}

.brand {
  font-weight: 700;
  font-size: 15px;
  letter-spacing: -0.02em;
  margin-right: 18px;
  white-space: nowrap;
}
.brand span { color: #7d8896; font-weight: 500; }

nav.tabs { display: flex; gap: 2px; overflow-x: auto; scrollbar-width: none; }
nav.tabs::-webkit-scrollbar { display: none; }

nav.tabs button {
  background: none;
  border: 0;
  color: #aeb6c0;
  font: inherit;
  font-size: 13.5px;
  font-weight: 500;
  padding: 8px 13px;
  border-radius: 7px;
  cursor: pointer;
  white-space: nowrap;
}
nav.tabs button:hover { color: #fff; background: rgba(255, 255, 255, 0.08); }
nav.tabs button.active { color: var(--ink); background: #fff; }

.whoami { margin-left: auto; display: flex; align-items: center; gap: 12px; font-size: 13px; color: #aeb6c0; white-space: nowrap; }
.whoami strong { color: #fff; font-weight: 600; }

main {
  padding: 22px max(22px, env(safe-area-inset-right)) calc(22px + env(safe-area-inset-bottom)) max(22px, env(safe-area-inset-left));
  max-width: 1240px;
  width: 100%;
  margin: 0 auto;
  flex: 1;
}

.page-head { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }

/* ---------- buttons ---------- */
button, .btn {
  font: inherit;
  font-size: 13.5px;
  font-weight: 500;
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s;
  white-space: nowrap;
}
button:hover:not(:disabled) { background: #f0f2f5; }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.primary { background: var(--ink); color: #fff; border-color: var(--ink); }
button.primary:hover:not(:disabled) { background: #2a2f38; }
button.accent { background: var(--accent); color: #fff; border-color: var(--accent); }
button.accent:hover:not(:disabled) { background: #1a5fd0; }
button.danger { color: var(--red); border-color: #f3c6c2; background: #fff; }
button.danger:hover:not(:disabled) { background: var(--red-soft); }
button.ghost { border-color: transparent; background: transparent; }
button.ghost:hover:not(:disabled) { background: #e9ecf0; }
button.sm { padding: 5px 10px; font-size: 12.5px; border-radius: 6px; }

/* ---------- cards ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.card-pad { padding: 18px; }
.card h2 { margin-bottom: 12px; }

.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; margin-bottom: 18px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 15px 16px; box-shadow: var(--shadow); }
.stat .k { font-size: 11px; text-transform: uppercase; letter-spacing: 0.07em; color: var(--ink-3); font-weight: 600; }
.stat .v { font-size: 25px; font-weight: 700; letter-spacing: -0.03em; margin-top: 5px; }
.stat .sub { font-size: 12px; color: var(--ink-3); margin-top: 2px; }
.stat .v.pos { color: var(--green); }
.stat .v.neg { color: var(--red); }

/* ---------- tables ---------- */
table { width: 100%; border-collapse: collapse; }
th {
  text-align: left;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-3);
  font-weight: 600;
  padding: 9px 12px;
  border-bottom: 1px solid var(--line);
  background: #fafbfc;
}
td { padding: 11px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover { background: #f7f9fb; }
.table-wrap { overflow-x: auto; border-radius: var(--radius); }

/* ---------- pills ---------- */
.pill {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.pill.scheduled { background: var(--accent-soft); color: #1a4fa8; }
.pill.in_progress { background: var(--purple-soft); color: var(--purple); }
.pill.complete { background: var(--green-soft); color: var(--green); }
.pill.cancelled { background: #eef0f3; color: var(--ink-3); }
.pill.paid { background: var(--green-soft); color: var(--green); }
.pill.unpaid { background: var(--amber-soft); color: var(--amber); }
.pill.sent { background: var(--accent-soft); color: #1a4fa8; }
.pill.void { background: #eef0f3; color: var(--ink-3); }
.pill.draft { background: #eef0f3; color: var(--ink-2); }
.pill.owner { background: var(--ink); color: #fff; }
.pill.installer { background: #eef0f3; color: var(--ink-2); }

/* ---------- calendar ---------- */
.cal-head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.cal-title { font-size: 19px; font-weight: 700; letter-spacing: -0.02em; min-width: 190px; }

.calendar { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.cal-dow { display: grid; grid-template-columns: repeat(7, 1fr); background: #fafbfc; border-bottom: 1px solid var(--line); }
.cal-dow div { padding: 9px 10px; font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--ink-3); font-weight: 600; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); }

.cal-cell {
  min-height: 122px;
  /* Without this, wide chips blow out the 1fr columns and the week stops lining up. */
  min-width: 0;
  overflow: hidden;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  padding: 7px;
  cursor: pointer;
  position: relative;
  transition: background 0.1s;
}
.cal-cell:nth-child(7n) { border-right: 0; }
.cal-cell:hover { background: #f7f9fb; }
.cal-cell.other { background: #fbfcfd; }
.cal-cell.other .daynum { color: var(--line-strong); }
.cal-cell.today .daynum { background: var(--ink); color: #fff; }
.daynum {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--ink-2);
  width: 23px; height: 23px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 50%;
  margin-bottom: 4px;
}
.cal-cell .add-hint {
  position: absolute; top: 7px; right: 7px;
  color: var(--line-strong); font-size: 17px; line-height: 1; opacity: 0;
}
.cal-cell:hover .add-hint { opacity: 1; }

.chip {
  display: block;
  width: 100%;
  max-width: 100%;
  text-align: left;
  font-size: 11.5px;
  padding: 4px 7px;
  border-radius: 5px;
  margin-bottom: 3px;
  border: 0;
  border-left: 3px solid var(--accent);
  background: var(--accent-soft);
  color: #1a4fa8;
  cursor: pointer;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 500;
}
.chip:hover { filter: brightness(0.96); }
.chip.in_progress { background: var(--purple-soft); color: var(--purple); border-left-color: var(--purple); }
.chip.complete { background: var(--green-soft); color: var(--green); border-left-color: var(--green); }
.chip.cancelled { background: #eef0f3; color: var(--ink-3); border-left-color: var(--ink-3); text-decoration: line-through; }
.chip .t { font-variant-numeric: tabular-nums; opacity: 0.75; margin-right: 4px; }

/* ---------- forms ---------- */
label { display: block; font-size: 11.5px; font-weight: 600; color: var(--ink-2); margin-bottom: 5px; text-transform: uppercase; letter-spacing: 0.04em; }
/* 16px is deliberate: iOS Safari zooms the page when a focused input is smaller. */
input, select, textarea {
  font: inherit;
  font-size: 16px;
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  -webkit-appearance: none;
  appearance: none;
}
select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%23838b96' d='M1 1l5 5 5-5'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 32px;
}
input[type="checkbox"] { -webkit-appearance: checkbox; appearance: checkbox; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }
textarea { min-height: 66px; resize: vertical; }
.field { margin-bottom: 13px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; }
.checkline { display: flex; align-items: center; gap: 9px; padding: 7px 10px; border: 1px solid var(--line); border-radius: 8px; margin-bottom: 6px; cursor: pointer; }
.checkline:hover { background: #f7f9fb; }
.checkline input { width: auto; }
.checkline.on { border-color: var(--accent); background: var(--accent-soft); }

fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin: 0 0 14px; }
legend { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--ink-3); font-weight: 700; padding: 0 6px; }

/* ---------- modal ---------- */
.overlay {
  position: fixed; inset: 0;
  background: rgba(16, 24, 40, 0.45);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 40px 16px;
  z-index: 60;
  overflow-y: auto;
}
.modal {
  background: var(--surface);
  border-radius: 12px;
  width: 100%;
  max-width: 620px;
  box-shadow: var(--shadow-lg);
  animation: pop 0.13s ease-out;
}
@keyframes pop { from { transform: translateY(-6px); opacity: 0; } }
.modal-head { padding: 17px 20px; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 12px; }
.modal-body { padding: 20px; }
.modal-foot { padding: 14px 20px; border-top: 1px solid var(--line); display: flex; gap: 10px; justify-content: flex-end; background: #fafbfc; border-radius: 0 0 12px 12px; flex-wrap: wrap; }
.modal-foot .grow { flex: 1; }

/* ---------- login ---------- */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
.login-card { width: 100%; max-width: 380px; background: var(--surface); border-radius: 12px; padding: 32px; box-shadow: var(--shadow-lg); }
.login-card h1 { margin-bottom: 4px; }
.login-card p.sub { color: var(--ink-3); margin: 0 0 22px; font-size: 13.5px; }

.error-msg { background: var(--red-soft); color: var(--red); padding: 9px 12px; border-radius: 8px; font-size: 13px; margin-bottom: 14px; }
.ok-msg { background: var(--green-soft); color: var(--green); padding: 9px 12px; border-radius: 8px; font-size: 13px; margin-bottom: 14px; }

.empty { text-align: center; padding: 48px 20px; color: var(--ink-3); }
.empty h3 { color: var(--ink-2); margin-bottom: 5px; }

/* ---------- agenda list (owner calendar on a phone) ---------- */
.agenda-day { margin-bottom: 14px; }
.agenda-date {
  font-size: 12px; font-weight: 700; letter-spacing: 0.05em; text-transform: uppercase;
  color: var(--ink-3); padding: 0 2px 7px;
}
.agenda-date.is-today { color: var(--accent); }
.agenda-job {
  display: flex; align-items: center; gap: 12px; width: 100%; text-align: left;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 13px 14px; margin-bottom: 7px; box-shadow: var(--shadow); cursor: pointer;
}
.agenda-job:hover { background: #f7f9fb; }
.agenda-job .bar { width: 4px; align-self: stretch; border-radius: 3px; background: var(--accent); flex: 0 0 4px; }
.agenda-job.in_progress .bar { background: var(--purple); }
.agenda-job.complete .bar { background: var(--green); }
.agenda-job.cancelled .bar { background: var(--ink-3); }
.agenda-job .body { flex: 1; min-width: 0; }
.agenda-job .t { font-size: 12px; font-weight: 700; color: var(--accent); font-variant-numeric: tabular-nums; }
.agenda-job.complete .t { color: var(--green); }
.agenda-job .n { font-size: 15px; font-weight: 700; letter-spacing: -0.01em; }
.agenda-job .d { font-size: 12.5px; color: var(--ink-3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.agenda-job .amt { font-size: 14px; font-weight: 700; flex: 0 0 auto; }

/* ---------- installer job cards ---------- */
.job-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 11px; box-shadow: var(--shadow); }
.job-card .when { font-size: 12px; font-weight: 700; color: var(--accent); text-transform: uppercase; letter-spacing: 0.05em; }
.job-card .who { font-size: 17px; font-weight: 700; margin: 3px 0 1px; letter-spacing: -0.02em; }
.job-card .what { color: var(--ink-2); }
.logged { background: var(--green-soft); color: var(--green); padding: 6px 11px; border-radius: 8px; font-size: 13px; font-weight: 600; display: inline-block; }

#update-bar {
  position: fixed;
  bottom: calc(18px + env(safe-area-inset-bottom));
  left: 50%; transform: translateX(-50%);
  background: var(--ink); color: #fff;
  padding: 10px 12px 10px 18px;
  border-radius: 999px;
  display: flex; align-items: center; gap: 10px;
  font-size: 13.5px; font-weight: 500;
  box-shadow: var(--shadow-lg);
  z-index: 95;
  max-width: calc(100vw - 24px);
  animation: pop 0.15s ease-out;
}
#update-bar button { padding: 6px 14px; font-size: 13px; border-radius: 999px; }
#update-bar button#do-update { background: #fff; color: var(--ink); border-color: #fff; }
#update-bar button.ghost { background: transparent; color: #aeb6c0; border-color: transparent; }
#update-bar button.ghost:hover { background: rgba(255, 255, 255, 0.1); }

.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 11px 20px; border-radius: 9px;
  font-size: 13.5px; font-weight: 500; box-shadow: var(--shadow-lg); z-index: 100;
  animation: pop 0.15s ease-out;
}
.toast.err { background: var(--red); }

/* Touch devices get bigger hit targets than a mouse needs. */
@media (pointer: coarse) {
  button, .btn { min-height: 44px; padding: 10px 16px; }
  button.sm { min-height: 38px; }
  .chip { min-height: 26px; }
  .checkline { padding: 12px; }
  td { padding: 14px 12px; }
}

@media (max-width: 760px) {
  main { padding: 14px 14px calc(14px + env(safe-area-inset-bottom)); }
  .brand {
    margin-right: 8px; font-size: 14px;
    max-width: 26vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  }
  .brand span { display: none; }
  .whoami strong { display: none; }
  .grid2, .grid3 { grid-template-columns: 1fr; }
  .cal-cell { min-height: 78px; padding: 4px; }
  .cal-dow div { padding: 7px 4px; font-size: 10px; }
  .chip { font-size: 10px; padding: 2px 4px; }
}

/* On a phone the tabs matter more than the shop's name. */
@media (max-width: 560px) {
  .brand { display: none; }
  header.topbar { padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right)); }
  nav.tabs button { padding: 8px 11px; }
}

/* ---------- live indicator ---------- */
.live-dot {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 11.5px; font-weight: 600; letter-spacing: 0.04em;
  text-transform: uppercase; color: var(--ink-3);
  padding: 3px 10px 3px 8px; border-radius: 999px;
  background: #eef7f0; border: 1px solid #cfe8d8;
}
.live-dot i {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--green); display: block;
  animation: live-pulse 2.4s ease-in-out infinite;
}
@keyframes live-pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.25; } }

/* ---------- type-ahead pickers ---------- */
/* Used for finding a customer by name and a vehicle by the tail of its VIN.
   The list floats over the form so a long match list never pushes the rest of
   the fields down while you are reading it. */
.ta-wrap { position: relative; }

.ta-list {
  position: absolute; z-index: 40; left: 0; right: 0; top: 100%;
  margin-top: 4px; max-height: 280px; overflow-y: auto;
  background: var(--surface); border: 1px solid var(--line-strong);
  border-radius: var(--radius); box-shadow: 0 8px 24px rgba(16, 24, 40, 0.14);
  -webkit-overflow-scrolling: touch;
}

.ta-item {
  display: block; width: 100%; text-align: left;
  padding: 10px 13px; border: 0; border-bottom: 1px solid var(--line);
  background: none; font: inherit; color: var(--ink); cursor: pointer;
  /* Comfortably tappable on the wall iPad rather than mouse-sized. */
  min-height: 44px;
}
.ta-item:last-child { border-bottom: 0; }
.ta-item:hover, .ta-item:focus { background: var(--accent-soft); }
.ta-item strong { display: block; font-weight: 600; }
.ta-item .small { display: block; margin-top: 1px; }
.ta-item.ta-new { color: var(--accent); font-weight: 600; }
.ta-item.muted { cursor: default; }
.ta-item.muted:hover { background: none; }

/* The "this is who / what you picked" panel that replaces the search box. */
.chosen-box {
  border: 1px solid var(--line-strong); border-radius: var(--radius);
  background: var(--bg); padding: 11px 13px; margin-bottom: 13px;
}
