:root {
  --bg-a: #f5f0e7;
  --bg-b: #d6e3f3;
  --ink: #2d2a24;
  --panel: #fff9f0;
  --panel-border: #d8cdbb;
  --layout-gap-edge: 16px;
  /* Keep intrinsic map size so .game-wrap can scroll when viewport is smaller. */
  --canvas-width: 768px;
  --canvas-height: 512px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Trebuchet MS", "Segoe UI", sans-serif;
  color: var(--ink);
  background: #f2efe7;
}

body.play-mode {
  overflow: hidden;
}

.layout {
  width: min(1100px, 100% - 24px);
  margin: var(--layout-gap-edge) auto;
  display: grid;
  grid-template-columns: 300px 1fr;
  gap: 16px;
  align-items: start;
}

.panel {
  padding: 16px;
  border: 2px solid var(--panel-border);
  border-radius: 14px;
  background: var(--panel);
  box-shadow: 0 8px 22px rgba(50, 45, 38, 0.1);
}

h1 {
  margin: 0 0 12px;
  font-size: 1.6rem;
}

p {
  margin: 0 0 10px;
  line-height: 1.35;
}


.hidden {
  display: none;
}

.mode-controls {
  margin: 12px 0 4px;
  display: flex;
  gap: 8px;
}

.tick-controls {
  margin: 8px 0 12px;
  display: grid;
  gap: 6px;
}

.tick-controls label {
  font-size: 0.9rem;
  font-weight: 700;
}

.tick-controls select {
  border: 1px solid #7f8ca1;
  border-radius: 8px;
  padding: 6px 8px;
  font: inherit;
  background: #fff;
  color: #23344a;
}

.play-btn,
.edit-btn {
  flex: 1;
  border: 0;
  border-radius: 999px;
  padding: 10px 18px;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

.play-btn {
  background: #2b7a3e;
  color: #fff;
}

.play-btn:hover {
  background: #1f5c2e;
}

.edit-btn {
  background: #7a4a2b;
  color: #fff;
}

.edit-btn:hover {
  background: #5c341f;
}

.github-auth-btn {
  width: 100%;
  margin: 0 0 8px;
  border: 1px solid #1d4f8a;
  border-radius: 999px;
  padding: 10px 18px;
  font: inherit;
  font-weight: 700;
  color: #fff;
  background: #2467b2;
  cursor: pointer;
}

.github-auth-btn:hover {
  background: #1b518d;
}

.github-auth-btn:disabled {
  cursor: default;
  opacity: 0.75;
}

.github-auth-status {
  margin: 0 0 8px;
  font-size: 0.9rem;
  color: #274d76;
}

body.play-mode .github-auth-btn {
  display: none;
}

body.play-mode .github-auth-status {
  display: none;
}

.tools {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  margin: 12px 0;
}

.tool {
  border: 2px solid #7f8ca1;
  border-radius: 6px;
  padding: 4px;
  background: #e9f1fb;
  cursor: pointer;
  aspect-ratio: 1;
  width: 100%;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tool.is-active {
  background: #284f78;
  border-color: #1f4062;
  box-shadow: 0 0 0 2px #1f4062;
}

.tool-preview {
  width: 100%;
  aspect-ratio: 1;
  height: auto;
  image-rendering: pixelated;
  border-radius: 3px;
  display: block;
}


.game-wrap {
  position: relative;
  border: 2px solid #264462;
  border-radius: 14px;
  overflow: auto;
  width: min(100%, calc(var(--canvas-width) + 4px));
  height: min(var(--canvas-height), calc(100vh - (var(--layout-gap-edge) * 2)));
  min-width: 320px;
  min-height: 240px;
  max-width: 100%;
  max-height: calc(100vh - (var(--layout-gap-edge) * 2));
  box-shadow: 0 10px 28px rgba(20, 45, 68, 0.18);
  background: #dbe8f7;
}

.edge-controls {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 5;
  pointer-events: none;
}

.edge-controls-row {
  position: absolute;
  display: flex;
  gap: 4px;
  pointer-events: auto;
}

.edge-controls-row button,
.exit-play-btn {
  border: 1px solid #1f4062;
  border-radius: 999px;
  padding: 4px 8px;
  font: inherit;
  font-size: 0.8rem;
  font-weight: 700;
  color: #fff;
  background: rgba(31, 64, 98, 0.88);
  cursor: pointer;
}

.edge-controls-row button:hover,
.exit-play-btn:hover {
  background: rgba(20, 45, 68, 0.95);
}

.edge-controls-top {
  top: 6px;
  left: 50%;
  transform: translateX(-50%);
}

.edge-controls-bottom {
  bottom: 6px;
  left: 50%;
  transform: translateX(-50%);
}

.edge-controls-left {
  top: 50%;
  left: 6px;
  transform: translateY(-50%);
  flex-direction: column;
}

.edge-controls-right {
  top: 50%;
  right: 6px;
  transform: translateY(-50%);
  flex-direction: column;
}

.exit-play-btn {
  position: fixed;
  top: 12px;
  right: 12px;
  z-index: 15;
}

body.play-mode .panel {
  display: none;
}

body.play-mode .layout {
  width: min(100%, calc(var(--canvas-width) + 32px));
  grid-template-columns: 1fr;
}

body.play-mode .game-wrap {
  overflow: hidden;
}

body.play-mode .edge-controls {
  display: none;
}

.map-resize {
  position: relative;
  width: var(--canvas-width);
  height: var(--canvas-height);
  min-width: 320px;
  min-height: 240px;
  resize: both;
  overflow: hidden;
}

.overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(9, 24, 41, 0.72);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
  z-index: 20;
}

.overlay.hidden {
  display: none;
}

.overlay-card {
  width: min(420px, calc(100% - 24px));
  border-radius: 16px;
  border: 2px solid #f0d56c;
  background: linear-gradient(150deg, #fff9d5, #ffe28f);
  color: #2a2308;
  text-align: center;
  padding: 22px 18px;
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.32);
}

.overlay-card h2 {
  margin: 0 0 8px;
  font-size: 2rem;
}

.overlay-card p {
  margin: 0 0 16px;
}

.overlay-card button {
  border: 0;
  border-radius: 999px;
  padding: 10px 18px;
  font: inherit;
  font-weight: 700;
  color: #fff;
  background: #2b4f7a;
  cursor: pointer;
}

.overlay-card button:hover {
  background: #1f3b5d;
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
}

@media (max-width: 900px) {
  .layout {
    grid-template-columns: 1fr;
  }
}

.pixel-editor-overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(9, 24, 41, 0.8);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
  z-index: 30;
}

.pixel-editor-overlay.hidden {
  display: none;
}

.pixel-editor-card {
  background: #1a2c40;
  border: 2px solid #3a6088;
  border-radius: 12px;
  padding: 16px;
  color: #e8f0f8;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.6);
  max-width: calc(100vw - 32px);
  max-height: calc(100vh - 32px);
  overflow: auto;
}

.pixel-editor-title {
  margin: 0 0 12px;
  font-size: 1.2rem;
  color: #c0d8f0;
}

.pixel-editor-body {
  display: flex;
  gap: 12px;
  align-items: flex-start;
}

.pixel-editor-canvas {
  image-rendering: pixelated;
  cursor: crosshair;
  border: 1px solid #3a6088;
  touch-action: none;
}

.pixel-editor-sidebar {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 130px;
}

.pixel-editor-palette {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 3px;
}

.palette-color {
  width: 22px;
  height: 22px;
  border-radius: 3px;
  border: 2px solid rgba(255, 255, 255, 0.15);
  cursor: pointer;
  box-sizing: border-box;
}

.palette-color:hover {
  border-color: rgba(255, 255, 255, 0.6);
}

.palette-color.is-selected {
  border-color: #fff;
  box-shadow: 0 0 0 1px #fff;
}

.palette-color.transparent-swatch {
  background-image: linear-gradient(45deg, #ccc 25%, transparent 25%),
    linear-gradient(-45deg, #ccc 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #ccc 75%),
    linear-gradient(-45deg, transparent 75%, #ccc 75%);
  background-size: 8px 8px;
  background-position: 0 0, 0 4px, 4px -4px, -4px 0;
  background-color: #fff;
}

.pixel-editor-current-color-label {
  font-size: 0.75rem;
  color: #8ab0d0;
  margin-top: 2px;
}

.pixel-editor-current-color {
  width: 40px;
  height: 40px;
  border-radius: 6px;
  border: 2px solid #3a6088;
}

.pixel-editor-custom-label {
  font-size: 0.75rem;
  color: #8ab0d0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.pixel-editor-custom-label input[type="color"] {
  width: 100%;
  height: 28px;
  border: 1px solid #3a6088;
  border-radius: 4px;
  cursor: pointer;
  background: none;
  padding: 1px;
}

.pixel-editor-actions {
  margin-top: 12px;
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.pixel-editor-actions button {
  border: 0;
  border-radius: 999px;
  padding: 8px 16px;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

.pixel-editor-save-btn {
  background: #2b7a3e;
  color: #fff;
}

.pixel-editor-save-btn:hover {
  background: #1f5c2e;
}

.pixel-editor-cancel-btn {
  background: #5a3a3a;
  color: #fff;
}

.pixel-editor-cancel-btn:hover {
  background: #4a2a2a;
}

.pixel-editor-reset-btn {
  background: #2e4a6e;
  color: #fff;
  margin-right: auto;
}

.pixel-editor-reset-btn:hover {
  background: #1e3a5e;
}

.pixel-editor-toolbox {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 36px;
  align-items: center;
}

.px-tool {
  width: 32px;
  height: 32px;
  border: 2px solid #3a6088;
  border-radius: 6px;
  background: #243850;
  color: #c0d8f0;
  cursor: pointer;
  font-size: 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  line-height: 1;
}

.px-tool:hover {
  background: #2e4f70;
  border-color: #5090c0;
}

.px-tool.is-active {
  background: #1a6aaa;
  border-color: #5eb8ff;
  box-shadow: 0 0 0 1px #5eb8ff;
}

.px-tool-separator {
  width: 24px;
  height: 1px;
  background: #3a6088;
  margin: 4px 0;
}

.px-pen-size-label {
  font-size: 0.65rem;
  color: #8ab0d0;
  text-align: center;
  margin-top: 2px;
}

.px-pen-size {
  width: 28px;
  height: 24px;
  border: 2px solid #3a6088;
  border-radius: 4px;
  background: #243850;
  color: #c0d8f0;
  cursor: pointer;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 0;
  line-height: 1;
}

.px-pen-size:hover {
  background: #2e4f70;
  border-color: #5090c0;
}

.px-pen-size.is-active {
  background: #1a6aaa;
  border-color: #5eb8ff;
}

/* Touch controls overlay (shown in play mode on touch devices) */
.touch-controls {
  position: fixed;
  bottom: 24px;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  padding: 0 24px;
  z-index: 14;
  pointer-events: none;
}

.touch-dpad {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  pointer-events: auto;
}

.touch-dpad-row {
  display: flex;
  gap: 4px;
  align-items: center;
}

.touch-dpad-center {
  width: 56px;
  height: 56px;
}

.touch-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: auto;
}

.touch-btn {
  width: 56px;
  height: 56px;
  border-radius: 12px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  background: rgba(31, 64, 98, 0.85);
  color: #fff;
  font-size: 1.4rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
  padding: 0;
}

.touch-btn:active {
  background: rgba(20, 45, 68, 0.95);
  border-color: rgba(255, 255, 255, 0.6);
}
