:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --border: #cbd5e1;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #4f2682;
  --primary-hover: #3b1c63;
  --excel: #15803d;
  --excel-hover: #166534;
  --accent: #2ca7df;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
}

/* Top Toolbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 24px;
  background: #ffffff;
  border-bottom: 1px solid var(--border);
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.06);
}

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

.brand-badge {
  width: 38px;
  height: 38px;
  border-radius: 8px;
  background: linear-gradient(135deg, var(--primary) 62%, var(--accent) 62%);
  color: #fff;
  font-weight: 800;
  font-size: 20px;
  display: grid;
  place-items: center;
}

.topbar-brand h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
}

.topbar-brand p {
  margin: 2px 0 0;
  font-size: 12px;
  color: var(--muted);
}

.topbar-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.status-badge {
  font-size: 12px;
  padding: 5px 10px;
  border-radius: 999px;
  background: #e2e8f0;
  color: #334155;
  font-weight: 600;
}

.status-badge.ok {
  background: #dcfce7;
  color: #166534;
}

.status-badge.warn {
  background: #fef3c7;
  color: #92400e;
}

.status-badge.err {
  background: #fee2e2;
  color: #991b1b;
}

.btn {
  border: none;
  border-radius: 7px;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease;
}

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

.btn-excel {
  background: var(--excel);
  color: #fff;
}
.btn-excel:hover {
  background: var(--excel-hover);
}

.btn-secondary {
  background: #e2e8f0;
  color: #1e293b;
}
.btn-secondary:hover {
  background: #cbd5e1;
}

.btn-sm {
  padding: 5px 10px;
  font-size: 12px;
  background: #eef2ff;
  color: var(--primary);
}
.btn-sm:hover {
  background: #e0e7ff;
}

.link-btn {
  background: none;
  border: none;
  padding: 0;
  color: var(--primary);
  font-weight: 700;
  text-decoration: underline;
  cursor: pointer;
  font-size: inherit;
}

/* Main Workspace Layout */
.workspace {
  display: grid;
  grid-template-columns: minmax(360px, 460px) 1fr;
  gap: 20px;
  padding: 20px 24px;
  max-width: 1520px;
  margin: 0 auto;
  align-items: start;
}

@media (max-width: 1100px) {
  .workspace {
    grid-template-columns: 1fr;
  }
}

/* Left Editor Panel */
.editor-panel {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.dropzone {
  position: relative;
  border: 2px dashed #94a3b8;
  border-radius: 10px;
  background: #ffffff;
  padding: 18px 16px;
  display: flex;
  align-items: center;
  gap: 14px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}

.dropzone.dragover {
  border-color: var(--primary);
  background: #f5f3ff;
}

.dropzone-icon {
  font-size: 28px;
  flex-shrink: 0;
}

.dropzone-text {
  display: flex;
  flex-direction: column;
  gap: 3px;
  font-size: 13px;
}

.dropzone-text small {
  color: var(--muted);
  font-size: 11.5px;
}

.spinner {
  width: 22px;
  height: 22px;
  border: 3px solid #e2e8f0;
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  margin-left: auto;
}

.hidden {
  display: none !important;
}

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

.form-sections {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
}

.card h2,
.card summary {
  margin: 0 0 10px;
  font-size: 13.5px;
  font-weight: 700;
  color: #1e293b;
}

.card summary {
  margin-bottom: 0;
  cursor: pointer;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.card-header h2 {
  margin: 0;
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.grid-3 {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 10px;
}

.col-span-2 {
  grid-column: span 2;
}

.mt-12 {
  margin-top: 12px;
}

label {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

label span {
  font-size: 11.5px;
  font-weight: 600;
  color: #475569;
}

input,
textarea {
  width: 100%;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 12.5px;
  font-family: inherit;
  color: var(--text);
  background: #ffffff;
}

input:focus,
textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(79, 38, 130, 0.14);
}

input[readonly] {
  background: #f8fafc;
  color: #475569;
}

/* Line Items Editor */
.items-editor {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.item-card {
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  padding: 10px;
  background: #f8fafc;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.item-card-top {
  display: flex;
  gap: 8px;
  align-items: end;
}

.item-card-top label {
  flex: 1;
}

.remove-item-btn {
  border: 1px solid #fecaca;
  background: #fff1f2;
  color: #be123c;
  border-radius: 6px;
  padding: 6px 9px;
  font-size: 12px;
  cursor: pointer;
}

.item-card-nums {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
}

.raw-ocr {
  margin: 10px 0 0;
  padding: 10px;
  background: #0f172a;
  color: #e2e8f0;
  border-radius: 6px;
  font-size: 11px;
  max-height: 220px;
  overflow: auto;
  white-space: pre-wrap;
}

/* Right Preview Panel & A4 Sheet */
.preview-panel {
  display: flex;
  justify-content: center;
  overflow-x: auto;
}

.a4-sheet {
  width: 210mm;
  min-height: 297mm;
  background: #ffffff;
  padding: 9mm 12mm 12mm;
  box-shadow: 0 8px 28px rgba(15, 23, 42, 0.14);
  font-family: 'Times New Roman', Times, serif;
  color: #000000;
  font-size: 11pt;
  line-height: 1.25;
}

.inv-banner img {
  width: 100%;
  height: auto;
  display: block;
}

.inv-title {
  text-align: center;
  font-size: 15pt;
  font-weight: 700;
  text-decoration: underline;
  margin: 10px 0 8px;
  letter-spacing: 0.4px;
}

.inv-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
  margin-bottom: 7px;
}

.inv-table td,
.inv-table th {
  padding: 3px 6px;
  font-size: 10.5pt;
  word-wrap: break-word;
}

.inv-meta-top td,
.inv-delivery td,
.inv-dispatch td,
.inv-words td {
  border: 1px solid #000000;
}

.inv-meta-top td:nth-child(2) {
  border-right: none;
}

.inv-meta-top td:nth-child(3) {
  border-left: none;
}

.inv-delivery td {
  border-left: none;
  border-right: none;
}

.inv-delivery td:first-child {
  border-left: 1px solid #000000;
}

.inv-delivery td:last-child {
  border-right: 1px solid #000000;
}

.inv-parties {
  border: 1px solid #000000;
}

.inv-parties td {
  border: none;
  padding: 2.5px 6px;
}

.inv-parties td.v-divider {
  border-left: 1px solid #000000;
}

.inv-delivery td.v-divider {
  border-left: 1px solid #000000;
}

.inv-dispatch td {
  height: 36px;
}

.inv-items th,
.inv-items td {
  border-left: 1px solid #000000;
  border-right: 1px solid #000000;
}

.inv-items thead th {
  border-top: 1px solid #000000;
  border-bottom: 1px solid #000000;
  text-align: center;
  font-weight: 700;
  padding: 6px 5px;
}

.inv-items tbody td {
  padding: 6px 6px;
  vertical-align: top;
}

.inv-items tbody tr.spacer-row td {
  height: 38px;
}

.inv-items tfoot td {
  border: 1px solid #000000;
  padding: 3.5px 6px;
}

.inv-words {
  margin-top: 8px;
}

.inv-words td {
  padding: 4px 6px;
}

.inv-terms {
  margin-top: 14px;
  text-align: center;
  font-size: 10pt;
  line-height: 1.35;
  padding: 0 18px;
}

.inv-cheque {
  margin-top: 14px;
  font-size: 10pt;
  padding-left: 28px;
}

.inv-signatures {
  margin-top: 48px;
  display: flex;
  justify-content: space-between;
  padding: 0 8px;
}

.sig-box {
  width: 155px;
  border-top: 1px solid #000000;
  text-align: center;
  padding-top: 4px;
  font-size: 10.5pt;
}

.align-top {
  vertical-align: top;
}

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

.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.bold {
  font-weight: 700;
}

.email-link {
  color: #0563c1;
  text-decoration: underline;
}

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

@media print {
  body {
    background: #ffffff;
  }

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

  .workspace {
    display: block;
    padding: 0;
    margin: 0;
  }

  .preview-panel {
    display: block;
    overflow: visible;
  }

  .a4-sheet {
    width: 100%;
    min-height: auto;
    padding: 2mm 4mm;
    box-shadow: none;
  }
}
