@font-face {
  font-family: "Noto Sans SC";
  src: url("./assets/NotoSansSC.woff2") format("woff2");
  font-style: normal;
  font-weight: 100 900;
  font-display: swap;
}

:root {
  --ink: #171714;
  --paper: #f1efe8;
  --panel: #fffdf7;
  --line: #d7d3c7;
  --line-strong: #b9b3a3;
  --accent: #df563e;
  --accent-strong: #c6452f;
  --green: #173c3a;
  --green-strong: #0e2a28;
  --muted: #77736a;
  --soft: #e8e2d4;
  --soft-strong: #ddd6c3;
  --focus: #2f6fda;
  --danger: #9d2f1e;
  --shadow-sm: 0 2px 8px rgba(23, 23, 20, 0.06);
  --shadow-md: 0 18px 48px rgba(23, 23, 20, 0.08);
  --shadow-lg: 0 22px 70px rgba(23, 23, 20, 0.18);
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --tap-min: 44px;
  --ease: cubic-bezier(0.16, 1, 0.3, 1);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--paper);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  text-rendering: optimizeLegibility;
}

body {
  min-height: 100%;
  min-height: 100dvh;
  margin: 0;
  background:
    linear-gradient(90deg, rgba(23, 23, 20, 0.045) 1px, transparent 1px),
    linear-gradient(180deg, rgba(23, 23, 20, 0.035) 1px, transparent 1px),
    var(--paper);
  background-size: 28px 28px;
  color: var(--ink);
  font-family: "Noto Sans SC", "Microsoft YaHei", -apple-system, BlinkMacSystemFont,
    "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

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

button {
  min-height: var(--tap-min);
  border: 0;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
  touch-action: manipulation;
  transition:
    transform 180ms var(--ease),
    background-color 180ms var(--ease),
    border-color 180ms var(--ease),
    box-shadow 180ms var(--ease),
    color 180ms var(--ease);
}

button:active {
  transform: translateY(1px) scale(0.99);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.48;
}

button:focus-visible,
input:focus-visible,
[role="tab"]:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--focus) 70%, white);
  outline-offset: 3px;
  border-radius: var(--radius-sm);
}

/* ===== Layout shell ===== */
.app-shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(360px, 520px);
  gap: clamp(28px, 4vw, 64px);
  width: min(100%, 1380px);
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  padding: clamp(24px, 4vw, 48px);
}

/* ===== Preview ===== */
.preview-section {
  display: grid;
  grid-template-rows: auto 1fr auto;
  align-items: center;
  min-width: 0;
}

.preview-heading {
  align-self: start;
}

.eyebrow {
  margin: 0 0 10px;
  color: var(--accent);
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  color: var(--ink);
  font-weight: 900;
  letter-spacing: -0.01em;
}

h1 {
  max-width: 11em;
  font-size: clamp(32px, 4vw, 58px);
  line-height: 1.05;
}

h2 {
  font-size: 24px;
  line-height: 1.2;
}

.canvas-frame {
  justify-self: center;
  width: min(100%, 405px);
  aspect-ratio: 9 / 16;
  padding: 10px;
  background: #22221f;
  border: 1px solid #2f2f2b;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  transition: transform 280ms var(--ease), box-shadow 280ms var(--ease);
}

.canvas-frame:hover {
  transform: translateY(-2px);
  box-shadow: 0 28px 80px rgba(23, 23, 20, 0.24);
}

#cover-canvas {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 9 / 16;
  background: #f4f1e8;
  border-radius: 2px;
}

.status-line {
  min-height: 28px;
  margin: 18px 0 0;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.6;
  text-align: center;
}

/* ===== Editor panel ===== */
.editor-panel {
  align-self: center;
  min-width: 0;
  padding: 28px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
}

.editor-header {
  margin-bottom: 22px;
}

.template-tabs {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 26px;
}

.template-tabs button {
  min-width: 0;
  min-height: var(--tap-min);
  padding: 12px 14px;
  color: var(--ink);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 800;
  text-align: left;
  box-shadow: var(--shadow-sm);
}

.template-tabs button:hover:not(.is-active) {
  border-color: var(--line-strong);
  background: var(--soft);
}

.template-tabs button.is-active,
.template-tabs button.active {
  color: #fffdf7;
  background: var(--green);
  border-color: var(--green);
  box-shadow: 0 6px 18px rgba(23, 60, 58, 0.28);
}

/* ===== Form fields ===== */
.dynamic-fields {
  display: grid;
  gap: 16px;
}

.form-field {
  display: grid;
  gap: 8px;
}

.form-field label {
  color: var(--ink);
  font-size: 14px;
  font-weight: 800;
}

.form-field input {
  width: 100%;
  min-height: var(--tap-min);
  padding: 11px 14px;
  color: var(--ink);
  background: #fffaf0;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  transition:
    border-color 180ms var(--ease),
    background-color 180ms var(--ease),
    box-shadow 180ms var(--ease);
  -webkit-appearance: none;
  appearance: none;
}

.form-field input::placeholder {
  color: color-mix(in srgb, var(--muted) 80%, transparent);
}

.form-field input:hover {
  border-color: var(--line-strong);
}

.form-field input:focus {
  border-color: var(--focus);
  background: #ffffff;
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--focus) 22%, transparent);
  outline: none;
}

.form-field input[type="checkbox"] {
  width: 22px;
  height: 22px;
  min-height: 22px;
  padding: 0;
  accent-color: var(--green);
  cursor: pointer;
}

.form-field--checkbox {
  grid-template-columns: auto 1fr;
  align-items: center;
  column-gap: 12px;
}

.form-field--checkbox label {
  cursor: pointer;
  font-weight: 700;
}

.field-counter {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
}

/* ===== Form actions ===== */
.form-actions {
  display: grid;
  grid-template-columns: minmax(0, 0.75fr) minmax(0, 1.25fr);
  gap: 12px;
  margin-top: 28px;
}

.secondary-action,
.primary-action {
  padding: 0 18px;
  font-weight: 900;
  font-size: 15px;
  letter-spacing: 0.02em;
  border-radius: var(--radius-sm);
}

.secondary-action {
  color: var(--ink);
  background: var(--soft);
  border: 1px solid var(--line);
}

.secondary-action:hover:not(:disabled) {
  background: var(--soft-strong);
  border-color: var(--line-strong);
}

.primary-action {
  color: #fffdf7;
  background: var(--accent);
  border: 1px solid var(--accent);
  box-shadow: 0 8px 20px rgba(223, 86, 62, 0.28);
}

.primary-action:hover:not(:disabled) {
  background: var(--accent-strong);
  border-color: var(--accent-strong);
  box-shadow: 0 10px 24px rgba(223, 86, 62, 0.36);
}

.form-error {
  min-height: 24px;
  margin: 14px 0 0;
  color: var(--danger);
  font-size: 14px;
  line-height: 1.6;
}

.form-error.has-error {
  padding: 10px 12px;
  background: color-mix(in srgb, var(--danger) 8%, transparent);
  border-left: 4px solid var(--danger);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

/* ===== Tablet (901px and below) ===== */
@media (max-width: 1024px) {
  .app-shell {
    grid-template-columns: minmax(0, 1fr) minmax(320px, 420px);
    gap: 32px;
    padding: 32px;
  }

  .editor-panel {
    padding: 24px;
  }
}

/* ===== Tablet / small landscape (900px and below) ===== */
@media (max-width: 900px) {
  .app-shell {
    grid-template-columns: 1fr;
    gap: 28px;
    padding: 20px;
  }

  .preview-section {
    gap: 18px;
  }

  h1 {
    font-size: 34px;
  }

  .canvas-frame {
    width: min(100%, 360px);
    padding: 8px;
  }

  .editor-panel {
    align-self: stretch;
    padding: 20px;
  }

  .template-tabs {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

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

/* ===== Phone (640px and below) ===== */
@media (max-width: 640px) {
  .app-shell {
    grid-template-columns: 1fr;
    gap: 20px;
    padding: 16px 14px;
    /* respect iOS safe areas */
    padding-top: max(16px, env(safe-area-inset-top));
    padding-bottom: max(16px, env(safe-area-inset-bottom));
    padding-left: max(14px, env(safe-area-inset-left));
    padding-right: max(14px, env(safe-area-inset-right));
  }

  h1 {
    font-size: 28px;
    line-height: 1.1;
  }

  h2 {
    font-size: 20px;
  }

  .eyebrow {
    font-size: 11px;
    letter-spacing: 0.14em;
  }

  .preview-heading {
    text-align: center;
  }

  .canvas-frame {
    width: min(100%, 320px);
    padding: 6px;
  }

  .canvas-frame:hover {
    transform: none;
  }

  .status-line {
    font-size: 13px;
    margin-top: 14px;
  }

  .editor-panel {
    padding: 18px;
    border-radius: var(--radius-md);
  }

  .editor-header {
    margin-bottom: 18px;
  }

  .template-tabs {
    display: flex;
    grid-template-columns: none;
    gap: 8px;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 2px 2px 8px;
    margin: 0 -2px 22px;
    scroll-snap-type: x proximity;
    scrollbar-width: thin;
    -webkit-overflow-scrolling: touch;
  }

  .template-tabs::-webkit-scrollbar {
    height: 4px;
  }

  .template-tabs::-webkit-scrollbar-thumb {
    background: var(--line-strong);
    border-radius: 2px;
  }

  .template-tabs button {
    flex: 0 0 auto;
    min-width: 120px;
    padding: 10px 14px;
    font-size: 13px;
    scroll-snap-align: start;
    box-shadow: none;
  }

  .dynamic-fields {
    gap: 14px;
  }

  .form-field label {
    font-size: 13px;
  }

  .form-field input {
    font-size: 16px; /* >= 16px avoids iOS zoom-on-focus */
    padding: 12px 14px;
  }

  .form-field--checkbox {
    grid-template-columns: auto 1fr;
    column-gap: 10px;
  }

  .form-actions {
    grid-template-columns: 1fr;
    gap: 10px;
    margin-top: 22px;
  }

  .secondary-action,
  .primary-action {
    min-height: 48px;
    font-size: 15px;
    padding: 0 16px;
  }

  .primary-action {
    width: 100%;
  }

  .form-error {
    font-size: 13px;
  }
}

/* ===== Tiny phones (380px and below) ===== */
@media (max-width: 380px) {
  h1 {
    font-size: 24px;
  }

  .canvas-frame {
    width: min(100%, 280px);
  }

  .template-tabs button {
    min-width: 104px;
    padding: 9px 12px;
    font-size: 12px;
  }

  .editor-panel {
    padding: 16px;
  }
}

/* ===== Landscape phones ===== */
@media (max-height: 500px) and (orientation: landscape) {
  .app-shell {
    grid-template-columns: minmax(0, 1fr) minmax(360px, 460px);
    padding: 12px 16px;
  }

  .preview-section {
    grid-template-rows: auto 1fr auto;
  }

  h1 {
    font-size: 22px;
  }

  h2 {
    font-size: 18px;
  }

  .eyebrow {
    margin-bottom: 6px;
  }

  .canvas-frame {
    width: min(100%, 220px);
  }

  .editor-panel {
    padding: 16px;
    max-height: calc(100dvh - 24px);
    overflow-y: auto;
  }
}

/* ===== Print / export ===== */
@media print {
  body {
    background: none;
  }
  .editor-panel {
    display: none;
  }
}

/* ===== Reduced motion ===== */
@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;
  }
}
