:root {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --surface-2: #f9fafb;
  --text: #111827;
  --text-secondary: #4b5563;
  --muted: #6b7280;
  --line: #e5e7eb;
  --line-strong: #d1d5db;
  --accent: #1d4ed8;
  --accent-hover: #1e40af;
  --accent-soft: #eff6ff;
  --danger: #b91c1c;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.04);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.06);
  --radius: 8px;
  --radius-lg: 10px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

.container {
  max-width: 1120px;
  margin: 0 auto;
  padding: 0 20px;
}

/* Header */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  box-shadow: var(--shadow-sm);
}

.topbar__inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 0;
  gap: 16px;
}

.topbar__actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand__logo {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: var(--radius);
  object-fit: contain;
  display: block;
}

.brand__title {
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

/* Layout */
.layout {
  display: grid;
  grid-template-columns: 1fr 0.92fr;
  gap: 20px;
  padding: 20px 20px 32px;
  align-items: start;
}

@media (max-width: 960px) {
  .layout {
    grid-template-columns: 1fr;
  }
}

/* Form panel */
.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: var(--shadow-sm);
}

.panel__header {
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--line);
}

.panel__title {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.panel__hint {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}

.grid2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 12px;
}

@media (max-width: 720px) {
  .grid2 {
    grid-template-columns: 1fr;
  }
}

.card {
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
}

.card__title {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-secondary);
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

/* Fields */
.fields {
  display: grid;
  gap: 10px;
}

.field {
  display: grid;
  gap: 4px;
}

.field__label {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-secondary);
}

.field__input {
  width: 100%;
  padding: 8px 10px;
  border-radius: 6px;
  background: var(--surface);
  border: 1px solid var(--line-strong);
  color: var(--text);
  font: inherit;
  font-size: 13px;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.field__input:hover {
  border-color: #9ca3af;
}

.field__input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.field__input--area {
  resize: vertical;
  min-height: 72px;
}

.field__input--customer {
  min-height: 96px;
}

.field__input--readonly {
  background: var(--surface-2);
  color: var(--text-secondary);
  cursor: default;
}

.seg {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

/* Buttons */
.btn {
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 6px;
  cursor: pointer;
  font: inherit;
  font-size: 13px;
  font-weight: 500;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.btn:hover {
  background: var(--surface-2);
  border-color: #9ca3af;
}

.btn:active {
  background: var(--line);
}

.btn--primary {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

.btn--primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.btn--ghost {
  background: transparent;
  border-color: transparent;
}

.btn--ghost:hover {
  background: var(--surface-2);
  border-color: var(--line);
}

.btn--small {
  padding: 6px 10px;
  font-size: 12px;
}

/* Items table (editor) */
.table {
  margin-top: 4px;
}

.table__head,
.table__row {
  display: grid;
  grid-template-columns: 1.4fr 0.45fr 0.65fr 0.65fr 36px;
  gap: 8px;
  align-items: center;
}

.table__head {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
  padding: 6px 4px;
  border-bottom: 1px solid var(--line);
}

.table__body {
  display: grid;
  gap: 8px;
  margin-top: 8px;
}

.table__row {
  padding: 0;
  border: none;
  background: transparent;
}

.table__row .field__input {
  padding: 7px 8px;
  font-size: 12px;
}

.iconBtn {
  width: 32px;
  height: 32px;
  border-radius: 6px;
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--muted);
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
  display: grid;
  place-items: center;
  transition: color 0.15s ease, border-color 0.15s ease, background 0.15s ease;
}

.iconBtn:hover {
  color: var(--danger);
  border-color: #fecaca;
  background: #fef2f2;
}

.right {
  text-align: right;
}

/* Preview */
.preview {
  position: sticky;
  top: 62px;
  align-self: start;
}

@media (max-width: 960px) {
  .preview {
    position: static;
  }
}

.preview__label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin-bottom: 8px;
}

/* Invoice document */
.invoice {
  background: #fff;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  overflow: visible;
  font-size: 13px;
}

.invoice__top {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  padding: 24px 24px 16px;
  border-bottom: 2px solid var(--text);
}

.invoice__from {
  display: flex;
  gap: 12px;
  min-width: 0;
  flex: 1;
}

.logoWrap {
  flex-shrink: 0;
  width: 52px;
  height: 52px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: var(--surface-2);
  display: grid;
  place-items: center;
  overflow: hidden;
}

.logo {
  max-width: 100%;
  max-height: 100%;
  display: none;
  object-fit: contain;
}

.fromBlock {
  min-width: 0;
}

.invoice__meta {
  text-align: right;
  flex-shrink: 0;
}

.invoice__title {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 0.06em;
  color: var(--text);
  line-height: 1.2;
}

.meta {
  margin-top: 12px;
  display: grid;
  gap: 4px;
  font-size: 12px;
}

.meta__row {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

.meta__row span {
  color: var(--muted);
  min-width: 4.5em;
  text-align: right;
}

.meta__row strong {
  font-weight: 600;
  min-width: 5em;
  text-align: right;
}

.billto {
  padding: 18px 24px 22px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-height: 5.5rem;
  overflow: visible;
}

.billto__label {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 2px;
  flex-shrink: 0;
}

.billto .h2 {
  margin: 0;
}

.billto #toAddressPreview,
.billto #toContactPreview {
  display: block;
  line-height: 1.55;
  word-break: break-word;
  overflow-wrap: anywhere;
  overflow: visible;
  min-height: 1.35em;
}

.billto #toAddressPreview:empty,
.billto #toContactPreview:empty {
  display: none;
  min-height: 0;
}

.h1 {
  font-size: 15px;
  font-weight: 600;
  line-height: 1.35;
}

.h2 {
  font-size: 14px;
  font-weight: 600;
  line-height: 1.35;
}

.muted {
  color: var(--muted);
}

.preline {
  white-space: pre-line;
}

.invoiceTable {
  padding: 16px 24px;
}

.invoiceTable__head,
.invoiceTable__row {
  display: grid;
  grid-template-columns: 1.5fr 0.5fr 0.75fr 0.75fr;
  gap: 12px;
  align-items: start;
}

.invoiceTable__head {
  padding: 8px 0;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  border-bottom: 1px solid var(--line-strong);
}

.invoiceTable__body {
  display: grid;
  gap: 0;
}

.invoiceTable__row {
  font-size: 13px;
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
}

.invoiceTable__row:last-child {
  border-bottom: none;
}

.totals {
  padding: 12px 24px 16px;
  margin-left: auto;
  width: min(100%, 280px);
  display: grid;
  gap: 6px;
}

.totals__row {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  font-size: 13px;
}

.totals__row--grand {
  padding-top: 10px;
  margin-top: 4px;
  border-top: 2px solid var(--text);
  font-size: 15px;
  font-weight: 700;
}

.totals__row--grand span,
.totals__row--grand strong {
  font-weight: 700;
}

.notes {
  padding: 14px 24px 20px;
  border-top: 1px solid var(--line);
  font-size: 12px;
  background: var(--surface-2);
}

.notes .muted {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 4px;
  display: block;
}

.preview__footer {
  margin-top: 10px;
}

.hint {
  padding: 10px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}

.usage-stats {
  margin-top: 8px;
  padding: 10px 0 16px;
  border-top: 1px solid var(--line);
  background: var(--surface);
}

.usage-stats__inner {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 8px 12px;
  font-size: 12px;
  color: var(--muted);
}

.usage-stats__item strong {
  color: var(--text);
  font-weight: 600;
  margin-left: 4px;
}

.usage-stats__sep {
  opacity: 0.5;
}

/* RTL */
body.rtl .topbar__inner,
body.rtl .brand,
body.rtl .topbar__actions,
body.rtl .card__title.row,
body.rtl .meta__row,
body.rtl .totals__row {
  flex-direction: row-reverse;
}

body.rtl .invoice__meta {
  text-align: left;
}

body.rtl .right {
  text-align: left;
}

body.rtl .invoice {
  direction: rtl;
}

body.rtl .invoice__top {
  flex-direction: row-reverse;
}

body.rtl .invoice__from {
  flex-direction: row-reverse;
}

body.rtl .meta__row span,
body.rtl .meta__row strong {
  text-align: left;
}

body.rtl .table__head,
body.rtl .table__row,
body.rtl .invoiceTable__head,
body.rtl .invoiceTable__row {
  direction: rtl;
}

body.rtl .totals {
  margin-left: 0;
  margin-right: auto;
}

/* Print */
@media print {
  html,
  body {
    height: auto !important;
    min-height: 0 !important;
    overflow: visible !important;
    background: #fff;
  }

  .no-print {
    display: none !important;
  }

  .layout {
    display: block;
    padding: 0;
  }

  .container {
    max-width: none;
    padding: 0;
  }

  .preview {
    position: static;
    overflow: visible;
  }

  .preview__label {
    display: none;
  }

  .invoice {
    box-shadow: none;
    border: none;
    border-radius: 0;
    overflow: visible;
  }

  .invoice__top,
  .billto,
  .invoiceTable,
  .totals,
  .notes {
    break-inside: avoid;
    page-break-inside: avoid;
  }

  .billto {
    padding: 14px 0 18px;
    min-height: auto;
    background: transparent;
    border-bottom: 1px solid #d1d5db;
  }

  .billto #toAddressPreview,
  .billto #toContactPreview {
    color: #374151 !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  @page {
    margin: 14mm;
  }
}
