/* ---------- Page reset & viewport-bound layout ---------- */
html, body {
  margin: 0;
  height: 100%;
}

*, *::before, *::after {
  box-sizing: border-box;
}

/* The HTML `hidden` attribute must always win over our display:flex rules. */
[hidden] {
  display: none !important;
}

body {
  min-height: 100dvh;          /* dvh handles mobile URL bar */
  display: flex;
  flex-direction: column;
  overflow: hidden;            /* no scrollbars; canvas is sized to fit */
  font-size: 14px;
}

/* ---------- Controls (everything above the stage) ---------- */
h1 {
  margin: 0.2em 0;
  font-size: 1.1rem;
}

header.controls {
  flex: 0 0 auto;              /* natural height, never grows */
}

/* ---------- Stage: takes all leftover viewport space ---------- */
#stage {
  flex: 1 1 auto;              /* fill remaining vertical space */
  min-height: 0;               /* allow shrinking below content size */
  min-width: 0;
  display: grid;
  place-items: center;         /* center the canvas */
  padding: 4px 4px 16px;       /* extra bottom padding so the border is fully visible */
}

/* ---------- Canvas: largest box with correct aspect ratio that fits ---------- */
#stage canvas {
  display: block;
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  border: 1px solid black;
}

#stage.landscape canvas { aspect-ratio: 16 / 9; }
#stage.portrait  canvas { aspect-ratio: 9 / 16; }

/* ---------- Phone in landscape: put controls beside the stage ---------- */
@media (orientation: landscape) and (max-height: 500px) {
  h1 { display: none; }

  body { flex-direction: row; }

  header.controls {
    flex: 0 0 auto;
    overflow-y: auto;          /* scroll controls if they exceed height */
    max-width: 40vw;
  }

  #stage { flex: 1 1 auto; }
}

/* ---------- Main wrapper ---------- */
main#app {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

/* ---------- Login screens ---------- */
.login {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  gap: 0.5rem;
}

.login form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  width: min(320px, 100%);
}

.login label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.login input {
  padding: 0.5rem;
  font-size: 1rem;
}

.login button, .login-btn {
  padding: 0.5rem;
  font-size: 1rem;
  cursor: pointer;
  text-align: center;
  text-decoration: none;
  display: inline-block;
}

.login-btn {
  background: #1976d2;
  color: white;
  border-radius: 4px;
  padding: 0.5rem 1rem;
}

.error {
  color: #b00020;
  margin: 0;
}

/* ---------- Session footer ---------- */
footer.session {
  flex: 0 0 auto;
  padding: 0.25rem 0.5rem;
  font-size: 0.85rem;
  border-top: 1px solid #ddd;
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

footer.session button {
  margin-left: auto;
}
