:root {
  --bg-canvas: #F4F6F9;
  --bg-surface: #FFFFFF;
  --bg-subtle: #F8FAFC;
  --border-hairline: #D8DEE6;
  --border-strong: #B8C2D0;
  --ink-primary: #0F172A;
  --ink-secondary: #334155;
  --ink-muted: #526074;
  --accent-navy: #1B3B6F;
  --accent-navy-hover: #142D55;
  --font-sans: 'IBM Plex Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'IBM Plex Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --sheet-accent: #1B3B6F;
}

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

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg-canvas);
  color: var(--ink-primary);
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea { font-family: inherit; font-size: inherit; }
.tabular { font-variant-numeric: tabular-nums; }
.mono { font-family: var(--font-mono); }

.topbar {
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-hairline);
  padding: 10px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  flex-wrap: wrap;
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand-lockup { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  width: 28px; height: 28px; border-radius: 4px;
  background: var(--accent-navy); color: #FFFFFF;
  display: inline-flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 14px;
}
.brand-name { font-weight: 700; font-size: 15px; display: block; line-height: 1.2; }
.brand-tier { font-weight: 500; color: var(--accent-navy); font-size: 12px; }
.brand-note { font-size: 11.5px; color: var(--ink-muted); }

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

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 7px 13px; border-radius: 4px; border: 1px solid transparent;
  font-weight: 500; font-size: 13px; cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn-primary { background: var(--accent-navy); color: #FFFFFF; }
.btn-primary:hover { background: var(--accent-navy-hover); }
.btn-secondary { background: var(--bg-surface); color: var(--ink-primary); border-color: var(--border-hairline); }
.btn-secondary:hover { background: var(--bg-subtle); border-color: var(--border-strong); }
.btn-sm { padding: 6px 11px; font-size: 12.5px; }
.btn-xs { padding: 4px 8px; font-size: 12px; }
.file-btn { cursor: pointer; }

.icon-btn {
  background: transparent; border: 1px solid var(--border-hairline); border-radius: 4px;
  width: 28px; height: 28px; display: inline-flex; align-items: center; justify-content: center;
  cursor: pointer; color: var(--ink-secondary);
}
.icon-btn:hover { background: var(--bg-subtle); color: var(--ink-primary); }

.split-workspace {
  display: grid;
  grid-template-columns: 480px 1fr;
  min-height: calc(100vh - 53px);
  max-width: 100vw;
  overflow-x: hidden;
}

.editor-column {
  min-width: 0;
  padding: 16px 18px 36px;
  border-right: 1px solid var(--border-hairline);
  overflow-y: auto;
  max-height: calc(100vh - 53px);
}

.form-card {
  background: var(--bg-surface);
  border: 1px solid var(--border-hairline);
  border-radius: 4px;
  padding: 14px 15px;
  margin-bottom: 14px;
}

.form-card h2 {
  margin: 0 0 10px;
  font-size: 13px;
  font-weight: 600;
  color: var(--ink-primary);
}

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

.logo-inline-actions { display: flex; gap: 6px; }

.field { margin-bottom: 10px; }
.field:last-child { margin-bottom: 0; }
.field label { display: block; font-size: 12px; font-weight: 500; color: var(--ink-secondary); margin-bottom: 4px; }

.input {
  width: 100%; padding: 7px 10px;
  border: 1px solid var(--border-hairline); border-radius: 4px;
  background: var(--bg-surface); color: var(--ink-primary); font-size: 13px;
}
.input:focus { outline: none; border-color: var(--accent-navy); box-shadow: 0 0 0 2px rgba(27, 59, 111, 0.1); }
textarea.input { resize: vertical; }

.color-input {
  width: 100%; height: 33px; padding: 2px;
  border: 1px solid var(--border-hairline); border-radius: 4px; background: var(--bg-surface); cursor: pointer;
}

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

.items-list { display: flex; flex-direction: column; gap: 10px; }
.item-box {
  border: 1px solid var(--border-hairline); border-radius: 4px; padding: 10px; background: var(--bg-subtle);
}
.item-top { display: grid; grid-template-columns: 1fr auto; gap: 8px; margin-bottom: 7px; align-items: center; }
.item-actions { display: inline-flex; gap: 4px; }
.item-nums { display: grid; grid-template-columns: 80px 1fr 110px; gap: 8px; margin-top: 7px; align-items: end; }
.item-total {
  padding: 7px 9px; background: var(--bg-surface); border: 1px solid var(--border-hairline);
  border-radius: 4px; text-align: right; font-weight: 600; font-size: 13px;
}

.pro-callout {
  background: var(--bg-surface);
  border: 1px solid var(--border-hairline);
  border-left: 3px solid var(--accent-navy);
  border-radius: 4px;
  padding: 14px 15px;
}
.pro-callout-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 6px; font-size: 13.5px; }
.price-tag {
  font-family: var(--font-mono); font-size: 12px; font-weight: 600;
  background: #EEF2F8; color: var(--accent-navy); padding: 2px 7px; border-radius: 3px; white-space: nowrap;
}
.pro-callout p { margin: 0 0 12px; font-size: 12.5px; color: var(--ink-muted); }
.pro-callout-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* Preview */
.preview-column {
  min-width: 0;
  background: #E9EEF5;
  display: flex;
  flex-direction: column;
  max-height: calc(100vh - 53px);
  overflow: hidden;
}
.preview-bar {
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-hairline);
  padding: 9px 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12.5px;
}
.preview-bar-title { font-weight: 500; color: var(--ink-secondary); }
.save-status { font-size: 12px; color: #0F766E; background: #ECFEF9; border: 1px solid #B2F5EA; padding: 2px 8px; border-radius: 3px; }

.paper-stage {
  flex: 1;
  overflow: auto;
  padding: 24px;
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

.document-sheet {
  width: 210mm;
  min-height: 297mm;
  background: #FFFFFF;
  color: #0F172A;
  box-shadow: 0 8px 28px rgba(15, 23, 42, 0.11);
  border: 1px solid #DCE2EA;
  padding: 16mm 17mm;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  flex-shrink: 0;
}

.sheet-top-rule { height: 4px; background: var(--sheet-accent); margin: -16mm -17mm 14mm; }
.sheet-header {
  display: flex; justify-content: space-between; align-items: flex-start; gap: 24px;
  padding-bottom: 18px; border-bottom: 1px solid #CBD5E1;
}
.sheet-logo { max-height: 44px; max-width: 170px; object-fit: contain; display: block; margin-bottom: 6px; }
.sheet-biz-name { font-size: 19px; font-weight: 700; }
.sheet-biz-meta { font-size: 11.5px; color: #475569; line-height: 1.5; margin-top: 4px; white-space: pre-line; }
.sheet-doc-title-block { text-align: right; }
.sheet-doc-type { font-size: 26px; font-weight: 700; letter-spacing: -0.02em; color: var(--sheet-accent); line-height: 1.1; }
.sheet-doc-number { font-family: var(--font-mono); font-size: 13px; font-weight: 600; margin-top: 4px; }

.sheet-parties {
  display: grid; grid-template-columns: 1.2fr 1fr; gap: 24px;
  padding: 18px 0; border-bottom: 1px solid #E2E8F0;
}
.sheet-section-label {
  font-size: 10.5px; font-weight: 600; color: #64748B; text-transform: uppercase;
  letter-spacing: 0.06em; margin-bottom: 6px;
}
.sheet-client-company { font-size: 14.5px; font-weight: 700; }
.sheet-client-details { font-size: 12px; color: #334155; line-height: 1.5; white-space: pre-line; margin-top: 3px; }
.sheet-meta-table { width: 100%; border-collapse: collapse; font-size: 12px; }
.sheet-meta-table td { padding: 3px 0; }
.sheet-meta-table td:first-child { color: #64748B; }
.sheet-meta-table td:last-child { text-align: right; font-weight: 600; }

.sheet-items-table { width: 100%; border-collapse: collapse; margin-top: 18px; }
.sheet-items-table th {
  text-align: left; font-size: 11px; font-weight: 600; color: #475569;
  text-transform: uppercase; letter-spacing: 0.05em; padding: 9px 8px;
  border-top: 1px solid #0F172A; border-bottom: 1px solid #0F172A;
}
.sheet-items-table td { padding: 10px 8px; border-bottom: 1px solid #E2E8F0; vertical-align: top; font-size: 12.5px; }
.text-right { text-align: right !important; }
.sheet-item-title { font-weight: 600; }
.sheet-item-desc { font-size: 11.5px; color: #475569; margin-top: 2px; white-space: pre-line; }

.sheet-summary-grid {
  display: grid; grid-template-columns: 1.15fr 0.85fr; gap: 24px; margin-top: 20px; align-items: start;
}
.sheet-payment-box { font-size: 11.8px; color: #334155; line-height: 1.55; white-space: pre-line; }
.sheet-totals-table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.sheet-totals-table td { padding: 5px 0; }
.sheet-totals-table td:last-child { text-align: right; font-weight: 500; }
.sheet-totals-table tr.sheet-total-line td {
  padding-top: 9px; padding-bottom: 6px; border-top: 1.5px solid #0F172A; font-size: 14.5px; font-weight: 700;
}
.sheet-totals-table tr.sheet-balance-line td {
  padding: 8px 10px; background: var(--sheet-accent); color: #FFFFFF; font-weight: 700; font-size: 14px;
}
.sheet-footer {
  margin-top: 26px; padding-top: 12px; border-top: 1px solid #E2E8F0;
  display: flex; justify-content: space-between; font-size: 11px; color: #64748B;
}

/* Modal & Toast */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15, 23, 42, 0.45);
  display: flex; align-items: center; justify-content: center; padding: 20px; z-index: 50;
}
.modal-backdrop[hidden] { display: none; }
.modal-card {
  width: 100%; max-width: 560px; background: var(--bg-surface);
  border: 1px solid var(--border-hairline); border-radius: 6px; padding: 18px 20px;
  box-shadow: 0 18px 40px rgba(15, 23, 42, 0.2);
}
.modal-header { display: flex; align-items: center; justify-content: space-between; padding-bottom: 10px; margin-bottom: 12px; border-bottom: 1px solid var(--border-hairline); }
.modal-header h2 { margin: 0; font-size: 16px; }
.modal-footer { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

.compare-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.compare-table th, .compare-table td { padding: 8px 10px; border-bottom: 1px solid var(--border-hairline); text-align: left; }
.compare-table th { font-size: 12px; color: var(--ink-muted); background: var(--bg-subtle); }

.toast-container { position: fixed; right: 18px; bottom: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 60; }
.toast { background: var(--ink-primary); color: #FFFFFF; padding: 9px 14px; border-radius: 4px; font-size: 13px; box-shadow: 0 8px 20px rgba(15, 23, 42, 0.25); }

@media (max-width: 1120px) {
  .split-workspace { grid-template-columns: 1fr; }
  .editor-column, .preview-column { max-height: none; }
}

@media (max-width: 560px) {
  .grid-2, .grid-3, .item-nums { grid-template-columns: 1fr; }
  .paper-stage { padding: 12px; }
}

@page { size: A4; margin: 0; }
@media print {
  html, body { background: #FFFFFF !important; margin: 0 !important; padding: 0 !important; }
  .no-print { display: none !important; }
  .split-workspace, .preview-column, .paper-stage {
    display: block !important; margin: 0 !important; padding: 0 !important;
    background: #FFFFFF !important; max-height: none !important; overflow: visible !important;
  }
  .document-sheet {
    width: 210mm !important; min-height: 297mm !important; margin: 0 !important;
    border: none !important; box-shadow: none !important; transform: none !important;
    print-color-adjust: exact !important; -webkit-print-color-adjust: exact !important;
  }
}
