:root {
  color-scheme: dark;
  font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  --cream: #f2edcf;
  --deep: #090b1d;
  --glass: rgba(9, 11, 29, 0.9);
}

* { box-sizing: border-box; }
html, body { width: 100%; height: 100%; margin: 0; overflow: hidden; background: var(--deep); }
body { color: var(--cream); }
button, input { font: inherit; }
button { color: inherit; }

#world {
  position: fixed; inset: 0; width: 100%; height: 100%; display: block;
  image-rendering: auto; touch-action: none;
}
#world.direct-control-ready { cursor: grab; }
#world.direct-control-active { cursor: grabbing; }
#labels {
  position: fixed; z-index: 4; inset: 0; width: 100%; height: 100%; display: block;
  pointer-events: none;
}

.hud {
  position: fixed; z-index: 10; top: 0; left: 0; right: 0;
  display: flex; align-items: center; justify-content: space-between; padding: 15px 18px;
  pointer-events: none;
}
.hud > * { pointer-events: auto; }
.brand { color: var(--cream); text-decoration: none; font-family: Georgia, serif; font-size: 18px; text-shadow: 2px 2px 0 rgba(5,6,17,.5); }
.brand span { display: inline-grid; place-items: center; width: 28px; height: 28px; margin-right: 7px; border: 1px solid rgba(255,255,255,.28); border-radius: 50%; background: rgba(16,44,40,.5); }
.hud-actions { display: flex; align-items: center; gap: 7px; }
.presence, .icon-button {
  height: 36px; border: 1px solid rgba(255,255,255,.18); border-radius: 9px;
  background: rgba(9,11,29,.76); box-shadow: 3px 3px 0 rgba(5,6,17,.3); backdrop-filter: blur(8px);
}
.presence { display: flex; align-items: center; gap: 6px; padding: 0 11px; font-size: 10px; text-transform: uppercase; letter-spacing: .08em; }
.presence i { width: 6px; height: 6px; border-radius: 1px; background: #ce765f; }
.presence i.online { background: #b9d66e; box-shadow: 0 0 8px #b9d66e; }
.icon-button { width: 36px; padding: 0; cursor: pointer; font-size: 17px; }
.icon-button:hover, .icon-button.active { background: rgba(46,79,67,.9); }
.icon-button.muted { opacity: .48; text-decoration: line-through; }
.mobile-zoom {
  position: fixed; z-index: 12; top: calc(58px + env(safe-area-inset-top)); right: max(11px, env(safe-area-inset-right));
  display: none; align-items: center; gap: 5px; padding: 5px;
  border: 1px solid rgba(255,255,255,.18); border-radius: 10px;
  background: rgba(9,11,29,.8); box-shadow: 3px 3px 0 rgba(5,6,17,.24); backdrop-filter: blur(7px);
}
.mobile-zoom button {
  width: 38px; height: 34px; padding: 0; border: 1px solid rgba(255,255,255,.2); border-radius: 7px;
  background: rgba(35,34,59,.92); color: var(--cream); font-size: 21px; font-weight: 900; line-height: 1;
}
.mobile-zoom button:active { transform: translate(1px,1px); background: rgba(74,100,65,.92); }
.mobile-zoom button:disabled { opacity: .28; }
.mobile-zoom span { padding: 0 2px; color: rgba(242,237,207,.62); font-size: 7px; font-weight: 900; letter-spacing: .08em; }
.record-button { color: rgba(237,141,164,.62); }
.record-button[data-status="armed"],
.record-button[data-status="recording"],
.record-button[data-status="stopping"] {
  border-color: rgba(237,141,164,.9); color: #ff9bb0; background: rgba(67,27,45,.94);
  box-shadow: 0 0 0 2px rgba(237,141,164,.18), 0 0 16px rgba(237,141,164,.36);
}
.record-button[data-status="recording"] { animation: rec-pulse 1s steps(2) infinite; }
.record-button[data-status="armed"] { animation: rec-pulse .48s steps(2) infinite; }
.record-button[data-status="stopping"] { animation: rec-pulse .28s steps(2) infinite; }
.download-recording { color: #b8f2cb; }
.download-recording[hidden] { display: none; }
@keyframes rec-pulse { 50% { opacity: .46; } }

.welcome {
  position: fixed; z-index: 20; inset: 0; display: grid; place-items: center; padding: 18px;
  background: rgba(5,6,17,.36); backdrop-filter: blur(4px);
  transition: opacity .25s steps(4), visibility .25s;
}
.welcome.dismissed { opacity: 0; visibility: hidden; }
.creator-card {
  width: min(380px, 100%); padding: 26px; border: 2px solid rgba(255,242,199,.42); border-radius: 4px;
  background: var(--glass); box-shadow: 9px 9px 0 rgba(7,27,24,.34);
}
.creator-copy { display: flex; align-items: baseline; justify-content: space-between; gap: 14px; }
.creator-copy p { margin: 0; color: #b8f2cb; font-size: 9px; font-weight: 900; letter-spacing: .16em; text-transform: uppercase; }
h1 { margin: 0; font: 25px/1 Georgia, serif; letter-spacing: -.025em; }
#avatarPreview { display: block; width: 136px; height: 136px; margin: 12px auto 10px; image-rendering: pixelated; }
.name-field span, legend { display: block; margin-bottom: 6px; color: rgba(255,242,199,.58); font-size: 9px; font-weight: 900; letter-spacing: .12em; text-transform: uppercase; }
#nameInput { width: 100%; height: 42px; padding: 0 12px; border: 1px solid rgba(255,255,255,.16); border-radius: 3px; outline: 0; background: rgba(5,25,23,.46); color: white; }
#nameInput:focus { border-color: #b8f2cb; }
fieldset { margin: 15px 0 0; padding: 0; border: 0; }
.coat-picker, .path-picker { display: grid; gap: 6px; }
.coat-picker { grid-template-columns: repeat(4, 1fr); }
.coat-picker legend, .path-picker legend { grid-column: 1 / -1; }
.coat-picker input, .path-picker input { position: absolute; opacity: 0; pointer-events: none; }
.swatch span, .path-picker label span { display: flex; align-items: center; justify-content: center; min-height: 35px; border: 1px solid rgba(255,255,255,.12); border-radius: 3px; cursor: pointer; font-size: 9px; }
.swatch span::before { content: ""; width: 8px; height: 8px; margin-right: 5px; background: #617f49; box-shadow: 1px 1px 0 #253b2d; }
.swatch.berry span::before { background: #ad4d50; }.swatch.sky span::before { background: #4188ae; }.swatch.sun span::before { background: #d79b24; }
.swatch input:checked + span, .path-picker input:checked + span { border-color: #eaf0b5; background: rgba(234,240,181,.11); color: white; }
.path-picker { grid-template-columns: repeat(3, 1fr); }
.path-picker label span { flex-direction: column; gap: 3px; min-height: 48px; }
.path-picker b { color: #b8f2cb; font-size: 15px; line-height: 1; }
.play-button { width: 100%; display: flex; justify-content: space-between; margin-top: 18px; padding: 13px 15px; border: 0; border-radius: 3px; cursor: pointer; background: #e7edaf; color: #173a34; font-weight: 900; box-shadow: 3px 3px 0 #759461; }
.play-button:hover { transform: translate(-1px,-1px); box-shadow: 4px 4px 0 #759461; }
.creator-card > small { display: block; margin-top: 12px; text-align: center; color: rgba(255,242,199,.38); font-size: 8px; }

.chat-panel { position: fixed; z-index: 13; left: 18px; bottom: 18px; width: min(300px, calc(100% - 36px)); display: flex; flex-direction: column; align-items: flex-start; gap: 6px; pointer-events: none; }
.chat-panel[hidden] { display: none; }
.chat-toggle { height: 30px; padding: 0 10px; border: 1px solid rgba(255,255,255,.2); border-radius: 4px; background: rgba(9,11,29,.92); box-shadow: 3px 3px 0 rgba(5,6,17,.34); cursor: pointer; font-size: 9px; font-weight: 900; letter-spacing: .1em; pointer-events: auto; }
.chat-toggle span { color: #b8f2cb; font-size: 8px; }
.chat-toggle em { min-width: 17px; height: 17px; display: inline-grid; place-items: center; margin-left: 5px; padding: 0 4px; border-radius: 9px; background: #ed8da4; color: #090b1d; font: 900 8px/1 ui-monospace, monospace; }
.chat-toggle em[hidden] { display: none; }
.chat-toggle.has-unread { border-color: rgba(237,141,164,.72); }
.chat-toggle[aria-expanded="true"] { border-color: rgba(184,242,203,.72); background: rgba(35,34,59,.96); }
.chat-log { width: 100%; max-height: 118px; overflow: auto; margin: 0; padding: 7px 8px; border: 1px solid rgba(255,255,255,.16); border-radius: 4px; background: rgba(9,11,29,.8); box-shadow: 3px 3px 0 rgba(5,6,17,.28); list-style: none; scrollbar-width: thin; pointer-events: auto; }
.chat-log[hidden] { display: none; }
.chat-log:empty::before { content: "Room chat appears here."; color: rgba(242,237,207,.42); font-size: 8px; }
.chat-log li { display: grid; grid-template-columns: minmax(52px, auto) 1fr; gap: 7px; padding: 3px 0; font-size: 9px; line-height: 1.35; }
.chat-log strong { max-width: 90px; overflow: hidden; color: #b8f2cb; text-overflow: ellipsis; white-space: nowrap; }
.chat-log span { min-width: 0; overflow-wrap: anywhere; color: rgba(242,237,207,.88); }
.chat { width: 100%; display: flex; padding: 5px; border: 1px solid rgba(255,255,255,.2); border-radius: 4px; background: rgba(9,11,29,.96); box-shadow: 5px 5px 0 rgba(5,6,17,.34); pointer-events: auto; }
.chat[hidden] { display: none; }
.chat input { min-width: 0; flex: 1; padding: 9px 10px; border: 0; outline: 0; background: transparent; color: white; }
.chat button { width: 36px; border: 0; border-radius: 2px; background: #e7edaf; color: #173a34; cursor: pointer; font-weight: 900; }
.pattern-dialog {
  position: fixed; z-index: 24; inset: 0; display: flex; align-items: flex-end; justify-content: center;
  padding: 18px; background: linear-gradient(180deg, transparent 20%, rgba(5,6,17,.42));
}
.pattern-dialog[hidden] { display: none; }
.pattern-dialog-box {
  width: min(760px, 100%); max-height: min(560px, calc(100vh - 72px)); overflow: auto;
  padding: 13px; border: 4px solid #f2edcf; outline: 3px solid #25253b; border-radius: 3px;
  background: rgba(9,11,29,.97); box-shadow: 9px 9px 0 rgba(5,6,17,.55);
}
.pattern-speaker { display: grid; grid-template-columns: 66px 1fr; gap: 13px; align-items: center; min-height: 72px; padding: 9px; border: 2px solid rgba(184,242,203,.32); background: rgba(35,34,59,.66); }
.pattern-speaker small { display: block; color: #9ecfea; font-size: 8px; font-weight: 900; letter-spacing: .12em; text-transform: uppercase; }
.pattern-speaker strong { display: block; margin-top: 2px; color: #fff; font: 800 17px/1 Georgia, serif; }
.pattern-speaker p { margin: 7px 0 0; color: var(--cream); font-size: 10px; line-height: 1.4; }
.pattern-portrait { position: relative; width: 58px; height: 58px; display: block; border: 2px solid #9ecfea; background: #17192f; image-rendering: pixelated; }
.pattern-portrait::before { content: ""; position: absolute; left: 17px; top: 8px; width: 24px; height: 23px; background: #c9a27d; box-shadow: -5px 5px 0 #4a4962, 5px 5px 0 #4a4962, 0 23px 0 #59765d, -8px 31px 0 #59765d, 8px 31px 0 #59765d; }
.pattern-portrait::after { content: ""; position: absolute; left: 22px; top: 16px; width: 4px; height: 4px; background: #090b1d; box-shadow: 10px 0 0 #090b1d, 5px 9px 0 #f2edcf; }
.pattern-slots { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 9px; }
.pattern-slot { min-width: 0; padding: 10px; border: 2px solid rgba(158,207,234,.28); background: rgba(23,25,47,.88); }
.pattern-slot h3 { margin: 0; color: #b8f2cb; font-size: 10px; letter-spacing: .08em; }
.pattern-slot p { min-height: 30px; margin: 6px 0 9px; color: rgba(242,237,207,.62); font-size: 8px; line-height: 1.4; }
.pattern-slot-buttons { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.pattern-option { min-height: 35px; padding: 7px 8px; border: 1px solid rgba(255,255,255,.2); border-radius: 2px; cursor: pointer; background: #25253b; color: var(--cream); font-size: 9px; font-weight: 900; }
.pattern-option:hover, .pattern-option:focus-visible, .pattern-option.confirming { outline: 2px solid #f2edcf; outline-offset: 1px; background: #38514b; }
.pattern-option:disabled { cursor: default; opacity: .28; }
.pattern-dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 9px; }
.pattern-clear { margin-right: auto; color: #ffb1bd; }
.pattern-dialog-hint { display: block; margin-top: 8px; color: rgba(242,237,207,.42); text-align: right; font-size: 7px; }
body.pattern-menu-open .touch-controls { opacity: .16; pointer-events: none; }
body.pattern-menu-open .touch-controls button { pointer-events: none; }
.touch-controls {
  position: fixed; z-index: 11; inset: 0;
  display: none;
  pointer-events: none; touch-action: none; user-select: none; -webkit-user-select: none;
}
.touch-controls button {
  display: inline-flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; padding: 0; border: 1px solid rgba(255,255,255,.24);
  background: rgba(12,39,36,.76); color: #fff2c7; box-shadow: 3px 3px 0 rgba(6,23,21,.24);
  font-size: 20px; line-height: 1; backdrop-filter: blur(6px); touch-action: none; pointer-events: auto;
}
.touch-controls button small { font-size: 6px; font-weight: 900; letter-spacing: .08em; }
.touch-dpad {
  position: absolute; left: max(16px, env(safe-area-inset-left)); bottom: calc(18px + env(safe-area-inset-bottom));
  width: 164px; height: 164px; display: grid; grid-template: repeat(3, 1fr) / repeat(3, 1fr);
  align-items: stretch; justify-items: stretch; pointer-events: none;
}
.touch-controls button.dpad-button {
  width: auto; height: auto; border-radius: 13px; border-color: rgba(255,255,255,.14);
  background: rgba(12,39,36,.48); box-shadow: none; color: rgba(255,242,199,.82); font-size: 24px;
}
.dpad-up { grid-area: 1 / 2; }
.dpad-left { grid-area: 2 / 1; }
.dpad-right { grid-area: 2 / 3; }
.dpad-down { grid-area: 3 / 2; }
.dpad-hub {
  grid-area: 2 / 2; display: grid; place-items: center; border: 1px solid rgba(255,255,255,.08);
  border-radius: 13px; background: rgba(9,11,29,.36); color: rgba(158,207,234,.38); font-size: 13px;
}
.touch-action-cluster {
  position: absolute; right: max(16px, env(safe-area-inset-right)); bottom: calc(20px + env(safe-area-inset-bottom));
  width: 160px; height: 136px; display: grid; grid-template: 1fr 1fr / 1fr 1fr;
  gap: 10px; transform: rotate(-7deg); pointer-events: none;
}
.touch-controls button.touch-face {
  width: 68px; height: 60px; justify-self: center; border-radius: 50%; font-size: 16px;
  transform: rotate(7deg); box-shadow: inset 0 -4px 0 rgba(5,6,17,.28), 3px 3px 0 rgba(5,6,17,.2);
}
.touch-face b { font-size: 18px; line-height: 1; }
.touch-controls button.touch-face-a { border-color: rgba(231,237,175,.74); background: rgba(74,100,65,.84); }
.touch-controls button.touch-face-b { border-color: rgba(220,120,128,.62); background: rgba(70,35,46,.84); }
.touch-controls button.touch-face-c { border-color: rgba(158,207,234,.66); background: rgba(31,43,68,.86); }
.touch-controls button.touch-face-d { border-color: rgba(182,155,219,.7); background: rgba(47,37,71,.86); }
.touch-controls button.touch-dash { position: relative; border-color: rgba(158,207,234,.58); background: rgba(31,43,68,.82); }
.touch-controls button.touch-dash::after {
  content: attr(data-charges); position: absolute; top: -6px; right: -6px;
  min-width: 16px; height: 16px; display: grid; place-items: center; padding: 0 2px;
  border: 1px solid rgba(255,255,255,.44); border-radius: 9px;
  background: rgba(158,207,234,.92); color: #090b1d; font: 900 9px/1 monospace;
}
.touch-controls button.dpad-button.pressed, .touch-controls button.dpad-button:active { transform: translate(2px,2px); background: rgba(115,143,87,.86); }
.touch-controls button.touch-face.pressed, .touch-controls button.touch-face:active { transform: rotate(7deg) translate(2px,2px); filter: brightness(1.25); box-shadow: inset 0 -1px 0 rgba(5,6,17,.2); }
.hint { position: fixed; z-index: 9; left: 50%; bottom: 18px; transform: translateX(-50%); margin: 0; padding: 7px 10px; border-radius: 3px; background: rgba(12,35,32,.56); color: rgba(255,242,199,.7); font-size: 8px; white-space: nowrap; transition: opacity .6s; }
.hint.faded { opacity: 0; }
.toast { position: fixed; z-index: 30; left: 50%; top: 64px; transform: translate(-50%,-6px); padding: 8px 11px; border: 2px solid var(--toast-accent, rgba(255,255,255,.17)); border-radius: 3px; background: rgba(10,31,29,.96); box-shadow: 0 0 15px color-mix(in srgb, var(--toast-accent, transparent) 24%, transparent); font-size: 9px; opacity: 0; pointer-events: none; transition: .16s steps(2); }
.toast.show { opacity: 1; transform: translate(-50%,0); }

@media (max-width: 520px) {
  .hud { padding: 11px; }
  .brand { font-size: 15px; }
  .brand span { width: 25px; height: 25px; }
  .presence { display: none; }
  .hud-actions { gap: 4px; }
  .icon-button { width: 32px; height: 32px; font-size: 15px; }
  .creator-card { padding: 21px; }
  .creator-copy { display: block; }
  .creator-copy p { margin-bottom: 6px; }
  .hint { bottom: 12px; max-width: calc(100% - 24px); overflow: hidden; text-overflow: ellipsis; }
  .pattern-dialog { padding: 8px; }
  .pattern-dialog-box { max-height: calc(100vh - 60px); padding: 9px; }
  .pattern-speaker { grid-template-columns: 52px 1fr; min-height: 60px; padding: 7px; }
  .pattern-portrait { width: 46px; height: 46px; transform: scale(.78); transform-origin: left top; }
  .pattern-slots { grid-template-columns: 1fr; }
  .pattern-slot { display: grid; grid-template-columns: minmax(80px, 1fr) minmax(130px, 1.25fr); align-items: center; gap: 7px; padding: 7px; }
  .pattern-slot h3 { grid-column: 1; }
  .pattern-slot p { grid-column: 1; min-height: 0; margin: 4px 0 0; }
  .pattern-slot-buttons { grid-column: 2; grid-row: 1 / span 2; }
  .pattern-dialog-hint { display: none; }
}

@media (hover: none) and (pointer: coarse), (max-width: 760px) {
  .touch-controls { display: flex; }
  .mobile-zoom { display: flex; }
  .hint { display: none; }
  .chat-panel { left: max(12px, env(safe-area-inset-left)); top: 58px; bottom: auto; width: auto; }
  .chat-panel.chat-open { width: min(360px, calc(100% - 24px)); }
  .chat-panel:not(.chat-open) .chat-toggle { height: 34px; border-radius: 17px; }
  .chat-panel.chat-open .chat-log { max-height: min(150px, 24vh); }
  body.chat-open .touch-controls { opacity: .12; pointer-events: none; }
  body.chat-open .touch-controls button { pointer-events: none; }
}

@media (max-width: 430px) {
  .touch-dpad { left: max(12px, env(safe-area-inset-left)); width: 150px; height: 150px; }
  .touch-action-cluster { right: max(10px, env(safe-area-inset-right)); width: 148px; height: 126px; gap: 7px; }
  .touch-controls button.touch-face { width: 62px; height: 56px; }
}

@media (max-height: 500px) and (orientation: landscape) {
  .touch-controls { display: flex; }
  .hint { display: none; }
  .welcome { padding: 0; }
  .creator-card { width: min(520px, calc(100% - 120px)); padding: 12px 18px; }
  #avatarPreview { display: none; }
  #nameInput { height: 34px; }
  fieldset { margin-top: 8px; }
  .swatch span { min-height: 30px; }
  .path-picker label span { min-height: 36px; }
  .play-button { margin-top: 9px; padding: 9px 12px; }
  .creator-card > small { margin-top: 6px; }
  .touch-dpad { bottom: calc(5px + env(safe-area-inset-bottom)); width: 126px; height: 126px; }
  .touch-controls button.dpad-button { border-radius: 10px; font-size: 18px; }
  .touch-action-cluster { bottom: calc(5px + env(safe-area-inset-bottom)); width: 132px; height: 108px; gap: 4px; }
  .touch-controls button.touch-face { width: 54px; height: 47px; }
  .touch-face b { font-size: 15px; }
  .touch-controls button small { font-size: 5px; }
  .chat-panel { top: 49px; }
  .chat-panel.chat-open .chat-log { max-height: 76px; }
}
