:root {
  color-scheme: dark; /* the page is already dark; stops automatic dark mode repainting tile ink */
  --slate: #1F2A36;
  --slate-light: #2E3D4D;
  --wood: #B07A45;
  --paper: #F3E9D2;
  --ink: #1B1B1B;
  --moss: #6AAA64;
  --amber: #E0B040;
  --brick: #C8553D;
  --chalk: #DCE3EA;
  /* fallback only: src/main.js sets --tile from the ladder's measured space on load and resize */
  --tile: min(13vw, 4.6vh, 52px);
  --gap: calc(var(--tile) * 0.12);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--slate) url("assets/art/background.png") repeat;
  color: var(--chalk);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

/* No text selection from long presses or drags on letters (CrazyGames asks for this on body). */
body {
  -webkit-user-select: none;
  user-select: none;
}

#app {
  height: 100%;
  max-width: 560px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) 8px env(safe-area-inset-bottom);
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  touch-action: manipulation;
}
button:focus-visible { outline: 3px solid var(--amber); outline-offset: 2px; }

.bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 4px 8px;
  padding: 6px 0;
  border-bottom: 1px solid var(--slate-light);
}
.brand { display: flex; align-items: center; gap: 8px; }
.brand h1 { font-size: 1.15rem; margin: 0; letter-spacing: 0.04em; }
.emblem {
  width: 26px; height: 26px;
  background: url("assets/art/emblem.png") center / contain no-repeat;
}
.meta { font-size: 0.9rem; opacity: 0.85; }
.tools { display: flex; gap: 4px; width: 100%; justify-content: space-between; }
.tools button {
  flex: 1;
  min-width: 0;
  padding: 0 4px;
  min-height: 36px;
  background: var(--slate-light);
  border: 1px solid #40536a;
  border-radius: 8px;
  font-size: 0.85rem;
}
.tools button:disabled { opacity: 0.4; cursor: default; }
#btn-sound[aria-pressed="false"] { text-decoration: line-through; opacity: 0.7; }

.ladder-wrap {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.ladder {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  padding: var(--gap) calc(var(--tile) * 0.7);
}
/* the two rails */
.ladder::before, .ladder::after {
  content: "";
  position: absolute;
  top: 0; bottom: 0;
  width: calc(var(--tile) * 0.28);
  background: url("assets/art/rail.png") repeat-y;
  background-size: 100% auto;
}
.ladder::before { left: calc(var(--tile) * 0.2); }
.ladder::after { right: calc(var(--tile) * 0.2); }

.row {
  position: relative;
  z-index: 1;
  display: flex;
  gap: var(--gap);
  justify-content: center;
}
/* the rung behind each row */
.row::before {
  content: "";
  position: absolute;
  left: calc(var(--tile) * -0.55);
  right: calc(var(--tile) * -0.55);
  top: 50%;
  height: calc(var(--tile) * 0.22);
  transform: translateY(-50%);
  z-index: -1;
  background: url("assets/art/rung.png") center / 100% 100% no-repeat;
}
.tile {
  width: var(--tile);
  height: var(--tile);
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: calc(var(--tile) * 0.55);
  text-transform: uppercase;
  border-radius: 6px;
  color: var(--ink);
  background: var(--slate-light);
  border: 2px solid #40536a;
  transition: background-color 120ms, transform 120ms;
}
.row.filled .tile, .row.active .tile.has {
  background: var(--paper) url("assets/art/tile.png") center / cover no-repeat;
  border-color: #d8c9a6;
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.35);
}
.row.active .tile { border-color: var(--amber); }
.row.active .tile.has { transform: scale(1.04); }
.row.goal .tile { background: var(--moss); border-color: #4e8a49; color: var(--ink); }
.row.start .tile { background: var(--paper) url("assets/art/tile.png") center / cover no-repeat; border-color: var(--wood); }
.row.won .tile { background: var(--moss); border-color: #4e8a49; color: var(--ink); }
.tile.changed { box-shadow: inset 0 -4px 0 var(--amber), 0 2px 0 rgba(0, 0, 0, 0.35); }
.row.shake { animation: shake 320ms; }
@keyframes shake {
  20%, 60% { transform: translateX(-6px); }
  40%, 80% { transform: translateX(6px); }
}

.message {
  min-height: 1.4em;
  margin: 4px 0;
  text-align: center;
  font-size: 0.95rem;
}
.message.error { color: #f08a74; }
.message.good { color: #9fd49a; }

/* Keys touch edge to edge: the visible gap is a transparent border, so every pixel of a row is a
   hit target. Ten keys cannot be 44px wide on a 390px phone; height carries the 44px minimum. */
.keyboard {
  display: flex;
  flex-direction: column;
  margin: 0 -8px; /* reclaim #app's side padding so ten keys stay >= 36px wide at 360px */
  padding-bottom: 6px;
  user-select: none;
}
.krow { display: flex; justify-content: center; }
.key {
  flex: 1;
  min-width: 0;
  padding: 0;
  max-width: 50px;
  min-height: 54px;
  border: 3px solid transparent;
  border-radius: 9px;
  background: #4a5d72 padding-box;
  font-weight: 600;
  text-transform: uppercase;
}
.key.wide { max-width: 78px; flex: 1.6; font-size: 0.8rem; }
.key:active, .key.pressed { background: var(--wood) padding-box; }
/* drag input (v1.1): keys and the rung being built must not scroll or zoom the page under a finger */
.key, .row.active .tile { touch-action: none; }
.row.active .tile.drop-over { background-color: var(--amber); transform: scale(1.1); }
.drag-ghost {
  position: fixed;
  z-index: 50;
  pointer-events: none;
  transform: translate(-50%, -90%) scale(1.15);
  background: var(--paper) url("assets/art/tile.png") center / cover no-repeat;
  border-color: var(--wood);
  box-shadow: 0 6px 12px rgba(0, 0, 0, 0.45);
}

.panel {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(10, 16, 22, 0.72);
  z-index: 10;
  padding: 16px;
}
.panel[hidden] { display: none; }
.card {
  width: min(420px, 100%);
  max-height: 100%;
  overflow: auto;
  background: var(--slate-light);
  border: 2px solid var(--wood);
  border-radius: 14px;
  padding: 18px 20px;
  text-align: center;
}
.card h2 { margin: 0 0 8px; }
.card p { line-height: 1.45; }
.example { font-weight: 700; letter-spacing: 0.06em; color: var(--paper); }
.keys { font-size: 0.8rem; opacity: 0.75; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.actions button, #help-close {
  min-height: 42px;
  padding: 0 14px;
  border-radius: 8px;
  border: 1px solid #40536a;
  background: var(--slate);
}
button.primary, #help-close.primary { background: var(--moss); border-color: #4e8a49; color: #fff; font-weight: 700; }
.actions button[hidden] { display: none; }
.stars { display: flex; gap: 0.15em; justify-content: center; min-height: 2.2rem; }
.stars span {
  width: 2.2rem; height: 2.2rem;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
}
.stars .on { background-image: url("assets/art/star-full.png"); }
.stars .off { background-image: url("assets/art/star-empty.png"); }
.solution { font-weight: 700; color: var(--paper); letter-spacing: 0.04em; }

/* Levels list: six across, every cell at least 44px tall; locked cells are dimmed and inert. */
.level-grid {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 6px;
  margin: 8px 0 14px;
}
.lvl {
  min-height: 52px;
  padding: 4px 0;
  border-radius: 8px;
  border: 2px solid #40536a;
  background: var(--slate);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  font-weight: 700;
}
.lvl.open { border-color: var(--amber); }
.lvl.done { background: var(--paper); border-color: var(--wood); color: var(--ink); }
.lvl.current { outline: 3px solid var(--amber); outline-offset: 1px; }
.lvl.locked { opacity: 0.4; cursor: default; }
.mini-stars { display: flex; gap: 1px; }
.mini-stars span {
  width: 11px; height: 11px;
  background: url("assets/art/star-empty.png") center / contain no-repeat;
}
.mini-stars .on { background-image: url("assets/art/star-full.png"); }
.lvl.locked .mini-stars { visibility: hidden; }
.emblem.big { display: inline-block; width: 64px; height: 64px; }
.stats, .next { font-size: 0.85rem; opacity: 0.8; }

@media (min-width: 700px) and (min-height: 700px) {
  .brand h1 { font-size: 1.4rem; }
}

/* Short landscape screens (phones on their side, small laptops): a single column cannot hold twelve rungs and a
   44px-tall keyboard in ~390px, so the ladder takes the left column at full height and the
   keyboard sits beside it. */
@media (orientation: landscape) and (max-height: 720px) {
  #app {
    max-width: 960px;
    display: grid;
    grid-template-columns: minmax(0, 2fr) minmax(0, 3fr);
    grid-template-rows: auto minmax(0, 1fr) auto auto minmax(0, 1fr);
    column-gap: 16px;
    padding-left: max(8px, env(safe-area-inset-left));
    padding-right: max(8px, env(safe-area-inset-right));
  }
  .bar { grid-column: 1 / -1; flex-wrap: nowrap; }
  .meta { white-space: nowrap; }
  .tools { width: auto; flex: 1; max-width: 420px; }
  .ladder-wrap { grid-column: 1; grid-row: 2 / -1; }
  .message { grid-column: 2; grid-row: 3; }
  .keyboard { grid-column: 2; grid-row: 4; margin: 0; }
}
