/* Web UI styles: colour tokens first, then page, card, keypad, dialog, small screens. */
:root {
  --bg: #eef3f7;
  --surface: #ffffff;
  --surface-2: #f4f8fb;
  --border: #d5e1ea;
  --text: #13293d;
  --muted: #5b7083;
  --accent: #0b7a99;
  --accent-text: #ffffff;
  --danger: #b42318;
  --danger-soft: rgb(180 35 24 / 0.12);
  --success: #157f3b;
  --radius: 12px;
  --gap: 16px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1720;
    --surface: #17212c;
    --surface-2: #1d2a37;
    --border: #2b3a4a;
    --text: #e6eef5;
    --muted: #9db0c2;
    --accent: #38b2d0;
    --accent-text: #0f1720;
    --danger: #f97066;
    --danger-soft: rgb(249 112 102 / 0.14);
    --success: #4ade80;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px var(--gap);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.nav-link, .back-link { color: var(--accent); font-weight: 600; text-decoration: none; }
.farm-line { display: flex; align-items: center; gap: 6px; margin: 0; padding: 6px 14px; color: var(--success); font-size: 14px; font-weight: 600; }
.farm-line[hidden] { display: none; }
.farm-line svg { flex: none; width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.card .farm-line { border-bottom: 1px solid var(--border); }
.kc-line { color: var(--text); font-weight: 500; } /* a fact, not good news: not the farm's green */
.upgrade-line { flex-wrap: wrap; color: var(--text); font-weight: 500; }
.upgrade-text { flex: 1 1 220px; }
/* The list grows into the free width and shrinks below its longest name, so a long item name never
   widens the page on a phone (it is cut with an ellipsis instead). */
.upgrade-item-label { display: inline-flex; flex: 1 1 200px; align-items: center; gap: 6px; min-width: 0; }
.upgrade-item {
  width: 100%;
  min-height: 36px;
  min-width: 0;
  text-overflow: ellipsis;
  padding: 0 8px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
  font-size: 16px; /* below 16px iOS zooms the page on focus */
  font-weight: 500;
}
.upgrade-item:disabled { opacity: 0.55; cursor: not-allowed; }
.upgrade-btn { min-height: 36px; padding: 0 14px; font-weight: 600; }
/* The pair line sits on its own above the machine grid, framed like a card. */
.pair-line { flex-wrap: wrap; margin: 0 0 var(--gap); color: var(--text); font-weight: 500; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.pair-text { flex: 1 1 260px; }
.pair-buttons { display: flex; flex-wrap: wrap; gap: 8px; }
.pair-btn { min-height: 36px; font-weight: 600; }
.pair-btn:disabled { opacity: 0.55; cursor: not-allowed; }
.upgrade-btn:disabled { opacity: 0.55; cursor: not-allowed; }
.brand { font-size: 18px; font-weight: 700; }
.page { max-width: 1280px; margin: 0 auto; padding: var(--gap); }
.page-head { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; margin: 0 0 var(--gap); }
.page-title { margin: 0; font-size: 22px; }
.machine-count { color: var(--muted); font-variant-numeric: tabular-nums; }
.add-machine { display: inline-flex; align-items: center; gap: 6px; min-height: 40px; margin-left: auto; font-weight: 600; }
.add-machine[hidden] { display: none; }
.add-machine:disabled { opacity: 0.55; cursor: not-allowed; }
.add-machine svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2.5; stroke-linecap: round; }
.empty-state { margin: 32px 0; color: var(--muted); text-align: center; }
.notice { margin: 0 0 var(--gap); padding: 10px 14px; border-radius: 8px; background: var(--danger-soft); color: var(--danger); }
.hint { margin: var(--gap) 0 0; color: var(--muted); font-size: 13px; text-align: center; }

.machine-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); /* room for a 1:1 game screen + side buttons */
  gap: var(--gap);
  align-items: start;
}

.card { overflow: hidden; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
}
.card-name { margin: 0; font-size: 16px; }
.card-clock { color: var(--muted); font-weight: 600; font-variant-numeric: tabular-nums; }
.card-body { display: flex; justify-content: center; gap: 8px; padding: 12px 12px 10px; }

.screen-wrap { position: relative; flex: none; width: 240px; height: 320px; overflow: hidden; background: #000; border-radius: 6px; }
.screen { width: 100%; height: 100%; }
/* noVNC's own screen div scrolls by default; its scrollbar would make it shrink the game to fit. */
.screen > div { overflow: hidden !important; }
.screen-status {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  margin: 0;
  padding: 16px;
  background: rgb(0 0 0 / 0.6);
  color: #fff;
  font-weight: 600;
  text-align: center;
}
.screen-status[hidden] { display: none; } /* the display rule above would otherwise override [hidden] */
/* Screen switched off: an opaque cover (a button, so a tap or Enter turns the screen back on). */
.screen-off {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 16px;
  background: #000;
  border: 0;
  border-radius: 0;
  color: #fff;
  font: inherit;
  font-weight: 600;
  text-align: center;
  cursor: pointer;
}
.screen-off[hidden] { display: none; }
.screen-off:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.card.is-faulted .screen-wrap { outline: 2px solid var(--danger); }

.side-actions { display: flex; flex-direction: column; gap: 8px; }
.icon-btn {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--accent);
  cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }
/* Destructive action sits apart from the others, at the bottom of the column. */
.icon-btn--danger { margin-top: auto; color: var(--danger); }
.card.is-removing { opacity: 0.5; pointer-events: none; }
.icon-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.keypad {
  display: grid;
  grid-template-columns: 64px 40px 40px 40px 64px;
  grid-template-areas:
    "lsoft . up . rsoft"
    ". left ok right clear"
    "star . down . hash";
  justify-content: center;
  gap: 6px;
  padding: 0 12px 14px;
}
.key {
  height: 40px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
}
.key.is-pressed { background: var(--surface-2); transform: translateY(1px); }
.key:focus-visible, .icon-btn:focus-visible, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.text-dialog {
  width: min(420px, calc(100vw - 32px));
  padding: 20px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
}
.text-dialog::backdrop { background: rgb(0 0 0 / 0.45); }
.dialog-title { margin: 0 0 12px; font-size: 17px; }
.text-dialog input[type="text"] {
  width: 100%;
  padding: 10px 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 16px; /* below 16px iOS zooms the page on focus */
}
.check { display: flex; align-items: center; gap: 8px; margin: 12px 0 16px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
.btn { padding: 8px 16px; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; color: var(--text); font: inherit; cursor: pointer; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

.screen canvas { image-rendering: pixelated; } /* scaled-up pixel art stays sharp */

/* Safe area insets & mobile app-like styling */
body {
  padding-bottom: max(0px, env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
}
.topbar {
  padding-top: max(12px, env(safe-area-inset-top));
  padding-left: max(var(--gap), env(safe-area-inset-left));
  padding-right: max(var(--gap), env(safe-area-inset-right));
}
.topbar-nav {
  display: flex;
  align-items: center;
  gap: 14px;
}
.logout-btn {
  color: var(--muted);
  font-size: 13px;
  text-decoration: none;
  padding: 4px 8px;
  border-radius: 6px;
  background: transparent;
  border: 1px solid var(--border);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
}
.logout-btn:hover {
  color: var(--danger);
  border-color: var(--danger);
  background: var(--danger-soft);
}

/* Authentication / Login Page */
.auth-page {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--gap);
  padding-top: max(var(--gap), env(safe-area-inset-top));
  padding-bottom: max(var(--gap), env(safe-area-inset-bottom));
}
.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px 24px;
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.08);
}
.auth-header {
  text-align: center;
  margin-bottom: 24px;
}
.auth-logo {
  width: 64px;
  height: 64px;
  margin: 0 auto 12px;
  border-radius: 16px;
  display: block;
  box-shadow: 0 4px 12px rgb(0 0 0 / 0.15);
}
.auth-title {
  margin: 0 0 6px;
  font-size: 22px;
  font-weight: 700;
}
.auth-subtitle {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
}
.auth-error {
  margin: 0 0 16px;
  padding: 10px 12px;
  background: var(--danger-soft);
  color: var(--danger);
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  text-align: center;
}
.form-group {
  margin-bottom: 16px;
}
.form-label {
  display: block;
  margin-bottom: 6px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
}
.form-input {
  width: 100%;
  height: 44px;
  padding: 0 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 16px;
  box-sizing: border-box;
  outline: none;
  transition: border-color 0.15s ease;
}
.form-input:focus {
  border-color: var(--accent);
}
.auth-check {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 16px 0 20px;
  font-size: 14px;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
}
.auth-check input {
  width: 18px;
  height: 18px;
  cursor: pointer;
  accent-color: var(--accent);
}
.auth-submit {
  width: 100%;
  height: 44px;
  font-weight: 600;
  font-size: 15px;
}

@media (max-width: 768px) {
  .keypad { grid-template-columns: 1.5fr 1fr 1fr 1fr 1.5fr; width: 100%; max-width: 360px; margin: 0 auto; }
  .key { height: 44px; -webkit-touch-callout: none; -webkit-user-select: none; user-select: none; }
}

@media (max-width: 480px) {
  .page { padding: 12px; }
  .machine-grid { grid-template-columns: 1fr; }
  .card-body { padding: 10px; }
  /* Phones: the game grows to the card width (keeping 3:4) next to the side buttons. */
  .card .screen-wrap { width: min(360px, calc(100vw - 94px)); height: auto; aspect-ratio: 3 / 4; }
}
