/* Light and dark are defined together: bare :root is light, the media query
   covers "system dark", and [data-theme] lets an explicit choice win. */
:root {
  --bg: #e8e2d2;
  --bg-2: #d8d0bc;
  --panel: #fbf7ec;
  --ink: #2f2437;
  --ink-soft: #6b5f78;
  --accent: #5f8f4e;
  --edge: #b9ae97;
  --kbd-bg: #fffdf6;
  --shadow: rgba(47, 36, 55, 0.22);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #171622;
    --bg-2: #101019;
    --panel: #22212f;
    --ink: #ece6dc;
    --ink-soft: #9a91a8;
    --accent: #8fd07a;
    --edge: #3a374a;
    --kbd-bg: #2c2a3a;
    --shadow: rgba(0, 0, 0, 0.5);
  }
}

:root[data-theme="dark"] {
  --bg: #171622;
  --bg-2: #101019;
  --panel: #22212f;
  --ink: #ece6dc;
  --ink-soft: #9a91a8;
  --accent: #8fd07a;
  --edge: #3a374a;
  --kbd-bg: #2c2a3a;
  --shadow: rgba(0, 0, 0, 0.5);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background: radial-gradient(circle at 50% 0%, var(--bg) 0%, var(--bg-2) 100%);
  color: var(--ink);
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

#cabinet {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  max-width: 100%;
}

header { text-align: center; }

h1 {
  margin: 0;
  font-size: 26px;
  letter-spacing: 5px;
  text-transform: uppercase;
  color: var(--ink);
}

.tagline {
  margin: 2px 0 0;
  font-size: 12px;
  color: var(--ink-soft);
  letter-spacing: 1px;
}

#screen-wrap {
  padding: 10px;
  background: var(--panel);
  border: 1px solid var(--edge);
  border-radius: 10px;
  box-shadow: 0 10px 28px var(--shadow);
  line-height: 0;
}

#screen {
  /* size is snapped to a whole pixel scale from JS; this is the fallback */
  width: min(calc(100vw - 60px), calc((100vh - 190px) * 320 / 288), 960px);
  aspect-ratio: 320 / 288;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #101018;
  border-radius: 3px;
  outline: none;
  display: block;
}

footer { text-align: center; }

#status {
  margin: 0 0 6px;
  font-size: 11px;
  color: var(--ink-soft);
}

.keys {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  gap: 4px 12px;
  justify-content: center;
  margin: 0;
  padding: 0;
  font-size: 11px;
  color: var(--ink-soft);
}

kbd {
  background: var(--kbd-bg);
  border: 1px solid var(--edge);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 1px 5px;
  font: inherit;
  font-size: 10px;
  color: var(--ink);
}

/* ---- the on-screen pad ---------------------------------------------------
   Only built when there is no keyboard worth speaking of (js/touch.js), so
   everything here is scoped to the class that goes on the body with it. */
#pad {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  width: min(100%, 460px);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.pad-rows {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  gap: 16px;
}

.pad-dir {
  display: grid;
  grid-template-columns: repeat(3, 46px);
  grid-template-rows: repeat(3, 46px);
  gap: 3px;
}
.pad-dir .up    { grid-area: 1 / 2; }
.pad-dir .left  { grid-area: 2 / 1; }
.pad-dir .right { grid-area: 2 / 3; }
.pad-dir .down  { grid-area: 3 / 2; }

.pad-face {
  display: grid;
  grid-template-columns: repeat(2, 58px);
  gap: 10px;
  align-items: center;
}
.pad-face .b { margin-top: 26px; }

.pad-aux {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
}

.pad-btn {
  font: inherit;
  font-size: 13px;
  color: var(--ink);
  background: var(--panel);
  border: 1px solid var(--edge);
  border-bottom-width: 3px;
  border-radius: 10px;
  padding: 0;
  min-height: 44px;
  touch-action: none;
  cursor: pointer;
}
.pad-btn:active,
.pad-btn[aria-pressed="true"] {
  background: var(--bg-2);
  border-bottom-width: 1px;
  transform: translateY(2px);
}

.pad-face .pad-btn {
  height: 58px;
  border-radius: 50%;
  font-size: 18px;
  font-weight: 700;
}
.pad-face .a { background: var(--accent); color: var(--panel); border-color: var(--accent); }

.pad-aux .pad-btn {
  min-height: 34px;
  font-size: 10px;
  letter-spacing: 1px;
  padding: 0 10px;
  color: var(--ink-soft);
}

/* With a pad on screen the printed key legend is just clutter, and the
   screen wants every pixel of height it can get. */
/* Screen at the top where the eyes are, pad at the bottom where the thumbs
   are, and the slack between them rather than all of it underneath. */
body.has-pad { padding: 8px; align-items: stretch; }
body.has-pad #cabinet {
  width: 100%;
  min-height: calc(100vh - 16px);
  justify-content: flex-start;
}
/* two auto margins share the slack, so the screen sits in the space above
   the pad rather than being jammed against the title */
body.has-pad #screen-wrap { margin-top: auto; }
body.has-pad #pad { margin-top: auto; }
body.has-pad .keys,
body.has-pad #status { display: none; }
body.has-pad header h1 { font-size: 19px; letter-spacing: 3px; }
body.has-pad header .tagline { display: none; }
body.has-pad #cabinet { gap: 8px; }
body.has-pad #screen-wrap { padding: 5px; border-radius: 7px; }
body.has-pad #screen { max-width: 100%; }
body.has-pad footer { margin-top: 4px; }

@media (max-height: 700px) {
  body.has-pad header { display: none; }
  .pad-dir { grid-template-columns: repeat(3, 42px); grid-template-rows: repeat(3, 42px); }
  .pad-face { grid-template-columns: repeat(2, 52px); }
  .pad-face .pad-btn { height: 52px; }
}

/* ---- landscape on a phone ------------------------------------------------
   Stacked controls have nowhere to go when the screen is 380px tall, and
   there is a great deal of unused room either side of a 320-wide game. So the
   pad moves into the margins: directions under the left thumb, Z and X under
   the right, and everything else along the top out of the way. */
@media (orientation: landscape) and (max-height: 560px) {
  body.has-pad { align-items: center; }
  body.has-pad #cabinet { min-height: 0; justify-content: center; }
  body.has-pad #screen-wrap { margin-top: 0; }
  body.has-pad footer { display: none; }

  body.has-pad #pad {
    position: fixed;
    inset: 0;
    width: auto;
    display: block;
    margin: 0;
    pointer-events: none;   /* only the buttons themselves take a finger */
    z-index: 2;
  }
  body.has-pad .pad-rows { display: contents; }
  body.has-pad .pad-dir,
  body.has-pad .pad-face,
  body.has-pad .pad-aux { position: fixed; pointer-events: auto; }

  body.has-pad .pad-dir { left: 10px; bottom: 10px; }
  body.has-pad .pad-face { right: 10px; bottom: 16px; }
  body.has-pad .pad-aux {
    top: 8px; right: 8px;
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
  }
  body.has-pad .pad-aux .pad-btn { min-height: 26px; font-size: 9px; padding: 0 8px; }
}
