* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

/* Author display values (grid/flex below) would silently beat the UA-level
   [hidden] rule, leaving "hidden" overlays covering the stage and eating
   every click. */
[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
  overflow: hidden;
  background: #0b0b10;
  color: #eee;
  font-family: "DotGothic16", "Hiragino Maru Gothic ProN", "BIZ UDGothic", system-ui, sans-serif;
  touch-action: manipulation;
  user-select: none;
}

button {
  font: inherit;
  color: inherit;
  background: #2e2c3a;
  border: 2px solid #565064;
  border-radius: 12px;
  padding: 10px 18px;
  cursor: pointer;
}
button:active {
  transform: translateY(1px);
}
button.primary {
  background: #4457a8;
  border-color: #6a7fd8;
}
button.big {
  font-size: 1.3rem;
  padding: 14px 28px;
}
button:disabled {
  opacity: 0.45;
}

.screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
}

/* --- Title --- */
/* Two stacked panels inside the title screen: play-key entry, then the
   はじめ/セーブ selection. Only one shows at a time. */
#panel-key,
#panel-select {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  width: 100%;
}
#panel-key[hidden],
#panel-select[hidden] {
  display: none;
}
.title-head {
  display: flex;
  align-items: center;
  gap: 22px;
}
/* The hero (same strip the in-game player uses), idling beside the title. */
.title-hero {
  width: 96px;
  height: 96px;
  background: url(/assets/chars/dungeon-player.png) no-repeat;
  background-size: 400% 100%;
  image-rendering: pixelated;
  animation: hero-idle 0.72s steps(4) infinite;
}
@keyframes hero-idle {
  to {
    background-position: -384px 0;
  }
}
#screen-title h1 {
  font-size: 3rem;
  letter-spacing: 0.1em;
  text-shadow: 0 4px 0 #000;
}
.tagline {
  color: #aaa;
}
.title-card {
  background: #17161f;
  border: 2px solid #2e2c3a;
  border-radius: 16px;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: min(480px, 90vw);
}
.title-card label {
  color: #999;
  font-size: 0.9rem;
}
.title-card input {
  font: inherit;
  font-size: 1.1rem;
  padding: 12px;
  border-radius: 10px;
  border: 2px solid #565064;
  background: #0e0d15;
  color: #fff;
}
.title-note {
  color: #9ad48e;
  font-size: 1.1rem;
}
.title-error {
  color: #e08080;
}
#title-full-note {
  color: #e0b070;
  font-size: 1rem;
  line-height: 1.5;
}
/* Continue slots: one row per saved run (resume + delete). */
#continue-slots {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.continue-slot {
  display: flex;
  gap: 8px;
}
.continue-resume {
  flex: 1;
  text-align: left;
  background: #1d2740;
  border-color: #3a4a78;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.continue-del {
  flex: 0 0 auto;
  padding: 0 16px;
  color: #e08080;
  border-color: #6a4a4a;
}
.mute-toggle {
  position: fixed;
  right: 16px;
  bottom: 16px;
}
/* Fullscreen toggle: fixed bottom-left, opposite the mute button, shown on
   every screen. Inherits the base button look. Sits above overlays (z-index
   10) so the exit control stays reachable while a modal is open. */
#btn-fullscreen {
  position: fixed;
  left: 16px;
  bottom: 16px;
  z-index: 20;
}

/* --- QR camera reader --- */
.qr-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}
.qr-frame {
  position: relative;
  width: min(80vw, 60vh);
  aspect-ratio: 1;
  border-radius: 16px;
  overflow: hidden;
  background: #000;
}
.qr-frame video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.qr-reticle {
  position: absolute;
  inset: 12%;
  border: 3px solid rgba(255, 255, 255, 0.9);
  border-radius: 12px;
  box-shadow: 0 0 0 100vmax rgba(0, 0, 0, 0.35);
  pointer-events: none;
}
.qr-hint {
  color: #eee;
  font-size: 1.1rem;
  text-align: center;
}

/* --- Game --- */
#screen-game {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: flex-start;
  gap: 0;
}

#hud {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 8px 14px;
  background: #14131c;
  border-bottom: 2px solid #2e2c3a;
  font-size: 1.05rem;
}
#hud-hp {
  position: relative;
  flex: 1;
  max-width: 380px;
  height: 26px;
  background: #3a1620;
  border-radius: 13px;
  overflow: hidden;
  border: 2px solid #565064;
}
#hud-hp-bar {
  position: absolute;
  inset: 0;
  width: 100%;
  background: #4caf50;
  transition: width 0.2s;
}
#hud-hp-bar.low {
  background: #e6a23c;
}
#hud-hp-bar.danger {
  background: #e05555;
  animation: blink 0.8s infinite;
}
@keyframes blink {
  50% {
    opacity: 0.5;
  }
}
#hud-hp-text {
  position: relative;
  display: block;
  text-align: center;
  line-height: 22px;
  font-size: 0.85rem;
  text-shadow: 0 1px 2px #000;
}
#btn-mute-game {
  margin-left: auto;
}

/* DQ-style command window: white frame with a dark rim, stacked bottom
   right, matching the message window's framing. */
#action-menu {
  position: absolute;
  right: 14px;
  bottom: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.dq-btn {
  min-width: 176px;
  padding: 12px 20px;
  font-size: 1.3rem;
  letter-spacing: 0.08em;
  color: #fff;
  background: rgba(8, 8, 14, 0.82);
  border: 3px solid rgba(255, 255, 255, 0.92);
  border-radius: 10px;
  box-shadow:
    0 0 0 3px rgba(0, 0, 0, 0.65),
    inset 0 0 0 1px rgba(0, 0, 0, 0.65);
}
.dq-btn:active {
  transform: translateY(1px);
}

#stage {
  position: relative;
  flex: 1;
  min-height: 0;
}
#game-canvas {
  width: 100%;
  height: 100%;
  display: block;
  image-rendering: pixelated;
}
#minimap {
  position: absolute;
  top: 10px;
  right: 10px;
  border: 1px solid #565064;
  border-radius: 4px;
  image-rendering: pixelated;
}

/* Visible-monster list (Monster Hunter style), below the minimap. */
#monster-list {
  position: absolute;
  top: 104px;
  right: 10px;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
  pointer-events: none;
  font-size: 0.9rem;
  text-shadow: 0 1px 3px #000;
}
.monster-entry {
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(10, 10, 16, 0.65);
  border: 1px solid #2e2c3a;
  border-radius: 8px;
  padding: 2px 10px 2px 4px;
}
.monster-entry .chip {
  width: 24px;
  height: 24px;
  image-rendering: pixelated;
  background-repeat: no-repeat;
  /* 4-frame idle strip; crop to frame 0 */
  background-size: 400% 100%;
  background-position: 0 0;
  text-align: center;
  line-height: 24px;
}
/* Torneko-style message window: a framed overlay at the bottom center,
   transparent inside so the map stays visible, auto-hidden when idle. */
#message-area {
  position: absolute;
  left: 50%;
  bottom: 14px;
  transform: translateX(-50%);
  /* Leaves room for the command window on the right. */
  width: min(640px, calc(100% - 440px));
  min-height: 96px;
  padding: 10px 16px;
  border: 3px solid rgba(255, 255, 255, 0.92);
  border-radius: 10px;
  /* Dark rim outside the white frame keeps it crisp on bright floors. */
  box-shadow:
    0 0 0 3px rgba(0, 0, 0, 0.65),
    inset 0 0 0 1px rgba(0, 0, 0, 0.65);
  background: transparent;
  pointer-events: none;
  display: flex;
  justify-content: center;
  align-items: flex-end;
}
#log {
  width: 100%;
  display: flex;
  flex-direction: column-reverse;
  justify-content: flex-start;
  gap: 4px;
  font-size: 1.3rem;
  text-align: left;
  text-shadow:
    0 2px 4px #000,
    0 0 6px #000;
}
#log .old {
  opacity: 0.55;
}
#btn-descend {
  position: absolute;
  left: 50%;
  bottom: 128px; /* clears the message window frame */
  transform: translateX(-50%);
}

/* --- Items sheet --- */
.sheet {
  position: absolute;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
  width: min(560px, 96vw);
  max-height: 70%;
  overflow-y: auto;
  background: #17161f;
  border: 2px solid #2e2c3a;
  border-bottom: none;
  border-radius: 16px 16px 0 0;
  padding: 14px;
}
.sheet h3 {
  margin-bottom: 10px;
}
.item-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 4px;
  border-bottom: 1px solid #2e2c3a;
}
.item-row .item-icon {
  width: 32px;
  height: 32px;
  flex: none;
  image-rendering: pixelated;
  font-size: 24px;
  text-align: center;
}
.item-row .name {
  flex: 1;
  display: flex;
  flex-direction: column;
}
.item-row .desc {
  color: #999;
  font-size: 0.78rem;
}
.item-row .equipped {
  color: #f4c542;
  font-size: 0.85rem;
}
.item-row button {
  padding: 6px 14px;
}

/* --- Direction picker --- */
#dir-picker {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, 0.5);
}
#dir-picker .grid {
  display: grid;
  grid-template-columns: repeat(3, 84px);
  grid-template-rows: repeat(3, 84px);
  gap: 8px;
}
#dir-picker button {
  font-size: 1.6rem;
}

/* --- Practice modal --- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(5, 5, 10, 0.82);
  display: grid;
  place-items: center;
  z-index: 10;
}
.practice-panel {
  /* The kanji question is learning material: a pixel font would be a bad
     model of real glyph shapes, so the modal opts out of DotGothic16. */
  font-family: "Hiragino Maru Gothic ProN", "BIZ UDGothic", system-ui, sans-serif;
  background: #fffdf5;
  color: #222;
  border-radius: 18px;
  padding: 18px 22px;
  width: min(960px, 94vw);
  max-height: 92vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.practice-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.practice-mode {
  font-size: 1.2rem;
  font-weight: bold;
  padding: 4px 16px;
  border-radius: 999px;
  color: #fff;
}
.practice-mode.test {
  background: #d05555;
}
.practice-mode.practice {
  background: #4a9;
}
.practice-close {
  background: #eee;
  color: #555;
  border-color: #ccc;
}
.practice-undo {
  background: #e7eefb;
  color: #33507f;
  border-color: #b9cbec;
}
.practice-reading {
  font-size: 2rem;
  text-align: center;
  letter-spacing: 0.15em;
}
.practice-sentence {
  text-align: center;
  color: #666;
}
.practice-cells {
  display: flex;
  justify-content: center;
  /* The kakitori writer captures strokes via pointer events but never sets
     touch-action itself, so inside this scrollable modal a pen or finger
     drag is stolen by the browser as a scroll gesture and the resulting
     pointercancel aborts the stroke (a Chromebook stylus hits this). Locking
     the writing surface (and every library-generated descendant) lets the
     strokes reach the matcher. Published kakitori 0.18.1 still ships no
     touch-action, so it must be set here rather than waiting for a release. */
  touch-action: none;
}
.practice-cells * {
  touch-action: none;
}
/* Post-failure feedback: verdict cards sit beside the writing column
   (aligned per cell), so the modal never grows taller than the writing
   phase and nothing needs scrolling on a tablet. */
.fb-layout {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  gap: 14px;
}
.fb-col {
  display: flex;
  flex-direction: column;
  gap: 4px;
  width: min(260px, 34vw);
}
.fb-char {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
  padding: 6px 10px;
  border: 2px solid;
  border-radius: 12px;
}
.fb-head {
  display: flex;
  align-items: center;
  gap: 8px;
}
.fb-next {
  font-size: 1.15rem;
}
.fb-char.ok {
  border-color: #8c6;
  color: #3a7d2c;
}
.fb-char.close {
  border-color: #e6a23c;
  color: #9a6700;
  background: #fff8e8;
}
.fb-char.ng {
  border-color: #e66;
  color: #c0392b;
  background: #fff0f0;
}
.fb-mark {
  font-size: 1.2rem;
  font-weight: bold;
}
.fb-glyph {
  /* The glyph doubles as the model answer, so prefer textbook-style faces
     that show real stroke shapes. */
  font-family:
    "UD デジタル 教科書体 N-R", "UD Digi Kyokasho N-R", "YuKyokasho", "Hiragino Maru Gothic ProN",
    serif;
  font-size: 2rem;
  line-height: 1.1;
}
.fb-notes {
  list-style: none;
  font-size: 0.8rem;
  line-height: 1.35;
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.practice-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.practice-hint {
  background: #fff3d6;
  color: #7a5c00;
  border-color: #e8c56a;
}
/* Post-failure stroke-order playback trigger. Distinct color from the amber
   "おてほん" writing-phase hint so the two aids never read as the same
   button. */
.fb-stroke {
  background: #e6f2ea;
  color: #276b3b;
  border-color: #a5cdb2;
}
.practice-status {
  color: #a33;
}

/* --- Rebirth picker (post-death "carry one item and restart" flow) --- */
.rebirth-panel {
  background: #17161f;
  color: #f1eadb;
  border: 2px solid #2e2c3a;
  border-radius: 14px;
  padding: 18px 20px;
  width: min(560px, 94vw);
  max-height: 92vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.rebirth-title {
  font-size: 1.3rem;
  text-align: center;
}
.rebirth-sub {
  font-size: 0.95rem;
  text-align: center;
  color: #cfc7b3;
}
.rebirth-items {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.rebirth-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: #23212c;
  border: 1px solid #3a374a;
  border-radius: 10px;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.rebirth-item:hover,
.rebirth-item:focus-visible {
  background: #2c2a37;
  border-color: #5a566e;
}
.rebirth-item .item-icon {
  width: 32px;
  height: 32px;
  flex: none;
  image-rendering: pixelated;
  font-size: 24px;
  text-align: center;
}
.rebirth-item .name {
  flex: 1;
  display: flex;
  flex-direction: column;
}
.rebirth-item .desc {
  color: #a9a292;
  font-size: 0.78rem;
}
.rebirth-give-up {
  margin-top: 4px;
  align-self: center;
}

/* --- Debug --- */
#debug-panel {
  position: absolute;
  left: 10px;
  top: 10px;
  display: flex;
  gap: 6px;
}
#debug-panel button {
  font-size: 0.8rem;
  padding: 4px 8px;
}

/* --- End screen --- */
#end-stats {
  font-size: 1.15rem;
  line-height: 2;
}
