﻿* {
  box-sizing: border-box;
}

:root {
  --bg: #0d170f;
  --panel: #162318;
  --panel-border: #6f8348;
  --text: #f3f6d9;
  --accent: #d4b25b;
  --ink: #101810;
}

body {
  margin: 0;
  min-height: 100dvh;
  background:
    radial-gradient(1100px 560px at 18% 0%, rgba(78, 120, 78, 0.46), transparent 65%),
    radial-gradient(900px 500px at 90% 100%, rgba(52, 80, 52, 0.3), transparent 70%),
    linear-gradient(160deg, #0d170f, #090f0a);
  color: var(--text);
  font-family: "Trebuchet MS", "Segoe UI", sans-serif;
}

.app {
  width: min(1240px, 100%);
  margin: 0 auto;
  padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  display: grid;
  gap: 10px;
}

.game-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 10px; min-height: 44px; }
.game-toolbar a { color: #e9d599; text-decoration: none; padding: 10px 2px; font-weight: 700; font-size: 13px; }
.game-toolbar > strong { font-size: 17px; }
.toolbar-actions { display: flex; gap: 6px; }
.toolbar-actions button { min-height: 44px; padding: 8px 13px; border-radius: 10px; }
.play-layout { display: grid; grid-template-columns: 164px minmax(0, 1fr) 164px; align-items: start; gap: 10px; }
.dashboard { display: grid; gap: 8px; min-width: 0; }

.hud {
  display: grid;
  gap: 7px;
  background: linear-gradient(180deg, rgba(38, 52, 32, 0.95), rgba(20, 30, 21, 0.96));
  border: 2px solid var(--panel-border);
  border-radius: 10px;
  padding: 7px;
  box-shadow:
    inset 0 0 0 1px rgba(179, 196, 120, 0.2),
    0 8px 22px rgba(0, 0, 0, 0.35);
}

.life-block {
  min-width: 0;
}

.hud-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 5px;
}

.hud-block {
  display: grid;
  gap: 3px;
  background: rgba(18, 28, 18, 0.58);
  border: 1px solid rgba(146, 165, 99, 0.42);
  border-radius: 8px;
  padding: 5px;
}

.label {
  letter-spacing: 0.06em;
  font-size: 10px;
  color: #d6ddb4;
}

.hud-block strong {
  font-size: 14px;
  overflow-wrap: anywhere;
  color: #f7f6df;
}

.hearts {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.hearts img {
  width: 17px;
  height: 17px;
  image-rendering: pixelated;
}

.quest-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  padding: 8px 10px;
  border: 1px solid rgba(153, 176, 99, 0.5);
  border-radius: 8px;
  background: rgba(18, 27, 17, 0.68);
}

.quest-bar strong {
  font-size: 12px;
  line-height: 1.35;
}

.stage {
  position: relative;
  width: min(100%, calc((100dvh - 100px) * 1.454545));
  justify-self: center;
  min-width: 0;
  border: 3px solid #7b8f54;
  border-radius: 10px;
  overflow: hidden;
  box-shadow:
    inset 0 0 0 2px rgba(35, 48, 25, 0.9),
    0 12px 30px rgba(0, 0, 0, 0.45);
}

canvas {
  width: 100%;
  height: auto;
  display: block;
  image-rendering: pixelated;
  background: #1f4422;
  touch-action: none;
  outline-offset: -4px;
}

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  align-content: center;
  justify-items: center;
  text-align: center;
  gap: 12px;
  padding: 16px;
  background: rgba(6, 10, 6, 0.74);
  z-index: 3;
  overflow: auto;
}

.overlay.hidden {
  display: none;
}

.overlay h1 {
  margin: 0;
  font-size: clamp(22px, 3.5vw, 40px);
  color: var(--accent);
}

.overlay p {
  margin: 0;
  font-size: clamp(13px, 1.6vw, 17px);
  max-width: 640px;
}
.overlay button { min-height: 44px; }
.secondary-button { color: #e9eecb; border-color: #7b8f54; background: #1b2c1d; font-size: 13px; }

.dialog {
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: 10px;
  padding: 10px;
  border: 2px solid rgba(173, 190, 112, 0.8);
  border-radius: 8px;
  background: rgba(8, 12, 8, 0.92);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.4);
  z-index: 2;
  max-height: calc(100% - 20px);
  overflow-y: auto;
}

.dialog button { min-height: 44px; margin-top: 8px; font-size: 13px; }
.dialog-text { line-height: 1.35; }

.dialog.hidden {
  display: none;
}

.dialog-speaker {
  margin: 0 0 4px 0;
  color: #e4cc7d;
  font-weight: 700;
}

.dialog-text {
  margin: 0;
  color: #e8efd0;
}

.dialog-hint {
  margin: 6px 0 0 0;
  font-size: 12px;
  color: #c7d5aa;
}

.subscreen {
  position: absolute;
  top: 12px;
  right: 12px;
  width: min(260px, calc(100% - 24px));
  padding: 10px;
  border: 2px solid rgba(198, 174, 95, 0.9);
  border-radius: 8px;
  background: rgba(10, 14, 10, 0.94);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.45);
  z-index: 2;
  max-height: calc(100% - 24px);
  overflow-y: auto;
}

.subscreen.hidden {
  display: none;
}

.subscreen-title {
  margin: 0;
  color: #ecd18d;
  font-weight: 700;
}

.subscreen-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.subscreen-heading { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.subscreen-heading button { border-radius: 8px; min-width: 44px; min-height: 44px; padding: 4px 10px; font-size: 22px; }

.subscreen-grid button {
  border-radius: 8px;
  text-align: left;
  min-height: 44px;
  font-size: 12px;
  padding: 8px;
}

.subscreen-grid button.selected {
  outline: 2px solid rgba(244, 231, 167, 0.95);
}

.subscreen-hint {
  margin: 8px 0 0 0;
  font-size: 12px;
  color: #cfdaa8;
}

button {
  border: 2px solid #27371f;
  background: linear-gradient(180deg, #d6ba72, #b38a3c);
  color: #172017;
  font-weight: 700;
  font-size: 15px;
  padding: 9px 16px;
  border-radius: 999px;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

button:focus-visible, a:focus-visible, canvas:focus-visible { outline: 3px solid #fff2b6; outline-offset: 2px; }
.touch-controls { display: grid; gap: 14px; justify-items: center; border: 1px solid #536c43; background: rgba(18, 28, 18, .75); border-radius: 12px; padding: 12px 6px; user-select: none; -webkit-user-select: none; }
.direction-pad { display: grid; grid-template-columns: repeat(3, 44px); grid-template-rows: repeat(3, 44px); gap: 3px; touch-action: none; }
.direction-pad button { min-width: 44px; min-height: 44px; padding: 0; border-radius: 10px; font-size: 20px; touch-action: none; }
.direction.up { grid-column: 2; grid-row: 1; }
.direction.left { grid-column: 1; grid-row: 2; }
.direction.right { grid-column: 3; grid-row: 2; }
.direction.down { grid-column: 2; grid-row: 3; }
.pad-center { grid-column: 2; grid-row: 2; display: grid; place-items: center; color: #78905d; font-size: 24px; }
.action-pad { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; width: 100%; touch-action: none; }
.action-pad button { min-height: 48px; font-size: 13px; padding: 8px 5px; border-radius: 10px; touch-action: none; }
.action-pad .attack { grid-column: 1 / -1; min-height: 52px; background: linear-gradient(180deg, #eff3bb, #b4ce7c); }
.action-pad p { grid-column: 1 / -1; margin: 2px 0 0; color: #c7d5aa; font-size: 11px; text-align: center; line-height: 1.4; }
.touch-controls button.pressed { background: #fff0ab; transform: translateY(1px); box-shadow: 0 0 0 2px #eef6ce; }
button:disabled { opacity: .5; cursor: default; }

.controls {
  display: flex;
  gap: 8px 14px;
  flex-wrap: wrap;
  font-size: 13px;
  color: #d8e2bd;
  padding-inline: 2px;
}

.controls p {
  margin: 0;
}

kbd {
  background: #182818;
  border: 1px solid #607f56;
  border-radius: 6px;
  padding: 2px 6px;
  font-size: 12px;
}

@media (max-width: 700px) and (orientation: portrait), (max-width: 540px) {
  .app { gap: 8px; }
  .game-toolbar { gap: 5px; }
  .game-toolbar > strong { font-size: 14px; }
  .toolbar-actions { gap: 4px; }
  .toolbar-actions button { padding: 8px 10px; font-size: 13px; }
  .play-layout { grid-template-columns: minmax(0, 1fr); gap: 8px; }
  .dashboard { gap: 6px; }
  .hud { gap: 5px; padding: 5px; }
  .life-block { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 3px 6px; }
  .hud-grid { grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 4px; }
  .hud-block { gap: 2px; padding: 4px; }
  .hud-block strong { font-size: 12px; }
  .label { font-size: 9px; letter-spacing: .03em; }
  .hearts { gap: 3px; }
  .hearts img { width: 15px; height: 15px; }
  .quest-bar { padding: 6px 8px; gap: 6px; }
  .quest-bar strong { font-size: 12px; }
  .stage { width: 100%; }
  .touch-controls { display: flex; justify-content: space-around; align-items: center; gap: 12px; padding: 8px; }
  .action-pad { width: min(180px, 50%); }
  .action-pad button { min-height: 44px; }
  .action-pad .attack { min-height: 48px; }
  .controls { font-size: 11px; gap: 8px 10px; line-height: 1.7; }
  .controls kbd { font-size: 10px; padding: 2px 4px; }
  .dialog { left: 6px; right: 6px; bottom: 6px; padding: 8px; font-size: 12px; max-height: calc(100% - 12px); }
  .dialog button { margin-top: 6px; }
  .dialog-hint { display: none; }
  .subscreen { top: 6px; right: 6px; max-height: calc(100% - 12px); padding: 8px; }
  .subscreen-heading { margin-bottom: 5px; }
  .subscreen-hint { font-size: 10px; margin-top: 5px; }
}

@media (orientation: landscape) and (max-height: 540px) and (min-width: 541px) {
  .app { gap: 5px; padding-top: max(4px, env(safe-area-inset-top)); padding-bottom: max(4px, env(safe-area-inset-bottom)); }
  .game-toolbar { min-height: 44px; }
  .game-toolbar > strong { font-size: 15px; }
  .play-layout { grid-template-columns: 132px minmax(0, 1fr) 146px; gap: 6px; }
  .stage { width: min(100%, calc((100dvh - 60px - env(safe-area-inset-top) - env(safe-area-inset-bottom)) * 1.454545)); }
  .hud { gap: 4px; padding: 4px; }
  .hud-grid { gap: 3px; }
  .hud-block { padding: 3px; gap: 1px; }
  .hud-block strong { font-size: 12px; }
  .label { font-size: 9px; letter-spacing: .01em; }
  .hearts { gap: 2px; }
  .hearts img { width: 13px; height: 13px; }
  .quest-bar { padding: 5px; gap: 4px; }
  .quest-bar strong { font-size: 11px; }
  .touch-controls { padding: 7px 3px; gap: 9px; }
  .action-pad { gap: 4px; }
  .action-pad button, .action-pad .attack { min-height: 44px; font-size: 12px; }
  .action-pad p { font-size: 10px; }
  .controls { display: none; }
}

/* The full keyboard legend is redundant on a short portrait phone. Keep the
   playfield and every 44px touch target together without a page scroll. */
@media (orientation: portrait) and (max-width: 700px) and (max-height: 700px) {
  .app { gap: 6px; }
  .play-layout { gap: 6px; }
  .dashboard { gap: 5px; }
  .touch-controls { padding-block: 6px; }
  .controls { display: none; }
}
