:root {
  --accent: #1f4e79;
  --ui-bg: #eef0f3;
  --ui-panel: #ffffff;
  --ui-border: #d5d9df;
  --ui-text: #1c2430;
  --ui-muted: #5d6877;
}

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

body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  grid-template-columns: 340px 1fr;
  background: var(--ui-bg);
  color: var(--ui-text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

/* ---------- Which parts show in which mode ---------- */

html.group .solo-only { display: none; }
html:not(.group) .group-only { display: none; }
html.group:not(.joined) #editor { display: none; }

/* ---------- Controls ---------- */

.controls {
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  padding: 20px;
  background: var(--ui-panel);
  border-right: 1px solid var(--ui-border);
}

.controls, .stack { display: flex; flex-direction: column; gap: 14px; }

.controls h1 { margin: 0; font-size: 20px; overflow-wrap: anywhere; }
.controls h2 { margin: 6px 0 0; font-size: 15px; }

.callout {
  padding: 14px;
  border: 1px solid var(--ui-border);
  border-radius: 8px;
  background: #f7f8fa;
}

.field { display: flex; flex-direction: column; gap: 5px; }
.field > span, .label-row > span, .label { font-weight: 600; font-size: 13px; }
.label-row { display: flex; justify-content: space-between; align-items: baseline; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

input[type="text"], input[type="number"], select, textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--ui-border);
  border-radius: 6px;
  background: #fff;
  color: inherit;
  font: inherit;
}
textarea { min-height: 220px; resize: vertical; line-height: 1.35; }
input:disabled { background: #f3f4f6; color: var(--ui-muted); }
input[type="color"] {
  width: 100%; height: 37px; padding: 2px;
  border: 1px solid var(--ui-border); border-radius: 6px; background: #fff;
}
:is(input, select, textarea, button, a):focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.check { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 13px; }
.check input { width: 16px; height: 16px; margin: 0; }

.hint { margin: 0; font-size: 13px; color: var(--ui-muted); }
.hint.warn { color: #a14a00; }
.hint strong { color: var(--ui-text); }

.link {
  padding: 0; border: 0; background: none;
  color: var(--ui-muted); font: inherit; font-size: 13px;
  text-decoration: underline; cursor: pointer;
  align-self: flex-start;
}

.group-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.group-list a { color: var(--accent); font-weight: 600; overflow-wrap: anywhere; }

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.btn {
  padding: 10px 12px;
  border: 1px solid var(--ui-border);
  border-radius: 6px;
  background: #fff;
  color: inherit;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.btn:hover { background: #f5f6f8; }
.btn:disabled { opacity: .6; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.12); }
.btn.wide { grid-column: 1 / -1; }

.share-out { display: flex; flex-direction: column; gap: 6px; }
.share-out input { font-size: 13px; color: var(--ui-muted); }

/* ---------- Shared-card view (opened from a single-card share link) ---------- */

.viewbar { display: none; }
body.viewing { display: block; }
body.viewing .controls { display: none; }
body.viewing .viewbar {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px 16px;
  padding: 12px 20px;
  background: var(--ui-panel);
  border-bottom: 1px solid var(--ui-border);
}
.viewbar strong { font-size: 16px; }
.viewbar-actions { display: flex; flex-wrap: wrap; gap: 8px; }

/* ---------- Preview: single-card maker (each .sheet is one printed page) ---------- */

.preview {
  padding: 24px;
  display: flex;
  flex-wrap: wrap;
  gap: 24px;
  justify-content: center;
  align-content: flex-start;
}

.sheet {
  width: min(100%, 560px);
  aspect-ratio: 8.5 / 11;
  padding: 5.88%; /* 0.5in margin on a letter page */
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .15), 0 6px 20px rgba(0, 0, 0, .08);
}

.sheet.per-1 .card { width: 100%; }

.sheet.per-2 { display: flex; flex-direction: column; align-items: center; }
.sheet.per-2 .card { width: 55%; }
.sheet.per-2 .card + .card { margin-top: 4%; }

.sheet.per-4 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  column-gap: 4%;
  align-content: start;
}
.sheet.per-4 .card:nth-child(n + 3) { margin-top: 8%; }

/* ---------- Preview: group (one tile per person) ---------- */

html.group .preview { display: block; }

.gallery-title { margin: 0 0 16px; font-size: 18px; }
.empty { margin: 0; color: var(--ui-muted); }

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 340px), 1fr));
  gap: 20px;
  align-items: start;
}

.tile {
  padding: 14px;
  border-radius: 10px;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .12), 0 4px 14px rgba(0, 0, 0, .06);
}
.tile-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.tile-name { font-weight: 700; font-size: 16px; overflow-wrap: anywhere; }
.tile-head .link { margin-left: auto; align-self: center; flex: none; }
.badge {
  flex: none;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .04em;
  text-transform: uppercase;
}

/* ---------- Card (sized in cqw so it scales identically on screen and paper) ---------- */

.card { container-type: inline-size; }

.card-inner {
  padding: 3cqw;
  border: max(1px, .6cqw) solid var(--accent);
  border-radius: 2.5cqw;
  background: #fff;
  color: #111;
  font-family: "Avenir Next", "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
}

.card-title {
  margin: 0 0 1.5cqw;
  height: 10cqw;
  line-height: 10cqw;
  font-size: 8cqw;
  font-weight: 800;
  text-align: center;
  color: var(--accent);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.grid {
  aspect-ratio: 1;
  display: grid;
  grid-template: repeat(var(--n), minmax(0, 1fr)) / repeat(var(--n), minmax(0, 1fr));
  border-top: max(1px, .3cqw) solid #111;
  border-left: max(1px, .3cqw) solid #111;
}

.cell {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.2cqw;
  overflow: hidden;
  border-right: max(1px, .3cqw) solid #111;
  border-bottom: max(1px, .3cqw) solid #111;
  font-weight: 600;
  line-height: 1.15;
}
.cell span {
  display: block;
  width: 100%;
  text-align: center;
  overflow-wrap: anywhere;
}
.cell.free {
  background: color-mix(in srgb, var(--accent) 14%, #fff);
  color: var(--accent);
  font-weight: 800;
}

@media (max-width: 800px) {
  body { grid-template-columns: 1fr; }
  .controls { position: static; height: auto; border-right: 0; border-bottom: 1px solid var(--ui-border); }
  .preview { padding: 16px; }
}

/* ---------- Print ---------- */

@page { margin: 0.5in; }

@media print {
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  body { display: block; min-height: 0; background: #fff; }
  .controls, body.viewing .viewbar { display: none; }
  .preview, html.group .preview { display: block; padding: 0; }

  .sheet {
    width: 100%;
    aspect-ratio: auto;
    padding: 0;
    box-shadow: none;
    break-inside: avoid;
    break-after: page;
  }
  .sheet:last-child { break-after: auto; }

  /* Group: one person's card per page, with their name above it. */
  .gallery { display: block; }
  .gallery-title, .empty, .badge, .tile-head .link { display: none; }
  .tile {
    padding: 0;
    border-radius: 0;
    box-shadow: none;
    break-inside: avoid;
    break-after: page;
  }
  .tile:last-child { break-after: auto; }
  .tile-name { font-size: 16pt; }
  body.print-one .tile:not(.print-target) { display: none; }
  body.print-one .tile.print-target { break-after: auto; }
}
