:root {
  --bg: #0f1115;
  --bg-2: #161a21;
  --card: #1b2029;
  --card-2: #20262f;
  --border: #2c3441;
  --text: #e6e9ef;
  --muted: #9aa4b2;
  --accent: #4cc2ff;
  --accent-2: #37b0e8;
  --radius: 12px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

/* The `hidden` attribute must always win — otherwise rules like
   .loading { display: flex } keep the loading screen up forever. */
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ---------- Loading screen ---------- */
.loading {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at 50% 30%, #1a2130 0%, var(--bg) 70%);
  z-index: 10;
  padding: 20px;
}
.loading-box { text-align: center; max-width: 340px; }
.gallows-icon { width: 64px; height: 64px; color: var(--accent); }
.gallows-icon.small { width: 34px; height: 34px; }
.loading h1 { font-size: 1.5rem; margin: 12px 0 4px; }
.loading p { color: var(--muted); margin: 0 0 16px; }
.spinner {
  width: 28px; height: 28px;
  margin: 0 auto;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- App shell ---------- */
.app {
  max-width: 960px;
  margin: 0 auto;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding: calc(var(--safe-top) + 12px) 16px calc(var(--safe-bottom) + 16px);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 2px 14px;
}
.brand { display: flex; align-items: center; gap: 10px; color: var(--accent); }
.brand-text { color: var(--text); line-height: 1.15; display: flex; flex-direction: column; }
.brand-text strong { font-size: 1.05rem; }
.brand-text small { color: var(--muted); font-size: 0.75rem; }
.status { color: var(--muted); font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.06em; }
.status.busy { color: var(--accent); }

/* ---------- Layout: single column on mobile ---------- */
.main {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr;
  gap: 14px;
  align-content: start;
}

/* ---------- Cards ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}
.concept p { margin: 0 0 10px; }
.concept p:last-child { margin-bottom: 0; }
.card-title { font-size: 1.15rem; margin: 0 0 10px; }
code {
  background: var(--card-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 1px 6px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.9em;
  color: var(--accent);
}

/* ---------- Editor ---------- */
.editor-wrap, .output-wrap { padding: 0; overflow: hidden; }
.editor-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  background: var(--card-2);
  border-bottom: 1px solid var(--border);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}
.editor-label { color: var(--accent); }
.editor {
  display: block;
  width: 100%;
  min-height: 200px;
  border: 0;
  background: var(--bg-2);
  color: #d6e2f0;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 16px; /* >=16px prevents iOS zoom-on-focus */
  line-height: 1.5;
  padding: 14px;
  resize: vertical;
  tab-size: 4;
  white-space: pre;
}
.editor:focus { outline: 2px solid var(--accent); outline-offset: -2px; }

/* ---------- Output ---------- */
.output {
  margin: 0;
  padding: 14px;
  background: var(--bg-2);
  color: #cfe6d6;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 15px;
  white-space: pre-wrap;
  word-break: break-word;
  min-height: 80px;
}
.output.empty { color: var(--muted); }

/* ---------- Command chips (no-syntax levels, e.g. L1) ---------- */
.commands-wrap { padding: 0; overflow: hidden; }
.commands {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 12px;
  background: var(--bg-2);
}
.cmd {
  font: inherit;
  font-size: 0.95rem;
  line-height: 1.3;
  padding: 10px 14px;
  border-radius: 8px;
  border: 1px solid #2a313c;
  background: #1d232d;
  color: #d7e1ee;
  cursor: pointer;
  transition: transform 0.04s ease, background 0.1s ease;
}
.cmd:hover:not(:disabled) { background: #242c38; }
.cmd:active:not(:disabled) { transform: scale(0.98); }
.cmd[disabled] {
  opacity: 0.55;
  cursor: default;
  background: #161b22;
  border-color: #22302b;
}
.cmd[disabled]::after { content: " ✓"; color: #6fbf95; }
.cmd:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ---------- Buttons ---------- */
.actions { display: flex; gap: 10px; }
.btn {
  min-height: 48px;
  min-width: 130px;
  border: 0;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.04s ease, filter 0.1s ease;
}
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--accent-2); color: #04202f; }
.btn-primary:hover:not(:disabled) { filter: brightness(1.1); }

/* ---------- Wide screens: editor and output side by side ---------- */
@media (min-width: 760px) {
  .main {
    grid-template-columns: 1.1fr 1fr;
    grid-template-areas:
      "concept  concept"
      "commands commands"
      "editor   output"
      "check    check"
      "actions  actions"
      "nav      nav";
    align-content: start;
  }
  .concept { grid-area: concept; }
  .commands-wrap { grid-area: commands; }
  .editor-wrap { grid-area: editor; }
  .output-wrap { grid-area: output; }
  .check-result-wrap { grid-area: check; }
  .actions { grid-area: actions; }
  .nav-row { grid-area: nav; }
  .editor, .output { min-height: 280px; }
}

/* ── Phase 2: level engine ──────────────────────────────── */
.progress-pill {
  font-size: 0.8rem;
  color: #9aa4b2;
  background: #1a1f27;
  border: 1px solid #2a313c;
  border-radius: 999px;
  padding: 5px 12px;
  white-space: nowrap;
}
.progress-pill b { color: #d7e1ee; }

.top-actions { display: flex; gap: 8px; }

.concept .card-title { margin: 0 0 8px; }
.concept p:last-child { margin-bottom: 0; }
.task { color: #c7d3e3; font-style: italic; }

.btn {
  font: inherit;
  cursor: pointer;
  border: 0;
  border-radius: 8px;
  padding: 12px 18px;
}
.btn[disabled] { opacity: 0.45; cursor: default; }
.btn-primary { background: #355f9e; color: #fff; }
.btn-primary:not([disabled]):hover { background: #3f6fb5; }
.btn-secondary { background: #232a35; color: #d7e1ee; }
.btn-secondary:not([disabled]):hover { background: #2b3442; }
.btn-check { background: #2f6b52; color: #fff; }
.btn-check:not([disabled]):hover { background: #387e61; }

.actions .btn { flex: 1; }
.actions .btn-primary { flex: 0 0 auto; min-width: 110px; }

.check-result {
  margin: 0 0 12px;
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 0.95rem;
}
.check-result.ok { background: #1d3a2c; color: #a8d8b9; border: 1px solid #2f6b52; }
.check-result.fail { background: #3c2420; color: #e8b4a8; border: 1px solid #7e3b2f; }
.check-result:last-child { margin-bottom: 0; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: #1d3a2c;
  color: #c9ecd6;
  border: 1px solid #2f6b52;
  border-radius: 8px;
  padding: 10px 16px;
  font-size: 0.9rem;
  z-index: 20;
}

.nav-row {
  display: flex;
  justify-content: space-between;
  gap: 8px;
}
.nav-row .btn { flex: 1; min-height: 44px; }

.check-result-wrap { margin: 0; }
.check-result-wrap:has(.check-result[hidden]) { display: none; }

.hint {
  border-left: 3px solid #355f9e;
  background: #182130;
  border-radius: 0 8px 8px 0;
  padding: 8px 12px;
  margin-top: 10px;
  font-size: 0.92rem;
  color: #c7d3e3;
}
.hint code { color: #9fc0e8; }

.check-tip { margin-top: 6px; font-size: 0.9rem; opacity: 0.9; }
.output-error { color: #f0a594; }
