:root {
  --blue: #2f6fb5;
  --blue-dark: #1f4f86;
  --blue-tab: #6b9bd8;
  --blue-tab-dark: #2a5a95;
  --blue-soft: #d6e3f1;
  --blue-softer: #eef4fb;
  --ink: #23272e;
  --muted: #5d6673;
  --line: #dde2e8;
  --panel: #f2f2f2;
  --white: #fff;
  --pink: #ff3d9a;
  --pink-soft: #f28b9b;
  --pink-badge: #e2667a;
  --green: #2e9e62;
  --radius: 6px;
  --shadow: 0 2px 6px rgba(20, 40, 70, 0.12);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: "Lexend", system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--ink);
  background: #3b7bc4;
  min-height: 100vh;
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { display: block; }

/* ---------- wavy blue background ---------- */
.waves { position: fixed; inset: 0; z-index: -1; background: linear-gradient(180deg, #3570b4 0%, #3d7cc5 45%, #6a9fd8 75%, #e9f1fa 100%); }
.waves svg { width: 100%; height: 100%; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  background: var(--white);
  padding: 10px max(16px, calc((100vw - 1200px) / 2));
  border-bottom: 1px solid var(--line);
  min-height: 56px;
}
.brand { display: flex; align-items: center; gap: 8px; text-decoration: none; color: var(--ink); font-weight: 600; font-size: 1.3rem; letter-spacing: -0.02em; }
.logo { width: 30px; height: 30px; }
.topbar-right { display: flex; align-items: center; gap: 14px; font-size: 0.95rem; min-width: 0; }
.topbar-right svg { width: 18px; height: 18px; flex: none; }
.user { display: flex; align-items: center; gap: 6px; color: var(--ink); min-width: 0; }
.user span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 14ch; }
.link-btn { background: none; border: 0; color: var(--muted); padding: 4px 2px; text-decoration: underline; text-underline-offset: 3px; }
.link-btn:hover { color: var(--ink); }

/* ---------- main white sheet ---------- */
.sheet {
  max-width: 1200px;
  margin: 16px auto 40px;
  background: var(--white);
  border-radius: 4px;
  box-shadow: var(--shadow);
  padding: 20px 20px 32px;
  min-height: calc(100vh - 120px);
}
h1 { font-size: 1.5rem; font-weight: 500; margin: 0 0 6px; letter-spacing: -0.01em; }
h2 { font-size: 1.1rem; font-weight: 500; margin: 0 0 12px; }
.section-title { margin-top: 32px; }
.lead { color: var(--muted); max-width: 62ch; margin: 0 0 22px; font-weight: 300; }
.muted { color: var(--muted); }

/* ---------- buttons & inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--blue); background: var(--white); color: var(--blue-dark);
  padding: 10px 18px; border-radius: var(--radius); font-weight: 500;
  transition: background 0.15s, transform 0.05s;
}
.btn:hover { background: var(--blue-softer); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--blue); color: var(--white); }
.btn.primary:hover { background: var(--blue-dark); }
.btn.small { padding: 6px 12px; font-size: 0.9rem; }
.btn.small svg { width: 16px; height: 16px; }
.btn.ghost { border-color: var(--line); color: var(--muted); }
.btn:disabled { opacity: 0.55; cursor: default; }

label { display: block; font-size: 0.9rem; color: var(--muted); margin-bottom: 12px; }
input {
  display: block; width: 100%; margin-top: 4px;
  border: 1px solid #c9d1db; border-radius: var(--radius);
  padding: 10px 12px; font-size: 1rem; background: var(--white);
}
input:focus { outline: 2px solid var(--blue-tab); outline-offset: 0; border-color: var(--blue); }
#joinCode { text-transform: uppercase; letter-spacing: 0.15em; font-weight: 600; }
.form-error { color: #c0344d; font-size: 0.9rem; min-height: 1.2em; margin: 8px 0 0; }

/* ---------- home ---------- */
.home-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 18px; }
.panel {
  border: 1px solid var(--line); border-radius: 4px; padding: 18px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08); background: var(--white);
}
.panel .btn { width: 100%; margin-top: 4px; }
.recent { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; }
.recent-card {
  display: block; text-align: left; border: 1px solid var(--line); border-radius: 4px; background: var(--white);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1); padding: 0; overflow: hidden; text-decoration: none; color: var(--ink);
}
.recent-card:hover { box-shadow: 0 3px 10px rgba(0, 0, 0, 0.15); }
.recent-card .thumb {
  height: 70px; background: #9fbfe2; color: #5a7fae; font-family: ui-monospace, monospace; font-size: 11px; line-height: 1.15;
  padding: 6px 8px; overflow: hidden; white-space: pre;
}
.recent-card .meta { padding: 10px 14px 12px; }
.recent-card b { display: block; font-weight: 500; }
.recent-card small { color: var(--muted); }
.steps { padding-left: 1.2em; margin: 0; color: var(--muted); }
.steps li { margin-bottom: 6px; }
.steps b { color: var(--ink); font-weight: 500; }

/* ---------- game layout ---------- */
.game-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.game-head h1 { margin: 0; overflow-wrap: anywhere; }
.invite { display: flex; align-items: center; gap: 10px; flex: none; }
.invite-code { font-size: 0.85rem; color: var(--muted); white-space: nowrap; }
.invite-code b { color: var(--blue-dark); font-weight: 600; letter-spacing: 0.08em; }
.waiting-actions { display: flex; justify-content: center; flex-wrap: wrap; gap: 8px; }
.waiting .hint { margin: 12px 0 0; }
.sample-hint { font-size: 0.88rem; color: var(--muted); margin: 0 0 8px; }
.link-btn.strong { color: var(--blue-dark); font-weight: 500; }
.join-panel { max-width: 440px; }
.host-area { margin-top: 28px; padding-top: 16px; border-top: 1px solid var(--line); max-width: 640px; }
.host-prompt { margin: 0; font-size: 0.9rem; color: var(--muted); }
.create-form { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.create-form input { flex: 1 1 180px; width: auto; margin: 0; padding: 7px 10px; font-size: 0.92rem; }
.create-form .form-error { flex-basis: 100%; margin: 0; }
.game-grid { display: grid; grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr); gap: 20px; align-items: start; }
.side { display: flex; flex-direction: column; gap: 20px; }
.tabs { display: none; }

/* TeachFront-style section: grey panel + blue tab with folded corner */
.group { background: var(--panel); border-radius: 2px; padding: 0 16px 16px 16px; margin-left: 0; position: relative; }
.group-tab {
  position: relative; display: inline-flex; align-items: center; gap: 8px;
  background: var(--blue-tab); color: #11233a; border: 0; border-radius: 2px;
  padding: 8px 16px 8px 14px; font-weight: 500; font-size: 0.95rem;
  margin: 0 0 16px -16px; top: -6px;
}
.group-tab::after {
  content: ""; position: absolute; left: 0; bottom: -10px;
  border-left: 16px solid var(--blue-tab-dark); border-bottom: 10px solid transparent;
}
.group-tab .chev { width: 16px; height: 16px; transition: transform 0.2s; }
.group.collapsed .chev { transform: rotate(-90deg); }
.group.collapsed .group-body { display: none; }
.group.collapsed { padding-bottom: 4px; }

/* ---------- bingo card ---------- */
.card {
  background: var(--white); border-radius: 4px; padding: 14px; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}
.card-title { display: grid; grid-template-columns: repeat(5, 1fr); text-align: center; margin-bottom: 8px; }
.card-title span { font-weight: 700; font-size: 1.5rem; color: var(--blue); letter-spacing: 0.02em; }
.grid { display: grid; grid-template-columns: repeat(5, 1fr); border: 2px solid #9aa6b5; border-radius: 3px; overflow: hidden; }
.cell {
  position: relative; aspect-ratio: 1 / 1; border: 1px solid #c6ced8; background: var(--white);
  display: flex; align-items: center; justify-content: center; text-align: center;
  padding: 4px; font-size: clamp(0.62rem, 1.35vw, 0.9rem); line-height: 1.15; overflow: hidden;
  overflow-wrap: break-word; word-break: normal; user-select: none; -webkit-tap-highlight-color: transparent;
  transition: background 0.15s;
}
button.cell:hover { background: var(--blue-softer); }
.cell .txt { position: relative; z-index: 1; max-width: 100%; overflow-wrap: anywhere; }
.cell.free { background: var(--blue-softer); color: var(--blue-dark); font-weight: 600; letter-spacing: 0.05em; cursor: default; }
.cell.empty { background: #f6f6f6; color: #aaa; cursor: default; }
.cell .x { position: absolute; inset: 10%; z-index: 0; pointer-events: none; opacity: 0; transform: scale(0.6); transition: opacity 0.15s, transform 0.15s; }
.cell.marked .x { opacity: 0.55; transform: scale(1); }
.cell.marked .txt { font-weight: 600; }

.cell.win { background: #fff0f7; }
.cell.free .x { opacity: 0.35; transform: scale(1); }
.card-actions { display: flex; align-items: center; flex-wrap: wrap; gap: 10px; margin-top: 14px; }
.hint { font-size: 0.82rem; color: var(--muted); }
.card-status { margin: 10px 0 0; font-size: 0.9rem; color: var(--muted); text-align: center; }
.card-status b { color: var(--pink); }

.waiting { background: var(--white); border-radius: 4px; padding: 22px; text-align: center; }
.waiting h3 { margin: 0 0 6px; font-weight: 500; font-size: 1.05rem; }
.progress { height: 10px; background: var(--blue-soft); border-radius: 999px; overflow: hidden; margin: 14px auto 8px; max-width: 360px; }
.progress > div { height: 100%; background: var(--blue); border-radius: 999px; transition: width 0.3s; }

/* ---------- phrases ---------- */
.add-form { display: flex; gap: 8px; }
.add-form input { margin: 0; }
.add-form .btn { flex: none; }
.phrase-list, .player-list { list-style: none; margin: 4px 0 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.phrase-list { max-height: 460px; overflow-y: auto; }
.row {
  display: flex; align-items: center; gap: 10px; background: var(--white);
  border-left: 3px solid var(--blue-tab); border-radius: 2px; padding: 9px 12px; min-height: 44px;
}
.row .main { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.row .by { color: var(--muted); font-size: 0.8rem; white-space: nowrap; }
.row .del {
  flex: none; border: 0; background: none; color: #9aa3ad; width: 30px; height: 30px; border-radius: 50%;
  display: grid; place-items: center;
}
.row .del:hover { background: #fde8ec; color: #c0344d; }
.row .del svg { width: 14px; height: 14px; }
.row.on-card { border-left-color: var(--pink-soft); }
.empty-note { color: var(--muted); font-size: 0.92rem; padding: 6px 2px; }

.badge { flex: none; font-size: 0.75rem; padding: 2px 9px; border-radius: 4px; font-weight: 500; white-space: nowrap; }
.badge.bingo { background: var(--pink-badge); color: var(--white); }
.badge.you { background: var(--blue-soft); color: var(--blue-dark); }
.badge.host { background: #e7e7e7; color: #555; }
.player-list .score { color: var(--muted); font-size: 0.85rem; white-space: nowrap; display: flex; align-items: center; gap: 4px; }
.player-list .score svg { width: 14px; height: 14px; }

/* ---------- suggestions ---------- */
.sheet-foot { margin-top: 36px; padding-top: 14px; border-top: 1px solid var(--line); text-align: center; }
#loadingView:not([hidden]) ~ .sheet-foot { display: none; }
.sheet-foot .link-btn { display: inline-flex; align-items: center; gap: 6px; font-size: 0.9rem; color: var(--blue-dark); }
.sheet-foot svg { width: 18px; height: 18px; }
.modal { border: 0; border-radius: 8px; padding: 22px; width: min(480px, calc(100vw - 32px)); box-shadow: 0 10px 30px rgba(0, 0, 0, 0.25); color: var(--ink); }
.modal::backdrop { background: rgba(15, 30, 50, 0.55); }
.modal h2 { margin-bottom: 4px; }
.modal .small { font-size: 0.88rem; margin: 0 0 14px; }
.modal .optional { color: #98a1ac; }
textarea {
  display: block; width: 100%; margin-top: 4px; resize: vertical; min-height: 110px;
  border: 1px solid #c9d1db; border-radius: var(--radius); padding: 10px 12px; font: inherit; font-size: 1rem; color: inherit;
}
textarea:focus { outline: 2px solid var(--blue-tab); outline-offset: 0; border-color: var(--blue); }
.hp { position: absolute; left: -9999px; width: 1px; height: 1px; overflow: hidden; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }

/* ---------- toast & celebration ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translate(-50%, 30px);
  background: #22303f; color: var(--white); padding: 10px 18px; border-radius: 6px; font-size: 0.92rem;
  opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s; z-index: 50; max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.celebrate { position: fixed; inset: 0; background: rgba(15, 30, 50, 0.55); display: grid; place-items: center; z-index: 60; padding: 16px; }
.celebrate-box { background: var(--white); border-radius: 8px; padding: 28px 32px; text-align: center; max-width: 360px; width: 100%; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.25); animation: pop 0.35s ease-out; }
.celebrate-word { font-size: 3rem; font-weight: 700; color: var(--pink); letter-spacing: 0.04em; transform: rotate(-4deg); }
.celebrate-box p { color: var(--muted); }
@keyframes pop { from { transform: scale(0.7); opacity: 0; } to { transform: scale(1); opacity: 1; } }

/* ---------- mobile ---------- */
@media (max-width: 820px) {
  .topbar { padding: 8px 16px; }
  .brand span { font-size: 1.1rem; }
  .user { display: none; }
  .sheet { margin: 0; border-radius: 0; padding: 16px 16px calc(28px + env(safe-area-inset-bottom)); min-height: calc(100vh - 56px); box-shadow: none; }
  .game-grid { grid-template-columns: 1fr; gap: 0; }
  .side { gap: 0; }

  /* tabs replace collapsible headers on phones */
  .tabs {
    display: flex; position: sticky; top: 56px; z-index: 10; background: var(--white);
    margin: 0 -16px 14px; padding: 0 8px; border-bottom: 1px solid var(--line);
  }
  .tabs button {
    flex: 1; background: none; border: 0; padding: 12px 4px 10px; font-weight: 500; color: var(--muted);
    border-bottom: 3px solid transparent; display: flex; align-items: center; justify-content: center; gap: 6px; font-size: 0.95rem;
  }
  .tabs button.active { color: var(--blue-dark); border-bottom-color: var(--blue); }
  .tabs .count { background: var(--blue-soft); color: var(--blue-dark); border-radius: 999px; font-size: 0.72rem; padding: 1px 7px; }
  .group { display: none; background: none; padding: 0; }
  .group.active { display: block; }
  .group.collapsed .group-body { display: block; }
  .group-tab { display: none; }
  .invite { display: flex; align-items: center; gap: 10px; flex: none; }
.invite-code { font-size: 0.85rem; color: var(--muted); white-space: nowrap; }
.invite-code b { color: var(--blue-dark); font-weight: 600; letter-spacing: 0.08em; }
.waiting-actions { display: flex; justify-content: center; flex-wrap: wrap; gap: 8px; }
.waiting .hint { margin: 12px 0 0; }
.sample-hint { font-size: 0.88rem; color: var(--muted); margin: 0 0 8px; }
.link-btn.strong { color: var(--blue-dark); font-weight: 500; }
.join-panel { max-width: 440px; }
.host-area { margin-top: 28px; padding-top: 16px; border-top: 1px solid var(--line); max-width: 640px; }
.host-prompt { margin: 0; font-size: 0.9rem; color: var(--muted); }
.create-form { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.create-form input { flex: 1 1 180px; width: auto; margin: 0; padding: 7px 10px; font-size: 0.92rem; }
.create-form .form-error { flex-basis: 100%; margin: 0; }
  .card { padding: 8px; box-shadow: none; }
  .card-title span { font-size: 1.25rem; }
  .cell { font-size: clamp(0.58rem, 2.7vw, 0.85rem); padding: 3px; }
  .phrase-list { max-height: none; }
  .add-form { position: sticky; top: 104px; z-index: 5; background: var(--white); padding: 4px 0 2px; }
}
@media (max-width: 380px) {
    .cell { font-size: 0.56rem; padding: 2px; }
}
