:root {
  --canvas: #ffffff;
  --surface: #f9f8f7;
  --surface-2: #f0efed;
  --raised: #ffffff;
  --text: #2c2c2b;
  --muted: #7d7a75;
  --border: #e6e5e3;
  --border-strong: #d6d4d0;
  --blue: #2783de;
  --blue-soft: #e5f2fc;
  --green: #46a171;
  --green-soft: #e8f1ec;
  --orange: #d5803b;
  --orange-soft: #fbebde;
  --red: #e56458;
  --red-soft: #fce9e7;
  --preview: #5d52a8;
  --preview-soft: #f0eef9;
  --side-note-w: 340px;
  --layout-gap: 16px;
  --radius: 8px;
}

* { box-sizing: border-box; }
html, body { margin: 0; width: 100%; min-height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans JP", sans-serif;
  color: var(--text);
  background: var(--surface);
  font-size: 14px;
  line-height: 1.5;
  padding: 16px;
  height: 100vh;
  overflow-x: hidden;
  overflow-y: auto;
}

button, input, select, textarea { font: inherit; }
button { color: inherit; }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 2px solid var(--blue);
  outline-offset: 2px;
}
.lucide {
  width: 16px;
  height: 16px;
  flex: 0 0 auto;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
button .lucide { width: 15px; height: 15px; }

.main-container {
  display: grid;
  grid-template-columns: minmax(520px, 1fr) minmax(620px, 1.1fr);
  width: calc(100% - var(--side-note-w) - var(--layout-gap));
  max-width: none;
  height: calc(100vh - 32px);
  margin: 0;
  gap: var(--layout-gap);
}

.form-section,
.preview-section {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--canvas);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, .05), 0 4px 12px rgba(0, 0, 0, .04);
}

.form-section { overflow-y: auto; padding: 16px; }
.preview-section { overflow: hidden; padding: 16px; border-color: #dedaf0; }

.app-header,
.preview-header {
  min-height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
  padding: 0 0 12px;
  border-bottom: 1px solid var(--border);
}

.app-title {
  min-width: 0;
  margin: 0;
  color: var(--text);
  font-size: 17px;
  line-height: 1.35;
  letter-spacing: -.01em;
}

.form-guidance {
  display: grid;
  grid-template-columns: 20px minmax(0, 1fr);
  align-items: start;
  gap: 0 10px;
  margin: -2px 0 12px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-left: 3px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
}
.form-guidance-icon { width: 18px; height: 18px; margin-top: 1px; color: var(--muted); }
.form-guidance-body { min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.form-guidance-title { color: #1e67aa; font-size: 13px; font-weight: 700; }
.form-guidance-content { display: block; min-width: 0; }

.form-card,
.preview-card {
  padding: 12px 0;
  margin: 0;
  background: transparent;
  border: 0;
  border-bottom: 1px solid var(--border);
  border-radius: 0;
}
.form-card:last-child,
.preview-card:last-child { border-bottom: 0; padding-bottom: 0; }
.form-card-pair { display: block; border-bottom: 0; }
.form-card-pair .form-card { border-bottom: 1px solid var(--border); }
.form-card-pair .form-card:last-child { border-bottom: 0; }

.section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 12px;
  color: var(--text);
  font-size: 15px;
  line-height: 1.35;
  font-weight: 700;
}

.section-num {
  width: 23px;
  height: 23px;
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  background: var(--blue-soft);
  color: var(--blue);
  font-size: 12px;
  font-weight: 700;
}
.section-required { margin-left: 2px; color: var(--red); font-size: 12px; font-weight: 700; white-space: nowrap; }
.section-required span { font-size: 17px; line-height: 1; }

label {
  display: block;
  margin: 11px 0 5px;
  color: var(--text);
  font-size: 13px;
  line-height: 1.35;
  font-weight: 600;
}

label:first-of-type { margin-top: 0; }
.required { margin-left: 4px; color: var(--red); font-size: 17px; line-height: 1; font-weight: 800; vertical-align: -1px; }
.save-hint { margin-left: 6px; color: var(--orange); font-size: 11px; font-weight: 600; }

input[type="text"],
input[type="number"],
select,
textarea {
  width: 100%;
  min-height: 40px;
  padding: 9px 10px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--raised);
  color: var(--text);
  font-size: 14px;
  transition: border-color .15s ease, box-shadow .15s ease, background .15s ease;
}

input:hover, select:hover, textarea:hover { border-color: #bdbab5; }
input:focus, select:focus, textarea:focus {
  border-color: var(--blue);
  box-shadow: 0 0 0 3px var(--blue-soft);
  outline: 0;
}

textarea[id="remarks"],
textarea[id="signature"] { min-height: 72px; resize: vertical; }

.is-invalid { border-color: var(--red) !important; background: var(--red-soft) !important; }
.field-error { display: none; margin-top: 4px; color: var(--red); font-size: 12px; font-weight: 600; }
.field-error.is-visible { display: block; }

.basic-info-card {
  display: grid;
  grid-template-columns: minmax(270px, .6fr) minmax(0, 1fr);
  column-gap: 18px;
  row-gap: 8px;
  align-items: center;
}
.basic-info-card .section-title { grid-column: 1 / -1; margin-bottom: 2px; }
.basic-info-card label { margin-top: 0; }
.basic-info-card .checkbox-group,
.basic-info-card select,
.basic-info-card input,
.basic-info-card .input-group-inline,
.basic-info-card .field-error { grid-column: 2; }
.basic-info-card .field-error { margin-top: -4px; }

.input-group-inline { display: flex; align-items: center; gap: 10px; }
.input-group-inline input { width: 150px; }

.checkbox-group { display: flex; flex-wrap: wrap; gap: 8px; }
.basic-info-card .checkbox-group { flex-wrap: nowrap; gap: 6px; min-width: 0; }
.cb-label {
  min-height: 38px;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  margin: 0;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
}
.basic-info-card .cb-label { padding-left: 8px; padding-right: 8px; font-size: 12px; }
.cb-label:hover { background: var(--surface-2); border-color: var(--border-strong); }
.cb-label:has(input:checked) { background: var(--blue-soft); border-color: var(--blue); color: var(--blue); }
.cb-label input { width: auto; min-height: 0; accent-color: var(--blue); cursor: pointer; }

.delivery-header,
.delivery-row {
  display: grid;
  grid-template-columns: 28px minmax(0, 1fr) minmax(0, 1fr) 32px;
  gap: 8px;
  align-items: center;
}
.delivery-header {
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}
.dh-name { grid-column: 2; }
.dh-email { grid-column: 3; }
.delivery-row { margin-bottom: 8px; }
.row-num { color: var(--blue); font-size: 13px; font-weight: 700; text-align: center; }
.d-name, .d-email { width: 100%; }

button {
  min-height: 40px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  padding: 0 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  font-weight: 600;
  transition: background .15s ease, border-color .15s ease, color .15s ease, transform .15s ease;
}
button:hover:not(:disabled) { transform: translateY(-1px); }
button:active:not(:disabled) { transform: translateY(0); }
button:disabled { opacity: .42; cursor: not-allowed; }

.btn-small {
  min-height: 36px;
  width: fit-content;
  padding: 0 12px;
  border-color: var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
}
.btn-small:hover { background: var(--surface-2); border-color: var(--border-strong); }
.btn-remove-row {
  width: 32px;
  min-height: 32px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 18px;
}
.btn-remove-row:hover { background: var(--red-soft); color: var(--red); }
.button-group { display: flex; flex-direction: column; gap: 8px; }

.copy-buttons { display: flex; gap: 8px; }
.copy-buttons button { min-width: 145px; color: #fff; }
.btn-subject { background: var(--orange); }
.btn-subject:hover { background: #bf6f32; }
.btn-body { background: var(--green); }
.btn-body:hover { background: #398d63; }
.btn-clear { background: var(--text); color: var(--canvas); }
.btn-clear:hover { background: #444340; }
.header-clear { width: fit-content; min-width: 214px; flex: 0 0 auto; }

.validation-summary {
  display: none;
  margin-bottom: 12px;
  padding: 10px 12px;
  border: 1px solid #efb4ad;
  border-radius: 6px;
  background: var(--red-soft);
  color: #9d4038;
  font-size: 13px;
  font-weight: 600;
  line-height: 1.55;
}
.validation-summary.is-visible { display: block; }

.note {
  color: var(--text);
  font-size: 13px;
  line-height: 1.6;
  background: var(--orange-soft);
  border: 1px solid #edc7a7;
  border-left: 3px solid var(--orange);
}
.note strong.title { display: block; margin-bottom: 10px; color: #9b5724; font-size: 15px; line-height: 1.45; }
.note ul { display: flex; flex-direction: column; gap: 10px; margin: 0; padding: 0; list-style: none; }
.note li { padding: 10px 12px; border: 1px solid #f0d9c2; border-radius: 6px; background: rgba(255, 255, 255, .74); line-height: 1.55; }
.note li strong { display: block; margin-bottom: 4px; }
.note li .note-key { display: flex; align-items: center; gap: 8px; min-height: 20px; color: #4f4b47; line-height: 1.4; }
.note-key .lucide { width: 18px; height: 18px; display: block; flex: 0 0 18px; transform: translateY(1px); }
.note-icon-required { color: var(--red); }
.note-icon-security { color: var(--orange); }
.note-icon-split { color: var(--green); }
.note-emphasis {
  font-weight: 700;
  text-decoration-line: underline;
  text-decoration-thickness: 2px;
  text-underline-offset: 3px;
}
.note-emphasis-required { color: #9d4038; text-decoration-color: var(--red); }
.note-warning { color: #9d4038; font-weight: 700; }
.note-emphasis-security { color: #8a4e20; text-decoration-color: var(--orange); }
.note-emphasis-split { color: #2f7756; text-decoration-color: var(--green); }

.bottom-note {
  position: fixed;
  top: 16px;
  right: 16px;
  bottom: 16px;
  z-index: 10;
  width: var(--side-note-w);
  max-width: calc(100vw - 32px);
  max-height: none;
  margin: 0;
  padding: 10px;
  overflow-y: auto;
}

.preview-header { border-color: #e3def5; }
.preview-header h3 {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0;
  color: var(--preview);
  font-size: 17px;
}
.preview-header h3::before { content: none; }
.preview-header h3 .lucide { color: var(--blue); }
.preview-actions { flex: 0 0 auto; margin-bottom: 0; }
.preview-actions button { white-space: nowrap; }

.preview-card { background: transparent; }
.preview-card + .preview-card { margin-top: 0; }
.preview-card.body-card { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.preview-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 0 0 8px;
  color: var(--preview);
  font-size: 13px;
  font-weight: 700;
}
.preview-help { color: var(--red); font-size: 11px; font-weight: 600; }
.preview-box {
  width: 100%;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--canvas);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans JP", sans-serif;
  font-size: 14px;
  line-height: 1.65;
  white-space: pre-wrap;
  word-break: break-word;
}
.preview-box.subject { font-weight: 700; }
textarea.preview-box.body { flex: 1; min-height: 0; margin: 0; resize: none; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  z-index: 100;
  max-width: min(460px, calc(100vw - 32px));
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--text);
  color: var(--canvas);
  box-shadow: 0 8px 24px rgba(0, 0, 0, .18);
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 18px);
  transition: opacity .18s, transform .18s;
}
.toast.is-visible { opacity: 1; transform: translate(-50%, 0); }
.toast.success { background: var(--green); border-color: var(--green); }
.toast.error { background: var(--red); border-color: var(--red); }

@media (max-width: 1600px) {
  body { height: auto; overflow: auto; }
  .main-container {
    width: 100%;
    max-width: none;
    height: auto;
    margin: 0 0 16px;
  }
  .bottom-note { position: static; width: 100%; max-width: none; margin: 16px 0 0; }
}

@media (max-width: 980px) {
  .main-container { grid-template-columns: 1fr; height: auto; margin-bottom: 16px; }
  .preview-section { min-height: 640px; }
  .form-card-pair { grid-template-columns: 1fr; gap: 0; }
  .form-card-pair .form-card:first-child { border-bottom: 1px solid var(--border); }
  .basic-info-card .checkbox-group { flex-wrap: wrap; }
  .note ul { grid-template-columns: 1fr; }
  .app-header, .preview-header { align-items: flex-start; flex-direction: column; }
  .copy-buttons { width: 100%; }
  .copy-buttons button { flex: 1; min-width: 0; }
  .form-guidance { grid-template-columns: 18px minmax(0, 1fr); gap: 0 8px; }
  .form-guidance-content { display: block; }
  .basic-info-card { display: block; }
  .basic-info-card label { margin-top: 11px; }
  .basic-info-card label:first-of-type { margin-top: 0; }
  .basic-info-card .field-error { margin-top: 4px; }
  .bottom-note { position: static; margin-top: 16px; }
}

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