@layer reset, base, theme, components, utilities;

@font-face {
  font-family: "Roboto Mono";
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url("fonts/roboto-mono-latin-400-normal.woff2") format("woff2");
}

@font-face {
  font-family: "Roboto Mono";
  font-style: normal;
  font-weight: 700;
  font-display: swap;
  src: url("fonts/roboto-mono-latin-700-normal.woff2") format("woff2");
}

@layer theme {
  :root {
    --bg: #050814;
    --surface: rgb(11 16 34 / 92%);
    --surface-solid: #11182c;
    --border: #2a3656;
    --text: #eef2f8;
    --muted: #a6b4cc;
    --accent: #38bdf8;
    --accent-bright: #7dd3fc;
    --warm: #fb923c;
    --danger: #f87171;
    --success: #34d399;
    --gold: #facc15;
    --pink: #f472b6;
    --target-size: 36px;
  }

  @media (pointer: coarse) {
    :root {
      --target-size: 44px;
    }
  }
}

@layer base {
  html {
    touch-action: manipulation;
    overscroll-behavior: none;
    -webkit-tap-highlight-color: transparent;
  }

  html,
  body {
    height: 100dvh;
    width: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: "Roboto Mono", monospace;
    overflow: hidden;
    overscroll-behavior: none;
  }

  body {
    display: flex;
    flex-direction: column;
  }

  button {
    cursor: pointer;
    min-block-size: var(--target-size);
    min-inline-size: var(--target-size);
  }

  a:focus-visible,
  button:focus-visible,
  input:focus-visible + label {
    outline: 3px solid var(--accent);
    outline-offset: 2px;
  }
}

@layer components {
  .topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    width: 100%;
    padding-top: max(0.45rem, env(safe-area-inset-top, 0px));
    padding-bottom: 0.45rem;
    padding-left: max(0.75rem, env(safe-area-inset-left, 0px));
    padding-right: max(0.75rem, env(safe-area-inset-right, 0px));
    background: rgb(8 12 26 / 90%);
    border-bottom: 1px solid var(--border);
    z-index: 5;
  }

  .back {
    display: inline-flex;
    align-items: center;
    min-block-size: var(--target-size);
    color: var(--muted);
    font-size: 0.8rem;
    text-decoration: none;
    white-space: nowrap;
  }

  .back:hover,
  .back:focus-visible {
    color: var(--text);
  }

  .hud-summary {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.85rem;
    font-weight: 700;
    min-inline-size: 0;
  }

  .hud-chip {
    padding: 0.2rem 0.45rem;
    border-radius: 0.4rem;
    background: var(--surface-solid);
    border: 1px solid var(--border);
    color: var(--text);
    white-space: nowrap;
  }

  #hud-score {
    color: var(--gold);
  }

  #hud-lives {
    color: var(--accent-bright);
    letter-spacing: 0.08em;
  }

  .buttons {
    display: flex;
    gap: 0.4rem;
  }

  .buttons button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0 0.55rem;
    font-size: 1rem;
    background: var(--surface-solid);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
  }

  .buttons button[aria-pressed="true"] {
    border-color: var(--accent);
    background: rgb(56 189 248 / 18%);
  }

  .stage {
    position: relative;
    flex: 1;
    width: 100%;
    min-height: 0;
    overflow: hidden;
  }

  #game {
    width: 100%;
    height: 100%;
    display: block;
    background: var(--bg);
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
  }

  .overlay {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: min(92%, 430px);
    max-height: calc(100% - 1.5rem);
    overflow-y: auto;
    padding: 1.25rem;
    text-align: center;
    background: var(--surface);
    border: 1px solid #38bdf8;
    border-radius: 0.85rem;
    box-shadow: 0 0 36px rgb(56 189 248 / 22%);
    z-index: 10;
  }

  .overlay[hidden] {
    display: none;
  }

  .overlay h2 {
    font-size: clamp(1.5rem, 4.5vw, 2rem);
    letter-spacing: 0.06em;
    color: var(--accent-bright);
    text-shadow: 0 0 14px rgb(56 189 248 / 55%);
  }

  .subtitle {
    margin-top: 0.45rem;
    font-size: 0.85rem;
    color: var(--muted);
  }

  .score-readout {
    margin-top: 0.75rem;
    font-size: 1.45rem;
    font-weight: 700;
    color: var(--gold);
  }

  .score-readout[hidden] {
    display: none;
  }

  .difficulty-group {
    margin-top: 0.9rem;
    border: none;
    padding: 0;
  }

  .difficulty-legend {
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--muted);
    margin-bottom: 0.4rem;
  }

  .difficulty-options {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.45rem;
  }

  .difficulty-options input {
    position: absolute;
    opacity: 0;
    width: 1px;
    height: 1px;
    pointer-events: none;
  }

  .difficulty-options label {
    display: flex;
    align-items: center;
    justify-content: center;
    min-block-size: var(--target-size);
    padding: 0.45rem 0.5rem;
    font-size: 0.85rem;
    font-weight: 700;
    color: var(--muted);
    background: var(--surface-solid);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    cursor: pointer;
    user-select: none;
  }

  .difficulty-options input:checked + label {
    color: var(--bg);
    background: var(--accent);
    border-color: var(--accent-bright);
  }

  .overlay-help {
    margin-top: 0.9rem;
    font-size: 0.8rem;
    line-height: 1.45;
    color: var(--text);
  }

  .help-list,
  .legend {
    list-style: none;
    padding: 0;
    text-align: left;
  }

  .help-list li + li {
    margin-top: 0.35rem;
  }

  .legend {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.35rem 0.5rem;
    margin-top: 0.75rem;
  }

  .pu {
    display: inline-block;
    width: 1.35rem;
    height: 1.35rem;
    margin-right: 0.35rem;
    line-height: 1.35rem;
    text-align: center;
    font-weight: 700;
    color: var(--bg);
    border-radius: 50%;
  }

  .pu-s {
    background: #60a5fa;
  }

  .pu-r {
    background: var(--warm);
  }

  .pu-h {
    background: var(--success);
  }

  .pu-l {
    background: var(--pink);
  }

  .best {
    margin-top: 0.75rem;
    font-size: 0.82rem;
    color: var(--muted);
  }

  .best:empty {
    display: none;
  }

  .start {
    margin-top: 0.95rem;
    width: 100%;
    padding: 0.75rem 1.5rem;
    font-size: 1.05rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    color: var(--bg);
    background: var(--accent);
    border: none;
    border-radius: 0.55rem;
    box-shadow: 0 0 18px rgb(56 189 248 / 45%);
  }

  .start:hover {
    background: var(--accent-bright);
  }

  .touch-controls {
    position: absolute;
    inset: auto 0 0 0;
    display: none;
    align-items: flex-end;
    justify-content: space-between;
    padding-left: max(1rem, env(safe-area-inset-left, 0px));
    padding-right: max(1rem, env(safe-area-inset-right, 0px));
    padding-bottom: max(1rem, env(safe-area-inset-bottom, 0px));
    pointer-events: none;
    z-index: 6;
  }

  .stage[data-state="playing"][data-touch="true"] .touch-controls {
    display: flex;
  }

  @media (pointer: coarse) {
    .stage[data-state="playing"] .touch-controls {
      display: flex;
    }
  }

  .stick-zone {
    width: 136px;
    height: 136px;
    border-radius: 50%;
    pointer-events: auto;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
  }

  .stick-canvas {
    width: 100%;
    height: 100%;
    display: block;
    pointer-events: none;
  }

  .action-pad {
    display: flex;
    align-items: flex-end;
    gap: 0.85rem;
    pointer-events: auto;
  }

  .touch-btn {
    border-radius: 50%;
    font-weight: 700;
    letter-spacing: 0.04em;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    backdrop-filter: blur(4px);
  }

  .fire-btn {
    width: 82px;
    height: 82px;
    font-size: 0.9rem;
    color: #fff;
    background: rgb(248 113 113 / 28%);
    border: 2px solid rgb(248 113 113 / 85%);
    box-shadow: 0 0 18px rgb(248 113 113 / 30%);
  }

  .fire-btn[data-active="true"] {
    background: rgb(248 113 113 / 58%);
    transform: scale(0.95);
  }

  .warp-btn {
    width: 64px;
    height: 64px;
    font-size: 0.75rem;
    color: #e0f2fe;
    background: rgb(56 189 248 / 24%);
    border: 2px solid rgb(56 189 248 / 80%);
    box-shadow: 0 0 14px rgb(56 189 248 / 25%);
  }

  .warp-btn[data-ready="false"] {
    opacity: 0.4;
    border-color: var(--border);
    box-shadow: none;
  }

  .warp-btn[data-active="true"] {
    background: rgb(56 189 248 / 55%);
    transform: scale(0.95);
  }

  @media (min-width: 768px) and (pointer: coarse) {
    .stick-zone {
      width: 152px;
      height: 152px;
    }

    .fire-btn {
      width: 92px;
      height: 92px;
    }

    .warp-btn {
      width: 72px;
      height: 72px;
    }
  }

  @media (max-width: 420px) {
    .back {
      font-size: 0.72rem;
    }

    .hud-summary {
      font-size: 0.76rem;
      gap: 0.3rem;
    }

    .hud-chip {
      padding: 0.18rem 0.35rem;
    }
  }

  @media (prefers-reduced-motion: reduce) {
    .fire-btn[data-active="true"],
    .warp-btn[data-active="true"] {
      transform: none;
    }
  }
}

@layer utilities {
  .visually-hidden:where(:not(:focus-within, :active)) {
    position: absolute !important;
    clip-path: inset(50%) !important;
    overflow: hidden !important;
    width: 1px !important;
    height: 1px !important;
    margin: -1px !important;
    padding: 0 !important;
    border: 0 !important;
    white-space: nowrap !important;
  }
}
