
  :root {
    --ink: #3b2855;
    --accent: #ff6bb5;
    --accent-2: #ffd166;
  }
  * { box-sizing: border-box; }
  html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    overflow: hidden;
    font-family: "Comic Sans MS", "Chalkboard SE", "Baloo", system-ui, sans-serif;
    color: var(--ink);
    background: linear-gradient(180deg, #ffeaf5, #e0f7ff);
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
  }
  #app {
    display: flex;
    flex-direction: column;
    height: 100dvh;
  }
  header {
    text-align: center;
    padding: 10px 12px 4px;
    flex-shrink: 0;
  }
  h1 {
    margin: 0;
    font-size: clamp(20px, 4.5vw, 32px);
    color: #d660b0;
    text-shadow: 0 2px 0 #fff, 0 4px 0 rgba(0,0,0,0.15);
  }
  .hint {
    font-size: clamp(12px, 2vw, 14px);
    color: #6d5982;
    margin-top: 2px;
  }
  #presence {
    font-size: 12px;
    color: rgba(60,40,80,0.6);
    margin-top: 2px;
  }

  #wall-wrap {
    flex: 1;
    margin: 8px clamp(8px, 2vw, 16px);
    position: relative;
    border-radius: 18px;
    border: 5px solid #fff;
    box-shadow: 0 6px 14px rgba(0,0,0,0.18);
    overflow: hidden;
    min-height: 0;
    transition: background 600ms ease-out;
  }
  /* Background scenes — applied to #wall-wrap.<name> */
  #wall-wrap.forest {
    background:
      radial-gradient(ellipse at 20% 100%, #5fa775 0 25%, transparent 26%),
      radial-gradient(ellipse at 80% 100%, #5fa775 0 25%, transparent 26%),
      radial-gradient(circle at 88% 14%, #fff2a0 0 36px, #ffd166 38px 52px, transparent 53px),
      linear-gradient(180deg, #d4f0ff 0%, #e5f7d8 60%, #93d4a4 100%);
  }
  #wall-wrap.beach {
    background:
      radial-gradient(circle at 88% 18%, #ffec8c 0 36px, #ffd166 38px 52px, transparent 53px),
      linear-gradient(180deg, #b6e3ff 0% 50%, #66c5e8 50.1% 75%, #f7e3a8 75.1% 100%);
  }
  #wall-wrap.castle {
    background:
      url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 300 100' preserveAspectRatio='none'><rect x='0' y='62' width='300' height='38' fill='%23b687d8'/><g fill='%23b687d8'><rect x='5' y='28' width='30' height='72'/><rect x='128' y='10' width='44' height='90'/><rect x='265' y='28' width='30' height='72'/></g><g fill='%23905bb5'><polygon points='5,28 20,10 35,28'/><polygon points='128,10 150,-8 172,10'/><polygon points='265,28 280,10 295,28'/></g><circle cx='150' cy='-5' r='3' fill='%23ffd166'/><g fill='%233e2454'><path d='M141 100 L141 78 Q150 68 159 78 L159 100 Z'/><rect x='15' y='50' width='10' height='12'/><rect x='145' y='30' width='10' height='14'/><rect x='275' y='50' width='10' height='12'/></g><g fill='%23b687d8'><rect x='10' y='56' width='10' height='6'/><rect x='30' y='56' width='10' height='6'/><rect x='50' y='56' width='10' height='6'/><rect x='70' y='56' width='10' height='6'/><rect x='90' y='56' width='10' height='6'/><rect x='110' y='56' width='10' height='6'/><rect x='180' y='56' width='10' height='6'/><rect x='200' y='56' width='10' height='6'/><rect x='220' y='56' width='10' height='6'/><rect x='240' y='56' width='10' height='6'/></g></svg>") bottom/100% 70% no-repeat,
      linear-gradient(180deg, #fff5fa 0%, #ffd9ec 40%, #ffc6e3 80%, #ffb3d9 100%);
  }
  #wall-wrap.sky {
    background: linear-gradient(180deg, #ff8fbf 0%, #ffa94d 25%, #ffd166 50%, #8ec97d 70%, #66d2ff 90%, #b36bff 100%);
  }
  #wall-wrap.meadow {
    background:
      radial-gradient(circle at 12% 18%, #fff 0 24px, transparent 25px),
      radial-gradient(circle at 70% 14%, #fff 0 18px, transparent 19px),
      linear-gradient(180deg, #cfeaff 0% 55%, #91d4a3 55.1% 100%);
  }
  #wall-wrap.night {
    background:
      radial-gradient(circle at 18% 22%, #fff 0 2px, transparent 3px),
      radial-gradient(circle at 38% 12%, #fff 0 2px, transparent 3px),
      radial-gradient(circle at 72% 28%, #fff 0 2.5px, transparent 3.5px),
      radial-gradient(circle at 86% 12%, #ffec8c 0 22px, #ffd166 24px 36px, transparent 37px),
      linear-gradient(180deg, #1a1148 0%, #3b2d6b 60%, #5a2d83 100%);
    color: #fff;
  }
  #wall-wrap.ocean {
    background:
      radial-gradient(ellipse at 50% 100%, #0e6a93 0 30%, transparent 31%),
      linear-gradient(180deg, #cdeefb 0% 18%, #5fb6db 18.1% 60%, #1f5f81 60.1% 100%);
  }
  #wall-wrap.space {
    background:
      radial-gradient(circle at 20% 24%, #fff 0 2px, transparent 3px),
      radial-gradient(circle at 44% 14%, #fff 0 1.6px, transparent 2.6px),
      radial-gradient(circle at 66% 30%, #ffe9a8 0 2.4px, transparent 3.4px),
      radial-gradient(circle at 82% 18%, #fff 0 2px, transparent 3px),
      radial-gradient(circle at 30% 60%, #cbb3ff 0 2px, transparent 3px),
      radial-gradient(circle at 78% 68%, #a8d8ff 0 60px, #7bb8ec 62px 80px, transparent 81px),
      linear-gradient(180deg, #0b0630 0%, #241a5c 55%, #3a1e6e 100%);
    color: #fff;
  }
  #wall-wrap.candy {
    background:
      radial-gradient(circle at 16% 22%, #fff 0 20px, transparent 21px),
      radial-gradient(circle at 74% 16%, #fff 0 15px, transparent 16px),
      radial-gradient(ellipse at 20% 100%, #ff9ecb 0 24%, transparent 25%),
      radial-gradient(ellipse at 82% 100%, #b3e6ff 0 24%, transparent 25%),
      linear-gradient(180deg, #fff0f8 0%, #ffd6ee 45%, #ffc4e6 70%, #ffb0dd 100%);
  }
  #wall {
    position: absolute;
    inset: 0;
    cursor: crosshair;
    touch-action: none;   /* pointer drag places a sticker — never scroll the canvas */
  }
  .placement {
    --sz: 1;
    --rot: 0deg;
    position: absolute;
    transform: translate(-50%, -50%) rotate(var(--rot)) scale(var(--sz));
    pointer-events: auto;
    cursor: pointer;
    filter: drop-shadow(0 3px 4px rgba(0,0,0,0.25));
    user-select: none;
    width: clamp(46px, 9vw, 70px);
    height: clamp(46px, 9vw, 70px);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: clamp(34px, 7vw, 54px);
    line-height: 1;
  }
  .placement img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    pointer-events: none;
  }
  .placement.fresh {
    animation: drop 340ms cubic-bezier(0.34, 1.56, 0.64, 1);
  }
  @keyframes drop {
    0%   { transform: translate(-50%, -50%) rotate(var(--rot)) scale(0);   opacity: 0; }
    70%  { transform: translate(-50%, -50%) rotate(var(--rot)) scale(calc(var(--sz) * 1.18)); opacity: 1; }
    100% { transform: translate(-50%, -50%) rotate(var(--rot)) scale(var(--sz)); }
  }
  /* Failed placement — a little wobble-out so a dropped sticker never just
     vanishes with no explanation. */
  .placement.oops { animation: oops 400ms ease-in forwards; }
  @keyframes oops {
    0%   { opacity: 1; transform: translate(-50%, -50%) rotate(var(--rot)) scale(var(--sz)); }
    100% { opacity: 0; transform: translate(-50%, -50%) rotate(calc(var(--rot) + 18deg)) scale(calc(var(--sz) * 0.6)); }
  }
  /* Ghost preview that follows the finger before a sticker is committed. */
  .placement.ghost {
    opacity: 0.45;
    pointer-events: none;
    cursor: none;
    filter: drop-shadow(0 2px 3px rgba(0,0,0,0.2));
    animation: none;
    z-index: 4;
  }
  /* Sparkle burst when a sticker lands — pure CSS, appended to the placement. */
  .spark {
    position: absolute;
    left: 50%; top: 50%;
    width: 7px; height: 7px;
    margin: -3.5px 0 0 -3.5px;
    border-radius: 50%;
    background: radial-gradient(circle, #fff 0 40%, #ffd166 60%, transparent 72%);
    pointer-events: none;
    animation: spark 600ms ease-out forwards;
    transform: rotate(var(--a)) translateY(0) scale(1);
  }
  @keyframes spark {
    0%   { opacity: 1; transform: rotate(var(--a)) translateY(0) scale(1); }
    100% { opacity: 0; transform: rotate(var(--a)) translateY(calc(var(--d) * -1)) scale(0.2); }
  }
  @media (prefers-reduced-motion: reduce) {
    .placement.fresh, .spark, .placement.oops { animation: none; }
    .spark { display: none; }
  }
  .placement.mine::after {
    content: "";
    position: absolute;
    bottom: -4px;
    right: -4px;
    width: 12px;
    height: 12px;
    background: #ff6bb5;
    border: 2px solid #fff;
    border-radius: 50%;
    box-shadow: 0 1px 2px rgba(0,0,0,0.2);
  }
  .name-toast {
    position: absolute;
    background: rgba(0,0,0,0.78);
    color: #fff;
    font-size: 13px;
    padding: 4px 10px;
    border-radius: 999px;
    transform: translate(-50%, -150%);
    pointer-events: none;
    white-space: nowrap;
    animation: toastIn 200ms ease-out;
    z-index: 30;
  }
  @keyframes toastIn {
    from { opacity: 0; transform: translate(-50%, -130%); }
    to   { opacity: 1; transform: translate(-50%, -150%); }
  }

  /* Sticker tray — multi-row, scrolling */
  #tray {
    background: #fff;
    border-top: 4px solid var(--accent);
    padding: 8px 6px;
    flex-shrink: 0;
    max-height: clamp(110px, 22vh, 170px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  #tray-inner {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 0 6px;
    justify-content: center;
  }
  .sticker {
    appearance: none;
    -webkit-appearance: none;
    font-family: inherit;
    color: inherit;
    flex-shrink: 0;
    background: #fff5fa;
    border: 3px solid #fce4ec;
    border-radius: 14px;
    padding: 6px;
    cursor: pointer;
    transition: transform 100ms;
    width: 54px;
    height: 54px;
    font-size: 28px;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .sticker:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
  .sticker img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    pointer-events: none;
  }
  .sticker.selected {
    background: #ffd9ec;
    border-color: var(--accent);
    transform: scale(1.12) translateY(-2px);
    box-shadow: 0 3px 0 var(--accent);
  }
  .tray-divider {
    flex-basis: 100%;
    height: 0;
    border-top: 2px dashed #ffc6e3;
    margin: 4px 0;
    opacity: 0.6;
  }

  #home-btn, #clear-mine, #music-btn {
    position: fixed;
    top: 10px;
    font-family: inherit;
    font-size: 14px;
    padding: 8px 13px;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 999px;
    border: 3px solid var(--accent);
    background: #fff;
    color: var(--ink);
    text-decoration: none;
    box-shadow: 0 3px 0 var(--accent);
    z-index: 30;
    cursor: pointer;
  }
  #home-btn   { left: 10px; }
  #clear-mine { right: 10px; }
  /* Music toggle sits just to the left of the Undo button. */
  #music-btn  { right: 148px; min-width: 44px; text-align: center; }
  #home-btn:active, #clear-mine:active, #music-btn:active { translate: 0 2px; box-shadow: 0 1px 0 var(--accent); }
  #music-btn.muted { opacity: 0.65; }
  #clear-mine.dim { opacity: 0.5; }
  #clear-mine:disabled { cursor: default; }

  #count {
    position: absolute;
    bottom: 8px;
    right: 12px;
    font-size: 13px;
    color: rgba(60,40,80,0.7);
    background: rgba(255,255,255,0.7);
    padding: 2px 8px;
    border-radius: 999px;
    z-index: 5;
  }

  /* Scene picker chips above the wall */
  #scenes {
    display: flex;
    justify-content: center;
    gap: 5px;
    padding: 2px 8px 4px;
    flex-shrink: 0;
    flex-wrap: wrap;
  }
  #scenes button {
    font-family: inherit;
    font-size: 22px;
    min-width: 44px;
    min-height: 44px;
    padding: 4px 8px;
    border-radius: 14px;
    border: 2px solid #fff;
    background: #fff;
    cursor: pointer;
    box-shadow: 0 2px 0 var(--accent);
  }
  #scenes button.active {
    border-color: var(--accent);
    transform: scale(1.1);
    box-shadow: 0 2px 0 var(--accent), 0 0 0 2px var(--accent);
  }
  #scenes button:active { translate: 0 2px; box-shadow: 0 0 0 var(--accent); }
  #scenes button:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

  /* Sticker size picker */
  #sizes {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 2px 8px;
    flex-shrink: 0;
  }
  #sizes .sizes-label { font-size: 12px; color: #8a76a0; }
  #sizes button {
    font-family: inherit;
    font-weight: bold;
    font-size: 15px;
    min-width: 44px;
    min-height: 40px;
    border-radius: 12px;
    border: 2px solid #ffc6e3;
    background: #fff5fa;
    color: var(--ink);
    cursor: pointer;
    box-shadow: 0 2px 0 #ffc6e3;
  }
  #sizes button.active {
    background: #ffd9ec;
    border-color: var(--accent);
    box-shadow: 0 2px 0 var(--accent);
    transform: scale(1.06);
  }
  #sizes button:active { translate: 0 2px; box-shadow: 0 0 0 var(--accent); }
  #sizes button:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

  /* Chat panel — top-right of the wall. Scrollable: drag/swipe inside
     the panel to read older messages. The column-reverse layout puts
     newest messages at the visual bottom of the panel and lets the
     browser keep them in view automatically. */
  #chat-panel {
    position: absolute;
    top: 8px;
    right: 8px;
    width: clamp(170px, 34vw, 300px);
    max-height: 66%;
    overflow-y: auto;
    overscroll-behavior: contain;
    /* The panel container ignores pointer events so taps in its empty area
       fall through to the wall (place a sticker); only the message bubbles
       themselves are interactive/scrollable. */
    pointer-events: none;
    display: flex;
    flex-direction: column-reverse;
    gap: 4px;
    z-index: 6;
    scrollbar-width: thin;
    scrollbar-color: rgba(176,62,127,0.45) transparent;
  }
  #wall-wrap.chat-hidden #chat-panel { display: none; }
  /* Tap to hide/show chat so it never buries the stickers underneath it. */
  #chat-toggle {
    position: absolute;
    top: 8px;
    left: 8px;
    z-index: 7;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 2px solid var(--accent);
    background: rgba(255,255,255,0.9);
    font-size: 18px;
    line-height: 1;
    cursor: pointer;
    box-shadow: 0 2px 4px rgba(0,0,0,0.18);
    padding: 0;
  }
  #chat-toggle:active { translate: 0 1px; }
  #chat-toggle:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
  #wall-wrap.chat-hidden #chat-toggle { opacity: 0.7; }
  @media (max-width: 500px) {
    #chat-panel { width: clamp(150px, 44vw, 220px); max-height: 50%; }
  }
  /* Webkit-flavored slim scrollbar so it's obvious the panel scrolls */
  #chat-panel::-webkit-scrollbar { width: 6px; }
  #chat-panel::-webkit-scrollbar-thumb {
    background: rgba(176,62,127,0.5);
    border-radius: 999px;
  }
  #chat-panel::-webkit-scrollbar-track { background: transparent; }
  .chat-msg {
    background: rgba(255,255,255,0.92);
    padding: 5px 9px;
    border-radius: 12px;
    font-size: 13px;
    line-height: 1.25;
    color: #3b2855;
    box-shadow: 0 2px 4px rgba(0,0,0,0.15);
    animation: chatIn 220ms ease-out;
    word-wrap: break-word;
    overflow-wrap: break-word;
    max-width: 100%;
    pointer-events: auto;   /* messages stay interactive/scrollable even though the panel doesn't */
  }
  /* Scene-change banner near the top of the wall. */
  .flash {
    position: absolute;
    top: 10px;
    left: 50%;
    transform: translate(-50%, -8px);
    background: rgba(0,0,0,0.72);
    color: #fff;
    font-size: 13px;
    padding: 6px 14px;
    border-radius: 999px;
    white-space: nowrap;
    pointer-events: none;
    opacity: 0;
    transition: opacity 220ms ease, transform 220ms ease;
    z-index: 20;
  }
  .flash.show { opacity: 1; transform: translate(-50%, 0); }
  .chat-msg .who {
    font-weight: bold;
    color: var(--accent);
    margin-right: 4px;
  }
  .chat-msg .av { font-size: 14px; margin-right: 2px; vertical-align: -2px; }
  .chat-msg .av img { width: 14px; height: 14px; vertical-align: -2px; object-fit: contain; }
  @keyframes chatIn {
    from { opacity: 0; transform: translateY(-6px); }
    to   { opacity: 1; transform: translateY(0); }
  }

  /* Chat composer — between wall and tray */
  #chat-bar {
    display: flex;
    gap: 6px;
    padding: 6px 10px;
    background: #fff;
    border-top: 2px solid #ffd9ec;
    flex-shrink: 0;
    align-items: center;
  }
  #chat-input {
    flex: 1;
    font-family: inherit;
    font-size: 14px;
    padding: 8px 12px;
    border-radius: 999px;
    border: 2px solid #ffc6e3;
    background: #fff5fa;
    color: var(--ink);
    min-width: 0;
  }
  #chat-input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
  #chat-input:disabled { opacity: 0.5; cursor: not-allowed; }
  #chat-send {
    font-family: inherit;
    font-size: 14px;
    padding: 8px 14px;
    border-radius: 999px;
    border: 2px solid var(--accent);
    background: var(--accent);
    color: #fff;
    cursor: pointer;
    font-weight: bold;
    box-shadow: 0 2px 0 #b03e7f;
    flex-shrink: 0;
  }
  #chat-send:active { translate: 0 2px; box-shadow: 0 0 0 #b03e7f; }
  #chat-send:disabled { background: #ccc; border-color: #aaa; box-shadow: 0 1px 0 #888; cursor: not-allowed; }
  #chat-status {
    font-size: 11px;
    color: #b03e7f;
    text-align: center;
    padding: 2px;
    min-height: 14px;
  }

  #login-prompt {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.45);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 50;
    padding: 14px;
  }
  #login-prompt.show { display: flex; }
  #login-prompt .panel {
    background: #fff;
    border: 5px solid var(--accent);
    border-radius: 22px;
    padding: 22px 26px;
    text-align: center;
    max-width: 340px;
    box-shadow: 0 12px 36px rgba(0,0,0,0.3);
  }
  #login-prompt h2 { margin: 0 0 8px; color: var(--accent); }
  #login-prompt p  { margin: 6px 0 14px; }
  #login-prompt a {
    display: inline-block;
    background: var(--accent);
    color: #fff;
    text-decoration: none;
    padding: 9px 18px;
    border-radius: 999px;
    font-weight: bold;
    box-shadow: 0 3px 0 #b03e7f;
  }

  /* Full-bleed painted title screen that doubles as the asset-loading
     curtain. The gate script keeps Start at "Loading…" until the title
     art has decoded — the multiplayer wall polls in the background so
     the kid sees a fully-painted scene the moment they tap Start. */
  #title-screen {
    position: fixed; inset: 0;
    z-index: 9000;
    display: grid; place-items: center;
    transition: opacity 0.5s ease;
    background: #0b2a44;
    overflow: hidden;
  }
  #title-screen.hide {
    opacity: 0;
    pointer-events: none;
  }
  #title-screen #title-art {
    position: absolute; inset: 0;
    width: 100%; height: 100%;
    object-fit: cover;
    object-position: center;
    z-index: 0;
    pointer-events: none;
  }
  #title-screen #title-text {
    position: absolute; z-index: 1;
    left: 50%; top: 4vh;
    transform: translateX(-50%);
    text-align: center;
    pointer-events: none;
    user-select: none;
    line-height: 0.9;
    font-family: "Fredoka", "Comic Sans MS", system-ui, sans-serif;
    font-weight: 800;
    width: max-content;
  }
  #title-screen #title-text .row1,
  #title-screen #title-text .row2 {
    display: block;
    color: #fff7d6;
    letter-spacing: 2px;
    text-shadow:
      -3px 0 0 #1a3f5c, 3px 0 0 #1a3f5c,
      0 -3px 0 #1a3f5c, 0 3px 0 #1a3f5c,
      -3px -3px 0 #1a3f5c, 3px 3px 0 #1a3f5c,
      -3px 3px 0 #1a3f5c, 3px -3px 0 #1a3f5c,
      0 6px 0 rgba(0,0,0,0.35),
      0 12px 22px rgba(0,0,0,0.55);
    animation: title-bob 2.4s ease-in-out infinite alternate;
  }
  #title-screen #title-text .row1 {
    font-size: clamp(24px, 6.5vw, 76px);
    color: #ffe18f;
    margin-bottom: 4px;
  }
  #title-screen #title-text .row2 {
    font-size: clamp(40px, 11vw, 132px);
    color: #ffdf63;
    letter-spacing: 4px;
    animation-delay: 0.2s;
  }
  @keyframes title-bob {
    from { transform: translateY(0)    rotate(-1deg); }
    to   { transform: translateY(-6px) rotate(1deg); }
  }
  #title-screen .title-actions {
    position: absolute; z-index: 1;
    left: 50%; bottom: 10vh;
    transform: translateX(-50%);
    display: flex; flex-direction: column;
    align-items: center; gap: 14px;
  }
  #title-screen .wood-btn {
    appearance: none;
    border: none;
    cursor: pointer;
    font-family: "Georgia", "Times New Roman", serif;
    font-weight: 900;
    font-size: clamp(20px, 3.4vw, 34px);
    letter-spacing: 0.06em;
    color: #4a2a14;
    text-shadow: 0 1px 0 rgba(255, 240, 210, 0.7);
    padding: 0.5em 1.6em;
    border-radius: 18px;
    background: linear-gradient(180deg, #fbe6b1 0%, #f1c878 45%, #d99a3f 100%);
    box-shadow:
      0 0 0 4px #4a2a14,
      0 0 0 7px #b6803a,
      inset 0 4px 0 rgba(255, 250, 220, 0.65),
      inset 0 -6px 8px rgba(120, 60, 20, 0.45),
      0 8px 14px rgba(0, 0, 0, 0.45);
    transition: transform 0.12s ease;
  }
  #title-screen .wood-btn:hover  { transform: translateY(-2px); }
  #title-screen .wood-btn:active { transform: translateY(2px); }
  #title-screen .wood-btn:disabled {
    cursor: wait;
    filter: saturate(0.5) brightness(0.85);
  }
