:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --text: #1d2433;
  --muted: #667085;
  --border: #e3e7ee;
  --primary: #2563eb;
  --primary-ink: #ffffff;
  --danger: #dc2626;
  --danger-bg: #fef3f2;
  --ok: #16a34a;
  --ok-bg: #ecfdf3;
  --ok-border: #abefc6;
  --warn: #b54708;
  --warn-bg: #fffaeb;
  --info-bg: #eff4ff;
  --topbar: #111827;
  --radius: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 24px; line-height: 1.25; margin: 0; }
h2 { font-size: 18px; margin: 0; }
h3 { font-size: 16px; margin: 0; }
p { margin: 6px 0; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.grow { flex: 1; min-width: 0; }
.nowrap { white-space: nowrap; }
[hidden] { display: none !important; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between;
  gap: 8px 16px; padding: 10px 16px;
  background: var(--topbar); color: #fff;
}
.topbar a, .topbar .link { color: #e5e7eb; }
.topbar .brand { color: #fff; font-weight: 700; font-size: 18px; }
.topbar nav { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 18px; font-size: 14px; }
.topbar form { margin: 0; }
.role-tag {
  font-size: 11px; padding: 1px 6px; border-radius: 999px;
  background: rgba(255,255,255,.15); color: #fff;
}
.link { background: none; border: 0; padding: 0; font: inherit; cursor: pointer; }

/* ---------- Layout ---------- */
.container { max-width: 1120px; margin: 0 auto; padding: 16px; }
.page-head {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between;
  gap: 12px; margin: 8px 0 16px;
}
.actions { display: flex; flex-wrap: wrap; gap: 8px; }
.crumbs { font-size: 14px; color: var(--muted); margin-bottom: 4px; }
.section-title { margin: 24px 0 10px; }
.desc { white-space: pre-line; }
.grid {
  display: grid; gap: 12px;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 330px), 1fr));
}

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 8px 14px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text);
  font: inherit; font-weight: 500; cursor: pointer; text-decoration: none;
  white-space: nowrap;
}
.btn:hover { border-color: #c5ccd8; text-decoration: none; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.primary:hover { background: #1d4ed8; }
.btn.danger { color: var(--danger); border-color: #f5c2c0; }
.btn.danger:hover { background: var(--danger-bg); }
.btn.small { min-height: 32px; padding: 4px 10px; font-size: 13px; }
.btn.block { width: 100%; }
.btn[disabled] { opacity: .45; cursor: not-allowed; }
.htmx-request.btn, .htmx-request .btn { opacity: .6; }

/* ---------- Cards ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px;
}
.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.project-card { display: block; color: inherit; transition: box-shadow .15s, border-color .15s; }
.project-card:hover { text-decoration: none; border-color: #c5ccd8; box-shadow: 0 4px 14px rgba(16,24,40,.08); }
.card-foot { display: flex; gap: 8px; margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--border); }
.empty {
  grid-column: 1 / -1; padding: 28px; text-align: center; color: var(--muted);
  background: var(--surface); border: 1px dashed var(--border); border-radius: var(--radius);
}
.error-card { max-width: 480px; margin: 40px auto; text-align: center; }

/* ---------- Badges ---------- */
.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px;
  font-size: 12px; font-weight: 600; white-space: nowrap;
  background: var(--info-bg); color: var(--primary);
}
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.danger { background: var(--danger-bg); color: var(--danger); }
.badge.neutral { background: #f2f4f7; color: #475467; }
.badge.info { background: var(--info-bg); color: var(--primary); }
.badge.purple { background: #f4f3ff; color: #5925dc; }
.badge.faded { opacity: .45; }
.breakdown { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }

/* ---------- Workflow ---------- */
.steps { display: flex; gap: 4px; list-style: none; margin: 10px 0 4px; padding: 0; }
.steps li { flex: 1; height: 5px; border-radius: 999px; background: #e9edf3; }
.steps li.done { background: var(--ok); }
.steps li.current { background: var(--primary); }
.next {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
  margin-top: 10px; padding: 10px; border-radius: 8px;
  background: var(--info-bg); font-size: 14px;
}
.ok-text { color: var(--ok); font-weight: 600; }
.history { margin-top: 10px; font-size: 13px; color: var(--muted); }
.history summary { cursor: pointer; }
.history ul { margin: 6px 0 0; padding-left: 18px; }
.alert.small { font-size: 13px; margin: 8px 0 0; padding: 8px 10px; }
.alert.warn { background: var(--warn-bg); color: var(--warn); }
.team { margin-bottom: 16px; }
.team .row .field { margin-bottom: 4px; }
.team .htmx-request { opacity: .7; }
.team form { margin: 0; }
.viewers { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--border); }
.viewers h3 { margin-bottom: 2px; }
.viewer-list { list-style: none; margin: 8px 0; padding: 0; display: grid; gap: 6px; }
.viewer-list li { display: flex; align-items: center; gap: 8px; padding: 6px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2); }
.add-viewer { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.add-viewer select { flex: 1 1 200px; width: auto; }
.badge.role-viewer { background: #f2f4f7; color: #475467; }
.badge.role-admin { background: #f4f3ff; color: #5925dc; }
.badge.role-manager { background: var(--info-bg); color: var(--primary); }
.badge.role-owner { background: var(--ok-bg); color: var(--ok); }
.badge.role-contractor { background: var(--warn-bg); color: var(--warn); }

/* ---------- Stats ---------- */
.stats {
  display: grid; gap: 10px; margin-bottom: 12px;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
}
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; }
.stat-label { display: block; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.stat-value { display: block; font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat.warn .stat-value { color: var(--danger); }
.progress { height: 8px; background: #e9edf3; border-radius: 999px; overflow: hidden; margin: 10px 0; }
.progress > div { height: 100%; background: var(--ok); transition: width .3s; }
.progress-row { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 12px; margin-bottom: 8px; font-size: 13px; }
.progress-row .progress { flex: 1 1 200px; }
#project-list > .stats { margin-top: 12px; }

/* ---------- Key/value lists ---------- */
.kv { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px 12px; margin: 12px 0; }
.kv dt { font-size: 12px; color: var(--muted); }
.kv dd { margin: 0; font-weight: 500; overflow-wrap: anywhere; }
.people { display: flex; flex-wrap: wrap; gap: 4px 16px; font-size: 13px; color: var(--muted); margin: 8px 0 14px; }
.people b { color: var(--text); font-weight: 600; }

/* ---------- Task card: drawings and approvals ---------- */
.docs, .approvals { display: grid; gap: 8px; margin-top: 10px; }
.doc, .approval {
  display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px;
  padding: 8px 10px; border-radius: 8px;
  background: var(--surface-2); border: 1px solid var(--border);
}
.doc-label { display: block; width: 100%; font-size: 12px; color: var(--muted); }
.approval .doc-label { width: auto; }
.doc > a { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.approval.ok { background: var(--ok-bg); border-color: var(--ok-border); }
.approval.ok strong { color: var(--ok); }
.upload { margin: 0; display: flex; align-items: center; gap: 6px; }
.upload label.btn { cursor: pointer; }
.uploading { display: none; font-size: 12px; color: var(--muted); }
.upload.htmx-request .uploading { display: inline; }

/* ---------- Forms ---------- */
.form { max-width: 680px; }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 14px; min-width: 0; }
.field label { font-weight: 500; font-size: 14px; }
input, select, textarea {
  width: 100%; padding: 10px 12px;
  font: inherit; font-size: 16px; /* 16px stops iOS zooming into fields */
  color: var(--text); background: #fff;
  border: 1px solid var(--border); border-radius: 8px;
}
input:focus, select:focus, textarea:focus { outline: 2px solid #bfd3fe; border-color: var(--primary); }
input:disabled { background: var(--surface-2); color: var(--muted); }
input.computed { background: var(--surface-2); font-weight: 700; font-variant-numeric: tabular-nums; }
input[type="file"] { padding: 8px; font-size: 14px; }
.field-narrow { max-width: 220px; }
.row { display: grid; gap: 0 12px; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
.search { margin: 0; }
.search input { max-width: 380px; }
.alert { padding: 10px 12px; border-radius: 8px; margin-bottom: 14px; font-size: 14px; }
.alert.error { background: var(--danger-bg); color: #b42318; }
.alert.success { background: var(--ok-bg); color: #067647; }
.role-help { margin: 0 0 16px; font-size: 14px; color: var(--muted); }
.role-help summary { cursor: pointer; color: var(--primary); }
.role-help ul { padding-left: 20px; margin: 8px 0 0; }

.login-wrap { min-height: calc(100vh - 32px); display: grid; place-items: center; }
.login-card { width: 100%; max-width: 380px; }
.login-card h1 { margin-bottom: 0; }
.login-card .muted { margin-bottom: 18px; }

/* ---------- Tables ---------- */
.table-wrap {
  overflow-x: auto; -webkit-overflow-scrolling: touch;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
thead th { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); background: var(--surface-2); white-space: nowrap; }
tfoot th { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }
.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.row-overdue td { background: #fffbfa; }
.row-actions { white-space: nowrap; text-align: right; }
.row-actions .btn + .btn { margin-left: 6px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 50;
  max-width: calc(100% - 32px); padding: 10px 16px; border-radius: 8px;
  background: #1f2937; color: #fff; font-size: 14px;
  box-shadow: 0 8px 24px rgba(0,0,0,.2);
}

/* ---------- Phones ---------- */
@media (max-width: 640px) {
  h1 { font-size: 20px; }
  .container { padding: 12px 16px; }
  .page-head .actions { width: 100%; }
  .page-head .actions .btn { flex: 1; }
  .search input { max-width: none; }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .stat-value { font-size: 18px; }

  /* Tables marked .stack become one card per row. */
  table.stack thead { display: none; }
  table.stack tr { display: block; padding: 8px 0; border-bottom: 1px solid var(--border); }
  table.stack tbody tr:last-child { border-bottom: 0; }
  table.stack td { display: flex; justify-content: space-between; gap: 12px; padding: 5px 12px; border: 0; text-align: right; }
  table.stack td::before { content: attr(data-label); font-weight: 600; color: var(--muted); text-align: left; }
  table.stack td.row-actions { justify-content: flex-end; padding-top: 8px; }
}

/* ---------- Print (report page) ---------- */
@media print {
  .topbar, .no-print, .toast { display: none !important; }
  body { background: #fff; font-size: 12px; }
  .container { max-width: none; padding: 0; }
  .stat, .table-wrap { border-color: #ccc; }
  .table-wrap { overflow: visible; }
  th, td { padding: 6px 8px; }
  .report-stats { grid-template-columns: repeat(6, 1fr); }
}

/* ---------- Task list ---------- */
.task-filters { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin: 0 0 12px; }
.task-filters select { width: auto; flex: 0 1 280px; }
.chip {
  display: inline-flex; align-items: center; gap: 8px; min-height: 40px; padding: 6px 12px;
  border: 1px solid var(--border); border-radius: 999px; background: var(--surface);
  font-size: 14px; font-weight: 500; cursor: pointer; user-select: none;
}
.chip input { width: auto; margin: 0; }
.chip.on { border-color: var(--primary); background: var(--info-bg); color: var(--primary); }
.chip-count { min-width: 22px; padding: 0 6px; border-radius: 999px; background: var(--primary); color: #fff; font-size: 12px; text-align: center; }

/* Task table on the project page. */
.task-table td { vertical-align: middle; }
.task-table td:first-child a { font-weight: 600; color: var(--text); }
.task-table td:first-child a:hover { color: var(--primary); }
.task-table td .badge { white-space: normal; }
.task-table tbody tr:hover td { background: var(--surface-2); }
.task-table tr.needs-action td:first-child { box-shadow: inset 4px 0 0 var(--primary); }
.pager { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 10px; margin: 14px 0 0; }
.pager span.btn[aria-disabled] { opacity: .4; cursor: default; }

/* Task page */
.task-detail .steps { max-width: 360px; }
.next .next-label { font-weight: 600; margin-right: 4px; }
.next-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; flex: 1; }
.next.needs-action { background: #e8efff; box-shadow: inset 4px 0 0 var(--primary); }
.kv-wide { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); margin-top: 16px; }
ul.history { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; font-size: 14px; }
ul.history li { padding-left: 14px; position: relative; }
ul.history li::before { content: ""; position: absolute; left: 0; top: .55em; width: 6px; height: 6px; border-radius: 50%; background: var(--ok); }

@media (max-width: 640px) {
  .task-filters select { flex: 1 1 100%; }
  .task-table tr.needs-action td:first-child { box-shadow: none; }
  .task-table tr.needs-action { box-shadow: inset 4px 0 0 var(--primary); }
}
