:root {
    --maroon: #800000;
    --maroon-dark: #5c0000;
    --maroon-pale: #f7eded;
    --border: #d9d2d2;
    --text: #22201f;
    --muted: #7a716e;
    --red: #b00020;
    --amber: #a05a00;
    --green: #1e6b34;
  }
  * { box-sizing: border-box; }
  html, body { margin: 0; height: 100%; }
  body {
    font-family: "Segoe UI", Arial, sans-serif;
    font-size: 13px;
    color: var(--text);
    background: #fff;
  }
  #app { display: flex; height: 100vh; }

  /* Sidebar */
  #sidebar {
    width: 200px; flex: none; background: var(--maroon); color: #fff;
    display: flex; flex-direction: column;
  }
  .brand {
    padding: 14px 16px 12px; font-size: 18px; font-weight: 700; letter-spacing: 2px;
    border-bottom: 1px solid rgba(255,255,255,.25);
  }
  .brand span { display: block; font-size: 10px; font-weight: 400; letter-spacing: .5px; opacity: .85; margin-top: 2px; }
  .brand span.sub { opacity: .65; }
  /* The nav scrolls on its own (brand stays pinned) so a long list never clips on a short screen. */
  #nav { display: flex; flex-direction: column; padding: 6px 0 10px; flex: 1; min-height: 0; overflow-y: auto; }
  .nav-group { display: flex; flex-direction: column; }
  .nav-group + .nav-group { margin-top: 6px; }
  .nav-head {
    padding: 8px 16px 3px; font-size: 10px; font-weight: 700; letter-spacing: 1.2px;
    text-transform: uppercase; color: rgba(255,255,255,.55); user-select: none;
  }
  .nav-item {
    padding: 7px 16px; color: #f2e6e6; cursor: pointer; font-size: 13px;
    border-left: 3px solid transparent; user-select: none;
  }
  .nav-item:hover { background: var(--maroon-dark); color: #fff; }
  .nav-item.active { background: var(--maroon-dark); color: #fff; border-left-color: #fff; font-weight: 600; }

  /* Main area */
  #main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
  #topbar {
    display: flex; align-items: center; gap: 12px;
    padding: 8px 16px; border-bottom: 2px solid var(--maroon); background: #fff;
  }
  #view-title { font-size: 16px; font-weight: 700; color: var(--maroon); }
  .spacer { flex: 1; }
  .proj-label { font-size: 12px; color: var(--muted); }
  #contract-select { margin-left: 6px; min-width: 260px; }
  #view { flex: 1; overflow: auto; padding: 12px 16px; }
  .loading { color: var(--muted); padding: 20px; }

  /* Controls */
  select, input, textarea, button {
    font-family: inherit; font-size: 13px; color: var(--text);
    border: 1px solid var(--border); border-radius: 3px; padding: 4px 6px; background: #fff;
  }
  textarea { resize: vertical; }
  .btn { cursor: pointer; padding: 5px 12px; background: #fff; }
  .btn:hover { background: var(--maroon-pale); }
  .btn.primary { background: var(--maroon); color: #fff; border-color: var(--maroon); font-weight: 600; }
  .btn.primary:hover { background: var(--maroon-dark); }
  .btn.danger { color: var(--red); border-color: var(--red); }
  .btn.danger:hover { background: #fbeaec; }

  .toolbar { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; flex-wrap: wrap; }
  .toolbar .count { color: var(--muted); font-size: 12px; }
  .form-notice { grid-column: 1 / -1; border-left: 4px solid var(--maroon); background: #f7eded; padding: 8px 12px; font-size: 12.5px; }
  .form-notice b { color: var(--maroon); }
  .note-line { margin: 0 0 8px; font-size: 12px; color: var(--muted); }
  .note-line b { color: var(--text); }
  .audit-line { font-size: 11px; margin-top: 8px; }
  .qfilter { min-width: 150px; }
  .date-range { font-size: 11px; color: var(--muted); display: inline-flex; align-items: center; gap: 2px; }
  .date-range input { padding: 2px 4px; }
  tr.deleted-row { color: var(--muted); }
  tr.archived-row { color: var(--muted); font-style: italic; background: #faf7f7; }

  /* Tables */
  table.grid { border-collapse: collapse; width: 100%; }
  table.grid th, table.grid td {
    border: 1px solid var(--border); padding: 3px 8px; text-align: left; vertical-align: top;
  }
  table.grid th {
    background: var(--maroon); color: #fff; font-weight: 600; cursor: pointer;
    white-space: nowrap; user-select: none; position: sticky; top: -12px;
  }
  table.grid th .arrow { font-size: 10px; margin-left: 3px; }
  table.grid tbody tr:nth-child(even) { background: #faf7f7; }
  table.grid tbody tr:hover { background: var(--maroon-pale); cursor: pointer; }
  td.num { text-align: right; white-space: nowrap; }
  .empty { color: var(--muted); padding: 16px 4px; }

  .badge {
    display: inline-block; font-size: 10px; font-weight: 700; padding: 1px 5px;
    border-radius: 3px; margin-left: 5px; white-space: nowrap;
  }
  .badge.red { background: var(--red); color: #fff; }
  .badge.amber { background: #f5c26b; color: #5a3c00; }
  .badge.outline { border: 1px solid var(--red); color: var(--red); }

  /* Sub-tabs (Site Supervision) */
  .subtabs { display: flex; flex-wrap: wrap; gap: 2px; margin-bottom: 10px; border-bottom: 2px solid var(--maroon); }
  .subtab {
    padding: 6px 16px; cursor: pointer; border: 1px solid var(--border); border-bottom: none;
    border-radius: 4px 4px 0 0; background: #f4f0f0; user-select: none;
  }
  .subtab.active { background: var(--maroon); color: #fff; border-color: var(--maroon); font-weight: 600; }

  /* Dashboard */
  .dash-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 12px; }
  .card { border: 1px solid var(--border); border-radius: 4px; padding: 10px 12px; background: #fff; }
  .card h3 {
    margin: 0 0 8px; font-size: 13px; color: var(--maroon); text-transform: uppercase;
    letter-spacing: .5px; border-bottom: 1px solid var(--border); padding-bottom: 4px;
  }
  .kv { display: grid; grid-template-columns: max-content 1fr; gap: 2px 14px; font-size: 12.5px; }
  .kv dt { color: var(--muted); }
  .kv dd { margin: 0; }
  .stat-row { display: flex; gap: 10px; flex-wrap: wrap; }
  .stat { flex: 1; min-width: 90px; text-align: center; padding: 8px 4px; background: var(--maroon-pale); border-radius: 4px; }
  .stat .n { font-size: 22px; font-weight: 700; color: var(--maroon); }
  .stat .l { font-size: 11px; color: var(--muted); }
  .progress { background: #eee; border-radius: 4px; height: 16px; overflow: hidden; margin: 6px 0 4px; }
  .progress .fill { height: 100%; background: var(--maroon); }
  .progress .fill.over { background: var(--red); }
  .proj-card { cursor: pointer; }
  .proj-card:hover { border-color: var(--maroon); box-shadow: 0 1px 4px rgba(128,0,0,.2); }
  .dash-table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
  .dash-table td, .dash-table th { border-bottom: 1px solid var(--border); padding: 3px 6px; text-align: left; }
  .dash-table th { color: var(--muted); font-weight: 600; font-size: 11px; text-transform: uppercase; }
  .overdue-text { color: var(--red); font-weight: 700; }

  /* Modal */
  #modal-backdrop {
    position: fixed; inset: 0; background: rgba(0,0,0,.4);
    display: flex; align-items: flex-start; justify-content: center; z-index: 100; overflow: auto; padding: 30px 0;
  }
  #modal { background: #fff; border-radius: 5px; width: 680px; max-width: 94vw; box-shadow: 0 4px 24px rgba(0,0,0,.35); }
  #modal-title {
    background: var(--maroon); color: #fff; font-weight: 700; padding: 10px 16px; border-radius: 5px 5px 0 0;
  }
  #modal-body { padding: 14px 16px; max-height: 65vh; overflow: auto; }
  .form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 16px; }
  /* min-width:0 lets a column shrink below its content; without it a <select> holding a long
   * option (a contract or front name) forces its column wide and pushes the form sideways. */
  .form-field { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
  .form-field input, .form-field select, .form-field textarea { width: 100%; max-width: 100%; box-sizing: border-box; }
  .form-field.wide { grid-column: 1 / -1; }
  .form-group-head {
    grid-column: 1 / -1; margin-top: 9px; padding-top: 7px; border-top: 1px solid var(--border);
    font-size: 10.5px; font-weight: 700; letter-spacing: .9px; text-transform: uppercase; color: var(--maroon);
  }
  .form-grid > .form-group-head:first-child { margin-top: 0; padding-top: 0; border-top: none; }
  .form-field label { font-size: 11.5px; color: var(--muted); font-weight: 600; }
  .form-field label .req { color: var(--red); }
  .form-field .ro-value { padding: 4px 2px; color: var(--text); background: #f6f4f4; border-radius: 3px; padding-left: 6px; min-height: 24px; }
  .form-field .hint { font-size: 11px; color: var(--muted); }
  #modal-error {
    margin: 0 16px; padding: 8px 10px; background: #fbeaec; color: var(--red);
    border: 1px solid var(--red); border-radius: 3px; font-size: 12.5px;
  }
  #modal-footer { display: flex; gap: 8px; padding: 12px 16px; justify-content: flex-end; }
  #modal-footer .btn.danger { margin-right: auto; }

  /* Toast */
  #toast {
    position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
    background: var(--text); color: #fff; padding: 8px 18px; border-radius: 4px; z-index: 200; font-size: 13px;
  }
  #toast.error { background: var(--red); }
  .hidden { display: none !important; }

  /* Settings (Admin) — the form-grid classes above are reused for the fields */
  .set-wrap { max-width: 920px; }
  .set-wrap .form-grid { margin-bottom: 14px; }
  .set-key { font-size: 10px; color: #b3a7a7; font-family: ui-monospace, Consolas, monospace; }
  .set-card { margin-bottom: 12px; }
  .set-card code { font-size: 11.5px; background: #f2eeee; padding: 0 3px; border-radius: 2px; }
  .set-card .hint { font-size: 11.5px; color: var(--muted); margin: 8px 0 0; }
  .prose p { font-size: 12.5px; line-height: 1.5; margin: 0 0 8px; }
  .prose .kv { gap: 6px 14px; align-items: start; }
  .prose .kv dt { font-weight: 600; color: var(--maroon); }
  .prose ul { margin: 6px 0 0; padding-left: 18px; }
  .prose li { margin-bottom: 3px; }
  .form-notice.privacy { border-left-color: #6b6b6b; background: #f4f4f4; }
  .set-ok, .set-note, .set-bad { font-size: 12.5px; padding: 7px 10px; border-radius: 3px; margin-top: 8px; }
  .set-ok { background: #eef5ee; border-left: 4px solid #4a7a4a; }
  .set-note { background: #fdf6e7; border-left: 4px solid #c99a2e; }
  .set-bad { background: #fbeaec; border-left: 4px solid var(--red); }
  #set-result:not(:empty) { margin-bottom: 12px; }
  .btn:disabled { cursor: default; opacity: .5; }
  .btn.primary:disabled:hover { background: var(--maroon); }

  /* Sign-in gate */
  #gate { position: fixed; inset: 0; background: var(--maroon); display: flex; align-items: center; justify-content: center; z-index: 300; }
  .gate-box { background: #fff; border-radius: 6px; padding: 26px 28px; width: 320px; display: flex; flex-direction: column; gap: 10px; box-shadow: 0 6px 30px rgba(0,0,0,.4); }
  .gate-brand { font-size: 22px; font-weight: 700; letter-spacing: 2px; color: var(--maroon); margin-bottom: 6px; }
  .gate-brand span { display: block; font-size: 11px; font-weight: 400; letter-spacing: .5px; color: var(--muted); }
  .gate-label { font-size: 12px; color: var(--muted); font-weight: 600; }
  #gate input { padding: 8px; }
  .gate-hint { font-size: 11.5px; color: var(--muted); }
  .gate-privacy {
    font-size: 11px; line-height: 1.45; color: var(--muted);
    border-top: 1px solid var(--border); padding-top: 8px; margin-top: 2px;
  }
  .gate-privacy b { color: var(--text); }
  .gate-msg { font-size: 12.5px; color: var(--red); }
  #userbar { display: flex; align-items: center; gap: 8px; margin-left: 12px; }
  #userbar .who { font-size: 12px; color: var(--maroon); font-weight: 600; }

  /* Site photos & signature */
  .site-actions { display: flex; align-items: center; gap: 8px; margin-top: 6px; flex-wrap: wrap; }
  .photo-strip { display: flex; flex-wrap: wrap; gap: 8px; padding: 6px; border: 1px solid var(--border); border-radius: 4px; min-height: 40px; background: #faf7f7; }
  .photo-chip { position: relative; width: 96px; text-align: center; font-size: 11px; }
  .photo-chip img { width: 96px; height: 72px; object-fit: cover; border: 1px solid var(--border); border-radius: 3px; display: block; }
  .photo-chip .ph { width: 96px; height: 72px; background: #eee; color: var(--muted); display: flex; align-items: center; justify-content: center; border-radius: 3px; }
  .photo-chip a { color: var(--maroon); text-decoration: none; }
  .photo-chip b { position: absolute; top: -6px; right: -6px; background: var(--red); color: #fff; width: 18px; height: 18px;
    border-radius: 50%; font-size: 12px; line-height: 18px; cursor: pointer; }
  .sig-pad { border: 1px dashed var(--maroon); border-radius: 4px; background: #fff; width: 100%; max-width: 600px; touch-action: none; }
  .sig-done { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
  .sig-done img { height: 70px; border: 1px solid var(--border); border-radius: 3px; background: #fff; }

  /* Daily report */
  .report-photos { display: flex; flex-wrap: wrap; gap: 12px; }
  .report-photos figure { margin: 0; width: 240px; }
  .report-photos img { width: 240px; height: 180px; object-fit: cover; border: 1px solid var(--border); border-radius: 3px; }
  .report-photos figcaption { font-size: 11px; color: var(--muted); text-align: center; }
  .report-photos a { color: var(--maroon); }
  .sig-block { display: flex; align-items: flex-end; gap: 16px; margin-top: 8px; }
  .sig-block img { height: 80px; border-bottom: 1px solid var(--text); }
  .sig-line { width: 220px; border-bottom: 1px solid var(--text); height: 40px; }

  /* Mobile / tablet — site staff work on phones */
  @media (max-width: 760px) {
    #app { flex-direction: column; height: auto; min-height: 100vh; }
    #sidebar { width: 100%; flex-direction: row; flex-wrap: wrap; align-items: center; }
    .brand { padding: 8px 12px; font-size: 15px; border-bottom: none; }
    .brand span { display: none; }
    #nav { flex-direction: row; flex-wrap: wrap; padding: 0 0 6px 6px; gap: 2px; overflow: visible; flex: 1 1 100%; }
    .nav-group { display: contents; }                    /* groups flatten into one row of chips */
    .nav-head { display: none; }
    .nav-item { padding: 6px 10px; border-left: none; border-radius: 3px; font-size: 12px; }
    .nav-item.active { border-left: none; background: #fff; color: var(--maroon); }
    #topbar { flex-wrap: wrap; gap: 6px; }
    #contract-select { min-width: 150px; }
    #view { padding: 10px; }
    .form-grid { grid-template-columns: 1fr; }         /* one field per row — easier on a phone */
    #modal { width: 100%; max-width: 100%; border-radius: 0; }
    #modal-backdrop { padding: 0; }
    #modal-body { max-height: none; }
    input, select, textarea, .btn { font-size: 16px; }  /* 16px stops iOS zooming on focus */
    .btn { padding: 8px 14px; }
    table.grid { display: block; overflow-x: auto; white-space: nowrap; }
  }

  /* Measurement grid */
  .qty-in { width: 100px; text-align: right; }
  .warn-text { color: var(--amber); font-weight: 700; }
  .ok-text { color: var(--green); font-weight: 700; }
  .ref-chip { display: inline-block; font-size: 11px; padding: 0 5px; margin: 0 2px; border: 1px solid var(--maroon);
    color: var(--maroon); border-radius: 3px; cursor: pointer; background: var(--maroon-pale); }
  .ref-chip:hover { background: var(--maroon); color: #fff; }

  /* Report / print */
  .report { max-width: 900px; }
  .report h2 { color: var(--maroon); border-bottom: 2px solid var(--maroon); padding-bottom: 4px; font-size: 18px; margin: 4px 0 12px; }
  .report h3 { color: var(--maroon); font-size: 13px; text-transform: uppercase; letter-spacing: .5px; margin: 18px 0 6px; }
  .report .rep-meta { color: var(--muted); font-size: 11.5px; margin-bottom: 12px; }
  .report table.grid th { position: static; }
  @media print {
    #sidebar, #topbar, .no-print, .toolbar { display: none !important; }
    #app { display: block; height: auto; }
    #main { display: block; }
    #view { overflow: visible; padding: 0; }
    body { font-size: 11px; }
    .card, .report table, .report-photos figure, .sig-block { break-inside: avoid; }
    table.grid th { background: #fff !important; color: var(--maroon) !important; border-bottom: 2px solid var(--maroon); }
  }
