:root {
  color-scheme: dark;
  --bg: #1b0303;
  --panel: #050716;
  --line: #ffffff;
  --text: #ffffff;
  --muted: #ffd7d7;
  --accent: #ff66c8;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  place-items: center;
  background:
    linear-gradient(180deg, rgba(255, 0, 0, 0.16), rgba(255, 0, 0, 0.68)),
    var(--bg);
  color: var(--text);
  font-family: "Segoe UI", "Meiryo", system-ui, sans-serif;
  touch-action: manipulation;
}

.app {
  width: min(100vw - 20px, 780px);
  display: grid;
  grid-template-columns: 1fr;
  grid-template-areas:
    "play"
    "controls";
  gap: 10px;
  align-items: center;
  justify-items: center;
}

.play-area {
  grid-area: play;
  position: relative;
  width: 100%;
  max-width: 760px;
  max-height: min(760px, calc(100vh - 22px));
  aspect-ratio: 1;
  border: 4px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: #210000;
  box-shadow: 0 22px 60px rgba(0, 0, 0, 0.35);
}

#game {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.overlay {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: none;
  place-items: center;
  background: rgba(8, 12, 10, 0.68);
}

.overlay.is-visible {
  display: grid;
}

.overlay-panel {
  width: min(84%, 320px);
  padding: 24px;
  border: 4px solid #ffffff;
  border-radius: 8px;
  background: rgba(5, 7, 22, 0.94);
  text-align: center;
}

h1 {
  margin: 0 0 8px;
  font-size: clamp(28px, 6vw, 40px);
  line-height: 1.08;
  letter-spacing: 0;
  overflow-wrap: anywhere;
}

#overlayMessage {
  margin: 8px 0 18px;
  color: var(--muted);
  font-size: clamp(15px, 3vw, 20px);
  line-height: 1.45;
  white-space: pre-line;
}

#overlayMessage:empty {
  display: none;
}

button {
  width: 150px;
  min-height: 42px;
  border: 0;
  border-radius: 6px;
  background: var(--accent);
  color: #180014;
  font: 700 16px/1 "Segoe UI", system-ui, sans-serif;
  cursor: pointer;
}

.side-panel {
  position: absolute;
  inset: 10px;
  z-index: 2;
  pointer-events: none;
}

.touch-controls {
  grid-area: controls;
  width: min(100%, 760px);
  display: none;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 8px;
}

.control-button {
  width: 100%;
  min-height: 48px;
  border: 3px solid rgba(255, 255, 255, 0.85);
  background: #ffd34d;
  color: #190808;
  font-size: 22px;
  touch-action: manipulation;
  user-select: none;
}

.stat,
.next-box {
  border: 2px solid rgba(255, 255, 255, 0.72);
  border-radius: 7px;
  background: rgba(5, 7, 22, 0.76);
  backdrop-filter: blur(3px);
}

.stats-stack {
  position: absolute;
  top: 0;
  right: 0;
  width: min(34%, 230px);
  display: grid;
  gap: 6px;
}

.stat {
  padding: 10px 12px;
}

.stat:first-child {
  margin-bottom: 14px;
}

.credit {
  position: absolute;
  right: 0;
  bottom: 0;
  color: rgba(255, 255, 255, 0.72);
  font-size: 12px;
  text-align: right;
}

.stat {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
}

.stat span,
.next-box span {
  color: var(--muted);
  font-size: clamp(15px, 2.4vw, 18px);
  font-weight: 900;
  letter-spacing: 0;
  text-transform: uppercase;
  text-shadow: 0 2px 0 #000;
}

.stat strong {
  font-size: clamp(21px, 3.6vw, 32px);
  font-weight: 900;
  line-height: 1;
  text-shadow: 0 2px 0 #000;
}

.next-box {
  position: absolute;
  left: 0;
  bottom: 0;
  width: min(29%, 180px);
  padding: 10px;
}

#next {
  width: 100%;
  height: auto;
  display: block;
  margin-top: 8px;
  background: #050716;
  border-radius: 6px;
}

@media (pointer: coarse), (max-width: 760px) {
  .touch-controls {
    display: grid;
  }
}

@media (max-width: 760px) and (orientation: portrait) {
  body {
    align-items: start;
    padding: 6px 0;
  }

  .app {
    width: min(100vw - 10px, 560px);
    grid-template-columns: 1fr;
    grid-template-areas:
      "play"
      "controls";
    gap: 7px;
  }

  .play-area {
    width: min(100%, calc(100vh - 88px));
    border-width: 3px;
  }

  .stats-stack {
    width: 38%;
    gap: 4px;
  }

  .next-box {
    width: 25%;
  }

  .stat,
  .next-box {
    padding: 5px;
  }

  .touch-controls {
    width: min(100%, 560px);
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 6px;
  }

  .control-button {
    min-height: clamp(46px, 8vh, 64px);
    font-size: 24px;
  }

  .credit {
    font-size: 11px;
  }
}

@media (pointer: coarse) and (orientation: landscape), (max-height: 560px) and (orientation: landscape) {
  body {
    padding: 6px;
  }

  .app {
    width: min(100vw - 12px, 980px);
    height: calc(100vh - 12px);
    grid-template-columns: minmax(54px, 76px) minmax(0, 1fr) minmax(54px, 76px);
    grid-template-areas: "left play right";
    gap: 7px;
  }

  .play-area {
    width: min(100%, calc(100vh - 12px));
    max-height: calc(100vh - 12px);
    border-width: 3px;
    justify-self: center;
  }

  .touch-controls {
    display: contents;
  }

  .control-button {
    min-height: 0;
    width: 100%;
    height: 100%;
    min-width: 0;
    font-size: clamp(19px, 4.5vw, 28px);
  }

  .control-button[data-action="left"] {
    grid-area: left;
    align-self: start;
    height: calc((100vh - 26px) / 2);
  }

  .control-button[data-action="rotate-left"] {
    grid-area: left;
    align-self: end;
    height: calc((100vh - 26px) / 2);
  }

  .control-button[data-action="right"] {
    grid-area: right;
    align-self: start;
    height: calc((100vh - 34px) / 3);
  }

  .control-button[data-action="rotate-right"] {
    grid-area: right;
    align-self: center;
    height: calc((100vh - 34px) / 3);
  }

  .control-button[data-action="drop"] {
    grid-area: right;
    align-self: end;
    height: calc((100vh - 34px) / 3);
  }

  .stats-stack {
    width: 35%;
    gap: 4px;
  }

  .stat,
  .next-box {
    padding: 5px;
  }

  .next-box {
    width: 24%;
  }
}
