:root {
  --paper: #f7f0e6;
  --paper-dark: #ebe2d4;
  --ink: #1a1510;
  --muted: #5c5346;
  --red: #b32424;
  --red-deep: #8b1c1c;
  --win: #0d5c3c;
  --lose: #6b1c2e;
  --line: #c9bbab;
  --radius: 16px;
  --shadow: 0 4px 24px rgba(45, 30, 20, 0.08);
  --font-serif: "Noto Serif SC", "Songti SC", STSong, serif;
  --font-sans: "Noto Sans SC", -apple-system, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100dvh;
  font-family: var(--font-sans);
  color: var(--ink);
  background: linear-gradient(160deg, #2a1210 0%, #4a1f1c 32%, #6b2d28 100%);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.bg-noise {
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: 0.04;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='4'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  z-index: 0;
}

.app {
  position: relative;
  z-index: 1;
  max-width: 520px;
  margin: 0 auto;
  min-height: 100dvh;
  padding: 0 16px 48px;
  padding-top: max(12px, env(safe-area-inset-top));
}

.header {
  text-align: center;
  padding: 8px 0 16px;
}

.back {
  display: inline-block;
  color: #f0d4c0;
  text-decoration: none;
  font-size: 0.85rem;
  font-weight: 500;
  margin-bottom: 12px;
  opacity: 0.85;
  transition: opacity 0.2s;
}
.back:hover, .back:focus-visible { opacity: 1; }

.brand h1 {
  font-family: var(--font-serif);
  font-size: clamp(1.75rem, 5.5vw, 2.1rem);
  font-weight: 900;
  color: #fff7ec;
  letter-spacing: 0.08em;
  margin: 6px 0 4px;
  text-shadow: 0 2px 20px rgba(0,0,0,0.3);
}
.tagline { font-size: 0.8rem; color: #e8c9b0; margin: 0; }

.brand-ribbon {
  display: inline-block;
  background: linear-gradient(90deg, var(--red-deep), var(--red));
  color: #fff;
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.25em;
  padding: 4px 14px 4px 16px;
  border-radius: 4px;
  box-shadow: 0 2px 0 #5a1010, 0 4px 12px rgba(0,0,0,0.25);
}

.form-main { display: block; scroll-margin-top: 12px; }
#surrender-wall { scroll-margin-top: 8px; }

/* 模板选择 */
.tpl-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
@media (min-width: 400px) {
  .tpl-grid { grid-template-columns: repeat(3, 1fr); }
}
.tpl-item {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin: 0;
  padding: 10px 8px 10px 10px;
  background: #fff;
  border: 2px solid var(--line);
  border-radius: 12px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, box-shadow 0.15s;
  min-height: 3.2rem;
}
.tpl-item:has(input:checked),
.tpl-item.tpl-item--on {
  border-color: #c24a4a;
  background: #fff5f0;
  box-shadow: 0 0 0 1px rgba(194, 74, 74, 0.25);
}
.tpl-item input {
  position: absolute;
  opacity: 0;
  width: 0; height: 0;
  pointer-events: none;
}
.tpl-t { font-size: 0.82rem; font-weight: 700; color: var(--ink); }
.tpl-d { font-size: 0.65rem; color: var(--muted); line-height: 1.35; }
.debt-cats { margin-top: 10px; padding: 10px; background: #fff8f0; border: 1px dashed #d4a99a; border-radius: 10px; }
.debt-cats-t { display: block; font-size: 0.75rem; color: var(--muted); margin-bottom: 8px; }
.debt-cats-row { display: flex; flex-wrap: wrap; gap: 8px 12px; }
.debt-chk { font-size: 0.78rem; color: var(--ink); cursor: pointer; }

/* 当前模板成稿风格预览 */
.tpl-style-preview {
  background: linear-gradient(165deg, #fdf8f0 0%, #f2e8d8 100%);
  border: 1px solid #c9a88a;
  border-radius: 12px;
  padding: 0 0 12px;
  margin-bottom: 14px;
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.5), 0 4px 16px rgba(0,0,0,0.06);
  overflow: hidden;
}
.tpl-style-ribbon {
  background: linear-gradient(90deg, #7a1212, #9e2020);
  color: #fff;
  font-size: 0.58rem;
  font-weight: 700;
  letter-spacing: 0.18em;
  text-align: center;
  padding: 6px 8px 7px;
}
.tpl-style-title {
  font-family: var(--font-serif);
  font-size: 1.15rem;
  font-weight: 900;
  text-align: center;
  color: #1a1510;
  margin: 10px 12px 4px;
  letter-spacing: 0.1em;
  line-height: 1.25;
}
.tpl-style-sub {
  font-size: 0.68rem;
  color: #6b4a3a;
  text-align: center;
  margin: 0 12px 8px;
  font-weight: 600;
}
.tpl-style-sample {
  font-size: 0.68rem;
  color: #3d3028;
  line-height: 1.5;
  margin: 0 12px 6px;
  padding-left: 8px;
  border-left: 2px solid rgba(180, 50, 50, 0.25);
}
.tpl-style-punch {
  font-size: 0.78rem;
  font-weight: 800;
  color: #6b0f0f;
  text-align: center;
  margin: 6px 12px 8px;
  font-family: var(--font-serif);
}
.tpl-style-punch[hidden] { display: none; }
.tpl-style-footer {
  font-size: 0.58rem;
  color: #8a7a6a;
  text-align: center;
  margin: 8px 8px 0;
  padding-top: 6px;
  border-top: 1px dashed #c4b0a0;
}

/* Cards */
.card {
  background: var(--paper);
  border-radius: var(--radius);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  padding: 16px 16px 18px;
  margin-bottom: 14px;
}
.card-h {
  font-family: var(--font-serif);
  font-size: 1.05rem;
  font-weight: 700;
  margin: 0 0 12px;
  color: var(--ink);
}
.card-h .opt { font-size: 0.75rem; font-weight: 500; color: var(--muted); }

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 10px;
}
.field span { font-size: 0.78rem; color: var(--muted); font-weight: 500; }
.field input {
  width: 100%;
  font-size: 1rem;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  font-family: inherit;
}
.field input:focus {
  outline: 2px solid rgba(179, 36, 36, 0.25);
  border-color: #d49a8c;
}
.row-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; }
.row-3 .field { margin-bottom: 0; }
.date-bar { margin-bottom: 4px; }
.date-quick { display: flex; gap: 8px; margin-top: 8px; }
.btn-today {
  flex: 1;
  max-width: 120px;
  background: #fff5eb;
  color: var(--red-deep);
  border: 1px solid #d4a99a;
  font-size: 0.8rem;
  font-weight: 600;
  padding: 8px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-family: inherit;
}
.btn-today:hover, .btn-today:focus-visible { background: #fff; box-shadow: 0 0 0 2px rgba(179, 36, 36, 0.2); }

.hint { font-size: 0.78rem; color: var(--muted); margin: 0 0 8px; }

.sign-section.lose { border-top: 3px solid var(--red); }
.side-head { margin-bottom: 8px; }
.badge {
  display: inline-block;
  font-size: 0.65rem;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 6px;
  vertical-align: 2px;
}
.badge.win { background: #e6f4ea; color: var(--win); }
.badge.lose { background: #fce8ed; color: var(--lose); }

.signer-row {
  background: #fff;
  border: 1px dashed #c8b8a4;
  border-radius: 12px;
  padding: 12px;
  margin-bottom: 10px;
  position: relative;
}
.signer-row label.name-lab { font-size: 0.8rem; color: var(--muted); }
.signer-row input.name-inp {
  width: 100%;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 0.95rem;
  margin: 4px 0 8px;
}
.sign-label { font-size: 0.72rem; color: #888; margin-bottom: 4px; }
.sign-label-tip { font-weight: 400; color: #a89888; font-size: 0.65rem; }
.sign-pad-wrap {
  position: relative;
  display: block;
  width: 100%;
  border-radius: 10px;
  border: 2px solid #c4a88a;
  background: linear-gradient(180deg, #fffef9 0%, #faf6ed 100%);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.sign-pad-wrap:hover, .sign-pad-wrap:focus-visible {
  outline: none;
  border-color: #9a4a4a;
  box-shadow: 0 0 0 3px rgba(179, 36, 36, 0.15);
}
.sign-pad-wrap.sign-pad-wrap--done { border-color: #8a9a7a; }
.sign-pad-hint {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  font-size: 0.85rem;
  color: #a09080;
  pointer-events: none;
  max-width: 90%;
  text-align: center;
  line-height: 1.4;
  z-index: 1;
}
.sign-pad-wrap.sign-pad-wrap--done .sign-pad-hint { display: none; }
.sign-pad-tools {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-top: 6px;
}
.btn-sign-tool {
  font-size: 0.7rem;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 8px;
  border: 1px solid #c9bbab;
  background: #fff9f0;
  color: #4a3f36;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  line-height: 1.2;
  font-family: inherit;
  transition: background 0.12s, border-color 0.12s;
}
.btn-sign-tool:hover, .btn-sign-tool:focus-visible {
  background: #fff0e0;
  border-color: #b32424;
  color: #2a1f1a;
  outline: none;
}
.btn-sign-undo:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  border-color: #ddd8d0;
  color: #a0a0a0;
  background: #f3efea;
}
.sign-pad {
  display: block;
  width: 100%;
  height: 120px;
  border-radius: 8px;
  background: #faf8f3;
  border: none;
  touch-action: none;
  cursor: crosshair;
  vertical-align: top;
  position: relative;
  z-index: 0;
}
.btn-remove-signer {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 8px;
  background: #f0e4d4;
  color: #666;
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
}
.btn-remove-signer:hover { background: #e5d0bc; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  padding: 12px 18px;
  border-radius: 12px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: transform 0.12s, box-shadow 0.2s;
}
.btn:active { transform: scale(0.98); }
.btn-primary {
  background: linear-gradient(180deg, #d42f2f, #b32424);
  color: #fff;
  box-shadow: 0 4px 0 #6e1515, 0 6px 16px rgba(0,0,0,0.2);
  width: 100%;
  padding: 14px 20px;
  font-size: 1.02rem;
}
.btn-primary:hover, .btn-primary:focus-visible {
  box-shadow: 0 4px 0 #6e1515, 0 8px 20px rgba(0,0,0,0.25);
  outline: none;
}
.btn-ghost {
  background: rgba(255,255,255,0.1);
  color: #f5e6dc;
  border-color: rgba(255,255,255,0.2);
  width: 100%;
  margin-top: 4px;
}
.form-card .btn-ghost {
  background: #fff8f0;
  color: var(--red);
  border-color: #e0c4b0;
  margin-top: 6px;
}
.btn-cam {
  flex: 1;
  min-width: 0;
  background: #fff;
  color: var(--ink);
  border: 1px solid var(--line);
  text-align: center;
  padding: 10px 8px;
  cursor: pointer;
  font-size: 0.85rem;
  border-radius: 10px;
}
.btn-cam:has(input:focus) { outline: 2px solid rgba(179, 36, 36, 0.35); }
.photo-row { display: flex; flex-direction: column; gap: 10px; }
.photo-actions { display: flex; gap: 8px; }
.photo-preview {
  position: relative;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--line);
  background: #000;
  max-height: 300px;
}
.photo-preview img {
  width: 100%;
  display: block;
  object-fit: cover;
  max-height: 300px;
  opacity: 0.95;
}
.btn-mini { font-size: 0.8rem; padding: 6px 12px; border-radius: 8px; background: #fff; border: 1px solid var(--line); cursor: pointer; }

.actions { margin: 8px 0 20px; }
.err { color: #b91c1c; font-size: 0.85rem; margin: 8px 0 0; min-height: 1.2em; }

.result-card { border: 2px solid var(--red); }
.result-wrap { border-radius: 10px; overflow: auto; -webkit-overflow-scrolling: touch; text-align: center; background: #1a0f0a; }
.result-wrap canvas, .result-wrap img { max-width: 100%; height: auto; vertical-align: top; }
.result-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; }
.result-actions .btn-primary { width: 100%; }
.result-actions .btn-ghost { color: var(--ink); }

/* 公开墙 */
.wall-section { margin-top: 28px; padding-top: 20px; border-top: 1px solid rgba(255,255,255,0.12); }
.wall-heading {
  font-family: var(--font-serif);
  font-size: 1.25rem;
  font-weight: 800;
  color: #fff5eb;
  text-align: center;
  margin: 0 0 8px;
  letter-spacing: 0.06em;
}
.wall-sub { font-size: 0.75rem; color: #d4b8a0; line-height: 1.6; margin: 0 0 12px; }
.wall-sub code { font-size: 0.7rem; color: #f0c9a8; }
.wall-note { font-size: 0.75rem; color: #a89080; margin: 8px 0 0; }
.wall-intro { font-size: 0.82rem; color: #e8c9b0; margin-bottom: 16px; line-height: 1.6; }
.wall-intro p { margin: 0 0 8px; }
.wall-actions-bar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.wall-list {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
@media (min-width: 400px) {
  .wall-list { grid-template-columns: repeat(2, 1fr); }
}
.wall-tile {
  display: flex;
  flex-direction: column;
  gap: 0;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0;
  box-shadow: var(--shadow);
  cursor: pointer;
  text-align: left;
  transition: transform 0.15s, box-shadow 0.2s;
  width: 100%;
  min-height: 0;
  font: inherit;
  color: inherit;
  appearance: none;
  overflow: hidden;
}
.wall-tile:hover, .wall-tile:focus-visible {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(0,0,0,0.12);
}
.wall-tile__img {
  width: 100%;
  aspect-ratio: 0.7;
  object-fit: cover;
  object-position: top center;
  display: block;
  background: #2a1810;
}
.wall-tile .meta { padding: 8px 10px 10px; min-width: 0; }
.wall-tile .date { font-size: 0.65rem; color: var(--muted); }
.wall-tile .t { font-size: 0.8rem; font-weight: 600; margin: 2px 0; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; line-height: 1.3; }
.wall-tile .sub { font-size: 0.68rem; color: #666; }

.empty {
  text-align: center;
  color: #c9a48c;
  font-size: 0.9rem;
  padding: 32px 12px;
}
.empty[hidden] { display: none; }

.footer { text-align: center; margin-top: 28px; font-size: 0.75rem; color: rgba(255,255,255,0.4); }

/* Modal */
.modal { position: fixed; inset: 0; z-index: 200; display: flex; align-items: flex-end; justify-content: center; padding: 16px; }
.modal[hidden] { display: none; }
.modal-back { position: absolute; inset: 0; background: rgba(0,0,0,0.6); }
.modal-box {
  position: relative;
  background: #fff5eb;
  width: 100%;
  max-width: 400px;
  max-height: 86vh;
  border-radius: 20px 20px 12px 12px;
  padding: 16px 16px 20px;
  overflow: auto;
  box-shadow: 0 -8px 40px rgba(0,0,0,0.35);
}
#modal-title { font-family: var(--font-serif); margin: 0 0 8px; font-size: 1.1rem; }
#modal-body { margin-bottom: 12px; }
#modal-body img, #modal-body canvas { max-width: 100%; height: auto; }

@media (min-width: 600px) {
  .app { max-width: 440px; }
  .bg-noise { opacity: 0.06; }
}
