:root {
  --ink: #111827;
  --muted: #6b7280;
  --line: #111;
  --paper: #fff;
  --bg: #eef1f5;
  --primary: #991b1b;
  --primary-dark: #7f1d1d;
  --soft: #f8fafc;
  --danger: #b91c1c;
  --sheet-width: 210mm;
  --sheet-height: 297mm;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { color-scheme: light; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: Arial, Helvetica, sans-serif;
}
button, input, textarea { font: inherit; }
button { cursor: pointer; }

.app-header {
  min-height: 76px;
  padding: 14px clamp(16px, 4vw, 48px);
  background: #fff;
  border-bottom: 1px solid #d8dee7;
  display: flex;
  gap: 24px;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 50;
}
.app-header h1 { margin: 2px 0 0; font-size: clamp(1.2rem, 2vw, 1.65rem); }
.eyebrow { font-size: .72rem; letter-spacing: .12em; text-transform: uppercase; color: var(--muted); font-weight: 700; }
.top-nav { display: flex; gap: 8px; }
.nav-btn {
  border: 1px solid #d3d9e2;
  background: #fff;
  border-radius: 9px;
  padding: 9px 14px;
  color: #374151;
  font-weight: 700;
}
.nav-btn.active { border-color: var(--primary); background: #fff5f5; color: var(--primary); }

.app-main { padding: 22px 16px 48px; }
.view { display: none; }
.view.active { display: block; }
.toolbar {
  max-width: calc(var(--sheet-width) + 40px);
  margin: 0 auto 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}
.toolbar-left, .toolbar-right { display: flex; gap: 8px; flex-wrap: wrap; }
.btn {
  border: 1px solid #cfd6df;
  background: #fff;
  color: #1f2937;
  border-radius: 8px;
  padding: 9px 14px;
  font-weight: 700;
  box-shadow: 0 1px 2px rgba(0,0,0,.04);
}
.btn:hover { background: #f9fafb; }
.btn.primary { background: var(--primary); color: white; border-color: var(--primary); }
.btn.primary:hover { background: var(--primary-dark); }
.btn.secondary { background: #f8fafc; }
.btn.danger { background: #fff; color: var(--danger); border-color: #fecaca; }
.btn.small { padding: 6px 9px; font-size: .78rem; }

.status {
  max-width: calc(var(--sheet-width) + 40px);
  margin: 0 auto 14px;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid #cbd5e1;
  background: #fff;
  font-size: .9rem;
}
.status.ok { border-color: #86efac; background: #f0fdf4; }
.status.error { border-color: #fca5a5; background: #fef2f2; color: #991b1b; }

.a4-sheet {
  width: var(--sheet-width);
  min-height: var(--sheet-height);
  margin: 0 auto;
  background: var(--paper);
  box-shadow: 0 10px 30px rgba(15,23,42,.14);
  padding: 12.5mm 12.5mm 10mm;
  font-family: "Times New Roman", Times, serif;
  color: #000;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.form-header {
  display: grid;
  grid-template-columns: 45mm 1fr 48mm;
  grid-template-rows: 21mm 14mm;
  border: .35mm solid var(--line);
}
.logo-cell, .form-title-cell, .meta-cell { min-width: 0; }
.logo-cell { border-right: .35mm solid var(--line); display: flex; align-items: center; justify-content: center; padding: 2mm; }
.logo-cell img { width: 36mm; height: auto; max-height: 14mm; object-fit: contain; }
.form-title-cell {
  display: flex; align-items: center; justify-content: center;
  font-size: 17pt; font-weight: 700;
  border-right: .35mm solid var(--line);
}
.meta-cell { font-size: 8.6pt; font-style: italic; }
.meta-cell > div { height: 25%; display: grid; grid-template-columns: 21mm 1fr; align-items: center; padding: 0 2mm; border-bottom: .25mm solid var(--line); }
.meta-cell > div:last-child { border-bottom: 0; }
.meta-cell span { white-space: nowrap; }
.meta-cell strong { font-weight: 400; white-space: nowrap; }
.walkthrough-title {
  grid-column: 1 / -1;
  border-top: .35mm solid var(--line);
  position: relative;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding-bottom: 2mm;
  font-size: 14pt;
}
.walkthrough-title span { position: absolute; left: 2mm; top: 1mm; font-size: 11pt; font-weight: 400; }
.walkthrough-title strong { font-size: 14.5pt; }

.form-section { margin-top: 6.3mm; }
.general-box { border: .35mm solid var(--line); height: 31mm; padding: 1.8mm 2mm 2mm; display: flex; flex-direction: column; }
.general-box label { font-size: 11.5pt; font-weight: 700; margin-bottom: 1mm; }
.general-box textarea { flex: 1; }

.a4-sheet input, .a4-sheet textarea {
  border: 0;
  outline: 0;
  background: transparent;
  color: #000;
  font-family: "Times New Roman", Times, serif;
  resize: none;
  min-width: 0;
}
.a4-sheet input:focus, .a4-sheet textarea:focus { background: #fffbea; box-shadow: inset 0 0 0 .2mm #f59e0b; }
.a4-sheet textarea { width: 100%; font-size: 10.5pt; line-height: 1.3; padding: 1mm; }
.a4-sheet input { width: 100%; font-size: 10.5pt; padding: 0 1mm; }
input[type="date"]::-webkit-calendar-picker-indicator { opacity: .6; }

.info-grid {
  display: grid;
  grid-template-columns: 31mm 1fr 31mm 42mm;
  grid-template-rows: 8.5mm 8.5mm;
  border: .35mm solid var(--line);
  font-size: 10.5pt;
}
.info-grid > * { border-right: .25mm solid var(--line); border-bottom: .25mm solid var(--line); display: flex; align-items: center; }
.info-grid > *:nth-child(4n) { border-right: 0; }
.info-grid > *:nth-last-child(-n+4) { border-bottom: 0; }
.info-grid label { padding: 0 2mm; }

.inspection-grid {
  display: grid;
  grid-template-columns: 36mm 1fr 77mm;
  grid-template-rows: 7.2mm 71mm;
  border: .35mm solid var(--line);
}
.inspection-grid > * { min-width: 0; }
.grid-head { display: flex; align-items: center; justify-content: center; font-size: 11pt; border-right: .25mm solid var(--line); border-bottom: .25mm solid var(--line); }
.grid-head:nth-child(3) { border-right: 0; }
.area-cell, .findings-cell { border-right: .25mm solid var(--line); }
.area-cell {
  padding: 3mm 2mm 2mm;
  display: flex;
  flex-direction: column;
  gap: 2.6mm;
  font-size: 10.5pt;
}
.area-field {
  display: flex;
  flex-direction: column;
  gap: .7mm;
  min-width: 0;
}
.area-field label {
  display: block;
  font-size: 10.5pt;
  line-height: 1.1;
}
.area-field input,
.area-field textarea {
  display: block;
  width: 100%;
  border: 0 !important;
  border-bottom: 0 !important;
  outline: 0;
  background: transparent;
  padding: .4mm .8mm;
  margin: 0;
  font-size: 10pt;
}
.area-field input { min-height: 7mm; }
.area-field textarea { resize: none; }
.manpower-field {
  flex: 1 1 auto;
  min-height: 24mm;
}
.manpower-field textarea {
  flex: 1 1 auto;
  min-height: 20mm;
  line-height: 1.25;
}
.findings-cell { padding: 2mm; }
.findings-cell textarea { height: 100%; font-size: 10.4pt; }
.recommendation-cell { padding: 2mm; display: flex; flex-direction: column; }
.recommendation-cell > textarea { flex: 1 1 auto; min-height: 26mm; }
.closeout-block { margin-top: 2mm; min-height: 31mm; display: flex; flex-direction: column; justify-content: flex-end; }
.closeout-label { font-size: 10.2pt; margin-bottom: 1mm; }
.mini-signature { height: 11mm; position: relative; display: flex; justify-content: center; align-items: flex-end; }
.closeout-name-row { display: grid; grid-template-columns: 1fr 28mm; gap: 1mm; border-bottom: .3mm solid #000; min-height: 6mm; align-items: end; }
.closeout-name-row input { text-align: center; font-weight: 600; font-size: 9pt; }
.signature-line-label { text-align: center; font-size: 10pt; font-weight: 700; padding-top: 1mm; }

.attachments-section { margin-top: 6mm; }
.attachments-section h2 { margin: 0 0 2mm; font-size: 11.5pt; }
.attachments-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 3mm; height: 50mm; }
.photo-slot {
  position: relative;
  border: .25mm dashed #9ca3af;
  overflow: hidden;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
}
.photo-upload {
  width: 100%; height: 100%; border: 0; background: #fafafa; color: #6b7280;
  display: flex; flex-direction: column; gap: 4px; align-items: center; justify-content: center;
  font-family: Arial, Helvetica, sans-serif; font-size: 10px;
}
.upload-symbol { font-size: 26px; line-height: 1; }
.photo-preview { width: 100%; height: 100%; object-fit: contain; display: block; }
.remove-photo, .remove-image {
  position: absolute; top: 4px; right: 4px; z-index: 3;
  border: 0; border-radius: 5px; padding: 3px 6px; background: rgba(127,29,29,.9); color: #fff;
  font-family: Arial, Helvetica, sans-serif; font-size: 9px;
}

.approval-grid {
  margin-top: auto;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  min-height: 26mm;
  border: .35mm solid var(--line);
}
.approval-cell { position: relative; padding: 2mm 1.5mm 0; border-right: .25mm solid var(--line); display: grid; grid-template-rows: 5mm 11mm 5mm 5mm; }
.approval-cell:last-child { border-right: 0; }
.approval-label { font-size: 9.5pt; }
.signature-stage { position: relative; display: flex; align-items: center; justify-content: center; min-height: 10mm; }
.signature-upload {
  border: 1px dashed #b6bec8; background: #f8fafc; color: #64748b; border-radius: 5px;
  font-family: Arial, Helvetica, sans-serif; font-size: 9px; padding: 4px 7px;
}
.signature-img { max-width: 45mm; max-height: 10mm; width: auto; height: auto; object-fit: contain; display: block; }
.printed-name { border-bottom: .3mm solid #000 !important; text-align: center; font-weight: 700; height: 5mm; padding: 0 1mm !important; }
.role-label { text-align: center; font-size: 10pt; font-weight: 700; padding-top: .7mm; white-space: nowrap; }
.approval-cell:last-child .role-label { font-size: 9.2pt; }

.stored-panel { max-width: 1100px; margin: 0 auto; background: #fff; border: 1px solid #d8dee7; border-radius: 14px; padding: 24px; box-shadow: 0 10px 30px rgba(15,23,42,.08); }
.stored-header { display: flex; justify-content: space-between; align-items: center; gap: 16px; }
.stored-header h2 { margin: 4px 0 0; }
.token-row { margin: 20px 0; display: grid; grid-template-columns: auto minmax(220px, 420px) auto; gap: 10px; align-items: center; }
.token-row label { font-weight: 700; }
.token-row input, .token-dialog input { border: 1px solid #cfd6df; border-radius: 8px; padding: 9px 10px; }
.table-wrap { overflow-x: auto; }
.reports-table { width: 100%; border-collapse: collapse; font-size: .9rem; }
.reports-table th, .reports-table td { padding: 11px 9px; border-bottom: 1px solid #e5e7eb; text-align: left; white-space: nowrap; }
.reports-table th { color: #4b5563; font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; }
.action-group { display: flex; gap: 6px; }
.empty-cell { text-align: center !important; color: var(--muted); padding: 30px !important; }
.token-dialog { border: 0; border-radius: 14px; padding: 0; width: min(92vw, 420px); box-shadow: 0 24px 70px rgba(0,0,0,.28); }
.token-dialog::backdrop { background: rgba(15,23,42,.55); }
.summary-dialog { width: min(94vw, 760px); }
.summary-wrap { padding: 22px; }
.summary-head { display:flex; align-items:center; justify-content:space-between; gap:16px; }
.summary-head h3 { margin: 4px 0 0; }
.summary-content { margin-top:18px; display:grid; grid-template-columns: 170px 1fr; border-top:1px solid #e5e7eb; }
.summary-content dt, .summary-content dd { margin:0; padding:10px 8px; border-bottom:1px solid #e5e7eb; }
.summary-content dt { font-weight:700; color:#4b5563; background:#f8fafc; }
.summary-content dd { white-space:pre-wrap; }

.token-dialog form { padding: 22px; }
.token-dialog h3 { margin: 0 0 8px; }
.token-dialog p { color: #6b7280; margin: 0 0 14px; }
.token-dialog input { width: 100%; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }


.forced-hidden { display: none !important; }
.readonly-mode .photo-upload,
.readonly-mode .signature-upload,
.readonly-mode .remove-photo,
.readonly-mode .remove-image { display: none !important; }

.readonly-mode .a4-sheet input,
.readonly-mode .a4-sheet textarea { pointer-events: none; }
.readonly-mode .photo-slot { border-color: transparent; }

@media (max-width: 900px) {
  .app-main { overflow-x: auto; }
  .a4-sheet { transform-origin: top left; }
  .token-row { grid-template-columns: 1fr; }
}

@page { size: A4 portrait; margin: 0; }
@media print {
  html, body { width: 210mm; height: 297mm; margin: 0 !important; padding: 0 !important; background: #fff !important; }
  body { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .no-print, .app-header, #storedView, .status, dialog { display: none !important; }
  .app-main, #editorView, #inspectionForm { display: block !important; margin: 0 !important; padding: 0 !important; width: 210mm !important; }
  .a4-sheet {
    margin: 0 !important;
    width: 210mm !important;
    min-height: 297mm !important;
    height: 297mm !important;
    box-shadow: none !important;
    page-break-after: always;
  }
  .a4-sheet input, .a4-sheet textarea { box-shadow: none !important; }
  input[type="date"]::-webkit-calendar-picker-indicator { display: none; }
  .a4-sheet input::placeholder, .a4-sheet textarea::placeholder { color: transparent !important; }
  .photo-slot { border-color: transparent !important; }
  .photo-upload, .signature-upload, .remove-photo, .remove-image { display: none !important; }
  .photo-preview[hidden], .signature-img[hidden] { display: none !important; }
}

/* Revision 2: permitting-system style image/signature controls + reinforced form lines */
.form-header,
.general-box,
.info-grid,
.inspection-grid,
.approval-grid {
  border-color: #000;
  border-style: solid;
  border-width: .38mm;
}
.logo-cell,
.form-title-cell { border-right: .30mm solid #000; }
.meta-cell > div { border-bottom: .28mm solid #000; }
.walkthrough-title { border-top: .30mm solid #000; }

/* Explicit cell borders prevent grid lines from disappearing at browser/print scaling. */
.info-grid > * { border: 0; }
.info-grid > :nth-child(1),
.info-grid > :nth-child(2),
.info-grid > :nth-child(3),
.info-grid > :nth-child(5),
.info-grid > :nth-child(6),
.info-grid > :nth-child(7) { border-right: .30mm solid #000; }
.info-grid > :nth-child(-n+4) { border-bottom: .30mm solid #000; }

.inspection-grid > * { border: 0; }
.inspection-grid > .grid-head { border-bottom: .30mm solid #000; }
.inspection-grid > .grid-head:nth-child(1),
.inspection-grid > .grid-head:nth-child(2),
.inspection-grid > .area-cell,
.inspection-grid > .findings-cell { border-right: .30mm solid #000; }

.approval-cell { border-right: .30mm solid #000; }
.approval-cell:last-child { border-right: 0; }

.upload-image-block { position: relative; }
.photo-input,
.signature-file {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}
.upload-button,
.remove-image-button {
  position: absolute;
  z-index: 12;
  top: 2px;
  padding: 4px 7px;
  border-radius: 5px;
  border: 1px solid #8b99a7;
  background: rgba(255,255,255,.94);
  color: #34475b;
  font: 600 9px/1 Arial, Helvetica, sans-serif;
  box-shadow: 0 1px 4px rgba(0,0,0,.12);
}
.upload-button { left: 50%; transform: translateX(-50%); white-space: nowrap; }
.remove-image-button { right: 2px; color: #9d2525; min-width: 22px; }

.attachments-grid { gap: 3mm; height: 50mm; }
.photo-slot {
  position: relative;
  overflow: hidden;
  background: #fff;
  border: 0;
  display: block;
}
.photo-slot::after {
  content: "";
  position: absolute;
  inset: 0;
  border: .20mm dashed #b6bec8;
  pointer-events: none;
}
.photo-preview {
  position: absolute;
  z-index: 6;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  pointer-events: none;
}

.mini-signature {
  height: 12mm;
  position: relative;
  display: block;
  overflow: visible;
}
.signature-stage {
  position: relative;
  min-height: 15mm;
  height: 15mm;
  overflow: visible;
}
.signature-img {
  position: absolute;
  z-index: 6;
  inset: 0 3mm 2.2mm 3mm;
  width: calc(100% - 6mm);
  height: calc(100% - 2.2mm);
  max-width: none;
  max-height: none;
  object-fit: contain;
  display: block;
  pointer-events: none;
}
.signature-name {
  position: absolute;
  z-index: 4;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 37%;
  padding: 0 1mm !important;
  border: 0 !important;
  border-bottom: .30mm solid #000 !important;
  outline: 0;
  background: transparent;
  text-align: center;
  font-weight: 700;
}
.signature-name:focus { background: rgba(211,235,255,.45) !important; box-shadow: inset 0 0 0 .2mm rgba(31,95,170,.45) !important; }
.approval-cell {
  min-height: 26mm;
  padding: 2mm 1.5mm 0;
  display: grid;
  grid-template-rows: 5mm 15mm 5mm;
}

.readonly-mode .photo-slot::after { display: none; }

@media print {
  .photo-slot::after { display: none !important; }
  .upload-button, .remove-image-button { display: none !important; }
  .form-header, .general-box, .info-grid, .inspection-grid, .approval-grid { border-color: #000 !important; }
}
.readonly-mode .remove-image-button { display: none !important; }
.photo-upload {
  width: auto;
  height: auto;
  display: block;
  flex: none;
  background: rgba(255,255,255,.94);
  color: #34475b;
}
@media print {
  html, body { width: 210mm !important; height: auto !important; min-height: 0 !important; }
  .a4-sheet { page-break-after: auto !important; break-after: auto !important; }
}
@media print {
  .a4-sheet { height: 296mm !important; min-height: 296mm !important; }
}
.a4-sheet input[type="hidden"] { display: none !important; }
input[type="hidden"] { display: none !important; }

/* Revision 3: Area fields are stacked beneath their labels; no static text lines. */
.readonly-mode .area-field input,
.readonly-mode .area-field textarea {
  background: transparent !important;
  box-shadow: none !important;
}
@media print {
  .area-field input,
  .area-field textarea {
    border: 0 !important;
    border-bottom: 0 !important;
    background: transparent !important;
    box-shadow: none !important;
  }
}
