:root {
  --bg: #f3f1ec;
  --surface: #ffffff;
  --text: #1b2430;
  --muted: #5d6673;
  --line: #e2ded5;
  --brand: #10243a;
  --brand-ink: #ffffff;
  --gold: #b88a2c;
  --danger: #b42318;
  --ok: #1d7a46;
  --radius: 12px;
  color-scheme: light;
}

* { box-sizing: border-box; }
/* Component display rules (e.g. .btn inline-flex) must never override the hidden attribute */
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'Kantumruy Pro', 'Khmer OS Battambang', sans-serif;
  font-size: 15px;
  line-height: 1.6;
}
button, input, select, textarea { font: inherit; color: inherit; }

/* Top bar */
.topbar {
  position: relative;
  z-index: 10;
  display: flex;
  flex-wrap: wrap;
  gap: 10px 16px;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  background: var(--brand);
  color: var(--brand-ink);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.12);
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark { color: var(--gold); font-size: 26px; line-height: 1; }
.brand h1 { margin: 0; font-size: 18px; font-weight: 700; line-height: 1.4; }
.brand p { margin: 0; font-size: 12.5px; opacity: 0.8; }
.topbar-actions { display: flex; flex-wrap: wrap; gap: 8px; width: 100%; }
.topbar-actions .btn { flex: 1 1 0; white-space: normal; text-align: center; line-height: 1.4; }
.topbar .btn-ghost { background: transparent; color: #fff; border-color: rgba(255, 255, 255, 0.35); }
.topbar .btn-ghost:hover { background: rgba(255, 255, 255, 0.1); }
.topbar .btn-primary { background: var(--gold); color: #1b1305; border-color: var(--gold); }

/* Layout: stacked on phone, side-by-side on wide screens */
.layout { display: flex; flex-direction: column; gap: 16px; padding: 16px; }
.preview { order: -1; }
.panel { display: flex; flex-direction: column; gap: 12px; }

@media (min-width: 980px) {
  .topbar { position: sticky; top: 0; padding: 12px 24px; }
  .topbar-actions { width: auto; }
  .topbar-actions .btn { flex: none; white-space: nowrap; }
  .layout { flex-direction: row; align-items: flex-start; padding: 20px 24px; gap: 20px; }
  .panel { width: 400px; flex: none; }
  .preview { order: 0; flex: 1; position: sticky; top: 84px; min-width: 0; }
}

/* Cards */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.card > summary {
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 48px;
  padding: 8px 16px;
  font-weight: 700;
  color: var(--brand);
}
.card > summary::-webkit-details-marker { display: none; }
.card > summary::after { content: '▾'; margin-left: auto; color: var(--muted); transition: transform 0.15s; }
.card:not([open]) > summary::after { transform: rotate(-90deg); }
.fields { display: flex; flex-direction: column; gap: 10px; padding: 0 16px 16px; }
#signer2-fields { padding: 0; }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; flex: 1; }
.field > span { font-size: 13px; font-weight: 600; color: var(--muted); }
.field-row { display: flex; gap: 10px; }
input:not([type='checkbox']):not([type='radio']):not([type='file']), select, textarea {
  width: 100%;
  min-height: 44px;
  padding: 8px 12px;
  background: #fff;
  border: 1px solid #cfcac0;
  border-radius: 8px;
}
textarea { resize: vertical; }
input:focus-visible, select:focus-visible, textarea:focus-visible, .btn:focus-visible, summary:focus-visible {
  outline: 3px solid rgba(184, 138, 44, 0.45);
  outline-offset: 1px;
}
.check { display: flex; align-items: center; gap: 10px; min-height: 44px; cursor: pointer; }
.check input { width: 20px; height: 20px; accent-color: var(--brand); flex: none; }

.hint { margin: 0; font-size: 13px; color: var(--muted); }
.hint.center { text-align: center; margin-top: 10px; }
.hint.error { color: var(--danger); font-weight: 600; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 8px 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}
.btn:hover { background: #f7f5f0; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-primary:hover { background: #1b3754; }
.btn-ghost { background: #fff; }
.btn-danger-ghost { color: var(--danger); border-color: #f0c9c5; background: #fff; }
.btn-link { border: 0; background: none; color: var(--muted); text-decoration: underline; font-weight: 500; }
.btn-icon { width: 44px; padding: 0; font-size: 24px; line-height: 1; }
.btn-file { position: relative; overflow: hidden; }
.btn-file input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; }
.btn-row .btn { flex: 1 1 auto; }

.image-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.image-preview {
  width: 88px;
  height: 44px;
  object-fit: contain;
  border: 1px solid var(--line);
  border-radius: 8px;
  /* light checkerboard shows that the scan background was removed */
  background: repeating-conic-gradient(#f1efe9 0 25%, #fff 0 50%) 0 0 / 12px 12px;
}
[data-image='logo'] .image-preview { width: 44px; }

.themes { display: flex; flex-wrap: wrap; gap: 8px; }
.theme-swatch {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  padding: 6px 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  cursor: pointer;
  font-size: 14px;
}
.theme-swatch input { position: absolute; opacity: 0; pointer-events: none; }
.theme-swatch i { width: 22px; height: 22px; border-radius: 50%; background: linear-gradient(135deg, var(--a) 55%, var(--b) 55%); }
.theme-swatch:has(input:checked) { border-color: var(--brand); box-shadow: 0 0 0 2px var(--brand) inset; font-weight: 700; }
.theme-swatch:has(input:focus-visible) { outline: 3px solid rgba(184, 138, 44, 0.45); }

.sizes { display: flex; flex-direction: column; gap: 2px; margin-bottom: 8px; }
.size-row { display: grid; grid-template-columns: 110px 1fr 52px; align-items: center; gap: 10px; min-height: 44px; }
.size-row > span { font-size: 14px; }
.size-row input[type='range'] { width: 100%; height: 44px; margin: 0; accent-color: var(--brand); cursor: pointer; }
.size-row output { text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; color: var(--brand); }
#overflow-warning { margin-bottom: 10px; }

.fields-inline { display: flex; flex-direction: column; gap: 10px; }
.badge { padding: 0 8px; border-radius: 999px; background: #eceae4; color: var(--muted); font-size: 12.5px; font-weight: 600; }
.badge.on { background: #e7f5ec; color: var(--ok); }

/* Participants */
.count {
  min-width: 26px;
  padding: 0 8px;
  border-radius: 999px;
  background: var(--gold);
  color: #1b1305;
  font-size: 13px;
  text-align: center;
}
.add-form { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; align-items: end; }
.add-form .field:first-child, .add-form .field:nth-child(2) { grid-column: span 2; }
@media (min-width: 420px) {
  .add-form .field:first-child, .add-form .field:nth-child(2) { grid-column: auto; }
}
.import-box { display: flex; flex-direction: column; gap: 8px; padding: 12px; border: 1px dashed #cfc7b6; border-radius: 10px; background: #fcfbf8; }
.paste > summary { cursor: pointer; min-height: 44px; display: flex; align-items: center; color: var(--brand); font-weight: 600; }
.paste textarea { margin-bottom: 8px; }

.status { margin: 0; padding: 10px 12px; border-radius: 8px; font-size: 14px; white-space: pre-line; }
.status.ok { background: #e7f5ec; color: var(--ok); }
.status.error { background: #fdecea; color: var(--danger); }

.people { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; max-height: 360px; overflow: auto; }
.person {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 6px 6px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}
.person.active { border-color: var(--brand); background: #f1f4f8; }
.person-main { flex: 1; min-width: 0; min-height: 44px; display: flex; flex-direction: column; justify-content: center; text-align: left; border: 0; background: none; padding: 0; cursor: pointer; }
.person-main b { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.person-main small { color: var(--muted); font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.person-no { color: var(--muted); font-size: 13px; min-width: 22px; }
.person-del { width: 44px; height: 44px; flex: none; border: 0; border-radius: 8px; background: none; color: var(--danger); font-size: 20px; cursor: pointer; }
.person-del:hover { background: #fdecea; }
.empty { margin: 0; padding: 16px; text-align: center; color: var(--muted); font-size: 14px; background: #faf9f6; border-radius: 8px; }

/* Preview */
.preview-bar { display: flex; align-items: center; justify-content: center; gap: 12px; margin-bottom: 10px; }
#preview-label { min-width: 0; text-align: center; font-weight: 600; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stage { position: relative; width: 100%; overflow: hidden; border-radius: 6px; box-shadow: 0 10px 30px rgba(16, 36, 58, 0.18), 0 2px 6px rgba(16, 36, 58, 0.1); }
.stage-inner { position: absolute; top: 0; left: 0; transform-origin: 0 0; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 20px;
  z-index: 20;
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 12px 18px;
  border-radius: 10px;
  background: #1b2430;
  color: #fff;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
}

/* Off-screen but measurable, so names can be auto-fitted before printing */
#print-area { position: absolute; left: -10000px; top: 0; }

@media print {
  @page { size: A4 landscape; margin: 0; }
  html, body { margin: 0; background: #fff; }
  body > :not(#print-area) { display: none !important; }
  #print-area { position: static; }
  #print-area .cert { break-after: page; page-break-after: always; }
  #print-area .cert:last-child { break-after: auto; page-break-after: auto; }
  .print-empty { padding: 40mm 30mm; text-align: center; font-size: 16pt; color: #1b2430; }
  .print-empty b { font-size: 22pt; }
}
