:root {
  --bg: #0b1020;
  --bg-elev: #10162c;
  --card: rgba(255, 255, 255, 0.045);
  --card-hover: rgba(255, 255, 255, 0.07);
  --border: rgba(255, 255, 255, 0.09);
  --border-soft: rgba(255, 255, 255, 0.06);
  --accent: #5b8cff;
  --accent2: #8b5bff;
  --accent-soft: rgba(91, 140, 255, 0.15);
  --text: #e8ecf6;
  --muted: #9aa4bf;
  --muted-2: #6b7593;
  --green: #35d07f;
  --green-soft: rgba(53, 208, 127, 0.14);
  --red: #ff5c72;
  --red-soft: rgba(255, 92, 114, 0.14);
  --amber: #ffb454;
  --amber-soft: rgba(255, 180, 84, 0.14);
  --gray-soft: rgba(154, 164, 191, 0.14);
  --radius: 18px;
  --radius-sm: 12px;
  --shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}
/* Deliberately no `height: 100%` on html/body: it caps the sticky topbar's containing block
   at one viewport, so the bar scrolls out of view instead of staying pinned. */
body {
  margin: 0;
  font-family: var(--font);
  background:
    radial-gradient(1200px 600px at 15% -10%, #1b2550 0%, transparent 60%),
    radial-gradient(1000px 500px at 100% 0%, #2a1b50 0%, transparent 55%),
    var(--bg);
  background-attachment: fixed;
  background-color: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
h1, h2, h3 { margin: 0; }
p { margin: 0; }
button {
  font-family: inherit;
  -webkit-appearance: none;
  appearance: none;
}
input, textarea, select {
  font-family: inherit;
  color: var(--text);
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 16px;
  outline: none;
  transition: border-color .15s ease, background .15s ease, box-shadow .15s ease;
  width: 100%;
  -webkit-appearance: none;
  appearance: none;
}
input::placeholder, textarea::placeholder { color: var(--muted-2); }
input:focus, textarea:focus {
  border-color: var(--accent);
  background: rgba(91,140,255,0.06);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
textarea { resize: vertical; min-height: 44px; }
input[type="date"] {
  color-scheme: dark;
  min-height: 42px;
}
a { color: inherit; text-decoration: none; }

.hidden { display: none !important; }

/* ---------- Topbar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  background: rgba(11, 16, 32, 0.7);
  padding-top: env(safe-area-inset-top);
  border-bottom: 1px solid var(--border-soft);
}
.topbar-inner {
  max-width: 1120px;
  margin: 0 auto;
  padding: 14px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; letter-spacing: .01em; }
.brand-dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  box-shadow: 0 0 0 4px rgba(91,140,255,0.15);
}
.topbar-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; justify-content: flex-end; }

.save-indicator {
  font-size: 12.5px;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
}
.save-indicator .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--muted-2); }
.save-indicator.saving .dot { background: var(--amber); animation: pulse 1s infinite ease-in-out; }
.save-indicator.saved .dot { background: var(--green); }
.save-indicator.error .dot { background: var(--red); }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .35; } }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--border);
  background: rgba(255,255,255,0.04);
  color: var(--text);
  padding: 9px 16px;
  border-radius: 999px;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  transition: transform .12s ease, background .15s ease, border-color .15s ease, box-shadow .15s ease;
  white-space: nowrap;
}
.btn:hover { background: rgba(255,255,255,0.08); transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn-primary {
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  border-color: transparent;
  box-shadow: 0 8px 24px rgba(91,140,255,0.35);
}
.btn-primary:hover { box-shadow: 0 10px 30px rgba(91,140,255,0.45); }
.btn-ghost { background: transparent; }
.btn-sm { padding: 6px 12px; font-size: 12.5px; }
.btn-lg { padding: 13px 22px; font-size: 15px; }
.btn:disabled { opacity: .5; cursor: not-allowed; transform: none; }
.btn-danger { color: var(--red); border-color: rgba(255,92,114,0.35); }
.btn-danger:hover { background: var(--red-soft); }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 32px; height: 32px; border-radius: 50%;
  border: 1px solid var(--border); background: rgba(255,255,255,0.03);
  color: var(--muted); cursor: pointer; transition: all .15s ease;
}
.icon-btn:hover { color: var(--red); border-color: rgba(255,92,114,0.35); background: var(--red-soft); }

/* ---------- Layout ---------- */
.app {
  max-width: 1120px;
  margin: 0 auto;
  padding: 32px calc(24px + env(safe-area-inset-right)) calc(80px + env(safe-area-inset-bottom)) calc(24px + env(safe-area-inset-left));
}

/* ---------- Dashboard ---------- */
.hero {
  display: flex; align-items: flex-end; justify-content: space-between; gap: 24px;
  margin-bottom: 28px; flex-wrap: wrap;
}
.eyebrow {
  display: inline-block; font-size: 12px; letter-spacing: .1em; text-transform: uppercase;
  color: var(--muted); margin-bottom: 10px;
}
.hero h1 {
  font-size: 36px; line-height: 1.15; margin-bottom: 10px;
  background: linear-gradient(90deg, #fff, var(--muted));
  -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent;
}
.hero p { color: var(--muted); font-size: 15px; max-width: 520px; line-height: 1.5; }

.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 22px; flex-wrap: wrap; }
.search {
  display: flex; align-items: center; gap: 10px;
  background: var(--card); border: 1px solid var(--border); border-radius: 999px;
  padding: 10px 16px; flex: 1; min-width: 240px; max-width: 480px; color: var(--muted);
}
.search input { border: none; background: transparent; padding: 0; box-shadow: none !important; }
.filter-chips { display: flex; gap: 8px; }
.chip {
  border: 1px solid var(--border); background: transparent; color: var(--muted);
  padding: 7px 14px; border-radius: 999px; font-size: 13px; cursor: pointer; font-weight: 600;
  transition: all .15s ease;
}
.chip.active { background: var(--accent-soft); border-color: rgba(91,140,255,0.4); color: #cfe0ff; }
.chip:hover { border-color: rgba(255,255,255,0.25); }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
}
.cards-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(270px, 1fr)); gap: 16px; }
.insp-card {
  padding: 20px; cursor: pointer; position: relative; transition: transform .15s ease, background .15s ease, border-color .15s ease;
}
.insp-card:hover { background: var(--card-hover); transform: translateY(-2px); border-color: rgba(255,255,255,0.16); }
.insp-card-top { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.insp-report-no { font-size: 17px; margin-bottom: 12px; padding-right: 30px; }
.insp-report-no:empty::before { content: "Untitled report"; color: var(--muted-2); font-weight: 400; font-style: italic; }

.badge { display: inline-flex; align-items: center; gap: 6px; font-size: 11.5px; font-weight: 700; letter-spacing: .03em; text-transform: uppercase; padding: 4px 10px; border-radius: 999px; }
.status-badge.draft { background: var(--amber-soft); color: var(--amber); }
.status-badge.completed { background: var(--green-soft); color: var(--green); }

.score-ring { position: relative; width: 34px; height: 34px; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.score-ring svg { position: absolute; inset: 0; transform: rotate(-90deg); }
.score-ring .score-text {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  font-size: 9px; font-weight: 700; color: var(--text); white-space: nowrap; line-height: 1;
}

.insp-meta { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 14px; margin: 0 0 14px; }
.insp-meta div { min-width: 0; }
.insp-meta dt { font-size: 10.5px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted-2); margin-bottom: 2px; }
.insp-meta dd { margin: 0; font-size: 13px; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.insp-meta dd:empty::before { content: "—"; color: var(--muted-2); }

.insp-card-footer { display: flex; gap: 8px; flex-wrap: wrap; }
.pill { font-size: 11.5px; font-weight: 600; padding: 4px 10px; border-radius: 999px; background: var(--gray-soft); color: var(--muted); }
.pill-no { background: var(--red-soft); color: var(--red); }
.pill-open { background: var(--amber-soft); color: var(--amber); }

.card-delete { position: absolute; top: 16px; right: 16px; }

.empty-state { text-align: center; padding: 70px 20px; color: var(--muted); }
.empty-illustration { font-size: 44px; margin-bottom: 14px; }
.empty-state h3 { color: var(--text); font-size: 19px; margin-bottom: 8px; }
.empty-state p { margin-bottom: 20px; }

/* ---------- Form shell ---------- */
.form-shell { display: grid; grid-template-columns: 220px 1fr; gap: 28px; align-items: start; }
.toc {
  position: sticky; top: 84px;
  display: flex; flex-direction: column; gap: 2px;
  padding: 6px 0;
}
.toc a {
  font-size: 13px; color: var(--muted); padding: 8px 14px; border-left: 2px solid transparent;
  transition: all .15s ease;
}
.toc a:hover { color: var(--text); }
.toc a.active { color: var(--text); border-left-color: var(--accent); background: linear-gradient(90deg, var(--accent-soft), transparent); font-weight: 600; }

.form-main { min-width: 0; display: flex; flex-direction: column; gap: 20px; }

.panel { padding: 22px 24px; }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.panel-head h2 { font-size: 17px; }
.optional-tag { font-size: 11px; font-weight: 500; color: var(--muted-2); text-transform: none; margin-left: 8px; }
.cat-progress { font-size: 12.5px; color: var(--muted); font-weight: 600; background: rgba(255,255,255,0.05); padding: 4px 10px; border-radius: 999px; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px 20px; }
.field label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 6px; font-weight: 600; letter-spacing: .01em; }

/* ---------- Checklist rows ---------- */
.check-rows { display: flex; flex-direction: column; }
.check-row {
  display: grid; grid-template-columns: 1fr auto; gap: 16px; align-items: center;
  padding: 16px 4px; border-top: 1px solid var(--border-soft);
}
.check-row:first-child { border-top: none; }
.check-text { font-size: 14px; line-height: 1.45; color: var(--text); padding-right: 8px; }
.check-controls { display: flex; align-items: center; gap: 10px; }

.segmented { display: flex; background: rgba(255,255,255,0.04); border: 1px solid var(--border); border-radius: 999px; padding: 3px; flex-shrink: 0; }
.seg { border: none; background: transparent; color: var(--muted); padding: 7px 14px; border-radius: 999px; font-size: 12.5px; font-weight: 700; cursor: pointer; transition: all .15s ease; }
.seg-yes.active { background: var(--green-soft); color: var(--green); }
.seg-no.active { background: var(--red-soft); color: var(--red); }
.seg-na.active { background: var(--gray-soft); color: var(--text); }
.seg:hover:not(.active) { color: var(--text); }

.remarks-input { width: 220px; font-size: 16px; padding: 8px 12px; }

.check-row.unanswered .check-text::before { content: ""; }

/* ---------- AI rewrite ---------- */
.ai-actions { display: flex; align-items: center; gap: 8px; }
.btn-ai { border-color: rgba(139,91,255,0.35); color: #d8c9ff; }
.btn-ai:hover:not(:disabled) { background: rgba(139,91,255,0.14); border-color: rgba(139,91,255,0.55); }
.btn-ai svg { flex-shrink: 0; }
.btn-ai.working svg { animation: ai-spin 1.1s linear infinite; }
@keyframes ai-spin { to { transform: rotate(360deg); } }
textarea.ai-busy { opacity: .55; }

/* ---------- Observations ---------- */
.section-hint { font-size: 12.5px; color: var(--muted-2); line-height: 1.5; margin: -6px 0 18px; }
.observation-list { display: flex; flex-direction: column; gap: 16px; }
.observation {
  border: 1px solid var(--border-soft); border-radius: var(--radius-sm);
  background: rgba(255,255,255,0.02); padding: 16px;
}
.observation-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.observation-index {
  font-size: 11.5px; font-weight: 700; letter-spacing: .05em; text-transform: uppercase;
  color: var(--muted); background: rgba(255,255,255,0.05); padding: 4px 10px; border-radius: 999px;
}
.observation-remove:hover { color: var(--red); }

/* Sits after the last observation, so adding another never means scrolling back up. */
.btn-add-block {
  width: 100%; justify-content: center; margin-top: 14px; min-height: 48px;
  background: transparent; border-style: dashed; color: var(--muted);
}
.btn-add-block:hover:not(:disabled) {
  color: #cfe0ff; border-color: rgba(91, 140, 255, 0.5);
  background: var(--accent-soft); transform: none;
}

/* ---------- Photos ---------- */
.photos-block { margin-top: 18px; border-top: 1px solid var(--border-soft); padding-top: 16px; }
.photos-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.photos-title { font-size: 13px; font-weight: 700; color: var(--text); }
.photos-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.photos-hint { font-size: 12px; color: var(--muted-2); margin-top: 10px; }

.photo-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 12px; }
.photo-grid:empty { display: none; }
.photo-thumb {
  position: relative; margin: 0; border-radius: var(--radius-sm); overflow: hidden;
  border: 1px solid var(--border); background: rgba(255,255,255,0.03);
}
.photo-thumb img { display: block; width: 100%; aspect-ratio: 4 / 3; object-fit: cover; background: rgba(0,0,0,0.25); }
.photo-thumb figcaption {
  font-size: 10.5px; color: var(--muted); padding: 6px 8px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.photo-remove {
  position: absolute; top: 6px; right: 6px; width: 26px; height: 26px;
  background: rgba(5,7,16,0.72); -webkit-backdrop-filter: blur(4px); backdrop-filter: blur(4px);
}
.photo-thumb.uploading { opacity: .5; }

/* ---------- Action plan ---------- */
.action-rows { display: flex; flex-direction: column; gap: 14px; }
.action-row {
  display: grid; grid-template-columns: 2.2fr 1fr 1fr auto auto; gap: 14px; align-items: end;
  padding: 16px; border: 1px solid var(--border-soft); border-radius: var(--radius-sm);
  background: rgba(255,255,255,0.02);
}
.action-row textarea.a-action { min-height: 40px; }
.field-status label { margin-bottom: 6px; }
.status-toggle {
  border: 1px solid var(--border); background: var(--amber-soft); color: var(--amber);
  padding: 9px 14px; border-radius: 10px; font-size: 12.5px; font-weight: 700; cursor: pointer; width: 100%;
  transition: all .15s ease;
}
.status-toggle.closed { background: var(--green-soft); color: var(--green); }
.a-remove { align-self: end; margin-bottom: 1px; }

/* ---------- Form action bar (mirrors the topbar actions at the end of the form) ---------- */
.form-actions {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap; justify-content: flex-end;
  margin-top: 4px; padding-top: 18px; border-top: 1px solid var(--border-soft);
}

/* ---------- Toast ---------- */
.toast-root {
  position: fixed;
  bottom: calc(24px + env(safe-area-inset-bottom));
  right: calc(24px + env(safe-area-inset-right));
  left: calc(24px + env(safe-area-inset-left));
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 10px;
}
.toast {
  background: var(--bg-elev); border: 1px solid var(--border); border-radius: 12px;
  padding: 12px 18px; font-size: 13.5px; box-shadow: var(--shadow);
  animation: toast-in .2s ease;
}
.toast.error { border-color: rgba(255,92,114,0.4); color: #ffb9c2; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(5,7,16,0.6); -webkit-backdrop-filter: blur(4px); backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center; z-index: 90; padding: 20px;
}
.modal {
  background: var(--bg-elev); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 26px; max-width: 380px; width: 100%; box-shadow: var(--shadow);
}
.modal h3 { font-size: 16px; margin-bottom: 8px; }
.modal p { color: var(--muted); font-size: 13.5px; line-height: 1.5; margin-bottom: 20px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; }
.recipient-list {
  list-style: none; margin: 0 0 20px; padding: 12px 14px;
  background: rgba(255,255,255,0.04); border: 1px solid var(--border); border-radius: var(--radius-sm);
}
.recipient-list li { font-size: 13.5px; font-weight: 600; color: var(--text); word-break: break-all; }
.recipient-list li + li { margin-top: 6px; }

/* ---------- Responsive ---------- */
.label-short { display: none; }

@media (max-width: 880px) {
  .form-shell { grid-template-columns: 1fr; }
  .toc {
    position: static; flex-direction: row; overflow-x: auto; gap: 6px; padding-bottom: 8px;
    -webkit-overflow-scrolling: touch; scrollbar-width: none;
  }
  .toc::-webkit-scrollbar { display: none; }
  .toc a { white-space: nowrap; border-left: none; border-bottom: 2px solid transparent; padding: 10px 14px; }
  .toc a.active { border-bottom-color: var(--accent); background: none; }
  .grid-2 { grid-template-columns: 1fr; }
  .check-row { grid-template-columns: 1fr; }
  .check-controls { flex-wrap: wrap; }
  .remarks-input { width: 100%; }
  .action-row { grid-template-columns: 1fr 1fr; }
  .action-row .field:first-child { grid-column: 1 / -1; }
  .a-remove { justify-self: end; }

  /* Keep the report actions permanently reachable: the topbar sheds them and a fixed bar
     takes over, so finishing the last section never means scrolling back to the top. */
  .hide-on-mobile { display: none !important; }
  .form-actions {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 45;
    margin: 0; justify-content: stretch; gap: 8px;
    padding: 10px calc(12px + env(safe-area-inset-right)) calc(10px + env(safe-area-inset-bottom)) calc(12px + env(safe-area-inset-left));
    background: rgba(11, 16, 32, 0.92);
    -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px);
    border-top: 1px solid var(--border);
  }
  .form-actions .btn {
    flex: 1; justify-content: center; min-height: 46px; padding: 10px 8px;
    font-size: 13px; gap: 6px;
  }
  /* Room for the fixed bar, so the last field is never trapped underneath it. */
  .form-main { padding-bottom: 78px; }

  /* Comfortable touch targets. */
  .btn { min-height: 42px; }
  .btn-sm { padding: 9px 14px; }
  .icon-btn { width: 38px; height: 38px; }
  .seg { padding: 10px 16px; min-height: 40px; }
  .status-toggle { min-height: 42px; }
  .chip { padding: 9px 16px; }
}

@media (max-width: 560px) {
  .app {
    padding: 18px calc(14px + env(safe-area-inset-right)) calc(40px + env(safe-area-inset-bottom)) calc(14px + env(safe-area-inset-left));
  }
  .hero { gap: 16px; margin-bottom: 22px; }
  .hero h1 { font-size: 26px; }
  .hero p { font-size: 14px; }
  .hero .btn-lg { width: 100%; justify-content: center; }
  .topbar-inner { padding: 10px 14px; gap: 10px; }
  .save-indicator { padding: 6px 4px; }
  .save-indicator .txt { display: none; }
  .panel { padding: 16px 14px; }
  .observation { padding: 13px; }
  .action-row { grid-template-columns: 1fr; }
  .observation-head .ai-actions { width: 100%; }
  .observation-head .ai-actions .btn-ai { flex: 1; justify-content: center; }
  .panel-head .ai-actions { width: 100%; }
  .panel-head .ai-actions .btn { flex: 1; justify-content: center; }
  .photos-actions { width: 100%; }
  .photos-actions .btn { flex: 1; justify-content: center; }
  .photo-grid { grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 10px; }
  .form-actions .label-full { display: none; }
  .form-actions .label-short { display: inline; }
  .cards-grid { grid-template-columns: 1fr; }
  .toolbar { gap: 12px; }
  .search { max-width: none; }
  .modal { padding: 20px; border-radius: var(--radius-sm); }
  .modal-actions .btn { flex: 1; justify-content: center; }
  .toast-root { left: 14px; right: 14px; align-items: stretch; }
}

/* Short viewports (phone in landscape): the fixed bar would eat most of the screen. */
@media (max-width: 880px) and (max-height: 460px) {
  .form-actions { position: static; margin-top: 4px; padding: 16px 0 0; background: none; border-top: 1px solid var(--border-soft); }
  .form-actions .btn { min-height: 40px; }
  .form-main { padding-bottom: 0; }
  .topbar { position: static; }
}
