/* ==========================================================================
   GAME LAYOUT — canvas full-screen, UI floats on top (portrait & landscape)
   Loaded after style.css. Theme tokens (--ink, --lamp, ...) come from style.css.
   ========================================================================== */
:root {
  --gap: 8px;
  --safe-t: max(var(--gap), env(safe-area-inset-top));
  --safe-r: max(var(--gap), env(safe-area-inset-right));
  --safe-b: max(var(--gap), env(safe-area-inset-bottom));
  --safe-l: max(var(--gap), env(safe-area-inset-left));
  --pad: 150px;           /* d-pad size */
  --btn-a: 80px;
  --btn-b: 58px;
  --btn-s: 54px;
  --ctrl-h: var(--pad);   /* height reserved by bottom controls */
  --glass: rgba(13, 11, 26, 0.72);
  --tasks-w: 118px;       /* right task sidebar */
}

#app { position: fixed; inset: 0; overflow: hidden; }
#stage { position: absolute; inset: 0; background: var(--night-900); }
#game {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}
canvas.portrait { image-rendering: pixelated; image-rendering: crisp-edges; display: block; }

/* floating layers (hidden outside a shift) */
#hud, #tasks, #dpad, #ab { position: absolute; z-index: 3; }
body:not([data-screen='play']):not([data-screen='pause']):not([data-screen='dialog']):not([data-screen='shop']) :is(#hud, #tasks, #dpad, #ab) { visibility: hidden; }

/* ---------- HUD ---------- */
#hud {
  top: var(--safe-t);
  left: var(--safe-l);
  right: var(--safe-r);
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 6px 8px;
  background: var(--glass);
  border: 3px solid var(--ink);
  outline: 2px solid var(--night-500);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
}
.hud-top { display: flex; align-items: center; gap: 8px; min-width: 0; }
#hud-face canvas { width: 22px !important; height: 32px !important; }
.hud-name { flex: 1; min-width: 0; color: var(--lamp); font-size: 9px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lvl { display: inline-block; background: var(--cyan); color: var(--ink); padding: 2px 4px; font-size: 7px; vertical-align: 1px; }
.rank { color: var(--muted); font-size: 6px; }
.hud-right { display: flex; flex-direction: column; align-items: flex-end; gap: 3px; }
#hud-clock { font-size: 14px; color: var(--lamp); text-shadow: 2px 2px 0 var(--ink); }
#hud-money { font-size: 8px; color: var(--green); }
.hud-btns { display: flex; gap: 4px; }
.mini {
  font-size: 8px;
  min-width: 40px;
  min-height: 28px;
  padding: 4px 6px;
  background: var(--night-700);
  border: 2px solid var(--ink);
  color: var(--paper);
}
.mini:hover { background: var(--night-500); }
.mini.zoom { min-width: 30px; padding: 4px; font-size: 11px; }

.track { flex: 1; height: 8px; min-width: 0; background: var(--ink); border: 1px solid var(--night-500); }
.fill { height: 100%; width: 50%; transition: width 0.25s steps(6); }
.fill.aman[data-level='hi'] { background: var(--green); }
.fill.aman[data-level='mid'] { background: var(--lamp); }
.fill.aman[data-level='lo'] { background: var(--pink); animation: blink 0.5s steps(2) infinite; }
.fill.kantuk { background: #a37bff; }
.fill.kantuk[data-level='lo'] { background: var(--pink); animation: blink 0.4s steps(2) infinite; }
.fill.xp { background: var(--cyan); }
.fill.hp { background: #ff4f6a; }
.fill.hp[data-level='lo'] { animation: blink 0.4s steps(2) infinite; }
.fill.sp { background: #4f8bff; }
.fill.sta { background: var(--green); }

.hud-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.sbar { display: flex; align-items: center; gap: 4px; font-size: 7px; min-width: 0; }
.sbar span { width: 20px; color: var(--paper); flex: none; }
.sbar .track { height: 9px; }
.sbar em { font-style: normal; font-size: 6px; color: var(--muted); width: 18px; text-align: right; flex: none; }
.hud-world { display: grid; grid-template-columns: 1fr 1fr; gap: 3px 10px; }
.meter { display: flex; align-items: center; gap: 6px; min-width: 0; }
.meter .lbl { font-size: 6px; width: 46px; color: var(--muted); flex: none; }
.hud-world .xpm { grid-column: 1 / -1; }
.hud-world .xpm .track { height: 4px; }
.hud-world .xpm em { font-style: normal; font-size: 7px; color: var(--cyan); }
#hud-score { font-size: 7px; }

/* ---------- stage messages ---------- */
.banner {
  position: absolute;
  z-index: 4;
  left: 50%;
  top: 30%;
  transform: translate(-50%, -20px) scale(0.9);
  background: var(--lamp);
  color: var(--ink);
  border: 3px solid var(--ink);
  box-shadow: 4px 4px 0 var(--ink);
  padding: 8px 10px;
  font-size: 9px;
  line-height: 1.6;
  text-align: center;
  width: max-content;
  max-width: 92%;
  opacity: 0;
  transition: opacity 0.2s, transform 0.2s steps(3);
  pointer-events: none;
}
.banner.show { opacity: 1; transform: translate(-50%, 0) scale(1); }
.toast {
  position: absolute;
  z-index: 4;
  left: var(--safe-l);
  right: calc(var(--safe-r) + var(--tasks-w) + 8px);
  bottom: calc(var(--safe-b) + var(--pad) + 10px);
  transform: translateY(10px);
  background: var(--paper);
  color: var(--ink);
  border: 3px solid var(--ink);
  box-shadow: 3px 3px 0 var(--ink);
  padding: 7px 9px;
  font-size: 8px;
  line-height: 1.7;
  opacity: 0;
  transition: opacity 0.2s, transform 0.2s;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.danger { border-left: 8px solid var(--pink); }
.toast.ok { border-left: 8px solid var(--green); }
.toast.info { border-left: 8px solid var(--cyan); }

/* ---------- task sidebar (right edge, vertical, translucent) ---------- */
#tasks {
  top: calc(var(--safe-t) + var(--hud-h, 92px) + 8px);
  right: var(--safe-r);
  bottom: calc(var(--safe-b) + var(--pad) + 10px);
  width: var(--tasks-w);
  display: flex;
  flex-direction: column;
  gap: 5px;
  pointer-events: none;           /* only the chips catch taps; the map stays reachable */
}
#tasks > * { pointer-events: auto; }
.tasks-head { pointer-events: none !important; font-size: 7px; color: var(--paper); text-shadow: 1px 1px 0 var(--ink); text-align: right; opacity: 0.8; }
.tasks-head b { color: var(--lamp); font-weight: normal; }
.task-quick { display: grid; grid-template-columns: 1fr 1fr; gap: 5px; }
.task-list { display: flex; flex-direction: column; gap: 5px; min-height: 0; overflow-y: auto; scrollbar-width: none; }
.task-list::-webkit-scrollbar { display: none; }
#tasks.expanded .task-list { padding-bottom: 2px; }

.chip {
  --c: var(--lamp);
  position: relative;
  flex: none;
  display: flex;
  align-items: center;
  gap: 5px;
  width: 100%;
  min-height: 38px;
  padding: 4px 6px 7px;
  background: rgba(13, 11, 26, 0.6);
  border: 2px solid var(--ink);
  box-shadow: 2px 2px 0 rgba(13, 11, 26, 0.7);
  text-align: left;
  opacity: 0.55;
  transition: opacity 0.15s;
}
.chip[hidden], .task-more[hidden] { display: none; }
.chip:hover, .chip:focus-visible, .chip.tracked, .chip.urgent { opacity: 1; }
.chip:active { transform: translate(2px, 2px); box-shadow: none; }
.chip .ic { flex: none; width: 18px; height: 18px; display: grid; place-items: center; background: var(--c); color: var(--ink); border: 2px solid var(--ink); font-size: 9px; }
.chip .tx { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.chip b { font-size: 7px; font-weight: normal; color: var(--c); white-space: nowrap; }
.chip small { font-size: 5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chip .tm { position: absolute; left: 3px; right: 3px; bottom: 2px; height: 2px; background: var(--ink); }
.chip .tm u { display: block; height: 100%; width: 100%; background: var(--c); text-decoration: none; }
.chip.tracked { background: rgba(59, 47, 143, 0.85); border-color: var(--c); }
.chip.urgent .ic { animation: blink 0.4s steps(2) infinite; }
.chip.quick { flex-direction: column; justify-content: center; gap: 2px; min-height: 40px; padding: 4px 2px 6px; text-align: center; }
.chip.quick .tx { align-items: center; }
.chip.quick small { font-size: 5px; }
.task-more {
  flex: none;
  font-size: 7px;
  padding: 6px 4px;
  background: rgba(13, 11, 26, 0.6);
  color: var(--lamp);
  border: 2px solid var(--ink);
  opacity: 0.75;
}
.task-more:hover, .task-more:active { opacity: 1; }

/* ---------- RPG controls (floating over the map) ---------- */
#dpad { left: var(--safe-l); bottom: var(--safe-b); width: var(--pad); height: var(--pad); touch-action: none; }
.d {
  position: absolute;
  width: calc(var(--pad) / 3);
  height: calc(var(--pad) / 3);
  background: rgba(36, 28, 92, 0.72);
  border: 3px solid var(--ink);
  box-shadow: 3px 3px 0 rgba(13, 11, 26, 0.8);
  font-size: 15px;
  color: var(--paper);
  touch-action: none;
  padding: 0;
}
.d.up { left: calc(var(--pad) / 3); top: 0; }
.d.down { left: calc(var(--pad) / 3); bottom: 0; }
.d.left { left: 0; top: calc(var(--pad) / 3); }
.d.right { right: 0; top: calc(var(--pad) / 3); }
.d-center { position: absolute; left: calc(var(--pad) / 3); top: calc(var(--pad) / 3); width: calc(var(--pad) / 3); height: calc(var(--pad) / 3); background: rgba(27, 21, 68, 0.72); border: 3px solid var(--ink); pointer-events: none; }
.d.ul, .d.ur, .d.dl, .d.dr { background: transparent; border: 0; box-shadow: none; }
.d.ul { left: 0; top: 0; } .d.ur { right: 0; top: 0; } .d.dl { left: 0; bottom: 0; } .d.dr { right: 0; bottom: 0; }
.d.is-down { background: rgba(59, 47, 143, 0.9); transform: translate(2px, 2px); box-shadow: 1px 1px 0 var(--ink); }

#ab { right: var(--safe-r); bottom: var(--safe-b); height: var(--pad); display: flex; align-items: flex-end; gap: 10px; }
.ab button {
  border-radius: 50%;
  border: 4px solid var(--ink);
  box-shadow: 4px 4px 0 rgba(13, 11, 26, 0.8);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  touch-action: none;
  color: var(--ink);
  padding: 4px;
  text-align: center;
  opacity: 0.92;
}
.ab small { font-size: 6px; line-height: 1.3; max-width: 64px; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.ab .a { width: var(--btn-a); height: var(--btn-a); font-size: 22px; background: var(--lamp); margin-bottom: calc(var(--pad) - var(--btn-a)); }
.ab .a.ready { animation: pulse 0.7s steps(2) infinite; }
.ab .b { width: var(--btn-b); height: var(--btn-b); font-size: 16px; background: var(--pink); }
.ab .s { width: var(--btn-s); height: var(--btn-s); font-size: 15px; background: #4f8bff; color: var(--paper); margin-bottom: calc(var(--btn-b) + 10px); }
.ab .s.ready { animation: pulse 0.9s steps(2) infinite; }
.ab .s.empty { filter: grayscale(0.8) brightness(0.7); }
.ab button.is-down { transform: translate(3px, 3px); box-shadow: 1px 1px 0 var(--ink); }

/* small phones */
@media (max-height: 700px) and (orientation: portrait) {
  :root { --pad: 126px; --btn-a: 70px; --btn-b: 50px; --btn-s: 46px; }
  .d { font-size: 13px; }
}

/* ---------- LANDSCAPE: thin HUD bar, tasks between the thumbs ---------- */
body[data-orientation='landscape'] {
  --pad: 132px;
  --btn-a: 72px;
  --btn-b: 52px;
  --btn-s: 48px;
  --ctrl-h: 0px;
}
body[data-orientation='landscape'] #hud { flex-direction: row; align-items: center; flex-wrap: wrap; gap: 4px 12px; padding: 4px 8px; }
body[data-orientation='landscape'] .hud-top { flex: 0 0 auto; }
body[data-orientation='landscape'] .hud-name { flex: 0 1 auto; max-width: 150px; }
body[data-orientation='landscape'] .rank { display: none; }
body[data-orientation='landscape'] .hud-stats { flex: 1 1 240px; }
body[data-orientation='landscape'] .hud-world { flex: 1 1 260px; grid-template-columns: 1fr 1fr 1fr; }
body[data-orientation='landscape'] .hud-world .xpm { grid-column: auto; }
body[data-orientation='landscape'] .hud-right { flex-direction: row; align-items: center; gap: 8px; }
body[data-orientation='landscape'] { --tasks-w: 124px; }
body[data-orientation='landscape'] .toast { top: calc(var(--safe-t) + var(--hud-h, 56px) + 8px); bottom: auto; left: calc(var(--safe-l) + var(--pad) + 12px); }
body[data-orientation='landscape'] .banner { top: 36%; }

/* desktop keyboard + mouse (no touch detected by ui/layout.js): no virtual pad */
body:not(.touch) #dpad, body:not(.touch) #ab { display: none; }
body:not(.touch) { --tasks-w: 150px; }
body:not(.touch) #tasks { bottom: var(--safe-b); }
body:not(.touch) .toast, body:not(.touch)[data-orientation] .toast { top: auto; left: var(--safe-l); bottom: var(--safe-b); }
body:not(.touch) .chip b { font-size: 8px; }
body:not(.touch) .chip small { font-size: 6px; }
