/* Valen UI: warm, quiet glass panels over the 3D apartment. One accent (mahjong gold), one alert (coral). */
:root {
  --ink: #f4f1ea; --ink-dim: #b9b4aa; --ink-faint: #8a857c;
  --glass: rgba(22, 24, 31, .72); --glass-hi: rgba(36, 39, 49, .86); --line: rgba(255, 255, 255, .09);
  --gold: #ffc83d; --gold-deep: #e8a21a; --coral: #ff7a59; --jade: #3fbf8a; --violet: #9b6bff; --sky: #3fb6ff;
  --r: 14px; --shadow: 0 10px 30px rgba(0, 0, 0, .28);
  --font: ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --han: "PingFang SC", "Hiragino Sans GB", "Noto Sans SC", "Microsoft YaHei", sans-serif;
  --safe-b: env(safe-area-inset-bottom, 0px); --safe-t: env(safe-area-inset-top, 0px);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #9cc9ef; color: var(--ink); font: 14px/1.45 var(--font); -webkit-font-smoothing: antialiased; }
canvas { touch-action: none; display: block; }
button { font: inherit; color: inherit; border: 0; border-radius: 10px; padding: 8px 14px; cursor: pointer; background: rgba(255, 255, 255, .1); font-weight: 600; transition: background .15s, transform .12s, opacity .15s; }
button:hover:not(:disabled) { background: rgba(255, 255, 255, .18); }
button:active:not(:disabled) { transform: scale(.96); }
button:disabled { opacity: .3; cursor: default; }
button.primary { background: linear-gradient(#ffd45e, var(--gold-deep)); color: #3a2400; box-shadow: 0 2px 0 #b97a00; }
button.primary:hover:not(:disabled) { background: linear-gradient(#ffdf7e, #f0ab25); }
button.icon { width: 38px; height: 38px; padding: 0; border-radius: 50%; font-size: 17px; background: var(--glass); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); box-shadow: var(--shadow); }
button.link { background: none; padding: 0; color: var(--ink-dim); text-decoration: underline; font-weight: 500; }
kbd { font: 600 11px/1 var(--font); padding: 2px 5px; border-radius: 5px; background: rgba(255, 255, 255, .14); color: var(--ink); border-bottom: 1px solid rgba(0, 0, 0, .4); }
input, select { font: inherit; color: var(--ink); background: rgba(255, 255, 255, .08); border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px; outline: none; }
input:focus, select:focus { border-color: var(--gold); }
select option { color: #222; }
[hidden] { display: none !important; }
.glass { background: var(--glass); backdrop-filter: blur(12px) saturate(1.2); -webkit-backdrop-filter: blur(12px) saturate(1.2); border: 1px solid var(--line); border-radius: var(--r); box-shadow: var(--shadow); }

/* ---------- lobby */
#join { position: fixed; inset: 0; z-index: 30; display: flex; align-items: center; justify-content: center; padding: 16px;
  background: radial-gradient(1200px 700px at 30% 20%, rgba(255, 200, 61, .18), transparent 60%), linear-gradient(160deg, rgba(20, 24, 40, .78), rgba(40, 22, 30, .82)); }
.lobby { width: min(440px, 100%); max-height: 100%; overflow: auto; padding: 26px 26px 18px; }
.lobby h1 { margin: 0; font-size: 40px; letter-spacing: -.5px; }
.lobby .tag { margin: 4px 0 18px; color: var(--ink-dim); }
.lobby label { display: block; font-size: 12px; color: var(--ink-dim); text-transform: uppercase; letter-spacing: .08em; }
.lobby label input { display: block; width: 100%; margin-top: 6px; font-size: 17px; }
#swatches { margin: 12px 0 16px; display: flex; gap: 8px; flex-wrap: wrap; }
.sw { width: 30px; height: 30px; border-radius: 50%; cursor: pointer; border: 3px solid transparent; box-shadow: inset 0 -3px 0 rgba(0, 0, 0, .25); transition: transform .12s; }
.sw:hover { transform: scale(1.1); } .sw.on { border-color: var(--ink); }
.lobby .row { display: flex; gap: 8px; margin-top: 8px; }
.lobby .row > * { flex: 1; }
#go { font-size: 17px; padding: 12px; }
.lobby .or { text-align: center; color: var(--ink-faint); font-size: 12px; margin: 14px 0 6px; }
#roomCode { text-transform: uppercase; letter-spacing: .15em; flex: 1.2; }
#roomNote { margin-top: 10px; font-size: 13px; color: var(--gold); min-height: 1em; }
#rooms { margin-top: 6px; display: grid; gap: 6px; }
.roomRow { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 10px; background: rgba(255, 255, 255, .05); cursor: pointer; }
.roomRow:hover { background: rgba(255, 255, 255, .1); }
.roomRow .n { flex: 1; font-weight: 600; } .roomRow .m { font-size: 12px; color: var(--ink-dim); }
.lobby h3 { margin: 18px 0 0; font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--ink-dim); }
.lobby footer { margin-top: 18px; font-size: 12px; color: var(--ink-faint); display: flex; gap: 6px; justify-content: center; }
.lobby footer a { color: var(--ink-dim); }

/* ---------- top bar */
#topbar { position: fixed; top: calc(10px + var(--safe-t)); left: 10px; right: 10px; z-index: 12; display: flex; align-items: center; gap: 8px; pointer-events: none; }
#topbar > * { pointer-events: auto; }
#topbar .spacer { flex: 1; pointer-events: none; }
#roomBadge { display: flex; align-items: center; gap: 8px; padding: 6px 8px 6px 12px; font-size: 13px; }
#roomBadge b { font-weight: 700; } #roomBadge .code { font: 700 12px/1 var(--font); letter-spacing: .15em; padding: 5px 7px; border-radius: 7px; background: rgba(255, 255, 255, .1); }
#roomBadge button { padding: 5px 9px; font-size: 12px; }
#players { position: fixed; top: calc(58px + var(--safe-t)); right: 10px; z-index: 11; padding: 8px 12px; font-size: 12.5px; min-width: 150px; max-width: 220px; }
#players .h { font-size: 11px; color: var(--ink-dim); text-transform: uppercase; letter-spacing: .08em; margin-bottom: 4px; }
#players .p { display: flex; align-items: center; gap: 7px; padding: 2px 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#players .dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
#players .you { color: var(--ink-dim); font-size: 11px; }
body.seated #players { display: none; }

/* ---------- prompt, connection, toasts */
#prompt { position: fixed; left: 50%; top: 62%; transform: translateX(-50%); z-index: 12; padding: 7px 16px; border-radius: 20px; background: rgba(0, 0, 0, .62); color: #fff; display: none; font-weight: 600; }
#conn { position: fixed; top: calc(60px + var(--safe-t)); left: 50%; transform: translateX(-50%); z-index: 25; padding: 8px 16px; border-radius: 20px; background: rgba(192, 32, 30, .9); color: #fff; font-weight: 700; box-shadow: var(--shadow); }
#chat { position: fixed; left: 50%; top: calc(64px + var(--safe-t)); transform: translateX(-50%); width: min(380px, 90vw); z-index: 14; display: none; background: var(--glass-hi); }

/* ---------- table panel (seated) */
#mj { position: fixed; left: 10px; bottom: calc(10px + var(--safe-b)); z-index: 12; width: 300px; padding: 10px 12px; display: none; font-size: 12.5px; }
#status { font-size: 12.5px; color: var(--ink-dim); } #status b { color: var(--ink); }
#msg { font-weight: 600; margin: 4px 0; min-height: 1.4em; }
#result { white-space: pre-line; font-size: 12.5px; color: var(--gold); }
#hover { font-size: 12px; color: var(--ink-dim); min-height: 16px; }
#actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
#actions button { padding: 7px 11px; font-size: 13px; white-space: nowrap; }
#actions button:disabled { display: none; }
#actions #bStart:not(:disabled) { background: linear-gradient(#ffd45e, var(--gold-deep)); color: #3a2400; }
#actions #bWin:not(:disabled) { background: linear-gradient(#ffd85a, #ff9a2e); color: #5a1d00; }
#claims { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
#claims:empty { display: none; }
#claims button { padding: 7px 11px; font-size: 13px; color: #fff; }
#claimTimer { flex-basis: 100%; height: 5px; border-radius: 3px; background: var(--coral); }
#mjTools { display: flex; gap: 6px; align-items: center; margin-top: 8px; padding-top: 8px; border-top: 1px solid var(--line); font-size: 12px; color: var(--ink-dim); }
#mjTools button { padding: 5px 9px; font-size: 12px; font-weight: 500; white-space: nowrap; }
#mjTools label { display: flex; align-items: center; gap: 5px; white-space: nowrap; }
#mjTools select { padding: 4px 6px; font-size: 12px; }
#mjTools .spacer { flex: 1; }
#huBig { position: fixed; left: 50%; bottom: 30%; transform: translateX(-50%); z-index: 15; font: 900 44px var(--han); padding: 14px 40px; border-radius: 40px; background: linear-gradient(#ffd85a, #ff9a2e); color: #5a1d00; box-shadow: 0 0 0 4px #fff, 0 0 40px rgba(255, 200, 61, .8); animation: huPulse 1.2s ease-in-out infinite; display: none; }
#huBig small { font-size: 16px; }
@keyframes huPulse { 0%, 100% { transform: translateX(-50%) scale(1); } 50% { transform: translateX(-50%) scale(1.06); } }

/* ---------- panels (help, scoreboard, settings) */
.panel { position: fixed; z-index: 26; left: 50%; top: 50%; transform: translate(-50%, -50%); width: min(560px, calc(100vw - 24px)); max-height: calc(100vh - 40px); overflow: auto; padding: 18px 20px; background: var(--glass-hi); }
.panel h2 { margin: 0 0 10px; font-size: 18px; display: flex; align-items: center; gap: 8px; }
.panel h2 .x { margin-left: auto; width: 30px; height: 30px; padding: 0; border-radius: 50%; }
.panel h3 { margin: 16px 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--ink-dim); }
.keys { display: grid; grid-template-columns: auto 1fr; gap: 5px 12px; font-size: 13px; }
.keys dt { text-align: right; white-space: nowrap; } .keys dd { margin: 0; color: var(--ink-dim); }
.credits { font-size: 12px; color: var(--ink-faint); } .credits a { color: var(--ink-dim); }
.set { display: flex; align-items: center; gap: 10px; padding: 7px 0; border-bottom: 1px solid var(--line); }
.set > span { flex: 1; } .set small { display: block; color: var(--ink-faint); font-size: 11.5px; }
.set input[type=range] { width: 160px; accent-color: var(--gold); padding: 0; }
.set input[type=checkbox] { width: 20px; height: 20px; accent-color: var(--gold); }
table.score { width: 100%; border-collapse: collapse; font-size: 13px; }
table.score td, table.score th { padding: 5px 6px; text-align: left; border-bottom: 1px solid var(--line); }
table.score th { font-size: 11px; color: var(--ink-dim); text-transform: uppercase; letter-spacing: .06em; font-weight: 600; }
table.score .num { text-align: right; font-variant-numeric: tabular-nums; }
.pos { color: var(--jade); } .neg { color: var(--coral); }
.hist { font-size: 12.5px; display: grid; gap: 6px; }
.hist .e { padding: 7px 9px; border-radius: 9px; background: rgba(255, 255, 255, .05); }
.hist .e .t { color: var(--ink-dim); font-size: 11.5px; }
#scrim { position: fixed; inset: 0; z-index: 25; background: rgba(8, 10, 16, .35); }

/* ---------- tutorial */
#tutorial { position: fixed; z-index: 27; left: 50%; bottom: calc(24px + var(--safe-b)); transform: translateX(-50%); width: min(420px, calc(100vw - 24px)); padding: 14px 16px; background: var(--glass-hi); }
#tutorial .step { font-size: 11px; color: var(--gold); text-transform: uppercase; letter-spacing: .1em; }
#tutorial p { margin: 6px 0 10px; font-size: 14.5px; }
#tutorial .row { display: flex; gap: 8px; justify-content: flex-end; }

/* ---------- touch controls */
#touch { position: fixed; inset: auto 0 0 0; height: 0; z-index: 13; }
#stick { position: fixed; left: calc(18px + env(safe-area-inset-left, 0px)); bottom: calc(18px + var(--safe-b)); width: 124px; height: 124px; border-radius: 50%; background: rgba(255, 255, 255, .12); border: 1px solid rgba(255, 255, 255, .2); touch-action: none; }
#stick .nub { position: absolute; left: 50%; top: 50%; width: 52px; height: 52px; margin: -26px; border-radius: 50%; background: rgba(255, 255, 255, .55); box-shadow: 0 2px 8px rgba(0, 0, 0, .3); }
#touchBtns { position: fixed; right: calc(14px + env(safe-area-inset-right, 0px)); bottom: calc(22px + var(--safe-b)); display: grid; grid-template-columns: repeat(2, 58px); gap: 10px; }
#touchBtns button { width: 58px; height: 58px; border-radius: 50%; padding: 0; background: rgba(22, 24, 31, .6); font-size: 13px; backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px); }
body.seated #stick { display: none; }
@media (pointer: coarse), (max-width: 700px) {
  #mj { left: 8px; right: 8px; width: auto; bottom: calc(8px + var(--safe-b)); }
  body.touch.seated #mj { bottom: calc(8px + var(--safe-b)); }
  #players { display: none; }
  .lobby { padding: 20px 18px 14px; }
}
body.touch:not(.seated) #mj { display: none !important; }

/* ---------- reduced motion / flashing */
body.calm *, body.calm *::before, body.calm *::after { animation-duration: .001s !important; animation-iteration-count: 1 !important; }

/* ---------- quiet notes (bites, small events) */
#notes { position: fixed; left: 50%; top: calc(88px + var(--safe-t)); transform: translateX(-50%); z-index: 16; display: grid; gap: 6px; justify-items: center; pointer-events: none; }
.note { padding: 6px 14px; font-size: 13px; font-weight: 600; border-radius: 20px; transition: opacity .5s, transform .5s; }
.note.out { opacity: 0; transform: translateY(-6px); }

/* ---------- your-turn frame (glow on the screen edge + a pill) */
#turnFrame { position: fixed; inset: 0; pointer-events: none; border: 6px solid var(--gold); box-shadow: inset 0 0 120px 20px rgba(255, 200, 60, .6); opacity: 0; transition: opacity .3s; z-index: 4; display: flex; justify-content: center; align-items: flex-start; padding-top: calc(14px + var(--safe-t)); font: 900 28px var(--font); color: #3a2600; letter-spacing: .5px; }
#turnFrame span { background: var(--gold); padding: 6px 22px; border-radius: 30px; box-shadow: 0 4px 14px rgba(0, 0, 0, .35); white-space: nowrap; }
@media (max-width: 700px) { #turnFrame { font-size: 15px; padding-top: calc(62px + var(--safe-t)); border-width: 4px; } #turnFrame span { padding: 5px 14px; } }
body.seated #touchBtns [data-when=walk] { display: none; }
body.seated #touchBtns { grid-template-columns: 58px; bottom: auto; top: calc(110px + var(--safe-t)); }

body.touch kbd { display: none; }
