/* pinyol's letters (brand manual): Jersey 15 for titles, Silkscreen for small labels. Pixelify Sans only draws the
   cover's preview, as the game draws it (map.js) */
@font-face { font-family: "Jersey"; src: url(fonts/jersey15.woff2) format("woff2"); font-display: swap; }
@font-face { font-family: "Silk"; src: url(fonts/silkscreen.woff2) format("woff2"); font-display: swap; }
@font-face { font-family: "Pixelify Sans"; src: url(fonts/PixelifySans.ttf); font-display: swap; }

:root {
  /* pinyol's colours (its site and brand manual): ink blues, paper, gold for what is chosen */
  --bg: #0b0e1f;
  --panel: #131837;
  --panel-2: #1c2350;
  --line: #2b3368;
  --field: #6e76b3; /* what can be typed in or chosen: 3:1 against the panels (WCAG 1.4.11) */
  --ink: #eef0ff;
  --muted: #8f97cc;
  --amber: #ffc94a;
  --amber-ink: #0b0e1f;
  --sky: #6cb4e0;
  --red: #e8443a;
  --radius: 6px;
  --display: "Jersey", system-ui, sans-serif;
  --label: "Silk", ui-monospace, monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
.vh { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; } /* read, not seen */
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
  display: grid;
  grid-template-rows: auto auto 1fr;
  grid-template-columns: minmax(0, 1fr); /* never wider than the window: the tabs scroll instead */
  overflow: hidden;
}

button, .button, select, input { font: inherit; color: inherit; }
button, .button {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 6px 12px;
  cursor: pointer;
  transition: background .12s, border-color .12s, transform .06s;
}
button:hover, .button:hover { border-color: var(--muted); }
button:active, .button:active { transform: translateY(1px); }
button:focus-visible, input:focus-visible, select:focus-visible, canvas:focus-visible, .button:focus-within,
summary:focus-visible { outline: 2px solid var(--sky); outline-offset: 2px; }
button:disabled { opacity: .45; cursor: not-allowed; }
button:disabled:active { transform: none; }
button.primary { background: var(--amber); color: var(--amber-ink); border-color: var(--amber); font-weight: 600; }
button.primary:hover { background: #ffc54d; }
button.danger { color: #ffb4ab; }
button.danger:hover { border-color: var(--red); }
button[aria-pressed="true"] { background: var(--amber); color: var(--amber-ink); border-color: var(--amber); }
input, select {
  background: var(--bg);
  border: 1px solid var(--field);
  border-radius: var(--radius);
  padding: 5px 8px;
}
input[type=range] { padding: 0; accent-color: var(--amber); }
input[type=color] { padding: 0; width: 44px; height: 30px; }
h2 { font: 11px/1.4 var(--label); letter-spacing: .06em; text-transform: uppercase; color: var(--muted); margin: 16px 0 8px; }
label { display: grid; gap: 4px; margin: 8px 0; color: var(--muted); }
label > input, label > select { color: var(--ink); }

/* header */
.top {
  display: flex; align-items: center; gap: 16px;
  padding: 10px 16px;
  background: linear-gradient(180deg, var(--panel-2), var(--panel));
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; margin: 0; font-size: inherit; }
.logo { display: block; line-height: 0; } .logo img { height: 36px; width: auto; }
.brand .sub { font: 24px/1 var(--display); color: var(--muted); }
.title {
  flex: 1; min-width: 0; max-width: 360px;
  font: 26px/1.1 var(--display); letter-spacing: .01em;
  background: transparent; border-color: transparent; border-bottom-color: var(--field); border-radius: 0;
}
.title:hover, .title:focus { border-color: var(--field); border-radius: var(--radius); }
.saved { color: var(--muted); font-size: 12px; white-space: nowrap; }
.actions { margin-left: auto; display: flex; gap: 8px; align-items: center; }
.menu { position: relative; }
.menu-list {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 20;
  display: grid; min-width: 220px;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: 0 12px 30px #0008;
}
.menu-list button { border: 0; border-radius: 0; text-align: left; background: none; }
.menu-list button:hover { background: var(--line); }

/* tabs */
.tabs { display: flex; gap: 2px; padding: 0 16px; background: var(--panel); border-bottom: 1px solid var(--line); }
.tabs button {
  font: 22px/1 var(--display); letter-spacing: .02em;
  background: none; border: 0; border-radius: 0;
  padding: 10px 16px 8px;
  color: var(--muted);
  border-bottom: 3px solid transparent;
}
.tabs button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--amber); }
.tabs button:hover { color: var(--ink); }

main { min-height: 0; }
.tab { height: 100%; display: grid; grid-template-columns: 220px 1fr 260px; min-height: 0; }
.tab[hidden] { display: none; }
#tab-map, #tab-sound { grid-template-columns: 220px 1fr; }
#tab-logic, #tab-game { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
#tab-game { grid-template-rows: auto 1fr; }
.list, .props { background: var(--panel); padding: 4px 14px 14px; overflow: auto; }
.list { border-right: 1px solid var(--line); }
.props { border-left: 1px solid var(--line); }
.work { display: flex; flex-direction: column; align-items: safe center; gap: 14px; padding: 16px; overflow: auto; min-width: 0; }
.tools { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; justify-content: center; }
.tools label { display: flex; align-items: center; gap: 6px; margin: 0 6px; }
.tools .gap { width: 14px; }
.hint { color: var(--muted); font-size: 12px; }
.add { width: 100%; margin-top: 8px; border-style: dashed; }
label.add { display: block; text-align: center; color: var(--ink); }
.warn { color: #ffb4ab; }
.tab.dropping .list { outline: 2px dashed var(--amber); outline-offset: -6px; }

/* lists of things: each a button in its item */
#drawings, #sounds, #videos, #levels { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: minmax(0, 1fr); gap: 4px; }
.item {
  display: flex; align-items: center; gap: 10px; width: 100%; min-width: 0; text-align: left;
  padding: 6px 8px; background: none; border: 1px solid transparent;
}
.item:hover { background: var(--panel-2); border-color: transparent; }
.item[aria-current="true"] { background: var(--panel-2); border-color: var(--amber); }
#drawings canvas { width: 32px; height: 32px; flex: none; object-fit: contain; image-rendering: pixelated; background: var(--bg); border-radius: 3px; }
.item > .name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item > .kind { margin-left: auto; flex: none; font-size: 11px; color: var(--muted); }

/* the pixel canvas: a checkerboard behind see-through pixels */
#pixels, #preview, #map {
  image-rendering: pixelated;
  background-color: #2a2433;
  background-image: linear-gradient(45deg, #332c3e 25%, transparent 25%, transparent 75%, #332c3e 75%),
                    linear-gradient(45deg, #332c3e 25%, transparent 25%, transparent 75%, #332c3e 75%);
  background-size: 16px 16px; background-position: 0 0, 8px 8px;
  border: 1px solid var(--line); border-radius: 4px;
  touch-action: none;
}
#pixels { max-width: 100%; height: auto; cursor: crosshair; } /* its own proportions; width set by sprites.js */
#pixels, #map { -webkit-touch-callout: none; user-select: none; }
.frames { display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 8px; }
.frames .label { color: var(--muted); }
#frames { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; }
#frames button { padding: 0; border: 2px solid var(--line); border-radius: 4px; background: var(--bg); line-height: 0; }
#frames button[aria-pressed="true"] { border-color: var(--amber); }
#frames canvas { width: 40px; height: 40px; object-fit: contain; image-rendering: pixelated; }
#preview { width: 64px; height: 64px; margin-left: 12px; }

.palette { display: grid; grid-template-columns: repeat(6, 1fr); gap: 4px; }
.palette button { aspect-ratio: 1; padding: 0; border-radius: 4px; border: 2px solid transparent; }
.palette button[aria-pressed="true"] { border-color: var(--ink); box-shadow: 0 0 0 2px var(--bg) inset; }
fieldset.use { border: 1px solid var(--line); border-radius: var(--radius); padding: 4px 10px; margin: 10px 0; }
fieldset.use legend { color: var(--muted); padding: 0 4px; }
fieldset.use label { display: flex; align-items: flex-start; gap: 8px; margin: 6px 0; color: var(--ink); }
fieldset.use input { margin-top: 3px; flex: none; }
fieldset.use label.sub { margin-left: 22px; color: var(--muted); }
#object-props label { grid-template-columns: 1fr auto; }
#object-props label select, #object-props label input { grid-column: 1 / -1; }
output { color: var(--amber); font-variant-numeric: tabular-nums; }
label.any { display: flex; align-items: center; justify-content: space-between; color: var(--ink); }
label.check { display: flex; align-items: center; gap: 8px; color: var(--ink); }

/* bringing a picture in */
dialog {
  background: var(--panel); color: var(--ink); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 4px 20px 18px; width: min(420px, calc(100vw - 32px)); box-shadow: 0 20px 50px #000a;
}
dialog::backdrop { background: #0009; }
#import-preview {
  display: block; margin: 8px auto 0; max-width: 100%; image-rendering: pixelated;
  background-color: #2a2433;
  background-image: linear-gradient(45deg, #332c3e 25%, transparent 25%, transparent 75%, #332c3e 75%),
                    linear-gradient(45deg, #332c3e 25%, transparent 25%, transparent 75%, #332c3e 75%);
  background-size: 16px 16px; background-position: 0 0, 8px 8px;
}
dialog p { text-align: center; margin: 6px 0; }
#console { width: min(560px, calc(100vw - 32px)); }
#console .way { border: 1px solid var(--line); border-radius: var(--radius); padding: 4px 16px 14px; margin: 12px 0; }
#console h3 { font: 22px/1.1 var(--display); margin: 12px 0 6px; }
#console p, #console ol { text-align: left; margin: 6px 0; }
#console ol { padding-left: 20px; }
#console li { margin: 4px 0; }
#console progress { width: 100%; accent-color: var(--amber); }
.status { min-height: 1.4em; }
.status.error { color: #ffb4ab; }
.dialog-buttons { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }

/* map */
.pieces { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; }
.pieces button { padding: 2px; aspect-ratio: 1; display: grid; place-items: center; }
.pieces canvas { width: 100%; height: 100%; object-fit: contain; image-rendering: pixelated; }
.map-work { align-items: stretch; }
.map-scroll { overflow: auto; flex: 1; min-height: 0; border-radius: 4px; }
#map { display: block; cursor: crosshair; }
.backdrops { display: flex; gap: 10px; align-items: center; color: var(--muted); }
.backdrops label { margin: 0; color: var(--ink); }
.cover-work { align-items: stretch; max-width: 560px; }
#cover-preview { width: 100%; max-width: 480px; align-self: center; image-rendering: pixelated; border: 1px solid var(--line); border-radius: 4px; }
#cover-preview.off { opacity: .35; }
fieldset.game-info { border: 1px solid var(--line); border-radius: var(--radius); padding: 2px 12px 8px; margin: 10px 0 0; }
fieldset.game-info legend { color: var(--ink); font-weight: 600; padding: 0 4px; }
.anim-rows { display: grid; gap: 6px; margin: 4px 0; }
.anim-row { display: flex; align-items: center; gap: 8px; color: var(--muted); }
.anim-row span { min-width: 4.5em; color: var(--ink); }
.anim-row input { width: 4.5em; }
label.inline { display: flex; align-items: center; gap: 8px; margin: 0; }

/* sounds */
.sound-work { align-items: stretch; max-width: 820px; }
.recipes { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; }
.recipes button { font: 20px/1 var(--display); padding: 12px 6px; }
.recipes button[aria-pressed="true"] { background: var(--sky); color: #08202e; border-color: var(--sky); }
.sound-head { display: flex; flex-wrap: wrap; align-items: end; gap: 8px; }
.sound-head label { margin: 0; flex: 1 1 160px; }
#wave { width: 100%; height: 120px; background: var(--bg); border: 1px solid var(--line); border-radius: 4px; }
.composer { display: grid; gap: 10px; min-width: 0; }
.composer:empty { display: none; }
.song-scroll { overflow-x: auto; border: 1px solid var(--line); border-radius: 4px; }
#song-grid { display: block; touch-action: none; cursor: pointer; }
@media (pointer: coarse) { #song-grid { touch-action: pan-x pan-y; } } /* a finger scrolls the grid; a tap puts a note */
.sliders { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 4px 18px; }
.sliders label { grid-template-columns: 1fr auto; margin: 4px 0; }
.sliders input, .sliders select { grid-column: 1 / -1; }

/* logic */
.blockly { min-height: 0; min-width: 0; overflow: hidden; }
.code { background: var(--panel); border-top: 1px solid var(--line); max-height: 40vh; overflow: auto; }
.code summary { padding: 8px 16px; cursor: pointer; color: var(--muted); }
.code pre { margin: 0; padding: 0 16px 16px; font: 12px/1.5 ui-monospace, monospace; color: #cfe8ff; }

/* game */
.game-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line); }
#game-status { color: var(--muted); }
#game-status.error { color: #ffb4ab; }
#player { width: 100%; height: 100%; border: 0; background: #0d0b12; }

/* always there for screen readers; seen only while it has something to say */
.toast {
  position: fixed; left: 16px; right: 16px; bottom: 20px; margin: 0 auto; width: fit-content; max-width: 640px;
  display: flex; align-items: center; gap: 12px;
  background: var(--panel-2); border: 1px solid var(--amber); border-radius: var(--radius);
  padding: 10px 12px 10px 16px; box-shadow: 0 10px 30px #0009; z-index: 50;
  opacity: 0; pointer-events: none; transition: opacity .15s;
}
.toast.shown { opacity: 1; pointer-events: auto; }
.toast.error { border-color: var(--red); }
.toast button { padding: 4px 10px; }
.coarse-only { display: none; }
@media (pointer: coarse) {
  .coarse-only { display: inline-block; }
  input, select { font-size: 16px; } /* iOS zooms into anything smaller */
  button, .button, .item { min-height: 44px; }
  .tools button, .dialog-buttons button { min-width: 44px; }
}
#map.pan { touch-action: pan-x pan-y; cursor: grab; }

@media (max-width: 900px) {
  body { overflow: auto; }
  .tab { grid-template-columns: 1fr; height: auto; }
  .list, .props { border: 0; }
  .top { flex-wrap: wrap; }
  .actions { margin-left: 0; flex-wrap: wrap; }
  .menu-list { right: auto; left: 0; max-width: calc(100vw - 32px); }
  #tab-map, #tab-sound { grid-template-columns: 1fr; } /* (these set their own columns above) */
  .title { order: 3; flex: 1 1 100%; max-width: none; }
  .saved { margin-left: auto; }
  .actions { order: 4; }
  .tabs { overflow-x: auto; padding: 0 8px; }
  .tabs button { padding: 10px 8px 8px; }
  .tabs button:focus-visible { outline-offset: -3px; } /* the strip scrolls: an outside ring would be cut */
  #tab-draw > .work { order: -1; } /* the canvas first, then the colours, then the list */
  #tab-draw > .props { order: 0; }
  #drawings { grid-auto-flow: column; grid-auto-columns: minmax(150px, 1fr); grid-template-columns: none; overflow-x: auto; }
  .map-scroll { max-height: 65dvh; overscroll-behavior: contain; }
  #tab-logic { height: 100%; min-height: 70dvh; grid-template-rows: minmax(420px, 1fr) auto; }
  #player { height: calc((100vw - 32px) * 2 / 3 + 260px); min-height: 420px; }
  .tools select { max-width: 100%; min-width: 0; }
}
@media (max-width: 30em) { .tabs { padding: 0 6px; } .tabs button { font-size: 18px; padding: 10px 6px 8px; } } /* the five fit in 320 px */
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }

/* the guide: a panel in a corner, and what it speaks about outlined */
.guide {
  position: fixed; right: 16px; bottom: 16px; z-index: 40; width: min(360px, calc(100vw - 32px));
  background: var(--panel-2); border: 2px solid var(--amber); border-radius: var(--radius);
  padding: 4px 16px 14px; box-shadow: 0 16px 40px #000b;
}
.guide h2 { font: 26px/1.1 var(--display); color: var(--ink); text-transform: none; letter-spacing: 0; margin: 4px 0 6px; }
.guide h2:focus { outline: none; }
.guide p { margin: 6px 0; line-height: 1.5; }
.guide-step { color: var(--muted); font-size: 12px; }
.guide-buttons { display: flex; gap: 8px; margin-top: 12px; }
.guide-buttons .gap { flex: 1; }
.guided { outline: 3px solid var(--amber) !important; outline-offset: 3px; border-radius: var(--radius); }
@media (max-width: 900px) { .guide { left: 16px; right: 16px; width: auto; } }
