:root, [data-theme="dark"] {
  --bg: #000000;
  --surface: #111111;
  --surface-2: #1a1a1a;
  --surface-3: #242424;
  --border: #2a2a2a;
  --border-strong: #3d3d3d;
  --text: #f2f2f2;
  --muted: #8f8f8f;
  --accent: #ededed;
  --accent-text: #000000;
  --over: #2e2e2e;
  --danger: #ff6b6b;
  --shadow: 0 12px 32px rgba(0, 0, 0, .7);
  color-scheme: dark;
}
[data-theme="light"] {
  --bg: #f2f2f2;
  --surface: #ffffff;
  --surface-2: #f6f6f6;
  --surface-3: #ebebeb;
  --border: #dddddd;
  --border-strong: #c4c4c4;
  --text: #111111;
  --muted: #6b6b6b;
  --accent: #111111;
  --accent-text: #ffffff;
  --over: #e4e4e4;
  --danger: #c62828;
  --shadow: 0 12px 32px rgba(0, 0, 0, .18);
  color-scheme: light;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
h1, h2 { margin: 0; font-weight: 650; letter-spacing: -.01em; }
h1 { font-size: 22px; }
h2 { font-size: 16px; }
.muted { color: var(--muted); }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.hidden { display: none !important; }

button, input, select, textarea { font: inherit; color: inherit; }
button {
  background: var(--surface-2); border: 1px solid var(--border-strong); border-radius: 8px;
  padding: 7px 12px; cursor: pointer; min-height: 36px;
}
button:hover { background: var(--surface-3); }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
button.primary:hover { opacity: .88; }
button.danger { color: var(--danger); }
.icon-btn { width: 36px; padding: 0; }
input, select, textarea {
  background: var(--surface-2); border: 1px solid var(--border-strong); border-radius: 8px;
  padding: 7px 10px; min-height: 36px; width: 100%;
}
select { width: auto; }
select option { background: var(--surface-2); color: var(--text); }
select option:checked { background: var(--surface-3); color: var(--text); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
input::placeholder, textarea::placeholder { color: var(--muted); }

.app { max-width: 2400px; margin: 0 auto; padding: 16px 16px 48px; }
.top { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.brand p { margin: 2px 0 0; font-size: 14px; }
.top-actions { display: flex; gap: 8px; align-items: center; }

.board { display: grid; gap: 16px; grid-template-columns: 1fr; align-items: start; }
.people { display: grid; gap: 16px; grid-template-columns: 1fr; align-items: start; }
@media (min-width: 900px) { .people { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (min-width: 1300px) {
  .board { grid-template-columns: 476px minmax(0, 1fr); }
  .pool { position: sticky; top: 12px; max-height: calc(100vh - 24px); display: flex; flex-direction: column; }
  .pool .cards { overflow-y: auto; flex: 1; min-height: 80px; }
}
/* Four people side by side only when each column can be about 440px wide. */
@media (min-width: 2340px) { .people { grid-template-columns: repeat(4, minmax(0, 1fr)); } }

.zone {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 14px;
  transition: background .12s, border-color .12s;
}
.zone.over { background: var(--over); border-color: var(--accent); }
.zone-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; margin-bottom: 10px; }
.zone-total { text-align: right; }
.zone-total strong { font-size: 20px; font-variant-numeric: tabular-nums; }
.zone-total small { display: block; color: var(--muted); font-size: 12px; }
.zone-foot { margin-top: 8px; display: flex; gap: 8px; }
.zone-foot button.export { flex: none; width: auto; border-style: solid; }
.zone-foot button { width: 100%; background: transparent; border-style: dashed; color: var(--muted); }
.zone-foot button:hover { color: var(--text); }

.breakdown { display: flex; flex-wrap: wrap; gap: 6px; margin: 0 0 10px; padding: 0; list-style: none; }
.breakdown li {
  font-size: 12px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px;
  padding: 3px 9px; font-variant-numeric: tabular-nums;
}
.breakdown li b { font-weight: 600; }
.breakdown li.warn { border-color: var(--border-strong); color: var(--text); border-style: dashed; }

.add-form { display: grid; grid-template-columns: 1fr 86px auto; gap: 6px; }
.hint { margin: 0; font-size: 12px; color: var(--muted); }
.pool-tools { display: flex; gap: 10px; align-items: center; justify-content: space-between; margin: 8px 0 10px; }
.pool-tools .hint { flex: 1; }
.select-bar {
  display: flex; gap: 8px; align-items: center; margin: 0 0 10px; padding: 8px;
  background: var(--surface-3); border: 1px solid var(--border-strong); border-radius: 10px;
}
.select-bar[hidden] { display: none; }
.select-bar .muted { flex: 1; font-size: 13px; }
button:disabled { opacity: .4; cursor: default; }
.pick { grid-row: span 2; width: 20px; height: 20px; min-height: 0; margin: 2px 4px 0 6px; accent-color: var(--accent); cursor: pointer; }
.selecting .card { grid-template-columns: auto 1fr auto; cursor: pointer; }
.card.is-picked { border-color: var(--accent); background: var(--surface-3); }
button.small { min-height: 30px; padding: 3px 10px; font-size: 12px; white-space: nowrap; flex: none; }

.cards { display: flex; flex-direction: column; gap: 8px; min-height: 56px; }
.cards:empty::before {
  content: attr(data-empty); display: grid; place-items: center; min-height: 56px; padding: 8px;
  color: var(--muted); font-size: 13px; border: 1px dashed var(--border-strong); border-radius: 10px; text-align: center;
}

.card {
  display: grid; grid-template-columns: auto 1fr auto; gap: 2px 8px; align-items: start;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 9px 10px 9px 4px;
  cursor: grab; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
.card:hover { border-color: var(--border-strong); }
.card.is-bought .card-name { color: var(--muted); }
.grip {
  grid-row: span 2; color: var(--muted); font-size: 18px; line-height: 1; padding: 2px 4px;
  touch-action: none; cursor: grab; align-self: stretch; display: flex; align-items: center;
}
.card-name { font-weight: 550; overflow-wrap: anywhere; padding-top: 1px; }
.card-cost { font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }
.card-cost.none { color: var(--muted); font-weight: 400; font-size: 13px; }
.card-row { grid-column: 2 / 4; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-top: 4px; }
.card-notes { grid-column: 2 / 4; font-size: 12px; color: var(--muted); overflow-wrap: anywhere; }
.tick, .occ, .edit {
  min-height: 28px; padding: 2px 9px; font-size: 12px; border-radius: 999px; background: transparent;
  border: 1px solid var(--border-strong); color: var(--muted);
}
.tick[aria-pressed="true"] { background: var(--text); color: var(--bg); border-color: var(--text); }
.edit { margin-left: auto; }
.occ { width: auto; cursor: pointer; color: var(--text); background: var(--surface-3); }
.occ.unset { color: var(--muted); background: transparent; border-style: dashed; }

.placeholder { border: 2px dashed var(--accent); border-radius: 10px; opacity: .5; }
.ghost {
  position: fixed; left: 0; top: 0; z-index: 50; pointer-events: none; margin: 0;
  box-shadow: var(--shadow); transform-origin: 0 0; opacity: .95; cursor: grabbing;
}
body.dragging, body.dragging * { cursor: grabbing !important; }

.footer { margin-top: 20px; color: var(--muted); font-size: 13px; text-align: center; }

.gate {
  position: fixed; inset: 0; z-index: 40; display: grid; place-items: center; background: var(--bg);
  color: var(--muted); padding: 16px; text-align: center;
}
.gate[hidden] { display: none; }
.login { width: min(360px, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 24px; text-align: left; color: var(--text); }
.login p { color: var(--muted); margin: 6px 0 16px; }
.login button { width: 100%; margin-top: 12px; }

dialog {
  background: var(--surface); color: var(--text); border: 1px solid var(--border-strong); border-radius: 14px;
  padding: 20px; width: min(440px, calc(100vw - 32px)); box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(0, 0, 0, .65); }
dialog form { display: grid; gap: 12px; }
dialog label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
dialog label > input, dialog label > select, dialog label > textarea { color: var(--text); font-size: 15px; }
dialog select { width: 100%; }
.checks { display: flex; gap: 20px; }
.checks label { display: flex; gap: 8px; align-items: center; color: var(--text); font-size: 15px; }
.checks input { width: 18px; height: 18px; min-height: 0; accent-color: var(--accent); }
.dialog-foot { display: flex; gap: 8px; align-items: center; }
.spacer { flex: 1; }
.error { color: var(--danger); margin: 0; min-height: 1em; font-size: 13px; }

.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 60;
  background: var(--surface-3); border: 1px solid var(--border-strong); border-radius: 10px;
  padding: 10px 14px; box-shadow: var(--shadow); display: flex; gap: 12px; align-items: center;
  max-width: calc(100vw - 32px);
}
.toast[hidden] { display: none; }
.toast button { min-height: 30px; padding: 3px 10px; }

@media (pointer: coarse) {
  .tick, .occ, .edit { min-height: 34px; padding: 4px 12px; }
  .grip { font-size: 22px; padding: 4px 8px; }
}
@media (max-width: 480px) {
  .top { flex-direction: column; align-items: stretch; }
  .top-actions { justify-content: space-between; }
}

/* ---------- iPad / tablet: fit everything on one screen ----------
   The page itself doesn't scroll; the Gift list and each person's column scroll inside. */
@media (min-width: 700px) and (max-width: 1400px) and (min-height: 600px) {
  html, body { height: 100%; overflow: hidden; }
  .app {
    max-width: none; height: 100vh; height: 100dvh; padding: 10px 12px 6px;
    padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right));
    display: flex; flex-direction: column;
  }
  .top { margin-bottom: 8px; flex: none; }
  h1 { font-size: 20px; }
  .footer { margin-top: 4px; font-size: 11px; flex: none; }

  .board { flex: 1; min-height: 0; gap: 10px; grid-template-columns: 290px minmax(0, 1fr); align-items: stretch; }
  .people {
    min-height: 0; gap: 10px; align-items: stretch;
    grid-template-columns: none; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr);
  }
  .zone { display: flex; flex-direction: column; min-height: 0; padding: 10px; border-radius: 12px; }
  .pool { position: static; max-height: none; }
  .zone-head, .add-form, .pool-tools, .select-bar, .breakdown, .zone-foot { flex: none; }
  .zone-head { margin-bottom: 8px; }
  .zone-total strong { font-size: 17px; }
  .cards, .pool .cards { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; gap: 6px; }
  .add-form { grid-template-columns: 1fr 72px auto; }
  .pool-tools { margin: 6px 0 8px; gap: 6px; }
  .pool-tools .hint { font-size: 11px; }
  .breakdown { margin-bottom: 8px; gap: 4px; }
  .breakdown li { font-size: 11px; padding: 2px 7px; }
  .zone-foot { margin-top: 6px; }
  .zone-foot button { min-height: 32px; padding: 4px 8px; font-size: 13px; }

  .card { padding: 7px 8px 7px 2px; font-size: 14px; }
  .card-row { gap: 4px; margin-top: 3px; }
  .card-notes { font-size: 11px; }
  .tick, .occ, .edit { min-height: 30px; padding: 2px 8px; font-size: 12px; }
  .occ { max-width: 100%; }
}

/* Tablet held upright: Gift list across the top, people side by side underneath. */
@media (min-width: 700px) and (max-width: 1400px) and (min-height: 600px) and (orientation: portrait) {
  .board { grid-template-columns: minmax(0, 1fr); grid-template-rows: 36% minmax(0, 1fr); }
  .pool .cards { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); align-content: start; }
  .pool .cards:empty { display: flex; }
}

/* Tablet tidy-ups: keep names on one line (the total drops below if needed); slimmer Gift list on smaller iPads. */
@media (min-width: 700px) and (max-width: 1400px) and (min-height: 600px) {
  .zone-head { flex-wrap: wrap; row-gap: 2px; }
  .zone-head h2 { font-size: 15px; white-space: nowrap; }
  .zone-total { margin-left: auto; }
}
@media (min-width: 700px) and (max-width: 1100px) and (min-height: 600px) and (orientation: landscape) {
  .board { grid-template-columns: 250px minmax(0, 1fr); }
  .add-form { grid-template-columns: 1fr 64px auto; }
}
