@media (prefers-color-scheme: dark) {
  body {
    background-color: #000;
    color: #fff;
  }
}

@media (prefers-color-scheme: light) {
  body {
    background-color: #fff;
    color: #000;
  }
}

body.dark-mode {
  background-color: #000;
  color: #fff;
}

body {
  margin: 0;
  font-family: sans-serif;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

body.compact-sidebar-active {
  align-items: flex-start;
}

* {
  box-sizing: border-box;
}

#layout {
  --outer-padding-inline: clamp(0.75rem, 2vmin, 1.5rem);
  --layout-gap: clamp(0.75rem, 2.5vmin, 1.5rem);
  --usable-height: 100vh;
  --usable-height: 100dvh;
  --usable-width: calc(100vw - (var(--outer-padding-inline) * 2));
  --layout-height: min(var(--usable-height),
                       calc((var(--usable-width) - var(--layout-gap)) / 0.75));
  height: var(--layout-height);
  display: flex;
  align-items: stretch;
  gap: var(--layout-gap);
  padding-inline: var(--outer-padding-inline);
}

#preview_container {
  width: 100%;
  aspect-ratio: 1;
}

#preview_text {
  display: none;
  align-items: center;
  justify-content: center;
  min-height: clamp(1.8rem, 6vmin, 2.8rem);
  font-size: clamp(1.8rem, 6vmin, 2.8rem);
  font-weight: 700;
  line-height: 1;
}

#sidebar {
  width: calc(var(--layout-height) * 0.25);
  max-width: calc(var(--layout-height) * 0.25);
  display: flex;
  flex-direction: column;
  gap: clamp(0.75rem, 2vmin, 1rem);
  flex: 0 0 auto;
}

#board {
  width: calc(var(--layout-height) * 0.5);
  height: 100%;
  position: relative;
  flex: 0 0 auto;
}

#game_canvas,
#preview_canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.sidebar_panel {
  border-radius: 18px;
  padding: clamp(0.85rem, 2.2vmin, 1.2rem);
}

#preview {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.sidebar_heading {
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.sidebar_stat {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

#score_value,
#level_value {
  font-size: clamp(1.8rem, 5vmin, 2.6rem);
  font-weight: 700;
  line-height: 1;
}

#layout.compact-sidebar {
  --outer-padding-inline: 0;
  --layout-gap: 0;
  --compact-row-height: clamp(1.15rem, 3.4vmin, 1.45rem);
  flex-direction: column;
  padding-inline: 0;
  gap: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
}

#layout.compact-sidebar #sidebar,
#layout.compact-sidebar #board {
  width: 100%;
}

#layout.compact-sidebar #sidebar {
  max-width: none;
  height: var(--compact-row-height);
  min-height: 0;
  flex: 0 0 var(--compact-row-height);
  flex-direction: row;
  align-items: stretch;
  gap: 0;
}

#layout.compact-sidebar #board {
  flex: 1 1 0;
  min-height: 0;
  height: auto;
}

#layout.compact-sidebar .sidebar_panel {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1 1 0;
  height: 100%;
  min-width: 0;
  padding: 0 clamp(0.04rem, 0.12vmin, 0.1rem);
  gap: clamp(0.03rem, 0.12vmin, 0.08rem);
  border-radius: 0;
  box-shadow: none;
  overflow: hidden;
  white-space: nowrap;
}

#layout.compact-sidebar #preview,
#layout.compact-sidebar .sidebar_stat {
  flex-direction: row;
}

#layout.compact-sidebar #preview {
  gap: clamp(0.03rem, 0.12vmin, 0.08rem);
}

#layout.compact-sidebar #preview_container {
  display: none;
}

#layout.compact-sidebar #preview_text {
  display: block;
  min-height: 0;
}

#layout.compact-sidebar .sidebar_heading {
  font-size: clamp(0.68rem, 1.5vmin, 0.84rem);
  font-weight: 700;
  letter-spacing: 0;
  text-transform: none;
  line-height: 1;
}

#layout.compact-sidebar .sidebar_heading::after {
  content: ":";
}

#layout.compact-sidebar #preview_text,
#layout.compact-sidebar #score_value,
#layout.compact-sidebar #level_value {
  font-size: clamp(0.9rem, 2.05vmin, 1.08rem);
  line-height: 1;
}

/* Overlay */

#overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.6);
  z-index: 10;
}

#overlay[hidden] {
  display: none;
}

#overlay_content {
  padding: 2em 3em;
  border-radius: 12px;
  text-align: center;
  min-width: 250px;
}

@media (prefers-color-scheme: dark) {
  .sidebar_panel {
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.12);
    box-shadow: 0 18px 32px rgba(0, 0, 0, 0.3);
  }

  #overlay_content {
    background: #1a1a1a;
    color: #fff;
    border: 1px solid #333;
  }
}

@media (prefers-color-scheme: light) {
  .sidebar_panel {
    background: rgba(255, 255, 255, 0.9);
    border: 1px solid rgba(0, 0, 0, 0.08);
    box-shadow: 0 18px 32px rgba(0, 0, 0, 0.12);
  }

  #overlay_content {
    background: #fff;
    color: #000;
    border: 1px solid #ccc;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
  }
}

body.dark-mode #overlay_content {
  background: #1a1a1a;
  color: #fff;
  border: 1px solid #333;
}

body.dark-mode .sidebar_panel {
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.12);
  box-shadow: 0 18px 32px rgba(0, 0, 0, 0.3);
}

#overlay_content h1,
#overlay_content h2 {
  margin-top: 0;
}

#overlay_content button {
  display: block;
  width: 100%;
  padding: 0.6em 1.2em;
  margin-top: 0.6em;
  font-size: 1em;
  cursor: pointer;
  border: none;
  border-radius: 6px;
  background: #4a90d9;
  color: #fff;
}

#overlay_content button:hover,
#overlay_content button.spatnav-focus {
  background: #357abd;
}

#overlay_content .spatnav-focus {
  outline: 3px solid #4a90d9;
  outline-offset: 2px;
}

#overlay_content input[type="text"] {
  display: block;
  width: 100%;
  padding: 0.4em;
  margin-top: 0.4em;
  font-size: 1em;
  border-radius: 4px;
  border: 1px solid #555;
  box-sizing: border-box;
}

#overlay_content label {
  display: block;
  text-align: left;
  margin-top: 0.5em;
}
