@layer reset, base, components;

:root {
  --bg: #0b0d10;
  --surface: #151a21;
  --text: #e6e9ee;
  --muted: #9aa4b2;
  --accent: #5b9dff;
  --border: #232b35;

  --space-2: 0.5rem;
  --space-3: 1rem;
  --space-4: 1.5rem;
  --space-5: 2.5rem;
  --radius: 0.5rem;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@layer reset {
  *,
  *::before,
  *::after {
    box-sizing: border-box;
  }
  :where(html, body, h1, p) {
    margin: 0;
    padding: 0;
  }
  :where(button) {
    font: inherit;
    color: inherit;
  }
}

@layer base {
  html {
    color-scheme: dark;
  }
  body {
    min-height: 100dvh;
    background: var(--bg);
    color: var(--text);
    font-family: var(--font);
    line-height: 1.5;
  }
}

@layer components {
  .app {
    display: grid;
    gap: var(--space-5);
    max-width: 60rem;
    margin-inline: auto;
    padding: var(--space-5) var(--space-3);
  }

  .hero {
    display: grid;
    gap: 0.35rem;
    padding-block: var(--space-3) var(--space-2);
  }
  .hero h1 {
    font-size: clamp(2rem, 5vw, 3rem);
    letter-spacing: -0.02em;
    line-height: 1.1;
  }
  .tagline {
    color: var(--muted);
    font-size: 1.05rem;
  }

  button {
    cursor: pointer;
  }

  .grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(13rem, 1fr));
    gap: var(--space-3);
  }

  .demo {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    padding: var(--space-3);
    background: linear-gradient(160deg, var(--surface), color-mix(in srgb, var(--surface) 85%, #000));
    border: 1px solid var(--border);
    border-radius: var(--radius);
    transition: border-color 0.18s ease, transform 0.18s ease, box-shadow 0.18s ease;
  }
  .demo:hover {
    transform: translateY(-2px);
    border-color: color-mix(in srgb, var(--accent) 50%, var(--border));
    box-shadow: 0 8px 24px -12px color-mix(in srgb, var(--accent) 60%, transparent);
  }

  .play {
    padding: var(--space-3);
    background: var(--accent);
    color: #06101f;
    border: 0;
    border-radius: var(--radius);
    font-size: 1rem;
    font-weight: 600;
    text-decoration: none;
    transition: filter 0.15s ease;
  }
  .play:hover {
    filter: brightness(1.08);
  }

  .downloads {
    display: flex;
    gap: 0.4rem;
  }

  .dl {
    flex: 1 1 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.35rem;
    padding: 0.4rem 0.5rem;
    font-size: 0.8rem;
    line-height: 1;
    color: var(--muted);
    background: color-mix(in srgb, var(--bg) 50%, transparent);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    text-decoration: none;
    transition: border-color 0.15s ease, color 0.15s ease;
  }
  .dl:hover {
    border-color: var(--accent);
    color: var(--accent);
  }
  .dl-ico {
    display: inline-flex;
  }
  .dl-ico svg {
    display: block;
  }

  .overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: #000;
    z-index: 10;
  }
  .overlay-on {
    display: block;
  }

  .controls {
    position: absolute;
    left: var(--space-3);
    bottom: var(--space-3);
    z-index: 13;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.3rem;
    background: color-mix(in srgb, var(--surface) 88%, transparent);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    backdrop-filter: blur(6px);
  }
  .ctl-exit {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.3rem 0.45rem;
    background: transparent;
    color: var(--muted);
    border: 0;
    border-radius: 0.35rem;
    cursor: pointer;
  }
  .ctl-exit:hover {
    color: var(--text);
  }
  .ctl-exit svg {
    transform: scaleX(-1);
  }

  /* Code toggle */
  .toggle {
    background: transparent;
    color: var(--muted);
    border: 1px solid var(--border);
    border-radius: 0.4rem;
    padding: 0.35rem 0.85rem;
    font: inherit;
    font-size: 0.85rem;
    cursor: pointer;
  }
  .toggle:hover:not(.toggle-on) {
    color: var(--text);
  }
  .toggle-on {
    background: var(--accent);
    color: #06101f;
    border-color: var(--accent);
  }

  /* code font size controls — only meaningful while the code panel is open */
  .font-ctl {
    display: none;
    gap: 0.25rem;
    margin-left: 0.15rem;
    padding-left: 0.4rem;
    border-left: 1px solid var(--border);
  }
  .code-open .font-ctl {
    display: inline-flex;
  }
  .fontbtn {
    min-width: 1.9rem;
    padding: 0.35rem 0.5rem;
    background: transparent;
    color: var(--muted);
    border: 1px solid var(--border);
    border-radius: 0.4rem;
    font-size: 0.8rem;
    line-height: 1;
  }
  .fontbtn:hover {
    color: var(--text);
    border-color: var(--accent);
  }

  .stage {
    position: absolute;
    inset: 0;
  }
  .game-canvas {
    position: absolute;
    inset: 0;
    display: block;
    width: 100%;
    height: 100%;
  }

  /* Code is a left-pinned panel that shares layout with the canvas. Mobile:
     full width (covers the canvas, which stays full-size underneath). Desktop:
     40% on the left, canvas takes the remaining 60% (see media query below). */
  .codepanel {
    display: none;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    width: 100%;
    overflow: auto;
    background: var(--bg);
    z-index: 11;
  }
  .code-open .codepanel {
    display: block;
  }
  /* keep the vertical scrollbar; hide the horizontal one (long lines still
     scroll via trackpad/shift-wheel, there's just no bar taking up space). */
  .codepanel::-webkit-scrollbar {
    width: 10px;
    height: 10px;
  }
  .codepanel::-webkit-scrollbar:horizontal {
    height: 0;
  }
  .codepanel::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 5px;
  }
  .codepanel::-webkit-scrollbar-track {
    background: transparent;
  }
  .resizer {
    display: none;
    position: absolute;
    top: 0;
    bottom: 0;
    left: var(--code-w, 50%);
    width: 10px;
    margin-left: -5px;
    cursor: ew-resize;
    z-index: 12;
  }
  .resizer:hover {
    background: color-mix(in srgb, var(--accent) 35%, transparent);
  }
  @media (min-width: 768px) {
    .codepanel {
      width: var(--code-w, 50%);
      border-right: 1px solid var(--border);
    }
    .code-open .stage {
      left: var(--code-w, 50%);
    }
    .code-open .resizer {
      display: block;
    }
  }
  .codehost pre.ts {
    margin: 0;
    /* extra bottom padding so the floating control bar doesn't occlude the
       last lines when scrolled to the end */
    padding: var(--space-3) var(--space-3) 4rem;
    font-family: ui-monospace, "Cascadia Code", "Consolas", monospace;
    font-size: var(--code-fs, 12px);
    line-height: 1.55;
    min-height: 100%;
    background: #0d1117;
    color: #c9d1d9;
  }
  .codehost code {
    counter-reset: ln;
  }
  .codehost .line {
    counter-increment: ln;
  }
  .codehost .line::before {
    content: counter(ln);
    display: inline-block;
    width: 3rem;
    margin-right: var(--space-3);
    text-align: right;
    color: #566173;
    user-select: none;
  }

  /* tree-sitter token theme (github-dark) */
  .t-keyword { color: #ff7b72; }
  .t-type { color: #ffa657; }
  .t-function, .t-function-special { color: #d2a8ff; }
  .t-string { color: #a5d6ff; }
  .t-comment { color: #8b949e; font-style: italic; }
  .t-number { color: #79c0ff; }
  .t-constant { color: #79c0ff; }
  .t-property { color: #79c0ff; }
  .t-label { color: #ffa657; }
  .t-operator, .t-delimiter { color: #9aa4b2; }
  .t-variable { color: #c9d1d9; }

  /* WebGPU-unsupported notice on the catalog */
  .warning {
    padding: var(--space-3);
    background: color-mix(in srgb, #f78c6c 12%, var(--surface));
    border: 1px solid color-mix(in srgb, #f78c6c 45%, var(--border));
    border-radius: var(--radius);
    color: var(--text);
  }

  /* loading / error layer over the game */
  .status {
    display: none;
    position: absolute;
    inset: 0;
    z-index: 12;
    align-items: center;
    justify-content: center;
    background: color-mix(in srgb, var(--bg) 80%, transparent);
  }
  .status-on {
    display: flex;
  }
  .status-box {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    max-width: 28rem;
    padding: var(--space-3) var(--space-4);
    color: var(--text);
    text-align: center;
  }
  .status-err .status-box {
    flex-direction: column;
    color: #ffb4a3;
  }
  .spinner {
    width: 1.5rem;
    height: 1.5rem;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
  }

  /* crash dialog */
  .crash-box {
    display: grid;
    gap: var(--space-3);
    max-width: 26rem;
    padding: var(--space-4);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    text-align: center;
  }
  .crash-box h2 {
    font-size: 1.4rem;
  }
  .crash-box p {
    color: var(--muted);
  }
  .crash-details {
    text-align: left;
  }
  .crash-details summary {
    cursor: pointer;
    color: var(--muted);
    user-select: none;
  }
  .crash-details summary:hover {
    color: var(--text);
  }
  .crash-log {
    margin: var(--space-2) 0 0;
    max-height: 12rem;
    overflow: auto;
    padding: var(--space-2);
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 0.35rem;
    font-family: ui-monospace, "Cascadia Code", "Consolas", monospace;
    font-size: 0.75rem;
    color: var(--muted);
    white-space: pre-wrap;
    word-break: break-word;
  }
  .crash-actions {
    display: flex;
    justify-content: center;
    gap: var(--space-3);
  }
  .btn-primary {
    background: var(--accent);
    color: #06101f;
    border: 0;
    border-radius: 0.4rem;
    padding: 0.5rem 1.4rem;
    font: inherit;
    font-weight: 600;
    cursor: pointer;
  }
  .btn-ghost {
    background: transparent;
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 0.4rem;
    padding: 0.5rem 1.4rem;
    font: inherit;
    cursor: pointer;
  }
}

@keyframes spin {
  to { transform: rotate(360deg); }
}
