:root {
  --ink: #22304a;
  --paper: #fdf0dc;
  --card: #fffaf2;
  --mint: #63d1bd;
  --pink: #ff93ac;
  --sun: #ffcf5a;
  --line: #22304a;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  font-family: ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI", "Rubik", sans-serif;
  color: var(--ink);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--paper); overscroll-behavior: none; }
body { -webkit-tap-highlight-color: transparent; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.defs { position: absolute; }
button { font: inherit; color: inherit; }

#world { position: fixed; inset: 0 auto auto 0; image-rendering: pixelated; image-rendering: crisp-edges; touch-action: none; cursor: grab; }
#world:active { cursor: grabbing; }
#front { position: fixed; inset: 0 auto auto 0; pointer-events: none; }

/* top: name, three pixel meters, sound and help */
#top {
  position: fixed; inset: 0 0 auto 0; z-index: 2;
  display: flex; align-items: center; gap: 10px;
  padding: calc(var(--safe-t) + 10px) 14px 10px;
}
.name b { font-size: 20px; font-weight: 800; letter-spacing: -0.01em; }
.meters { flex: 1; display: flex; gap: 6px; justify-content: center; min-width: 0; }
.meter { display: flex; align-items: center; gap: 4px; flex: 1; max-width: 80px; min-width: 0; }
.meter .emo { flex: none; font-size: 15px; line-height: 1; font-weight: 400; }
.meter span { flex: 1; height: 10px; border: 2px solid var(--line); background: #fff; position: relative; min-width: 16px; }
.meter i { position: absolute; inset: 0 auto 0 0; width: calc(var(--v, 50) * 1%); transition: width 400ms steps(8); }
[dir="rtl"] .meter i { inset: 0 0 0 auto; }
.meter.food { color: #e8454e; } .meter.food i { background: #e8454e; }
.meter.energy { color: #e0a030; } .meter.energy i { background: var(--sun); }
.meter.joy { color: #ff5c86; } .meter.joy i { background: var(--pink); }
.meter.pee { color: #d9a91e; } .meter.pee i { background: #f2d35b; }
.meter.low span { animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { border-color: #e8454e; } }
.icon {
  width: 44px; height: 44px; flex: none; display: grid; place-items: center;
  border: 2px solid var(--line); border-radius: 12px; background: var(--card); cursor: pointer;
  box-shadow: 0 3px 0 var(--line); transition: transform 90ms, box-shadow 90ms;
}
.icon:active { transform: translateY(3px); box-shadow: 0 0 0 var(--line); }
.icon svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* bottom tray: two categories; each opens four things above it */
#tray { position: fixed; inset: auto 0 0 0; z-index: 2; padding: 10px 14px calc(var(--safe-b) + 12px); }
.cats { display: flex; gap: 8px; justify-content: center; }
.cat {
  display: flex; align-items: center; justify-content: center; flex: 1; max-width: 120px; min-width: 0; min-height: 60px; padding: 6px 8px;
  border: 2px solid var(--line); border-radius: 16px; background: var(--card); cursor: pointer;
  box-shadow: 0 4px 0 var(--line); transition: transform 90ms, box-shadow 90ms; font-size: 16px; font-weight: 800;
}
.cat canvas { width: 60px; height: 36px; image-rendering: pixelated; }
.cat:active, .cat[aria-expanded="true"] { transform: translateY(4px); box-shadow: 0 0 0 var(--line); }
.cat[aria-expanded="true"] { background: var(--sun); }
.panel {
  position: absolute; inset: auto 14px 100% 14px; margin-bottom: 2px;
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; padding: 10px;
  border: 2px solid var(--line); border-radius: 16px; background: var(--card); box-shadow: 0 4px 0 var(--line);
  animation: pop2 160ms ease-out;
}
.panel.dragging { opacity: 0.35; }
.feed {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  min-width: 0; min-height: 66px; padding: 6px 4px 5px;
  border: 2px solid var(--line); border-radius: 14px; background: var(--card); cursor: grab; touch-action: none;
  box-shadow: 0 4px 0 var(--line); transition: transform 90ms, box-shadow 90ms;
}
.feed:active { transform: translateY(4px); box-shadow: 0 0 0 var(--line); cursor: grabbing; }
.feed canvas { width: 32px; height: 32px; image-rendering: pixelated; }
.feed span { font-size: 13px; font-weight: 700; }
.feed[aria-pressed="true"] { background: var(--sun); }
.icon:focus-visible, .feed:focus-visible, .primary:focus-visible, .back:focus-visible { outline: 3px solid #2f6fb0; outline-offset: 3px; }

#toast {
  position: fixed; z-index: 3; top: calc(var(--safe-t) + 66px); left: 50%; transform: translateX(-50%);
  padding: 8px 14px; border: 2px solid var(--line); border-radius: 12px; background: var(--card);
  font-size: 15px; font-weight: 700; box-shadow: 0 3px 0 var(--line); width: max-content; max-width: calc(100vw - 32px); text-align: center;
  animation: pop 180ms steps(3);
}
@keyframes pop { from { transform: translate(-50%, -6px); opacity: 0; } }

/* dialogs: chunky cards in the same pixel spirit */
dialog {
  width: min(380px, calc(100vw - 32px)); border: 3px solid var(--line); border-radius: 20px; padding: 22px 20px 18px;
  background: var(--card); color: var(--ink); box-shadow: 0 6px 0 var(--line); text-align: center;
}
dialog::backdrop { background: rgba(34, 48, 74, 0.45); }
dialog[open] { animation: pop2 220ms ease-out; }
@keyframes pop2 { from { transform: translateY(12px) scale(0.97); opacity: 0; } }
dialog h1, dialog h2 { margin: 6px 0 6px; font-size: 24px; letter-spacing: -0.01em; }
dialog h2 { font-size: 20px; }
dialog p { margin: 0 0 12px; font-size: 16px; line-height: 1.5; }
dialog small { display: block; margin-top: 12px; font-size: 13px; color: #4a5670; }
.badge { width: 88px; height: 88px; image-rendering: pixelated; border-radius: 22px; border: 3px solid var(--line); }
.tips { list-style: none; margin: 0 0 16px; padding: 0; text-align: start; font-size: 15.5px; line-height: 1.45; }
.tips li { padding: 7px 0; border-bottom: 2px dashed #e7d7bd; }
.tips li:last-child { border-bottom: 0; }
.primary {
  width: 100%; min-height: 50px; border: 2px solid var(--line); border-radius: 14px; background: var(--mint);
  font-size: 17px; font-weight: 800; cursor: pointer; box-shadow: 0 4px 0 var(--line); transition: transform 90ms, box-shadow 90ms;
}
.primary:active { transform: translateY(4px); box-shadow: 0 0 0 var(--line); }
.close { position: absolute; top: 10px; inset-inline-end: 10px; box-shadow: none; }
.close:active { transform: none; }
.picker { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 10px 0 12px; }
.pick {
  display: flex; flex-direction: column; align-items: center; gap: 2px; min-height: 76px; padding: 6px 2px 5px;
  border: 2px solid var(--line); border-radius: 12px; background: #fff; cursor: pointer; box-shadow: 0 3px 0 var(--line);
}
.pick canvas { width: 52px; height: 52px; image-rendering: pixelated; }
.pick span { font-size: 12px; font-weight: 700; line-height: 1.2; }
.pick[aria-pressed="true"] { background: var(--sun); transform: translateY(3px); box-shadow: 0 0 0 var(--line); }
.pick:focus-visible { outline: 3px solid #2f6fb0; outline-offset: 2px; }
.err { color: #c0303a; font-weight: 700; font-size: 14px; }
/* settings menu under the gear */
.menu {
  position: fixed; z-index: 4; top: calc(var(--safe-t) + 62px); inset-inline-end: 12px; width: min(300px, calc(100vw - 24px));
  border: 2px solid var(--line); border-radius: 16px; background: var(--card); box-shadow: 0 5px 0 var(--line); overflow: hidden;
  animation: pop2 160ms ease-out;
}
.mhead { display: flex; flex-direction: column; gap: 2px; padding: 12px 16px 10px; border-bottom: 2px dashed #e7d7bd; }
.mhead b { font-size: 17px; }
.mhead span { font-size: 13.5px; color: #4a5670; }
.mitem {
  display: flex; align-items: center; justify-content: space-between; gap: 10px; width: 100%; min-height: 52px; padding: 0 16px;
  border: 0; border-bottom: 2px dashed #e7d7bd; background: none; cursor: pointer; font-size: 16px; font-weight: 700; text-align: start;
}
.mitem:active { background: #f6ead6; }
.mitem svg, .icon.small svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.mroom { padding: 10px 16px 12px; border-bottom: 2px dashed #e7d7bd; font-size: 14px; font-weight: 700; }
.seg { display: grid; grid-template-columns: repeat(2, 1fr); gap: 6px; margin-top: 8px; }
.seg button { min-height: 44px; border: 2px solid var(--line); border-radius: 12px; background: #fff; font-size: 14px; font-weight: 700; cursor: pointer; }
.seg button[aria-pressed="true"] { background: var(--mint); }
.mver { display: flex; align-items: center; justify-content: space-between; padding: 10px 16px; font-size: 14px; color: #4a5670; font-weight: 700; }
.icon.small { width: 44px; height: 44px; box-shadow: 0 2px 0 var(--line); }
.mitem:focus-visible, .seg button:focus-visible, .cat:focus-visible { outline: 3px solid #2f6fb0; outline-offset: -3px; }
.namefield { display: block; margin: 4px 0 6px; font-size: 15px; font-weight: 700; text-align: start; }
.nameinput {
  width: 100%; min-height: 48px; margin-bottom: 14px; padding: 0 14px; border: 2px solid var(--line); border-radius: 12px;
  background: #fff; font: inherit; font-size: 17px; font-weight: 700; color: var(--ink); -webkit-user-select: text; user-select: text;
}
.nameinput:focus { outline: 3px solid #2f6fb0; outline-offset: 2px; }
.back { display: inline-block; margin-top: 12px; padding: 10px; color: #2f6fb0; font-weight: 700; text-decoration: none; }
#installHelp { margin-top: 10px; font-size: 14.5px; }

.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.sr button:focus { position: fixed; inset: auto 16px calc(var(--safe-b) + 100px) auto; width: auto; height: auto; clip: auto; padding: 10px 14px; background: var(--card); border: 2px solid var(--line); border-radius: 12px; }

@media (max-width: 360px) { .name { display: none; } .cat { min-width: 96px; } }
@media (prefers-reduced-motion: reduce) { dialog[open], #toast, .menu, .panel { animation: none; } .meter.low span { animation: none; border-color: #e8454e; } }

[hidden] { display: none !important; }

.feed.room canvas { width: 64px; height: 56px; border-radius: 6px; }
.feed.room[aria-pressed="true"] { background: var(--mint); }
#call .picker { grid-template-columns: repeat(4, 1fr); }
.primary.alt { background: var(--card); margin-top: 4px; }
.cat .catimg { width: 48px; height: 48px; image-rendering: auto; }
.callpic { width: 72px; height: 72px; display: block; margin: 0 auto 2px; }

/* mini games */
body.gaming #tray, body.gaming #menuBtn { display: none; }
#gameHud { position: fixed; z-index: 3; top: calc(var(--safe-t) + 60px); inset-inline: 12px; display: flex; justify-content: space-between; align-items: center; pointer-events: none; }
#gameHud .icon { pointer-events: auto; }
.gscore { min-width: 64px; padding: 6px 12px; border: 2px solid var(--line); border-radius: 12px; background: var(--card); box-shadow: 0 3px 0 var(--line); font-size: 20px; font-weight: 800; text-align: center; }
.gcard {
  position: fixed; z-index: 4; inset: 50% auto auto 50%; transform: translate(-50%, -50%); width: min(320px, calc(100vw - 32px));
  padding: 20px 18px 16px; border: 3px solid var(--line); border-radius: 20px; background: var(--card); box-shadow: 0 6px 0 var(--line); text-align: center;
  animation: pop2 200ms ease-out;
}
.gcard h2 { margin: 0 0 6px; font-size: 22px; }
.gcard p { margin: 0 0 14px; font-size: 17px; font-weight: 700; }
.feed.game img { width: 44px; height: 44px; }
.feed.game small { font-size: 11px; color: #4a5670; font-weight: 700; }
.howtos { list-style: none; margin: 6px 0 14px; padding: 0; }
.howtos li { margin-bottom: 10px; }
.howtos p { margin: 4px 0 0; font-size: 15.5px; font-weight: 700; }
.howto { width: 100%; height: auto; display: block; }
.chpic canvas { width: 96px; height: 96px; image-rendering: pixelated; }
.chscore { font-size: 22px; font-weight: 800; }
#gResult { white-space: pre-line; }
