:root {
  --bg: #eef3f0;
  --ink: #142019;
  --muted: #66736c;
  --card: #fffcf7;
  --line: #d7e0da;
  --accent: #1f6b54;
  --accent-2: #154c3c;
  --soft: rgba(31, 107, 84, 0.12);
  --shadow: 0 18px 40px rgba(20, 32, 25, 0.08);
  --radius: 18px;
  --font: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
}

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

body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(800px 360px at 0% 0%, #dceee5, transparent 55%),
    radial-gradient(700px 300px at 100% 0%, #efe6d8, transparent 50%),
    var(--bg);
}

.app {
  width: min(720px, calc(100% - 1.5rem));
  margin: 1.1rem auto 2rem;
  display: grid;
  gap: 0.85rem;
}

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.top h1 {
  margin: 0;
  font-size: 1.35rem;
  letter-spacing: -0.03em;
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1rem;
}

.tpl-one {
  margin: 0 0 0.9rem;
  font-size: 0.92rem;
}
.tpl-list {
  display: none;
}

.tpl-card {
  position: relative;
  display: grid;
  gap: 0.45rem;
  border: 2px solid var(--line);
  border-radius: 14px;
  padding: 0.55rem;
  background: #fff;
  cursor: pointer;
  transition: border-color .15s ease, box-shadow .15s ease, transform .12s ease;
}

.tpl-card:hover { transform: translateY(-1px); }
.tpl-card.is-selected,
.tpl-card:has(input:checked) {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--soft);
}

.tpl-card input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.tpl-preview {
  display: block;
  aspect-ratio: 4/3;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid #e4dfd5;
  background: #f7f3ea;
}

.tpl-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: top center;
  display: block;
}

.tpl-caption {
  text-align: center;
  font-size: 0.86rem;
  font-weight: 700;
}

.fields {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 0.65rem;
  margin-bottom: 0.85rem;
}

.field {
  display: grid;
  gap: 0.3rem;
  min-width: 0;
}

.field-wide { grid-column: 1 / -1; }

.field > span {
  font-size: 0.75rem;
  color: var(--muted);
  font-weight: 700;
}

input[type="text"],
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.7rem 0.8rem;
  font: inherit;
  font-size: 0.95rem;
  background: #fff;
  color: var(--ink);
  outline: none;
}

input[type="text"]:focus,
textarea:focus {
  border-color: color-mix(in srgb, var(--accent) 50%, var(--line));
  box-shadow: 0 0 0 3px var(--soft);
}

textarea {
  resize: vertical;
  min-height: 180px;
  line-height: 1.45;
}

.stamps { margin-bottom: 0.9rem; }

.sig-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 0.55rem;
}

.sig-card {
  position: relative;
  border: 2px solid var(--line);
  border-radius: 12px;
  padding: 0.55rem;
  background: #fff;
  cursor: pointer;
  text-align: center;
}

.sig-card img {
  display: block;
  width: 100%;
  height: 64px;
  object-fit: contain;
  margin-bottom: 0.25rem;
}

.sig-card.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--soft);
}

.sig-card .sig-del {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 22px;
  height: 22px;
  border: none;
  border-radius: 999px;
  background: rgba(20,20,20,.7);
  color: #fff;
  cursor: pointer;
}

.sig-empty {
  color: var(--muted);
  font-size: 0.86rem;
  padding: 0.4rem 0;
}

.stamp-admin {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  margin-top: 0.55rem;
}

.upload-chip {
  position: relative;
  display: inline-flex;
  align-items: center;
  border: 1px dashed color-mix(in srgb, var(--accent) 45%, var(--line));
  border-radius: 999px;
  padding: 0.4rem 0.75rem;
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--accent-2);
  background: var(--soft);
  cursor: pointer;
}

.upload-chip input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 999px;
  padding: 0.78rem 1.2rem;
  font: inherit;
  font-size: 0.94rem;
  font-weight: 750;
  cursor: pointer;
  text-decoration: none;
  color: inherit;
}

.btn.block { width: 100%; }
.btn.sm { padding: 0.45rem 0.85rem; font-size: 0.82rem; }
.btn.primary {
  background: linear-gradient(145deg, #2a8a6c, var(--accent-2));
  color: #fff;
  box-shadow: 0 10px 22px rgba(21, 76, 60, 0.22);
}
.btn.ghost {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--ink);
}

.preview-card { padding: 0.75rem; }
.preview-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.55rem;
  font-weight: 700;
  font-size: 0.9rem;
}

#preview {
  width: 100%;
  min-height: 640px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #ece8e0;
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(16, 22, 19, 0.45);
  display: grid;
  place-items: center;
  padding: 1rem;
  z-index: 40;
}

.modal-card {
  width: min(640px, 100%);
  max-height: min(90vh, 860px);
  overflow: auto;
  background: var(--card);
  border-radius: 18px;
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  padding: 1rem;
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.7rem;
}

.modal-head h2 { margin: 0; font-size: 1.1rem; }

.icon-x {
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 999px;
  background: transparent;
  font-size: 1.4rem;
  cursor: pointer;
  color: var(--muted);
}

.modal-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.4rem;
  margin-bottom: 0.75rem;
}

.tab {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 999px;
  padding: 0.55rem 0.7rem;
  font: inherit;
  font-weight: 700;
  font-size: 0.86rem;
  cursor: pointer;
}

.tab.is-active {
  background: var(--soft);
  border-color: var(--accent);
  color: var(--accent-2);
}

.var-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin: 0.55rem 0 0.85rem;
}

.chip {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 999px;
  padding: 0.35rem 0.65rem;
  font: inherit;
  font-size: 0.78rem;
  font-weight: 700;
  cursor: pointer;
  color: var(--accent-2);
}

.chip:hover { background: var(--soft); }

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
}

.modal-msg {
  margin: 0.65rem 0 0;
  font-size: 0.84rem;
  color: #157a4d;
}

@media (max-width: 640px) {
  .fields { grid-template-columns: 1fr 1fr; }
  .field-wide { grid-column: 1 / -1; }
  #preview { min-height: 480px; }
}

@media (max-width: 420px) {
  .tpl-list,
  .fields { grid-template-columns: 1fr; }
}
