@font-face {
  font-family: "Unscii";
  src: url("../fonts/unscii-8.woff") format("woff");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

:root {
  color-scheme: dark;
  --bg: #000;
  --ink: #c8c8c8;
  --dim: #8a8a8a;
  --bright: #f2f2f2;
  --yellow: #ffff55;
  --red: #ff3333;
  --wall: #b4b4b4;
  --floor: #6e6e6e;
  --green: #6dff6d;
  --blue: #8eb4ff;
  --line: #3a3a3a;
  --edge: #8a8a8a;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: "Unscii", "Courier New", ui-monospace, monospace;
  font-size: 16px;
  line-height: 1.25;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: none;
  font-smooth: never;
}

button, input { font-family: inherit; }

#app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  padding:
    calc(6px + var(--safe-top))
    calc(8px + var(--safe-right))
    calc(6px + var(--safe-bottom))
    calc(8px + var(--safe-left));
  overflow: hidden;
}

button {
  background: #000;
  color: var(--bright);
  border: 1px solid var(--edge);
  border-radius: 0;
  padding: 10px 12px;
  font-size: 16px;
  line-height: 1.2;
  touch-action: manipulation;
  cursor: pointer;
}

button:active { background: #1c1c1c; }
button:focus-visible { outline: 1px solid var(--yellow); outline-offset: 2px; }
button:disabled { color: #444; border-color: #333; }

.scroll {
  overflow: auto;
  touch-action: pan-y;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  min-height: 0;
}

.screen {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.screen.scroll { align-items: stretch; width: 100%; }

.word {
  color: var(--yellow);
  font-size: 32px;
  line-height: 1;
  letter-spacing: 6px;
  text-align: center;
  margin-top: 8px;
}

.tag, .fine, .note {
  color: var(--dim);
  text-align: center;
}

.tag { color: var(--ink); }

.banner {
  line-height: 1;
  text-align: center;
  margin: 4px auto;
  white-space: nowrap;
}

.banner span { line-height: 1; }
.wall { color: var(--wall); }
.floor { color: var(--floor); }
.mon { color: var(--red); }
.player { color: var(--yellow); }
.stairs { color: #fff; }
.item.heal { color: var(--green); }
.item.mana { color: var(--blue); }
.item.food { color: #ffaa55; }
.item.weapon { color: #fff; }
.item.armor { color: #7ee0e0; }
.item.ring { color: #ff77ff; }
.item.gold, .seal { color: var(--yellow); }

.menu {
  width: min(100%, 420px);
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 4px auto;
}

.btn-primary { border-color: #d0d0d0; color: #fff; }
.ghost {
  border-color: #444;
  color: var(--dim);
  padding: 4px 8px;
  background: transparent;
}
.ghost.on { color: var(--yellow); border-color: var(--yellow); }

.best { text-align: center; color: var(--ink); }
.best b { color: var(--yellow); font-weight: 400; }

.cards { width: min(100%, 520px); margin: 0 auto; display: flex; flex-direction: column; gap: 10px; }
.card {
  text-align: left;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.card .name { color: var(--yellow); }
.card .stats { color: var(--bright); }
.card .spells { color: var(--blue); }
.card .blurb { color: var(--dim); }

.modal {
  width: min(100%, 420px);
  margin: 12px auto;
  border: 1px solid var(--edge);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

h1, h2, p { font-weight: 400; margin: 0; }
h1 { color: var(--yellow); text-align: center; font-size: 32px; letter-spacing: 4px; }
h2 { color: var(--bright); }
.bad { color: var(--red); }
.good { color: var(--green); }
.warn { color: var(--yellow); }

.end-screen { justify-content: flex-start; padding-top: 12px; }
.end-screen .reason, .end-screen .sheet { text-align: center; width: min(100%, 460px); margin: 0 auto; }
.sheet { color: var(--ink); }
.lastlog { width: min(100%, 460px); margin: 8px auto; color: var(--dim); }
.lastlog div:last-child { color: var(--bright); }

.help { width: min(100%, 640px); margin: 0 auto; display: flex; flex-direction: column; gap: 10px; }
.help p { color: var(--ink); }
.help .k { color: var(--dim); }

#play {
  position: relative;
  flex: 1;
  min-height: 0;
  display: grid;
  overflow: hidden;
}

html[data-layout="stack"] #play {
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-areas:
    "top"
    "map"
    "pad";
}

html[data-layout="side"] #play {
  grid-template-columns: minmax(240px, 336px) minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-areas:
    "stats top"
    "stats map"
    "stats pad";
}

.top { grid-area: top; min-width: 0; display: flex; flex-direction: column; }
.map { grid-area: map; min-width: 0; min-height: 0; position: relative; overflow: hidden; touch-action: none; background: #000; }
.pad { grid-area: pad; min-width: 0; display: flex; flex-direction: column; align-items: center; gap: 4px; padding-top: 4px; }

.logrow { display: flex; flex-direction: column; min-width: 0; }
.log {
  min-width: 0;
  min-height: calc(1.25em * 2);
  max-height: calc(1.25em * 4);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  cursor: pointer;
}
.log .msg { white-space: normal; color: #8d8d8d; }
.log .msg.bad { color: #aa4444; }
.log .msg:last-child { color: var(--bright); }
.log .msg.bad:last-child { color: #ff5555; }
.log .msg.good:last-child { color: #d8d8d8; }
.log .msg.warn:last-child { color: var(--yellow); }

html[data-layout="side"] .log { max-height: calc(1.25em * 2); min-height: calc(1.25em * 2); }

.spellrow { display: flex; gap: 4px; align-items: stretch; }
.spellrow .spellline { flex: 1; min-width: 0; }
.spellrow .ghost { flex: none; padding: 0 6px; }
.here:empty { display: none; }
.aim { max-width: 34%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.spellline, .hud {
  width: 100%;
  display: flex;
  gap: 8px;
  align-items: center;
  justify-content: space-between;
  background: transparent;
  border: 0;
  border-top: 1px solid var(--line);
  padding: 3px 0;
  color: var(--ink);
  text-align: left;
}
.spellline .nm { color: var(--bright); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.spellline .cost { color: var(--blue); }
.spellline .face { color: var(--yellow); }
.spellline.poor .nm, .spellline.poor .cost { color: #aa4444; }
.here {
  min-height: 1.25em;
  max-height: 2.6em;
  color: #9a9a9a;
  white-space: normal;
  overflow: hidden;
}
.hud { flex-wrap: wrap; gap: 2px 10px; border-bottom: 1px solid var(--line); }
.hud .hp.ok { color: var(--green); }
.hud .hp.mid { color: var(--yellow); }
.hud .hp.low { color: var(--red); }
.hud .en.low { color: var(--red); }
.hud .en.mid { color: var(--yellow); }
.hud .poison { color: var(--red); }
.hud .fade { color: #bbbb66; }
html[data-layout="side"] .hud { display: none; }

.stats {
  min-height: 0;
  min-width: 0;
  padding: 0 8px 4px 0;
}
html[data-layout="stack"] .stats { display: none; }
html.stats-open[data-layout="stack"] .stats {
  display: block;
  position: absolute;
  z-index: 6;
  inset: 0;
  background: #000;
  padding: 8px 4px 16px;
}
html[data-layout="side"] .stats { grid-area: stats; }

.stat {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  white-space: nowrap;
}
.stat .k { color: #8d8d8d; }
.stat .v { color: var(--bright); text-align: right; overflow: hidden; text-overflow: ellipsis; }
.stat .v.low { color: var(--red); }
.stat .v.mid { color: var(--yellow); }
.stat .v.ok { color: var(--green); }
.stat .v.mp { color: var(--blue); }
.stat .v.hot { color: var(--yellow); }
.hr { border-top: 1px dashed #444; margin: 4px 0; }
.meter {
  height: 12px;
  background: #141414;
  border: 1px solid #6a6a6a;
  margin: 2px 0 4px;
}
.meter > div { height: 100%; background: #9a9a9a; width: 0; }
.stats .close { width: 100%; margin-bottom: 8px; }

.grid {
  position: absolute;
  left: 0;
  top: 0;
  display: grid;
  gap: 0;
  line-height: 1;
  font-kerning: none;
  font-variant-ligatures: none;
}
.cell {
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  line-height: 1;
  letter-spacing: 0;
}
.cell.wall.lair, .cell.wall-dim.lair { --ink: #c46b8a; color: #c46b8a; }
.cell.door { color: #e0b15a; background: #3a2408; }
.cell.door.dim, .cell.door-dim { color: #6a5a3a; }
.cell.door.open { color: #8d8d8d; }
.alert-banner[hidden] { display: none !important; }
.alert-banner {
  position: absolute;
  left: 8px;
  right: 8px;
  top: 28%;
  height: 46%;
  z-index: 30;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 12px;
  padding: 16px;
  background: #3a0000;
  color: #fff;
  border: 2px solid #ff4444;
}
.alert-banner.energy { background: #2a2200; border-color: #e0b15a; color: #ffe7a3; }
.alert-banner h2 { margin: 0; color: inherit; }
.cell.wall, .cell.wall-dim {
  --ink: var(--wall);
  color: var(--wall);
  font-kerning: none;
  font-variant-ligatures: none;
}
.cell.floor { color: var(--floor); }
.cell.wall-dim { --ink: #3a3a3a; color: #3a3a3a; }
.cell.wm0 { background: linear-gradient(var(--ink), var(--ink)) center / 62% 62% no-repeat; }
.cell.wm1 { background: linear-gradient(var(--ink), var(--ink)) top center / 28% 50% no-repeat; }
.cell.wm2 { background: linear-gradient(var(--ink), var(--ink)) right center / 50% 28% no-repeat; }
.cell.wm3 {
  background:
    linear-gradient(var(--ink), var(--ink)) top center / 28% 50% no-repeat,
    linear-gradient(var(--ink), var(--ink)) right center / 50% 28% no-repeat;
}
.cell.wm4 { background: linear-gradient(var(--ink), var(--ink)) bottom center / 28% 50% no-repeat; }
.cell.wm5 { background: linear-gradient(var(--ink), var(--ink)) center / 28% 100% no-repeat; }
.cell.wm6 {
  background:
    linear-gradient(var(--ink), var(--ink)) right center / 50% 28% no-repeat,
    linear-gradient(var(--ink), var(--ink)) bottom center / 28% 50% no-repeat;
}
.cell.wm7 {
  background:
    linear-gradient(var(--ink), var(--ink)) center / 28% 100% no-repeat,
    linear-gradient(var(--ink), var(--ink)) right center / 50% 28% no-repeat;
}
.cell.wm8 { background: linear-gradient(var(--ink), var(--ink)) left center / 50% 28% no-repeat; }
.cell.wm9 {
  background:
    linear-gradient(var(--ink), var(--ink)) top center / 28% 50% no-repeat,
    linear-gradient(var(--ink), var(--ink)) left center / 50% 28% no-repeat;
}
.cell.wm10 { background: linear-gradient(var(--ink), var(--ink)) center / 100% 28% no-repeat; }
.cell.wm11 {
  background:
    linear-gradient(var(--ink), var(--ink)) center / 100% 28% no-repeat,
    linear-gradient(var(--ink), var(--ink)) top center / 28% 50% no-repeat;
}
.cell.wm12 {
  background:
    linear-gradient(var(--ink), var(--ink)) bottom center / 28% 50% no-repeat,
    linear-gradient(var(--ink), var(--ink)) left center / 50% 28% no-repeat;
}
.cell.wm13 {
  background:
    linear-gradient(var(--ink), var(--ink)) center / 28% 100% no-repeat,
    linear-gradient(var(--ink), var(--ink)) left center / 50% 28% no-repeat;
}
.cell.wm14 {
  background:
    linear-gradient(var(--ink), var(--ink)) center / 100% 28% no-repeat,
    linear-gradient(var(--ink), var(--ink)) bottom center / 28% 50% no-repeat;
}
.cell.wm15 {
  background:
    linear-gradient(var(--ink), var(--ink)) center / 100% 28% no-repeat,
    linear-gradient(var(--ink), var(--ink)) center / 28% 100% no-repeat;
}
.cell.floor-dim { color: #242424; }
.cell.mon { color: var(--red); }
.cell.mon.sleep { color: #992222; }
.cell.mon.boss { color: #ff5555; }
.cell.mon.mini {
  color: #ffe14a;
  background: #4a3200;
  box-shadow: inset 0 0 0 2px #ffb000;
}
.cell.mon.lich { color: #d090ff; }
.cell.mon.sleep.mini {
  color: #e0b040;
  background: #2a1c00;
  box-shadow: inset 0 0 0 2px #aa7700;
}
.cell.player { color: var(--yellow); animation: blink 1.05s steps(1, end) infinite; }
.cell.player.hid { color: #aaaa44; }
.cell.blood { color: #662222; }
.cell.stairs { color: #ffffff; }
.cell.keeper { color: #ffff55; }
.cell.chest { color: #ffbf6a; background: #4a2a08; box-shadow: inset 0 0 0 2px #c47a28; }
.cell.mem { opacity: 0.72; }
.cell.item.mem { color: #8a8a8a; background: transparent; box-shadow: none; }
.cell.stairs.mem, .cell.seal.mem, .cell.keeper.mem, .cell.corpse.mem { color: #6e6e6e; background: transparent; box-shadow: none; }
.cell.chest.mem { color: #8a6230; background: #1a1006; box-shadow: inset 0 0 0 1px #6a4520; opacity: 1; }
.item.shield { color: #9fd4ff; }
.cell.ally { color: #d0d0d0; }
.cell.ally.pet {
  color: #39ff6a;
  background: #063014;
  box-shadow: inset 0 -3px 0 #39ff6a;
}
.cell.ally.risen {
  color: #c8fff6;
  background: #12343a;
}
.cell.waif { color: #9dff9d; }
.cell.corpse { color: #cc5555; }
.cell.corpse.mini { color: #e09030; }
.cell.corpse.boss { color: #ff7777; }
.cell.corpse.lich { color: #c090ee; }
.cell.corpse.pet { color: #6fbf8a; }
.item.scroll { color: #eee8aa; }
.cell.seal { color: var(--yellow); }
.cell.item.heal { color: var(--green); }
.cell.item.mana { color: var(--blue); }
.cell.item.food { color: #ffaa55; }
.cell.item.weapon { color: #ffffff; }
.cell.item.armor { color: #7ee0e0; }
.cell.item.ring { color: #ff77ff; }
.cell.item.gold { color: var(--yellow); }

@keyframes blink {
  0%, 55% { opacity: 1; }
  56%, 100% { opacity: 0.25; }
}

.fx { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.floater {
  position: absolute;
  transform: translate(-50%, 0);
  animation: floatup 680ms linear forwards;
  color: var(--yellow);
  text-shadow: 1px 0 0 #000, -1px 0 0 #000, 0 1px 0 #000, 0 -1px 0 #000;
}
.floater.hurt, .floater.poison { color: #ff4444; }
.floater.heal { color: var(--green); }
.floater.miss { color: #888; }
.floater.crit { color: #fff; }
.floater.bolt { color: #9ecbff; }
.cell.aim { outline: 1px solid #ffff55; outline-offset: -2px; }
.cell.beam {
  color: #ffffff;
  background: #2f7fe0;
  font-weight: 700;
  text-shadow: 0 0 0 #fff;
}
.cell.target { box-shadow: inset 0 0 0 3px #9fd0ff; }
.cell.target.hot {
  color: #111;
  background: #ffe14a;
  box-shadow: inset 0 0 0 3px #fff6a8;
}
.name-screen input, .bond input, .overlay input {
  display: block;
  width: 100%;
  min-height: 44px;
  margin: 8px 0;
  padding: 8px;
  font-size: 16px;
  background: #000;
  color: #fff;
  border: 1px solid #aaa;
}
.swatch {
  display: inline-block;
  min-width: 1.2em;
  padding: 0 3px;
  text-align: center;
  line-height: 1.2;
}
.swatch.mini { color: #ffe14a; background: #4a3200; box-shadow: inset 0 0 0 2px #ffb000; }
.swatch.pet { color: #39ff6a; background: #063014; box-shadow: inset 0 -3px 0 #39ff6a; }
.swatch.risen { color: #c8fff6; background: #12343a; }
@keyframes floatup {
  from { transform: translate(-50%, 0); opacity: 1; }
  to { transform: translate(-50%, -18px); opacity: 0; }
}

.pad-row {
  display: flex;
  gap: 8px;
  align-items: stretch;
  justify-content: center;
  width: min(100%, 560px);
}
.dpad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 4px;
  width: min(46vw, 198px);
  height: min(46vw, 198px);
  flex: none;
  touch-action: none;
}
html[data-layout="side"] .dpad { width: 132px; height: 132px; }
.dpad button {
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
  color: #ddd;
}
.dpad button.diag { color: #777; }
.actions {
  flex: 1;
  min-width: 0;
  max-width: 280px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  gap: 4px;
}
.actions button { padding: 0; }
.stairs-go {
  width: min(100%, 560px);
  height: 36px;
  padding: 0;
  color: var(--yellow);
  border-color: var(--yellow);
}
.stairs-go[hidden] { display: none !important; }
.shop-go, .bond-go {
  width: min(100%, 560px);
  height: 44px;
  padding: 0;
  color: #ffff55;
  border-color: #ffff55;
}
.bond-go { color: #9dff9d; border-color: #9dff9d; }
.shop-go[hidden], .bond-go[hidden] { display: none !important; }

.chev {
  width: 0;
  height: 0;
  border-style: solid;
  display: block;
}
.chev.n { border-width: 0 7px 9px 7px; border-color: transparent transparent #ddd transparent; }
.chev.s { border-width: 9px 7px 0 7px; border-color: #ddd transparent transparent transparent; }
.chev.e { border-width: 7px 0 7px 9px; border-color: transparent transparent transparent #ddd; }
.chev.w { border-width: 7px 9px 7px 0; border-color: transparent #ddd transparent transparent; }
.dot {
  width: 8px;
  height: 8px;
  background: #ddd;
  display: block;
}

.overlay {
  position: absolute;
  z-index: 40;
  inset: 0;
  background: #000;
  padding: 4px 2px 28px;
}
.overlay > button.wide { display: block; width: 100%; margin-top: 8px; }
.overlay[hidden] { display: none !important; }
.overlay h2 { margin-bottom: 8px; }
.bag-row, .eq-row {
  display: grid;
  grid-template-columns: 20px minmax(0, 1fr) auto auto;
  gap: 6px;
  align-items: center;
  margin-bottom: 6px;
}
.eq-row { grid-template-columns: 20px minmax(0, 1fr) auto; }
.bag-row .nm, .eq-row .nm { white-space: normal; }
.bag-row button, .eq-row button { padding: 8px 8px; }
.curse-row {
  display: grid;
  grid-template-columns: 20px minmax(0, 1fr) auto;
  gap: 6px;
  align-items: center;
  margin-bottom: 8px;
}
.curse-row .nm { white-space: normal; }
.curse-row button {
  min-height: 44px;
  min-width: 84px;
  padding: 10px 12px;
}
.worn { color: var(--dim); }
.section-label { color: var(--dim); margin: 8px 0 4px; }
.enchant-panel, .enchant-row {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
  width: 100%;
  min-width: 0;
  position: static;
}
.enchant-row { margin-bottom: 12px; }
.enchant-panel h2, .enchant-panel p, .enchant-panel .fine, .enchant-row .fine { text-align: left; }
.enchant-panel .nm, .enchant-row .nm, .enchant-panel p {
  min-width: 0;
  white-space: normal;
  overflow-wrap: anywhere;
}
.enchant-choice, .enchant-row > button, .enchant-panel > button.wide {
  display: block;
  width: 100%;
  min-height: 44px;
  position: static;
  white-space: normal;
  overflow-wrap: anywhere;
  text-align: left;
}
button:disabled.enchant-choice { color: #8a8a8a; border-color: #333; }
button:disabled.enchant-choice .why { display: block; color: #c4a35a; }

.keys { color: var(--dim); }
.crash {
  position: absolute;
  left: 8px;
  right: 8px;
  bottom: 8px;
  z-index: 20;
  color: #ff5555;
  background: #000;
  border: 1px solid #ff3333;
  padding: 8px;
  white-space: pre-wrap;
}

@media (max-height: 700px) {
  html[data-layout="stack"] .dpad { width: min(38vw, 150px); height: min(38vw, 150px); }
  .word { font-size: 24px; }
}
@media (max-height: 520px) {
  html[data-layout="side"] .stat { line-height: 1; }
  html[data-layout="side"] .hr { margin: 2px 0; }
  html[data-layout="side"] .meter { height: 8px; margin: 1px 0 2px; }
  html[data-layout="side"] .stats { padding-bottom: 0; }
}
@media (max-height: 430px) {
  html[data-layout="side"] .dpad { width: 104px; height: 104px; }
  html[data-layout="side"] .stairs-go { height: 28px; }
  .log { min-height: 1.25em; max-height: calc(1.25em * 2); }
}
@media (min-width: 1100px) and (min-height: 760px) {
  html[data-layout="side"] #play { grid-template-columns: 340px minmax(0, 1fr); }
  .stat, .log, .spellline, .here { font-size: 18px; }
  html[data-layout="side"] .dpad { width: 168px; height: 168px; }
}
@media (prefers-reduced-motion: reduce) {
  .cell.player, .floater { animation: none; }
}
@media (hover: hover) and (min-width: 900px) {
  .desktop-hint { display: block; }
}
.desktop-hint { display: none; text-align: center; color: #666; }
