:root {
  --bg: #070b12;
  --panel: #0b121b;
  --ink: #e8f0f7;
  --muted: #748394;
  --line: #263746;
  --mint: #73f0b0;
  --rose: #ff7088;
  --orange: #ffb45c;
  --grid-line: #1c2a38;
}

[data-theme="light"] {
  --bg: #faf8ef;
  --panel: #f1eee7;
  --ink: #3e3934;
  --muted: #8b8177;
  --line: #d8d0c5;
  --mint: #38b000;
  --rose: #e63946;
  --orange: #f77f00;
  --grid-line: #dfd8cb;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--ink);
  font: 12px ui-monospace, SFMono-Regular, Consolas, monospace;
}
body { display: flex; justify-content: center; }
button, a, select { -webkit-tap-highlight-color: transparent; }
.page { width: min(600px, 100%); padding: 18px 15px calc(20px + env(safe-area-inset-bottom)); }

header.topbar {
  min-height: 45px;
  border-bottom: 1px solid var(--line);
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}
header h1 { font-size: 19px; letter-spacing: .14em; margin: 0; white-space: nowrap; }
header a, header button {
  border: 0;
  background: none;
  color: var(--muted);
  font: 10px inherit;
  letter-spacing: .1em;
  text-decoration: none;
  min-height: 44px;
  cursor: pointer;
}
.header-actions { display: flex; justify-content: flex-end; gap: 11px; }

.toolbar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 7px;
  margin-bottom: 10px;
}
.tool-select, .stat-pill {
  border: 1px solid var(--line);
  background: var(--panel);
  padding: 6px 8px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  font-size: 9px;
  color: var(--muted);
  letter-spacing: .08em;
  min-width: 0;
}
.tool-select select {
  border: 0;
  background: none;
  color: var(--mint);
  font: 700 11px inherit;
  outline: none;
  cursor: pointer;
  padding: 0;
}
.stat-pill b {
  font-size: 11px;
  color: var(--mint);
  white-space: nowrap;
}

.frame {
  position: relative;
  width: min(100%, 480px);
  aspect-ratio: 1 / 1;
  margin: auto;
  border: 1px solid var(--line);
  padding: 4px;
  background: #111b26;
  box-shadow: 5px 5px #030609;
}
[data-theme="light"] .frame {
  background: #e6dfd3;
  box-shadow: 5px 5px #d3cab8;
}

.frame canvas {
  display: block;
  width: 100%;
  height: 100%;
  background: #080e16;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
[data-theme="light"] .frame canvas {
  background: #f7f4ec;
}

.overlay {
  position: absolute;
  inset: 4px;
  background: #050910ee;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 15px;
  text-align: center;
}
[data-theme="light"] .overlay {
  background: #f5f1e8f2;
}
.overlay.hide { display: none; }
.overlay b { font-size: clamp(22px, 6vw, 36px); letter-spacing: .12em; text-shadow: 2px 2px #18372b; }
.overlay span { font-size: 10px; color: var(--muted); letter-spacing: .1em; }
.overlay button {
  border: 0;
  background: var(--mint);
  color: #06110c;
  font: 700 11px inherit;
  letter-spacing: .12em;
  min-height: 44px;
  padding: 12px 20px;
  box-shadow: 3px 3px #2a8158;
  cursor: pointer;
}

footer {
  margin-top: 14px;
  text-align: center;
  color: var(--muted);
  font-size: 9px;
  letter-spacing: .08em;
  line-height: 1.5;
}

.levels-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: none;
  padding: 16px;
  background: #050910e8;
  overflow: auto;
}
[data-theme="light"] .levels-overlay {
  background: #faf8efee;
}
.levels-overlay.show { display: flex; justify-content: center; align-items: flex-start; }
.levels-dialog {
  width: min(580px, 100%);
  margin: auto;
  border: 1px solid var(--line);
  background: var(--panel);
  box-shadow: 6px 6px #030609;
  padding: 14px;
}
.levels-header {
  min-height: 42px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--line);
  margin-bottom: 12px;
}
.levels-header b { font-size: 12px; letter-spacing: .12em; }
.levels-header span { color: var(--muted); font-size: 9px; letter-spacing: .08em; display: block; margin-top: 3px; }
.levels-header button {
  border: 0;
  background: none;
  color: var(--muted);
  font: 10px inherit;
  cursor: pointer;
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 7px;
}
.level-card {
  min-height: 72px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
  padding: 8px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  cursor: pointer;
}
.level-card:hover, .level-card:focus-visible { border-color: var(--mint); outline: none; }
.level-card.current { border-color: var(--orange); }
.level-card.completed { border-left: 3px solid var(--mint); }
.level-card:disabled { opacity: .4; cursor: not-allowed; }
.level-num { font-size: 9px; color: var(--muted); letter-spacing: .08em; }
.level-status { font-size: 9px; line-height: 1.2; color: var(--mint); }

@media (max-width: 520px) {
  .page { padding: 12px 10px max(18px, env(safe-area-inset-bottom)); }
  header.topbar { min-height: 40px; margin-bottom: 10px; }
  header h1 { font-size: 16px; }
  .toolbar { grid-template-columns: repeat(2, 1fr); gap: 6px; }
  .level-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
