:root {
  --ink: #17181a;
  --ink-2: #4a4f57;
  --ink-3: #7a828c;
  --line: #dfe3e8;
  --bg: #f4f5f7;
  --surface: #ffffff;
  --accent: #17181a;
  --focus: #2f6fed;
  --urgent: #c8371f;
  --ok: #1f7a44;
  --radius: 14px;
  --pad: 18px;
  --bar-h: 68px;
  font-synthesis-weight: none;
}

* { box-sizing: border-box; }

/* A class that sets `display` beats the UA rule for [hidden], which silently
   leaves elements like the job summary on screen as empty boxes. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}

.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 14px;
  padding: 10px max(16px, env(safe-area-inset-left)) 10px max(16px, env(safe-area-inset-right));
  padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.topbar-logo { height: 92px; width: auto; }
.topbar-title { display: flex; align-items: baseline; gap: 10px; flex: 1; min-width: 0; }
.topbar-title strong { font-size: 17px; letter-spacing: -0.01em; }
.ref-badge {
  font-size: 12px; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase;
  color: var(--ink-3); border: 1px solid var(--line); border-radius: 999px; padding: 2px 9px;
  white-space: nowrap;
}
.ref-badge.saved { color: var(--ok); border-color: #bfe3cd; background: #f1faf4; }
.topbar-right { display: flex; align-items: center; gap: 10px; }
.user-chip { font-size: 13px; color: var(--ink-3); max-width: 42vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-chip.demo {
  font-weight: 700; letter-spacing: .08em; color: #8a5a00;
  background: #fff4d6; border: 1px solid #e8c877; border-radius: 999px; padding: 3px 11px;
}
.status.warn { color: #8a5a00; }

/* ---------- layout ---------- */
.sheet {
  max-width: 880px;
  margin: 0 auto;
  padding: 18px max(14px, env(safe-area-inset-left)) 0 max(14px, env(safe-area-inset-right));
  display: flex; flex-direction: column; gap: 14px;
}
.spacer { height: calc(var(--bar-h) + 24px + env(safe-area-inset-bottom)); }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: var(--pad);
}
.card h2 {
  margin: 0 0 14px;
  font-size: 12px; font-weight: 700; letter-spacing: 0.1em; text-transform: uppercase;
  color: var(--ink-3);
}
.card h2 em { font-style: normal; font-weight: 500; text-transform: none; letter-spacing: 0; }
.card.conditional { border-left: 3px solid var(--ink); }

.note {
  margin: -4px 0 14px; padding: 10px 12px;
  background: #fbf7ee; border: 1px solid #ecdfc4; border-radius: 10px;
  font-size: 13px; color: #6b5a34;
}

/* ---------- fields ---------- */
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 14px; }
.span2 { grid-column: 1 / -1; }
@media (max-width: 560px) { .grid { grid-template-columns: 1fr; } .span2 { grid-column: auto; } }

.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > span { font-size: 13px; font-weight: 600; color: var(--ink-2); }
.field > span em { font-style: normal; font-weight: 400; color: var(--ink-3); }

input, select, textarea {
  font: inherit; color: var(--ink); background: var(--surface);
  border: 1px solid var(--line); border-radius: 10px;
  padding: 11px 12px; min-height: 46px; width: 100%;
  appearance: none; -webkit-appearance: none;
}
textarea { min-height: unset; resize: vertical; line-height: 1.5; }
select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8'%3E%3Cpath d='M1 1l5 5 5-5' fill='none' stroke='%237a828c' stroke-width='1.8' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 14px center; padding-right: 38px;
}
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--focus); box-shadow: 0 0 0 3px rgba(47,111,237,.14);
}
input.invalid, select.invalid { border-color: var(--urgent); box-shadow: 0 0 0 3px rgba(200,55,31,.12); }

.job-summary {
  margin-top: 12px; padding: 12px 14px;
  background: var(--bg); border-radius: 10px;
  font-size: 14px; color: var(--ink-2);
  display: flex; flex-wrap: wrap; gap: 4px 22px;
}
.job-summary b { color: var(--ink); font-weight: 600; }

/* ---------- purpose tiles ---------- */
.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
@media (max-width: 640px) { .tiles { grid-template-columns: repeat(2, 1fr); } }
.tile {
  border: 1px solid var(--line); background: var(--surface); border-radius: 12px;
  padding: 14px 12px; min-height: 62px;
  font: inherit; font-weight: 600; color: var(--ink-2); text-align: left;
  cursor: pointer; transition: border-color .12s, background .12s, color .12s;
}
.tile:hover { border-color: var(--ink-3); }
.tiles.invalid { outline: 2px solid var(--urgent); outline-offset: 6px; border-radius: 4px; }
.tile[aria-pressed="true"] {
  background: var(--ink); border-color: var(--ink); color: #fff;
}

/* ---------- checkboxes ---------- */
.checks { display: flex; flex-wrap: wrap; gap: 10px; }
.check {
  display: flex; align-items: center; gap: 10px;
  border: 1px solid var(--line); border-radius: 12px; padding: 12px 16px 12px 13px;
  cursor: pointer; font-weight: 600; color: var(--ink-2); flex: 1 1 150px;
}
/* The global `appearance: none` above strips checkboxes back to empty circles
   with no tick, so they must opt back into the native control. */
.check input[type="checkbox"] {
  appearance: auto; -webkit-appearance: checkbox;
  width: 22px; height: 22px; min-height: 0; flex: none;
  border: none; padding: 0; border-radius: 0;
  accent-color: var(--ink);
}
.check:has(input:checked) { border-color: var(--ink); color: var(--ink); background: #fafafa; }
.check-urgent input { accent-color: var(--urgent); }
.check-urgent:has(input:checked) { border-color: var(--urgent); color: var(--urgent); background: #fdf4f2; }

/* ---------- variation lines ---------- */
.lines { width: 100%; border-collapse: collapse; }
.lines th {
  text-align: left; font-size: 12px; letter-spacing: .06em; text-transform: uppercase;
  color: var(--ink-3); font-weight: 700; padding-bottom: 8px;
}
.lines th.amt { text-align: right; width: 140px; }
.lines td { padding: 0 0 10px; vertical-align: top; }
.lines td + td { padding-left: 10px; }
.lines td.amt input { text-align: right; }
.lines td.rm { width: 46px; padding-left: 6px; }
.btn-rm {
  border: 1px solid var(--line); background: var(--surface); border-radius: 10px;
  width: 46px; height: 46px; font-size: 20px; line-height: 1; color: var(--ink-3); cursor: pointer;
}
.btn-rm:hover { border-color: var(--urgent); color: var(--urgent); }

.totals {
  display: grid; grid-template-columns: 1fr auto; gap: 6px 24px;
  margin: 18px 0 0; padding-top: 14px; border-top: 1px solid var(--line);
  font-size: 15px;
}
.totals dt { color: var(--ink-2); }
.totals dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.totals .grand { font-weight: 700; font-size: 18px; color: var(--ink); }

/* ---------- photos ---------- */
.photo-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.photo-btn { display: inline-flex; align-items: center; margin: 0; }
.photo-note { font-size: 13px; color: var(--ink-3); }
.photo-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 10px; margin-top: 14px;
}
.photo-grid:empty { margin-top: 0; }
.photo {
  position: relative; border: 1px solid var(--line); border-radius: 10px; overflow: hidden;
  background: var(--bg); aspect-ratio: 4 / 3;
}
.photo img { width: 100%; height: 100%; object-fit: cover; display: block; }
.photo button {
  position: absolute; top: 5px; right: 5px; width: 26px; height: 26px;
  border: none; border-radius: 50%; cursor: pointer;
  background: rgba(23,24,26,.72); color: #fff; font-size: 15px; line-height: 1;
}
.photo span {
  position: absolute; left: 0; right: 0; bottom: 0; padding: 3px 6px;
  background: rgba(23,24,26,.62); color: #fff; font-size: 11px;
}

/* ---------- sign-off ---------- */
.signoff { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
@media (max-width: 640px) { .signoff { grid-template-columns: 1fr; } }
.signer h3 {
  margin: 0 0 12px; font-size: 12px; font-weight: 700;
  letter-spacing: .08em; text-transform: uppercase; color: var(--ink);
}
.signer .field { margin-bottom: 12px; }
.sigpad { border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: var(--surface); }
.sigpad canvas { display: block; width: 100%; height: 130px; touch-action: none; cursor: crosshair; }
.sigpad-bar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 8px 6px 12px; border-top: 1px dashed var(--line);
  font-size: 12px; color: var(--ink-3);
}

/* ---------- buttons + action bar ---------- */
.btn {
  font: inherit; font-weight: 600; border-radius: 11px; padding: 12px 20px;
  min-height: 46px; cursor: pointer; border: 1px solid transparent; white-space: nowrap;
}
.btn-primary { background: var(--ink); color: #fff; }
.btn-primary:hover { background: #000; }
.btn-ghost { background: var(--surface); border-color: var(--line); color: var(--ink); }
.btn-ghost:hover { border-color: var(--ink-3); }
.btn-sm { padding: 8px 14px; min-height: 40px; font-size: 14px; }
.btn[disabled] { opacity: .5; cursor: default; }

.actionbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px;
  height: var(--bar-h);
  padding: 0 max(16px, env(safe-area-inset-left)) 0 max(16px, env(safe-area-inset-right));
  padding-bottom: env(safe-area-inset-bottom);
  background: rgba(255,255,255,.92); backdrop-filter: blur(10px);
  border-top: 1px solid var(--line);
}
.status { flex: 1; font-size: 14px; color: var(--ink-3); min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status.err { color: var(--urgent); }
.status.ok { color: var(--ok); }

/* ---------- preview overlay ---------- */
.overlay { position: fixed; inset: 0; z-index: 40; background: #6b7079; overflow: auto; }
.overlay-bar {
  position: sticky; top: 0; z-index: 2;
  display: flex; gap: 12px; justify-content: flex-end;
  padding: 12px 16px; padding-top: calc(12px + env(safe-area-inset-top));
  background: rgba(23,24,26,.94);
}
.overlay-bar .btn-ghost { background: transparent; color: #fff; border-color: #4a4f57; }
.overlay-bar .btn-primary { background: #fff; color: var(--ink); }
.overlay-body { padding: 22px 14px 60px; }

/* ---------- the document ---------- */
.printdoc {
  width: 100%; max-width: 210mm; margin: 0 auto; padding: 16mm 15mm;
  background: #fff; color: #000; box-shadow: 0 10px 34px rgba(0,0,0,.28);
  font-size: 12px; line-height: 1.5;
}
.printdoc .doc-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 20px; }
.printdoc .doc-head img { height: 76px; }
.printdoc .doc-co { text-align: right; font-size: 10.5px; color: #333; line-height: 1.5; }
.printdoc h1 {
  margin: 18px 0 0; padding-bottom: 8px; border-bottom: 2px solid #000;
  font-size: 19px; letter-spacing: .02em; text-transform: uppercase;
}
.printdoc .doc-meta {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 0 26px;
  margin: 14px 0 0;
}
.printdoc .doc-meta div {
  display: flex; gap: 8px; padding: 5px 0; border-bottom: 1px solid #e2e2e2;
}
.printdoc .doc-meta dt { flex: 0 0 92px; color: #555; }
.printdoc .doc-meta dd { margin: 0; font-weight: 600; }
.printdoc .doc-for { margin: 16px 0 0; display: flex; gap: 20px; align-items: center; flex-wrap: wrap; }
.printdoc .doc-for b { font-size: 10.5px; letter-spacing: .08em; text-transform: uppercase; color: #555; }
.printdoc .tick { display: inline-flex; align-items: center; gap: 7px; }
.printdoc .tick i {
  width: 13px; height: 13px; border: 1.4px solid #000; display: inline-block;
  font-style: normal; line-height: 11px; text-align: center; font-size: 12px;
}
.printdoc .tick.on i::after { content: "\2713"; }
.printdoc .tick.on { font-weight: 700; }
.printdoc h2 {
  margin: 20px 0 6px; font-size: 10.5px; letter-spacing: .1em;
  text-transform: uppercase; color: #555;
}
.printdoc .body { white-space: pre-wrap; min-height: 26mm; }
.printdoc table.doc-lines { width: 100%; border-collapse: collapse; margin-top: 4px; }
.printdoc table.doc-lines th, .printdoc table.doc-lines td {
  border-bottom: 1px solid #ddd; padding: 6px 4px; text-align: left;
}
.printdoc table.doc-lines th { border-bottom: 1.5px solid #000; font-size: 10.5px; text-transform: uppercase; letter-spacing: .05em; }
.printdoc table.doc-lines .amt { text-align: right; width: 110px; font-variant-numeric: tabular-nums; }
.printdoc table.doc-lines tr.total td { font-weight: 700; border-bottom: none; }
.printdoc .condition { margin-top: 10px; font-style: italic; font-size: 11px; }
.printdoc .doc-sign { display: grid; grid-template-columns: 1fr 1fr; gap: 26px; margin-top: 24px; }
.printdoc .doc-sign .box { }
.printdoc .doc-sign img { display: block; height: 58px; max-width: 100%; object-fit: contain; object-position: left bottom; }
.printdoc .doc-sign .rule { border-bottom: 1px solid #000; height: 58px; }
.printdoc .doc-sign .cap { display: flex; justify-content: space-between; gap: 10px; padding-top: 5px; font-size: 11px; }
.printdoc .doc-foot {
  margin-top: 26px; padding-top: 8px; border-top: 1px solid #ddd;
  font-size: 9.5px; color: #777; display: flex; justify-content: space-between;
}

/* ---------- print ---------- */
@media print {
  @page { size: A4; margin: 0; }
  html, body { background: #fff; }
  .no-print, .topbar, .actionbar, .overlay-bar { display: none !important; }
  .sheet { display: none !important; }
  .overlay { position: static; background: #fff; overflow: visible; }
  .overlay-body { padding: 0; }
  .printdoc { box-shadow: none; max-width: none; padding: 14mm 15mm; }
  .printdoc .doc-sign, .printdoc table.doc-lines { break-inside: avoid; }
}
