* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: 'LXGW WenKai', sans-serif;
  background-color: #f8f4ef;
  color: #5a4a42;
  line-height: 1.6;
}

.container {
  width: 100%;
  padding: 24px 32px 48px;
}

header {
  text-align: center;
  margin-bottom: 32px;
  padding: 24px 0;
}

h1 {
  font-size: 32px;
  font-weight: 700;
  color: #4a3f35;
  margin-bottom: 8px;
  letter-spacing: 2px;
}

.description {
  font-size: 14px;
  color: #8c7b6d;
}

/* Section Styles */
.section {
  background-color: #fffdf9;
  border-radius: 12px;
  border: 1px solid #ebe5dd;
  margin-bottom: 16px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(139, 121, 101, 0.08);
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  cursor: pointer;
}

.section-title {
  font-size: 18px;
  font-weight: bold;
  color: #4a3f35;
}

.arrow {
  font-size: 20px;
  color: #b0a193;
  transition: transform 0.3s ease;
}

.section-content {
  padding: 0 20px 20px;
  display: none;
}

.section.is-open .section-content {
  display: block;
}

.section.is-open .arrow {
  transform: rotate(90deg);
}

/* Toggle Switch */
.toggle-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  font-size: 15px;
  cursor: pointer;
}

.switch {
  position: relative;
  display: inline-block;
  width: 48px;
  height: 26px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #e0d5c8;
  transition: .4s;
  border-radius: 26px;
}

.slider:before {
  position: absolute;
  content: "";
  height: 20px;
  width: 20px;
  left: 3px;
  bottom: 3px;
  background-color: white;
  transition: .4s;
  border-radius: 50%;
}

input:checked + .slider {
  background-color: #8b7b6b;
}

input:checked + .slider:before {
  transform: translateX(22px);
}

.field-group.is-hidden {
  display: none;
}

label {
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
  color: #7a6b5d;
}

input[type="text"],
select,
textarea {
  width: 100%;
  padding: 12px;
  border: 1px solid #e5ddd5;
  border-radius: 8px;
  font-size: 15px;
  font-family: inherit;
  background-color: #fcfaf7;
  color: #4a3f35;
}

textarea {
  min-height: 200px;
  resize: vertical;
  line-height: 1.8;
}

input[type="text"]:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: #b0a193;
  box-shadow: 0 0 0 2px rgba(176, 161, 147, 0.2);
}

.row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-bottom: 16px;
}

.row > div {
  margin-bottom: 0;
}

.row label {
  margin-bottom: 8px;
}

.row input[type="text"],
.row select {
  width: 100%;
}

input[type="range"] {
  width: 100%;
  margin: 8px 0;
  accent-color: #8b7b6b;
}

input[type="number"] {
  width: 100%;
  padding: 8px;
  border: 1px solid #e5ddd5;
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  background-color: #fcfaf7;
  color: #4a3f35;
  text-align: center;
}

input[type="number"]:focus {
  outline: none;
  border-color: #b0a193;
}

.hint {
  font-size: 12px;
  color: #a09389;
  margin-top: 8px;
  line-height: 1.5;
}

/* Actions */
.actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 12px;
}

button {
  padding: 14px;
  border: none;
  border-radius: 24px;
  font-size: 15px;
  font-family: inherit;
  cursor: pointer;
  background-color: #e8e0d6;
  color: #5a4a42;
  transition: background-color 0.2s;
}

button:hover {
  background-color: #ded5ca;
}

.reset {
  text-align: center;
  margin-bottom: 24px;
}

.reset button {
  background: none;
  color: #a09389;
  font-size: 13px;
  padding: 8px;
}

.reset button:hover {
  color: #7a6b5d;
  background: none;
}

/* Preview Card */
.preview-card {
  background-color: #fffdf9;
  border-radius: 12px;
  border: 1px solid #ebe5dd;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(139, 121, 101, 0.08);
}

.preview-title {
  text-align: center;
  font-size: 16px;
  font-weight: bold;
  color: #8b7b6b;
  margin-bottom: 20px;
  letter-spacing: 2px;
  padding-bottom: 12px;
  border-bottom: 1px solid #f0ebe5;
}

.preview-letter {
  overflow-x: auto;
  overflow-y: hidden;
  padding-bottom: 8px;
  -webkit-overflow-scrolling: touch;
}

/* ==================== Letter Styles ==================== */

.letter {
  --paper-width: 760px;
  --paper-length: 760px;
  --paper-padding-y: 72px;
  --paper-padding-x: 76px;
  --paper-base: #e2c184;
  --paper-line-h: rgba(112, 77, 37, 0.09);
  --paper-line-v: rgba(112, 77, 37, 0.045);
  --line-h-color: transparent;
  --line-v-color: transparent;
  --paper-overlay:
    radial-gradient(circle at 14% 14%, rgba(255, 247, 221, 0.28), transparent 20%),
    radial-gradient(circle at 86% 80%, rgba(126, 80, 36, 0.16), transparent 22%),
    linear-gradient(180deg, rgba(255, 255, 255, 0.18), rgba(255, 255, 255, 0) 26%, rgba(79, 52, 25, 0.06) 100%);
  --paper-accent: rgba(120, 76, 33, 0.08);
  --paper-border: rgba(107, 77, 41, 0.24);
  --paper-text: #4e3620;
  --paper-muted: rgba(78, 54, 32, 0.76);
  --rule-gap: 38px;
  --grid-gap: 42px;
  --letter-font: 'LXGW WenKai';
  position: relative;
  width: var(--paper-width);
  min-height: var(--paper-length);
  margin: 0 auto;
  padding: var(--paper-padding-y) var(--paper-padding-x);
  color: var(--paper-text);
  background:
    linear-gradient(var(--line-h-color) 1px, transparent 1px),
    linear-gradient(90deg, var(--line-v-color) 1px, transparent 1px),
    var(--paper-overlay),
    var(--paper-base);
  background-size: 100% var(--rule-gap), var(--grid-gap) 100%, 100% 100%, 100% 100%;
  background-position: 0 0, 0 0, 0 0, 0 0;
  border-radius: 10px;
  box-shadow:
    0 34px 90px rgba(66, 44, 22, 0.22),
    0 10px 26px rgba(66, 44, 22, 0.12),
    inset 0 1px 0 rgba(255, 248, 235, 0.42),
    inset 0 0 90px rgba(139, 94, 52, 0.12),
    inset 0 -28px 40px rgba(88, 57, 28, 0.08);
  isolation: isolate;
  overflow: hidden;
}

.letter[data-lines="none"] {
  --line-h-color: transparent;
  --line-v-color: transparent;
}

.letter[data-lines="horizontal"] {
  --line-h-color: var(--paper-line-h);
  --line-v-color: transparent;
}

.letter[data-lines="vertical"] {
  --line-h-color: transparent;
  --line-v-color: var(--paper-line-v);
}

.letter[data-lines="grid"] {
  --line-h-color: var(--paper-line-h);
  --line-v-color: var(--paper-line-v);
}

.letter::before {
  content: "";
  position: absolute;
  inset: 18px;
  border: 1px solid var(--paper-border);
  box-shadow:
    inset 0 0 0 1px rgba(255, 255, 255, 0.12),
    inset 0 16px 22px rgba(255, 255, 255, 0.06),
    inset 0 -18px 26px rgba(79, 52, 25, 0.06);
  border-radius: 2px;
  pointer-events: none;
}

.letter.is-exporting {
  background:
    linear-gradient(var(--line-h-color) 1px, transparent 1px),
    linear-gradient(90deg, var(--line-v-color) 1px, transparent 1px),
    var(--paper-base);
  background-size: 100% var(--rule-gap), var(--grid-gap) 100%, 100% 100%;
  background-position: 0 0, 0 0, 0 0;
  box-shadow: none;
  isolation: auto;
  overflow: visible;
}

.letter.is-exporting::before {
  box-shadow: none;
}

.letter[data-theme="kraft"] {
  --paper-base: #e1c184;
  --paper-line-h: rgba(112, 77, 37, 0.09);
  --paper-line-v: rgba(112, 77, 37, 0.045);
  --paper-overlay:
    radial-gradient(circle at 14% 14%, rgba(255, 245, 214, 0.28), transparent 20%),
    linear-gradient(180deg, rgba(255, 255, 255, 0.16), rgba(255, 255, 255, 0) 28%, rgba(83, 54, 27, 0.06) 100%);
  --paper-border: rgba(107, 77, 41, 0.26);
  --paper-text: #4e3620;
  --paper-muted: rgba(78, 54, 32, 0.76);
}

.letter[data-theme="plain"] {
  --paper-base: #fcfcfb;
  --paper-line-h: rgba(117, 117, 117, 0.075);
  --paper-line-v: rgba(117, 117, 117, 0.028);
  --paper-overlay:
    linear-gradient(180deg, rgba(255, 255, 255, 0.82), rgba(244, 244, 242, 0.46));
  --paper-accent: rgba(120, 120, 120, 0.06);
  --paper-border: rgba(120, 120, 120, 0.16);
  --paper-text: #2e2e2e;
  --paper-muted: rgba(46, 46, 46, 0.72);
}

.letter[data-theme="ivory"] {
  --paper-base: #fef1d7;
  --paper-line-h: rgba(161, 131, 82, 0.105);
  --paper-line-v: rgba(161, 131, 82, 0.05);
  --paper-overlay:
    radial-gradient(circle at top left, rgba(255, 243, 211, 0.42), transparent 30%),
    linear-gradient(180deg, rgba(255, 255, 255, 0.44), rgba(255, 255, 255, 0));
  --paper-accent: rgba(176, 137, 70, 0.08);
  --paper-border: rgba(161, 131, 82, 0.22);
  --paper-text: #5a4125;
  --paper-muted: rgba(90, 65, 37, 0.72);
}

.letter[data-theme="blush"] {
  --paper-base: #ffe9ee;
  --paper-line-h: rgba(190, 113, 130, 0.115);
  --paper-line-v: rgba(190, 113, 130, 0.06);
  --paper-overlay:
    radial-gradient(circle at 18% 14%, rgba(255, 231, 236, 0.52), transparent 20%),
    radial-gradient(circle at 86% 74%, rgba(214, 132, 150, 0.18), transparent 18%),
    linear-gradient(180deg, rgba(255, 255, 255, 0.44), rgba(255, 255, 255, 0));
  --paper-accent: rgba(179, 76, 100, 0.08);
  --paper-border: rgba(190, 113, 130, 0.22);
  --paper-text: #69414d;
  --paper-muted: rgba(105, 65, 77, 0.74);
}

.letter[data-theme="mist"] {
  --paper-base: #edf5fb;
  --paper-line-h: rgba(77, 111, 152, 0.12);
  --paper-line-v: rgba(77, 111, 152, 0.06);
  --paper-overlay:
    radial-gradient(circle at 16% 20%, rgba(220, 238, 251, 0.42), transparent 24%),
    linear-gradient(160deg, rgba(214, 233, 247, 0.62), rgba(255, 255, 255, 0));
  --paper-accent: rgba(63, 101, 146, 0.08);
  --paper-border: rgba(77, 111, 152, 0.22);
  --paper-text: #35506c;
  --paper-muted: rgba(53, 80, 108, 0.74);
  --grid-gap: 48px;
}

.letter-inner {
  display: flex;
  flex-direction: column;
  gap: 34px;
}

.recipient {
  font-size: 21px;
  line-height: 1.7;
  font-family: var(--letter-font), serif;
}

.content {
  white-space: pre-wrap;
  line-height: 2.05;
  font-size: 19px;
  letter-spacing: 0.04em;
  text-align: justify;
  word-break: break-word;
  font-family: var(--letter-font), serif;
}

.signature {
  display: grid;
  gap: 8px;
  margin-top: 6px;
  align-self: end;
  text-align: right;
}

.sender {
  font-size: 20px;
  line-height: 1.8;
  font-family: var(--letter-font), serif;
}

.date {
  font-size: 15px;
  color: var(--paper-muted);
}

/* Print */
@media print {
  body {
    background: #fff;
  }

  .container {
    max-width: none;
    padding: 0;
  }

  header,
  .section,
  .actions,
  .reset,
  .preview-title {
    display: none;
  }

  .preview-card {
    border: none;
    box-shadow: none;
    padding: 0;
    background: transparent;
  }

  .letter {
    width: 100%;
    min-height: auto;
    border-radius: 0;
    box-shadow: none;
  }
}

/* Responsive */
@media (max-width: 900px) {
  .row {
    grid-template-columns: 1fr;
  }

  .letter {
    --paper-padding-y: 54px;
    --paper-padding-x: 34px;
  }
}

@media (max-width: 640px) {
  .container {
    padding: 16px 12px 36px;
  }

  h1 {
    font-size: 24px;
  }

  .description {
    font-size: 13px;
  }

  .section-header {
    padding: 14px 16px;
  }

  .section-title {
    font-size: 16px;
  }

  .section-content {
    padding: 0 16px 16px;
  }

  .toggle-row {
    font-size: 14px;
  }

  label {
    font-size: 13px;
  }

  input[type="text"],
  select,
  textarea {
    font-size: 16px; /* 防止 iOS 自动缩放 */
    padding: 10px;
  }

  textarea {
    min-height: 160px;
  }

  .actions {
    grid-template-columns: 1fr;
  }

  button {
    font-size: 14px;
    padding: 12px;
  }

  .preview-card {
    padding: 12px;
  }

  .preview-title {
    font-size: 14px;
    margin-bottom: 12px;
    padding-bottom: 8px;
  }

  /* 信纸缩放适配屏幕宽度 */
  .preview-letter {
    overflow-x: auto;
  }

  .letter {
    --paper-padding-y: 40px;
    --paper-padding-x: 28px;
    transform-origin: top left;
  }

  .recipient {
    font-size: 18px;
  }

  .content {
    font-size: 16px;
    line-height: 1.9;
  }

  .sender {
    font-size: 17px;
  }

  .date {
    font-size: 13px;
  }
}
