:root {
  --bg: #f4efe4;
  --surface: #fffaf1;
  --surface-2: #ece5d6;
  --text: #221d18;
  --muted: #6f665b;
  --line: #ddd3c1;
  --sun: #e8743b;
  --sun-ink: #b4521f;
  --sun-soft: #fbe1d1;
  --sea: #1c9a94;
  --sea-ink: #127570;
  --sea-soft: #d2efec;
  --neutral: #dccfb4;
  --neutral-ink: #4b4334;
  --trap: #2a2630;
  --trap-ink: #f4efe4;
  --card: #fffdf8;
  --card-ink: #221d18;
  --shadow: 0 1px 0 rgba(60, 45, 20, 0.08), 0 6px 18px -10px rgba(60, 45, 20, 0.35);
  --radius: 14px;
  --font: ui-rounded, 'SF Pro Rounded', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #16141b;
    --surface: #201d26;
    --surface-2: #2a2631;
    --text: #f1ece3;
    --muted: #a59d92;
    --line: #37323f;
    --sun: #ef8148;
    --sun-ink: #ffb088;
    --sun-soft: #3d2619;
    --sea: #24aea7;
    --sea-ink: #76ddd6;
    --sea-soft: #15302f;
    --neutral: #8d826d;
    --neutral-ink: #f6efe1;
    --trap: #050407;
    --trap-ink: #f1ece3;
    --card: #2d2934;
    --card-ink: #f1ece3;
    --shadow: 0 1px 0 rgba(0, 0, 0, 0.3), 0 8px 20px -12px rgba(0, 0, 0, 0.8);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  min-height: 100dvh;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { margin: 0; line-height: 1.15; }

.app {
  max-width: 980px;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) 16px calc(32px + env(safe-area-inset-bottom));
}

/* ---------- buttons & inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 0 18px;
  border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); font-weight: 650;
  transition: transform 0.08s ease, background 0.15s ease;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn.primary { background: var(--text); color: var(--bg); border-color: transparent; }
.btn.sun { background: var(--sun); color: #fff; border-color: transparent; }
.btn.sea { background: var(--sea); color: #fff; border-color: transparent; }
.btn.ghost { background: transparent; }
.btn.small { min-height: 36px; padding: 0 12px; font-size: 14px; border-radius: 10px; }
.btn.full { width: 100%; }
.linkish { background: none; border: 0; padding: 4px; color: var(--muted); text-decoration: underline; text-underline-offset: 3px; font-size: 14px; }

input, textarea, select {
  width: 100%; min-height: 44px; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 12px; background: var(--card);
}
input:focus-visible, textarea:focus-visible, select:focus-visible, button:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--sea) 55%, transparent); outline-offset: 2px;
}
label { display: grid; gap: 6px; font-size: 14px; font-weight: 600; color: var(--muted); }

/* ---------- home ---------- */
.home { max-width: 420px; margin: 6vh auto 0; display: grid; gap: 22px; }
.brand { text-align: center; display: grid; gap: 10px; justify-items: center; }
.brand img { width: 64px; height: 64px; }
.brand h1 { font-size: 40px; letter-spacing: -0.02em; }
.tag { margin: 0; color: var(--muted); }
.panel {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 18px; display: grid; gap: 14px; box-shadow: var(--shadow);
}
.or { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
.or::before, .or::after { content: ''; flex: 1; height: 1px; background: var(--line); }
.row { display: flex; gap: 10px; }
.code-input { text-transform: uppercase; letter-spacing: 0.3em; font-weight: 700; text-align: center; }
.rules { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 4px 18px; }
.rules summary { padding: 12px 0; font-weight: 650; cursor: pointer; }
.rules ol { padding-left: 20px; margin: 0 0 14px; color: var(--muted); display: grid; gap: 6px; }
.conn { text-align: center; color: var(--muted); font-size: 13px; }

/* ---------- top bar ---------- */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; }
.topbar .title { display: flex; align-items: center; gap: 10px; font-weight: 750; }
.topbar .title img { width: 28px; height: 28px; }
.codechip {
  display: inline-flex; align-items: center; gap: 8px; padding: 6px 6px 6px 12px;
  border-radius: 999px; background: var(--surface); border: 1px solid var(--line); font-size: 14px;
}
.codechip b { letter-spacing: 0.18em; font-size: 16px; }
.offline { color: var(--sun-ink); font-weight: 650; font-size: 13px; }

/* ---------- lobby ---------- */
.lobby-head { text-align: center; display: grid; gap: 8px; margin: 8px 0 20px; }
.bigcode { font-size: clamp(44px, 14vw, 72px); font-weight: 800; letter-spacing: 0.16em; line-height: 1; }
.teams { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 560px) { .teams { grid-template-columns: 1fr; } }
.team {
  border-radius: var(--radius); padding: 14px; display: grid; gap: 8px; align-content: start;
  border: 2px solid transparent;
}
.team-sun { background: var(--sun-soft); border-color: color-mix(in srgb, var(--sun) 45%, transparent); }
.team-sea { background: var(--sea-soft); border-color: color-mix(in srgb, var(--sea) 45%, transparent); }
.team h3 { display: flex; align-items: center; gap: 8px; font-size: 18px; }
.team-sun h3 { color: var(--sun-ink); }
.team-sea h3 { color: var(--sea-ink); }
.dot { width: 12px; height: 12px; border-radius: 50%; display: inline-block; flex: none; }
.dot.sun { background: var(--sun); }
.dot.sea { background: var(--sea); }
.slot-label { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); margin-top: 4px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 5px 10px; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--line); font-size: 14px; font-weight: 600;
}
.chip.me { outline: 2px solid var(--text); outline-offset: -1px; }
.chip.away { opacity: 0.5; }
.chip .crown { font-size: 12px; }
.empty { color: var(--muted); font-size: 14px; font-style: italic; }
.unassigned { margin-top: 12px; }
.host-tools { margin-top: 16px; display: grid; gap: 12px; }
.host-tools .row { flex-wrap: wrap; }
.hint { color: var(--muted); font-size: 14px; margin: 0; }
.problems { color: var(--sun-ink); font-size: 14px; margin: 0; }
textarea { min-height: 96px; resize: vertical; }

/* ---------- game ---------- */
.scores { display: flex; gap: 8px; align-items: center; }
.score {
  display: inline-flex; align-items: center; gap: 6px; padding: 4px 12px; border-radius: 999px;
  font-weight: 800; font-size: 15px; color: #fff;
}
.score.sun { background: var(--sun); }
.score.sea { background: var(--sea); }
.score.active { box-shadow: 0 0 0 3px var(--bg), 0 0 0 5px currentColor; }
.score.sun.active { color: #fff; box-shadow: 0 0 0 3px var(--bg), 0 0 0 5px var(--sun); }
.score.sea.active { box-shadow: 0 0 0 3px var(--bg), 0 0 0 5px var(--sea); }

.banner {
  border-radius: var(--radius); padding: 12px 16px; margin-bottom: 12px; display: grid; gap: 4px;
  text-align: center; border: 2px solid transparent;
}
.banner.sun { background: var(--sun-soft); border-color: color-mix(in srgb, var(--sun) 45%, transparent); }
.banner.sea { background: var(--sea-soft); border-color: color-mix(in srgb, var(--sea) 45%, transparent); }
.banner .main { font-size: 19px; font-weight: 750; }
.banner .sub { font-size: 14px; color: var(--muted); }
.banner .clueword { font-size: 26px; font-weight: 850; letter-spacing: 0.04em; }
.banner.win { animation: pop 0.4s ease; }
@keyframes pop { from { transform: scale(0.96); opacity: 0.4; } to { transform: none; opacity: 1; } }

.clueform { display: grid; grid-template-columns: 1fr 88px auto; gap: 8px; margin-bottom: 12px; }
@media (max-width: 420px) { .clueform { grid-template-columns: 1fr 72px; } .clueform .btn { grid-column: 1 / -1; } }

.board { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: clamp(5px, 1.2vw, 10px); margin-bottom: 12px; }
.cardw {
  position: relative; aspect-ratio: 4 / 3; min-width: 0;
  border-radius: clamp(8px, 1.4vw, 12px); border: 1px solid var(--line);
  background: var(--card); color: var(--card-ink); box-shadow: var(--shadow);
  display: grid; place-items: center; padding: 4px;
  font-weight: 800; letter-spacing: 0.02em; text-align: center;
  /* shrink long words so they fit on one line instead of breaking mid-word */
  font-size: min(clamp(9px, 2.5vw, 17px), calc((min(100vw, 980px) - 90px) / 5 / (var(--len, 6) * 0.72)));
  line-height: 1.1; letter-spacing: 0;
  transition: transform 0.12s ease, background 0.2s ease, box-shadow 0.15s ease;
}
.cardw .w { overflow-wrap: normal; word-break: normal; }
.cardw .by {
  position: absolute; bottom: 3px; left: 0; right: 0; font-size: clamp(7px, 1.6vw, 11px);
  font-weight: 600; opacity: 0.8; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 0 4px;
}
.cardw:disabled { cursor: default; }
.cardw.clickable:hover { transform: translateY(-2px); }
.cardw.armed { box-shadow: 0 0 0 3px var(--bg), 0 0 0 6px var(--text); transform: translateY(-2px); }
.cardw.armed::after {
  content: 'tap again'; position: absolute; top: 3px; left: 0; right: 0;
  font-size: clamp(7px, 1.5vw, 10px); font-weight: 700; text-transform: uppercase; opacity: 0.7;
}

/* clue-giver key (unrevealed) */
.cardw.key-sun { background: var(--sun-soft); border: 2px solid var(--sun); color: var(--sun-ink); }
.cardw.key-sea { background: var(--sea-soft); border: 2px solid var(--sea); color: var(--sea-ink); }
.cardw.key-neutral { background: color-mix(in srgb, var(--neutral) 35%, var(--card)); border: 2px dashed var(--neutral); }
.cardw.key-trap { background: var(--trap); color: var(--trap-ink); border: 2px solid var(--trap); }

/* revealed */
.cardw.revealed { box-shadow: none; }
.cardw.t-sun { background: var(--sun); color: #fff; border-color: transparent; }
.cardw.t-sea { background: var(--sea); color: #fff; border-color: transparent; }
.cardw.t-neutral { background: var(--neutral); color: var(--neutral-ink); border-color: transparent; }
.cardw.t-trap { background: var(--trap); color: var(--trap-ink); border-color: transparent; }
.cardw.revealed.seen .w { opacity: 0.55; text-decoration: line-through; text-decoration-thickness: 2px; }
.cardw.t-trap .w::before { content: '✕ '; }
.cardw.key-trap .w::before { content: '✕ '; }

.actions { display: flex; gap: 8px; justify-content: center; margin-bottom: 18px; flex-wrap: wrap; }
.side { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 680px) { .side { grid-template-columns: 1fr; } }
.box { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.box h3 { font-size: 15px; margin-bottom: 8px; }
.log { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; font-size: 14px; max-height: 240px; overflow: auto; }
.log li { display: flex; gap: 8px; align-items: baseline; }
.log .dot { transform: translateY(1px); }
.cluelist { display: flex; flex-wrap: wrap; gap: 6px; }
.cluechip { padding: 4px 10px; border-radius: 999px; font-weight: 750; font-size: 14px; color: #fff; }
.cluechip.sun { background: var(--sun); }
.cluechip.sea { background: var(--sea); }
.footer-tools { display: flex; justify-content: center; gap: 8px; margin-top: 16px; flex-wrap: wrap; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(18px + env(safe-area-inset-bottom)); transform: translate(-50%, 30px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-weight: 600; font-size: 15px;
  max-width: calc(100vw - 32px); opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s; z-index: 10;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

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