:root {
  --ink: #241c14;
  --paper: #f4efe2;
  --gold: #f0c14a;
  --green: #2f6b45;
  --orange: #e07a22;
  --red: #c6453c;
  --blue: #2c4f86;
  --line: #1c1915;
  color: var(--ink);
  font-family: "Segoe UI", "Microsoft YaHei", "PingFang SC", sans-serif;
  background: #1a1d27;
}

* { box-sizing: border-box; }

html, body { height: 100%; }
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background:
    radial-gradient(circle at 50% -10%, #343a52, transparent 46%),
    #1a1d27;
  color: var(--paper);
}

button, input, textarea { font: inherit; }

button {
  cursor: pointer;
  border: 3px solid var(--line);
  background: var(--gold);
  color: var(--ink);
  box-shadow: 4px 4px 0 var(--line);
  border-radius: 0;
  font-weight: 700;
}

button:hover { transform: translate(1px, 1px); box-shadow: 3px 3px 0 var(--line); }
button:active { transform: translate(4px, 4px); box-shadow: 0 0 0 var(--line); }
button:disabled { opacity: 0.45; cursor: default; transform: none; }
button:focus-visible { outline: 3px solid #9fd0ff; outline-offset: 2px; }

#shell {
  width: min(860px, 100%);
  height: 100vh;
  height: 100dvh;
  margin: 0 auto;
  padding: 6px 10px 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

#top {
  display: grid;
  grid-template-columns: auto 1fr auto auto;
  gap: 12px 18px;
  align-items: center;
  margin-bottom: 0;
  flex: 0 0 auto;
}

.brand h1, #title-card h2, .panel h2, #ritual h2 {
  margin: 0;
  font-weight: 800;
  letter-spacing: 0.04em;
}

.brand h1 { font-size: 22px; color: var(--gold); }

.kicker {
  margin: 0 0 2px;
  font-size: 12px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: #d7c39a;
}

#pips { display: flex; flex-wrap: wrap; gap: 6px; }

#pips span {
  border: 2px solid #3c4258;
  color: #9aa3bd;
  padding: 4px 8px;
  font-size: 13px;
}

#pips span.on {
  border-color: var(--gold);
  color: #1c1915;
  background: var(--gold);
}

.hud-side { text-align: right; }
#zone { margin: 0 0 4px; color: #d5d8e6; }
#hearts, #badges, #end-badges { display: flex; gap: 6px; justify-content: flex-end; }
.heart, .badge {
  width: 18px;
  height: 18px;
  border: 2px solid #111;
}
.heart.on { background: var(--red); }
.heart.off { background: #3a3142; }
.badge {
  width: auto;
  min-width: 28px;
  height: 26px;
  padding: 0 6px;
  display: grid;
  place-items: center;
  font-size: 13px;
  background: #3a3142;
  color: #8d8494;
}
.badge.on { background: var(--gold); color: #1c1915; }

.hud-actions { display: flex; gap: 8px; }

#screen {
  position: relative;
  flex: 0 1 auto;
  width: min(100%, calc((100dvh - 250px) * 240 / 176));
  max-height: calc(100dvh - 250px);
  aspect-ratio: 240 / 176;
  margin-inline: auto;
  background: #10141c;
  border: 4px solid #d7c39a;
  box-shadow: 0 0 0 4px #1c1915, 8px 8px 0 #000;
  overflow: hidden;
}

#game {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #1b1f2a;
}

#marks {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

#flash {
  pointer-events: none;
  position: absolute;
  inset: 0;
}
#flash.bad { background: rgba(198, 69, 60, 0.35); }
#flash.good { background: rgba(240, 193, 74, 0.22); }

#banner, #toast {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s linear;
  text-align: center;
}
#banner {
  top: 16px;
  background: #f4efe2;
  color: #1c1915;
  border: 4px solid #1c1915;
  padding: 8px 14px;
  font-weight: 800;
  font-size: 22px;
}
#toast {
  bottom: 12px;
  background: rgba(20, 16, 12, 0.88);
  color: #fff8e8;
  border: 2px solid #f0c14a;
  padding: 6px 10px;
  font-size: 16px;
}
#banner.show, #toast.show { opacity: 1; }

#phone {
  position: absolute;
  top: 10px;
  right: 8px;
  z-index: 4;
  width: min(250px, 48%);
  padding: 10px 10px 8px;
  background: #172033;
  color: #f4efe2;
  border: 4px solid #f0c14a;
  box-shadow: 4px 4px 0 #1c1915;
  animation: phone-in 0.18s linear;
}
#phone[hidden] { display: none; }
.phone-kicker {
  margin: 0 0 4px;
  color: #8dcea0;
  font-size: 12px;
  letter-spacing: 0.08em;
}
#phone-msg {
  margin: 0 0 8px;
  font-size: 16px;
  line-height: 1.35;
  cursor: pointer;
}
#phone-reply {
  margin: 0 0 8px;
  color: #f0c14a;
  font-size: 14px;
  line-height: 1.35;
  white-space: pre-line;
}
#phone-replies {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 8px;
}
#phone-replies button {
  padding: 6px 8px;
  background: #8dcea0;
  color: #172033;
  text-align: left;
  line-height: 1.3;
}
#phone-reply[hidden],
#phone-replies[hidden] { display: none; }
.phone-actions { display: flex; gap: 8px; }
.phone-actions button { flex: 1; padding: 6px 0; }
#phone-open { background: #8dcea0; }
#phone-dismiss { background: #d7d3cc; }
@keyframes phone-in {
  from { transform: translateY(-8px); }
  to { transform: none; }
}

#title-card {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 28px 8% 24px;
  background: linear-gradient(180deg, rgba(16, 18, 28, 0.2), rgba(16, 18, 28, 0.78) 28%, rgba(16, 18, 28, 0.88));
  color: var(--paper);
}
#title-card h2 { font-size: clamp(26px, 4vw, 40px); color: var(--gold); }
.lead { max-width: 38em; font-size: 15px; line-height: 1.4; margin: 6px 0; }
#title-form { display: flex; flex-wrap: wrap; gap: 10px; align-items: end; margin: 8px 0 12px; }
#title-form label { width: 100%; font-size: 14px; color: #d7c39a; }
#name {
  border: 3px solid var(--line);
  background: var(--paper);
  color: var(--ink);
  padding: 10px 12px;
  min-width: 180px;
  font-size: 20px;
}
#btn-start { font-size: 20px; padding: 10px 18px; }
.controls { display: flex; flex-wrap: wrap; gap: 8px 16px; padding: 0; margin: 0; list-style: none; color: #d5d8e6; }

#lower {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 8px;
  margin-top: 0;
  flex: 0 0 auto;
  align-items: end;
}

#box { min-height: 0; }

#dialog, #ritual {
  display: grid;
  grid-template-columns: 56px 1fr;
  gap: 8px;
  min-height: 0;
  background: var(--paper);
  color: var(--ink);
  border: 4px solid var(--line);
  box-shadow: 6px 6px 0 #000;
  padding: 8px;
}
#ritual { grid-template-columns: 1fr; }
#dialog[hidden], #ritual[hidden], #title-card[hidden], #book[hidden], #ending[hidden], #err[hidden] { display: none; }

#portrait {
  width: 48px;
  height: 48px;
  image-rendering: pixelated;
  background: #d7ecf5;
  border: 3px solid var(--line);
}
#who { margin: 0; color: var(--orange); font-weight: 800; }
#line { margin: 2px 0 4px; font-size: 16px; line-height: 1.35; min-height: 2.4em; }
#hint { margin: 0; color: #7a6853; font-size: 14px; }

#choices, .ritual-actions, .end-actions { display: flex; flex-direction: column; gap: 8px; }
.choice, .ritual-actions button, .end-actions button, #pad button, .hud-actions button, .panel-head button {
  text-align: left;
  padding: 10px 12px;
  font-size: 18px;
}
.ritual-actions { flex-direction: row; flex-wrap: wrap; }
.ritual-actions button { text-align: center; min-width: 120px; }
#ritual-text { font-size: 20px; line-height: 1.45; margin-top: 8px; }

#pad {
  display: grid;
  grid-template-columns: repeat(3, 40px);
  grid-template-areas:
    ". up ."
    "left down right"
    "run run run";
  gap: 6px;
}
.pad-up { grid-area: up; }
#pad [data-dir="left"] { grid-area: left; }
#pad [data-dir="down"] { grid-area: down; }
#pad [data-dir="right"] { grid-area: right; }
.pad-run { grid-area: run; text-align: center; background: #f4efe2; }
.pad-run.on { background: #e07a22; color: white; }

#legend { color: #b7bdd0; font-size: 12px; margin: 0; flex: 0 0 auto; }
#keys {
  align-self: end;
  margin: 0;
  padding: 8px 10px;
  background: rgba(28, 25, 21, 0.9);
  color: #fff8e8;
  border: 2px solid #ffe14a;
  font-size: 14px;
  font-weight: 700;
  line-height: 1.35;
  max-width: 11em;
  white-space: normal;
}
#pad button { padding: 4px 0; font-size: 14px; text-align: center; }

#book, #ending {
  position: fixed;
  inset: 0;
  background: rgba(10, 12, 18, 0.72);
  display: grid;
  place-items: center;
  padding: 18px;
  z-index: 5;
}
.panel {
  width: min(720px, 100%);
  max-height: min(88vh, 900px);
  overflow: auto;
  background: var(--paper);
  color: var(--ink);
  border: 4px solid var(--line);
  box-shadow: 8px 8px 0 #000;
  padding: 16px;
}
.panel.wide { width: min(860px, 100%); }
.panel-head, .end-actions { display: flex; justify-content: space-between; gap: 12px; align-items: center; }
#book-list, #end-list { display: grid; gap: 8px; margin: 12px 0; }
.rule {
  display: grid;
  grid-template-columns: 92px 1fr;
  gap: 8px;
  border: 2px solid #e4dccb;
  padding: 8px;
}
.rule b { font-size: 14px; }
.tag { font-size: 13px; font-weight: 800; }
.tag.ok { color: var(--green); }
.tag.bad { color: var(--red); }
.tag.yet { color: #8a7b68; }
#end-rank { font-size: 36px; color: var(--green); }
#end-summary { font-size: 18px; }
#end-badges { justify-content: flex-start; margin: 8px 0; }
#review-text {
  width: 100%;
  min-height: 120px;
  border: 3px solid var(--line);
  padding: 8px;
  background: #fffdf8;
  color: var(--ink);
}
#err {
  position: fixed;
  left: 8px;
  right: 8px;
  bottom: 8px;
  background: #c6453c;
  color: white;
  padding: 8px 12px;
  z-index: 9;
}

@media (max-width: 800px) {
  #top { grid-template-columns: 1fr 1fr; }
  #line, #ritual-text { font-size: 15px; }
  .hud-side { text-align: left; }
  #hearts, #badges { justify-content: flex-start; }
  #keys { white-space: normal; }
}
