:root {
  color-scheme: dark;
  --app-width: 100dvw;
  --app-height: 100dvh;
  --bg: #171717;
  --panel: #242424;
  --line: #3f3f3f;
  --text: #f3f0e8;
  --muted: #b9b3a6;
  --accent: #e7c45a;
  --accent-2: #6fb7c9;
  --danger: #c7584c;
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
  width: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Arial, Helvetica, sans-serif;
  overflow-x: hidden;
}

button {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #303030;
  color: var(--text);
  min-height: 42px;
  padding: 0 14px;
  font: inherit;
  cursor: pointer;
}

button:hover,
button:focus-visible,
a:hover,
a:focus-visible {
  border-color: var(--accent);
  outline: none;
}

button:active,
a:active {
  transform: translateY(1px);
}

a {
  color: var(--text);
}

.splash {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 18px;
  padding: 18px;
  background: #101010;
}

.splash.hidden {
  display: none;
}

.splash img {
  width: min(88vw, 420px);
  max-height: min(70vh, 640px);
  object-fit: contain;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.splash-actions {
  width: min(88vw, 420px);
  display: grid;
  gap: 10px;
}

.channel-link,
.channel-button {
  min-height: 42px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #303030;
  color: var(--text);
  padding: 0 14px;
  text-align: center;
  text-decoration: none;
}

.channel-link {
  background: var(--danger);
  border-color: var(--danger);
  font-weight: 700;
}

.app {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  grid-template-columns: minmax(320px, 1fr) 300px;
  gap: 20px;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

.play-area {
  display: grid;
  align-content: center;
  justify-items: center;
  gap: 14px;
  min-width: 0;
}

.top-banner {
  width: min(100%, 720px);
  display: block;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: #080808;
  line-height: 0;
}

.top-banner img {
  display: block;
  width: 100%;
  max-height: 140px;
  object-fit: contain;
}

.topbar {
  width: min(100%, 720px);
  display: block;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: clamp(28px, 5vw, 46px);
  line-height: 1;
}

h2 {
  font-size: 20px;
}

#status,
.stats {
  color: var(--muted);
}

.level-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.stats {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px 14px;
  text-align: right;
}

.stats strong {
  color: var(--accent);
}

.board {
  --tile: 30px;
  display: grid;
  grid-template-columns: repeat(16, var(--tile));
  grid-template-rows: repeat(16, var(--tile));
  background: #070707;
  border: 1px solid var(--line);
  width: calc(var(--tile) * 16 + 2px);
  height: calc(var(--tile) * 16 + 2px);
  touch-action: none;
  overflow: visible;
}

.tile {
  width: var(--tile);
  height: var(--tile);
  border: 0;
  border-radius: 0;
  padding: 0;
  appearance: none;
  display: block;
  background: transparent;
  overflow: visible;
  line-height: 0;
}

.tile.reachable {
  cursor: pointer;
}

.tile img {
  display: block;
  width: var(--tile);
  height: var(--tile);
  image-rendering: pixelated;
  object-fit: fill;
  pointer-events: none;
}

.controls {
  width: min(100%, 720px);
  display: grid;
  grid-template-columns: repeat(3, minmax(96px, 1fr)) auto;
  align-items: center;
  gap: 10px;
}

.control-spacer {
  min-height: 42px;
}

.dpad {
  display: grid;
  grid-template-columns: repeat(3, 48px);
  grid-template-rows: repeat(2, 44px);
  gap: 10px;
}

.dpad button {
  min-height: 44px;
  padding: 0;
  position: relative;
}

.dpad button span {
  display: block;
  width: 0;
  height: 0;
  margin: auto;
}

.dpad [data-dir="up"] {
  grid-column: 2;
}

.dpad [data-dir="up"] span {
  border-left: 12px solid transparent;
  border-right: 12px solid transparent;
  border-bottom: 18px solid var(--text);
}

.dpad [data-dir="left"] {
  grid-column: 1;
  grid-row: 2;
}

.dpad [data-dir="left"] span {
  border-top: 12px solid transparent;
  border-bottom: 12px solid transparent;
  border-right: 18px solid var(--text);
}

.dpad [data-dir="down"] {
  grid-column: 2;
  grid-row: 2;
}

.dpad [data-dir="down"] span {
  border-left: 12px solid transparent;
  border-right: 12px solid transparent;
  border-top: 18px solid var(--text);
}

.dpad [data-dir="right"] {
  grid-column: 3;
  grid-row: 2;
}

.dpad [data-dir="right"] span {
  border-top: 12px solid transparent;
  border-bottom: 12px solid transparent;
  border-left: 18px solid var(--text);
}

.levels {
  min-height: 0;
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 12px;
  padding-left: 18px;
  border-left: 1px solid var(--line);
}

.levels-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.level-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.level-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(42px, 1fr));
  align-content: start;
  gap: 8px;
  overflow: auto;
  padding-right: 4px;
}

.level-list button {
  min-width: 0;
  min-height: 38px;
  padding: 0;
}

.level-list .active {
  background: var(--accent);
  border-color: var(--accent);
  color: #181818;
  font-weight: 700;
}

.level-list .solved {
  border-color: var(--accent-2);
}

dialog {
  width: min(92vw, 390px);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.62);
}

dialog form {
  justify-items: center;
  display: grid;
  gap: 12px;
}

dialog p {
  color: var(--muted);
  text-align: center;
}

.complete-animation {
  display: block;
  width: min(72vw, 230px);
  max-height: min(45vh, 300px);
  object-fit: contain;
}

menu {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 0;
  padding: 0;
}

@media (max-width: 860px) {
  .app {
    grid-template-columns: 1fr;
    align-content: start;
  }

  .play-area {
    align-content: start;
  }

  .levels {
    border-left: 0;
    border-top: 1px solid var(--line);
    padding-left: 0;
    padding-top: 14px;
  }

  .level-list {
    max-height: 180px;
  }
}

@media (max-width: 520px) {
  .app {
    gap: 14px;
  }

  .topbar {
    width: 100%;
  }

  .level-meta {
    align-items: flex-start;
    gap: 8px;
  }

  .stats {
    justify-content: flex-end;
    gap: 4px 8px;
    text-align: right;
    font-size: 14px;
  }

  .board {
    --tile: max(18px, min(30px, calc((100vw - 34px) / 16)));
  }

  .controls {
    grid-template-columns: 1fr 1fr 1fr;
    gap: 8px;
  }

  .dpad {
    grid-column: 1 / -1;
    justify-self: center;
    gap: 12px;
  }
}

@media (orientation: landscape) and (max-height: 560px) {
  html,
  body {
    width: var(--app-width);
    height: var(--app-height);
    overflow: hidden;
    overscroll-behavior: none;
  }

  .splash {
    padding: 10px;
  }

  .app {
    width: var(--app-width);
    min-height: var(--app-height);
    height: var(--app-height);
    grid-template-columns: 1fr;
    gap: 0;
    overflow: hidden;
    padding: max(10px, env(safe-area-inset-top)) max(24px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  }

  .play-area {
    --side-width: clamp(190px, 30vw, 260px);
    width: 100%;
    height: 100%;
    max-width: 100%;
    max-height: 100%;
    overflow: hidden;
    display: grid;
    grid-template-columns: minmax(0, 1fr) var(--side-width);
    grid-template-rows: auto auto auto 1fr;
    grid-template-areas:
      "board banner"
      "board meta"
      "board actions"
      "board joystick";
    align-items: center;
    justify-items: center;
    gap: 4px 12px;
  }

  .top-banner {
    grid-area: banner;
    align-self: start;
    width: calc(100% - 34px);
    max-width: 190px;
    margin-top: 10px;
  }

  .top-banner img {
    max-height: 34px;
  }

  .topbar {
    grid-area: meta;
    align-self: start;
    width: calc(100% - 34px);
    max-width: 190px;
  }

  h1 {
    display: none;
  }

  .level-meta {
    align-items: center;
    gap: 6px;
    font-size: 11px;
    line-height: 1.15;
  }

  .stats {
    display: grid;
    gap: 1px;
    justify-items: end;
    font-size: 11px;
    line-height: 1.15;
  }

  .board {
    grid-area: board;
    align-self: center;
    justify-self: center;
    max-width: calc(100% - 4px);
    max-height: calc(100% - 4px);
    --tile: min(calc((var(--app-width) - var(--side-width) - 58px) / 16), calc((var(--app-height) - 26px) / 16), 38px);
  }

  .controls {
    grid-column: 2;
    grid-row: 3 / 5;
    align-self: start;
    width: calc(100% - 34px);
    max-width: 190px;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    justify-items: stretch;
  }

  .control-spacer {
    display: none;
  }

  .controls #undo,
  .controls #restart {
    min-height: 32px;
    padding: 0 6px;
    font-size: 12px;
  }

  /* Dpad estilo controle de game com espaço vazio no meio */
  .dpad {
    grid-column: 1 / -1;
    justify-self: center;
    margin-top: clamp(60px, 12vh, 100px);
    grid-template-columns: repeat(3, clamp(44px, 7vw, 56px));
    grid-template-rows: repeat(3, clamp(42px, 7vh, 54px));
    gap: clamp(20px, 3vh, 32px);
  }

  .dpad button {
    min-height: clamp(42px, 7vh, 54px);
  }

  .dpad [data-dir="up"] {
    grid-column: 2;
    grid-row: 1;
  }

  .dpad [data-dir="left"] {
    grid-column: 1;
    grid-row: 2;
  }

  .dpad [data-dir="down"] {
    grid-column: 2;
    grid-row: 3;
  }

  .dpad [data-dir="right"] {
    grid-column: 3;
    grid-row: 2;
  }

  .levels {
    display: none;
  }
}