:root {
  --bark: #3b2415;
  --wood: #8a5530;
  --honey: #f2c46d;
  --honey-dark: #c98a3a;
  --parchment: #f7e3b5;
  --parchment-light: #fcf0cf;
  --moss: #5f8a3a;
  --dusk: #2b1d14;
  --font: 'Pixelify Sans', ui-rounded, 'Trebuchet MS', system-ui, sans-serif;
  --st: env(safe-area-inset-top, 0px);
  --sb: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden; background: var(--dusk);
  font-family: var(--font); color: var(--bark);
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  overscroll-behavior: none; touch-action: none;
}
canvas#game {
  position: fixed; left: 0; top: 0; display: block; touch-action: none;
  image-rendering: -webkit-optimize-contrast; image-rendering: crisp-edges; image-rendering: pixelated;
}
button { font-family: var(--font); color: var(--bark); touch-action: manipulation; cursor: pointer; }
img { image-rendering: pixelated; }

/* ---------- HUD ---------- */
#hud {
  position: fixed; left: 0; right: 0; top: calc(var(--st) + 10px); padding: 0 12px;
  display: flex; justify-content: space-between; align-items: flex-start; gap: 8px;
  transition: opacity .8s ease; opacity: 0; pointer-events: none;
}
body.playing #hud { opacity: 1; pointer-events: auto; }
.plank {
  display: flex; align-items: center; gap: 8px; padding: 6px 12px 6px 8px;
  background: var(--parchment); border: 2px solid var(--bark); border-radius: 6px;
  box-shadow: inset 0 -3px 0 #e2c48a, inset 0 2px 0 var(--parchment-light), 0 3px 0 rgba(20, 10, 5, .35);
  font-size: 16px; line-height: 1;
}
#clockIcon { width: 21px; height: 21px; }
#clockDay { font-weight: 700; }
#clockTime { opacity: .8; min-width: 62px; }
.hud-btns { display: flex; gap: 8px; }
.square {
  width: 44px; height: 44px; padding: 0; display: grid; place-items: center;
  background: var(--parchment); border: 2px solid var(--bark); border-radius: 6px;
  box-shadow: inset 0 -3px 0 #e2c48a, inset 0 2px 0 var(--parchment-light), 0 3px 0 rgba(20, 10, 5, .35);
}
.square:active { transform: translateY(2px); box-shadow: inset 0 -1px 0 #e2c48a, 0 1px 0 rgba(20, 10, 5, .35); }
.square img { width: 27px; height: auto; }

/* ---------- Buttons ---------- */
.wood {
  display: inline-flex; align-items: center; justify-content: center; gap: 10px;
  background: var(--honey); border: 2px solid var(--bark); border-radius: 7px;
  box-shadow: inset 0 -4px 0 var(--honey-dark), inset 0 2px 0 #fde4a6, 0 4px 0 rgba(20, 10, 5, .4);
  font-size: 19px; font-weight: 500; padding: 12px 20px 14px;
}
.wood:active { transform: translateY(2px); box-shadow: inset 0 -2px 0 var(--honey-dark), 0 2px 0 rgba(20, 10, 5, .4); }
.wood.small { font-size: 16px; padding: 9px 14px 11px; }

/* ---------- Action button ---------- */
#actionWrap {
  position: fixed; left: 0; right: 0; bottom: calc(var(--sb) + 22px); display: flex; justify-content: center;
  pointer-events: none; opacity: 0; transform: translateY(12px); transition: opacity .25s ease, transform .25s ease;
}
#actionWrap.show { opacity: 1; transform: none; }
#actionWrap.show #actionBtn { pointer-events: auto; }
#actionBtn { min-width: 190px; padding: 10px 22px 12px 14px; }
#actionIcon { width: 34px; height: auto; }
#actionBtn.hot { background: #ffd98a; animation: nudge .5s ease-in-out infinite alternate; }
@keyframes nudge { to { transform: scale(1.06); } }

/* ---------- Toasts ---------- */
#toasts {
  position: fixed; left: 0; right: 0; top: calc(var(--st) + 66px); display: flex; flex-direction: column; align-items: center; gap: 6px;
  pointer-events: none; padding: 0 16px;
}
.toast {
  display: flex; align-items: center; gap: 10px; max-width: 340px;
  background: rgba(252, 240, 207, .96); border: 2px solid var(--bark); border-radius: 6px; padding: 7px 12px 8px 8px;
  font-size: 15px; line-height: 1.3; box-shadow: 0 3px 0 rgba(20, 10, 5, .3);
  animation: toastIn .35s ease; transition: opacity .6s ease, transform .6s ease;
}
.toast img { width: 26px; height: auto; flex: none; }
.toast.out { opacity: 0; transform: translateY(-6px); }
@keyframes toastIn { from { opacity: 0; transform: translateY(-8px); } }

/* ---------- Resting (sitting) ---------- */
body.resting #hud, body.resting #actionWrap { opacity: 0; pointer-events: none; }
body.resting #toasts { top: calc(var(--st) + 14px); }
#quote {
  position: fixed; left: 24px; right: 24px; top: 30%; margin: 0; text-align: center;
  font-size: 22px; line-height: 1.35; color: #fff4dc; text-shadow: 2px 2px 0 rgba(43, 29, 20, .75);
  opacity: 0; transition: opacity 1.6s ease; pointer-events: none; text-wrap: balance;
}
#quote.show { opacity: .95; }
#sitPanel {
  position: fixed; left: 0; right: 0; bottom: calc(var(--sb) + 20px); display: flex; justify-content: center; gap: 8px; flex-wrap: wrap; padding: 0 10px;
  opacity: 0; pointer-events: none; transition: opacity 1s ease .3s;
}
#sitPanel.show { opacity: 1; pointer-events: auto; }
#sitPanel .wood { background: rgba(242, 196, 109, .92); }

/* ---------- Breathing ---------- */
#breath {
  position: fixed; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 22px;
  background: rgba(24, 16, 34, .38); opacity: 0; pointer-events: none; transition: opacity .8s ease;
}
#breath.show { opacity: 1; pointer-events: auto; }
#breath .ring {
  width: 200px; height: 200px; border-radius: 50%; transform: scale(.55);
  border: 3px solid rgba(255, 238, 205, .85);
  background: radial-gradient(circle, rgba(255, 214, 150, .5), rgba(255, 190, 120, .08) 70%);
  box-shadow: 0 0 40px rgba(255, 200, 130, .35);
}
#breathText { margin: 0; font-size: 24px; color: #fff4dc; text-shadow: 2px 2px 0 rgba(43, 29, 20, .7); }
#breath .hint { margin: 0; font-size: 14px; color: rgba(255, 244, 220, .6); }

/* ---------- Overlays ---------- */
.overlay {
  position: fixed; inset: 0; display: flex; align-items: flex-end; justify-content: center;
  background: rgba(30, 18, 10, .45); opacity: 0; pointer-events: none; transition: opacity .25s ease;
  padding: calc(var(--st) + 16px) 12px calc(var(--sb) + 16px);
}
.overlay.show { opacity: 1; pointer-events: auto; }
.panel {
  width: 100%; max-width: 440px; background: var(--parchment); border: 3px solid var(--wood); border-radius: 8px;
  box-shadow: 0 0 0 3px var(--bark), inset 0 0 0 3px var(--parchment-light), 0 10px 0 rgba(20, 10, 5, .35);
  padding: 18px 16px 16px; transform: translateY(20px); transition: transform .3s ease;
}
.overlay.show .panel { transform: none; }
.panel h2 { margin: 0 0 12px; font-size: 21px; font-weight: 700; }
.sheet-panel { display: flex; flex-direction: column; gap: 8px; }
#sheetBody { display: flex; flex-direction: column; gap: 8px; }
.opt {
  display: flex; align-items: center; gap: 12px; text-align: left; padding: 10px 12px;
  background: var(--parchment-light); border: 2px solid #c9a46a; border-radius: 6px;
}
.opt:active { background: #fff6dc; border-color: var(--bark); }
.opt img { width: 40px; height: auto; flex: none; }
.opt-text { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.opt-text b { font-size: 18px; font-weight: 500; }
.opt-text small { font-size: 14px; opacity: .7; }
.opt-count { font-size: 16px; opacity: .75; }
.sheet-panel > .wood { align-self: center; margin-top: 6px; }

.journal-panel { display: flex; flex-direction: column; max-height: 82vh; }
.tabs { display: flex; gap: 6px; margin-bottom: 12px; }
.tab {
  flex: 1; padding: 8px 4px 10px; font-size: 16px; border-radius: 6px 6px 3px 3px;
  background: #e8cf98; border: 2px solid #b08a52; color: rgba(59, 36, 21, .75);
}
.tab.on { background: var(--parchment-light); border-color: var(--bark); color: var(--bark); font-weight: 700; }
#journalBody { overflow-y: auto; -webkit-overflow-scrolling: touch; touch-action: pan-y; min-height: 160px; padding-right: 2px; }
#journalBody h3 { font-size: 16px; margin: 12px 0 8px; font-weight: 500; opacity: .8; }
#journalBody h3:first-child { margin-top: 0; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(58px, 1fr)); gap: 8px; }
.cell {
  position: relative; aspect-ratio: 1; display: grid; place-items: center;
  background: var(--parchment-light); border: 2px solid #c9a46a; border-radius: 6px; padding: 0;
}
.cell img { width: 36px; height: auto; }
.cell span { position: absolute; right: 4px; bottom: 1px; font-size: 14px; font-weight: 700; }
.cell.unknown img { filter: brightness(0); opacity: .18; }
.empty { font-size: 17px; line-height: 1.45; opacity: .75; text-align: center; padding: 30px 10px; margin: 0; }
#journalCaption { min-height: 42px; margin: 10px 0; font-size: 15px; line-height: 1.4; opacity: .85; }
.journal-panel > .wood { align-self: center; }
.moments { display: grid; grid-template-columns: 1fr auto; gap: 10px 16px; margin: 0; font-size: 17px; }
.moments dt { opacity: .8; }
.moments dd { margin: 0; font-weight: 700; text-align: right; }

#letter { align-items: center; }
.paper {
  width: 100%; max-width: 380px; padding: 26px 24px 20px; border-radius: 3px;
  background: repeating-linear-gradient(to bottom, #fbf1dc 0 29px, #ead9b4 29px 30px), #fbf1dc;
  box-shadow: 0 0 0 2px #d8bf8e, 0 12px 0 rgba(20, 10, 5, .3); transform: rotate(-1.2deg) translateY(20px); transition: transform .35s ease;
  display: flex; flex-direction: column;
}
#letter.show .paper { transform: rotate(-1.2deg); }
#letterText { margin: 0; font-size: 19px; line-height: 30px; }
#letterFrom { margin: 12px 0 18px; font-size: 18px; text-align: right; color: #8a3a26; }
.paper .wood { align-self: center; }

#catch { align-items: center; }
.catch-panel { text-align: center; position: relative; max-width: 320px; }
.catch-panel img { width: 96px; height: auto; margin: 6px 0 10px; }
.catch-panel h2 { margin-bottom: 6px; }
.catch-panel p { margin: 0; font-size: 16px; opacity: .8; line-height: 1.4; }
.badge { position: absolute; top: -14px; right: 14px; background: var(--moss); color: #fff4dc; border: 2px solid var(--bark); border-radius: 5px; padding: 3px 10px 5px; font-size: 15px; }

#fade { position: fixed; inset: 0; background: #0e0a14; opacity: 0; pointer-events: none; transition: opacity 1s ease; }
#fade.on { opacity: 1; pointer-events: auto; }

/* ---------- Title ---------- */
#title {
  position: fixed; inset: 0; display: flex; flex-direction: column; justify-content: space-between; align-items: center;
  padding: calc(var(--st) + 14vh) 24px calc(var(--sb) + 9vh);
  background: linear-gradient(to bottom, rgba(43, 29, 20, .55), rgba(43, 29, 20, 0) 45%, rgba(43, 29, 20, 0) 60%, rgba(43, 29, 20, .65));
  transition: opacity 1.1s ease; cursor: pointer;
}
#title.gone { opacity: 0; pointer-events: none; }
.title-top, .title-bottom { text-align: center; }
#title h1 {
  margin: 0; font-size: clamp(46px, 14vw, 76px); font-weight: 700; letter-spacing: .01em; line-height: 1;
  color: #ffeec4; text-shadow: 3px 3px 0 #8a4a26, 6px 6px 0 var(--bark);
}
#title .sub { margin: 14px 0 0; font-size: 19px; color: #fff4dc; text-shadow: 2px 2px 0 rgba(43, 29, 20, .8); }
#title .start { margin: 0; font-size: 22px; color: #fff4dc; text-shadow: 2px 2px 0 var(--bark); animation: breathe 2.6s ease-in-out infinite; }
#title .install { margin: 14px 0 0; font-size: 14px; color: rgba(255, 244, 220, .75); max-width: 280px; line-height: 1.4; }
@keyframes breathe { 50% { opacity: .45; } }

@media (prefers-reduced-motion: reduce) {
  #title .start, #actionBtn.hot { animation: none; }
}
button:focus-visible { outline: 3px solid #fff4dc; outline-offset: 2px; }
#fade.quick { transition: opacity .35s ease; }
body.indoor #quote { top: calc(var(--st) + 11%); }

/* ---------- Full-bleed on iPhone ---------- */
html { height: 100%; min-height: 100vh; min-height: calc(100% + env(safe-area-inset-top, 0px)); background: #140d09; }
body { position: fixed; left: 0; top: 0; width: 100%; height: var(--app-h, 100%); transform: translateZ(0); background: #140d09; }

/* ---------- Wandering path ---------- */
#banner {
  position: fixed; left: 16px; right: 16px; top: 24%; text-align: center; pointer-events: none; opacity: 0;
}
#banner.show { animation: banner 4.2s ease forwards; }
#bannerTitle { margin: 0; font-size: 34px; font-weight: 700; color: #fff1cf; text-shadow: 2px 2px 0 #8a4a26, 4px 4px 0 var(--bark); }
#bannerSub { margin: 8px 0 0; font-size: 17px; color: #fff4dc; text-shadow: 2px 2px 0 rgba(43, 29, 20, .8); }
@keyframes banner { 0% { opacity: 0; transform: translateY(8px); } 15%, 75% { opacity: 1; transform: none; } 100% { opacity: 0; } }
#sheetBody { max-height: 58vh; overflow-y: auto; -webkit-overflow-scrolling: touch; touch-action: pan-y; }
.tab { font-size: 14px; }
.places { display: flex; flex-direction: column; gap: 8px; }
.place { display: flex; align-items: center; gap: 10px; padding: 8px 10px; background: var(--parchment-light); border: 2px solid #c9a46a; border-radius: 6px; }
.place img { width: 21px; flex: none; }
.place-text { flex: 1; display: flex; flex-direction: column; gap: 2px; }
.place-text b { font-size: 17px; font-weight: 500; }
.place-text small { font-size: 13px; opacity: .7; }
.place .wood.small { font-size: 14px; padding: 6px 10px 8px; }
