:root {
  --ink: #3b2d26;
  --ink-soft: #7a6558;
  --paper: rgba(255, 249, 240, 0.86);
  --paper-solid: #fff8ee;
  --line: rgba(59, 45, 38, 0.12);
  --accent: #e8875a;
  --accent-soft: #f6c9a8;
  --good: #7fbf8a;
  --bg: #231c19;

  /* Surfaces: written by HUD.js from the screen-segment layout */
  --wall-x: 0px; --wall-y: 0px; --wall-w: 100vw; --wall-h: 50vh;
  --floor-x: 0px; --floor-y: 50vh; --floor-w: 100vw; --floor-h: 50vh;
  --safe: 12px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: "Nunito", system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
}

#stage {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  display: block;
  touch-action: none;
  outline: none;
}

button { font: inherit; color: inherit; cursor: pointer; }

/* ---------- Hinge crease (virtual hinge only) ---------- */
#crease {
  position: fixed;
  pointer-events: none;
  background: linear-gradient(var(--crease-dir, 180deg), rgba(0,0,0,0) 0%, rgba(20,12,8,0.28) 50%, rgba(255,255,255,0.10) 52%, rgba(0,0,0,0) 100%);
  z-index: 2;
}

/* ---------- Emulated device body (desktop) ---------- */
#device {
  position: fixed;
  pointer-events: none;
  z-index: 1;
  border-radius: 18px;
  box-shadow:
    0 0 0 9px #17120f,
    0 0 0 10px rgba(255, 255, 255, 0.08),
    0 30px 80px rgba(0, 0, 0, 0.55);
}
#device span {
  position: absolute;
  left: 50%;
  bottom: -30px;
  transform: translateX(-50%);
  white-space: nowrap;
  font-size: 11px;
  font-weight: 700;
  color: rgba(255, 244, 230, 0.4);
}

.previewing #device, .previewing #crease, .previewing #sim #sim-rotate { display: none; }
/* In the 3D preview the flat screens aren't visible: centre the bar & hints under the phone. */
.previewing #bar { left: 50vw; top: auto; bottom: 24px; }
.previewing #hint { left: 50vw; top: auto; bottom: 78px; }

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; pointer-events: none; z-index: 5; }
#hud > * { pointer-events: auto; }

#status {
  position: absolute;
  left: calc(var(--status-x, var(--wall-x)) + var(--safe));
  top: calc(var(--status-y, var(--wall-y)) + var(--safe) + env(safe-area-inset-top, 0px));
  display: grid;
  gap: 5px;
  padding: 9px 11px 8px;
  background: var(--paper);
  border-radius: 14px;
  box-shadow: 0 6px 20px rgba(30, 18, 10, 0.18);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  min-width: 128px;
  transition: opacity .4s;
}
.stat { display: flex; align-items: center; gap: 7px; font-size: 12px; }
.stat .ico { width: 16px; text-align: center; font-size: 12px; line-height: 1; }
.stat i {
  flex: 1;
  height: 6px;
  border-radius: 6px;
  background: rgba(59, 45, 38, 0.1);
  overflow: hidden;
  display: block;
}
.stat b {
  display: block;
  height: 100%;
  width: 50%;
  border-radius: 6px;
  background: var(--accent);
  transition: width .6s ease, background-color .6s;
}
.stat[data-stat="happiness"] b { background: #ec7f86; }
.stat[data-stat="hunger"] b { background: #e7a55a; }
.stat[data-stat="energy"] b { background: #e9c94f; }
.stat.low b { animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .45; } }

.chips { display: flex; gap: 8px; font-size: 12px; font-weight: 800; color: var(--ink-soft); padding-top: 2px; }
.chips em { font-style: normal; color: var(--ink); }
.chips .bump { animation: bump .5s ease; display: inline-block; }
@keyframes bump { 40% { transform: scale(1.35); } }

#corner {
  position: absolute;
  top: calc(var(--wall-y) + var(--safe) + env(safe-area-inset-top, 0px));
  left: calc(var(--wall-x) + var(--wall-w) - var(--safe));
  transform: translateX(-100%);
  display: flex;
  gap: 6px;
}
.icon-btn {
  width: 34px; height: 34px;
  border-radius: 50%;
  border: 0;
  background: var(--paper);
  box-shadow: 0 4px 14px rgba(30, 18, 10, 0.16);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  display: grid; place-items: center;
  font-size: 15px;
  color: var(--ink);
  transition: transform .15s;
}
.icon-btn:active { transform: scale(.9); }
.icon-btn.on { background: var(--accent); color: #fff; }

#mode-chip {
  position: absolute;
  top: calc(var(--wall-y) + var(--safe) + 42px + env(safe-area-inset-top, 0px));
  left: calc(var(--wall-x) + var(--wall-w) - var(--safe));
  transform: translateX(-100%);
  display: flex; gap: 6px; align-items: baseline;
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(35, 25, 20, 0.35);
  color: #fff6ea;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: .02em;
  pointer-events: none;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}
#mode-angle { font-variant-numeric: tabular-nums; opacity: .75; min-width: 3.2ch; text-align: right; }

#toast {
  position: absolute;
  top: calc(var(--wall-y) + var(--safe) + 72px + env(safe-area-inset-top, 0px));
  left: calc(var(--wall-x) + var(--wall-w) / 2);
  transform: translate(-50%, -12px);
  opacity: 0;
  padding: 8px 14px;
  border-radius: 999px;
  background: var(--paper-solid);
  box-shadow: 0 8px 26px rgba(30, 18, 10, 0.25);
  font-weight: 800;
  font-size: 13px;
  pointer-events: none;
  transition: opacity .35s, transform .35s cubic-bezier(.2, 1.4, .4, 1);
  width: max-content;
  max-width: calc(var(--wall-w) - 120px);
  text-align: center;
  line-height: 1.3;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.secret { background: #2f2340; color: #ffe9b8; }

#hint {
  position: absolute;
  left: calc(var(--floor-x) + var(--floor-w) / 2);
  top: calc(var(--floor-y) + var(--floor-h) - var(--safe) - 58px - env(safe-area-inset-bottom, 0px));
  transform: translate(-50%, 6px);
  opacity: 0;
  color: #fff6ea;
  font-size: 12.5px;
  font-weight: 700;
  text-shadow: 0 1px 8px rgba(0, 0, 0, 0.55);
  pointer-events: none;
  transition: opacity .6s, transform .6s;
  white-space: nowrap;
}
#hint.show { opacity: .95; transform: translate(-50%, 0); }

#bar {
  position: absolute;
  left: calc(var(--floor-x) + var(--floor-w) / 2);
  top: calc(var(--floor-y) + var(--floor-h) - var(--safe) - 46px - env(safe-area-inset-bottom, 0px));
  transform: translate(-50%, 16px) scale(.96);
  opacity: 0;
  pointer-events: none;
  display: flex;
  gap: 4px;
  padding: 5px;
  border-radius: 999px;
  background: var(--paper);
  box-shadow: 0 10px 30px rgba(30, 18, 10, 0.25);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  transition: opacity .25s, transform .3s cubic-bezier(.2, 1.3, .4, 1);
}
#bar.show { opacity: 1; transform: translate(-50%, 0) scale(1); pointer-events: auto; }
#bar .title {
  align-self: center;
  padding: 0 8px 0 10px;
  font-size: 12px;
  font-weight: 800;
  color: var(--ink-soft);
  white-space: nowrap;
}
#bar button {
  border: 0;
  background: transparent;
  border-radius: 999px;
  height: 36px;
  padding: 0 12px 0 9px;
  display: flex; align-items: center; gap: 5px;
  font-size: 12.5px; font-weight: 800;
  white-space: nowrap;
  transition: background .15s, transform .12s;
}
#bar button:hover { background: rgba(232, 135, 90, 0.12); }
#bar button:active { transform: scale(.94); background: rgba(232, 135, 90, 0.22); }
#bar button[disabled] { opacity: .4; }
#bar button .e { font-size: 15px; }

#shop {
  position: absolute;
  left: calc(var(--floor-x) + var(--floor-w) / 2);
  top: calc(var(--floor-y) + var(--floor-h) / 2);
  transform: translate(-50%, -50%);
  width: min(320px, calc(var(--floor-w) - 32px));
  max-height: calc(var(--floor-h) - 32px);
  overflow: auto;
  background: var(--paper-solid);
  border-radius: 18px;
  box-shadow: 0 18px 50px rgba(20, 10, 5, 0.4);
  padding: 12px;
}
#shop header, #sim header {
  display: flex; justify-content: space-between; align-items: center;
  font-weight: 800; font-size: 14px; margin-bottom: 8px;
}
.close { border: 0; background: rgba(59,45,38,.08); width: 26px; height: 26px; border-radius: 50%; font-size: 12px; }
#shop .items { display: grid; grid-template-columns: repeat(auto-fill, minmax(88px, 1fr)); gap: 8px; }
#shop .item {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 14px;
  padding: 10px 6px 8px;
  display: grid; justify-items: center; gap: 2px;
  font-size: 12px; font-weight: 800;
}
#shop .item .e { font-size: 24px; }
#shop .item small { color: var(--ink-soft); font-weight: 700; }
#shop .item[disabled] { opacity: .45; }
#shop .item.owned small { color: var(--good); }

/* ---------- Simulator ---------- */
#sim {
  position: fixed;
  right: 14px;
  bottom: 14px;
  width: 244px;
  max-height: calc(var(--floor-h) - 20px);
  overflow: auto;
  z-index: 8;
  padding: 12px 12px 10px;
  border-radius: 18px;
  background: rgba(33, 26, 23, 0.9);
  color: #f7e9d8;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  font-size: 12px;
}
#sim .close { background: rgba(255,255,255,.1); color: #f7e9d8; }
#sim .readout { font-size: 13px; margin-bottom: 4px; }
#sim .readout strong { font-size: 18px; font-variant-numeric: tabular-nums; color: #ffd2a8; }
#sim-hinge { width: 100%; height: auto; display: block; touch-action: none; cursor: grab; }
#sim-hinge line { stroke-width: 7; stroke-linecap: round; }
#sim-floor { stroke: #c89a74; }
#sim-wall { stroke: #f2d6b6; }
#sim-arc { stroke: rgba(255,255,255,.25); stroke-width: 1.5; }
#sim-knob { fill: #ffb27e; stroke: #fff; stroke-width: 2; cursor: grab; }
#sim-hinge .pivot { fill: #3b2d26; stroke: #ffd2a8; stroke-width: 2; }
#sim-range { width: 100%; accent-color: #ffb27e; margin: 4px 0 6px; }
#sim .presets { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 8px; }
#sim .presets button {
  border: 0; border-radius: 8px; padding: 4px 7px;
  background: rgba(255,255,255,.08); color: #f7e9d8; font-size: 11px; font-weight: 800;
  font-variant-numeric: tabular-nums;
}
#sim .presets button.on { background: #ffb27e; color: #2b1d15; }
#sim .wide {
  width: 100%; border: 0; border-radius: 10px; padding: 7px 10px; margin: 0 0 6px;
  background: rgba(255, 178, 126, 0.16); color: #ffd2a8; font-size: 12px; font-weight: 800;
  display: flex; justify-content: center; align-items: center; gap: 6px;
}
#sim .wide:hover { background: rgba(255, 178, 126, 0.26); }
#sim .wide kbd { font: inherit; font-size: 10px; opacity: .6; border: 1px solid currentColor; border-radius: 4px; padding: 0 4px; }
#sim .row { display: flex; gap: 7px; align-items: center; margin: 5px 0; font-weight: 700; }
#sim .row input { accent-color: #ffb27e; }
#sim .row.disabled { opacity: .4; }
#sim-info { margin: 8px 0 0; display: grid; grid-template-columns: auto 1fr; gap: 2px 8px; font-size: 10.5px; color: rgba(247,233,216,.62); }
#sim-info dt { font-weight: 800; }
#sim-info dd { margin: 0; text-align: right; color: rgba(247,233,216,.9); }

/* ---------- Intro ---------- */
#intro {
  position: fixed; inset: 0; z-index: 10;
  display: grid; place-items: center;
  background: radial-gradient(ellipse at 50% 40%, rgba(60, 42, 34, 0.55), rgba(20, 14, 12, 0.88));
  transition: opacity .8s ease;
}
#intro.gone { opacity: 0; pointer-events: none; }
#intro .card { text-align: center; color: #fff4e6; padding: 24px; max-width: 360px; }
#intro .glyph svg { width: 120px; height: 80px; filter: drop-shadow(0 10px 20px rgba(0,0,0,.4)); }
#intro h1 { font-size: 30px; margin: 10px 0 6px; letter-spacing: -.01em; }
#intro p { margin: 0 0 20px; line-height: 1.5; color: rgba(255, 244, 230, 0.8); font-weight: 600; }
#intro button {
  border: 0; border-radius: 999px; padding: 12px 22px;
  background: #ffb27e; color: #2b1d15; font-weight: 800; font-size: 15px;
  box-shadow: 0 10px 30px rgba(255, 150, 90, 0.35);
}
#intro small { display: block; margin-top: 14px; color: rgba(255, 244, 230, 0.5); font-size: 11.5px; line-height: 1.5; }

@media (max-width: 520px) {
  #status { min-width: 112px; padding: 7px 9px; }
  #bar button { padding: 0 10px 0 8px; }
  #bar .title { display: none; }
}
@media (hover: none) {
  #sim { right: 8px; bottom: 8px; }
}
