:root {
  --page: 8px;
  --frame: 12px;
  --gap: 10px;
  --panel: clamp(180px, 34vw, 360px);
  --board-h: min(
    calc(100dvh - (var(--page) + var(--frame)) * 2),
    calc((100vw - var(--panel) - var(--gap) - (var(--page) + var(--frame)) * 2) * 2)
  );
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  height: 100dvh;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: var(--page);
}

.container {
  display: flex;
  justify-content: center;
  align-items: center;
  max-width: 100%;
  max-height: 100%;
}

.game-area {
  display: flex;
  align-items: flex-start;
  gap: var(--gap);
  max-width: 100%;
  max-height: 100%;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  padding: var(--frame);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

.board {
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  grid-template-rows: repeat(20, 1fr);
  gap: 2px;
  height: var(--board-h);
  width: auto;
  aspect-ratio: 1 / 2;
  flex: 0 0 auto;
  background: #121520;
  border: 3px solid #fff;
  border-radius: 10px;
  padding: 4px;
}

.cell {
  background: #121520;
  border-radius: 3px;
  transition: all 0.1s;
}

.cell.filled {
  border: 1px solid rgba(255, 255, 255, 0.3);
  box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.5);
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: var(--panel);
  max-height: var(--board-h);
  min-height: 0;
  align-self: start;
}

.stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.info-section {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  padding: 8px 6px;
  text-align: center;
  min-width: 0;
}

.info-section h2 {
  color: #fff;
  font-size: 13px;
  margin-bottom: 4px;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}

.info-value {
  color: #fff;
  font-size: 22px;
  font-weight: bold;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
  line-height: 1.1;
}

.next-board {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: repeat(6, 1fr);
  gap: 1px;
  width: min(64px, 100%);
  aspect-ratio: 1;
  height: auto;
  background: #121520;
  border: 2px solid #fff;
  border-radius: 8px;
  padding: 3px;
  margin: 0 auto;
}

.mini-cell {
  background: #121520;
  border-radius: 2px;
  transition: all 0.1s;
}

.mini-cell.filled {
  border: 1px solid rgba(255, 255, 255, 0.3);
  box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.5);
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  min-width: 0;
}

.controls button,
.pad button {
  padding: 8px 6px;
  font-size: 13px;
  white-space: nowrap;
  font-weight: bold;
  color: #fff;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
  touch-action: none;
}

.controls button {
  flex: 1 1 6.4em;
}

.pad {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.pad button {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  min-height: 36px;
  line-height: 1;
  font-size: 16px;
}

.pad button span {
  font-size: 10px;
  font-weight: 600;
  opacity: 0.9;
}

.controls button:hover,
.pad button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
}

.controls button:active,
.pad button:active {
  transform: translateY(0);
}

.instructions {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  padding: 8px 10px;
  min-width: 0;
}

.instructions h3 {
  color: #fff;
  font-size: 13px;
  margin-bottom: 6px;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}

.instructions p {
  color: #fff;
  display: grid;
  grid-template-columns: 4.8em minmax(0, 1fr);
  gap: 6px;
  font-size: 12px;
  margin: 3px 0;
  white-space: nowrap;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
}

.instructions span {
  font-weight: bold;
  white-space: nowrap;
}

@media (max-width: 520px) and (min-height: 640px) {
  :root {
    --panel: min(100%, 520px);
    --board-h: min(
      calc((100vw - (var(--page) + var(--frame)) * 2) * 2),
      calc(100dvh - 480px)
    );
  }

  .container {
    width: 100%;
  }

  .game-area {
    flex-direction: column;
    align-items: stretch;
    width: min(100%, 520px);
  }

  .board {
    align-self: center;
    max-width: 100%;
  }

  .side-panel {
    width: 100%;
    max-height: none;
  }
}

@media (max-height: 520px) {
  .info-section {
    padding: 4px;
  }

  .info-section h2,
  .instructions h3 {
    font-size: 11px;
    margin-bottom: 2px;
  }

  .info-value {
    font-size: 16px;
  }

  .next-board {
    width: min(44px, 100%);
  }

  .controls {
    gap: 4px;
  }

  .controls button,
  .pad button {
    padding: 4px 2px;
    font-size: 11px;
  }

  .pad {
    gap: 4px;
  }

  .pad button {
    min-height: 28px;
    font-size: 14px;
  }

  .instructions {
    padding: 4px 8px;
  }

  .instructions p {
    font-size: 10px;
    margin: 1px 0;
  }
}
