:root {
  --bg: #0b0c1a;
  --panel: rgba(22, 24, 48, .86);
  --panel-2: #22254a;
  --line: #34386a;
  --text: #ecebff;
  --muted: #9a9bc0;
  --accent: #ff5c8a;
  --accent-2: #6c7bff;
  --ok: #46d39a;
  --danger: #ff6b6b;
  --radius: 12px;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font-family: system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", sans-serif; }
.hidden { display: none !important; }
button, input { font: inherit; }
button { cursor: pointer; border: 1px solid var(--line); background: var(--panel-2); color: var(--text);
  padding: 8px 14px; border-radius: 10px; transition: filter .15s; -webkit-tap-highlight-color: transparent; }
button:hover { filter: brightness(1.15); }
button.primary { background: var(--accent); border-color: transparent; font-weight: 700; padding: 12px; width: 100%; font-size: 1.05rem; }
button.primary-sm { background: var(--accent); border-color: transparent; font-weight: 700; }
button.ghost { background: transparent; }
button.danger { background: var(--danger); color: #fff; border-color: transparent; }
button.small, .small { padding: 6px 10px; font-size: .85rem; }
svg.ic { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.pill svg.ic, .small svg.ic, .ghost svg.ic { width: 16px; height: 16px; }
.users .u svg.ic { width: 13px; height: 13px; }
button.icon, .icon { width: 42px; height: 42px; padding: 0; display: inline-grid; place-items: center; font-size: 1.15rem; border-radius: 12px; }
input[type="text"], input:not([type]), input[type="file"] { background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px; width: 100%; }
input:focus { outline: 2px solid var(--accent-2); outline-offset: 1px; }
.row { display: flex; gap: 8px; align-items: center; }
.row input { flex: 1; min-width: 0; }
.muted-text { color: var(--muted); }

/* ---- Lobby ---- */
.screen { min-height: 100%; }
#lobby { display: grid; place-items: center; padding: calc(16px + var(--sat)) 16px calc(16px + var(--sab)); }
.card { width: min(560px, 100%); background: #181a30; border: 1px solid var(--line); border-radius: 18px; padding: 24px; }
.card h1 { margin: 0 0 6px; font-size: 1.8rem; background: linear-gradient(90deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text; background-clip: text; color: transparent; }
.card h2 { font-size: 1rem; margin: 28px 0 8px; color: var(--muted); }
.lead { color: var(--muted); margin: 0 0 20px; }
.field { display: block; margin-bottom: 16px; border: 0; padding: 0; }
.field > span, .field legend { display: block; font-size: .85rem; color: var(--muted); margin-bottom: 6px; padding: 0; }
.field small { display: block; color: var(--muted); font-size: .75rem; margin-top: 6px; }
.radio { display: flex; align-items: center; gap: 8px; margin: 6px 0; flex-wrap: wrap; }
.radio input[type="file"], .radio input#vrmUrl { flex: 1; min-width: 160px; padding: 6px 10px; }
.check { display: flex; align-items: center; gap: 8px; margin-bottom: 18px; }
.status { min-height: 1.4em; margin-top: 10px; color: var(--muted); font-size: .9rem; }
.status.error { color: var(--danger); }
.roomlist { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; }
.roomlist li { background: var(--panel-2); border-radius: 10px; padding: 10px 12px; display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.roomlist li span { color: var(--muted); font-size: .85rem; }

/* ---- Room: full-screen world + HUD overlays ---- */
#room { position: fixed; inset: 0; overflow: hidden; background: radial-gradient(ellipse at 50% 30%, #1c1e44 0%, #0b0c1a 70%); }
.world { position: absolute; inset: 0; touch-action: none; user-select: none; -webkit-user-select: none; overflow: hidden; }
.screen-placeholder { width: 100%; height: 100%; display: grid; place-content: center; text-align: center; gap: 10px;
  background: linear-gradient(160deg, #1a1c3c, #0d0e22); color: #9a9bc0; font-size: 18px; }
.screen-placeholder .ph-logo { font-size: 44px; font-weight: 800; background: linear-gradient(90deg, #ff5c8a, #6c7bff);
  -webkit-background-clip: text; background-clip: text; color: transparent; }

.hud { position: absolute; z-index: 5; display: flex; gap: 8px; align-items: center; pointer-events: none; }
.hud > * { pointer-events: auto; }
.hud-tl { top: calc(10px + var(--sat)); left: calc(10px + var(--sal)); flex-wrap: wrap; }
.hud-tr { top: calc(10px + var(--sat)); right: calc(10px + var(--sar)); max-width: 50vw; }
.hud-tc { top: calc(10px + var(--sat)); left: 50%; transform: translateX(-50%); flex-direction: column; align-items: center; }
.hud-b { bottom: calc(12px + var(--sab)); left: 50%; transform: translateX(-50%); background: var(--panel); border: 1px solid var(--line);
  border-radius: 16px; padding: 8px; backdrop-filter: blur(10px); max-width: calc(100vw - 20px); flex-wrap: nowrap; justify-content: center; }
.pill { background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 6px 12px; display: flex; align-items: center; gap: 6px;
  backdrop-filter: blur(10px); font-size: .9rem; }
.pill .icon { width: 30px; height: 30px; font-size: .95rem; border-radius: 999px; }
.pill.btn { cursor: pointer; }
.brand { font-weight: 800; color: #fff; }
.users { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.users .u { background: var(--panel); backdrop-filter: blur(10px); border: 1px solid var(--line); border-radius: 999px; padding: 4px 10px; font-size: .8rem; display: flex; gap: 5px; align-items: center; white-space: nowrap; }
.users .u.host { border-color: var(--accent); }
.users .u .ic { opacity: .75; }
.popover { margin-top: 6px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 10px; width: min(360px, 92vw); display: grid; gap: 8px; backdrop-filter: blur(10px); }

.mic { background: var(--ok); color: #053; border-color: transparent; font-weight: 700; width: 48px; height: 42px; padding: 0; font-size: 1.2rem; border-radius: 12px; }
.mic.off { background: var(--danger); color: #fff; }
.emotes { display: flex; gap: 4px; }
.sit { background: var(--accent-2); color: #fff; border-color: transparent; font-weight: 700; height: 42px; padding: 0 12px; white-space: nowrap; display: inline-flex; align-items: center; gap: 6px; }
button.run-btn { position: absolute; z-index: 6; right: calc(20px + var(--sar)); bottom: calc(118px + var(--sab)); width: 56px; height: 56px; border-radius: 50%; background: rgba(255,255,255,.1); border: 2px solid rgba(255,255,255,.25); display: none; }
.run-btn.on { background: var(--accent); border-color: transparent; }
@media (pointer: coarse) { button.run-btn { display: inline-grid; } }
body.touch button.run-btn { display: inline-grid; }
.users .u .host-ic { color: #ffd166; }
.emotes button { font-size: 1.2rem; padding: 0; width: 40px; height: 42px; }
.file-btn { position: relative; overflow: hidden; background: var(--panel-2); border: 1px solid var(--line); cursor: pointer; }
.file-btn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
#watchBtn.on, #chatToggle.on { background: var(--accent-2); border-color: transparent; color: #fff; }

/* chat panel */
.chat { position: absolute; z-index: 6; right: calc(10px + var(--sar)); top: calc(64px + var(--sat)); bottom: calc(80px + var(--sab)); width: 300px;
  display: flex; flex-direction: column; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; backdrop-filter: blur(10px); }
.chat-head { display: flex; justify-content: space-between; align-items: center; padding: 6px 6px 6px 12px; border-bottom: 1px solid var(--line); font-size: .85rem; color: var(--muted); }
.chat-head .icon { width: 30px; height: 30px; font-size: .9rem; }
.chatlog { flex: 1; overflow-y: auto; padding: 10px; display: flex; flex-direction: column; gap: 6px; font-size: .9rem; overscroll-behavior: contain; }
.chatlog .m b { color: var(--accent-2); margin-right: 6px; }
.chatlog .sys { color: var(--muted); font-size: .8rem; }
.chat form { padding: 8px; border-top: 1px solid var(--line); }
.chat-peek { position: absolute; z-index: 4; left: calc(10px + var(--sal)); bottom: calc(80px + var(--sab)); max-width: min(70vw, 360px);
  display: flex; flex-direction: column; gap: 4px; pointer-events: none; font-size: .85rem; }
.chat-peek .m { background: rgba(10,11,30,.6); border-radius: 8px; padding: 4px 8px; animation: fadeout 8s forwards; }
.chat-peek .m b { color: var(--accent-2); margin-right: 5px; }
@keyframes fadeout { 0%, 75% { opacity: 1; } 100% { opacity: 0; } }

/* joystick */
.joystick { position: absolute; z-index: 6; left: calc(24px + var(--sal)); bottom: calc(96px + var(--sab)); width: 120px; height: 120px; border-radius: 50%;
  background: rgba(255,255,255,.08); border: 2px solid rgba(255,255,255,.25); touch-action: none; display: none; }
.joystick .knob { position: absolute; left: 50%; top: 50%; width: 52px; height: 52px; margin: -26px 0 0 -26px; border-radius: 50%;
  background: rgba(255,255,255,.55); box-shadow: 0 2px 8px rgba(0,0,0,.4); }
@media (pointer: coarse) { .joystick { display: block; } #hint { display: none; } }
body.touch .joystick { display: block; } body.touch #hint { display: none; }

.hint { position: absolute; z-index: 4; left: 50%; transform: translateX(-50%); bottom: calc(76px + var(--sab)); color: var(--muted); font-size: .75rem; pointer-events: none; }
.toast { position: fixed; left: 50%; bottom: calc(90px + var(--sab)); transform: translateX(-50%); background: var(--panel-2); border: 1px solid var(--line);
  padding: 10px 16px; border-radius: 10px; z-index: 20; box-shadow: 0 6px 24px rgba(0,0,0,.4); max-width: 90vw; }
.toast.error { border-color: var(--danger); }

/* ---- mobile ---- */
@media (max-width: 760px) {
  .hud-tl .brand { display: none; }
  .hud-tr { max-width: 46vw; }
  .hud-tc { top: calc(58px + var(--sat)); }
  .users .u { font-size: .72rem; padding: 3px 8px; }
  .chat { left: calc(8px + var(--sal)); right: calc(8px + var(--sar)); top: auto; bottom: calc(76px + var(--sab)); width: auto; height: 46vh; }
  .hud-b { gap: 6px; padding: 6px; flex-wrap: wrap; }
  .hud-b .icon, .emotes button, .mic, .hud-b .select { width: 36px; height: 36px; font-size: 1rem; }
  .hud-b .sit { height: 36px; padding: 0 8px; font-size: .85rem; }
  .hud-b { gap: 5px; row-gap: 6px; }
  .chat-peek { bottom: calc(240px + var(--sab)); left: calc(136px + var(--sal)); max-width: 55vw; }
  .joystick { bottom: calc(118px + var(--sab)); left: calc(16px + var(--sal)); width: 110px; height: 110px; }
}
@media (min-width: 761px) { #chatClose { display: none; } .chat-peek { display: none; } }
.chat:not(.open) { display: none; }

/* ---- lobby gallery / preview ---- */
.avatar-row { display: flex; gap: 12px; align-items: stretch; margin-bottom: 8px; }
.preview { width: 150px; height: 210px; flex: none; border-radius: 12px; background: radial-gradient(ellipse at 50% 30%, #2a2d5c, #15172b); border: 1px solid var(--line); }
.preview.loading { opacity: .5; }
.gallery-col { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 6px; }
.gallery { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.gcard { display: flex; align-items: center; gap: 6px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; cursor: pointer; font-size: .9rem; }
.gcard:has(input:checked) { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.credit { color: var(--muted); font-size: .72rem; line-height: 1.3; }

/* ---- HUD additions ---- */
.pill.points { gap: 4px; }
.pill.points b { color: #ffd166; }
.pill.points span { color: var(--muted); font-size: .8rem; }
.zoom-col { position: absolute; z-index: 6; right: calc(12px + var(--sar)); top: 50%; transform: translateY(-50%); display: none; flex-direction: column; gap: 8px; }
@media (pointer: coarse) { .zoom-col { display: flex; } }
body.touch .zoom-col { display: flex; }
.zoom-col .icon { width: 46px; height: 46px; border-radius: 50%; background: rgba(255,255,255,.1); border: 2px solid rgba(255,255,255,.25); }
#camBtn.on, #trackBtn.on { background: var(--accent-2); border-color: transparent; color: #fff; }
.select { appearance: none; -webkit-appearance: none; background: var(--panel-2) url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='%23ecebff' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><circle cx='12' cy='8' r='4'/><path d='M4 21a8 8 0 0 1 16 0'/></svg>") center/20px no-repeat; color: transparent; width: 42px; cursor: pointer; }
.select option { color: var(--text); background: var(--panel-2); }
.panel { position: absolute; z-index: 7; left: 50%; bottom: calc(76px + var(--sab)); transform: translateX(-50%); width: min(420px, 94vw); background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 10px; backdrop-filter: blur(10px); display: grid; gap: 10px; }
.panel-head { display: flex; justify-content: space-between; align-items: center; font-size: .9rem; color: var(--muted); padding-left: 4px; }
.panel-head .icon { width: 30px; height: 30px; }
.shop-list { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.shop-item { display: grid; place-items: center; gap: 4px; padding: 10px 6px; border-radius: 12px; }
.shop-item .big { width: 28px; height: 28px; color: var(--accent); }
.shop-item .nm { font-size: .8rem; }
.shop-item .pr { font-size: .75rem; color: #ffd166; font-weight: 700; }
.track-video { position: absolute; z-index: 6; right: calc(10px + var(--sar)); bottom: calc(80px + var(--sab)); width: 120px; height: 90px; object-fit: cover; border-radius: 10px; border: 1px solid var(--line); transform: scaleX(-1); background: #000; }
@media (max-width: 760px) {
  .avatar-row { flex-direction: column; }
  .preview { width: 100%; height: 180px; }
  .shop-list { grid-template-columns: repeat(2, 1fr); }
  .track-video { width: 90px; height: 68px; bottom: calc(190px + var(--sab)); }
  .pill.points { font-size: .8rem; }
  .hud-b { max-width: calc(100vw - 16px); }
}
