:root {
  --bg: #0d1016;
  --panel: #161b24;
  --panel-2: #1d2430;
  --line: #2a3342;
  --text: #e8ecf2;
  --muted: #8a96a8;
  --accent: #f5d742;
  --accent-2: #2fd3e6;
  --court: #2d8a3a;
  --clay: #d8742a;
  --danger: #ff6464;
  --ok: #5fd27a;
  --radius: 10px;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Noto Sans KR", "Segoe UI", sans-serif; }
code, kbd, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
kbd { background: var(--panel-2); border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; padding: 0 5px; font-size: 12px; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.center { text-align: center; }

.top { display: flex; align-items: center; justify-content: space-between; padding: 12px 20px; border-bottom: 1px solid var(--line); }
.brand { font-weight: 900; letter-spacing: 2px; font-size: 20px; color: var(--accent-2); }
.brand .vs { color: var(--accent); }
.brand .online { color: var(--muted); font-size: 12px; letter-spacing: 3px; margin-left: 4px; }
.conn { font-size: 12px; padding: 3px 10px; border-radius: 99px; background: var(--panel-2); color: var(--muted); }
.conn.ok { color: var(--ok); }
.conn.bad { color: var(--danger); }

.view { max-width: 860px; margin: 18px auto; padding: 0 16px; display: grid; gap: 14px; }
#view-game { max-width: none; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; }
.card h2 { margin: 0 0 10px; font-size: 14px; letter-spacing: 1px; color: var(--accent); text-transform: uppercase; }
.row { display: flex; align-items: center; flex-wrap: wrap; margin: 8px 0; }
.gap { gap: 8px; }

input, select { background: var(--panel-2); color: var(--text); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font: inherit; }
input:focus, select:focus { outline: 2px solid var(--accent-2); outline-offset: -1px; }
#name { width: 220px; }
.code-input { width: 100px; text-transform: uppercase; letter-spacing: 4px; font-family: ui-monospace, Menlo, monospace; text-align: center; }

.btn { background: var(--panel-2); color: var(--text); border: 1px solid var(--line); border-radius: 8px; padding: 8px 14px; font: inherit; cursor: pointer; }
.btn:hover:not(:disabled) { border-color: var(--accent-2); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: #1b1600; border-color: var(--accent); font-weight: 700; }
.btn.ghost { background: transparent; }
.btn.big { padding: 11px 26px; font-size: 16px; }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.file { position: relative; overflow: hidden; }
.btn.file input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.status { margin-left: 10px; color: var(--muted); }
.status.ok { color: var(--ok); }
.status.bad { color: var(--danger); }

.keys { border-collapse: collapse; }
.keys td { padding: 3px 14px 3px 0; }
.keys td:first-child { color: var(--muted); }

.room-head { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 10px; }
.room-code { font: 800 36px/1 ui-monospace, Menlo, monospace; letter-spacing: 8px; color: var(--accent); }

.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; margin-bottom: 12px; }
.seg-btn { background: transparent; color: var(--muted); border: 0; padding: 8px 16px; font: inherit; cursor: pointer; }
.seg-btn + .seg-btn { border-left: 1px solid var(--line); }
.seg-btn.on { background: var(--accent-2); color: #002227; font-weight: 700; }
.seg-btn:disabled { cursor: default; }

.court { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 6px 0 10px; }
.side { border-radius: var(--radius); padding: 10px; background: linear-gradient(180deg, rgba(45,138,58,.25), rgba(45,138,58,.08)); border: 1px solid rgba(45,138,58,.5); }
.side h3 { margin: 0 0 8px; font-size: 12px; color: var(--muted); font-weight: 600; letter-spacing: 1px; }
.seat { display: flex; align-items: center; justify-content: space-between; gap: 8px; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; margin-bottom: 6px; min-height: 44px; }
.seat .tag { font: 700 13px ui-monospace, Menlo, monospace; color: var(--accent); min-width: 26px; }
.seat .who { flex: 1; }
.seat.me { border-color: var(--accent-2); }
.seat .off { color: var(--danger); font-size: 12px; }
.seat .rom-bad { color: var(--danger); font-size: 12px; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px 16px; margin-bottom: 8px; }
.grid label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.grid label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
#settings.readonly select, #settings.readonly input { pointer-events: none; opacity: .7; }

.chat-log { height: 120px; overflow-y: auto; background: var(--panel-2); border-radius: 8px; padding: 8px 10px; font-size: 13px; }
.chat-log .from { color: var(--accent-2); margin-right: 6px; }
.chat-log .sys { color: var(--muted); font-style: italic; }
#chat-form input { flex: 1; }

/* game */
.screens { position: relative; display: flex; justify-content: center; align-items: flex-start; gap: 12px; padding: 0 8px; }
.screen-wrap { display: flex; flex-direction: column; align-items: center; }
.screen-label { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
canvas { display: block; background: #000; border-radius: 4px; box-shadow: 0 0 0 1px var(--line), 0 10px 40px rgba(0,0,0,.6); }
.overlay { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; text-align: center; font-size: 18px; font-weight: 700; background: rgba(0,0,0,.55); pointer-events: none; white-space: pre-line; }
.hud { display: flex; align-items: center; flex-wrap: wrap; gap: 10px; max-width: 1100px; margin: 4px auto 0; padding: 8px 12px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); }
.hud .spacer { flex: 1; }
.pill { background: var(--panel-2); border-radius: 99px; padding: 2px 10px; font-size: 13px; color: var(--accent); }
.mono { font-size: 12px; color: var(--muted); }
.warn { color: var(--danger); font-size: 13px; font-weight: 700; }
.vol { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.vol input { width: 90px; padding: 0; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--panel-2); border: 1px solid var(--line); padding: 10px 18px; border-radius: 8px; box-shadow: 0 6px 30px rgba(0,0,0,.5); z-index: 10; }
.toast.err { border-color: var(--danger); }

@media (max-width: 640px) {
  .court { grid-template-columns: 1fr; }
  .room-code { font-size: 28px; }
}

/* login */
.login { max-width: 420px; margin: 40px auto 0; width: 100%; }
.login form input { flex: 1; min-width: 0; }

/* touch gamepad */
:root { --pad: 156px; --btn: 74px; --touch-h: 250px; }
.touch, .touch * { user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; }
.touch { touch-action: none; z-index: 5; }
.touch button { touch-action: none; font: inherit; cursor: pointer; border: 0; }
.tpad { position: relative; width: var(--pad); height: var(--pad); border-radius: 50%; touch-action: none;
  background: radial-gradient(circle, rgba(255,255,255,.06) 0 38%, rgba(255,255,255,.12) 39% 100%); border: 2px solid rgba(255,255,255,.18); }
.tpad-ring { position: absolute; inset: 22%; border-radius: 50%; border: 1px dashed rgba(255,255,255,.18); pointer-events: none; }
.tpad-knob { position: absolute; left: 50%; top: 50%; width: 42%; height: 42%; margin: -21% 0 0 -21%; border-radius: 50%; pointer-events: none;
  background: rgba(47,211,230,.35); border: 2px solid var(--accent-2); transition: transform 30ms linear; }
.tbtns { position: relative; width: calc(var(--btn) * 2 + 26px); height: calc(var(--btn) * 1.55); }
.tb, .ta { position: absolute; width: var(--btn); height: var(--btn); border-radius: 50%; color: #fff; font-weight: 900; font-size: 24px;
  box-shadow: 0 4px 0 rgba(0,0,0,.45), inset 0 -3px 0 rgba(0,0,0,.25); }
.tb { left: 0; bottom: 0; background: #b0342a; }
.ta { right: 0; top: 0; background: #e2483c; }
.tmid { display: flex; flex-direction: column; align-items: center; gap: 10px; }
.tsel { display: flex; gap: 8px; }
.ts { width: 38px; height: 38px; border-radius: 50%; color: #fff; font-weight: 800; font-size: 15px; background: var(--panel-2); border: 2px solid var(--line) !important; }
.ts.c1 { border-color: #3d6cff !important; } .ts.c2 { border-color: #3ddc5a !important; }
.ts.c3 { border-color: #d64fd6 !important; } .ts.c4 { border-color: #f5d742 !important; }
.ts.coin { width: auto; height: 30px; border-radius: 15px; padding: 0 14px; font-size: 12px; letter-spacing: 1px; color: var(--accent); }
.touch .on { filter: brightness(1.45); transform: scale(.93); }
.tpad.on { filter: none; transform: none; }

body.touch-on .top, body.touch-on #game-help { display: none; }
body.touch-on #view-game { margin: 0; padding: 4px 4px 0; gap: 4px; }
body.touch-on .screen-label { display: none; }
body.touch-on .screens { padding: 0; gap: 4px; }
body.touch-on .hud { margin: 0 auto; padding: 4px 8px; gap: 6px; font-size: 12px; }
body.touch-on .hud .mono { font-size: 10px; }
body.touch-on .vol { display: none; }

/* orientation: body.port / body.land / body.compact are set by app.js (effective orientation,
   so the rotated pseudo-landscape mode uses the landscape rules too) */
body.compact { --pad: 134px; --btn: 62px; }
body.port.touch-on .screens { flex-direction: column; align-items: center; }
body.port.touch-on .touch { position: fixed; left: 0; right: 0; bottom: 0; height: calc(var(--touch-h) + env(safe-area-inset-bottom));
  display: flex; align-items: flex-end; justify-content: space-between;
  padding: 6px max(16px, env(safe-area-inset-right)) calc(8px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left)); }
body.port.touch-on .tmid { position: absolute; top: 4px; left: 50%; transform: translateX(-50%); flex-direction: row; margin: 0; }
body.port.touch-on .tpad { margin-bottom: 10px; }
body.port.touch-on .tbtns { margin-bottom: 18px; }
body.port.compact .ts { width: 32px; height: 32px; font-size: 13px; }
body.port.compact .tsel { gap: 5px; }

body.land.touch-on .touch { position: fixed; inset: 0; pointer-events: none; }
body.land.touch-on .tpad, body.land.touch-on .tbtns, body.land.touch-on .tmid { position: absolute; pointer-events: auto; }
body.land.touch-on .tpad { left: max(14px, env(safe-area-inset-left)); bottom: 18px; }
body.land.touch-on .tbtns { right: max(14px, env(safe-area-inset-right)); bottom: 22px; }
body.land.touch-on .tmid { left: max(10px, env(safe-area-inset-left)); top: 10px; align-items: flex-start; }
body.land.touch-on .hud { position: fixed; right: max(8px, env(safe-area-inset-right)); top: 6px; z-index: 6;
  flex-direction: column; align-items: stretch; width: calc(var(--side, 180px) - 16px); max-width: none;
  background: transparent; border: 0; padding: 0; gap: 5px; }
body.land.touch-on #hud-net, body.land.touch-on .hud .spacer, body.land.touch-on #btn-view, body.land.touch-on #btn-aspect { display: none; }
body.land.touch-on .hud .pill { text-align: center; }
body.land.touch-on #hud-warn:empty { display: none; }

/* pseudo landscape (iPhone etc. where orientation lock is unavailable): rotate the game view 90deg */
body.pseudo-land { overflow: hidden; }
body.pseudo-land #view-game { position: fixed; top: 0; left: 0; margin: 0; max-width: none; overflow: hidden; z-index: 20;
  width: var(--rot-w); height: var(--rot-h); background: var(--bg);
  transform-origin: 0 0; transform: translateX(var(--rot-x)) rotate(90deg); }

/* lobby on phones */
@media (max-width: 520px) {
  .view { margin: 10px auto; padding: 0 10px; }
  .card { padding: 14px; }
  #name { width: 100%; }
  .row.gap > .btn.primary { flex: 1; }
  .seg { display: flex; }
  .seg-btn { flex: 1; padding: 8px 6px; font-size: 13px; }
  .keys td { padding-right: 8px; font-size: 13px; }
}
.tpad, .tbtns, .tmid { flex-shrink: 0; }
body.touch-on .hud .btn.small { padding: 3px 8px; font-size: 12px; }
body.touch-on .pill { font-size: 12px; padding: 1px 8px; }

/* room list */
.room-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 10px; }
.room-card { background: var(--panel-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.room-card .rc-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.room-card .rc-title { font-weight: 700; }
.room-card .rc-state { font-size: 12px; padding: 2px 8px; border-radius: 99px; background: rgba(95,210,122,.15); color: var(--ok); white-space: nowrap; }
.room-card.playing .rc-state { background: rgba(255,100,100,.15); color: var(--danger); }
.room-card .rc-seats { font-size: 13px; color: var(--muted); }
.room-card .rc-seats b { color: var(--text); font-weight: 600; }
.room-card .btn { align-self: flex-start; }
.room-title { font-size: 22px; font-weight: 800; color: var(--accent); }
/* the touch layer only catches touches on its controls */
body.touch-on .touch { pointer-events: none; }
body.touch-on .tpad, body.touch-on .tbtns, body.touch-on .tmid { pointer-events: auto; }
