:root {
  --bg: #0b1322; --panel: #121c32; --panel2: #18253f; --line: #263756; --text: #e6eefc; --muted: #8ea2c4;
  --accent: #4fd1e8; --ok: #3ddc84; --warn: #ffb84a; --bad: #ff6b6b;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif; }
h1, h2 { margin: 0; }
.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 16px; background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { font-size: 19px; }
.brand small { color: var(--muted); font-weight: 500; font-size: 14px; margin-left: 6px; }
.lamp { width: 12px; height: 12px; border-radius: 50%; background: var(--ok); box-shadow: 0 0 10px var(--ok); }
button { font: inherit; color: var(--text); background: var(--panel2); border: 1px solid var(--line); border-radius: 9px; padding: 6px 12px; cursor: pointer; }
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: 0.4; cursor: not-allowed; }
button[aria-pressed="true"], button.on { background: #16465a; border-color: var(--accent); }
input, textarea { font: inherit; color: var(--text); background: #0d1730; border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; width: 100%; }
input:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
textarea { resize: vertical; }

.layout { display: grid; grid-template-columns: 250px minmax(300px, 1fr) 340px; gap: 14px; padding: 14px; align-items: start; max-width: 1400px; margin: 0 auto; }
.palette, .card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 12px; }
.card + .card { margin-top: 12px; }
.side { position: sticky; top: 10px; max-height: calc(100vh - 20px); overflow-y: auto; }
.side-tabs { display: flex; gap: 4px; margin-bottom: 10px; position: sticky; top: 0; z-index: 2; background: var(--bg, #0b1326); padding-bottom: 2px; }
.side-tabs .seg { flex: 1; padding: 6px 4px; font-size: 13px; }
.card h2, .palette h3 { font-size: 13px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); margin: 0 0 8px; }
.tools { display: flex; gap: 6px; margin-bottom: 10px; }
.tools .tool { flex: 1; padding: 6px 4px; }
.group { margin-bottom: 10px; }
.cells { display: flex; flex-wrap: wrap; gap: 4px; }
.pal { padding: 0; width: 38px; height: 38px; border-radius: 8px; overflow: hidden; background: transparent; }
.pal canvas { display: block; width: 36px; height: 36px; }
.pal[aria-pressed="true"] { outline: 2px solid var(--accent); border-color: var(--accent); }

.stage { min-width: 0; }
.size-row { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-bottom: 8px; }
.size-row label { display: flex; align-items: center; gap: 6px; color: var(--muted); }
.size-row input[type="number"] { width: 64px; }
.layers { display: flex; align-items: center; gap: 4px; }
.layers .lbl { color: var(--muted); margin-right: 4px; }
.seg { padding: 6px 11px; }
.layer-tabs { display: flex; gap: 6px; margin-bottom: 8px; }
.canvas-wrap { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 10px; display: flex; overflow: auto; }
.canvas-wrap > canvas { margin: auto; }
.canvas-wrap.zoomed > #grid { max-width: none; }
#grid { touch-action: none; cursor: crosshair; max-width: 100%; height: auto; border-radius: 6px; }
.info { color: var(--muted); min-height: 2.8em; margin: 8px 2px; }

.field { display: block; margin-bottom: 10px; color: var(--muted); font-size: 13px; }
.field input, .field textarea { margin-top: 3px; color: var(--text); font-size: 15px; }
.count { float: right; font-size: 12px; }
.count.over { color: var(--bad); }
.stars { display: flex; gap: 6px; margin-top: 3px; }
.row { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0; }
.note { color: var(--muted); font-size: 13px; margin: 6px 0; }
.note.bad { color: var(--bad); }
.note.ok { color: var(--ok); }
.checks { list-style: none; margin: 0; padding: 0; }
.checks li { padding: 5px 0 5px 22px; position: relative; font-size: 14px; border-bottom: 1px solid var(--line); }
.checks li:last-child { border-bottom: 0; }
.checks li::before { content: ""; position: absolute; left: 4px; top: 11px; width: 10px; height: 10px; border-radius: 50%; background: var(--ok); }
.checks li.warning::before { background: var(--warn); }
.checks li.error::before { background: var(--bad); }
.conn-block { margin-bottom: 10px; font-size: 14px; }
.conn-block .name { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.conn-block .name canvas { width: 28px; height: 28px; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; }
.chip { padding: 3px 9px; border-radius: 999px; font-size: 13px; }
.conn-empty { color: var(--muted); font-size: 13px; }
select { font: inherit; color: var(--text); background: #0d1730; border: 1px solid var(--line); border-radius: 8px; padding: 4px 6px; margin-right: 6px; }
.meter { height: 10px; background: #0d1730; border: 1px solid var(--line); border-radius: 999px; overflow: hidden; }
.bar { height: 100%; width: 0; background: var(--ok); }
.bar.warn { background: var(--warn); }
.bar.bad { background: var(--bad); }
.meter-text { margin: 6px 0 0; color: var(--muted); font-size: 13px; }
.qr-box { display: flex; justify-content: center; background: #fff; border-radius: 10px; padding: 6px; min-height: 60px; }
.qr-box canvas { max-width: 100%; height: auto; image-rendering: pixelated; }
.qr-box.empty { background: var(--panel2); color: var(--muted); align-items: center; font-size: 13px; }
.foot { color: var(--muted); font-size: 12px; text-align: center; padding: 16px; }
.foot p { margin: 4px auto; max-width: 900px; }
.foot a { color: var(--accent); }

@media (max-width: 1099px) { .layout { grid-template-columns: 220px 1fr; } .side { grid-column: 1 / -1; position: static; max-height: none; overflow: visible; } }
@media (max-width: 700px) { .layout { grid-template-columns: 1fr; padding: 10px; } .palette { order: 2; } .stage { order: 1; } .side { order: 3; } }

[hidden] { display: none !important; }
button.primary { background: #16465a; border-color: var(--accent); }
.play-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 8px; flex-wrap: wrap; }
.play-stats { display: flex; align-items: baseline; gap: 8px; }
.moves { font-size: 26px; font-weight: 700; }
.penalty { color: var(--bad); font-weight: 700; }
.status { color: var(--muted); }
.status.dead { color: var(--bad); }
.status.won { color: var(--ok); }
#play-canvas { touch-action: none; max-width: 100%; height: auto; border-radius: 6px; }
.hint-card { background: var(--panel); border: 1px solid var(--accent); border-radius: 14px; padding: 10px 14px; margin: 10px 0; }
.pad { display: grid; grid-template-columns: repeat(3, 64px); grid-template-rows: repeat(2, 48px); gap: 6px; justify-content: center; margin: 10px 0; }
.pad button { font-size: 18px; }
.pad [data-act="up"] { grid-column: 2; grid-row: 1; }
.pad [data-act="left"] { grid-column: 1; grid-row: 2; }
.pad [data-act="down"] { grid-column: 2; grid-row: 2; }
.pad [data-act="right"] { grid-column: 3; grid-row: 2; }
.pad [data-act="wait"] { grid-column: 3; grid-row: 1; font-size: 14px; }
.play-actions { justify-content: center; }
.result { background: var(--panel); border: 1px solid var(--ok); border-radius: 14px; padding: 10px 14px; margin: 10px 0; }
.result p { margin: 0 0 6px; }
.layout.playing .palette { display: none; }
.layout.playing { grid-template-columns: minmax(300px, 1fr) 340px; }
@media (max-width: 1099px) { .layout.playing { grid-template-columns: 1fr; } }

/* Look: colour scheme, floor and wall pictures */
.card h2 small { text-transform: none; letter-spacing: 0; font-weight: 500; font-size: 13px; margin-left: 6px; color: var(--text); }
.opts { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 4px; }
.opts .opt { padding: 0; border-radius: 8px; overflow: hidden; }
.opts .opt canvas { display: block; width: 48px; height: 24px; }
.opts .opt.auto { padding: 0 9px; height: 26px; font-size: 12px; }
.opts .opt[aria-pressed="true"] { outline: 2px solid var(--accent); border-color: var(--accent); }
.opts .swatch { width: 30px; height: 26px; border-radius: 8px; }

/* Zoom and touch */
.zoom { display: flex; gap: 4px; }
.zoom button { min-width: 38px; padding: 6px 8px; }
button, .pal, .tool, .seg { touch-action: manipulation; }
.palette, .side-tabs, .tools { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.pal canvas { display: block; width: 100%; height: 100%; }

/* Fingers (iPad, phones): bigger targets, and 16 px text in fields so that iOS does not zoom the page when one is focused */
@media (pointer: coarse) {
  button { min-height: 44px; }
  .pal { width: 48px; height: 48px; }
  .cells { gap: 6px; }
  .chip { padding: 8px 14px; }
  .seg { min-width: 44px; }
  input, textarea, select { font-size: 16px; min-height: 40px; }
  .opts .opt { min-height: 0; }
  .opts .opt canvas { width: 56px; height: 28px; }
  .opts .opt.auto { height: 34px; }
  .opts .swatch { width: 38px; height: 34px; }
  .layers .seg, .layer-tabs button { min-height: 44px; }
}

/* Installed as an app: leave room for the notch and the home bar */
body { padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }
@media (display-mode: standalone) { .foot .offline { display: none; } }

/* Tablet (iPad and similar, 700 to 1099 px wide, or landscape up to 1400 px): three columns. The page scrolls as a whole and the
   grid in the middle gets all the height it needs. Palette and settings stay docked at the top of the window and scroll on their own. */
@media (min-width: 700px) and (max-width: 1099px), (orientation: landscape) and (min-width: 1100px) and (max-width: 1400px) and (max-height: 900px) {
  .layout { grid-template-columns: 196px minmax(0, 1fr) 252px; grid-template-areas: "palette stage side"; gap: 10px; padding: 10px; align-items: start; width: 100%; }
  .palette, .side { position: sticky; top: 8px; max-height: calc(100vh - 16px); max-height: calc(100dvh - 16px); overflow-y: auto; overscroll-behavior: contain; min-height: 0; }
  .palette { grid-area: palette; }
  .side { grid-area: side; grid-column: auto; }
  .stage { grid-area: stage; min-width: 0; }
  .layout.playing { grid-template-columns: minmax(0, 1fr); grid-template-areas: "stage"; }
  .layout.playing .side { display: none; }
}
@media (orientation: portrait) and (min-width: 700px) and (max-width: 1099px) {
  .layout { grid-template-columns: 172px minmax(0, 1fr) 226px; gap: 8px; padding: 8px; }
  .side-tabs .seg { font-size: 10px; letter-spacing: -0.02em; }
}
@media (orientation: landscape) and (min-width: 700px) and (max-width: 1400px) and (max-height: 900px) {
  .layout { grid-template-columns: 232px minmax(0, 1fr) 296px; }
}
@media (min-width: 700px) and (max-width: 1099px), (min-width: 700px) and (max-width: 1400px) and (max-height: 900px) {
  .tools { flex-wrap: wrap; }
  .tools .tool { flex: 1 1 40%; }
  .side-tabs .seg { font-size: 11.5px; padding: 6px 1px; min-width: 0; }
  .size-row { gap: 6px; }
  .size-row input[type="number"] { width: 56px; }
}
