:root {
  --bg: #fff5f3;
  --bg-2: #ffe9ec;
  --surface: #ffffff;
  --ink: #3b2230;
  --muted: #8b6c79;
  --line: #f1d8de;
  --primary: #e8566c;
  --primary-ink: #ffffff;
  --truth: #5a72f0;
  --dare: #f2683c;
  --surprise: #9a5ce0;
  --done: #23a776;
  --chicken: #d9862a;
  --online: #2fc785;
  --offline: #c9b6bd;
  --shadow: 0 10px 30px -12px rgba(160, 50, 80, .25);
  --radius: 18px;
  --font-head: "Fredoka", ui-rounded, "SF Pro Rounded", system-ui, sans-serif;
  --font-body: "Nunito", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1218;
    --bg-2: #2a1922;
    --surface: #2a1c24;
    --ink: #fbe9ee;
    --muted: #c29fae;
    --line: #432b37;
    --primary: #ff6f86;
    --primary-ink: #2a0c14;
    --truth: #7d92ff;
    --dare: #ff8a5c;
    --surprise: #b585f2;
    --done: #3cc98f;
    --chicken: #f0a24a;
    --offline: #6d5560;
    --shadow: 0 10px 30px -12px rgba(0, 0, 0, .6);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.5;
  color: var(--ink);
  background: radial-gradient(120% 60% at 50% 0%, var(--bg-2), var(--bg) 70%) fixed, var(--bg);
  padding: env(safe-area-inset-top, 0) env(safe-area-inset-right, 0) env(safe-area-inset-bottom, 0) env(safe-area-inset-left, 0);
}

[hidden] { display: none !important; }

h1, h2, .brand, .big-code, .btn, summary, .card-text, .round b, .pscore {
  font-family: var(--font-head);
}

/* ---------- layout ---------- */
.topbar {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  max-width: 720px;
  margin: 0 auto;
  padding: 16px 16px 4px;
}
.brand { font-size: 1.5rem; font-weight: 700; color: var(--primary); letter-spacing: .2px; }
.brand-heart { display: inline-block; animation: beat 2.4s ease-in-out infinite; }
.conn { color: var(--muted); font-size: .8rem; font-weight: 700; display: flex; align-items: center; gap: 6px; }
.conn::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--chicken); }
.conn.live::before { background: var(--online); }

.notice {
  display: flex; align-items: center; gap: 10px;
  background: color-mix(in srgb, var(--chicken) 14%, var(--surface));
  border: 2px solid color-mix(in srgb, var(--chicken) 45%, transparent);
  border-radius: 14px; padding: 12px 14px; margin: 0 0 16px; font-weight: 700;
  animation: fade .25s ease;
}
.notice .big { font-size: 1.4rem; }

.wrap { max-width: 720px; margin: 0 auto; padding: 8px 16px 40px; }
.screen { animation: fade .25s ease; }
.center { text-align: center; }
#screen-loading { padding: 80px 0; }

.hero { text-align: center; margin: 24px 0 20px; }
.hero h1 { font-size: clamp(1.8rem, 6vw, 2.5rem); line-height: 1.15; margin: 0 0 8px; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  margin: 0 0 16px;
}

.row { display: flex; gap: 10px; align-items: stretch; }
.row > input { flex: 1; min-width: 0; }
.row.between { justify-content: space-between; align-items: center; }

/* ---------- forms ---------- */
label { display: block; font-weight: 700; margin: 0 0 6px; font-size: .95rem; }
input, textarea {
  width: 100%;
  font: inherit;
  color: var(--ink);
  background: var(--bg);
  border: 2px solid var(--line);
  border-radius: 12px;
  padding: 12px 14px;
  outline: none;
  transition: border-color .15s;
}
input:focus, textarea:focus { border-color: var(--primary); }
textarea { resize: vertical; min-height: 64px; }
#form-name > input { margin-bottom: 14px; }
.code-input { text-transform: uppercase; letter-spacing: 4px; font-weight: 700; }

.divider { display: flex; align-items: center; gap: 12px; color: var(--muted); margin: 20px 0 14px; font-size: .9rem; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--line); }

.error { color: #d63a55; font-weight: 600; margin: 12px 0 0; }

/* ---------- buttons ---------- */
.btn {
  appearance: none;
  border: 0;
  border-radius: 14px;
  padding: 12px 18px;
  font-size: 1.05rem;
  font-weight: 600;
  cursor: pointer;
  color: #fff;
  background: var(--muted);
  transition: transform .08s ease, filter .15s, opacity .15s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.btn:hover { filter: brightness(1.06); }
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .5; cursor: default; transform: none; }
.btn:focus-visible { outline: 3px solid color-mix(in srgb, var(--primary) 50%, transparent); outline-offset: 2px; }
.btn-block { display: block; width: 100%; }
.btn-primary { background: var(--primary); color: var(--primary-ink); }
.btn-secondary { background: var(--ink); color: var(--surface); }
.btn-ghost { background: transparent; color: var(--muted); border: 2px solid var(--line); }
.btn-link { background: none; color: var(--muted); text-decoration: underline; font-size: .95rem; margin-top: 8px; }
.btn-truth { background: var(--truth); }
.btn-dare { background: var(--dare); }
.btn-surprise { background: var(--surprise); }
.btn-done { background: var(--done); }
.btn-chicken { background: var(--chicken); }
.btn-ghost.confirm { border-color: #d63a55; color: #d63a55; }

/* ---------- lobby ---------- */
.big-code {
  font-size: clamp(2.6rem, 14vw, 4rem);
  font-weight: 700;
  letter-spacing: 8px;
  color: var(--primary);
  margin: 4px 0 16px;
  user-select: all;
}
.waiting { display: flex; justify-content: center; align-items: center; gap: 10px; margin: 24px 0 4px; color: var(--muted); font-weight: 600; }
.pulse {
  display: inline-block; width: 14px; height: 14px; border-radius: 50%;
  background: var(--primary); animation: pulse 1.4s ease-out infinite;
}

/* ---------- scoreboard ---------- */
.scoreboard {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 10px 12px;
  margin-bottom: 16px;
  position: sticky;
  top: max(8px, env(safe-area-inset-top, 0px));
  z-index: 5;
}
.player {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px; border-radius: 12px;
  border: 2px solid transparent;
  min-width: 0;
  transition: border-color .2s, background .2s;
}
#p1 { flex-direction: row-reverse; text-align: right; }
.player.active { border-color: var(--primary); background: color-mix(in srgb, var(--primary) 10%, transparent); }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--offline); flex: none; }
.dot.on { background: var(--online); box-shadow: 0 0 0 3px color-mix(in srgb, var(--online) 25%, transparent); }
.pname { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; min-width: 0; }
.pscore { font-size: 1.5rem; font-weight: 700; color: var(--primary); }
.pscore.bump { animation: bump .5s ease; }
.round { text-align: center; color: var(--muted); font-size: .8rem; line-height: 1.1; }
.round b { display: block; font-size: 1.3rem; color: var(--ink); }

/* ---------- stage ---------- */
.stage { min-height: 280px; margin-bottom: 20px; }
.status { text-align: center; font-weight: 700; font-size: 1.1rem; margin: 6px 0 16px; }

.choose { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.choose .btn {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 22px 8px; font-size: 1.15rem; border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.choose .big { font-size: 2rem; line-height: 1; }

.card {
  position: relative;
  background: var(--surface);
  border-radius: 22px;
  border: 3px solid var(--card-accent, var(--primary));
  box-shadow: var(--shadow);
  padding: 22px 22px 26px;
  min-height: 200px;
  display: flex; flex-direction: column;
}
.card.truth { --card-accent: var(--truth); }
.card.dare { --card-accent: var(--dare); }
.card.flip { animation: flip .5s cubic-bezier(.2, .8, .3, 1.1); }
.card-top { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.badge {
  background: var(--card-accent); color: #fff; font-family: var(--font-head);
  font-weight: 600; padding: 4px 12px; border-radius: 999px; font-size: .9rem; text-transform: uppercase; letter-spacing: 1px;
}
.tag { color: var(--muted); font-size: .85rem; font-weight: 700; text-transform: capitalize; }
.card-text {
  flex: 1; display: flex; align-items: center; justify-content: center; text-align: center;
  font-size: clamp(1.25rem, 4.5vw, 1.6rem); font-weight: 500; line-height: 1.35;
  margin: 18px 4px 6px; overflow-wrap: anywhere;
}
.card-author { text-align: center; color: var(--muted); font-size: .9rem; margin: 6px 0 0; font-style: italic; }

.judge { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 16px; }
.judge .btn { padding: 16px 8px; }

/* ---------- folds ---------- */
.fold { padding: 0; }
.fold > summary {
  list-style: none; cursor: pointer; padding: 16px 20px; font-size: 1.1rem; font-weight: 600;
  display: flex; align-items: center; gap: 8px;
}
.fold > summary::-webkit-details-marker { display: none; }
.fold > summary::after { content: "▾"; margin-left: auto; color: var(--muted); transition: transform .2s; }
.fold:not([open]) > summary::after { transform: rotate(-90deg); }
.fold > :not(summary) { margin-left: 20px; margin-right: 20px; }
.fold > :last-child { margin-bottom: 18px; }
.count { color: var(--muted); font-size: .9rem; font-weight: 500; }

.history { list-style: none; padding: 0; margin-top: 0; }
.history li {
  display: grid; grid-template-columns: auto 1fr; gap: 2px 10px;
  padding: 10px 0; border-top: 1px solid var(--line);
}
.history li:first-child { border-top: 0; padding-top: 0; }
.h-icon { font-size: 1.2rem; grid-row: span 2; }
.h-meta { font-size: .85rem; color: var(--muted); font-weight: 700; }
.h-text { font-size: .95rem; overflow-wrap: anywhere; }

.cats { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { position: relative; margin: 0; }
.chip input { position: absolute; opacity: 0; pointer-events: none; }
.chip span {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 14px; border-radius: 999px; border: 2px solid var(--line);
  font-weight: 700; cursor: pointer; user-select: none; transition: all .15s;
}
.chip input:checked + span { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.chip input:focus-visible + span { outline: 3px solid color-mix(in srgb, var(--primary) 50%, transparent); }

.custom-form { display: grid; gap: 10px; }
.seg { display: inline-flex; border: 2px solid var(--line); border-radius: 12px; overflow: hidden; justify-self: start; }
.seg label { margin: 0; font-weight: 700; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: block; padding: 6px 16px; cursor: pointer; }
.seg input:checked + span { background: var(--ink); color: var(--surface); }
.seg input:focus-visible + span { outline: 3px solid var(--primary); outline-offset: -3px; }

.custom-list { list-style: none; padding: 0; margin-top: 14px; }
.custom-list li {
  display: flex; gap: 10px; align-items: flex-start; padding: 10px 0; border-top: 1px solid var(--line);
}
.custom-list .c-body { flex: 1; min-width: 0; }
.custom-list .c-text { overflow-wrap: anywhere; }
.custom-list .c-meta { font-size: .8rem; color: var(--muted); font-weight: 700; }
.icon-btn {
  appearance: none; border: 0; background: none; color: var(--muted); cursor: pointer;
  font-size: 1.1rem; padding: 4px 8px; border-radius: 8px;
}
.icon-btn:hover { background: var(--bg-2); color: #d63a55; }

.footer-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin-top: 8px; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  background: var(--ink); color: var(--surface);
  padding: 12px 18px; border-radius: 12px; font-weight: 600;
  box-shadow: var(--shadow); z-index: 50; max-width: calc(100% - 32px); text-align: center;
  animation: rise .25s ease;
}

/* ---------- animations ---------- */
@keyframes fade { from { opacity: 0; transform: translateY(6px); } }
@keyframes rise { from { opacity: 0; transform: translate(-50%, 10px); } }
@keyframes beat { 0%, 60%, 100% { transform: scale(1); } 30% { transform: scale(1.18); } }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--primary) 60%, transparent); } 100% { box-shadow: 0 0 0 16px transparent; } }
@keyframes flip { from { transform: perspective(800px) rotateY(80deg) scale(.9); opacity: 0; } }
@keyframes bump { 40% { transform: scale(1.4); } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* ---------- phones ---------- */
@media (max-width: 480px) {
  .panel { padding: 16px; }
  .choose { gap: 8px; }
  .choose .btn { padding: 18px 4px; font-size: 1rem; }
  .player { padding: 6px; gap: 6px; }
  .pname { font-size: .9rem; }
  .pscore { font-size: 1.3rem; }
  .card { padding: 18px 16px 22px; }
}
