:root {
  --paper: #f8f5ed;
  --paper-deep: #efe8d8;
  --ink: #292724;
  --muted: #746e63;
  --line: #d8d0c2;
  --line-dark: #b9ad9c;
  --red: #b43a2c;
  --red-dark: #8e2a22;
  --red-pale: #f5e5df;
  --green: #3b685b;
  --white: #fffdf8;
  --shadow: 0 18px 48px rgba(48, 38, 25, 0.08);
  font-family: "Avenir Next", "Hiragino Sans", "Yu Gothic", YuGothic, sans-serif;
  color: var(--ink);
  background: var(--paper-deep);
  font-synthesis: none;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { min-width: 320px; background: var(--paper-deep); }

body {
  min-width: 320px;
  margin: 0;
  background:
    linear-gradient(rgba(90, 74, 54, 0.035) 1px, transparent 1px),
    linear-gradient(90deg, rgba(90, 74, 54, 0.025) 1px, transparent 1px),
    var(--paper-deep);
  background-size: 24px 24px;
  color: var(--ink);
  font-size: 16px;
  line-height: 1.5;
}

button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.52; }

:focus-visible {
  outline: 3px solid rgba(180, 58, 44, 0.45);
  outline-offset: 2px;
}

.sr-only {
  position: absolute;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.site-header {
  border-bottom: 1px solid rgba(91, 72, 49, 0.14);
  background: rgba(248, 245, 237, 0.86);
}

.header-inner,
.app-shell,
.site-footer {
  width: min(1440px, calc(100% - 48px));
  margin: 0 auto;
}

.header-inner {
  min-height: 218px;
  display: flex;
  justify-content: space-between;
  align-items: stretch;
  gap: 32px;
  padding: 40px 0 30px;
}

.brand-lockup { align-self: end; }

.eyebrow,
.section-kicker,
.document-eyebrow {
  margin: 0;
  color: var(--red);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.16em;
  line-height: 1.4;
}

.brand-lockup h1 {
  margin: 8px 0 3px;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(42px, 6vw, 76px);
  font-weight: 700;
  letter-spacing: -0.08em;
  line-height: 0.94;
}

.brand-lockup h1 span { color: var(--red); }
.brand-description { margin: 14px 0 0; color: var(--muted); font-size: 14px; }

.header-art {
  position: relative;
  width: min(310px, 32vw);
  overflow: hidden;
  border: 1px solid rgba(102, 78, 48, 0.16);
  background: #dfd6c7;
  box-shadow: 10px 10px 0 rgba(180, 58, 44, 0.08);
}

.header-art img { display: block; width: 100%; height: 148px; object-fit: cover; opacity: 0.88; mix-blend-mode: multiply; }
.stamp {
  position: absolute;
  right: 14px;
  bottom: 12px;
  display: grid;
  width: 48px;
  height: 48px;
  place-items: center;
  border: 2px solid var(--red);
  border-radius: 50%;
  color: var(--red);
  font-family: Georgia, serif;
  font-size: 8px;
  font-weight: 700;
  line-height: 1.1;
  opacity: 0.8;
  transform: rotate(-12deg);
}

.app-shell { padding: 34px 0 54px; }

.intro {
  display: flex;
  justify-content: space-between;
  align-items: end;
  gap: 24px;
  margin-bottom: 28px;
}

.intro h2,
.panel-heading h2 {
  margin: 5px 0 5px;
  font-family: Georgia, "Times New Roman", serif;
  font-weight: 700;
  letter-spacing: -0.03em;
}

.intro h2 { font-size: clamp(28px, 3.4vw, 42px); }
.intro p:not(.section-kicker) { max-width: 670px; margin: 0; color: var(--muted); font-size: 14px; }

.intro-note {
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: 350px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}

.note-mark,
.error-icon {
  display: inline-grid;
  flex: 0 0 auto;
  width: 24px;
  height: 24px;
  place-items: center;
  border-radius: 50%;
  background: var(--red);
  color: white;
  font-weight: 800;
}

.workspace { display: grid; grid-template-columns: minmax(0, 1.04fr) minmax(430px, 0.96fr); gap: 26px; align-items: start; }
.editor-panel,
.preview-panel { min-width: 0; }

.editor-panel {
  padding: 27px 28px 30px;
  border: 1px solid var(--line);
  background: rgba(255, 253, 248, 0.74);
  box-shadow: var(--shadow);
}

.preview-panel { position: sticky; top: 18px; }
.panel-heading { display: flex; justify-content: space-between; align-items: start; gap: 16px; margin-bottom: 22px; }
.panel-heading h2 { font-size: 27px; }
.step-badge { display: inline-grid; width: 34px; height: 34px; place-items: center; border: 1px solid var(--line-dark); color: var(--muted); font-size: 12px; font-weight: 800; letter-spacing: 0.08em; }
.step-badge-red { border-color: rgba(180, 58, 44, 0.42); color: var(--red); }

.sample-strip {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 15px;
  margin: 0 0 26px;
  padding: 14px 16px;
  border-left: 3px solid var(--red);
  background: var(--red-pale);
}

.sample-copy h3 { margin: 0 0 2px; font-size: 14px; }
.sample-copy p { margin: 0; color: var(--muted); font-size: 12px; }
.sample-actions { display: flex; flex-wrap: wrap; gap: 7px; justify-content: flex-end; }

.form-section { min-width: 0; margin: 0 0 26px; padding: 0; border: 0; }
.form-section > legend { width: 100%; margin-bottom: 13px; padding: 0; border-bottom: 1px solid var(--line); font-size: 15px; font-weight: 800; }
.legend-note { margin-left: 8px; color: var(--muted); font-size: 11px; font-weight: 500; }
.form-grid { display: grid; gap: 16px; }
.two-up { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.field { min-width: 0; }
.field-wide { grid-column: 1 / -1; }
.field label { display: block; margin-bottom: 6px; font-size: 13px; font-weight: 700; }
.required { color: var(--red); font-size: 10px; font-weight: 800; letter-spacing: 0.05em; }
.optional { color: var(--muted); font-size: 10px; font-weight: 500; }

input,
select,
textarea {
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--line-dark);
  border-radius: 0;
  background: rgba(255, 253, 248, 0.96);
  color: var(--ink);
  font-size: 16px;
  line-height: 1.35;
  transition: border-color 140ms ease, box-shadow 140ms ease, background 140ms ease;
}

input, select { padding: 9px 11px; }
textarea { min-height: 88px; padding: 10px 11px; resize: vertical; }
input::placeholder, textarea::placeholder { color: #a39a8d; opacity: 1; }
input:hover, select:hover, textarea:hover { background: #fffefb; }
input:focus, select:focus, textarea:focus { border-color: var(--red); box-shadow: 0 0 0 3px rgba(180, 58, 44, 0.12); outline: none; }
input[aria-invalid="true"], select[aria-invalid="true"], textarea[aria-invalid="true"] { border-color: var(--red); background: #fff8f5; }

.input-with-unit { position: relative; }
.input-with-unit input { padding-right: 38px; }
.input-with-unit span { position: absolute; top: 50%; right: 12px; color: var(--muted); font-size: 13px; transform: translateY(-50%); }
.helper-text { margin: 10px 0 0; color: var(--muted); font-size: 12px; }
.input-error { display: none; margin-top: 4px; color: var(--red-dark); font-size: 11px; line-height: 1.35; }
.input-error.is-visible { display: block; }

.table-wrap { max-width: 100%; overflow-x: auto; }
.editor-table-wrap { border: 1px solid var(--line); }
.line-editor, .preview-lines { width: 100%; border-collapse: collapse; }
.line-editor { min-width: 700px; }
.line-editor th { padding: 9px 7px; border-bottom: 1px solid var(--line); background: #f1ebdf; color: var(--muted); font-size: 11px; font-weight: 800; text-align: left; white-space: nowrap; }
.line-editor td { position: relative; padding: 6px; border-bottom: 1px solid rgba(216, 208, 194, 0.7); vertical-align: top; }
.line-editor tr:last-child td { border-bottom: 0; }
.line-input { min-height: 44px; padding: 8px; font-size: 16px; }
.col-name, .cell-name { width: 25%; }
.col-quantity, .cell-quantity { width: 10%; }
.col-unit, .cell-unit { width: 10%; }
.col-price, .cell-unitPrice { width: 15%; }
.col-tax, .cell-taxRate { width: 11%; }
.col-note, .cell-note { width: 25%; }
.col-remove, .cell-remove { width: 34px; }
.table-input-error { max-width: 150px; }
.remove-line { display: inline-grid; width: 44px; height: 44px; place-items: center; border: 0; background: transparent; color: var(--muted); font-size: 23px; line-height: 1; }
.remove-line:hover { color: var(--red); background: var(--red-pale); }
.line-editor-footer { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 10px; }
.line-count { color: var(--muted); font-size: 12px; }

.button { display: inline-flex; min-height: 44px; align-items: center; justify-content: center; border: 1px solid transparent; border-radius: 0; padding: 9px 14px; font-size: 14px; font-weight: 800; line-height: 1.2; transition: background 140ms ease, border-color 140ms ease, color 140ms ease, transform 140ms ease; }
.button:hover:not(:disabled) { transform: translateY(-1px); }
.button-primary { background: var(--red); color: white; }
.button-primary:hover:not(:disabled) { background: var(--red-dark); }
.button-outline { border-color: var(--red); background: transparent; color: var(--red-dark); }
.button-outline:hover:not(:disabled) { background: var(--red-pale); }
.button-quiet { min-height: 44px; border-color: rgba(180, 58, 44, 0.35); background: rgba(255, 253, 248, 0.55); color: var(--red-dark); padding: 9px 10px; font-size: 12px; }
.button-quiet:hover:not(:disabled) { border-color: var(--red); background: white; }

.error-summary { margin: -7px 0 20px; padding: 13px 15px; border: 1px solid rgba(180, 58, 44, 0.32); background: #fff3ef; color: var(--red-dark); }
.error-summary-heading { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.error-icon { width: 20px; height: 20px; font-size: 12px; }
.error-summary ul { margin: 8px 0 0 28px; padding: 0; font-size: 12px; }
.error-summary a { color: var(--red-dark); text-decoration: underline; text-underline-offset: 2px; }
.form-actions { display: flex; flex-wrap: wrap; gap: 10px; padding-top: 4px; }
.form-actions .button { flex: 1 1 210px; }
.action-status { min-height: 22px; margin: 12px 0 0; color: var(--muted); font-size: 12px; }

.stale-banner { margin: 0 0 12px; padding: 9px 12px; border: 1px solid rgba(180, 58, 44, 0.3); background: var(--red-pale); color: var(--red-dark); font-size: 12px; }
.document-preview { min-height: 720px; padding: 37px 39px; border: 1px solid var(--line); background: var(--white); box-shadow: var(--shadow); }
.preview-placeholder { min-height: 645px; display: grid; align-content: center; justify-items: center; padding: 30px; text-align: center; color: var(--muted); }
.placeholder-mark { display: grid; width: 58px; height: 58px; place-items: center; margin-bottom: 13px; border: 1px solid var(--line-dark); color: var(--red); font-size: 32px; }
.preview-placeholder h3 { margin: 0 0 5px; font-family: Georgia, serif; font-size: 21px; color: var(--ink); }
.preview-placeholder p { margin: 0; font-size: 13px; }
.document-header { display: flex; justify-content: space-between; gap: 24px; padding-bottom: 23px; border-bottom: 2px solid var(--ink); }
.document-header h2 { margin: 7px 0 0; font-family: Georgia, serif; font-size: 33px; letter-spacing: 0.04em; }
.document-meta { min-width: 170px; font-size: 11px; }
.document-meta div { display: flex; justify-content: space-between; gap: 12px; padding: 4px 0; border-bottom: 1px solid var(--line); }
.document-meta span { color: var(--muted); }
.document-meta strong { font-weight: 700; text-align: right; }
.document-parties { display: flex; justify-content: space-between; gap: 22px; padding: 26px 0 20px; }
.recipient-block, .issuer-block { display: grid; gap: 5px; }
.issuer-block { text-align: right; }
.label { color: var(--muted); font-size: 10px; font-weight: 800; letter-spacing: 0.1em; }
.recipient-block strong, .issuer-block strong { font-size: 15px; white-space: pre-wrap; }
.document-subject { display: flex; gap: 12px; align-items: baseline; margin-bottom: 16px; padding: 10px 12px; border-left: 3px solid var(--red); background: #f8f3ea; }
.document-subject span { color: var(--muted); font-size: 11px; font-weight: 800; }
.document-subject strong { font-size: 14px; overflow-wrap: anywhere; }
.preview-table-wrap { border-top: 2px solid var(--ink); border-bottom: 1px solid var(--ink); }
.preview-lines { table-layout: fixed; }
.preview-lines th { padding: 9px 6px; background: #f1ebdf; color: var(--muted); font-size: 10px; font-weight: 800; text-align: left; }
.preview-lines td { padding: 9px 6px; border-top: 1px solid var(--line); font-size: 11px; vertical-align: top; overflow-wrap: anywhere; }
.preview-lines th:nth-child(1), .preview-lines td:nth-child(1) { width: 35%; }
.preview-lines th:nth-child(2), .preview-lines td:nth-child(2) { width: 10%; text-align: right; }
.preview-lines th:nth-child(3), .preview-lines td:nth-child(3) { width: 10%; }
.preview-lines th:nth-child(4), .preview-lines td:nth-child(4) { width: 15%; text-align: right; }
.preview-lines th:nth-child(5), .preview-lines td:nth-child(5) { width: 10%; text-align: right; }
.preview-lines th:nth-child(6), .preview-lines td:nth-child(6) { width: 20%; text-align: right; }
.preview-lines td.preview-name { font-weight: 700; }
.preview-line-note { display: block; margin-top: 3px; color: var(--muted); font-size: 9px; font-weight: 500; line-height: 1.35; overflow-wrap: anywhere; }
.document-bottom { display: grid; grid-template-columns: minmax(0, 1fr) minmax(190px, 0.92fr); gap: 28px; padding-top: 23px; }
.preview-notes { min-width: 0; }
.preview-notes p { margin: 7px 0 0; white-space: pre-wrap; overflow-wrap: anywhere; font-size: 11px; }
.totals-block { border-top: 1px solid var(--line-dark); }
.total-row, .tax-row { display: flex; justify-content: space-between; gap: 12px; padding: 7px 0; border-bottom: 1px solid var(--line); font-size: 11px; }
.discount-row { color: var(--red-dark); }
.tax-breakdown { padding: 2px 0; }
.grand-total { margin-top: 4px; border-top: 2px solid var(--ink); border-bottom: 2px solid var(--ink); padding: 12px 0; font-size: 16px; }
.grand-total strong { color: var(--red-dark); font-size: 19px; }
.document-footer { margin-top: 28px; padding-top: 9px; border-top: 1px solid var(--line); color: var(--muted); font-size: 9px; line-height: 1.5; }
.preview-footnote { margin: 10px 4px 0; color: var(--muted); font-size: 11px; }

.site-footer { display: flex; justify-content: space-between; gap: 24px; padding: 18px 0 32px; border-top: 1px solid rgba(91, 72, 49, 0.15); color: var(--muted); font-size: 11px; }
.site-footer p { margin: 0; }
.site-footer p:last-child { max-width: 660px; text-align: right; }

@media (max-width: 1080px) {
  .workspace { grid-template-columns: 1fr; }
  .preview-panel { position: static; }
  .document-preview { max-width: 820px; margin: 0 auto; }
}

@media (max-width: 680px) {
  .header-inner, .app-shell, .site-footer { width: min(100% - 28px, 620px); }
  .header-inner { min-height: 0; align-items: end; padding: 26px 0 22px; }
  .header-art { width: 110px; }
  .header-art img { height: 84px; }
  .stamp { right: 5px; bottom: 5px; width: 32px; height: 32px; font-size: 5px; }
  .brand-lockup h1 { font-size: 44px; }
  .brand-description { max-width: 210px; font-size: 12px; }
  .intro { display: block; margin-bottom: 22px; }
  .intro-note { margin-top: 15px; }
  .editor-panel { padding: 21px 16px 24px; }
  .two-up { grid-template-columns: 1fr; gap: 13px; }
  .field-wide { grid-column: auto; }
  .sample-strip { display: block; }
  .sample-actions { justify-content: flex-start; margin-top: 11px; }
  .document-preview { min-height: 0; padding: 24px 18px; }
  .preview-placeholder { min-height: 480px; }
  .document-header { display: block; }
  .document-meta { min-width: 0; margin-top: 20px; }
  .document-parties { display: block; }
  .issuer-block { margin-top: 16px; text-align: left; }
  .document-bottom { grid-template-columns: 1fr; gap: 22px; }
  .site-footer { display: block; }
  .site-footer p + p { margin-top: 10px; text-align: left; }
}

@media print {
  @page { size: A4; margin: 12mm; }
  :root { background: white; }
  html, body { min-width: 0; background: white; }
  body { color: #111; }
  .screen-only { display: none !important; }
  .app-shell { width: auto; margin: 0; padding: 0; }
  .workspace { display: block; }
  .preview-panel { width: 100%; }
  .document-preview { min-height: 0; width: 100%; padding: 0; border: 0; box-shadow: none; }
  #preview-document { display: block !important; }
  .preview-lines td, .preview-lines th { font-size: 10px; }
  .document-footer { font-size: 8px; }
  .preview-lines tr { break-inside: avoid; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; }
}
