/* ==========================================================
   BLACKSTREAM // forest — base styles
   The look: dark CRT box, pixel-scaled scene, VHS overlay.
   ========================================================== */

:root {
  --bg: #050807;
  --fg: #9bd6a8;
  --dim: #2a3a30;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  width: 100%;
  height: 100%;
  background: #000;
  overflow: hidden;
  font-family: "Courier New", ui-monospace, monospace;
  color: var(--fg);
  cursor: default;
}

/* The "TV" container. Keeps a 4:3-ish feel by default but stretches. */
#screen {
  position: fixed;
  inset: 0;
  background: #000;
  overflow: hidden;
}

/* Low-res scene canvas, scaled to fill via CSS for that chunky pixel look.
   Preserve the internal 320:200 (1.6:1) aspect ratio so sprites and walls
   aren't horizontally stretched on widescreen displays \u2014 letterbox with
   black bars via the parent's black background. */
#scene {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  /* Fill whichever dimension is the binding constraint while keeping 320:200. */
  width:  min(100vw, calc(100vh * (320 / 200)));
  height: min(100vh, calc(100vw * (200 / 320)));
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  /* slight curvature illusion */
  filter: contrast(1.05) saturate(0.85);
}

/* VHS overlay: matches the scene canvas exactly (same letterbox). */
#vhs {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width:  min(100vw, calc(100vh * (320 / 200)));
  height: min(100vh, calc(100vw * (200 / 320)));
  pointer-events: none;
  mix-blend-mode: screen;
}

/* HUD: classic camcorder OSD */
#hud {
  position: absolute;
  inset: 0;
  pointer-events: none;
  font-size: 14px;
  letter-spacing: 1px;
  text-shadow: 0 0 3px rgba(155, 214, 168, 0.6);
}
.hud-line {
  position: absolute;
  top: 16px;
  left: 18px;
  color: #c8ffd1;
  opacity: 0.85;
}
.blink {
  color: #ff3a3a;
  animation: blink 1.1s steps(2, end) infinite;
}
@keyframes blink {
  50% { opacity: 0; }
}

/* Vignette + scanline overlay applied to the whole screen. */
#screen::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(ellipse at center,
      rgba(0,0,0,0) 55%,
      rgba(0,0,0,0.55) 85%,
      rgba(0,0,0,0.95) 100%),
    repeating-linear-gradient(
      to bottom,
      rgba(0,0,0,0) 0px,
      rgba(0,0,0,0) 2px,
      rgba(0,0,0,0.25) 3px,
      rgba(0,0,0,0) 4px);
  z-index: 5;
}

/* BGM toggle button */
#btn-bgm {
  position: absolute;
  top: 72px;
  left: 18px;
  z-index: 4;
  pointer-events: auto;
  font-family: monospace;
  font-size: 12px;
  color: #00ff41;
  background: rgba(0, 0, 0, 0.6);
  border: 1px solid rgba(0, 255, 65, 0.4);
  border-radius: 2px;
  padding: 4px 10px;
  cursor: pointer;
  text-shadow: 0 0 6px rgba(0, 255, 65, 0.6);
  transition: all 0.2s;
}
#btn-bgm:hover {
  background: rgba(0, 255, 65, 0.1);
  border-color: rgba(0, 255, 65, 0.6);
}
#btn-bgm.off {
  color: #5a6a5a;
  border-color: rgba(90, 106, 90, 0.4);
  text-shadow: none;
}
#btn-bgm.off:hover {
  border-color: rgba(90, 106, 90, 0.6);
  background: rgba(90, 106, 90, 0.1);
}

/* Minimap overlay. */
#minimap {
  position: absolute;
  top: 16px;
  right: 18px;
  z-index: 4;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  border: 1px solid rgba(46, 230, 107, 0.85);
  border-radius: 2px;
  box-shadow: 0 0 10px rgba(46, 230, 107, 0.25), 0 0 8px rgba(0, 0, 0, 0.45);
}

/* Boot screen (hidden once the player presses anything). */
#boot {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #000;
  z-index: 10;
  color: #9bd6a8;
  text-shadow: 0 0 6px rgba(155, 214, 168, 0.5);
}
#boot pre {
  font-size: 16px;
  line-height: 1.6;
  white-space: pre;
}
.boot-pass {
  margin-top: 12px;
  font-size: 14px;
  font-family: monospace;
  display: flex;
  align-items: center;
  gap: 6px;
}
.boot-pass label {
  white-space: nowrap;
}
.boot-pass input {
  background: transparent;
  border: 1px solid #3a5a40;
  color: #9bd6a8;
  padding: 4px 8px;
  font-family: monospace;
  font-size: 14px;
  outline: none;
  width: 200px;
}
.boot-pass input:focus {
  border-color: #6ab87a;
}
.boot-pass-error {
  color: #d44a4a;
  font-size: 12px;
  margin-left: 4px;
}
.boot-pass-error.hidden { display: none; }
.boot-warning {
  position: absolute;
  bottom: 18px;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 13px;
  font-family: monospace;
}
#boot.hidden { display: none; }

@media (max-width: 480px) {
  #boot pre {
    font-size: 11px;
    line-height: 1.3;
  }
  .boot-pass {
    font-size: 11px;
    gap: 4px;
  }
  .boot-pass input {
    font-size: 11px;
    width: 140px;
    padding: 3px 6px;
  }
  .boot-pass-error {
    font-size: 10px;
  }
  .boot-warning {
    font-size: 10px;
    bottom: 8px;
  }
}

/* Loading screen (between password and game start) */
#loading-screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #000;
  z-index: 10;
  color: #9bd6a8;
  text-shadow: 0 0 6px rgba(155, 214, 168, 0.5);
}
#loading-screen.hidden { display: none; }
#loading-screen pre {
  font-size: 16px;
  line-height: 1.6;
  white-space: pre;
  margin-bottom: 20px;
}
.load-bar {
  width: 280px;
  height: 6px;
  background: #1a2a1a;
  border: 1px solid #2a3a30;
  overflow: hidden;
}
.load-fill {
  height: 100%;
  width: 0%;
  background: #9bd6a8;
  box-shadow: 0 0 8px rgba(155, 214, 168, 0.5);
  transition: width 0.3s ease;
}
.load-text {
  margin-top: 10px;
  font-size: 13px;
  font-family: monospace;
  color: #5a6a5a;
}

/* Dialogue system styles */
#dialogue-container {
  position: absolute;
  inset: 0;
  z-index: 15;
  background: rgba(0, 0, 0, 0.7);
}

#dialogue-container.hidden {
  display: none;
}

/* Layer that hosts positioned character sprites. Fills the area
   above the dialogue box; sprites use % coordinates relative to it. */
#dialogue-sprite-layer {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 25vh; /* leave room for the dialogue box */
  overflow: hidden;
  pointer-events: none;
}

.dialogue-sprite {
  position: absolute;
  height: 80%;
  width: auto;
  /* Bottom-center anchor; JS overrides transform per line to add scale/flip. */
  transform: translate(-50%, -100%);
  transition:
    left     0.35s cubic-bezier(.4,.0,.2,1),
    top      0.35s cubic-bezier(.4,.0,.2,1),
    transform 0.35s cubic-bezier(.4,.0,.2,1),
    filter   0.25s ease,
    opacity  0.25s ease;
  filter: drop-shadow(0 0 25px rgba(155, 214, 168, 0.8));
  will-change: left, top, transform, filter;
  pointer-events: none;
}

.dialogue-sprite.dim {
  filter: brightness(0.35) saturate(0.6) drop-shadow(0 0 10px rgba(0,0,0,0.9));
  opacity: 0.75;
}

#dialogue-box {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 25vh;
  padding: 16px 40px;
  background: rgba(5, 8, 7, 0.98);
  border-top: 2px solid #2a3a30;
  box-shadow: 0 0 30px rgba(0, 0, 0, 0.9), inset 0 0 30px rgba(155, 214, 168, 0.15);
  display: flex;
  flex-direction: column;
  justify-content: center;
}

#dialogue-speaker {
  font-family: monospace;
  font-size: 20px;
  color: #8affab;
  text-shadow: 0 0 8px rgba(88, 255, 142, 0.6);
  margin-bottom: 8px;
  display: none;
}

#dialogue-text {
  font-size: 28px;
  line-height: 1.6;
  color: #c8ffd1;
  text-shadow: 0 0 10px rgba(155, 214, 168, 0.7);
  margin: 0;
  text-align: left;
}

/* Inventory system styles - sidebar layout */
#inventory-container {
  position: absolute;
  top: 50%;
  right: 18px;
  transform: translateY(-50%);
  z-index: 4;
  width: 180px;
  max-height: calc(100vh - 120px);
  background: rgba(5, 8, 7, 0.9);
  border: 1px solid rgba(42, 58, 48, 0.8);
  border-radius: 4px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.8);
  padding: 12px;
  overflow-y: auto;
}

#inventory-container::-webkit-scrollbar {
  width: 4px;
}

#inventory-container::-webkit-scrollbar-track {
  background: rgba(0, 0, 0, 0.3);
}

#inventory-container::-webkit-scrollbar-thumb {
  background: #2a3a30;
  border-radius: 2px;
}

.inventory-header {
  font-family: monospace;
  font-size: 14px;
  color: #00ff41;
  text-shadow: 0 0 6px rgba(0, 255, 65, 0.5);
  border-bottom: 1px solid #2a3a30;
  padding-bottom: 6px;
  margin-bottom: 10px;
}

.inventory-items {
  max-height: none;
  overflow-y: visible;
}

.item-icon {
  font-size: 18px;
  flex-shrink: 0;
}
.item-icon-img {
  width: 20px;
  height: 20px;
  object-fit: contain;
}

.item-name {
  font-size: 13px;
  color: #c8ffd1;
  text-shadow: 0 0 4px rgba(155, 214, 168, 0.4);
  flex: 1;
}

.item-qty {
  font-size: 12px;
  color: #00ff41;
  text-shadow: 0 0 4px rgba(0, 255, 65, 0.4);
}

.inventory-empty {
  font-family: monospace;
  font-size: 12px;
  color: #5a6a5a;
  text-align: center;
  padding: 15px;
}

/* Item obtained toast animation */
@keyframes toastFade {
  0%   { opacity: 0; transform: translateX(-50%) translateY(20px); }
  15%  { opacity: 1; transform: translateX(-50%) translateY(0); }
  80%  { opacity: 1; transform: translateX(-50%) translateY(0); }
  100% { opacity: 0; transform: translateX(-50%) translateY(-20px); }
}

/* Item detail view styles */
#item-detail-container {
  position: absolute;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.85);
}

#item-detail-container.hidden {
  display: none;
}

.item-detail-box {
  width: 90%;
  max-width: 560px;
  background: rgba(5, 8, 7, 0.98);
  border: 2px solid #2a3a30;
  border-radius: 4px;
  padding: 24px;
  box-shadow: 0 0 40px rgba(155, 214, 168, 0.2), inset 0 0 30px rgba(0, 0, 0, 0.5);
}

.item-detail-layout {
  display: flex;
  align-items: center;
  gap: 20px;
}

.item-detail-icon {
  flex-shrink: 0;
  font-size: 64px;
  filter: drop-shadow(0 0 15px rgba(155, 214, 168, 0.6));
}
.item-detail-icon-img {
  width: 120px;
  height: 120px;
  object-fit: contain;
}

.item-detail-info {
  flex: 1;
  text-align: center;
  min-width: 0;
}

.item-detail-actions {
  display: flex;
  justify-content: center;
  gap: 8px;
}

.item-detail-name {
  font-family: monospace;
  font-size: 24px;
  color: #c8ffd1;
  text-shadow: 0 0 10px rgba(155, 214, 168, 0.7);
  margin-bottom: 8px;
}

.item-detail-text {
  font-family: monospace;
  font-size: 14px;
  color: #9bd6a8;
  line-height: 1.8;
  margin-bottom: 20px;
  padding: 12px;
  background: rgba(0, 0, 0, 0.4);
  border-radius: 3px;
  border: 1px solid rgba(42, 58, 48, 0.5);
}

.item-detail-close {
  font-family: monospace;
  font-size: 12px;
  color: #5a6a5a;
  padding-top: 12px;
  border-top: 1px solid #2a3a30;
}

.item-detail-drop {
  font-family: monospace;
  font-size: 14px;
  color: #ff6b6b;
  background: rgba(255, 107, 107, 0.1);
  border: 1px solid rgba(255, 107, 107, 0.4);
  padding: 8px 16px;
  margin-top: 16px;
  border-radius: 3px;
  cursor: pointer;
  transition: all 0.2s;
  text-shadow: 0 0 4px rgba(255, 107, 107, 0.4);
}

.item-detail-drop:hover {
  background: rgba(255, 107, 107, 0.2);
  border-color: rgba(255, 107, 107, 0.6);
  box-shadow: 0 0 10px rgba(255, 107, 107, 0.3);
}

.item-detail-drop:active {
  transform: scale(0.98);
  background: rgba(255, 107, 107, 0.3);
}

/* Inventory item button styles */
.inventory-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px;
  margin-bottom: 4px;
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(42, 58, 48, 0.5);
  border-radius: 3px;
  font-family: monospace;
  color: inherit;
  cursor: pointer;
  transition: all 0.2s;
}

.inventory-item:hover {
  background: rgba(42, 58, 48, 0.4);
  border-color: #4a5a4a;
  box-shadow: 0 0 10px rgba(155, 214, 168, 0.2);
}

.inventory-item:active {
  transform: scale(0.98);
}

.item-detail-use {
  font-family: monospace;
  font-size: 14px;
  color: #00ff41;
  background: rgba(0, 255, 65, 0.1);
  border: 1px solid rgba(0, 255, 65, 0.4);
  padding: 8px 16px;
  margin-top: 16px;
  margin-right: 8px;
  border-radius: 3px;
  cursor: pointer;
  transition: all 0.2s;
  text-shadow: 0 0 4px rgba(0, 255, 65, 0.4);
}

.item-detail-use:hover {
  background: rgba(0, 255, 65, 0.2);
  border-color: rgba(0, 255, 65, 0.6);
  box-shadow: 0 0 10px rgba(0, 255, 65, 0.3);
}

.item-detail-use:active {
  transform: scale(0.98);
}

#chest-password-container {
  position: absolute;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.85);
}

.chest-password-box {
  width: auto;
  min-width: 400px;
  max-width: 500px;
  background: rgba(5, 8, 7, 0.98);
  border: 2px solid #2a3a30;
  border-radius: 4px;
  padding: 24px;
  box-shadow: 0 0 40px rgba(155, 214, 168, 0.2), inset 0 0 30px rgba(0, 0, 0, 0.5);
  text-align: center;
}

.chest-password-title {
  font-family: monospace;
  font-size: 20px;
  color: #c8ffd1;
  text-shadow: 0 0 10px rgba(155, 214, 168, 0.7);
  margin-bottom: 24px;
}

.chest-password-display {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-bottom: 24px;
}

.chest-digit {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.chest-arrow {
  width: 40px;
  height: 30px;
  font-size: 16px;
  color: #9bd6a8;
  background: rgba(155, 214, 168, 0.1);
  border: 1px solid rgba(155, 214, 168, 0.3);
  border-radius: 3px;
  cursor: pointer;
  transition: all 0.2s;
  text-shadow: 0 0 4px rgba(155, 214, 168, 0.4);
}

.chest-arrow:hover {
  background: rgba(155, 214, 168, 0.2);
  border-color: rgba(155, 214, 168, 0.5);
  box-shadow: 0 0 8px rgba(155, 214, 168, 0.3);
}

.chest-arrow:active {
  transform: scale(0.95);
}

.chest-digit-value {
  width: 60px;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: monospace;
  font-size: 32px;
  color: #c8ffd1;
  background: rgba(0, 0, 0, 0.5);
  border: 2px solid #2a3a30;
  border-radius: 4px;
  margin: 8px 0;
  text-shadow: 0 0 10px rgba(155, 214, 168, 0.7);
  box-shadow: inset 0 0 15px rgba(0, 0, 0, 0.5);
}

.chest-password-confirm {
  font-family: monospace;
  font-size: 14px;
  color: #00ff41;
  background: rgba(0, 255, 65, 0.15);
  border: 1px solid rgba(0, 255, 65, 0.5);
  padding: 10px 24px;
  margin-right: 12px;
  border-radius: 3px;
  cursor: pointer;
  transition: all 0.2s;
  text-shadow: 0 0 4px rgba(0, 255, 65, 0.5);
}

.chest-password-confirm:hover {
  background: rgba(0, 255, 65, 0.25);
  border-color: rgba(0, 255, 65, 0.7);
  box-shadow: 0 0 12px rgba(0, 255, 65, 0.4);
}

.chest-password-confirm:active {
  transform: scale(0.98);
}

.chest-password-cancel {
  font-family: monospace;
  font-size: 14px;
  color: #ff6b6b;
  background: rgba(255, 107, 107, 0.1);
  border: 1px solid rgba(255, 107, 107, 0.4);
  padding: 10px 24px;
  border-radius: 3px;
  cursor: pointer;
  transition: all 0.2s;
  text-shadow: 0 0 4px rgba(255, 107, 107, 0.4);
}

.chest-password-cancel:hover {
  background: rgba(255, 107, 107, 0.2);
  border-color: rgba(255, 107, 107, 0.6);
  box-shadow: 0 0 10px rgba(255, 107, 107, 0.3);
}

.chest-password-cancel:active {
  transform: scale(0.98);
}

.chest-password-error {
  font-family: monospace;
  font-size: 13px;
  color: #ff6b6b;
  margin-top: 16px;
  text-shadow: 0 0 6px rgba(255, 107, 107, 0.6);
  min-height: 18px;
}

/* --- End screen (flashback sequence) --- */
#end-flashback {
  position: absolute;
  inset: 0;
  z-index: 100;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
}
#end-flashback.hidden { display: none; }
#end-flashback img {
  max-width: 90%;
  max-height: 90%;
  object-fit: contain;
  opacity: 0;
  transition: opacity 0.15s ease-in-out;
}
#end-flashback img.visible {
  opacity: 1;
}

/* ==========================================================
   Mobile / Touch Controls
   ========================================================== */

/* Portrait warning — shown only on mobile in portrait */
#portrait-warning {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 999;
  background: #000;
  align-items: center;
  justify-content: center;
}
#portrait-warning > div {
  font-family: monospace;
  font-size: 22px;
  color: #00ff41;
  text-shadow: 0 0 12px rgba(0, 255, 65, 0.5);
  text-align: center;
  line-height: 1.6;
}
@media (orientation: portrait) and (max-width: 1024px) and (pointer: coarse) {
  #portrait-warning { display: flex; }
}

/* Touch controls — visible only on touch devices in landscape */
#touch-controls {
  display: none;
  position: fixed;
  bottom: 8px;
  left: 0;
  right: 0;
  z-index: 90;
  justify-content: space-between;
  align-items: flex-end;
  padding: 0 10px;
  pointer-events: none;
}

@media (orientation: landscape) and (pointer: coarse) {
  #touch-controls { display: flex; }

  /* Minimap — use viewport width so it scales proportionally across devices */
  #minimap {
    top: 6px;
    right: 6px;
    width: clamp(80px, 25vw, 130px);
    height: auto;
  }

  /* Inventory — shrink and anchor to viewport width */
  #inventory-container {
    width: clamp(120px, 18vw, 180px);
    right: 6px;
    padding: 8px;
    max-height: calc(100vh - 80px);
  }
  .inventory-header    { font-size: 11px; padding-bottom: 4px; margin-bottom: 6px; }
  .inventory-item      { gap: 4px; padding: 4px; margin-bottom: 3px; }
  .item-icon           { font-size: 14px; }
  .item-icon-img       { width: 16px; height: 16px; }
  .item-name           { font-size: 10px; }
  .item-qty            { font-size: 9px; }
  .inventory-empty     { font-size: 10px; padding: 8px; }

  /* Item detail popup — scale down icons and text */
  .item-detail-icon-img { width: 64px; height: 64px; }
  .item-detail-icon     { font-size: 36px; }
  .item-detail-name     { font-size: 18px; margin-bottom: 4px; }
  .item-detail-text     { font-size: 12px; padding: 8px; margin-bottom: 12px; line-height: 1.6; }
  .item-detail-layout   { gap: 12px; }
  .item-detail-close    { font-size: 10px; padding-top: 8px; }
  .item-detail-drop,
  .item-detail-use      { font-size: 11px; padding: 6px 12px; margin-top: 10px; }

  #hud .hud-line { font-size: 9px; top: 6px; left: 8px; }
  #btn-bgm { font-size: 8px; top: 42px; left: 8px; padding: 2px 6px; }
}

.touch-group {
  pointer-events: auto;
  display: flex;
}

/* D-pad cross layout */
.touch-dpad {
  display: grid;
  grid-template-columns: 40px 40px 40px;
  grid-template-rows: 40px 40px 40px;
  gap: 3px;
}
.touch-up    { grid-column: 2; grid-row: 1; }
.touch-left  { grid-column: 1; grid-row: 2; }
.touch-right { grid-column: 3; grid-row: 2; }
.touch-down  { grid-column: 2; grid-row: 3; }

/* Action buttons (right side) */
.touch-actions {
  flex-direction: column;
  gap: 6px;
  margin-bottom: 6px;
}

.touch-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: 1.5px solid rgba(0, 255, 65, 0.45);
  border-radius: 8px;
  background: rgba(5, 8, 7, 0.75);
  color: #00ff41;
  font-size: 16px;
  font-family: monospace;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  transition: background 0.1s;
}
.touch-btn:active,
.touch-btn.active {
  background: rgba(0, 255, 65, 0.25);
  border-color: rgba(0, 255, 65, 0.8);
}

.touch-inv {
  width: 40px;
  height: 30px;
  font-size: 9px;
  border-color: rgba(0, 255, 65, 0.3);
  color: rgba(0, 255, 65, 0.7);
}

/* Hide touch controls during dialogue and before game starts */
#touch-controls.hidden-dialogue,
#touch-controls.hidden {
  display: none !important;
}
