/* ==========================================================================
   EMBERFALL — retro frozen-survival theme
   ========================================================================== */

:root {
  --bg:        #060811;
  --bg2:       #0a0f1e;
  --panel:     rgba(13, 19, 34, 0.92);
  --panel-2:   rgba(16, 24, 42, 0.96);
  --line:      #2a3352;
  --line-2:    #3b4a6e;
  --ink:       #e8eef7;
  --ink-dim:   #8a94ab;
  --ice:       #9fd3e8;
  --ice-deep:  #5b8bd6;
  --fire:      #ffb347;
  --fire-hi:   #ffd98a;
  --fire-lo:   #d9602a;
  --ember-glow:#ff8a3d;
  --gold:      #ffd98a;
  --danger:    #e0718b;
  --good:      #6fc96b;
  --px-font:   'Press Start 2P', monospace;
  --body-font: 'VT323', monospace;
}

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

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--body-font);
  font-size: 20px;
  line-height: 1.35;
  overflow: hidden;
  -webkit-font-smoothing: none;
  overscroll-behavior: none;
}

#app {
  position: relative;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

/* ---------------- CRT scanlines ---------------- */
.scanlines {
  position: fixed; inset: 0; pointer-events: none; z-index: 9000;
  background: repeating-linear-gradient(
    to bottom,
    rgba(0,0,0,0) 0px, rgba(0,0,0,0) 2px,
    rgba(0,0,0,0.14) 3px, rgba(0,0,0,0.14) 3px
  );
  mix-blend-mode: multiply;
}
.scanlines::after {
  content: ""; position: absolute; inset: 0;
  background: radial-gradient(ellipse at center, rgba(255,255,255,0.03) 0%, rgba(0,0,0,0.28) 100%);
}

/* ---------------- screens ---------------- */
.screen { display: none; position: absolute; inset: 0; min-width: 0; min-height: 0; }
.screen.active { display: flex; }
.overlay {
  z-index: 100;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  padding:
    max(12px, env(safe-area-inset-top))
    max(12px, env(safe-area-inset-right))
    max(12px, env(safe-area-inset-bottom))
    max(12px, env(safe-area-inset-left));
  background: rgba(3,5,10,0.82);
}

.bg-canvas {
  position: absolute; inset: 0; width: 100%; height: 100%;
  object-fit: cover; image-rendering: pixelated;
}

canvas { image-rendering: pixelated; }

/* ---------------- typography ---------------- */
.eyebrow {
  font-family: var(--px-font); font-size: 9px; letter-spacing: 2px;
  color: var(--ice-deep); margin-bottom: 14px;
}
.game-title {
  font-family: var(--px-font); font-size: clamp(22px, 9vw, 58px);
  line-height: 1.15; color: #fff;
  text-shadow: 0 0 10px rgba(255,143,61,0.55), 0 0 40px rgba(255,143,61,0.35);
  letter-spacing: 2px; margin-bottom: 14px;
}
.tagline {
  font-family: var(--px-font); font-size: 10px; line-height: 1.8;
  color: var(--ink-dim); letter-spacing: 1px; margin-bottom: 22px;
}
.fineprint {
  font-family: var(--px-font); font-size: 7px; letter-spacing: 1px;
  color: var(--ink-dim); margin-top: 20px; line-height: 1.9;
}
.dim { color: var(--ink-dim); }

/* ---------------- panels ---------------- */
.panel {
  background: var(--panel);
  border: 2px solid var(--line);
  border-radius: 6px;
  box-shadow: 0 0 0 2px rgba(0,0,0,0.4), 0 8px 30px rgba(0,0,0,0.5);
}
.panel-title {
  font-family: var(--px-font); font-size: 8px; letter-spacing: 2px;
  color: var(--ice); padding: 8px 10px 6px; border-bottom: 1px solid var(--line);
}

/* ---------------- title screen ---------------- */
#screen-title {
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding:
    max(20px, env(safe-area-inset-top))
    max(20px, env(safe-area-inset-right))
    max(20px, env(safe-area-inset-bottom))
    max(20px, env(safe-area-inset-left));
  overflow-y: auto;
}
.title-inner { position: relative; z-index: 2; max-width: 460px; width: 100%; }
.roster-box { margin: 6px auto 4px; padding: 10px 12px; text-align: left; }
.roster-head {
  font-family: var(--px-font); font-size: 8px; letter-spacing: 1px;
  color: var(--ice); margin-bottom: 10px;
}
.roster { display: flex; flex-direction: column; gap: 7px; }
.roster-row { display: flex; align-items: center; gap: 10px; }
.roster-face {
  width: 26px; height: 26px; border-radius: 4px; border: 2px solid var(--line-2);
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; font-family: var(--px-font);
}
.roster-name { font-size: 19px; color: var(--ink); }
.roster-role { font-size: 15px; color: var(--ink-dim); margin-left: 4px; }
.title-actions { display: flex; flex-direction: column; gap: 10px; margin-top: 12px; }

/* ---------------- buttons ---------------- */
.btn {
  font-family: var(--px-font); font-size: 11px; letter-spacing: 1px;
  padding: 14px 16px; color: var(--ink); cursor: pointer;
  background: var(--panel-2); border: 2px solid var(--line-2); border-radius: 5px;
  text-transform: uppercase; transition: transform 0.05s ease; min-height: 46px;
  touch-action: manipulation; -webkit-tap-highlight-color: transparent;
}
.btn:hover { border-color: var(--ice-deep); }
.btn:active { transform: translateY(2px); }
.btn.primary {
  background: linear-gradient(180deg, #ff8a3d, #d9602a);
  border-color: #ffd98a; color: #1a0b00; text-shadow: 0 1px 0 rgba(255,255,255,0.35);
}
.btn.ghost { background: rgba(16,24,42,0.7); }
.btn.big { width: 100%; }
.btn.tiny { font-size: 13px; padding: 8px 10px; min-height: 34px; }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn .sub { display: block; font-size: 7px; color: inherit; opacity: 0.7; margin-top: 3px; font-family: var(--body-font); letter-spacing: 0; }

/* ---------------- how-to / intro ---------------- */
.howto-card, .intro-card {
  max-width: 520px; width: 100%; padding: 22px; text-align: left;
  max-height: 100%; overflow-y: auto; overscroll-behavior: contain; position: relative; z-index: 2;
}
.event-stamp {
  font-family: var(--px-font); font-size: 8px; letter-spacing: 3px;
  color: var(--fire); margin-bottom: 12px;
}
.event-title {
  font-family: var(--px-font); font-size: 14px; line-height: 1.5;
  color: #fff; margin-bottom: 12px; letter-spacing: 1px;
}
.howto-body { display: flex; flex-direction: column; gap: 10px; margin-bottom: 18px; font-size: 19px; color: var(--ink); }
.howto-body b { color: var(--fire-hi); }
.intro-text { font-size: 21px; margin-bottom: 22px; min-height: 4em; }

/* ---------------- game screen ---------------- */
#screen-game { flex-direction: column; overflow: hidden; }
.scene-canvas { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }

.hud {
  position: relative; z-index: 3; display: flex; align-items: center; gap: 12px;
  padding:
    calc(8px + env(safe-area-inset-top))
    max(10px, env(safe-area-inset-right))
    8px
    max(10px, env(safe-area-inset-left));
  background: rgba(6,8,16,0.86); border-bottom: 2px solid var(--line);
  flex-wrap: wrap;
}
.hud-left { display: flex; gap: 14px; align-items: center; }
.hud-item { display: flex; flex-direction: column; gap: 2px; min-width: 36px; }
.hud-item .label { font-family: var(--px-font); font-size: 7px; color: var(--ink-dim); letter-spacing: 1px; }
.hud-item .value { font-family: var(--px-font); font-size: 13px; color: var(--ink); }
.hud-item.weather .value { color: var(--ice); }
.ember-meter { flex: 1; display: flex; align-items: center; gap: 8px; min-width: 160px; }
.ember-label { font-family: var(--px-font); font-size: 7px; color: var(--fire); letter-spacing: 1px; }
.ember-bar {
  flex: 1; height: 16px; background: rgba(0,0,0,0.5);
  border: 1px solid var(--line-2); border-radius: 3px; overflow: hidden;
}
.ember-fill {
  height: 100%; width: 55%;
  background: linear-gradient(90deg, #5a1c14, #d9602a 55%, #ffd98a);
  box-shadow: 0 0 12px rgba(255,138,61,0.7);
  transition: width 0.35s ease;
}
.ember-val { font-family: var(--px-font); font-size: 11px; color: var(--fire-hi); min-width: 24px; text-align: right; }
.hud-actions { display: flex; gap: 6px; }

.game-body {
  position: relative; z-index: 2; flex: 1; display: flex; gap: 10px;
  padding:
    10px
    max(10px, env(safe-area-inset-right))
    max(10px, env(safe-area-inset-bottom))
    max(10px, env(safe-area-inset-left));
  min-height: 0;
  overflow: hidden;
}
.side { width: 180px; display: flex; flex-direction: column; overflow-y: auto; flex-shrink: 0; min-height: 0; }
.res-list { padding: 8px 10px; display: flex; flex-direction: column; gap: 7px; }
.res-row { display: flex; align-items: center; gap: 7px; font-size: 19px; }
.res-row .glyph { width: 20px; text-align: center; }
.res-name { color: var(--ink-dim); flex: 1; }
.res-val { font-family: var(--px-font); font-size: 12px; color: var(--ink); }
.res-row.flash .res-val { color: var(--fire); }
.party { padding: 8px 10px 10px; display: flex; flex-direction: column; gap: 7px; }
.member { display: flex; align-items: center; gap: 8px; font-size: 17px; }
.member-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.member-dot.healthy { background: var(--good); box-shadow: 0 0 6px var(--good); }
.member-dot.cold { background: var(--ice-deep); box-shadow: 0 0 6px var(--ice-deep); }
.member-dot.frostbitten { background: var(--danger); box-shadow: 0 0 8px var(--danger); }
.member-dot.ill { background: #c9a14f; box-shadow: 0 0 6px #c9a14f; }
.member-dot.dead { background: #333; }
.member-name { color: var(--ink); }
.member-cond { margin-left: auto; color: var(--ink-dim); font-size: 15px; }
.member.dead .member-name { text-decoration: line-through; color: var(--ink-dim); }

.center-col {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}
.route-strip { padding: 8px 10px; display: flex; align-items: center; gap: 5px; overflow-x: auto; flex-shrink: 0; }
.route-node { width: 11px; height: 11px; border-radius: 50%; background: #1a2333; border: 1px solid var(--line); flex-shrink: 0; }
.route-node.visited { background: var(--ice-deep); border-color: var(--ice); }
.route-node.current { background: var(--fire); border-color: var(--fire-hi); box-shadow: 0 0 8px var(--fire); }
.route-node.next { background: #3b4a6e; }
.route-label { font-family: var(--px-font); font-size: 7px; color: var(--ink-dim); letter-spacing: 1px; margin-right: 4px; flex-shrink: 0; }
.journal {
  flex: 1; overflow-y: auto; padding: 10px 12px; min-height: 0;
  font-size: 19px; line-height: 1.5;
}
.journal .jline { padding: 3px 0; border-bottom: 1px dotted rgba(90,139,214,0.15); }
.journal .jline.j-key { color: var(--ice); }
.journal .jline.j-good { color: var(--good); }
.journal .jline.j-bad { color: var(--danger); }
.journal .jline.j-fire { color: var(--fire-hi); }
.journal .jline.j-day { font-family: var(--px-font); font-size: 8px; color: var(--ink-dim); letter-spacing: 1px; padding-top: 4px; }

.actions { padding: 10px; display: grid; gap: 8px; }
.actions.row2 { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); padding: 0; }
#actions { flex: 0 0 auto; }
.actions .btn { font-size: 10px; }

/* ---------------- modals / cards ---------------- */
.event-card, .trade-card {
  max-width: 520px; width: 100%; padding: 22px; text-align: left;
  max-height: 100%; overflow-y: auto; overscroll-behavior: contain; position: relative; z-index: 2;
}
.event-art { font-size: 44px; line-height: 1; margin: 4px 0 12px; filter: drop-shadow(0 0 10px rgba(255,143,61,0.4)); }
.event-body { font-size: 21px; margin-bottom: 18px; color: var(--ink); }
.event-choices { display: flex; flex-direction: column; gap: 10px; }
.event-choices .btn { text-align: left; line-height: 1.4; }
.event-result { font-size: 20px; margin-top: 16px; padding: 12px; border-left: 3px solid var(--fire); background: rgba(255,143,61,0.08); }

.mini-card {
  max-width: 560px;
  width: 100%;
  max-height: 100%;
  padding: 16px;
  text-align: center;
  overflow-y: auto;
  overscroll-behavior: contain;
}
.mini-head { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 10px; gap: 10px; flex-wrap: wrap; }
.mini-title { font-family: var(--px-font); font-size: 12px; color: var(--fire-hi); letter-spacing: 1px; }
.mini-status { font-family: var(--px-font); font-size: 8px; color: var(--ink-dim); letter-spacing: 1px; }
.mini-card canvas { width: 100%; height: auto; border: 2px solid var(--line); border-radius: 5px; background: #05060c; max-height: 54dvh; }
.mini-hint { font-size: 17px; color: var(--ink-dim); margin: 10px 0; }
.mini-card .btn { margin-top: 4px; }

.trade-scrip { font-family: var(--px-font); font-size: 11px; color: var(--gold); margin-bottom: 12px; letter-spacing: 1px; }
.trade-list { display: flex; flex-direction: column; gap: 10px; margin-bottom: 16px; }
.trade-row { display: flex; align-items: center; gap: 10px; padding: 8px; border: 1px solid var(--line); border-radius: 5px; }
.trade-row .t-info { flex: 1; }
.trade-row .t-name { font-size: 19px; color: var(--ink); }
.trade-row .t-price { font-size: 15px; color: var(--ink-dim); }
.trade-row .t-q { display: flex; gap: 6px; align-items: center; }
.trade-row .t-q .btn { font-size: 12px; padding: 6px 10px; min-height: 34px; }

/* ---------------- end screen ---------------- */
#screen-end {
  overflow-y: auto;
  align-items: center;
  justify-content: center;
  padding:
    max(20px, env(safe-area-inset-top))
    max(20px, env(safe-area-inset-right))
    max(20px, env(safe-area-inset-bottom))
    max(20px, env(safe-area-inset-left));
}
.end-inner { max-width: 520px; width: 100%; padding: 24px; text-align: center; position: relative; z-index: 2; }
.end-badge { font-size: 56px; margin-bottom: 8px; }
.end-badge.ivictory { text-shadow: 0 0 30px rgba(255,143,61,0.8); }
.end-title { font-family: var(--px-font); font-size: 18px; line-height: 1.5; color: #fff; margin-bottom: 10px; }
.end-sub { font-size: 20px; color: var(--ink-dim); margin-bottom: 18px; }
.end-summary { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 16px; text-align: left; }
.end-stat { padding: 10px; border: 1px solid var(--line); border-radius: 5px; }
.end-stat .es-k { font-family: var(--px-font); font-size: 7px; color: var(--ink-dim); letter-spacing: 1px; margin-bottom: 4px; }
.end-stat .es-v { font-family: var(--px-font); font-size: 13px; color: var(--ice); }
.end-log { max-height: 160px; overflow-y: auto; text-align: left; padding: 10px 12px; font-size: 17px; margin-bottom: 16px; }
.end-actions { display: flex; flex-direction: column; gap: 10px; }

/* ---------------- responsive ---------------- */
@media (max-width: 640px) {
  .hud {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 6px 8px;
    padding:
      calc(6px + env(safe-area-inset-top))
      max(8px, env(safe-area-inset-right))
      6px
      max(8px, env(safe-area-inset-left));
  }
  .hud-left { grid-column: 1; grid-row: 1; min-width: 0; gap: 10px; }
  .hud-actions { grid-column: 2; grid-row: 1; }
  .ember-meter { grid-column: 1 / -1; grid-row: 2; min-width: 0; }

  .game-body {
    flex-direction: column;
    gap: 6px;
    padding:
      6px
      max(6px, env(safe-area-inset-right))
      max(6px, env(safe-area-inset-bottom))
      max(6px, env(safe-area-inset-left));
  }
  .side {
    width: 100%;
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    flex: 0 0 auto;
    overflow: hidden;
  }
  .side > .panel-title { display: none; }
  .res-list, .party {
    min-width: 0;
    flex-direction: row;
    flex-wrap: nowrap;
    gap: 10px;
    padding: 5px 8px;
    overflow-x: auto;
    overflow-y: hidden;
    overscroll-behavior-inline: contain;
    touch-action: pan-x;
    scrollbar-width: none;
  }
  .res-list::-webkit-scrollbar, .party::-webkit-scrollbar { display: none; }
  .res-list { border-bottom: 1px solid var(--line); }
  .res-row { width: auto; flex: 0 0 auto; gap: 4px; font-size: 17px; }
  .res-row .glyph { width: auto; }
  .res-name { display: none; }
  #res-gear { font-size: 0; }
  #res-gear::after { content: attr(data-tier); font-family: var(--px-font); font-size: 12px; }
  .member { width: auto; flex: 0 0 auto; gap: 5px; font-size: 16px; }
  .member-cond { display: none; }

  .center-col { gap: 6px; }
  .route-strip { padding: 5px 8px; }
  .journal { padding: 8px 10px; font-size: 18px; }
  #actions { padding: 8px; gap: 6px; }
  .actions.row2 { gap: 6px; }
}

@media (pointer: coarse) {
  .btn { min-height: 48px; }
  .btn.tiny { min-width: 48px; min-height: 48px; padding: 8px; }
}

@media (max-height: 700px) {
  #screen-end { justify-content: flex-start; }
}

@media (max-height: 520px) {
  .hud {
    gap: 6px;
    padding:
      calc(4px + env(safe-area-inset-top))
      max(6px, env(safe-area-inset-right))
      4px
      max(6px, env(safe-area-inset-left));
    flex-wrap: nowrap;
  }
  .hud-left { gap: 10px; }
  .hud-item .label { font-size: 6px; }
  .hud-item .value { font-size: 10px; }
  .ember-meter { min-width: 100px; }

  .game-body {
    gap: 6px;
    padding:
      6px
      max(6px, env(safe-area-inset-right))
      max(6px, env(safe-area-inset-bottom))
      max(6px, env(safe-area-inset-left));
  }
  .center-col { gap: 6px; }
  .route-strip { padding: 4px 8px; }
  .journal { padding: 6px 8px; font-size: 17px; line-height: 1.3; }
  #actions { padding: 6px; gap: 6px; }
  .actions.row2 { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; }
  #actions .btn { min-height: 48px; padding: 8px 6px; font-size: 8px; letter-spacing: 0; }
  #actions .sub { display: none; }
  .mini-card canvas { max-height: 40dvh; }
  .event-card, .trade-card, .howto-card, .intro-card { padding: 14px; }
}

@media (orientation: landscape) and (max-width: 640px) and (max-height: 520px) {
  .game-body { flex-direction: row; }
  .side {
    width: 140px;
    display: flex;
    flex: 0 0 140px;
    flex-direction: column;
    overflow-x: hidden;
    overflow-y: auto;
  }
  .side > .panel-title { display: block; padding: 4px 6px; }
  .res-list, .party {
    flex-direction: column;
    gap: 4px;
    padding: 5px 6px;
    overflow: visible;
    touch-action: pan-y;
  }
  .res-list { border-bottom: 0; }
  .res-row { width: 100%; font-size: 15px; }
  .res-name { display: block; }
  .member { width: 100%; font-size: 15px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}