:root {
  --soil: #5c3a21;
  --bark: #8b5a2b;
  --wood: #c48a4a;
  --plank: #e8c48a;
  --cream: #f6e6c3;
  --paper: #fff4d8;
  --leaf: #4a7c3f;
  --moss: #6aa84f;
  --ink: #3a2414;
  --muted: #7a5a3a;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: #2d1a0e;
  color: var(--ink);
  font-family: "Nunito", sans-serif;
  overflow: hidden;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}

#frame {
  min-height: 100%;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background:
    linear-gradient(180deg, #7ec8e3 0%, #b8e0f0 30%, #f7d9a8 62%, #c48a4a 100%);
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

#view {
  position: relative;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  box-shadow: 0 8px 0 #5c3a21, 0 18px 40px rgba(45, 26, 14, 0.45);
  border: 6px solid #8b5a2b;
  background: #1e2a3a;
}

#game {
  display: block;
  width: 960px;
  height: 540px;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #87c5e8;
}

@media (max-width: 1000px) {
  #game { width: min(640px, 100vw - 12px); height: auto; aspect-ratio: 16 / 9; }
}
@media (max-width: 680px) {
  #game { width: min(100vw - 8px, 100%); height: auto; aspect-ratio: 16 / 9; }
}

#touch {
  display: none;
  width: min(960px, 100%);
  padding: 8px 10px 12px;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
}

#touch.hidden { display: none !important; }

.dpad {
  display: grid;
  grid-template-columns: 52px 52px 52px;
  grid-template-rows: 52px 52px 52px;
  grid-template-areas:
    ". up ."
    "left . right"
    ". down .";
  gap: 4px;
}

.dpad .pad[data-dir="up"] { grid-area: up; }
.dpad .pad[data-dir="left"] { grid-area: left; }
.dpad .pad[data-dir="right"] { grid-area: right; }
.dpad .pad[data-dir="down"] { grid-area: down; }

.pad, .act {
  font-family: "Press Start 2P", monospace;
  font-size: 9px;
  color: #fff4d8;
  background: #8b5a2b;
  border: 3px solid #5c3a21;
  box-shadow: 0 3px 0 #3a2414;
  border-radius: 8px;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  margin: 0;
  padding: 0;
}

.pad {
  width: 52px;
  height: 52px;
}

.pad:active, .act:active, .pad.held, .act.held {
  background: #6aa84f;
  border-color: #3d5e32;
  transform: translateY(2px);
  box-shadow: 0 1px 0 #3a2414;
}

.actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.act {
  width: 64px;
  height: 64px;
  border-radius: 50%;
}

.act.use { background: #6aa84f; border-color: #3d5e32; }
.act.atk { background: #d47a6a; border-color: #8b4036; }
.act.pause {
  width: 44px;
  height: 44px;
  font-size: 8px;
  background: #c48a4a;
  border-color: #8b5a2b;
}

@media (pointer: coarse), (hover: none) {
  #touch:not(.hidden) { display: flex; }
  #view { width: min(100vw - 8px, 100%); }
  #game { width: 100%; height: auto; aspect-ratio: 16 / 9; }
  #toast { bottom: auto; top: 28px; font-size: 10px; max-width: 90%; white-space: normal; text-align: center; }
  .wood { width: min(300px, 94%); max-height: 80dvh; overflow: auto; }
}

@media (pointer: coarse) and (orientation: landscape) {
  #frame { flex-direction: row; align-items: stretch; }
  #view { flex: 1; display: flex; }
  #game { width: 100%; height: 100%; max-height: 100dvh; aspect-ratio: auto; object-fit: contain; }
  #touch {
    flex-direction: column;
    width: auto;
    justify-content: space-between;
    padding: 12px 10px;
  }
}

.hidden { display: none !important; }

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(45, 26, 14, 0.28);
  padding: 10px;
  touch-action: manipulation;
  -webkit-user-select: auto;
  user-select: auto;
}

.wood {
  background: #fff4d8;
  border: 4px solid #8b5a2b;
  outline: 2px solid #e8c48a;
  outline-offset: -8px;
  padding: 16px 18px 14px;
  width: min(280px, 92%);
  box-shadow: 0 4px 0 #5c3a21;
  text-align: center;
}

.wood h1 {
  font-family: "Press Start 2P", monospace;
  font-size: 14px;
  line-height: 1.5;
  color: #5c3a21;
  text-shadow: 2px 2px 0 #f4c430;
  margin: 6px 0 10px;
}

.wood h2 {
  font-family: "Press Start 2P", monospace;
  font-size: 9px;
  line-height: 1.5;
  color: #5c3a21;
  margin-bottom: 10px;
}

.kicker {
  font-size: 11px;
  font-weight: 800;
  color: #6aa84f;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.blurb, .hint {
  font-size: 12px;
  color: #7a5a3a;
  margin: 6px 0 10px;
}

label {
  display: block;
  font-size: 12px;
  font-weight: 800;
  text-align: left;
  margin-bottom: 4px;
}

input {
  width: 100%;
  padding: 6px 8px;
  border: 3px solid #c48a4a;
  background: #fff8e8;
  color: #3a2414;
  font-family: inherit;
  font-weight: 700;
  margin-bottom: 8px;
  touch-action: manipulation;
  -webkit-user-select: text;
  user-select: text;
}

button {
  font-family: inherit;
  font-weight: 800;
  background: #6aa84f;
  color: #fff4d8;
  border: 3px solid #3d5e32;
  padding: 6px 12px;
  cursor: pointer;
  margin-top: 6px;
}

button:hover { background: #4a7c3f; }

.row { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }

.error { color: #8b4036; font-size: 12px; min-height: 16px; font-weight: 700; }

.wood ul {
  text-align: left;
  font-size: 12px;
  margin: 8px 0 10px 16px;
}

#chest-list {
  list-style: none;
  margin: 8px 0;
  max-height: 90px;
  overflow: auto;
  text-align: left;
  font-size: 13px;
  font-weight: 700;
}

#chest-list li {
  border-bottom: 1px dashed #e8c48a;
  padding: 3px 0;
}

.dialog-box { text-align: left; }
#dialog-name { font-weight: 800; color: #4a7c3f; font-size: 12px; }
#dialog-text { font-size: 13px; margin: 6px 0 8px; }

#hud {
  position: absolute;
  left: 6px;
  right: 6px;
  top: 4px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  pointer-events: none;
  font-size: 10px;
  font-weight: 800;
  color: #fff4d8;
  text-shadow: 1px 1px 0 #5c3a21;
}

.hud-left, .hud-right { display: flex; flex-direction: column; gap: 2px; align-items: flex-start; }
.hud-right { align-items: flex-end; text-align: right; }

#hp-wrap { display: flex; align-items: center; gap: 4px; }
#hp-bar {
  width: 72px;
  height: 8px;
  background: #5c3a21;
  border: 1px solid #e8c48a;
}
#hp-fill {
  display: block;
  height: 100%;
  width: 100%;
  background: #d47a6a;
}

#buff-tag {
  background: #6aa84f;
  color: #fff4d8;
  padding: 1px 5px;
  border: 1px solid #3d5e32;
}

#toast {
  position: absolute;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%);
  background: #fff4d8;
  border: 3px solid #8b5a2b;
  color: #3a2414;
  font-size: 11px;
  font-weight: 800;
  padding: 4px 10px;
  white-space: nowrap;
}
