/* ==========================================================================
   Design tokens
   ========================================================================== */
:root {
  --bg: #F4F5F7;
  --card: #FFFFFF;
  --border: #DDE1E6;
  --text: #15191E;
  --text-2: #5B6470;
  --primary: #1F5FBF;
  --primary-press: #194E9E;
  --muted: #EEF0F3;

  --cs1: #6BAF92;
  --cs2: #E3B341;
  --cs3: #E07A2E;
  --cs4: #B3261E;

  --revisit-text: #9A3F06;
  --revisit-bg: #FEF0E6;
  --revisit-border: #E07A2E;
  --severe-bg: #FBE7E6;

  --radius-card: 14px;
  --radius-ctl: 10px;
  --tap: 44px;
  --gutter: 16px;
  --content-max: 720px;

  --font: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* ==========================================================================
   Base
   ========================================================================== */
*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

h1, h2, h3, p { margin: 0; }
a { color: inherit; text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
img { display: block; max-width: 100%; }
[hidden] { display: none !important; }

:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

.mono { font-family: var(--mono); }
.muted { color: var(--text-2); }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

.icon { width: 20px; height: 20px; flex: none; }
.icon-sm { width: 16px; height: 16px; flex: none; }

/* ==========================================================================
   Controls
   ========================================================================== */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--tap); padding: 0 16px;
  border-radius: var(--radius-ctl);
  border: 1px solid var(--border);
  background: var(--card);
  font-weight: 600; font-size: 16px;
  transition: background-color .15s, border-color .15s;
}
.btn:active { background: var(--muted); }

.btn-primary {
  background: var(--primary); border-color: var(--primary); color: #fff;
  min-height: 52px; font-size: 17px;
}
.btn-primary:active { background: var(--primary-press); }
.btn-block { width: 100%; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: var(--tap); height: var(--tap);
  border: 0; background: transparent; border-radius: var(--radius-ctl);
}
.icon-btn:active { background: var(--muted); }
.icon-btn .icon { width: 24px; height: 24px; }

.field { display: flex; flex-direction: column; gap: 8px; margin-bottom: 18px; }
.field-label { font-weight: 600; font-size: 15px; display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.field-label .req { color: var(--cs4); }
.field-hint { font-size: 13px; color: var(--text-2); }
.field-error { font-size: 13px; color: var(--cs4); font-weight: 500; }

.input, .select, .textarea {
  width: 100%;
  min-height: var(--tap);
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-ctl);
  background: var(--card);
  font-size: 16px; /* 16px keeps iOS from zooming on focus */
}
.textarea { min-height: 88px; resize: vertical; line-height: 1.35; }
.select {
  appearance: none; -webkit-appearance: none;
  padding-right: 40px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 24 24' fill='none' stroke='%235B6470' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center;
}
.input.invalid, .select.invalid, .textarea.invalid { border-color: var(--cs4); }

/* ==========================================================================
   Chips, badges, pills
   ========================================================================== */
.cs-chip {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 2px 8px; border-radius: 6px;
  font-size: 13px; font-weight: 700; white-space: nowrap; line-height: 1.5;
}
.cs-1 { background: var(--cs1); color: var(--text); }
.cs-2 { background: var(--cs2); color: var(--text); }
.cs-3 { background: var(--cs3); color: var(--text); }
.cs-4 { background: var(--cs4); color: #fff; }

.pill {
  display: inline-flex; align-items: center;
  padding: 4px 10px; border-radius: 8px;
  font-size: 14px; font-weight: 600; white-space: nowrap;
  background: var(--muted); color: var(--text);
}
.pill-severe { background: var(--severe-bg); color: var(--cs4); }
.pill-revisit { background: var(--revisit-bg); color: var(--revisit-text); }
.pill-todo { background: transparent; border: 1px dashed #9AA3AE; color: var(--text-2); }

.badge-revisit {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 10px; border-radius: 6px;
  background: var(--revisit-bg); color: var(--revisit-text);
  font-size: 13px; font-weight: 700; letter-spacing: .02em; text-transform: uppercase;
}

/* ==========================================================================
   Layout
   ========================================================================== */
.page { max-width: var(--content-max); margin: 0 auto; padding: 0 var(--gutter); }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  padding: 14px;
}

/* ==========================================================================
   Home
   ========================================================================== */
.home { min-height: 100vh; min-height: 100dvh; display: flex; flex-direction: column; }
.home-top { display: flex; align-items: center; justify-content: space-between; padding: 20px 0 8px; }
.brand { display: flex; align-items: center; gap: 12px; font-weight: 700; font-size: 18px; }
.brand-mark {
  width: 36px; height: 36px; border-radius: 9px; background: var(--text); color: #fff;
  display: inline-flex; align-items: center; justify-content: center;
}
.demo-pill {
  border: 1.5px solid var(--border); border-radius: 999px; padding: 3px 12px;
  font-size: 13px; font-weight: 700; color: var(--text-2); letter-spacing: .03em;
}
.home h1 { font-size: 32px; font-weight: 700; letter-spacing: -.02em; margin-top: 24px; }
.home-intro { color: var(--text-2); margin-top: 6px; font-size: 16px; }
.project-list { display: flex; flex-direction: column; gap: 14px; margin-top: 22px; }
.project-card { display: block; padding: 16px; position: relative; transition: border-color .15s; }
.project-card:hover { border-color: #B9C0C8; }
.project-card:active { background: #FAFBFC; }
.project-card .chev { position: absolute; top: 16px; right: 14px; color: var(--text-2); }
.eyebrow { font-size: 12px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--text-2); }
.project-card h2 { font-size: 20px; font-weight: 600; margin-top: 4px; padding-right: 28px; }
.project-card .office { color: var(--text-2); margin-top: 2px; }
.count-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.project-link { font-family: var(--mono); font-size: 13px; color: var(--text-2); margin-top: 12px; }
.home-foot {
  margin-top: auto; padding: 32px 0 calc(24px + var(--safe-bottom));
  display: flex; gap: 10px; color: var(--text-2); font-size: 14px;
}
.home-foot .icon { margin-top: 1px; }

/* ==========================================================================
   Project header + toggle
   ========================================================================== */
.proj-header {
  position: sticky; top: 0; z-index: 1000;
  background: var(--card);
  border-bottom: 1px solid var(--border);
}
.proj-header-inner { max-width: var(--content-max); margin: 0 auto; padding: 10px var(--gutter) 12px; }
.office-mode .proj-header-inner { max-width: none; }
.proj-title-row { display: flex; align-items: center; gap: 8px; }
.proj-title-row .back { margin-left: -10px; }
.proj-titles { flex: 1; min-width: 0; }
.proj-titles h1 { font-size: 19px; font-weight: 700; line-height: 1.2; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.proj-titles p { color: var(--text-2); font-size: 14px; }

.segmented {
  display: grid; grid-template-columns: 1fr 1fr; gap: 4px;
  margin-top: 10px; padding: 4px;
  background: var(--muted); border-radius: 12px;
}
.segmented button {
  min-height: 40px; border: 0; border-radius: 9px; background: transparent;
  font-weight: 600; color: var(--text-2); font-size: 16px;
}
.segmented button[aria-selected="true"] {
  background: var(--card); color: var(--text);
  box-shadow: 0 1px 2px rgba(21, 25, 30, .12), 0 0 0 1px rgba(21, 25, 30, .04);
}
.office-mode .segmented { max-width: 420px; }

/* ==========================================================================
   Field view
   ========================================================================== */
.field-view { padding-bottom: calc(110px + var(--safe-bottom)); }
.list-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; padding: 18px 0 12px; }
.list-head h2 { font-size: 22px; font-weight: 700; letter-spacing: -.01em; }
.list-head .meta { color: var(--text-2); font-size: 14px; text-align: right; }

.inspector-picker { position: relative; display: inline-flex; align-items: center; gap: 4px; color: var(--text-2); font-size: 14px; }
.inspector-picker select {
  appearance: none; -webkit-appearance: none; border: 0; background: transparent;
  font-size: 14px; color: var(--text); font-weight: 600; padding: 10px 16px 10px 0; margin-right: 4px; min-height: var(--tap);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='none' stroke='%235B6470' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right center;
}

.defect-list { display: flex; flex-direction: column; gap: 12px; }
.defect-card { padding: 12px; }
.defect-card.is-revisit { border: 2px solid var(--revisit-border); padding: 11px; }
.defect-card.is-new { animation: flash 1.6s ease-out; }
@keyframes flash { 0% { box-shadow: 0 0 0 4px rgba(31, 95, 191, .35); } 100% { box-shadow: 0 0 0 4px rgba(31, 95, 191, 0); } }

.defect-row { display: flex; gap: 14px; }
.thumb {
  width: 84px; height: 84px; flex: none; border-radius: 10px; overflow: hidden;
  background: #BCC0BB; position: relative;
}
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.no-photo {
  width: 100%; height: 100%; display: flex; align-items: flex-end; padding: 8px;
  font-size: 11px; font-weight: 700; color: #3E444B; letter-spacing: .04em; background: #BCC0BB;
}
.defect-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.defect-title-row { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.defect-title { font-size: 18px; font-weight: 600; line-height: 1.25; }
.defect-meta { color: var(--text-2); font-size: 14px; }
.defect-status { font-size: 14px; font-weight: 600; }
.defect-status.pending { font-weight: 400; color: var(--text-2); }
.engineer-note {
  margin-top: 12px; padding: 12px 14px; border-radius: 10px;
  background: #FFF6EF;
}
.engineer-note .label { color: var(--revisit-text); font-weight: 600; font-size: 14px; margin-bottom: 2px; }

.empty {
  text-align: center; color: var(--text-2); padding: 40px 16px;
  border: 1px dashed var(--border); border-radius: var(--radius-card); background: var(--card);
}

.bottom-bar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 900;
  padding: 12px var(--gutter) calc(14px + var(--safe-bottom));
  background: var(--bg);
}
.bottom-bar-inner { max-width: var(--content-max); margin: 0 auto; }
.bottom-bar .btn-primary { min-height: 56px; font-size: 18px; border-radius: 14px; box-shadow: 0 6px 16px rgba(31, 95, 191, .22); }
.bottom-bar .btn-primary .icon { width: 24px; height: 24px; }

/* ==========================================================================
   Add defect screen
   ========================================================================== */
.screen {
  position: fixed; inset: 0; z-index: 2000;
  background: var(--card);
  display: flex; flex-direction: column;
}
.screen-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 8px 8px var(--gutter); border-bottom: 1px solid var(--border);
}
.screen-head h2 { font-size: 22px; font-weight: 700; }
.screen-body { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.screen-body-inner { max-width: var(--content-max); margin: 0 auto; padding: 16px var(--gutter) 8px; }
.screen-foot { border-top: 1px solid var(--border); padding: 12px var(--gutter) calc(12px + var(--safe-bottom)); }
.screen-foot-inner { max-width: var(--content-max); margin: 0 auto; }

.photo-row { display: flex; gap: 14px; align-items: stretch; margin-bottom: 18px; }
.photo-preview { width: 132px; height: 110px; border-radius: 12px; overflow: hidden; flex: none; background: #BCC0BB; }
.photo-preview img { width: 100%; height: 100%; object-fit: cover; }
.photo-side { flex: 1; min-width: 0; display: flex; flex-direction: column; justify-content: center; gap: 8px; }
.photo-name { color: var(--text-2); font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.cs-picker { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.cs-picker button {
  min-height: 64px; border-radius: var(--radius-ctl);
  border: 1px solid var(--border); background: var(--card);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
}
.cs-picker .num { font-size: 20px; font-weight: 700; line-height: 1; }
.cs-picker .lbl { font-size: 14px; color: var(--text-2); }
.cs-picker button[aria-checked="true"] { border: 2px solid var(--text); }
.cs-picker button[aria-checked="true"] .lbl { color: inherit; font-weight: 600; }
.cs-picker button.cs-1[aria-checked="true"] { background: var(--cs1); }
.cs-picker button.cs-2[aria-checked="true"] { background: var(--cs2); }
.cs-picker button.cs-3[aria-checked="true"] { background: var(--cs3); }
.cs-picker button.cs-4[aria-checked="true"] { background: var(--cs4); color: #fff; }
.cs-picker button:not([aria-checked="true"]) { background: var(--card); color: var(--text); }

.picker-map-wrap { position: relative; }
.picker-map { height: 220px; border-radius: 12px; border: 1px solid var(--border); overflow: hidden; background: #E6E8E3; }
.map-hint {
  position: absolute; left: 10px; bottom: 10px; z-index: 500;
  background: #fff; padding: 5px 10px; border-radius: 8px; font-size: 14px; font-weight: 500;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .15); pointer-events: none;
}
.coords { font-family: var(--mono); font-size: 14px; font-weight: 400; color: var(--text-2); }
.loc-actions { display: flex; justify-content: flex-end; margin-top: 2px; }
.link-btn {
  border: 0; background: transparent; color: var(--primary); font-weight: 600; font-size: 14px;
  min-height: var(--tap); padding: 0 4px; display: inline-flex; align-items: center; gap: 6px;
}

/* ==========================================================================
   Map pins
   ========================================================================== */
.pin-cs {
  width: 30px; height: 30px; border-radius: 50%;
  border: 2.5px solid #fff; box-shadow: 0 1px 4px rgba(0, 0, 0, .35);
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 700; font-family: var(--font);
}
.pin-cs.selected { width: 40px; height: 40px; border: 3.5px solid var(--text); font-size: 16px; }
.pin-drop svg { filter: drop-shadow(0 2px 2px rgba(0, 0, 0, .3)); }
.leaflet-container { font-family: var(--font); }
.leaflet-bar a { width: 40px !important; height: 40px !important; line-height: 40px !important; font-size: 20px !important; }
.leaflet-control-attribution { font-size: 10px; }

/* ==========================================================================
   Office view
   ========================================================================== */
.office-layout { display: flex; flex-direction: column; }
.office-map-col { position: relative; }
.office-map { height: 250px; background: #E6E8E3; }
.legend {
  display: flex; flex-wrap: wrap; gap: 6px 16px; align-items: center;
  padding: 10px var(--gutter); background: var(--card); border-bottom: 1px solid var(--border);
  font-size: 14px; font-weight: 500;
}
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 12px; height: 12px; border-radius: 50%; display: inline-block; }
.office-list-col { padding: 0 var(--gutter) calc(32px + var(--safe-bottom)); }
.office-list-col .list-head h2 { font-size: 20px; }

.office-item {
  display: flex; align-items: center; gap: 14px; width: 100%;
  text-align: left; padding: 12px 14px; min-height: 64px;
}
.office-item[aria-current="true"] { border: 2px solid var(--text); padding: 11px 13px; }
.cs-square {
  width: 38px; height: 38px; border-radius: 8px; flex: none;
  display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 17px;
}
.office-item .txt { flex: 1; min-width: 0; }
.office-item .t1, .office-item .t2 { display: block; }
.office-item .t1 { font-weight: 600; font-size: 17px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.office-item .t2 { color: var(--text-2); font-size: 14px; }

.pdf-btn { min-height: var(--tap); padding: 0 14px; gap: 6px; }

/* Bottom sheet */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 1500; background: rgba(21, 25, 30, .18);
  animation: fade .18s ease-out;
}
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 1600;
  max-height: 74vh; max-height: 74dvh;
  background: var(--card); border-radius: 22px 22px 0 0;
  box-shadow: 0 -8px 30px rgba(21, 25, 30, .18);
  display: flex; flex-direction: column;
  animation: slide-up .22s ease-out;
}
.sheet-grip { width: 40px; height: 5px; border-radius: 3px; background: #C9CED4; margin: 8px auto 0; flex: none; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 4px 8px 0 var(--gutter); flex: none; }
.sheet-head .count { color: var(--text-2); font-weight: 600; font-size: 15px; }
.sheet-body { overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 6px var(--gutter) calc(20px + var(--safe-bottom)); }
.sheet-photo { width: 100%; aspect-ratio: 16 / 9; border-radius: 14px; overflow: hidden; background: #BCC0BB; }
.sheet-photo img { width: 100%; height: 100%; object-fit: cover; }
.sheet-title-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 16px; }
.sheet-title-row h3 { font-size: 26px; font-weight: 700; letter-spacing: -.01em; line-height: 1.15; }
.sheet-title-row .cs-chip { font-size: 15px; padding: 4px 10px; }
.kv-grid { display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); gap: 12px; margin-top: 12px; }
.kv-grid .k { color: var(--text-2); font-size: 14px; }
.kv-grid .v { font-size: 15px; }
.kv-grid .v.mono { font-size: 13.5px; white-space: nowrap; }
.sheet-notes { margin-top: 14px; font-size: 16px; padding-bottom: 16px; border-bottom: 1px solid var(--border); }
.sheet-section { margin-top: 16px; }

.status-picker { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.status-picker button {
  min-height: var(--tap); padding: 6px 4px; border-radius: var(--radius-ctl);
  border: 1px solid var(--border); background: var(--card); font-weight: 500; font-size: 15px;
}
.status-picker button[aria-checked="true"] { border: 2px solid var(--text); background: var(--muted); font-weight: 600; }
.status-picker button.revisit[aria-checked="true"] { border-color: var(--revisit-text); background: var(--revisit-bg); color: var(--revisit-text); }

/* Toast */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(96px + var(--safe-bottom)); z-index: 3000;
  background: var(--text); color: #fff; padding: 12px 16px; border-radius: 12px;
  font-size: 15px; font-weight: 500; max-width: calc(100% - 32px); width: max-content;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .2);
  animation: fade .18s ease-out;
}

.loading, .error-box { padding: 48px var(--gutter); text-align: center; color: var(--text-2); }
.error-box h2 { color: var(--text); font-size: 20px; margin-bottom: 8px; }
.error-box .btn { margin-top: 16px; }

@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes slide-up { from { transform: translateY(40px); opacity: .6; } to { transform: none; opacity: 1; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* ==========================================================================
   Wider screens: office list beside the map
   ========================================================================== */
@media (min-width: 900px) {
  .office-layout {
    flex-direction: row; align-items: flex-start;
    height: calc(100vh - var(--header-h, 120px));
  }
  .office-map-col { flex: 1; height: 100%; display: flex; flex-direction: column; position: sticky; top: var(--header-h, 120px); }
  .office-map { flex: 1; height: auto; }
  .office-list-col {
    width: 420px; flex: none; height: 100%; overflow-y: auto;
    border-left: 1px solid var(--border);
  }
  .sheet {
    left: auto; right: calc(420px + 16px); bottom: 16px; top: calc(var(--header-h, 120px) + 16px);
    width: min(440px, calc(100vw - 420px - 80px)); max-height: none; border-radius: 18px;
  }
  .sheet-grip { display: none; }
  .sheet-backdrop { background: transparent; }
}

/* Very narrow phones: stack the detail fields */
@media (max-width: 359px) {
  .kv-grid { grid-template-columns: 1fr; gap: 8px; }
}
