html,
body {
  margin: 0;
  min-height: 100%;
  min-height: 100dvh;
  background: linear-gradient(180deg, #ffe7ef 0%, #fff7cf 100%);
  font-family: "Trebuchet MS", "Verdana", sans-serif;
  overscroll-behavior: none;
}

body {
  display: grid;
  place-items: center;
  width: 100%;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  box-sizing: border-box;
}

.dev-menu-toggle {
  position: fixed;
  bottom: 14px;
  left: 14px;
  top: auto;
  z-index: 20;
  border: 0;
  border-radius: 999px;
  padding: 12px 18px;
  font: 700 18px "Trebuchet MS", "Verdana", sans-serif;
  color: #fff;
  background: linear-gradient(180deg, #5e315b 0%, #8c487b 100%);
  box-shadow: 0 12px 28px rgba(94, 49, 91, 0.28);
  cursor: pointer;
}

.dev-menu {
  position: fixed;
  bottom: 62px;
  left: 14px;
  top: auto;
  z-index: 20;
  width: min(300px, calc(100vw - 28px));
  max-height: min(70vh, calc(100vh - 90px));
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 16px;
  border: 4px solid #5e315b;
  border-radius: 20px;
  background: rgba(255, 248, 252, 0.98);
  box-shadow: 0 22px 46px rgba(94, 49, 91, 0.24);
  box-sizing: border-box;
  display: grid;
  gap: 10px;
  scrollbar-width: thin;
  scrollbar-color: #8c487b #ffe7ef;
}

.dev-menu::-webkit-scrollbar {
  width: 10px;
}
.dev-menu::-webkit-scrollbar-thumb {
  background: #8c487b;
  border-radius: 999px;
}
.dev-menu::-webkit-scrollbar-track {
  background: #ffe7ef;
  border-radius: 999px;
}

.dev-menu[hidden] {
  display: none;
}

.dev-menu h2 {
  margin: 0 0 4px;
  color: #5e315b;
  font: 700 24px "Trebuchet MS", "Verdana", sans-serif;
}

.dev-subtitle {
  margin: 6px 0 0;
  color: #8c487b;
  font: 700 16px "Trebuchet MS", "Verdana", sans-serif;
}

.dev-field,
.dev-check {
  display: grid;
  gap: 6px;
  color: #5e315b;
  font: 700 15px "Trebuchet MS", "Verdana", sans-serif;
}

.dev-field input,
.dev-field select {
  width: 100%;
  box-sizing: border-box;
  border: 2px solid #caa4c8;
  border-radius: 12px;
  padding: 10px 12px;
  font: 700 15px "Trebuchet MS", "Verdana", sans-serif;
  color: #5e315b;
  background: #fff;
}

.dev-field input[type="color"] {
  min-height: 46px;
  padding: 6px;
}

.dev-check {
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 10px;
}

.dev-btn {
  border: 0;
  border-radius: 14px;
  padding: 10px 14px;
  font: 700 16px "Trebuchet MS", "Verdana", sans-serif;
  color: #fff;
  background: linear-gradient(180deg, #ff6d92 0%, #d94872 100%);
  box-shadow: 0 10px 22px rgba(217, 72, 114, 0.22);
  cursor: pointer;
}

.app-shell {
  width: 100%;
  max-width: 100vw;
  display: grid;
  place-items: center;
  gap: 8px;
  padding: 8px;
  box-sizing: border-box;
}

canvas {
  /* Fit phones/tablets: never taller than the viewport, keep 5:3 */
  width: min(97vw, 1200px, calc((100dvh - 72px) * 5 / 3));
  max-width: 100%;
  height: auto;
  max-height: calc(100dvh - 72px);
  aspect-ratio: 5 / 3;
  border: 6px solid #5e315b;
  border-radius: 18px;
  box-shadow: 0 24px 60px rgba(94, 49, 91, 0.22);
  background: #f6fbff;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  display: block;
}

.controls-row {
  width: min(97vw, 1200px);
  display: flex;
  justify-content: center;
}

.action-btn {
  border: 0;
  border-radius: 999px;
  padding: 12px 26px;
  font: 700 22px "Trebuchet MS", "Verdana", sans-serif;
  color: #fff;
  background: linear-gradient(180deg, #59c7ff 0%, #2d89d1 100%);
  box-shadow: 0 12px 26px rgba(45, 137, 209, 0.28);
  cursor: pointer;
}

.action-btn[hidden] {
  display: none;
}

.pause-btn {
  background: linear-gradient(180deg, #ff8ab0 0%, #e25584 100%);
  box-shadow: 0 12px 26px rgba(226, 85, 132, 0.28);
}

.pause-btn.is-paused {
  background: linear-gradient(180deg, #6ad39a 0%, #2f9f66 100%);
  box-shadow: 0 12px 26px rgba(47, 159, 102, 0.28);
}

.pause-btn-fixed {
  position: fixed;
  top: max(10px, env(safe-area-inset-top));
  right: max(10px, env(safe-area-inset-right));
  z-index: 25;
  min-width: 120px;
  font-size: 20px !important;
  padding: 12px 22px !important;
  touch-action: manipulation;
}


.dev-online-panel {
  display: grid;
  gap: 8px;
  padding: 10px;
  border: 2px dashed #caa4c8;
  border-radius: 14px;
  background: #fff7fb;
}

.dev-online-panel[hidden] {
  display: none;
}

.dev-hint {
  margin: 0;
  color: #8c487b;
  font: 600 13px "Trebuchet MS", "Verdana", sans-serif;
  line-height: 1.35;
}

.dev-code-big {
  font-size: 2rem;
  font-weight: 800;
  letter-spacing: 0.35em;
  text-align: center;
  color: #5e315b;
  background: #fff4a8;
  border: 3px solid #5e315b;
  border-radius: 12px;
  padding: 0.4rem 0.6rem;
  margin: 0.4rem 0;
}

.dev-code-pad {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 4px;
  margin: 0.4rem 0;
  max-height: 140px;
  overflow: auto;
}
.dev-code-pad button {
  font-weight: 700;
  padding: 0.35rem 0;
  border: 2px solid #5e315b;
  border-radius: 8px;
  background: #fff;
  color: #5e315b;
  cursor: pointer;
}
.dev-code-pad button:hover { background: #fff4a8; }
.dev-row { display: flex; gap: 6px; margin: 0.25rem 0; }
.dev-row .dev-btn { flex: 1; }
#dev-online-code:focus {
  outline: 3px solid #5d9bff;
  background: #fffceb;
}


/* Phones & small tablets */
@media (max-width: 900px) {
  .app-shell {
    padding: 4px;
    gap: 4px;
  }

  canvas {
    width: min(100vw - 8px, calc((100dvh - 64px) * 5 / 3));
    max-height: calc(100dvh - 64px);
    border-width: 4px;
    border-radius: 12px;
    box-shadow: 0 10px 28px rgba(94, 49, 91, 0.2);
  }

  .dev-menu-toggle {
    bottom: max(10px, env(safe-area-inset-bottom));
    left: max(10px, env(safe-area-inset-left));
    padding: 14px 18px;
    font-size: 16px;
    min-height: 48px;
    touch-action: manipulation;
  }

  .dev-menu {
    bottom: max(64px, calc(env(safe-area-inset-bottom) + 56px));
    left: max(8px, env(safe-area-inset-left));
    right: max(8px, env(safe-area-inset-right));
    width: auto;
    max-width: calc(100vw - 16px);
    max-height: min(65dvh, calc(100dvh - 100px));
    padding: 12px;
  }

  .dev-btn {
    min-height: 48px;
    font-size: 16px;
    padding: 12px 14px;
    touch-action: manipulation;
  }

  .dev-code-pad {
    grid-template-columns: repeat(5, 1fr);
    max-height: 180px;
  }

  .dev-code-pad button {
    min-height: 44px;
    font-size: 16px;
    touch-action: manipulation;
  }

  .pause-btn-fixed {
    min-width: 96px;
    font-size: 16px !important;
    padding: 10px 16px !important;
  }

  .controls-row {
    display: none; /* canvas has its own Pause */
  }
}

@media (max-width: 600px) and (orientation: portrait) {
  canvas {
    /* Prefer fitting width on tall phones */
    width: calc(100vw - 8px);
    max-height: calc(100dvh - 56px);
  }
}

@media (max-height: 500px) and (orientation: landscape) {
  canvas {
    width: auto;
    height: calc(100dvh - 12px);
    max-height: calc(100dvh - 12px);
    max-width: calc(100vw - 12px);
  }
  .dev-menu-toggle {
    padding: 10px 14px;
    font-size: 14px;
    min-height: 40px;
  }
}
