:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --text: #0f172a;
  --muted: #475569;
  --border: #cbd5e1;
  --primary: #1d4ed8;
  --primary-hover: #1e40af;
  --secondary: #0f766e;
  --secondary-hover: #115e59;
  --warn-bg: #fffbeb;
  --warn-border: #f59e0b;
  --warn-text: #92400e;
  --err-bg: #fef2f2;
  --err-border: #ef4444;
  --err-text: #991b1b;
  --info-bg: #eff6ff;
  --info-border: #3b82f6;
  --info-text: #1e3a8a;
  --kai-font: "DFKai-SB", "BiauKai", "標楷體", "TW-Kai", "KaiTi", serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Microsoft JhengHei", "PingFang TC", "Noto Sans TC", system-ui, sans-serif;
  line-height: 1.5;
}

.hidden {
  display: none !important;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 16px;
  border-radius: 8px;
  border: 1px solid transparent;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, opacity 0.15s;
  white-space: nowrap;
}

.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover:not(:disabled) {
  background: var(--primary-hover);
}

.btn-secondary {
  background: var(--secondary);
  color: #fff;
}
.btn-secondary:hover:not(:disabled) {
  background: var(--secondary-hover);
}

.btn-ghost {
  background: #fff;
  color: var(--text);
  border-color: var(--border);
}
.btn-ghost:hover:not(:disabled) {
  background: #f8fafc;
}

.btn-sm {
  padding: 6px 11px;
  font-size: 13px;
}

.btn-xs {
  padding: 3px 7px;
  font-size: 12px;
  border-radius: 5px;
}

.btn-full {
  width: 100%;
}

/* Login Gate */
.login-shell {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border-radius: 14px;
  padding: 32px 28px;
  box-shadow: 0 12px 32px rgba(15, 23, 42, 0.1);
  border: 1px solid #e2e8f0;
}

.login-badge {
  display: inline-block;
  font-size: 12px;
  font-weight: 700;
  color: var(--primary);
  background: #dbeafe;
  padding: 3px 10px;
  border-radius: 999px;
  margin-bottom: 10px;
}

.login-card h1 {
  margin: 0 0 6px;
  font-size: 22px;
}

.login-sub {
  margin: 0 0 22px;
  font-size: 13px;
  color: var(--muted);
}

.login-form label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 6px;
}

.login-form input {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 15px;
  margin-bottom: 14px;
}

.login-form input:focus {
  outline: 2px solid #93c5fd;
  border-color: var(--primary);
}

.inline-error {
  background: var(--err-bg);
  color: var(--err-text);
  border: 1px solid var(--err-border);
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 13px;
  margin-bottom: 12px;
}

.login-foot {
  margin: 18px 0 0;
  font-size: 12px;
  color: var(--muted);
  text-align: center;
}

/* Topbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  background: #0f172a;
  color: #fff;
  padding: 12px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-dot {
  width: 12px;
  height: 12px;
  border-radius: 999px;
  background: #38bdf8;
  flex-shrink: 0;
}

.brand-title {
  font-weight: 700;
  font-size: 16px;
}

.brand-meta {
  font-size: 12px;
  color: #cbd5e1;
}

.topbar-right {
  display: flex;
  gap: 8px;
}

.topbar .btn-ghost {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
  border-color: rgba(255, 255, 255, 0.25);
}
.topbar .btn-ghost:hover {
  background: rgba(255, 255, 255, 0.2);
}

/* Main Container */
.main-container {
  max-width: 1080px;
  margin: 0 auto;
  padding: 20px 16px 48px;
}

.panel {
  background: var(--surface);
  border-radius: 12px;
  border: 1px solid #e2e8f0;
  padding: 20px;
  box-shadow: 0 2px 10px rgba(15, 23, 42, 0.04);
}

.panel-header {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: flex-end;
  gap: 14px;
  margin-bottom: 16px;
}

.panel-header h2 {
  margin: 0 0 4px;
  font-size: 18px;
}

.muted {
  margin: 0;
  font-size: 13px;
  color: var(--muted);
}

.folder-name-box {
  min-width: 240px;
  flex: 1;
  max-width: 340px;
}

.header-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 12px;
  flex: 1;
  justify-content: flex-end;
}

.mode-box label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 4px;
}

.segmented {
  display: inline-flex;
  background: #e2e8f0;
  padding: 3px;
  border-radius: 9px;
  gap: 3px;
}

.seg-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  padding: 6px 12px;
  border-radius: 7px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, box-shadow 0.15s;
  white-space: nowrap;
}

.seg-btn.active {
  background: #fff;
  color: var(--primary);
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.14);
}

.segmented-sm .seg-btn {
  padding: 5px 11px;
  font-size: 13px;
}

.contractor-title {
  text-align: center;
  font-family: var(--kai-font);
  font-size: 16pt;
  font-weight: 700;
  letter-spacing: 0.12em;
  line-height: 1.3;
  margin: 0 0 3mm;
  color: #000;
}

.photo-slot-inner.contractor-slot {
  justify-content: center;
  align-items: center;
  gap: 0;
}

.folder-name-box label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 4px;
}

.folder-name-box input,
.export-bar input {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 7px;
  font-size: 14px;
}

/* Dropzone */
.drop-zone {
  border: 2px dashed #94a3b8;
  border-radius: 12px;
  padding: 30px 18px;
  text-align: center;
  background: #f8fafc;
  transition: border-color 0.15s, background 0.15s;
}

.drop-zone.drag-over {
  border-color: var(--primary);
  background: #eff6ff;
}

.drop-icon {
  font-size: 34px;
  margin-bottom: 6px;
}

.drop-title {
  font-weight: 700;
  font-size: 15px;
  margin-bottom: 4px;
}

.drop-hint {
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 14px;
}

.drop-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
}

/* Staging Thumbnails */
.staging-area {
  margin-top: 18px;
  padding-top: 16px;
  border-top: 1px solid #e2e8f0;
}

.staging-bar {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}

.staging-summary {
  font-weight: 700;
  font-size: 14px;
}

.staging-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.thumb-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(126px, 1fr));
  gap: 10px;
}

.thumb-card {
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  overflow: hidden;
  background: #fff;
}

.thumb-card img {
  width: 100%;
  height: 90px;
  object-fit: cover;
  display: block;
}

.thumb-meta {
  padding: 5px 7px;
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Status & Alerts */
.status-box {
  margin-top: 16px;
  padding: 14px 16px;
  border-radius: 10px;
  background: var(--info-bg);
  border: 1px solid var(--info-border);
  color: var(--info-text);
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 14px;
  font-weight: 600;
}

.spinner {
  width: 20px;
  height: 20px;
  border: 3px solid #bfdbfe;
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.alert {
  margin-top: 12px;
  padding: 12px 15px;
  border-radius: 10px;
  font-size: 13px;
  border: 1px solid;
}

.alert-error {
  background: var(--err-bg);
  border-color: var(--err-border);
  color: var(--err-text);
}

.alert-warn {
  background: var(--warn-bg);
  border-color: var(--warn-border);
  color: var(--warn-text);
}

.alert-info {
  background: var(--info-bg);
  border-color: var(--info-border);
  color: var(--info-text);
}

.alert ul {
  margin: 6px 0 0;
  padding-left: 18px;
}

/* Editor Toolbar */
.export-bar {
  position: sticky;
  top: 58px;
  z-index: 20;
  background: #ffffff;
  border: 1px solid #cbd5e1;
  border-radius: 12px;
  padding: 12px 16px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  box-shadow: 0 4px 14px rgba(15, 23, 42, 0.08);
}

.export-left,
.export-right {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.field-inline {
  display: flex;
  align-items: center;
  gap: 6px;
}

.field-inline label {
  font-size: 12px;
  font-weight: 700;
  color: var(--muted);
  white-space: nowrap;
}

.field-inline input {
  width: 210px;
}

.field-inline input.input-sm {
  width: 105px;
}

.print-tip {
  margin: 12px 0 18px;
  font-size: 12.5px;
  color: var(--muted);
}

/* A4 Inspection Report Sheets */
.pages-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 24px;
}

.a4-page {
  width: 210mm;
  min-height: 297mm;
  padding: 12.7mm;
  background: #fff;
  box-shadow: 0 6px 24px rgba(15, 23, 42, 0.14);
  font-family: var(--kai-font);
  color: #000;
}

.page-label {
  font-family: "Microsoft JhengHei", sans-serif;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 6px;
  display: flex;
  justify-content: space-between;
}

.report-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
  border: 1.5pt solid #000;
  font-family: var(--kai-font);
  font-size: 10pt;
}

.report-table td,
.report-table th {
  border: 0.75pt solid #000;
  padding: 2px 4px;
  vertical-align: middle;
}

.col-0 { width: 9.95%; }
.col-1 { width: 53.93%; }
.col-2 { width: 9.78%; }
.col-3 { width: 26.34%; }

.hdr-row {
  height: 7.4mm;
}

.hdr-label {
  text-align: center;
  font-weight: 400;
}

.hdr-input {
  width: 100%;
  border: 1px dashed transparent;
  background: transparent;
  font-family: var(--kai-font);
  font-size: 10pt;
  color: #000;
  padding: 1px 3px;
}

.hdr-input:hover,
.hdr-input:focus {
  border-color: #60a5fa;
  background: #f8fafc;
  outline: none;
}

.hdr-input.center {
  text-align: center;
}

.subhdr-row {
  background: #e6e6e6;
  height: 8mm;
  font-size: 12pt;
  text-align: center;
  -webkit-print-color-adjust: exact;
  print-color-adjust: exact;
}

.subhdr-row td {
  border-top: 1.5pt solid #000;
}

.photo-row {
  height: 80mm;
}

.photo-row.needs-review {
  background: #fffbeb;
}

.photo-cell {
  position: relative;
  padding: 3mm 2mm !important;
}

.photo-slot-inner {
  display: flex;
  align-items: flex-end;
  justify-content: flex-start;
  gap: 8px;
  height: 74mm;
}

.item-num {
  min-width: 16px;
  font-size: 10pt;
  line-height: 1;
  padding-bottom: 2px;
}

.photo-img-wrap {
  flex: 1;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.photo-img-wrap img {
  max-width: 101mm;
  max-height: 73.5mm;
  width: auto;
  height: auto;
  display: block;
}

.slot-controls {
  position: absolute;
  top: 4px;
  right: 4px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
  background: rgba(255, 255, 255, 0.92);
  padding: 3px 6px;
  border-radius: 6px;
  border: 1px solid #cbd5e1;
  font-family: "Microsoft JhengHei", sans-serif;
  font-size: 11px;
  z-index: 5;
}

.wm-toggle {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  cursor: pointer;
  user-select: none;
  margin-right: 3px;
}

.review-tag {
  background: #fef3c7;
  color: #92400e;
  border: 1px solid #f59e0b;
  border-radius: 4px;
  padding: 1px 5px;
  font-weight: 700;
  cursor: pointer;
}

.desc-cell {
  vertical-align: middle;
  padding: 4px 5px !important;
}

.desc-editor {
  width: 100%;
  min-height: 52mm;
  border: 1px dashed transparent;
  background: transparent;
  resize: vertical;
  font-family: var(--kai-font);
  font-size: 10pt;
  line-height: 1.55;
  color: #000;
  padding: 4px;
}

.desc-editor:hover,
.desc-editor:focus {
  border-color: #60a5fa;
  background: #f8fafc;
  outline: none;
}

.print-desc {
  display: none;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: var(--kai-font);
  font-size: 10pt;
  line-height: 1.55;
  color: #000;
}

/* Mobile responsiveness (screen only) */
@media screen and (max-width: 840px) {
  .a4-page {
    width: 100%;
    min-height: auto;
    padding: 10px;
  }
  .photo-row {
    height: auto;
  }
  .photo-slot-inner {
    height: auto;
    flex-direction: column;
    align-items: stretch;
  }
  .photo-img-wrap img {
    max-width: 100%;
    max-height: 260px;
  }
  .slot-controls {
    position: static;
    margin-bottom: 6px;
  }
  .desc-editor {
    min-height: 110px;
  }
  .field-inline input {
    width: 160px;
  }
}

/* Exact A4 Print Styles */
@page {
  size: A4 portrait;
  margin: 12.7mm;
}

@media print {
  body {
    background: #fff !important;
    margin: 0 !important;
    padding: 0 !important;
  }

  .no-print {
    display: none !important;
  }

  .main-container {
    max-width: none !important;
    margin: 0 !important;
    padding: 0 !important;
  }

  .pages-container {
    display: block !important;
    gap: 0 !important;
  }

  .a4-page {
    width: 100% !important;
    min-height: auto !important;
    padding: 0 !important;
    margin: 0 !important;
    box-shadow: none !important;
    break-after: page;
    page-break-after: always;
  }

  .a4-page:last-child {
    break-after: auto;
    page-break-after: auto;
  }

  .photo-row.needs-review {
    background: #fff !important;
  }

  .hdr-input {
    border: none !important;
    background: transparent !important;
  }

  .desc-editor {
    display: none !important;
  }

  .print-desc {
    display: block !important;
  }

  .photo-row {
    height: 79.5mm !important;
  }

  .photo-slot-inner {
    height: 73.5mm !important;
  }

  .photo-img-wrap img {
    max-width: 101mm !important;
    max-height: 73mm !important;
  }
}
