:root {
  --ink: #2c2018;
  --parch: #e6dcc3;
  --black: #120d0b;
  --stone: #2c2018;
  --earth: #7a5230;
  --ochre: #b8843f;
  --gold: #e0b661;
  --moss: #6f7f45;
  --oxblood: #8f2f28;
  --hudh: 104px;
}

* { box-sizing: border-box; }

button {
  -webkit-tap-highlight-color: transparent;
}
button:focus { outline: none; }
button:focus-visible { outline: 2px solid var(--earth); outline-offset: 2px; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  background: var(--black);
  color: var(--parch);
  font-family: "VT323", monospace;
}

body { position: fixed; inset: 0; }

img { image-rendering: pixelated; }

#game {
  position: absolute;
  inset: 0;
  z-index: 0;
}

#game canvas {
  display: block;
  touch-action: none;
  image-rendering: pixelated;
}

#gloom {
  position: fixed;
  left: 0;
  top: 0;
  width: 560px;
  height: auto;
  z-index: 5;
  pointer-events: none;
  mix-blend-mode: multiply;
  opacity: 0.22;
  user-select: none;
}

#title {
  position: fixed;
  left: clamp(8px, 1.4vw, 22px);
  top: clamp(6px, 1.4vh, 16px);
  width: clamp(150px, min(32vw, 46vh), 420px);
  height: auto;
  z-index: 12;
  pointer-events: none;
  user-select: none;
}

/* ---------- in-world readouts ---------- */

#tip {
  position: fixed;
  z-index: 22;
  padding: 4px 8px 3px;
  background: var(--parch);
  border: 2px solid var(--ink);
  color: var(--ink);
  font-family: "VT323", monospace;
  font-size: 17px;
  line-height: 1.05;
  white-space: nowrap;
  pointer-events: none;
}

#tip b { font-family: "Silkscreen", monospace; font-weight: 700; font-size: 13px; }

#msg {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(var(--hudh) + 16px);
  margin: 0 auto;
  width: fit-content;
  z-index: 21;
  max-width: min(92vw, 620px);
  padding: 5px 14px 4px;
  background: var(--parch);
  border: 2px solid var(--ink);
  color: var(--ink);
  font-family: "VT323", monospace;
  font-size: clamp(16px, 2.1vh, 21px);
  line-height: 1.1;
  text-align: center;
  pointer-events: none;
}

#hint {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(var(--hudh) + 58px);
  margin: 0 auto;
  width: fit-content;
  z-index: 20;
  padding: 4px 12px 3px;
  background: var(--parch);
  border: 2px solid var(--ink);
  color: var(--ink);
  font-family: "Silkscreen", monospace;
  font-weight: 700;
  font-size: clamp(11px, 1.5vh, 14px);
  letter-spacing: 0.04em;
  white-space: nowrap;
  pointer-events: none;
}

.hidden { display: none !important; }

@media (orientation: portrait) {
  body.touch #msg { bottom: calc(var(--hudh) + 196px); max-width: 84vw; }
}

/* ---------- ledger strip ---------- */

#hud {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  min-height: 92px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: clamp(8px, 1.5vw, 26px);
  padding: 36px clamp(46px, 4vw, 64px) 14px;
  background-image: url("assets/ledger_hud_01_parchment_strip.webp");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  color: var(--ink);
  user-select: none;
}

#hud::before,
#hud::after {
  content: "";
  position: absolute;
  left: 1.5%;
  right: 1.5%;
  height: 2px;
  background: var(--earth);
  opacity: 0.45;
  pointer-events: none;
}
#hud::before { top: 7px; }
#hud::after { bottom: 5px; }

.hud-group { display: flex; align-items: center; gap: 6px; }

.col {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 3px;
}

.line { display: flex; align-items: center; gap: 5px; }

.hud-label {
  font-family: "Silkscreen", monospace;
  font-weight: 700;
  font-size: 10px;
  letter-spacing: 0.09em;
  line-height: 1.5;
  color: var(--ink);
  opacity: 0.8;
  white-space: nowrap;
}

.icons { display: flex; align-items: center; gap: 5px; }

.heart {
  width: clamp(22px, 3.4vh, 32px);
  height: auto;
}
.heart.off { opacity: 0.2; }

.pip {
  width: clamp(13px, 2.2vh, 20px);
  height: auto;
}
.pip.off { opacity: 0.2; }

.vitals { gap: clamp(12px, 1.8vw, 28px); align-items: flex-start; }

.coin {
  width: clamp(32px, 5.2vh, 50px);
  height: auto;
}

.num {
  font-family: "Silkscreen", monospace;
  font-weight: 700;
  font-size: clamp(18px, 3.3vh, 32px);
  line-height: 1;
  color: var(--ink);
  min-width: 1.2em;
}

#gold { min-width: 1.6em; }

.slots { display: flex; align-items: center; gap: clamp(4px, 0.8vw, 9px); }

.slot {
  position: relative;
  width: clamp(36px, 5.8vh, 54px);
  height: clamp(36px, 5.8vh, 54px);
  background-image: url("assets/ledger_hud_05_gold_tally_plate.webp");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  display: flex;
  align-items: center;
  justify-content: center;
  flex: none;
}

.slot img {
  width: 78%;
  height: 78%;
  object-fit: contain;
}

.slot.filled { cursor: help; }

.gauges { flex-direction: row; align-items: flex-start; gap: clamp(12px, 1.8vw, 26px); margin-left: auto; }

.torch-icon {
  width: clamp(24px, 3.8vh, 34px);
  height: auto;
}

.depth-group { gap: 2px; }


.depth-label {
  font-family: "Silkscreen", monospace;
  font-weight: 700;
  font-size: clamp(15px, 2.4vh, 22px);
  color: var(--ink);
}

#helpBtn {
  margin-left: auto;
  width: clamp(28px, 4.2vh, 38px);
  height: clamp(28px, 4.2vh, 38px);
  flex: none;
  background: var(--parch);
  border: 2px solid var(--ink);
  color: var(--ink);
  font-family: "Pixelify Sans", monospace;
  font-size: clamp(15px, 2.5vh, 21px);
  line-height: 1;
  cursor: pointer;
  padding: 0;
}

@media (hover: hover) {
  #helpBtn:hover { background: var(--ink); color: var(--parch); }
}

/* ---------- touch pad ---------- */

#pad {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(var(--hudh) + 12px);
  z-index: 26;
  display: none;
  justify-content: space-between;
  align-items: flex-end;
  padding: 0 12px;
  pointer-events: none;
}

body.touch #pad { display: flex; }
body.touch #hint { display: none !important; }

#dpad {
  display: grid;
  grid-template-columns: repeat(3, var(--dbtn));
  grid-template-rows: repeat(3, var(--dbtn));
  gap: 4px;
  pointer-events: auto;
  --dbtn: clamp(44px, 8.4vh, 58px);
}

.dbtn:focus,
.dbtn:focus-visible { outline: none; }

.dbtn {
  width: var(--dbtn);
  height: var(--dbtn);
  background: var(--parch);
  border: 3px solid var(--ink);
  padding: 0;
  cursor: pointer;
  position: relative;
  touch-action: none;
}

.dbtn:active { background: #c9b98f; }

.dbtn::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 0;
  height: 0;
}

.dbtn.up { grid-column: 2; grid-row: 1; }
.dbtn.left { grid-column: 1; grid-row: 2; }
.dbtn.right { grid-column: 3; grid-row: 2; }
.dbtn.down { grid-column: 2; grid-row: 3; }

.dbtn.up::before {
  transform: translate(-50%, -60%);
  border-left: 9px solid transparent;
  border-right: 9px solid transparent;
  border-bottom: 12px solid var(--ink);
}
.dbtn.down::before {
  transform: translate(-50%, -40%);
  border-left: 9px solid transparent;
  border-right: 9px solid transparent;
  border-top: 12px solid var(--ink);
}
.dbtn.left::before {
  transform: translate(-60%, -50%);
  border-top: 9px solid transparent;
  border-bottom: 9px solid transparent;
  border-right: 12px solid var(--ink);
}
.dbtn.right::before {
  transform: translate(-40%, -50%);
  border-top: 9px solid transparent;
  border-bottom: 9px solid transparent;
  border-left: 12px solid var(--ink);
}

#actBtn:focus,
#actBtn:focus-visible { outline: none; }

#actBtn {
  pointer-events: auto;
  min-width: clamp(74px, 15vw, 104px);
  height: clamp(52px, 9vh, 64px);
  background: var(--parch);
  border: 3px solid var(--ink);
  color: var(--ink);
  font-family: "Pixelify Sans", monospace;
  font-size: clamp(13px, 2vh, 17px);
  line-height: 1.05;
  letter-spacing: 0.02em;
  padding: 2px 8px;
  cursor: pointer;
  touch-action: none;
}

#actBtn:disabled { opacity: 0.38; }
#actBtn:not(:disabled):active { background: #c9b98f; }

/* ---------- cards ---------- */

#overlay, #help, #store {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(18, 13, 11, 0.78);
}

.card {
  position: relative;
  width: min(760px, 96vw);
  max-height: 92vh;
  overflow: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: var(--earth) transparent;
  padding: 22px clamp(18px, 3vw, 34px) 24px;
  background: var(--parch);
  border: 3px solid var(--ink);
  color: var(--ink);
}

.card::-webkit-scrollbar { width: 10px; }
.card::-webkit-scrollbar-track { background: transparent; }
.card::-webkit-scrollbar-thumb {
  background: var(--earth);
  border: 2px solid var(--parch);
}

.card-band {
  height: 10px;
  margin: -22px calc(clamp(18px, 3vw, 34px) * -1) 16px;
  border-bottom: 2px solid var(--ink);
  background: var(--earth);
}

.kicker {
  margin: 0 0 4px;
  font-family: "Silkscreen", monospace;
  font-weight: 700;
  font-size: 12px;
  letter-spacing: 0.14em;
  color: var(--oxblood);
}

.card h2 {
  margin: 0 0 10px;
  font-family: "Pixelify Sans", monospace;
  font-size: clamp(26px, 5vh, 42px);
  font-weight: 700;
  line-height: 1.02;
  color: var(--ink);
}

.lede {
  margin: 0 0 16px;
  font-family: "VT323", monospace;
  font-size: clamp(18px, 2.4vh, 22px);
  line-height: 1.15;
}

.stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  border: 2px solid var(--ink);
  margin-bottom: 16px;
}

.stat {
  padding: 8px 10px 7px;
  border-right: 2px solid var(--ink);
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.stat:last-child { border-right: 0; }

.stat-label {
  font-family: "VT323", monospace;
  font-size: 15px;
  letter-spacing: 0.05em;
}

.stat-value {
  font-family: "Silkscreen", monospace;
  font-weight: 700;
  font-size: clamp(20px, 3.4vh, 30px);
  line-height: 1;
}

.boards {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
  margin-bottom: 18px;
}

.board-title {
  margin: 0 0 4px;
  padding-bottom: 3px;
  border-bottom: 2px solid var(--ink);
  font-family: "Silkscreen", monospace;
  font-weight: 700;
  font-size: 12px;
  letter-spacing: 0.08em;
}

.board-list {
  margin: 0;
  padding: 0 0 0 18px;
  font-family: "VT323", monospace;
  font-size: 18px;
  line-height: 1.25;
}

.board-list li { padding: 1px 0; }
.board-list a { color: var(--oxblood); text-decoration: none; border-bottom: 1px solid var(--ink); }
.board-list .empty { list-style: none; opacity: 0.7; }
.board-list .me { font-weight: bold; }

.help-cols {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 18px;
  margin-bottom: 8px;
}

.step {
  margin: 0 0 8px;
  font-family: "VT323", monospace;
  font-size: 19px;
  line-height: 1.15;
}

.step b {
  font-family: "Silkscreen", monospace;
  font-weight: 700;
  font-size: 12px;
  letter-spacing: 0.08em;
  color: var(--oxblood);
}

.law {
  margin: 6px 0 18px;
  padding-top: 10px;
  border-top: 2px solid var(--ink);
  font-family: "VT323", monospace;
  font-size: 19px;
  font-style: italic;
}

.btn {
  display: inline-block;
  padding: 8px 18px 6px;
  background: var(--parch);
  border: 3px solid var(--ink);
  color: var(--ink);
  font-family: "Pixelify Sans", monospace;
  font-size: 18px;
  font-weight: 700;
  cursor: pointer;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

@media (hover: hover) {
  .btn:hover { background: var(--ink); color: var(--parch); }
}
.btn.big { font-size: clamp(18px, 2.6vh, 23px); padding: 10px 24px 8px; }

/* ---------- the surface store ---------- */

.store-card { width: min(1000px, 96vw); }

.store-stats { margin-bottom: 12px; }

.store-cols {
  display: grid;
  grid-template-columns: 1.15fr 1fr;
  gap: clamp(14px, 2.4vw, 28px);
  margin: 4px 0 2px;
}

.store-col { min-width: 0; }

.store-col-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  border-bottom: 2px solid var(--ink);
  margin-bottom: 2px;
}

.store-col-head .board-title { border-bottom: 0; margin: 0; padding-bottom: 2px; }

.store-col > .board-title { margin-bottom: 2px; }

.spoils-list, .stock-list { border-top: 0; }

.spoils-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 5px 0;
  border-bottom: 1px solid rgba(44, 32, 24, 0.3);
}

.spoils-row img {
  width: 30px;
  height: 30px;
  object-fit: contain;
  flex: none;
}

.spoils-name {
  flex: 1;
  min-width: 0;
  font-family: "VT323", monospace;
  font-size: 19px;
  line-height: 1.05;
}

.spoils-value {
  font-family: "Silkscreen", monospace;
  font-weight: 700;
  font-size: 13px;
  white-space: nowrap;
}

.btn.tiny {
  font-size: 14px;
  padding: 5px 10px 4px;
  border-width: 2px;
  min-height: 34px;
}

@media (max-width: 700px), (pointer: coarse) {
  .btn.tiny { min-height: 40px; padding: 7px 12px 5px; }
}

.btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

@media (hover: hover) {
  .btn:disabled:hover { background: var(--parch); color: var(--ink); }
}

.spoils-empty, .stock-empty {
  padding: 10px 0;
  font-family: "VT323", monospace;
  font-size: 18px;
  opacity: 0.7;
}

.stock-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 7px 0;
  border-bottom: 1px solid rgba(44, 32, 24, 0.3);
}

.stock-group {
  margin: 10px 0 2px;
  padding: 5px 0 4px;
  border-top: 2px solid var(--ink);
  font-family: "Silkscreen", monospace;
  font-weight: 700;
  font-size: 11px;
  letter-spacing: 0.1em;
  color: var(--oxblood);
}

.stock-group:first-child { margin-top: 2px; border-top: 0; padding-top: 0; }

.stock-info { flex: 1; min-width: 0; }

.stock-name {
  margin: 0;
  font-family: "VT323", monospace;
  font-size: 19px;
  line-height: 1.05;
}

.stock-name .lvl {
  font-family: "Silkscreen", monospace;
  font-size: 10px;
  font-weight: 700;
  margin-left: 6px;
  opacity: 0.75;
}

.stock-desc {
  margin: 0;
  font-family: "VT323", monospace;
  font-size: 15px;
  line-height: 1.05;
  opacity: 0.8;
}

.stock-buy { display: flex; flex-direction: column; align-items: flex-end; gap: 2px; flex: none; }

.stock-cost {
  font-family: "Silkscreen", monospace;
  font-size: 11px;
  font-weight: 700;
  white-space: nowrap;
}

.store-note {
  min-height: 1.25em;
  margin: 12px 0 2px;
  font-family: "VT323", monospace;
  font-size: 18px;
  line-height: 1.1;
  color: var(--oxblood);
}

.store-foot {
  position: sticky;
  bottom: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 14px;
  margin: 10px 0 0;
  padding: 10px 0 8px;
  background: var(--parch);
  border-top: 2px solid var(--ink);
}

.store-purse { display: flex; align-items: center; gap: 8px; }

.store-purse .coin { width: 38px; }

.store-purse .num { font-size: clamp(22px, 3.8vh, 34px); display: block; }

.store-purse .hud-label { display: block; font-size: 9px; margin-bottom: 1px; }

.store-boards { border-top: 2px solid var(--ink); padding-top: 12px; margin-bottom: 0; }

/* ---------- narrow screens ---------- */

@media (max-width: 700px) {
  #hud {
    flex-wrap: wrap;
    align-content: center;
    gap: 8px;
    padding: 30px 24px 8px;
    justify-content: center;
  }
  .hud-label { font-size: 8px; }
  .heart { width: 18px; }
  .pip { width: 10px; }
  .coin { width: 26px; }
  .num { font-size: 16px; }
  .torch-icon { width: 18px; }
  .depth-label { font-size: 14px; }
  #helpBtn { order: 5; margin-left: 0; width: 30px; height: 30px; font-size: 16px; }
  .vitals { gap: 10px; align-items: center; }
  .icons { gap: 3px; }
  .gauges { gap: 10px; align-items: center; margin-left: 0; }
  .pack { order: 6; flex-basis: 100%; align-items: center; }
  .slots { justify-content: center; gap: 4px; }
  .slot { width: 34px; height: 34px; }
  .card h2 { font-size: 26px; }
  .stats { grid-template-columns: 1fr 1fr; }
  .stat:nth-child(2) { border-right: 0; }
  .stat:nth-child(1), .stat:nth-child(2) { border-bottom: 2px solid var(--ink); }
  .boards, .help-cols, .store-cols { grid-template-columns: 1fr; }
  .store-card { padding: 18px 14px 20px; }
  .store-foot { align-items: flex-start; }
  .store-purse .coin { width: 32px; }
  .stock-row { align-items: flex-start; }
  .card { padding: 18px 14px 20px; }
  .card-band { margin: -18px -14px 14px; }
}

@media (max-height: 520px) {
  #title { width: clamp(120px, min(22vw, 34vh), 240px); }
  #hud { min-height: 64px; padding: 30px 34px 8px; }
  .hud-label { font-size: 8px; }
  .heart { width: 20px; }
  .pip { width: 11px; }
  .coin { width: 28px; }
  .num { font-size: 18px; }
  .slot { width: 34px; height: 34px; }
  .torch-icon { width: 20px; }
  .depth-label { font-size: 14px; }
  #dpad { --dbtn: 40px; }
  #actBtn { min-width: 74px; height: 44px; font-size: 13px; }
  .card { max-height: 96vh; }

  #store .store-card { padding: 12px 18px 12px; }
  #store .card-band { height: 6px; margin: -12px -18px 8px; }
  #store .kicker { display: none; }
  #store h2 { font-size: 22px; margin-bottom: 6px; }
  #store .lede { font-size: 16px; margin-bottom: 8px; }
  #store .store-stats { margin-bottom: 8px; }
  #store .stat { padding: 4px 8px; }
  #store .stat-label { font-size: 11px; }
  #store .stat-value { font-size: 18px; }
  #store .store-cols { gap: 14px; }
  #store .stock-row { padding: 5px 0; }
  #store .stock-name { font-size: 17px; }
  #store .stock-desc { font-size: 13px; }
  #store .store-note { margin: 6px 0 0; font-size: 15px; min-height: 1em; }
  #store .store-foot { margin-top: 6px; padding: 4px 0 2px; }
  #store .store-purse .coin { width: 26px; }
  #store .store-purse .num { font-size: 20px; }
  #store .btn.big { font-size: 16px; padding: 6px 16px 4px; }
}
