:root {
  color-scheme: dark;
  --bg: #14161c;
  --panel: #1f232c;
  --border: #2d323e;
  --text: #e8e9ee;
  --muted: #9aa0ad;
  --brand: #f08a3c;
  --gem: #2fa8d8;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, 'Segoe UI', sans-serif;
}

.app {
  display: flex;
  flex-direction: column;
  gap: 8px;
  height: 100dvh;
  max-width: 1400px;
  margin: 0 auto;
  padding: max(8px, env(safe-area-inset-top)) 12px max(8px, env(safe-area-inset-bottom));
}

.topbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; min-height: 32px; }
.home-link { color: inherit; font-weight: 600; text-decoration: none; }
.account { display: flex; align-items: center; gap: 10px; min-width: 0; }
.account strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 16ch; }
.account a, .sign-in-link { color: var(--brand); font-weight: 600; }
.account button { min-height: 32px; padding: 4px 12px; }
.home-link:hover { text-decoration: underline; }

button {
  font: inherit;
  font-weight: 600;
  min-height: 40px;
  padding: 6px 14px;
  border: 0;
  border-radius: 10px;
  background: var(--brand);
  color: #1b1208;
  cursor: pointer;
}
button.quiet { background: transparent; color: var(--text); border: 1px solid var(--border); }
button:focus-visible, a:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }

.muted { color: var(--muted); }

/* Menu */
.menu { overflow-y: auto; }
.menu h1 { margin: 8px 0 4px; font-size: 2rem; }
.cave-list {
  list-style: none;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 10px;
}
.cave-list button {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  min-height: 56px;
  text-align: left;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
}
.cave-list button:hover { border-color: var(--brand); }
.cave-list .best { color: var(--muted); font-weight: 400; font-variant-numeric: tabular-nums; }
.cave-list .cleared .best::before { content: '✓ '; color: #7fd48a; }

/* Daily and practice */
.modes {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 10px;
  margin: 16px 0 8px;
}
.mode {
  padding: 14px 16px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.mode h2, .section-title { margin: 0 0 4px; font-size: 1.15rem; }
.section-title { margin-top: 16px; }
.mode p { margin: 0 0 10px; }
.practice-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.seed-label { color: var(--muted); }
#seed-input {
  width: 8ch;
  min-height: 40px;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.error { color: #ff8f80; margin: 8px 0 0 !important; }

.daily-board { margin-top: 12px; }
.board { list-style: none; margin: 0; padding: 0; font-variant-numeric: tabular-nums; }
.board li { display: grid; grid-template-columns: 2.2em 1fr auto; gap: 8px; padding: 2px 0; }
.board li span:nth-child(2) { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.board li.me { color: var(--brand); font-weight: 700; }
.note { color: var(--muted); margin: 8px 0 0; }

/* Play */
.play { display: flex; flex-direction: column; gap: 8px; flex: 1; min-height: 0; }
.hud { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px; }
.cave-name { font-weight: 700; margin-right: auto; }
.stat { display: inline-flex; align-items: center; gap: 6px; font-variant-numeric: tabular-nums; }
.gem { width: 12px; height: 12px; background: var(--gem); transform: rotate(45deg); border-radius: 2px; }

.stage {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
#board { display: block; image-rendering: pixelated; border-radius: 8px; }

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(10, 12, 16, 0.6);
}
.card {
  max-width: 360px;
  padding: 20px 24px;
  text-align: center;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
}
.card h2 { margin: 0 0 6px; }
.actions { display: flex; justify-content: center; gap: 10px; flex-wrap: wrap; margin-top: 14px; }

/* Touch controls: only on touch screens. */
.touch-controls { display: none; }
@media (pointer: coarse) {
  .touch-controls {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 8px 4px;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
  }
  .keys { display: none; }
}
.dpad {
  display: grid;
  grid-template-columns: repeat(3, 56px);
  grid-template-rows: repeat(3, 56px);
  gap: 4px;
}
.dpad button, .grab {
  min-height: 0;
  padding: 0;
  font-size: 1.2rem;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
}
.dpad [data-dir="up"] { grid-area: 1 / 2; }
.dpad [data-dir="left"] { grid-area: 2 / 1; }
.dpad [data-dir="right"] { grid-area: 2 / 3; }
.dpad [data-dir="down"] { grid-area: 3 / 2; }
.grab { width: 96px; height: 96px; border-radius: 50%; font-size: 1rem; }
.pressed { background: var(--brand) !important; color: #1b1208 !important; }
