:root {
  color-scheme: dark;
  --bg: #181410;
  --panel: #241e17;
  --border: #4a3a26;
  --gold: #c9a24a;
  --text: #e8dcc0;
  --muted: #9a8a6a;
  --danger: #b5493f;
  --good: #4a8a4a;
  /* Vertical space #topbar actually occupies, used by .vnc-frame-wrap's
     sizing calc below. Kept as a variable (not hardcoded in that calc)
     because the mobile media query further down shrinks the topbar and
     must update this in lockstep, or the vnc box sizing math goes stale. */
  --topbar-budget: 90px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Georgia, 'Times New Roman', serif;
  min-height: 100vh;
}

h1 {
  font-size: 1.3rem;
  color: var(--gold);
  text-align: center;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  margin: 0 0 1.5rem;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 20px;
  margin-bottom: 16px;
}

/* The login form is the only thing still narrow/centered — everything
   logged-in (topbar, VNC, admin panel) uses the full page width. */
.login-panel {
  max-width: 480px;
  margin: 48px auto;
}

label {
  display: block;
  margin-bottom: 4px;
  color: var(--muted);
  font-size: 0.9rem;
}

input[type="text"], input[type="password"] {
  width: 100%;
  padding: 8px 10px;
  margin-bottom: 14px;
  background: #15110c;
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  font-size: 1rem;
}

button {
  width: 100%;
  padding: 10px;
  background: var(--gold);
  border: none;
  border-radius: 4px;
  color: #241e17;
  font-weight: bold;
  font-size: 1rem;
  cursor: pointer;
}

button:disabled {
  background: #5a5040;
  cursor: not-allowed;
}

button.secondary {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
}

.error {
  color: var(--danger);
  margin-bottom: 10px;
  min-height: 1.2em;
  font-size: 0.9rem;
}

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 0.75rem;
}
.badge.rw { background: var(--good); color: #0e1a0e; }
.badge.ro { background: var(--border); color: var(--muted); }

.hidden { display: none !important; }

/* --- Compact top bar: exactly two rows, always, regardless of
   content length — row 1 is whoami + actions (never wraps, both
   sides are short/fixed); row 2 is turn info, which scrolls
   horizontally instead of wrapping if the player list is long. Using
   flex-wrap here instead would let a long player list push the bar to
   3+ lines unpredictably, which is exactly what shouldn't happen. --- */
#topbar {
  padding: 6px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.topbar-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

#whoami {
  color: var(--muted);
  font-size: 0.85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

#topbar button {
  width: auto;
  padding: 5px 14px;
  font-size: 0.85rem;
  white-space: nowrap;
}

#turn-status {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 4px;
  font-size: 0.85rem;
  color: var(--muted);
  white-space: nowrap;
  overflow-x: auto;
  scrollbar-width: thin;
}

#turn-status > * { flex-shrink: 0; }

#turn-status .active-name {
  color: var(--gold);
  font-weight: bold;
}

#turn-status.yours .active-name { color: var(--good); }

.players-list {
  display: flex;
  gap: 6px;
}
.players-list span {
  padding: 2px 8px;
  border-radius: 10px;
  border: 1px solid var(--border);
  font-size: 0.8rem;
  color: var(--muted);
  white-space: nowrap;
}
.players-list span.active {
  color: var(--gold);
  border-color: var(--gold);
}

/* --- VNC: as large as possible below the topbar, exact aspect ratio --- */
#vnc-panel {
  padding: 8px 16px 0;
}

.vnc-mode-row {
  text-align: center;
  margin-bottom: 6px;
}

/* Mobile-only zoom controls (see the mobile media query below for the
   display:flex that actually reveals these) — on desktop this stays
   display:none so .vnc-mode-row's existing centered-badge look is
   completely unchanged. */
.vnc-zoom-controls {
  display: none;
  gap: 6px;
}

.vnc-zoom-controls button {
  width: auto;
  padding: 2px 10px;
  font-size: 0.8rem;
}

/* VCMI's own resolution is cfg.display.width/height (nix/homm3-cloud.nix,
   default 1280x800 = 8:5). Rather than giving the box an arbitrary
   width+height and letting noVNC's resize=scale letterbox inside it
   (which wastes space whenever the box's own shape doesn't match
   8:5), size the box itself to that exact ratio via aspect-ratio, and
   let width/height both come from whichever of "100% of the
   available width" or "available height, convertex to width via the
   ratio" is smaller — that's the largest size that fits either
   dimension without overflowing or distorting. If display.width/height
   ever changes from the 8:5 default, update the 1.6 multiplier below
   to match (width/height).
   #topbar (two rows) + the badge row above take roughly 90px.

   .vnc-scroll is the fixed-size visible viewport (this box never
   changes size). .vnc-frame-wrap is the actual content: 100% of
   .vnc-scroll normally, but the mobile zoom buttons grow it past 100%
   via JS (app.js), which makes it overflow .vnc-scroll and become
   natively scrollable/pannable — same mechanism as zooming into any
   oversized image in a scrolling div, nothing exotic. Desktop never
   touches zoom, so .vnc-frame-wrap stays at 100% and this is a no-op
   there. */
.vnc-scroll {
  position: relative;
  aspect-ratio: 8 / 5;
  /* 100dvh (not 100vh) so this recomputes correctly right after a phone
     rotates, before the browser's address/toolbar chrome has settled —
     100vh on mobile browsers is notoriously stale/wrong during that
     transition. Falls back fine on desktop, where dvh == vh. */
  width: min(100%, calc((100dvh - var(--topbar-budget)) * 1.6));
  max-height: calc(100dvh - var(--topbar-budget));
  margin: 0 auto;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}

.vnc-frame-wrap {
  width: 100%;
  height: 100%;
}

.vnc-frame-wrap iframe {
  display: block;
  width: 100%;
  height: 100%;
  border: none;
  background: #000;
}

/* Mobile-only D-pad for panning once zoomed in. Dragging directly on the
   iframe doesn't pan it — noVNC's own touch handling captures drag
   gestures on the canvas for remote-mouse emulation before they ever
   reach .vnc-scroll's native overflow:auto scrolling, so a plain
   "swipe to pan" never had a chance to work. These buttons sidestep that
   entirely: they're separate DOM elements the iframe never sees, and
   app.js scrolls #vnc-scroll directly via scrollBy() — a real DOM scroll,
   not a touch gesture, so noVNC's touch capture is irrelevant to it.
   display:none by default (desktop); shown by the mobile media query
   below, and only while actually zoomed in (app.js toggles the "active"
   class — nothing to pan at 100%). Absolutely positioned against
   .vnc-scroll (position:relative, set above) so it stays pinned to a
   screen corner regardless of current scroll position. */
.vnc-pan-controls {
  display: none;
  position: absolute;
  right: 8px;
  bottom: 8px;
  grid-template-columns: repeat(3, 32px);
  grid-template-rows: repeat(2, 32px);
  gap: 3px;
  z-index: 5;
}

.vnc-pan-btn {
  width: 100%;
  padding: 0;
  background: rgba(20, 16, 12, 0.72);
  border: 1px solid rgba(201, 162, 74, 0.6);
  color: var(--text);
  font-size: 1rem;
  line-height: 1;
  border-radius: 4px;
}

#pan-up    { grid-column: 2; grid-row: 1; }
#pan-left  { grid-column: 1; grid-row: 2; }
#pan-down  { grid-column: 2; grid-row: 2; }
#pan-right { grid-column: 3; grid-row: 2; }

/* --- Admin panel: below the VNC view, reached by scrolling --- */
#admin-panel {
  max-width: 480px;
  margin: 16px auto 32px;
}

/* --- Mobile: phones in either orientation. Targeted by max-height
   (catches landscape phones, which are wide but short) OR max-width
   (catches portrait phones) — a typical laptop/desktop window is both
   wide AND tall, so it matches neither and this block is a no-op there,
   same as the "computer works great, don't change it" ask.

   The point of this block: the desktop topbar is two full-height rows
   of padding/font-size that cost ~90px. On a landscape phone, total
   viewport height might only be ~360px, so that fixed 90px eats a huge
   share of it and leaves the vnc box starved for height — which is
   exactly why rotating to landscape didn't visibly give a wider game
   view before (the box is aspect-ratio-locked to 8:5, so it's capped by
   whichever of width/height is tightest, and height was the bottleneck).
   Shrinking the topbar here, and shrinking --topbar-budget to match,
   gives that height back to the box. */
@media (max-width: 480px), (max-height: 500px) {
  /* Set on :root, not #topbar: custom properties inherit down the DOM
     tree, and .vnc-scroll (which reads --topbar-budget) is #topbar's
     sibling, not its descendant — scoping the override to #topbar alone
     would silently never reach it. */
  :root {
    --topbar-budget: 56px;
  }

  #topbar {
    padding: 4px 10px;
  }

  #whoami { font-size: 0.75rem; }

  #topbar button {
    padding: 4px 10px;
    font-size: 0.78rem;
  }

  #turn-status {
    margin-top: 2px;
    font-size: 0.75rem;
  }

  #vnc-panel { padding: 4px 8px 0; }

  .vnc-mode-row {
    margin-bottom: 3px;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .vnc-zoom-controls { display: flex; }

  .vnc-pan-controls.active { display: grid; }
}
