:root {
  --ink: #262626;
  --muted: #8a633b;
  --accent: #9f4f21;
  --accent-2: #cfa15f;
  --line: #b85558;
  --fill: #f6d2af;
  --paper: #ffffff;
  --surface: #f7f4ef;
  --field: #ffffff;
  --editor-width: clamp(435px, 34vw, 470px);
  font-family: Arial, Helvetica, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  color: var(--ink);
  background: var(--surface);
}

body.auth-locked {
  overflow: hidden;
}

button,
input,
textarea,
select {
  font: inherit;
}

.app-shell {
  display: grid;
  grid-template-columns: var(--editor-width) minmax(760px, 1fr);
  gap: 20px;
  min-height: 100vh;
  padding: 20px;
}

.editor {
  align-self: start;
  position: sticky;
  top: 20px;
  display: grid;
  gap: 16px;
  max-height: calc(100vh - 40px);
  overflow: auto;
  padding: 18px;
  border: 1px solid #ded6ce;
  border-radius: 8px;
  background: #fffdf9;
  box-shadow: 0 10px 30px rgba(64, 45, 29, 0.08);
}

.editor-header {
  display: grid;
  gap: 8px;
}

.editor h1 {
  margin: 0;
  color: var(--accent);
  font-size: 1.35rem;
  line-height: 1.2;
}

.status-line {
  display: none;
  margin: 0;
  color: #675a4c;
  font-size: 0.8rem;
  font-weight: 700;
}

.status-line.is-error {
  color: #9a2a1d;
}

.status-line.is-ready {
  color: #2f6b35;
}

.auth-overlay {
  position: fixed;
  inset: 0;
  z-index: 10000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(74, 55, 39, 0.4);
  backdrop-filter: blur(3px);
}

.auth-card {
  width: min(100%, 360px);
  display: grid;
  gap: 12px;
  padding: 22px;
  border: 1px solid #d8cabc;
  border-radius: 10px;
  background: #fffdf9;
  box-shadow: 0 14px 34px rgba(64, 45, 29, 0.18);
}

.auth-card h2 {
  margin: 0;
  color: var(--accent);
  font-size: 1.15rem;
  line-height: 1.2;
}

.auth-card p {
  margin: 0;
  color: #6b6259;
  font-size: 0.88rem;
  line-height: 1.4;
}

.auth-card form {
  display: grid;
  gap: 10px;
}

.auth-card label {
  font-size: 0.78rem;
}

.auth-card input {
  min-height: 38px;
}

.auth-error {
  min-height: 18px;
  color: #9a2a1d;
  font-size: 0.8rem;
  font-weight: 700;
}

.editor-section {
  display: grid;
  gap: 10px;
  padding-top: 14px;
  border-top: 1px solid #e6ded7;
}

.section-title {
  margin: 0;
  color: #6c4b2d;
  font-size: 0.82rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.field-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 10px;
}

.field-grid > label:nth-child(1),
.field-grid > label:nth-child(2) {
  grid-column: span 3;
}

.field-grid > label:nth-child(3),
.field-grid > label:nth-child(4) {
  grid-column: span 2;
}

.compact-document-grid {
  grid-template-columns: 1.05fr 0.95fr 0.8fr 1.05fr;
  align-items: end;
}

.compact-document-grid > label:nth-child(1),
.compact-document-grid > label:nth-child(2),
.compact-document-grid > label:nth-child(3),
.compact-document-grid > label:nth-child(4) {
  grid-column: auto;
}

.compact-document-grid label {
  font-size: 0.8rem;
}

label {
  display: grid;
  gap: 5px;
  color: #513c2d;
  font-size: 0.82rem;
  font-weight: 700;
}

input,
textarea,
select {
  width: 100%;
  min-height: 36px;
  border: 1px solid #cfc3b8;
  border-radius: 6px;
  background: var(--field);
  color: var(--ink);
  padding: 8px 9px;
}

textarea {
  resize: vertical;
}

.line-editor {
  display: grid;
  gap: 8px;
}

.line-card {
  display: grid;
  grid-template-columns: 1.3fr 1fr 1.3fr 1.8fr 32px;
  gap: 8px;
  align-items: end;
  padding: 10px;
  border: 1px solid #e2d9d1;
  border-radius: 8px;
  background: #ffffff;
}

.line-card .item-field {
  grid-column: 1 / -1;
}

.line-card label {
  font-size: 0.75rem;
}

.line-card input {
  min-height: 34px;
  padding: 7px 8px;
}

.line-card select {
  min-height: 34px;
  padding: 7px 8px;
}

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

.money-input {
  text-align: right;
}

.icon-button,
.action-button {
  border: 0;
  border-radius: 5px;
  cursor: pointer;
  font-weight: 800;
}

.icon-button {
  width: 28px;
  height: 30px;
  background: #eee5dc;
  color: #6f3324;
}

.action-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.action-button {
  min-height: 30px;
  padding: 5px 10px;
  background: var(--accent);
  color: white;
  font-size: 0.8rem;
  line-height: 1.1;
}

.action-button.secondary {
  border: 1px solid #c9b49f;
  background: #ffffff;
  color: #73451f;
}

.action-button.danger {
  color: #8e2d20;
}

.action-button:disabled,
.icon-button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  clip-path: inset(50%);
}

.preview-wrap {
  display: flex;
  justify-content: center;
  overflow: auto;
}

body.stacked-layout .app-shell {
  grid-template-columns: 1fr;
  gap: 16px;
  max-width: 1120px;
  margin: 0 auto;
}

body.stacked-layout .editor {
  position: static;
  max-height: none;
  width: min(100%, 1120px);
  margin: 0 auto;
}

body.stacked-layout .preview-wrap {
  justify-content: center;
  overflow: auto;
}

body.stacked-layout .top-toolbar {
  display: grid;
  grid-template-columns: 1fr 1.15fr;
  gap: 14px;
}

body.stacked-layout .top-toolbar-card {
  align-content: start;
  padding: 14px 14px 0;
  border: 1px solid #e6ded7;
  border-radius: 8px;
  background: #fffefa;
}

body.stacked-layout .entry-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1.15fr;
  gap: 14px;
}

body.stacked-layout .entry-card {
  align-content: start;
  min-height: 100%;
  padding: 14px 14px 0;
  border: 1px solid #e6ded7;
  border-radius: 8px;
  background: #fffefa;
}

body.stacked-layout .entry-card .section-title {
  margin-bottom: 2px;
}

body.stacked-layout .document-row {
  display: grid;
  grid-template-columns: 1.05fr 1fr 0.85fr 0.95fr;
  gap: 8px;
}

body.stacked-layout .document-row label {
  font-size: 0.76rem;
}

body.stacked-layout .document-row input {
  min-height: 34px;
}

body.stacked-layout .action-footer {
  padding-top: 12px;
}

body.stacked-layout .footer-actions {
  justify-content: flex-start;
}

body.stacked-layout .compact-actions {
  margin-top: 4px;
}

.invoice-page {
  width: 8.5in;
  min-height: 11in;
  padding: 0.46in 0.42in 0.34in;
  background: var(--paper);
  box-shadow: 0 16px 45px rgba(54, 35, 24, 0.16);
  display: flex;
  flex-direction: column;
}

.logo-row {
  display: flex;
  justify-content: center;
}

.logo {
  width: 3.5in;
  max-width: 42%;
  height: auto;
}

.top-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.45in;
  margin-top: 0.1in;
}

.business-block,
.customer-block {
  color: var(--accent);
  font-size: 14px;
  font-weight: 700;
  line-height: 1.42;
}

.business-block {
  color: var(--accent);
  font-size: 14px;
  font-weight: 700;
  line-height: 1.28;
}

.customer-block {
  white-space: pre-line;
}

.business-block .soft {
  color: var(--accent-2);
  font-style: italic;
}

.business-block .business-name,
.business-block .business-tagline,
.business-block .business-address,
.business-block .business-phone-row {
  display: block;
  margin: 0;
}

.business-block .business-address {
  white-space: pre-line;
}

.title-block {
  display: grid;
  align-content: start;
  justify-items: end;
  color: var(--accent);
}

.document-title {
  margin: 0 0 0.58in;
  font-size: 21px;
  font-weight: 900;
  letter-spacing: 0.01em;
}

.meta-line {
  display: grid;
  grid-template-columns: auto auto;
  gap: 8px;
  justify-content: end;
  margin: 3px 0;
  font-size: 14px;
  font-weight: 800;
}

.meta-line span:last-child {
  color: #7f3e1e;
}

.customer-block {
  min-height: 1.2in;
  margin-top: 0.24in;
}

.comments {
  margin-top: 0.1in;
  min-height: 0.34in;
  color: var(--accent);
  font-size: 13px;
  font-weight: 800;
  width: 100%;
}

.comments .comment-text {
  display: block;
  width: 100%;
  margin-top: 4px;
  color: var(--ink);
  font-weight: 500;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.invoice-table {
  width: 100%;
  margin-top: 0.12in;
  border-collapse: collapse;
  table-layout: fixed;
  font-size: 13px;
}

.invoice-table th,
.invoice-table td {
  height: 0.28in;
  border: 2px solid var(--line);
  padding: 5px 8px;
  vertical-align: middle;
}

.invoice-table th {
  background: var(--fill);
  color: #d7731f;
  font-weight: 900;
  text-align: center;
  -webkit-print-color-adjust: exact;
  print-color-adjust: exact;
}

.item-col {
  width: 49%;
}

.qty-col {
  width: 14%;
  text-align: center;
}

.unit-col {
  width: 10%;
  text-align: center;
}

.rate-col {
  width: 13%;
}

.amount-col {
  width: 14%;
}

.money-cell {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 3px;
  align-items: baseline;
  width: 100%;
}

.money-cell .currency {
  text-align: left;
}

.money-cell .number {
  text-align: right;
}

.totals {
  display: grid;
  grid-template-columns: 1fr 3.66in;
  margin-top: 0;
}

.totals-table {
  grid-column: 2;
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
  font-size: 14px;
}

.totals-table td {
  height: 0.33in;
  border: 2px solid var(--line);
  padding: 6px 8px;
}

.totals-table td:first-child {
  width: 62%;
  color: var(--accent-2);
  font-weight: 900;
  text-align: right;
}

.totals-table .due-row td {
  background: #f3bf94;
  color: var(--accent);
  font-size: 16px;
  font-weight: 900;
  -webkit-print-color-adjust: exact;
  print-color-adjust: exact;
}

.footer {
  margin-top: auto;
  padding-top: 0.34in;
  display: grid;
  gap: 0.13in;
  text-align: center;
}

.footer strong {
  color: var(--accent);
  font-size: 18px;
}

.footer span {
  color: var(--accent-2);
  font-size: 13px;
  font-weight: 700;
}

.footer b {
  color: var(--muted);
  font-size: 14px;
}

@media (max-width: 1180px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .editor {
    position: static;
    max-height: none;
  }

  body.stacked-layout .entry-grid {
    grid-template-columns: 1fr;
  }

  body.stacked-layout .top-toolbar {
    grid-template-columns: 1fr;
  }

  body.stacked-layout .document-row {
    grid-template-columns: 1fr 1fr;
  }
}

.save-hint {
  display: none;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #2d5a27;
  color: white;
  padding: 14px 22px;
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 700;
  z-index: 9999;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
  white-space: nowrap;
  text-align: center;
}

@media (max-width: 760px) {
  .app-shell {
    padding: 10px;
  }

  .line-card {
    grid-template-columns: 1fr;
  }

  .line-card .item-field {
    grid-column: auto;
  }

  .line-card .icon-button {
    width: 100%;
  }

  .invoice-page {
    width: 100%;
    min-width: 720px;
  }
}

@media print {
  @page {
    size: letter;
    margin: 0 0 0.25in 0;
  }

  body {
    background: white;
  }

  .editor {
    display: none;
  }

  .save-hint {
    display: none !important;
  }

  .app-shell {
    display: block;
    min-height: auto;
    padding: 0;
    max-width: none !important;
    margin: 0 !important;
  }

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

  .invoice-page {
    width: 8.5in;
    min-height: auto;
    padding-bottom: 0;
    box-shadow: none;
    page-break-after: avoid;
  }

  .footer {
    margin-top: 0.18in;
    padding-top: 0.16in;
    gap: 0.08in;
  }
}
