/* POWDER Web -- browser shell styles.
   Layout: CSS grid on #app adapts to portrait (controls below) and landscape
   (controls beside the game). Accessibility themes via body classes. */

:root {
  --bg: #0b0b0b;
  --panel: #161616;
  --fg: #e8e8e8;
  --muted: #9a9a9a;
  --accent: #c9a227;
  --btn: #1e1e1e;
  --btn-border: #3a3a3a;
  --danger: #c0473b;
  --ui-scale: 1;
}

/* High-contrast theme. */
body.contrast {
  --bg: #000;
  --panel: #000;
  --fg: #fff;
  --muted: #d0d0d0;
  --accent: #ffd400;
  --btn: #000;
  --btn-border: #fff;
}

/* Larger UI text. */
body.large-text { --ui-scale: 1.25; }
body.controls-compact { --control-scale: 0.88; }
body.controls-large { --control-scale: 1.14; }
body:not(.controls-compact):not(.controls-large) { --control-scale: 1; }

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: calc(16px * var(--ui-scale));
  overscroll-behavior: none;
  overflow: hidden;
}

/* ---- App layout ---- */
#app {
  display: grid;
  height: 100dvh;
  max-width: 100vw;
  overflow: hidden;
  /* minmax(0,1fr) lets the game column shrink below the canvas's intrinsic
     width instead of blowing the grid out past the viewport. */
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto 1fr auto auto;
  grid-template-areas: "top" "screen" "controls" "status";
}
#topbar    { grid-area: top; }
#screen    { grid-area: screen; }
#touch-controls { grid-area: controls; }
#statusbar { grid-area: status; }

/* Landscape: put the controls beside the game. */
@media (orientation: landscape) {
  #app {
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: auto 1fr auto;
    grid-template-areas:
      "top    top"
      "screen controls"
      "status status";
  }
}

/* Mobile portrait with on-screen controls: the game spans the FULL width at the
   top (sized to its own 4:3 aspect, so no letterboxing), and the touch controls
   take the generous space underneath. `controls-on` is toggled by app.js. */
@media (orientation: portrait) {
  body.controls-on #app {
    grid-template-rows: auto auto 1fr auto;   /* top · game · controls · status */
  }
  /* On mobile the complete 4:3 game frame spans the full width. */
  body.controls-on #screen {
    width: 100%;
    height: auto;
    aspect-ratio: 4 / 3;
    max-height: 70dvh;        /* keep room for controls on short screens */
    align-self: start;
    overflow: hidden;
  }
}

#topbar, #statusbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 6px 12px;
  background: var(--panel);
}
#topbar { justify-content: space-between; }
#title { font-weight: 700; letter-spacing: 0.18em; color: var(--accent); }

.topbar-actions { display: flex; align-items: center; gap: 8px; }

#statusbar { justify-content: center; font-size: 0.7rem; color: var(--muted); }
#status { max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---- First-run quick start ---- */
#quickstart {
  position: fixed;
  left: max(12px, env(safe-area-inset-left));
  right: max(12px, env(safe-area-inset-right));
  bottom: calc(54px + env(safe-area-inset-bottom));
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  max-width: 760px;
  margin: 0 auto;
  padding: 12px;
  border: 1px solid var(--btn-border);
  border-radius: 12px;
  background: color-mix(in srgb, var(--panel) 94%, transparent);
  box-shadow: 0 10px 30px rgba(0,0,0,0.5);
}
#quickstart[hidden] { display: none; }
#quickstart h2 {
  margin: 0 0 4px;
  font-size: 0.9rem;
  letter-spacing: 0.08em;
  color: var(--accent);
  text-transform: uppercase;
}
#quickstart p { margin: 0; color: var(--fg); font-size: 0.85rem; line-height: 1.35; }
#quickstart button { flex: 0 0 auto; padding: 8px 14px; }

@media (max-width: 520px) {
  #quickstart {
    align-items: stretch;
    flex-direction: column;
    top: calc(48px + env(safe-area-inset-top));
    bottom: auto;
  }
}

/* ---- Buttons (shared) ---- */
button {
  font-family: inherit;
  color: var(--fg);
  background: var(--btn);
  border: 1px solid var(--btn-border);
  border-radius: 8px;
  cursor: pointer;
  min-height: 44px;            /* accessible touch target */
}
button:hover { border-color: var(--accent); }
button:active { background: #2a2a2a; }

#fullscreen-btn, #menu-btn {
  min-width: 44px;
  font-size: 1.1rem;
  line-height: 1;
  background: transparent;
  color: var(--muted);
}
#fullscreen-btn:hover, #menu-btn:hover { color: var(--fg); }
.topbar-actions .install-btn {
  min-height: 36px;
  padding: 0 12px;
  border-color: color-mix(in srgb, var(--accent) 70%, var(--btn-border));
  color: var(--accent);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.05em;
}

@media (max-width: 390px) {
  .topbar-actions { gap: 6px; }
  .topbar-actions .install-btn { padding: 0 10px; }
}

/* ---- Game viewport ---- */
#screen {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
  background: #000;
}
#game-frame {
  /* app.js fits and centers the complete 4:3 SDL surface. */
  flex: 0 0 auto;
  overflow: hidden;
  background: #000;
}
#canvas {
  /* Preserve POWDER's source pixels when fitting its 512x384 SDL surface to
     the viewport. Fractional scaling can vary a pixel's displayed width
     slightly, but it does not invent colours or blur tile edges. */
  image-rendering: pixelated;
  display: block;
  width: 100%;
  height: 100%;
  background: #000;
  outline: none;
  touch-action: none;
}
#loading {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 16px; color: var(--muted); background: #000;
}
.spinner {
  width: 36px; height: 36px;
  border: 3px solid #333; border-top-color: var(--accent);
  border-radius: 50%; animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- Touch controls ---- */
#touch-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
  background: var(--panel);
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
  -webkit-touch-callout: none;
}
#touch-controls[hidden] { display: none; }

@media (orientation: landscape) {
  #touch-controls {
    flex-direction: column-reverse;
    padding: 14px calc(14px + env(safe-area-inset-right)) 14px 14px;
  }
}

#dpad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  grid-template-areas: "nw n ne" "w c e" "sw s se";
  gap: 4px;
  width: calc(min(50vw, 252px) * var(--control-scale));
  aspect-ratio: 1 / 1;
  flex: 0 0 auto;
}
.dbtn {
  font-size: 1.75rem;
  font-weight: 700;
  padding: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--btn);
  touch-action: none;
  -webkit-touch-callout: none;
}
.dbtn.center {
  justify-self: center;
  align-self: center;
  width: 52%;
  height: 52%;
  min-width: 34px;
  min-height: 34px;
  border-radius: 999px;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--muted);
}

#action-buttons {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  width: min(38vw, 180px);
  flex: 0 0 auto;
}
.abtn {
  font-size: 0.95rem;
  padding: 8px 4px;
  touch-action: none;
  -webkit-touch-callout: none;
}
.abtn:last-child {
  grid-column: 1 / -1;
  border-color: color-mix(in srgb, var(--accent) 55%, var(--btn-border));
}

@media (orientation: landscape) {
  #dpad { width: calc(min(40vh, 220px) * var(--control-scale)); }
  #action-buttons { width: min(34vh, 170px); }
}

/* ---- Dialogs ---- */
dialog#settings, dialog#install-help {
  width: min(94vw, 30rem);
  max-height: 88dvh;
  overflow: auto;
  border: 1px solid var(--btn-border);
  border-radius: 12px;
  background: var(--panel);
  color: var(--fg);
  line-height: 1.5;
}
dialog#settings::backdrop, dialog#install-help::backdrop { background: rgba(0,0,0,0.65); }
dialog#settings h2, dialog#install-help h2 {
  margin: 0 0 4px;
  letter-spacing: 0.12em;
  color: var(--accent);
}
dialog#settings h3 {
  margin: 18px 0 8px; font-size: 0.8rem; letter-spacing: 0.1em;
  text-transform: uppercase; color: var(--muted);
  border-bottom: 1px solid var(--btn-border); padding-bottom: 4px;
}
dialog#settings section { margin-bottom: 4px; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 6px 0; }
.row select { font-size: 1rem; padding: 8px; min-height: 44px; background: var(--btn); color: var(--fg); border: 1px solid var(--btn-border); border-radius: 8px; }
.row input[type="checkbox"] { width: 24px; height: 24px; }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; }
.btn-row button { flex: 1 1 auto; padding: 8px 12px; }
button.danger { border-color: var(--danger); color: #ff9a8f; }
.hint { font-size: 0.85rem; color: var(--muted); }
.close-row { margin-top: 16px; text-align: right; }
.close-row button { padding: 8px 20px; }

table.controls { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
table.controls td { padding: 3px 0; vertical-align: top; }
table.controls td:first-child { color: var(--muted); width: 50%; }
dialog#settings a { color: var(--accent); }

/* Reduced motion. */
body.reduced-motion * { animation: none !important; transition: none !important; }
