:root {
  --ink: #352d29;
  --muted: #786d65;
  --line: rgba(53, 45, 41, 0.16);
  --surface: #fffaf1;
  --panel: rgba(255, 250, 241, 0.78);
  --accent: #9a5f47;
  --accent-strong: #6f3c2c;
  --card-bg: #d9eadf;
  --card-fg: #352d29;
  --shadow: 0 20px 60px rgba(45, 34, 25, 0.18);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  overflow-x: hidden;
  color: var(--ink);
  background:
    radial-gradient(circle at 15% 10%, rgba(255, 244, 214, 0.9), transparent 30%),
    linear-gradient(135deg, #f6efe1 0%, #e9f0e2 44%, #efe3d7 100%);
  font-family: "Palatino Linotype", "Songti SC", "Noto Serif SC", Georgia, serif;
}

button,
input,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.app {
  display: grid;
  grid-template-columns: minmax(320px, 460px) minmax(360px, 1fr);
  width: 100%;
  max-width: 100vw;
  min-height: 100vh;
  overflow-x: hidden;
}

.editor {
  min-width: 0;
  padding: 22px;
  background: rgba(255, 250, 241, 0.72);
  border-right: 1px solid var(--line);
  backdrop-filter: blur(18px);
  overflow-y: auto;
}

.topbar,
.section-head,
.actions,
.photo-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.kicker {
  margin: 0 0 4px;
  color: var(--accent);
  font-size: 0.78rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: clamp(2rem, 8vw, 3.2rem);
  line-height: 0.95;
  letter-spacing: 0;
}

.icon-button {
  width: 44px;
  height: 44px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: #fff8ec;
  color: var(--accent-strong);
  font-size: 1.7rem;
  line-height: 1;
}

.notice {
  margin: 20px 0;
  padding: 12px 14px;
  border: 1px solid rgba(154, 95, 71, 0.24);
  border-radius: 8px;
  background: rgba(255, 246, 224, 0.9);
  color: #704634;
  font-size: 0.92rem;
  line-height: 1.55;
}

.field {
  display: grid;
  gap: 8px;
  margin-bottom: 16px;
  color: var(--muted);
  font-size: 0.9rem;
}

.field input,
.field textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 252, 246, 0.96);
  color: var(--ink);
  outline: none;
  padding: 13px 14px;
}

.field textarea {
  min-height: 168px;
  resize: vertical;
  line-height: 1.65;
}

.field input:focus,
.field textarea:focus {
  border-color: rgba(154, 95, 71, 0.55);
  box-shadow: 0 0 0 3px rgba(154, 95, 71, 0.1);
}

.photo-picker input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.photo-picker span,
.text-button,
.primary-button,
.secondary-button,
.segment {
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 14px;
  background: rgba(255, 252, 246, 0.95);
  color: var(--ink);
}

.text-button {
  color: var(--muted);
}

.panel {
  margin: 18px 0;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 250, 241, 0.58);
}

.panel legend {
  padding: 0 8px;
  color: var(--muted);
}

.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 14px;
}

.ratio-grid {
  grid-template-columns: repeat(4, 1fr);
  margin-bottom: 0;
}

.text-grid {
  grid-template-columns: repeat(4, 1fr);
}

.range-field {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px 14px;
  align-items: center;
  margin-bottom: 16px;
  color: var(--muted);
  font-size: 0.9rem;
}

.range-field strong {
  color: var(--accent-strong);
  font-size: 0.95rem;
}

.range-field input {
  grid-column: 1 / -1;
  width: 100%;
  accent-color: var(--accent-strong);
}

.segment.active {
  background: var(--accent-strong);
  color: #fffaf1;
  border-color: var(--accent-strong);
}

.swatches {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 8px;
  margin-bottom: 14px;
}

.swatch {
  aspect-ratio: 1;
  border: 2px solid rgba(53, 45, 41, 0.12);
  border-radius: 50%;
  padding: 0;
}

.swatch.active {
  outline: 2px solid var(--accent-strong);
  outline-offset: 3px;
}

.color-readout {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 12px;
  color: var(--muted);
  font-size: 0.9rem;
}

.color-chip {
  width: 28px;
  height: 28px;
  border: 1px solid rgba(53, 45, 41, 0.16);
  border-radius: 50%;
  background: var(--custom-color, #d9eadf);
  box-shadow: inset 0 0 0 3px rgba(255, 250, 241, 0.72);
}

#customBgButton.active {
  background:
    linear-gradient(rgba(111, 60, 44, 0.72), rgba(111, 60, 44, 0.72)),
    var(--custom-color, #d9eadf);
  border-color: var(--custom-color, #d9eadf);
}

.compact {
  margin-bottom: 0;
}

.actions {
  position: sticky;
  bottom: 0;
  padding: 14px 0 4px;
  background: linear-gradient(180deg, rgba(255, 250, 241, 0), rgba(255, 250, 241, 0.95) 30%);
}

.single-action {
  justify-content: stretch;
}

.primary-button,
.secondary-button {
  flex: 1;
  font-weight: 700;
}

.primary-button {
  background: var(--accent-strong);
  color: #fffaf1;
  border-color: var(--accent-strong);
}

.history {
  margin-top: 26px;
}

.section-head h2 {
  margin: 0;
  font-size: 1.05rem;
}

.section-head span {
  color: var(--muted);
  font-size: 0.88rem;
}

.entry-list {
  display: grid;
  gap: 10px;
  margin-top: 12px;
}

.entry-item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  min-height: 58px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 252, 246, 0.88);
  padding: 11px 12px;
}

.entry-load {
  min-width: 0;
  border: 0;
  background: transparent;
  color: var(--ink);
  padding: 0;
  text-align: left;
}

.entry-item strong,
.entry-item span {
  display: block;
}

.entry-item strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.entry-item span {
  color: var(--muted);
  font-size: 0.84rem;
}

.delete-entry {
  border: 0;
  background: transparent;
  color: #9a5f47;
  padding: 8px;
}

.preview-wrap {
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 28px;
  overflow: auto;
}

.preview-head {
  display: flex;
  justify-content: space-between;
  width: min(430px, 100%);
  margin-bottom: 12px;
  color: var(--muted);
  font-size: 0.9rem;
}

.preview-pages {
  display: grid;
  width: 100%;
  max-width: 430px;
  justify-items: center;
  gap: 18px;
}

.preview-page {
  display: block;
  width: 100%;
  max-width: 430px;
  height: auto;
  border-radius: 8px;
  box-shadow: var(--shadow);
  background: var(--card-bg);
}

.preview-actions {
  display: flex;
  width: min(430px, 100%);
  margin-top: 16px;
}

.export-results {
  display: none;
  width: min(430px, 100%);
  margin-top: 14px;
  padding: 12px;
  border: 1px solid rgba(53, 45, 41, 0.14);
  border-radius: 8px;
  background: rgba(255, 250, 241, 0.82);
}

.export-results.has-results {
  display: grid;
  gap: 10px;
}

.export-results p {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.5;
}

.export-link {
  display: block;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 14px;
  background: rgba(255, 252, 246, 0.96);
  color: var(--accent-strong);
  font-weight: 700;
  text-align: center;
  text-decoration: none;
}

.mobile-tabbar {
  display: none;
}

@media (max-width: 1040px) {
  .app {
    display: block;
    width: 100vw;
    max-width: 100vw;
    min-height: 100dvh;
    padding-bottom: 78px;
  }

  .editor {
    width: 100%;
    min-height: calc(100dvh - 78px);
    border-right: 0;
    padding: 18px 18px 28px;
  }

  .field input,
  .field textarea {
    font-size: 16px;
  }

  .topbar {
    position: sticky;
    top: 0;
    z-index: 3;
    margin: -18px -18px 18px;
    padding: 16px 18px 14px;
    background: rgba(255, 250, 241, 0.88);
    border-bottom: 1px solid rgba(53, 45, 41, 0.08);
    backdrop-filter: blur(16px);
  }

  .app[data-mobile-tab="preview"] .editor {
    display: none;
  }

  .editor-panel {
    display: none;
  }

  .app[data-mobile-tab="write"] [data-mobile-panel="write"],
  .app[data-mobile-tab="style"] [data-mobile-panel="style"] {
    display: block;
  }

  .preview-wrap {
    width: 100%;
    min-height: calc(100dvh - 78px);
    padding: 18px 14px 96px;
    background: rgba(246, 239, 225, 0.86);
  }

  .app:not([data-mobile-tab="preview"]) .preview-wrap {
    display: none;
  }

  .preview-actions {
    position: fixed;
    left: 14px;
    right: 14px;
    bottom: 90px;
    z-index: 5;
    width: auto;
    max-width: none;
    margin: 0;
  }

  .preview-head {
    width: 330px;
    max-width: calc(100vw - 28px);
  }

  .preview-pages,
  .preview-page,
  .export-results {
    width: 100%;
    max-width: calc(100vw - 28px);
  }

  .mobile-tabbar {
    position: fixed;
    left: 12px;
    right: 12px;
    bottom: 12px;
    z-index: 10;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    padding: 8px;
    border: 1px solid rgba(53, 45, 41, 0.12);
    border-radius: 8px;
    background: rgba(255, 250, 241, 0.92);
    box-shadow: 0 12px 34px rgba(45, 34, 25, 0.16);
    backdrop-filter: blur(18px);
  }

  .mobile-tab {
    min-height: 46px;
    border: 0;
    border-radius: 7px;
    background: transparent;
    color: var(--muted);
    font-weight: 700;
  }

  .mobile-tab.active {
    background: var(--accent-strong);
    color: #fffaf1;
  }
}

@media (max-width: 420px) {
  .photo-row,
  .actions {
    align-items: stretch;
    flex-direction: column;
  }

  .photo-picker span,
  .text-button,
  .primary-button,
  .secondary-button {
    width: 100%;
    text-align: center;
  }

  .ratio-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .text-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}
