*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #fafafa;
  --surface: #ffffff;
  --border: #e4e4e7;
  --text: #09090b;
  --text-secondary: #52525b;
  --text-muted: #a1a1aa;
  --primary: #09090b;
  --primary-hover: #27272a;
  --radius: 6px;
  --radius-lg: 10px;
  --max-w: 960px;
}

body {
  font-family: 'Outfit', -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
}

/* Nav */
.nav {
  position: sticky;
  top: 0;
  z-index: 100;
  background: rgba(250,250,250,0.92);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
.nav-inner {
  max-width: var(--max-w);
  margin: 0 auto;
  padding: 0 1.5rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 56px;
}
.nav-logo {
  font-weight: 700;
  font-size: 1rem;
  color: var(--text);
  text-decoration: none;
  letter-spacing: -0.02em;
}
.nav-format-switch {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}
.nav-badge,
.nav-link {
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  padding: 0.25rem 0.6rem;
  border-radius: 4px;
  border: 1px solid var(--border);
  text-decoration: none;
}
.nav-badge {
  color: var(--text-muted);
  background: #f0f0f0;
}
.nav-link {
  color: var(--text-muted);
  background: transparent;
  border-color: transparent;
  transition: color 0.15s, background 0.15s, border-color 0.15s;
}
.nav-link:hover {
  color: var(--text);
  background: rgba(0,0,0,0.04);
  border-color: var(--border);
}

main {
  max-width: var(--max-w);
  margin: 0 auto;
  padding: 2rem 1.5rem;
}

h2 {
  font-size: 0.95rem;
  font-weight: 700;
  margin-bottom: 0.75rem;
  letter-spacing: -0.01em;
}

/* Page header (above upload area on viewer pages) */
.page-header {
  margin-bottom: 1.75rem;
}
.page-header h1 {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin-bottom: 0.5rem;
  color: var(--text);
}
.page-header .page-sub {
  font-size: 0.95rem;
  color: var(--text-muted);
  max-width: 56ch;
  line-height: 1.5;
}

/* Upload area */
.upload-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  padding: 2.5rem;
  border: 1px dashed var(--border);
  border-radius: var(--radius-lg);
  cursor: pointer;
  color: var(--text-muted);
  font-size: 0.875rem;
  transition: border-color 0.2s, background 0.2s, color 0.2s;
  margin-bottom: 2rem;
  background: var(--surface);
}
.upload-area:hover,
.upload-area.dragover {
  border-color: #a1a1aa;
  background: #f9f9f9;
  color: var(--text-secondary);
}
.upload-icon { font-size: 1.75rem; line-height: 1; }

.file-meta { font-size: 0.76rem; color: var(--text-muted); margin-top: 0.1rem; }

/* Buttons */
button, .btn {
  padding: 0.4rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text-secondary);
  cursor: pointer;
  font-family: 'Outfit', sans-serif;
  font-size: 0.8rem;
  font-weight: 500;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
button:hover, .btn:hover {
  background: #f4f4f5;
  color: var(--text);
  border-color: #d4d4d8;
}
.del-btn { padding: 0.4rem 0.6rem; }
.del-btn:hover {
  color: #dc2626;
  border-color: #fca5a5;
  background: #fef2f2;
}

.viewer-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 1.5rem;
}
#save-pdf-btn { font-weight: 600; }

/* Declaration header */
.decl-header {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.25rem 1.5rem;
  margin-bottom: 1rem;
}
.decl-header h3 {
  font-size: 1rem;
  font-weight: 700;
  margin-bottom: 0.9rem;
  letter-spacing: -0.01em;
}
.decl-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 0.75rem 1.5rem;
}
.decl-field label {
  display: block;
  font-size: 0.65rem;
  text-transform: uppercase;
  color: var(--text-muted);
  letter-spacing: 0.06em;
  margin-bottom: 0.15rem;
}
.decl-field span { font-size: 0.88rem; }

/* Parties */
.decl-parties {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 0.75rem;
  margin-bottom: 1rem;
}
.party-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1rem 1.25rem;
}
.party-card h4 {
  font-size: 0.65rem;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.07em;
  margin-bottom: 0.4rem;
}
.party-card .name { font-weight: 600; font-size: 0.88rem; margin-bottom: 0.2rem; }
.party-card .addr { font-size: 0.81rem; color: var(--text-secondary); }

/* Goods table */
.goods-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  font-size: 0.85rem;
}
.goods-table th {
  background: #fafafa;
  text-align: left;
  padding: 0.6rem 0.85rem;
  font-weight: 700;
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
}
.goods-table td {
  padding: 0.6rem 0.85rem;
  border-bottom: 1px solid #f4f4f5;
}
.goods-table tr:last-child td { border-bottom: none; }
.goods-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.goods-table tfoot td {
  font-weight: 600;
  border-top: 1px solid var(--border);
  background: #fafafa;
  color: var(--text-secondary);
}

/* Loading */
.loading {
  text-align: center;
  padding: 3rem;
  color: var(--text-muted);
  font-size: 0.88rem;
}

/* QMF records dump */
.records-dump { margin-top: 1.5rem; }
.records-title { font-size: 0.95rem; font-weight: 600; margin-bottom: 0.5rem; }
.record {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 0.5rem;
  overflow: hidden;
}
.record summary {
  padding: 0.6rem 1rem;
  cursor: pointer;
  font-size: 0.85rem;
  color: var(--text-secondary);
  user-select: none;
}
.record summary code {
  font-size: 0.75rem;
  background: #f4f4f5;
  color: var(--text-muted);
  padding: 0.1rem 0.4rem;
  border-radius: 3px;
}
.fields-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8rem;
  border-top: 1px solid var(--border);
}
.fields-table td {
  padding: 0.35rem 0.75rem;
  border-bottom: 1px solid #f9f9f9;
  vertical-align: top;
}
.fields-table tr:last-child td { border-bottom: none; }
.fields-table .field-code {
  width: 4rem;
  color: var(--text-muted);
  font-family: ui-monospace, "SF Mono", monospace;
  font-size: 0.75rem;
}
.fields-table .field-label {
  width: 16rem;
  color: var(--text-muted);
}
.fields-table .field-value {
  word-break: break-word;
  white-space: pre-wrap;
  color: var(--text);
}

/* Responsive */
@media (max-width: 600px) {
  main { padding: 1.5rem 1rem; }
  .decl-grid { grid-template-columns: 1fr 1fr; }
  .decl-parties { grid-template-columns: 1fr; }
  .goods-table { font-size: 0.78rem; }
  .goods-table th, .goods-table td { padding: 0.5rem 0.6rem; }
}

/* Print: A4 portrait, fits all goods table columns within page width */
@media print {
  @page { size: A4 portrait; margin: 12mm; }

  html, body { background: #fff !important; }
  body { font-size: 9pt; line-height: 1.35; }

  .nav, #upload-section, .viewer-actions { display: none !important; }

  main { max-width: none; padding: 0; margin: 0; }

  /* Color-preserving for table headers/footers */
  .goods-table th,
  .goods-table tfoot td,
  .decl-header,
  .party-card { -webkit-print-color-adjust: exact; print-color-adjust: exact; }

  .decl-header {
    padding: 0.5cm 0.6cm;
    margin-bottom: 0.35cm;
    border-radius: 4px;
    page-break-inside: avoid;
  }
  .decl-header h3 { font-size: 12pt; margin-bottom: 0.35cm; }
  .decl-grid {
    grid-template-columns: repeat(3, 1fr);
    gap: 0.3cm 0.6cm;
  }
  .decl-field label { font-size: 6.5pt; margin-bottom: 0.05cm; }
  .decl-field span { font-size: 9pt; }

  .decl-parties {
    grid-template-columns: 1fr 1fr;
    gap: 0.3cm;
    margin-bottom: 0.4cm;
  }
  .party-card {
    padding: 0.35cm 0.45cm;
    border-radius: 4px;
    page-break-inside: avoid;
  }
  .party-card h4 { font-size: 6.5pt; margin-bottom: 0.15cm; }
  .party-card .name { font-size: 9pt; margin-bottom: 0.1cm; }
  .party-card .addr,
  .party-card .file-meta { font-size: 7.5pt; }

  /* Goods table: fixed layout to guarantee A4 fit, repeat header on page break */
  .goods-table {
    table-layout: fixed;
    width: 100%;
    font-size: 8pt;
    border-radius: 4px;
    page-break-inside: auto;
  }
  .goods-table thead { display: table-header-group; }
  .goods-table tfoot { display: table-footer-group; }
  .goods-table tr { page-break-inside: avoid; }
  .goods-table th {
    font-size: 6.5pt;
    padding: 4px 5px;
    background: #f4f4f5 !important;
  }
  .goods-table td {
    padding: 4px 5px;
    word-break: break-word;
    overflow-wrap: break-word;
    vertical-align: top;
  }
  .goods-table tfoot td { background: #f4f4f5 !important; }

  /* Column widths shared by IMFX (№,Товар,УКТЗЕД,К-сть,Вага,Ціна,Сума)
     and QMF (№,Товар,УКТЗЕД,К-сть,Вага,Сума,Вал.) — 7 columns each */
  .goods-table th:nth-child(1), .goods-table td:nth-child(1) { width: 5%; }
  .goods-table th:nth-child(2), .goods-table td:nth-child(2) { width: 36%; }
  .goods-table th:nth-child(3), .goods-table td:nth-child(3) { width: 14%; }
  .goods-table th:nth-child(4), .goods-table td:nth-child(4) { width: 11%; }
  .goods-table th:nth-child(5), .goods-table td:nth-child(5) { width: 11%; }
  .goods-table th:nth-child(6), .goods-table td:nth-child(6) { width: 13%; }
  .goods-table th:nth-child(7), .goods-table td:nth-child(7) { width: 10%; }
}
