:root {
  color-scheme: dark;
  --ink: #f8f3d6;
  --muted: #c8bd8a;
  --line: #6f5c38;
  --panel: #1b1722;
  --panel-2: #27213a;
  --gold: #f0c35b;
  --red: #e4574f;
  --green: #5ec467;
  --blue: #5aa9e6;
  --black: #07070b;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}
html, body { height: 100%; margin: 0; overflow: hidden; }
body {
  background:
    radial-gradient(circle at 20% 18%, rgba(240,195,91,.14), transparent 26%),
    linear-gradient(180deg, #111425 0%, #080910 100%);
  color: var(--ink);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  touch-action: none;
  user-select: none;
}

#app {
  min-height: 100dvh;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  gap: 14px;
  padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
}

.game-wrap {
  min-width: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 10px;
}

.titlebar, .side, .message, .battle, .menu {
  background: linear-gradient(180deg, rgba(39,33,58,.98), rgba(17,16,26,.98));
  border: 3px solid var(--line);
  box-shadow: 0 0 0 2px #0c0a10, inset 0 0 0 2px rgba(255,255,255,.05);
}

.titlebar {
  min-height: 48px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  gap: 12px;
}

h1 {
  margin: 0;
  font-size: 18px;
  line-height: 1.2;
  letter-spacing: 0;
  color: var(--gold);
  text-shadow: 2px 2px 0 #000;
  white-space: nowrap;
}

.place {
  color: var(--muted);
  font-size: 13px;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.stage {
  position: relative;
  min-height: 0;
  display: grid;
  place-items: center;
}

canvas {
  width: min(100%, calc((100dvh - 170px) * 1.333));
  max-height: calc(100dvh - 170px);
  aspect-ratio: 4 / 3;
  image-rendering: pixelated;
  background: #1b5f42;
  border: 4px solid #0c0a10;
  box-shadow: 0 0 0 3px var(--line), 0 18px 45px rgba(0,0,0,.42);
}

.message {
  min-height: 86px;
  padding: 10px 12px;
  font-size: 15px;
  line-height: 1.55;
}

.message strong { color: var(--gold); font-weight: 800; }

.side {
  padding: 12px;
  display: grid;
  align-content: start;
  gap: 12px;
  overflow: auto;
}

.stat-title {
  color: var(--gold);
  font-size: 14px;
  margin-bottom: 8px;
}

.stat-row {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  font-size: 14px;
  line-height: 1.8;
  border-bottom: 1px solid rgba(255,255,255,.08);
}

.bar {
  height: 10px;
  background: #100f17;
  border: 1px solid var(--line);
  margin-top: 5px;
  overflow: hidden;
}

.fill { height: 100%; background: var(--green); }
.fill.mp { background: var(--blue); }

.quest {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.55;
}

.party-list {
  display: grid;
  gap: 7px;
}

.party-member {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 5px 7px;
  background: rgba(0,0,0,.18);
  border: 1px solid rgba(255,255,255,.08);
  font-size: 13px;
  line-height: 1.35;
}

.party-member span:last-child {
  color: var(--muted);
  text-align: right;
}

.battle, .menu {
  position: absolute;
  inset: 12px;
  display: none;
  grid-template-rows: minmax(0, 1fr) auto;
  padding: 14px;
  z-index: 4;
  overflow: hidden;
}

.battle.open, .menu.open { display: grid; }

.battle > div:first-child, .menu > div:first-child {
  min-height: 0;
  overflow: auto;
}

.battle-scene {
  display: grid;
  grid-template-columns: 1fr 1fr;
  align-items: center;
  gap: 12px;
  min-height: 0;
}

.enemy-card, .hero-card {
  min-height: 190px;
  display: grid;
  place-items: center;
  border: 2px solid rgba(255,255,255,.12);
  background: rgba(0,0,0,.18);
  padding: 10px;
  text-align: center;
}

.sprite-big {
  width: 132px;
  height: 132px;
  image-rendering: pixelated;
  margin: 0 auto 8px;
}

.commands {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  margin-top: 12px;
}

button {
  appearance: none;
  -webkit-appearance: none;
  border: 2px solid var(--line);
  background: #17131f;
  color: var(--ink);
  font: inherit;
  min-height: 44px;
  padding: 8px;
  box-shadow: inset 0 -3px 0 rgba(0,0,0,.35);
  touch-action: manipulation;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

button:active, button.pressed {
  transform: translateY(1px);
  background: #332944;
}

button[disabled] {
  color: #827860;
  opacity: .55;
}

.controls {
  display: none;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 0;
  align-items: center;
  min-height: 162px;
  width: 100%;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

.stick {
  --stick-x: 0px;
  --stick-y: 0px;
  position: relative;
  justify-self: center;
  width: 154px;
  height: 154px;
  border-radius: 50%;
  border: 3px solid rgba(20,18,28,.92);
  background:
    radial-gradient(circle at 50% 50%, rgba(248,243,214,.08) 0 25%, transparent 26%),
    radial-gradient(circle at 50% 50%, rgba(240,195,91,.18) 0 38%, rgba(20,18,30,.78) 39% 100%);
  box-shadow:
    inset 0 2px 0 rgba(255,255,255,.18),
    inset 0 -8px 0 rgba(0,0,0,.22),
    0 8px 18px rgba(0,0,0,.28);
  touch-action: none;
  overflow: hidden;
}

.stick::before, .stick::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  background: rgba(248,243,214,.14);
  transform: translate(-50%, -50%);
  pointer-events: none;
}

.stick::before {
  width: 104px;
  height: 14px;
  border-radius: 999px;
}

.stick::after {
  width: 14px;
  height: 104px;
  border-radius: 999px;
}

.stick-thumb {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 58px;
  height: 58px;
  border-radius: 50%;
  border: 2px solid #4b1027;
  background:
    linear-gradient(135deg, rgba(255,255,255,.20), transparent 35%),
    #8b1f45;
  box-shadow:
    inset -4px -5px 0 rgba(0,0,0,.28),
    inset 3px 3px 0 rgba(255,255,255,.18),
    0 5px 0 #34101f;
  transform: translate(-50%, -50%) translate(var(--stick-x), var(--stick-y));
  transition: transform 90ms ease-out;
  pointer-events: none;
}

.stick.active .stick-thumb {
  transition: none;
  box-shadow:
    inset -3px -4px 0 rgba(0,0,0,.30),
    inset 2px 2px 0 rgba(255,255,255,.16),
    0 2px 0 #34101f;
}

.stick[data-dir="up"]::after,
.stick[data-dir="down"]::after,
.stick[data-dir="left"]::before,
.stick[data-dir="right"]::before {
  background: rgba(240,195,91,.32);
}

.actions button {
  border-radius: 999px;
  background: rgba(20,18,30,.88);
  backdrop-filter: blur(8px);
  font-weight: 900;
  font-size: 18px;
}

.actions {
  display: grid;
  grid-template-columns: repeat(2, 64px);
  grid-template-rows: 64px 30px;
  grid-template-areas:
    "b a"
    "menu menu";
  gap: 9px 12px;
  justify-self: center;
  justify-content: center;
  align-items: center;
  padding: 12px 10px 10px;
  border: 2px solid rgba(44,43,52,.8);
  border-radius: 8px 8px 22px 22px;
  background: linear-gradient(180deg, rgba(204,202,187,.92), rgba(121,126,119,.92));
  box-shadow: inset 0 2px 0 rgba(255,255,255,.26), inset 0 -5px 0 rgba(0,0,0,.18), 0 8px 18px rgba(0,0,0,.28);
}

.actions [data-act="a"] {
  grid-area: a;
  width: 64px;
  height: 64px;
  min-height: 64px;
  padding: 0;
  background: #8b1f45;
  border-color: #4b1027;
  box-shadow: inset -4px -5px 0 rgba(0,0,0,.28), inset 3px 3px 0 rgba(255,255,255,.18), 0 5px 0 #34101f;
}

.actions [data-act="b"] {
  grid-area: b;
  width: 64px;
  height: 64px;
  min-height: 64px;
  padding: 0;
  background: #8b1f45;
  border-color: #4b1027;
  box-shadow: inset -4px -5px 0 rgba(0,0,0,.28), inset 3px 3px 0 rgba(255,255,255,.18), 0 5px 0 #34101f;
}

.actions [data-act="menu"] {
  grid-area: menu;
  justify-self: center;
  min-height: 26px;
  width: 92px;
  padding: 3px 8px;
  border-radius: 999px;
  background: #242630;
  border-color: #101118;
  color: #d9d5c2;
  font-size: 11px;
  box-shadow: inset 0 -2px 0 rgba(0,0,0,.45);
}

.hint {
  align-self: center;
  justify-self: center;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.35;
  text-align: center;
}

.log {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.55;
  max-height: 170px;
  overflow: auto;
}

.image-tool {
  display: grid;
  gap: 8px;
}

.image-tool.compact {
  margin-top: 14px;
}

.image-tool canvas {
  width: 100%;
  max-width: 192px;
  height: auto;
  aspect-ratio: 4 / 3;
  background: #0d0b12;
  border: 2px solid rgba(240,195,91,.62);
  box-shadow: 0 0 0 2px #0c0a10, inset 0 0 0 2px rgba(255,255,255,.05);
}

.tool-actions {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 7px;
}

.tool-actions a {
  display: grid;
  place-items: center;
  min-height: 38px;
  padding: 7px 8px;
  border: 2px solid var(--line);
  background: #17131f;
  color: var(--ink);
  text-decoration: none;
  box-shadow: inset 0 -3px 0 rgba(0,0,0,.35);
  font-size: 13px;
}

@media (pointer: coarse), (max-width: 860px) {
  #app {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 1fr) auto;
    gap: 8px;
    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));
  }
  .side { display: none; }
  .game-wrap { grid-template-rows: auto auto auto auto; align-content: start; }
  .stage { align-items: start; }
  .controls {
    position: fixed;
    left: max(8px, env(safe-area-inset-left));
    right: max(8px, env(safe-area-inset-right));
    bottom: max(8px, env(safe-area-inset-bottom));
    z-index: 20;
    display: grid;
  }
  .hint { display: none; }
  .message { min-height: 74px; font-size: 13px; line-height: 1.45; padding: 8px 10px; }
  .titlebar { min-height: 40px; padding: 6px 9px; }
  h1 { font-size: 14px; }
  .place { font-size: 11px; }
  canvas { width: min(100%, calc(100dvh - 290px) * 1.333); max-height: calc(100dvh - 290px); }
      .battle, .menu { inset: 6px; padding: 10px; }
      .menu {
        position: fixed;
        inset: max(8px, env(safe-area-inset-top)) 8px calc(178px + env(safe-area-inset-bottom)) 8px;
        z-index: 12;
      }
      .battle-scene { grid-template-columns: 1fr; gap: 8px; }
      .enemy-card, .hero-card { min-height: 118px; }
      .sprite-big { width: 82px; height: 82px; }
      .commands { grid-template-columns: repeat(2, minmax(0, 1fr)); }
      .image-tool.compact canvas { max-width: 160px; }
      button { min-height: 42px; font-size: 13px; }
    }

@media (max-height: 620px) and (orientation: landscape) {
  #app { grid-template-columns: minmax(0,1fr) 240px; grid-template-rows: 1fr; }
  .controls { position: fixed; left: 10px; right: 10px; bottom: 10px; pointer-events: none; }
  .controls button { pointer-events: auto; opacity: .86; }
  .hint { display: none; }
  canvas { max-height: calc(100dvh - 92px); }
  .message { min-height: 58px; }
  .side { display: grid; }
}
