/* The Mysterious Island — mobile-first layout around a crisp 7x7 pixel-art board. */
:root {
  --tile: 48px;
  --bg: #1d2a22;
  --bg2: #2b3a2f;
  --paper: #e9e4d2;
  --paper2: #d9d2b8;
  --ink: #2a2620;
  --muted: #6d665a;
  --accent: #3f7d4e;
  --accent-ink: #ffffff;
  --danger: #a33a2c;
  --line: #b9b097;
  --gold: #f2c94c;
  --radius: 10px;
  --font: "Trebuchet MS", "Segoe UI", system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow-x: hidden; }
body {
  background: var(--bg);
  color: var(--paper);
  font: 15px/1.4 var(--font);
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

#app { display: flex; flex-direction: column; min-height: 100vh; min-height: 100dvh; }

/* ---------- Top bar ---------- */
#hud {
  display: flex; align-items: center; gap: 10px;
  padding: 8px max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
  padding-top: max(8px, env(safe-area-inset-top));
  background: var(--bg2);
  border-bottom: 2px solid #121a15;
}
.hud-who { display: flex; align-items: center; gap: 8px; min-width: 0; flex: 1; }
.hud-icon { width: 28px; height: 28px; image-rendering: pixelated; }
.hud-name { font-weight: bold; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 30vw; }
.hud-place { font-size: 12px; color: #a9bba9; white-space: nowrap; }
.hud-cash { font-weight: bold; color: var(--gold); white-space: nowrap; }
.hud-btns { display: flex; gap: 6px; }
.hbtn {
  background: #3b4d40; border: 1px solid #57705d; border-radius: 8px;
  padding: 6px 10px; min-height: 36px; cursor: pointer;
}
.hbtn:hover { background: #46604c; }
.hbtn:active { transform: translateY(1px); }

/* ---------- Stage ---------- */
#stage {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 16px; padding: 12px 16px max(12px, env(safe-area-inset-bottom));
}
#board-wrap {
  position: relative;
  width: calc(var(--tile) * 7); height: calc(var(--tile) * 7);
  border: 3px solid #0e1511; border-radius: 4px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .45);
  background: #00ff00; /* grass, same as the canvas background */
}
#board { display: block; width: calc(var(--tile) * 7); height: calc(var(--tile) * 7); cursor: pointer; touch-action: manipulation; }
#board, .sprite, .chess-board img {
  image-rendering: pixelated; image-rendering: crisp-edges;
  -webkit-user-drag: none; user-select: none;
}

/* Startup loader */
#loading {
  position: fixed; inset: 0; z-index: 200; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 12px; background: var(--bg);
}
.loading-title { margin: 0; font-size: 20px; font-weight: bold; color: var(--paper); }
#loading .progress { width: min(240px, 70vw); background: #2f3f34; }
.progress .bar.manual { animation: none; transition: width .1s linear; }

/* ---------- D-pad ---------- */
#controls {
  display: grid;
  grid-template-columns: 64px 64px 64px;
  grid-template-rows: 56px 56px 56px;
  grid-template-areas: ". up ." "left . right" ". down .";
  gap: 4px;
}
.dir {
  border: 0; border-radius: 12px; background: #3b4d40; color: #dfe9dc;
  font-size: 22px; cursor: pointer; touch-action: none;
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, .35);
}
.dir:active { background: #57705d; transform: translateY(2px); box-shadow: none; }
.dir.up { grid-area: up; } .dir.down { grid-area: down; } .dir.left { grid-area: left; } .dir.right { grid-area: right; }

@media (orientation: landscape) and (min-aspect-ratio: 23/20) {
  #stage { flex-direction: row; gap: 28px; }
  #controls { grid-template-columns: 60px 60px 60px; grid-template-rows: 60px 60px 60px; }
}
@media (pointer: fine) and (min-width: 900px) {
  #controls { opacity: .85; }
}

/* ---------- Panel (popup over the board) ---------- */
#panel {
  position: fixed; z-index: 20;
  left: 50%; top: calc(var(--hud-h, 56px) + (100dvh - var(--hud-h, 56px)) / 2);
  transform: translate(-50%, -50%);
  width: min(460px, calc(100vw - 24px));
  max-height: min(680px, calc(100dvh - var(--hud-h, 56px) - 24px));
  display: flex; flex-direction: column;
  background: var(--paper); color: var(--ink);
  border: 3px solid #5b5240; border-radius: var(--radius);
  box-shadow: 0 6px 18px rgba(0, 0, 0, .5), 0 0 0 100vmax rgba(8, 14, 10, .45);
  user-select: text; -webkit-user-select: text;
}
.panel-head {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 6px 6px 12px; border-bottom: 2px solid var(--line); background: var(--paper2);
  border-radius: 7px 7px 0 0;
}
.panel-head h2 { flex: 1; margin: 0; font-size: 17px; }
#panel-close {
  width: 36px; height: 36px; border-radius: 8px; border: 1px solid var(--line);
  background: #fff8e6; font-size: 22px; line-height: 1; cursor: pointer; color: var(--ink);
}
#panel-body { flex: 1 1 auto; min-height: 0; overflow-y: auto; padding: 10px 12px 12px; -webkit-overflow-scrolling: touch; }
#panel-body p { margin: 0 0 8px; }
.page-img { display: flex; justify-content: center; gap: 6px; margin: 2px 0 8px; }
.sprite { width: 32px; height: 32px; flex: none; }
.sprite.big { width: 48px; height: 48px; }

.btn-row { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 10px; }
.btn {
  min-height: 40px; padding: 8px 14px; border-radius: 8px; cursor: pointer;
  background: #fffaf0; color: var(--ink); border: 1px solid #a99f86;
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, .12);
}
.btn:hover:not([disabled]) { background: #fff; }
.btn:active:not([disabled]) { transform: translateY(1px); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: #2f5f3b; }
.btn.danger { background: var(--danger); color: #fff; border-color: #7a2a20; }
.btn[disabled] { opacity: .45; cursor: not-allowed; }
.btn.small { min-height: 34px; padding: 4px 10px; font-size: 14px; }
.btn:focus-visible, .hbtn:focus-visible, .dir:focus-visible, #panel-close:focus-visible { outline: 3px solid var(--gold); outline-offset: 1px; }

.section h3 { margin: 12px 0 4px; font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.rows { list-style: none; margin: 0; padding: 0; }
.row { display: flex; align-items: center; gap: 8px; padding: 4px 2px; border-bottom: 1px solid var(--paper2); min-height: 42px; }
.row.empty { color: var(--muted); }
.row-name { flex: 1; min-width: 0; }
.row-right { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.price { font-weight: bold; color: #4e5f2a; font-size: 14px; }
.muted { color: var(--muted); font-size: 13px; }
.cash-line { text-align: right; }
.hint { color: var(--muted); font-size: 13px; }
.warn { color: var(--danger); font-size: 14px; min-height: 1em; }
.center { text-align: center; }

.progress { height: 10px; border-radius: 5px; background: var(--paper2); overflow: hidden; margin: 8px 12px; }
.progress .bar { height: 100%; width: 0; background: var(--accent); animation: fill linear forwards; }
@keyframes fill { to { width: 100%; } }

/* Tasks */
.tasks { margin: 0; }
.tasks dt { font-weight: bold; margin-top: 8px; font-size: 13px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.tasks dd { margin: 2px 0 0; }
.tasks dd.done { color: var(--accent); font-weight: bold; }
.tips { margin: 0; padding-left: 18px; font-size: 14px; }

/* Pet fish */
.row-name { display: flex; flex-direction: column; }
.row-sub { color: var(--muted); font-size: 12px; line-height: 1.3; }
.pet-name { align-self: flex-start; border-radius: 4px; }
.pet-name.champion {
  background: linear-gradient(180deg, #ffe58a, #e8b423); color: #3b2a00;
  padding: 0 6px; font-weight: bold; box-shadow: 0 0 0 1px #b88a0e;
}
.name-form label { display: block; font-weight: bold; margin-bottom: 6px; }
.text-input {
  width: 100%; font: inherit; font-size: 17px; padding: 8px 10px; border-radius: 8px;
  border: 2px solid #a99f86; background: #fffdf6; color: var(--ink);
}
.text-input:focus { outline: 3px solid var(--gold); border-color: var(--accent); }

/* Journal */
.journal { display: flex; flex-direction: column; gap: 4px; }
.journal-text { min-height: 40vh; resize: vertical; line-height: 1.4; font-size: 16px; white-space: pre-wrap; }
.journal-count { align-self: flex-end; }

/* Arena */
.stages { display: flex; justify-content: center; gap: 4px; flex-wrap: wrap; margin-bottom: 6px; }
.stage { font-size: 12px; padding: 2px 8px; border-radius: 10px; background: var(--paper2); color: var(--muted); }
.stage.now { background: var(--accent); color: #fff; font-weight: bold; }
.stage.beat { text-decoration: line-through; }
.fighter { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: center; gap: 2px; }
.fighter .pet-name { align-self: center; font-weight: bold; }
.hpbar { width: 90%; height: 8px; border-radius: 4px; background: #cfc7ab; overflow: hidden; }
.hpfill { height: 100%; background: #4f9a5d; }
.arena { display: flex; align-items: center; justify-content: space-around; gap: 6px; text-align: center; margin: 4px 0 6px; }
.fighter .label { font-size: 13px; color: var(--muted); }
.fighter .hp { font-weight: bold; }
.vs { font-weight: bold; color: var(--muted); }
.log { font-size: 14px; text-align: center; }

/* Bible */
.chapters { display: grid; grid-template-columns: repeat(auto-fill, minmax(44px, 1fr)); gap: 6px; }
.verses { padding-left: 28px; margin: 0; font-size: 15px; line-height: 1.5; }
.verses li { margin-bottom: 4px; }

/* Chess */
.chess-wrap { display: flex; flex-direction: column; align-items: center; }
.chess-board { display: grid; grid-template-columns: repeat(8, minmax(0, 32px)); border: 2px solid #000; width: min(100%, 232px); }
.chess-board .sq { aspect-ratio: 1; padding: 0; border: 1px solid #000; display: flex; align-items: center; justify-content: center; cursor: pointer; }
.chess-board .sq.dark { background: #5b5b5b; }
.chess-board .sq.light { background: #9c9c9c; }
.chess-board .sq.sel { border-color: #fff000; box-shadow: inset 0 0 0 1px #fff000; }
.chess-board img { width: 75%; height: auto; }
.chess-controls { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; justify-content: center; margin-top: 8px; font-size: 14px; }
.chess-controls select { font: inherit; padding: 4px; }
.credit { font-size: 10px; color: var(--muted); margin-top: 8px !important; }
.credit a { color: var(--muted); }

/* ---------- Toast ---------- */
#toast {
  position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: #111a14; color: #fff; padding: 10px 16px; border-radius: 20px; z-index: 50;
  box-shadow: 0 4px 14px rgba(0, 0, 0, .4); max-width: calc(100vw - 32px); text-align: center;
}

/* ---------- Name prompt ---------- */
#intro {
  position: fixed; inset: 0; z-index: 100; display: flex; align-items: center; justify-content: center;
  padding: 16px; background: radial-gradient(circle at 50% 30%, #3a5444, #121a15);
}
.intro-card {
  width: min(360px, 100%); background: var(--paper); color: var(--ink);
  border: 3px solid #5b5240; border-radius: 14px; padding: 22px 20px; text-align: center;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .5);
}
.intro-art { width: 72px; height: 72px; image-rendering: pixelated; }
.intro-card h1 { margin: 8px 0 0; font-size: 24px; }
.intro-sub { margin: 2px 0 16px; color: var(--muted); }
.intro-card label { display: block; margin-bottom: 6px; font-weight: bold; }
.intro-card input {
  width: 100%; font: inherit; font-size: 17px; padding: 10px 12px; border-radius: 8px;
  border: 2px solid #a99f86; background: #fffdf6; color: var(--ink); user-select: text;
}
.intro-card input:focus { outline: 3px solid var(--gold); border-color: var(--accent); }
.intro-card .btn { width: 100%; margin-top: 4px; font-size: 17px; }

@media (max-width: 380px) {
  .hbtn { padding: 6px 8px; font-size: 14px; }
  .hud-place { display: none; }
  #controls { grid-template-columns: 58px 58px 58px; grid-template-rows: 50px 50px 50px; }
}
