:root {
  --navy: #0A2540;
  --navy-2: #12355E;
  --navy-3: #1B4677;
  --teal: #00C896;
  --teal-dark: #007A5C;
  --mint: #DDF7EE;
  --paper: #FFFFFF;
  --ink: #0A2540;
  --muted: #8FA6C1;
  --red: #FF5A6E;
  --gold: #FFC93C;
  --radius: 12px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(120% 80% at 50% 0%, var(--navy-2) 0%, var(--navy) 60%);
  background-color: var(--navy);
  color: #fff;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Calibri, Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button, input, select { font: inherit; }
[hidden] { display: none !important; }

.screen { min-height: 100dvh; padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom)); }
.center { display: flex; flex-direction: column; justify-content: center; align-items: center; text-align: center; }
.box { width: 100%; max-width: 420px; }

.logo { font-size: 44px; line-height: 1; margin-bottom: 8px; }
h1 { font-size: 30px; margin: 0 0 4px; letter-spacing: -0.02em; }
h1 span { color: var(--teal); }
.sub { color: var(--muted); margin: 0 0 20px; font-size: 14px; }

.rules { list-style: none; padding: 0; margin: 0 0 22px; text-align: left; display: grid; gap: 10px; }
.rules li { background: rgba(255,255,255,.06); border-radius: var(--radius); padding: 10px 12px; font-size: 15px; line-height: 1.35; display: flex; gap: 10px; align-items: flex-start; }
.rules li b.n { flex: none; width: 24px; height: 24px; border-radius: 50%; background: var(--teal); color: var(--navy); display: grid; place-items: center; font-size: 13px; }
.chip { display: inline-block; padding: 1px 7px; border-radius: 6px; font-size: 12px; font-weight: 800; letter-spacing: .04em; vertical-align: 1px; }
.chip.term { background: var(--paper); color: var(--ink); }
.chip.ex { background: var(--mint); color: var(--teal-dark); }

input[type=text] {
  width: 100%; padding: 14px 16px; border-radius: var(--radius); border: 2px solid var(--navy-3);
  background: rgba(255,255,255,.08); color: #fff; font-size: 18px; outline: none;
}
input[type=text]:focus { border-color: var(--teal); }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 0; border-radius: var(--radius); padding: 14px 20px; font-weight: 700; font-size: 17px;
  background: var(--teal); color: var(--navy); cursor: pointer; touch-action: manipulation;
}
.btn:active { transform: scale(.98); }
.btn.ghost { background: rgba(255,255,255,.1); color: #fff; }
.btn.full { width: 100%; margin-top: 12px; }
.error { color: var(--red); min-height: 20px; margin: 8px 0 0; font-size: 14px; }

/* lobby */
.wait { font-size: 18px; margin: 18px 0 6px; }
.dots span { display: inline-block; width: 10px; height: 10px; margin: 0 3px; border-radius: 50%; background: var(--teal); animation: bounce 1.2s infinite ease-in-out; }
.dots span:nth-child(2) { animation-delay: .15s; }
.dots span:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%, 80%, 100% { transform: scale(.5); opacity: .5; } 40% { transform: scale(1); opacity: 1; } }
.count { color: var(--muted); margin: 10px 0 26px; }
.row { display: flex; gap: 10px; }
.row .btn { flex: 1; }

/* play */
#play { padding: 0; height: 100dvh; display: flex; flex-direction: column; overflow: hidden; }
.bar {
  display: flex; align-items: center; gap: 8px; padding: max(8px, env(safe-area-inset-top)) 10px 6px;
  font-variant-numeric: tabular-nums; font-size: 15px;
}
.bar .brand { font-weight: 800; margin-right: auto; font-size: 14px; white-space: nowrap; }
.bar .brand span { color: var(--teal); }
.stat { background: rgba(255,255,255,.08); border-radius: 8px; padding: 4px 8px; white-space: nowrap; }

#grid {
  flex: 1; min-height: 0; display: grid; gap: 6px; padding: 4px 8px;
  grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(var(--rows, 4), 1fr);
  max-width: 720px; width: 100%; margin: 0 auto;
}
.card {
  position: relative; border: 0; padding: 0; background: none; perspective: 700px; cursor: pointer;
  touch-action: manipulation; min-width: 0; min-height: 0; color: var(--ink);
}
.card .inner { position: absolute; inset: 0; transition: transform .35s cubic-bezier(.3,.7,.4,1.2); transform-style: preserve-3d; }
.card.flipped .inner, .card.matched .inner { transform: rotateY(180deg); }
.back, .face {
  position: absolute; inset: 0; border-radius: 10px; backface-visibility: hidden; -webkit-backface-visibility: hidden;
}
.back {
  background:
    radial-gradient(circle, rgba(0,200,150,.22) 1.5px, transparent 2px) 0 0 / 12px 12px,
    linear-gradient(145deg, var(--navy-3), var(--navy-2));
  border: 2px solid rgba(0,200,150,.45);
  display: grid; place-items: center;
}
.back b { font-size: clamp(22px, 7vw, 40px); color: var(--teal); text-shadow: 0 2px 0 rgba(0,0,0,.25); }
.back::after { content: "🦖"; position: absolute; right: 5px; bottom: 3px; font-size: 11px; opacity: .55; }
.face {
  transform: rotateY(180deg); background: var(--paper); border: 2px solid #fff;
  display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center;
  padding: 4px 4px 5px; gap: 2px; overflow: hidden; container-type: size;
}
.card.ex .face { background: var(--mint); border-color: var(--mint); }
.face .tag {
  font-size: 8px; font-weight: 800; letter-spacing: .08em; text-transform: uppercase;
  background: var(--navy); color: #fff; border-radius: 4px; padding: 1px 5px;
}
.card.ex .face .tag { background: var(--teal-dark); }
.face .emo { font-size: clamp(20px, 7vw, 38px); line-height: 1.1; }
.face .title { font-weight: 800; font-size: clamp(10px, 2.9vw, 15px); line-height: 1.12; hyphens: auto; overflow-wrap: anywhere; }
.face .desc {
  font-size: clamp(8.5px, 2.3vw, 12px); line-height: 1.15; color: #3B5576;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden;
}
@container (max-height: 120px) { .face .desc { display: none; } }
@container (max-height: 82px) { .face .tag { display: none; } .face .emo { font-size: 20px; } }

.card.matched .face { border-color: var(--teal); box-shadow: 0 0 0 2px var(--teal), 0 0 14px rgba(0,200,150,.6); }
.card.matched .face::after { content: "✓"; position: absolute; top: 2px; right: 4px; color: var(--teal-dark); font-weight: 900; font-size: 13px; }
.card.matched { animation: pop .4s ease; }
.card.wrong .face { border-color: var(--red); box-shadow: 0 0 0 2px var(--red); }
.card.wrong { animation: shake .4s ease .3s; }
@keyframes pop { 50% { transform: scale(1.08); } }
@keyframes shake { 20%, 60% { transform: translateX(-4px); } 40%, 80% { transform: translateX(4px); } }

#panel {
  flex: none; height: 128px; margin: 4px 8px max(8px, env(safe-area-inset-bottom)); padding: 8px 10px;
  border-radius: var(--radius); background: rgba(255,255,255,.07); overflow: hidden;
  max-width: 704px; width: calc(100% - 16px); align-self: center; position: relative;
}
#panel.cooldown::after {
  content: ""; position: absolute; left: 0; bottom: 0; height: 3px; width: 100%; background: var(--teal);
  transform-origin: left; animation: cooldown 1s linear forwards;
}
@keyframes cooldown { from { transform: scaleX(1); } to { transform: scaleX(0); } }
#panel .status { font-weight: 800; font-size: 15px; margin-bottom: 6px; }
#panel .status.ok { color: var(--teal); }
#panel .status.no { color: var(--red); }
#panel .minis { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.mini { border-radius: 8px; padding: 5px 7px; background: var(--paper); color: var(--ink); font-size: 12.5px; line-height: 1.25; min-height: 0; }
.mini.ex { background: var(--mint); }
.mini b { display: block; font-size: 13.5px; }
.mini span { display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; color: #3B5576; }
#panel .why { margin: 6px 0 0; font-size: 13px; color: #CFE0F2; }
#panel .hint { font-size: 15px; line-height: 1.4; color: #CFE0F2; }

#countdown {
  position: fixed; inset: 0; background: rgba(10,37,64,.88); display: grid; place-items: center; z-index: 20; text-align: center;
}
#countdown .num { font-size: 120px; font-weight: 900; color: var(--teal); line-height: 1; }
#countdown p { font-size: 20px; margin: 10px 0 0; }

/* done */
.trophy { font-size: 72px; line-height: 1; }
.rank { font-size: 40px; font-weight: 900; margin: 8px 0 4px; }
.rank.first { color: var(--gold); }
.facts { display: flex; gap: 10px; justify-content: center; margin: 14px 0 18px; }
.facts div { background: rgba(255,255,255,.08); border-radius: var(--radius); padding: 8px 14px; font-variant-numeric: tabular-nums; }
.facts b { display: block; font-size: 22px; }
.facts small { color: var(--muted); }
.list { list-style: none; padding: 0; margin: 0 0 18px; text-align: left; display: grid; gap: 6px; }
.list li { display: flex; gap: 10px; align-items: center; background: rgba(255,255,255,.06); border-radius: 10px; padding: 8px 12px; font-variant-numeric: tabular-nums; }
.list li .who { flex: 1; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list li .t { color: var(--muted); font-size: 14px; }
.list li.me { outline: 2px solid var(--teal); }
h2 { font-size: 16px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 22px 0 10px; text-align: left; }
.recap { list-style: none; padding: 0; margin: 0; text-align: left; display: grid; gap: 8px; }
.recap li { background: rgba(255,255,255,.06); border-radius: 10px; padding: 10px 12px; font-size: 14px; line-height: 1.35; }
.recap li b { color: #fff; }
.recap li .arrow { color: var(--teal); margin: 0 4px; }
.recap li small { display: block; color: #B8CBE0; margin-top: 2px; }

#offline { position: fixed; top: 8px; left: 50%; transform: translateX(-50%); background: var(--red); color: #fff; padding: 6px 12px; border-radius: 8px; font-size: 13px; z-index: 30; }

/* board (projector) */
.board { height: 100dvh; display: grid; grid-template-rows: auto 1fr auto; padding: 2.2vw 3vw 1.4vw; gap: 1.6vw; overflow: hidden; }
.board header { display: flex; align-items: baseline; gap: 1.5vw; }
.board header h1 { font-size: 3.4vw; margin: 0; }
.board header p { color: var(--muted); font-size: 1.5vw; margin: 0; }
.board main { display: grid; grid-template-columns: minmax(0, 0.9fr) minmax(0, 1.1fr); gap: 3vw; min-height: 0; }
.qrbox { background: #fff; color: var(--navy); border-radius: 2vw; padding: 2vw; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; min-height: 0; }
.qrbox img { width: min(100%, 52vh); aspect-ratio: 1; image-rendering: pixelated; }
.qrbox .url { font-size: 2.3vw; font-weight: 900; margin-top: 1vw; letter-spacing: -0.01em; }
.qrbox .steps { font-size: 1.35vw; color: #33506F; margin-top: .4vw; }
.side { display: flex; flex-direction: column; min-height: 0; }
.side .state { font-size: 2.8vw; font-weight: 900; margin: 0 0 .6vw; }
.side .state small { display: block; font-size: 1.5vw; color: var(--muted); font-weight: 600; margin-top: .3vw; }
.bignum { font-size: 5vw; font-weight: 900; color: var(--teal); line-height: 1; font-variant-numeric: tabular-nums; }
.names { display: flex; flex-wrap: wrap; gap: .5vw; align-content: flex-start; overflow: hidden; min-height: 0; margin-top: 1vw; }
.names span { background: rgba(255,255,255,.1); border-radius: 999px; padding: .35vw 1vw; font-size: 1.3vw; animation: pop .4s ease; }
.names span.away { opacity: .45; }
.ranking { list-style: none; margin: 1vw 0 0; padding: 0; display: grid; gap: .5vw; overflow: hidden; min-height: 0; }
.ranking li { display: grid; grid-template-columns: 4vw 1fr auto auto; gap: 1.2vw; align-items: center; background: rgba(255,255,255,.07); border-radius: 1vw; padding: .6vw 1.2vw; font-size: 1.7vw; font-variant-numeric: tabular-nums; }
.ranking li:first-child { background: rgba(255,201,60,.18); outline: 2px solid var(--gold); font-size: 2.2vw; }
.ranking li .pos { font-weight: 900; text-align: center; }
.ranking li .who { font-weight: 800; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ranking li .t { color: #CFE0F2; }
.ranking li .m { color: var(--muted); font-size: .8em; }
.winner { margin-top: 1vw; background: var(--gold); color: var(--navy); border-radius: 1.2vw; padding: 1vw 1.6vw; font-size: 2.4vw; font-weight: 900; text-align: center; }
.board footer { display: flex; gap: .8vw; align-items: center; justify-content: flex-end; font-size: 14px; color: var(--muted); }
.board footer select { background: var(--navy-2); color: #fff; border: 1px solid var(--navy-3); border-radius: 8px; padding: 8px 10px; }
.board footer .btn { padding: 9px 16px; font-size: 15px; }
.boardpage #countdown .num { font-size: 22vw; }
.boardpage #countdown p { font-size: 3vw; }

@media (max-width: 800px) {
  .board { height: auto; min-height: 100dvh; overflow: visible; padding: 16px; gap: 16px; }
  .board header { flex-direction: column; gap: 2px; }
  .board header h1 { font-size: 28px; }
  .board header p, .qrbox .steps, .side .state small { font-size: 14px; }
  .board main { grid-template-columns: 1fr; }
  .qrbox { padding: 16px; border-radius: 16px; }
  .qrbox .url { font-size: 18px; }
  .side .state { font-size: 24px; }
  .bignum { font-size: 40px; }
  .names span { font-size: 14px; padding: 4px 10px; }
  .ranking li { grid-template-columns: 32px 1fr auto auto; font-size: 16px; padding: 8px 12px; gap: 10px; }
  .ranking li:first-child { font-size: 18px; }
  .winner { font-size: 20px; padding: 12px; }
  .board footer { flex-wrap: wrap; justify-content: flex-start; }
}
