:root {
  --bg-0: #05060f;
  --bg-1: #0a0e1c;
  --bg-2: #111830;
  --panel: rgba(18, 24, 48, .82);
  --ink: #eaf0ff;
  --muted: #8b97c4;
  --cyan: #00e5ff;
  --magenta: #ff2e97;
  --yellow: #ffd400;
  --green: #33e06a;
  --purple: #b14dff;
  --line: rgba(120, 150, 255, .18);
  --glow-cyan: 0 0 14px rgba(0, 229, 255, .55);
  --glow-mag: 0 0 14px rgba(255, 46, 151, .55);
  --radius: 16px;
  --font-disp: 'Orbitron', 'Pretendard Variable', sans-serif;
  --font-pix: 'Press Start 2P', 'Orbitron', monospace;
  --font-body: 'Pretendard Variable', Pretendard, -apple-system, sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body {
  height: 100%;
  font-family: var(--font-body);
  color: var(--ink);
  background: radial-gradient(1200px 800px at 50% -10%, #1a2352 0%, var(--bg-1) 55%, var(--bg-0) 100%);
  overflow: hidden;
}

/* 배경: 네온 그리드 + 스캔라인 */
.grid-bg {
  position: fixed; inset: 0; pointer-events: none; z-index: 0; opacity: .35;
  background-image:
    linear-gradient(rgba(80, 120, 255, .09) 1px, transparent 1px),
    linear-gradient(90deg, rgba(80, 120, 255, .09) 1px, transparent 1px);
  background-size: 42px 42px;
  mask-image: radial-gradient(circle at 50% 30%, #000 0%, transparent 85%);
  -webkit-mask-image: radial-gradient(circle at 50% 30%, #000 0%, transparent 85%);
}
.scanlines {
  position: fixed; inset: 0; pointer-events: none; z-index: 998; opacity: .06;
  background: repeating-linear-gradient(0deg, #fff 0 1px, transparent 1px 3px);
}

.screen { position: fixed; inset: 0; display: none; overflow-y: auto; z-index: 1; }
.screen.active { display: block; }

/* ===== 버튼 ===== */
.btn {
  font-family: var(--font-disp); font-weight: 700; font-size: 16px; color: var(--ink);
  border: 2px solid rgba(140, 170, 255, .5); border-radius: 12px; padding: 12px 20px; cursor: pointer;
  background: rgba(30, 40, 78, .8); transition: transform .08s, box-shadow .12s, background .12s;
  user-select: none; white-space: nowrap; letter-spacing: .3px;
}
.btn:hover { box-shadow: var(--glow-cyan); border-color: var(--cyan); }
.btn:active { transform: translateY(2px); }
.btn.big { font-size: 20px; padding: 15px 26px; width: 100%; }
.btn.small { font-size: 13px; padding: 8px 14px; }
.btn-primary { background: linear-gradient(135deg, var(--magenta), #c8006f); border-color: var(--magenta); color: #fff; }
.btn-primary:hover { box-shadow: var(--glow-mag); }
.btn-accent { background: linear-gradient(135deg, var(--cyan), #0099cc); border-color: var(--cyan); color: #04121a; }
.btn-ghost { background: rgba(30, 40, 78, .6); }
.btn:disabled { opacity: .4; cursor: not-allowed; filter: grayscale(.5); box-shadow: none; }

/* ===== 카드 ===== */
.card {
  background: var(--panel); border: 2px solid var(--line); border-radius: var(--radius);
  box-shadow: 0 10px 40px rgba(0, 0, 0, .5), inset 0 0 0 1px rgba(255, 255, 255, .03);
  padding: 22px; backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}

/* ===== 홈 ===== */
.home-wrap { min-height: 100%; display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 22px; padding: 32px 20px; }
.title-block { text-align: center; }
.tetro-rain { font-size: 40px; letter-spacing: 6px; animation: floaty 2.4s ease-in-out infinite; }
@keyframes floaty { 0%,100%{transform:translateY(0)} 50%{transform:translateY(-8px)} }
.game-title {
  font-family: var(--font-disp); font-weight: 900; font-size: clamp(34px, 9vw, 68px); line-height: 1;
  margin-top: 10px; color: #fff; letter-spacing: 1px;
  text-shadow: 0 0 18px rgba(0, 229, 255, .6), 0 0 40px rgba(255, 46, 151, .35);
}
.game-title span { color: var(--cyan); text-shadow: 0 0 20px rgba(0, 229, 255, .8); }
.subtitle { margin-top: 14px; font-size: 16px; color: var(--ink); background: rgba(0, 229, 255, .08);
  display: inline-block; padding: 7px 16px; border-radius: 999px; border: 1px solid rgba(0, 229, 255, .3); }
.subtitle b { color: var(--cyan); }

.home-card { width: min(440px, 100%); display: flex; flex-direction: column; gap: 16px; }
.fld { display: flex; flex-direction: column; gap: 6px; font-family: var(--font-disp); font-size: 14px; color: var(--muted); letter-spacing: .5px; }
.fld input, .join-row input {
  font-family: var(--font-body); font-size: 18px; font-weight: 600;
  border: 2px solid var(--line); border-radius: 12px; padding: 13px 16px; background: rgba(6, 10, 24, .8); color: var(--ink);
  outline: none; width: 100%;
}
.fld input:focus, .join-row input:focus { border-color: var(--cyan); box-shadow: var(--glow-cyan); }
.divider { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
.divider::before, .divider::after { content:''; flex:1; height: 1px; background: var(--line); }
.join-row { display: flex; gap: 10px; }
.join-row input { text-align: center; letter-spacing: 8px; font-family: var(--font-disp); font-weight: 800; text-transform: uppercase; }
.err-msg { color: var(--magenta); font-weight: 700; min-height: 18px; font-size: 14px; text-align: center; }
.foot { color: var(--muted); font-size: 12px; text-align: center; }

.join-room-badge { font-family: var(--font-disp); font-weight: 700; font-size: 18px; text-align: center;
  background: rgba(0, 229, 255, .08); color: var(--ink); border: 1px solid rgba(0,229,255,.3);
  border-radius: 12px; padding: 12px; margin-bottom: 4px; }
.join-room-badge b { color: var(--cyan); letter-spacing: 4px; }
#btn-back-home { align-self: center; }

/* ===== 로비 ===== */
.lobby-wrap { max-width: 1000px; margin: 0 auto; padding: 18px 16px 40px; }
.lobby-head { display: flex; align-items: center; justify-content: space-between; gap: 12px;
  flex-wrap: wrap; margin-bottom: 16px; }
.room-badge { display: flex; align-items: center; gap: 10px; background: rgba(6,10,24,.85); color: #fff;
  border: 2px solid var(--cyan); border-radius: 12px; padding: 8px 18px; box-shadow: var(--glow-cyan); }
.rb-label { font-size: 12px; color: var(--muted); }
.rb-code { font-family: var(--font-disp); font-size: 26px; font-weight: 800; letter-spacing: 5px; color: var(--cyan); }
.head-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.member-count { font-family: var(--font-disp); background: rgba(30,40,78,.7); border: 1px solid var(--line);
  padding: 7px 12px; border-radius: 999px; font-size: 13px; }

.lobby-grid { display: grid; grid-template-columns: 1.2fr 1fr; gap: 16px; align-items: start; }
@media (max-width: 820px) { .lobby-grid { grid-template-columns: 1fr; } }

.panel-title { font-family: var(--font-disp); font-weight: 700; font-size: 17px; display: flex; align-items: center;
  justify-content: space-between; margin-bottom: 14px; letter-spacing: .5px; }
.panel-title b { color: var(--cyan); }
.host-tag { font-size: 11px; background: var(--magenta); color: #fff; padding: 3px 10px; border-radius: 999px; display: none; }
body.is-host .host-tag { display: inline-block; }

.participant-list { display: flex; flex-wrap: wrap; gap: 8px; max-height: 46vh; overflow-y: auto; }
.p-chip { display: flex; align-items: center; gap: 8px; background: rgba(6,10,24,.7); border: 2px solid var(--line);
  border-radius: 999px; padding: 6px 10px 6px 10px; animation: pop .2s ease; }
@keyframes pop { from { transform: scale(.6); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.p-chip .dot { width: 12px; height: 12px; border-radius: 3px; flex: none; box-shadow: 0 0 6px currentColor; }
.p-chip .p-emoji { font-size: 17px; }
.p-chip .p-name { font-weight: 700; font-size: 14px; max-width: 120px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.p-chip .x { border: none; background: none; cursor: pointer; font-size: 15px; color: var(--magenta); padding: 0 2px; font-weight: 800; }
.p-chip.me { border-color: var(--cyan); box-shadow: 0 0 0 1px rgba(0,229,255,.3); }
.p-chip.ready { background: rgba(51,224,106,.14); border-color: rgba(51,224,106,.5); }
.tag-host { font-size: 10px; background: var(--magenta); color: #fff; padding: 1px 6px; border-radius: 999px; }
.tag-me { font-size: 10px; background: var(--cyan); color: #04121a; padding: 1px 6px; border-radius: 999px; font-weight: 700; }
.ready-badge { font-size: 13px; }

/* 설정 */
.setting { display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 12px 0; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.setting:last-of-type { border-bottom: none; }
.s-label { font-family: var(--font-disp); font-weight: 600; font-size: 14px; }
.seg { display: flex; background: rgba(6,10,24,.7); border: 2px solid var(--line); border-radius: 10px; overflow: hidden; }
.seg-wrap { flex-wrap: wrap; }
.seg-btn { font-family: var(--font-disp); font-weight: 600; border: none; background: none; color: var(--ink);
  padding: 8px 13px; cursor: pointer; font-size: 13px; }
.seg-btn.active { background: var(--cyan); color: #04121a; }
.stepper { display: flex; align-items: center; gap: 10px; font-family: var(--font-disp); font-weight: 800; font-size: 18px; }
.st-btn { width: 32px; height: 32px; border-radius: 8px; border: 2px solid var(--line); background: rgba(6,10,24,.7);
  color: var(--ink); font-size: 18px; font-weight: 800; cursor: pointer; line-height: 1; }
.st-btn:hover { border-color: var(--cyan); }
.st-suffix { font-size: 13px; color: var(--muted); }
body:not(.is-host) .seg-btn, body:not(.is-host) .st-btn,
body:not(.is-host) .switch input { pointer-events: none; opacity: .85; }

.switch { position: relative; width: 52px; height: 28px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider { position: absolute; inset: 0; background: rgba(6,10,24,.9); border: 2px solid var(--line); border-radius: 999px; transition: .2s; }
.slider::before { content:''; position: absolute; width: 18px; height: 18px; left: 3px; top: 3px;
  background: var(--muted); border-radius: 50%; transition: .2s; }
.switch input:checked + .slider { background: rgba(0,229,255,.2); border-color: var(--cyan); }
.switch input:checked + .slider::before { transform: translateX(24px); background: var(--cyan); box-shadow: var(--glow-cyan); }

.hint { font-size: 12px; color: var(--muted); margin-top: 8px; }
.hint.center { text-align: center; }
.setting-desc { font-size: 12px; color: var(--muted); line-height: 1.55; margin-top: 12px;
  background: rgba(0,229,255,.05); border: 1px solid rgba(0,229,255,.18); border-radius: 10px; padding: 9px 12px; }
.setting-desc b { color: var(--cyan); }
.start-btn, .ready-btn { margin-top: 16px; }
.ready-btn.is-ready { background: linear-gradient(135deg, var(--green), #22a94f); border-color: var(--green); color: #04140a; }
.notice { background: rgba(255,46,151,.08); border: 1px solid rgba(255,46,151,.3); border-radius: 10px;
  padding: 10px 12px; font-size: 13px; font-weight: 600; margin-bottom: 10px; }
.settings-body.locked { opacity: .7; pointer-events: none; }
.ready-count { font-size: 12px; background: rgba(6,10,24,.8); color: var(--ink); padding: 4px 10px; border-radius: 999px; border: 1px solid var(--line); }

/* 조작법 안내 */
.controls-guide { margin-top: 16px; }
.cg-sub { font-size: 12px; color: var(--muted); font-weight: 400; }
.keys-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 10px 18px; }
.key-item { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--ink); }
.key-item b { font-weight: 600; color: var(--muted); }
.keycap { display: inline-flex; align-items: center; justify-content: center; min-width: 26px; height: 26px;
  padding: 0 7px; background: rgba(6,10,24,.9); border: 1px solid var(--line); border-bottom-width: 3px;
  border-radius: 7px; font-family: var(--font-disp); font-weight: 700; font-size: 12px; color: var(--cyan);
  box-shadow: 0 1px 0 rgba(0,0,0,.4); }

/* ===== 게임 ===== */
#screen-game { background: transparent; }
.game-topbar { position: fixed; top: 0; left: 0; right: 0; height: 46px; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; padding: 0 16px; gap: 12px;
  background: rgba(6,10,24,.9); border-bottom: 1px solid var(--line); font-family: var(--font-disp); font-weight: 700; }
.game-status { font-size: 14px; color: var(--muted); }
.alive-count { font-size: 15px; color: var(--green); text-shadow: 0 0 8px rgba(51,224,106,.5); }
.game-timer { font-family: var(--font-disp); font-weight: 800; font-size: 17px; color: var(--yellow); min-width: 60px; text-align: right; }

.arena {
  position: fixed; top: 46px; left: 0; right: 0; bottom: 0;
  display: grid; grid-template-columns: 1fr auto 1fr; gap: 10px;
  padding: 10px; overflow: hidden;
}
.opp-col { display: flex; flex-wrap: wrap; gap: 10px; align-content: center; justify-content: center;
  align-items: center; overflow-y: auto; max-height: 100%; }
.opp-left { justify-content: flex-end; }
.opp-right { justify-content: flex-start; }

.opp { position: relative; background: rgba(6,10,24,.6); border: 1px solid var(--line); border-radius: 8px;
  padding: 6px; display: flex; flex-direction: column; align-items: center; gap: 3px; }
.opp-canvas { display: block; border-radius: 4px; background: #0c1020; box-shadow: inset 0 0 12px rgba(0,0,0,.5); }
.opp-label { display: flex; align-items: center; gap: 4px; max-width: 100%; }
.opp-av { font-size: 13px; }
.opp-name { font-size: 12px; font-weight: 600; color: var(--ink); max-width: 100px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.opp-stat { font-family: var(--font-disp); font-size: 10px; color: var(--muted); letter-spacing: .3px; }
.opp-badge { position: absolute; top: 45%; left: 50%; transform: translate(-50%,-50%); font-size: 28px; }
.opp.dead { opacity: .55; filter: grayscale(.6); }
.opp.dead .opp-name { color: var(--magenta); }

/* 중앙 내 보드 */
.my-area { display: flex; align-items: flex-start; justify-content: center; gap: 10px; }
.side { display: flex; flex-direction: column; gap: 8px; padding-top: 4px; }
.side-right { align-items: flex-start; }
.mini-box { background: rgba(6,10,24,.7); border: 1px solid var(--line); border-radius: 10px;
  padding: 8px; display: flex; flex-direction: column; align-items: center; gap: 4px; }
.mini-label { font-family: var(--font-disp); font-weight: 700; font-size: 11px; color: var(--muted); letter-spacing: 1px; }
.mini-box canvas { display: block; }
.stat { background: rgba(6,10,24,.7); border: 1px solid var(--line); border-radius: 10px; padding: 7px 10px;
  min-width: 78px; text-align: center; }
.stat span { display: block; font-family: var(--font-disp); font-size: 10px; color: var(--muted); letter-spacing: 1px; }
.stat b { font-family: var(--font-disp); font-weight: 800; font-size: 18px; color: var(--cyan); }

.board-wrap { position: relative; }
#my-canvas { display: block; border: 2px solid rgba(0,229,255,.4); border-radius: 6px;
  box-shadow: 0 0 30px rgba(0,229,255,.25), inset 0 0 40px rgba(0,0,0,.6); background: #0a0e1c; }

/* 라인 클리어 텍스트 이펙트 */
.line-flash { position: absolute; top: 34%; left: 50%; transform: translate(-50%,-50%);
  text-align: center; pointer-events: none; opacity: 0; z-index: 8; }
.line-flash.show { animation: flashpop .8s ease; }
.line-flash .fl-main { display: block; font-family: var(--font-disp); font-weight: 900; font-size: 34px;
  color: #fff; text-shadow: 0 0 16px var(--cyan), 0 0 30px var(--cyan); letter-spacing: 2px; }
.line-flash .fl-4 { color: var(--yellow); text-shadow: 0 0 18px var(--yellow), 0 0 40px var(--magenta); font-size: 42px; }
.line-flash .fl-combo { display: block; margin-top: 4px; font-family: var(--font-disp); font-weight: 800;
  font-size: 18px; color: var(--magenta); text-shadow: 0 0 12px var(--magenta); }
@keyframes flashpop {
  0% { opacity: 0; transform: translate(-50%,-50%) scale(.4); }
  25% { opacity: 1; transform: translate(-50%,-50%) scale(1.15); }
  70% { opacity: 1; transform: translate(-50%,-60%) scale(1); }
  100% { opacity: 0; transform: translate(-50%,-90%) scale(.9); }
}

.countdown { position: absolute; inset: 0; z-index: 20; display: flex; align-items: center; justify-content: center;
  background: rgba(6,10,24,.55); border-radius: 6px; }
.countdown span { font-family: var(--font-disp); font-weight: 900; font-size: 22vh; color: #fff;
  text-shadow: 0 0 30px var(--cyan), 0 0 60px var(--magenta); animation: cd .95s ease-in-out; }
@keyframes cd { 0% { transform: scale(2.2); opacity: 0; } 40% { transform: scale(1); opacity: 1; } 100% { transform: scale(.4); opacity: 0; } }

@media (max-width: 900px) {
  .arena { grid-template-columns: 1fr; grid-template-rows: auto auto auto; overflow-y: auto; }
  .my-area { order: -1; }
  .side { flex-direction: row; flex-wrap: wrap; justify-content: center; }
  .mini-box { flex-direction: row; }
  .opp-col { flex-direction: row; max-height: none; }
}

/* 터치 컨트롤 */
.touch-controls { position: fixed; bottom: 0; left: 0; right: 0; z-index: 15; display: none;
  justify-content: space-between; padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
  background: linear-gradient(0deg, rgba(6,10,24,.95), transparent); pointer-events: none; }
.tc-left, .tc-right { display: flex; gap: 8px; pointer-events: auto; }
.tc-btn { width: 58px; height: 58px; border-radius: 14px; border: 2px solid var(--line);
  background: rgba(30,40,78,.9); color: var(--ink); font-family: var(--font-disp); font-weight: 700;
  font-size: 18px; cursor: pointer; user-select: none; touch-action: manipulation; }
.tc-btn:active { background: var(--cyan); color: #04121a; }
.tc-btn.tc-drop { background: rgba(255,46,151,.9); border-color: var(--magenta); font-size: 13px; }
body.touch #touch-controls { display: flex; }

/* 결과 */
.result-modal { position: fixed; inset: 0; z-index: 30; display: flex; align-items: center; justify-content: center;
  background: rgba(3,5,14,.75); padding: 20px; backdrop-filter: blur(4px); }
.result-card { position: relative; background: var(--panel); border: 2px solid var(--cyan); border-radius: 20px;
  box-shadow: 0 0 50px rgba(0,229,255,.3); padding: 26px 24px; width: min(460px, 100%); text-align: center;
  overflow: hidden; animation: pop .3s ease; max-height: 92vh; display: flex; flex-direction: column; }
.trophy { font-size: 60px; animation: floaty 1.6s ease-in-out infinite; }
.result-title { font-family: var(--font-disp); font-weight: 900; font-size: 30px; margin: 4px 0 16px;
  color: var(--cyan); text-shadow: 0 0 16px rgba(0,229,255,.6); }
.winner-list { display: flex; flex-direction: column; gap: 10px; margin-bottom: 14px; max-height: 34vh; overflow-y: auto; }
.winner-item { display: flex; align-items: center; gap: 12px; background: rgba(255,212,0,.1); border: 2px solid var(--yellow);
  border-radius: 14px; padding: 12px 16px; box-shadow: 0 0 20px rgba(255,212,0,.2); }
.winner-item .medal { font-size: 24px; }
.winner-item .w-emoji { font-size: 22px; }
.winner-item .w-name { font-family: var(--font-disp); font-weight: 800; font-size: 20px; }
.winner-item .w-rank { margin-left: auto; font-family: var(--font-disp); color: var(--muted); font-size: 14px; }
.result-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.full-rank { margin-top: 12px; text-align: left; border-top: 1px solid var(--line); padding-top: 12px;
  max-height: 30vh; overflow-y: auto; }
.fr-title { font-family: var(--font-disp); font-weight: 700; font-size: 13px; color: var(--muted); }
.rank-row { display: flex; align-items: center; gap: 10px; padding: 6px 4px; font-size: 14px; }
.rank-row .rank-no { font-family: var(--font-disp); font-weight: 700; width: 32px; color: var(--cyan); }
.rank-dot { width: 11px; height: 11px; border-radius: 3px; box-shadow: 0 0 5px currentColor; }
.rank-row .r-score { margin-left: auto; color: var(--muted); font-family: var(--font-disp); font-size: 12px; }

.confetti { position: fixed; inset: 0; pointer-events: none; z-index: 31; overflow: hidden; }
.confetti i { position: absolute; width: 9px; height: 13px; top: -20px; opacity: .9; animation: fall linear forwards; }
@keyframes fall { to { transform: translateY(120vh) rotate(720deg); } }

.toast { position: fixed; bottom: 26px; left: 50%; transform: translateX(-50%); z-index: 50;
  background: rgba(6,10,24,.95); color: #fff; border: 1px solid var(--cyan); padding: 12px 20px; border-radius: 12px;
  font-weight: 700; box-shadow: var(--glow-cyan); animation: pop .2s ease; }

/* ===== 채팅 ===== */
.chat { position: fixed; left: 12px; bottom: 12px; z-index: 25; width: 270px; max-width: calc(100vw - 24px);
  background: rgba(10,14,28,.82); border: 1px solid var(--line); border-radius: 14px;
  box-shadow: 0 8px 30px rgba(0,0,0,.5); overflow: hidden; font-family: var(--font-body);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px); }
.chat-head { background: rgba(6,10,24,.9); color: var(--cyan); font-family: var(--font-disp); font-weight: 700;
  padding: 7px 12px; cursor: grab; font-size: 13px; display: flex; justify-content: space-between; align-items: center;
  touch-action: none; user-select: none; }
.chat-head:active { cursor: grabbing; }
.chat.collapsed .chat-body { display: none; }
.chat-log { height: 140px; overflow-y: auto; padding: 8px 10px; display: flex; flex-direction: column; gap: 4px; font-size: 13px; }
.chat-msg { line-height: 1.35; word-break: break-word; }
.chat-msg b { color: var(--cyan); }
.chat-form { display: flex; gap: 6px; padding: 8px; border-top: 1px solid var(--line); }
.chat-form input { flex: 1; min-width: 0; border: 1px solid var(--line); border-radius: 9px; background: rgba(6,10,24,.8);
  color: var(--ink); padding: 7px 10px; font-size: 14px; font-family: var(--font-body); outline: none; }
.chat-form input:focus { border-color: var(--cyan); }
.chat-send { border: 1px solid var(--cyan); background: rgba(0,229,255,.15); color: var(--cyan); border-radius: 9px;
  padding: 0 12px; font-family: var(--font-disp); font-weight: 700; cursor: pointer; font-size: 12px; }
