/* Shared shell: tokens, top bar, sidebar, form controls, dialogs.
   Tokens are lifted from the LeapEye operator UI (main.css) so this sits in the
   same visual family as space.bbleap.com. Page-specific rules live alongside in
   devices.css / map.css. */

:root {
  --sidebar-width: 146px;
  --sidebar-bg: #008440;
  --sidebar-bg-elevated: #00391c;
  --sidebar-text: #ffffff;
  --sidebar-text-dim: #c8e6d3;
  --sidebar-active-bg: #00391c;
  --sidebar-border: rgba(255, 255, 255, 0.08);

  --content-bg: #313131;
  --surface: #494949;
  --surface-elevated: #595959;
  --surface-border: #494949;
  --content-text: #ffffff;
  --content-text-dim: #acacac;
  --content-text-muted: #6f6f6f;

  --topbar-bg: #212121;
  --input-bg: #212121;
  --input-border: #494949;
  --input-text: #ffffff;

  --accent: #e6007e;
  --success: #008440;
  --success-text: #3ec07a;
  --success-bg: rgba(0, 132, 64, 0.18);
  --warning: #eab308;
  --warning-bg: rgba(234, 179, 8, 0.12);
  --warning-border: rgba(234, 179, 8, 0.35);
  --danger: #ef4444;
  --danger-bg: rgba(239, 68, 68, 0.15);

  --mono: ui-monospace, "SF Mono", Consolas, monospace;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0; padding: 0; height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  background: var(--content-bg);
  color: var(--content-text);
  -webkit-tap-highlight-color: transparent;
  /* Stop iOS inflating body text when a phone is turned to landscape. */
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

/* 100dvh tracks the browser chrome sliding in and out on a phone; the 100vh
   line before it is the fallback for anything that has not got dvh. */
#app-shell { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }
#main-row { flex: 1 1 auto; display: flex; min-height: 0; }

/* --- Top bar --- */

.topbar {
  position: relative; flex: 0 0 auto;
  display: flex; align-items: center; gap: 16px;
  padding: 6px 16px; min-height: 52px;
  background: var(--topbar-bg);
  border-bottom: 1px solid var(--surface-border);
}
.topbar-title {
  display: inline-flex; align-items: center; gap: 9px;
  font-size: 1.4rem; font-weight: 700; letter-spacing: 0.04em; line-height: 1;
  color: inherit; text-decoration: none;
}
/* The skunk is drawn for a light ground, so it carries its own white tile onto
   the dark bar rather than being recoloured. Shared with the login mark. */
.brand-mark {
  flex: 0 0 auto; width: 34px; height: 34px;
  padding: 3px; border-radius: 8px;
  background: #fff; object-fit: contain;
}
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 14px; }
.topbar-meta {
  display: flex; flex-direction: column; align-items: flex-end; gap: 1px;
  font-family: var(--mono); font-size: 0.7rem; line-height: 1.15;
  color: var(--content-text-dim); white-space: nowrap; text-align: right;
}
/* A button that opens the account menu (shell.js); it never signs out by itself. */
.avatar {
  appearance: none; border: 0; padding: 0; cursor: pointer;
  width: 38px; height: 38px; border-radius: 50%;
  background: var(--accent); color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-family: inherit; font-weight: 700; font-size: 0.85rem; letter-spacing: 0.02em;
  text-decoration: none; flex: 0 0 auto;
}
.avatar:hover, .avatar[aria-expanded="true"] { filter: brightness(1.12); }
.avatar:focus-visible { outline: 2px solid var(--success-text); outline-offset: 2px; }

/* Who is signed in, and Sign out. Fixed to the window under the avatar. */
.account-menu {
  position: fixed; z-index: 2200; min-width: 220px; max-width: calc(100vw - 16px);
  padding: 6px; background: var(--surface); border: 1px solid var(--surface-elevated);
  border-radius: 10px; box-shadow: 0 12px 32px rgba(0, 0, 0, 0.5);
}
.account-who { display: flex; flex-direction: column; gap: 2px; padding: 8px 10px 10px; min-width: 0; }
.account-name, .account-email { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.account-name { color: var(--content-text); }
.account-email { font-size: 0.8rem; color: var(--content-text-dim); }
.account-email:empty { display: none; }
.account-role { font-size: 0.75rem; color: var(--success-text); }
.account-role:empty { display: none; }
.account-sep { height: 1px; margin: 0 4px 6px; background: rgba(255, 255, 255, 0.12); }
/* An admin looking through someone else's eyes wears a ring for it. */
.avatar.viewing { box-shadow: 0 0 0 3px var(--topbar-bg), 0 0 0 5px var(--warning); }

/* The bar across the top while an admin views the site as someone else. */
.viewas-bar {
  flex: 0 0 auto; display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 8px 16px; background: #5c4a07; color: #fff4c2;
  border-bottom: 1px solid var(--warning-border); font-size: 0.88rem; line-height: 1.35;
}
.viewas-text { flex: 1 1 320px; }
.viewas-stop {
  flex: 0 0 auto; display: inline-flex; align-items: center; min-height: 34px; padding: 0 16px;
  border-radius: 9999px; background: #fff4c2; color: #3a2e02; font-weight: 700; text-decoration: none;
}
.viewas-stop:hover { background: #fff; }
.account-item {
  display: flex; align-items: center; gap: 10px; min-height: 40px; padding: 0 10px;
  border-radius: 7px; color: var(--content-text); font-weight: 600; text-decoration: none;
}
.account-item svg { flex: 0 0 auto; width: 16px; height: 16px; stroke: currentColor; fill: none; stroke-width: 2; }
.account-item:hover { background: rgba(255, 255, 255, 0.08); }
.account-item:focus-visible { background: rgba(255, 255, 255, 0.08); outline: 2px solid var(--success-text); outline-offset: -2px; }

/* --- Sidebar --- */

#sidebar {
  flex: 0 0 var(--sidebar-width); width: var(--sidebar-width);
  display: flex; flex-direction: column;
  background: var(--sidebar-bg); color: var(--sidebar-text);
  padding: 8px; overflow: hidden;
}
/* Spacing is tuned to the narrow rail: at 146px the label has ~76px to sit in,
   which "Machines" fills at this weight, so it must not wrap. */
.nav-item {
  display: flex; align-items: center; gap: 10px;
  min-height: 52px; padding: 0 12px;
  border: 0; border-radius: 8px;
  background: transparent; color: var(--sidebar-text);
  font: inherit; font-weight: 600; text-align: left; text-decoration: none;
  white-space: nowrap; cursor: pointer; width: 100%;
  transition: background 0.12s ease;
}
.nav-item:hover { background: rgba(0, 57, 28, 0.55); }
.nav-item.active { background: var(--sidebar-active-bg); }
.nav-icon { width: 20px; height: 20px; stroke: currentColor; fill: none; stroke-width: 1.8; flex: 0 0 auto; }

/* --- Content --- */

#content-area { flex: 1 1 auto; overflow: auto; padding: 20px 24px 28px; }

.banner {
  padding: 12px 16px; border-radius: 6px; margin-bottom: 16px;
  font-size: 0.95rem; line-height: 1.4;
  background: var(--warning-bg); color: #fde68a; border: 1px solid var(--warning-border);
}

/* --- Form controls --- */

.controls { display: flex; gap: 10px 12px; flex-wrap: wrap; align-items: center; margin-bottom: 16px; }
.search-input, .control-select, .control-input {
  min-height: 44px; border: 1px solid var(--input-border); border-radius: 8px;
  background: var(--input-bg); color: var(--input-text);
  font: inherit; font-size: 0.95rem; padding: 0 14px;
}
.search-input { flex: 1 1 300px; min-width: 220px; }
.search-input::placeholder, .control-input::placeholder { color: var(--content-text-muted); }
.control-select {
  appearance: none; -webkit-appearance: none; min-width: 180px; cursor: pointer;
  padding: 0 36px 0 14px;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='none' stroke='%23acacac' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'><polyline points='6 9 12 15 18 9'/></svg>");
  background-repeat: no-repeat; background-position: right 12px center;
}
.search-input:focus, .control-select:focus, .control-input:focus {
  outline: none; border-color: var(--success); box-shadow: 0 0 0 2px rgba(0, 132, 64, 0.25);
}
.control-select option { background: var(--input-bg); color: var(--input-text); }
.control-select:disabled, .control-input:disabled { opacity: 0.45; cursor: not-allowed; }

/* --- Searchable picker (picker.js) ---
   A text box over a list: the machine finder, and the filters that stand in a
   row of controls where a dropdown used to. */

.picker { position: relative; }
.controls .picker { flex: 0 1 240px; min-width: 180px; }
.picker-field { position: relative; display: flex; align-items: center; }
.picker-icon {
  position: absolute; left: 13px; width: 16px; height: 16px;
  color: var(--content-text-muted); pointer-events: none;
}
.picker-input {
  width: 100%; min-height: 44px; padding: 0 40px 0 38px;
  border: 1px solid var(--input-border); border-radius: 8px;
  background: var(--input-bg); color: var(--input-text);
  font: inherit; font-size: 0.95rem;
}
.picker-input::placeholder { color: var(--content-text-muted); }
/* In a row of filters the placeholder is the filter's state, "All customers", so
   it reads as the value it is rather than as a hint. */
.controls .picker-input::placeholder { color: var(--input-text); opacity: 1; }
.picker-input:not(:focus) { text-overflow: ellipsis; }
.picker-input:focus {
  outline: none; border-color: var(--success); box-shadow: 0 0 0 2px rgba(0, 132, 64, 0.25);
}
.picker-clear {
  appearance: none; position: absolute; right: 8px;
  width: 26px; height: 26px; border: 0; border-radius: 6px;
  background: transparent; color: var(--content-text-muted);
  font-size: 0.78rem; line-height: 1; cursor: pointer;
}
.picker-clear:hover { background: rgba(255, 255, 255, 0.08); color: var(--content-text); }
/* The chevron says there is a list to open; the cross takes its place once the box
   holds something to empty. */
.picker-chevron {
  position: absolute; right: 13px; width: 14px; height: 14px;
  color: #acacac; pointer-events: none;
}
.picker-filled .picker-chevron { display: none; }

/* Fixed, not absolute: the dialog is a scroll box and would clip a list laid out
   inside it. picker.js owns left/top/width/max-height. */
.picker-list {
  position: fixed; z-index: 2100; overflow-y: auto;
  background: #262626; border: 1px solid var(--input-border); border-radius: 8px;
  box-shadow: 0 14px 36px rgba(0, 0, 0, 0.55);
}
.picker-list::-webkit-scrollbar { width: 8px; }
.picker-list::-webkit-scrollbar-thumb { background: rgba(255, 255, 255, 0.18); border-radius: 8px; }

.picker-head {
  position: sticky; top: 0; z-index: 1;
  padding: 7px 12px 5px; background: #262626;
  font-size: 0.66rem; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase;
  color: var(--content-text-muted);
}
.picker-opt {
  display: flex; align-items: center; gap: 10px;
  padding: 7px 12px; cursor: pointer;
  border-left: 2px solid transparent;
}
.picker-opt.active { background: rgba(0, 132, 64, 0.22); border-left-color: var(--success-text); }
.picker-opt[aria-selected="true"] .picker-title { color: var(--success-text); }
.picker-opt-main { flex: 1 1 auto; min-width: 0; }
.picker-title {
  display: block; font-size: 0.9rem; font-weight: 600; color: var(--content-text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.picker-sub {
  display: block; margin-top: 1px; font-size: 0.76rem; color: var(--content-text-dim);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
/* Matches are tinted rather than highlighter-boxed: on a dark panel a background
   swatch on every hit turns the list into stripes. */
.picker-opt mark { background: none; color: var(--success-text); font-weight: 700; }
.picker-meta {
  flex: 0 0 auto; font-family: var(--mono); font-size: 0.72rem;
  color: var(--content-text-muted); font-variant-numeric: tabular-nums; white-space: nowrap;
}
.picker-opt-extra .picker-title { font-family: var(--mono); }
.picker-opt-all .picker-title { color: var(--content-text-dim); }
.picker-empty { padding: 14px 12px; font-size: 0.82rem; color: var(--content-text-muted); }

.toggle-btn {
  appearance: none; height: 44px; min-height: 44px; padding: 0 20px;
  border-radius: 9999px; border: 1px solid var(--success);
  background: transparent; color: var(--content-text);
  font: inherit; font-size: 0.9rem; font-weight: 600; letter-spacing: 0.03em;
  cursor: pointer; transition: background 0.15s ease, color 0.15s ease;
}
.toggle-btn:hover { background: rgba(0, 132, 64, 0.12); }
.toggle-btn[aria-pressed="true"] { background: var(--success); color: #fff; }
.toggle-btn[aria-pressed="true"]:hover { background: #00a050; }

.primary-btn {
  appearance: none; display: inline-flex; align-items: center; justify-content: center;
  height: 44px; min-height: 44px; padding: 0 22px;
  border: 1px solid var(--success); border-radius: 9999px;
  background: var(--success); color: #fff;
  font: inherit; font-size: 0.9rem; font-weight: 600; line-height: 1;
  cursor: pointer; transition: background 0.12s ease;
}
.primary-btn:hover { background: #00a050; border-color: #00a050; }
.primary-btn:active { background: var(--sidebar-bg-elevated); }
.primary-btn:disabled { opacity: 0.5; cursor: progress; }

.ghost-btn {
  appearance: none; height: 44px; padding: 0 20px;
  border: 1px solid var(--success); border-radius: 9999px;
  background: transparent; color: var(--content-text);
  font: inherit; font-size: 0.9rem; font-weight: 600; cursor: pointer; text-decoration: none;
  display: inline-flex; align-items: center; justify-content: center;
}
.ghost-btn:hover { background: rgba(0, 132, 64, 0.14); }
.ghost-btn:disabled { opacity: 0.45; cursor: not-allowed; }
.ghost-btn:disabled:hover { background: transparent; }
.ghost-btn.quiet { border-color: var(--input-border); color: var(--content-text-dim); }
.ghost-btn.quiet:hover { background: rgba(255, 255, 255, 0.06); }

.count { color: var(--content-text-dim); font-size: 0.85rem; margin: 0 0 10px 2px; }
.empty { padding: 48px 16px; text-align: center; color: var(--content-text-dim); font-style: italic; }

/* --- Tables (the machines page and the list of maps) --- */

.table-wrap { background: var(--topbar-bg); border-radius: 10px; overflow: auto; }
table { width: 100%; border-collapse: collapse; }
thead th {
  position: sticky; top: 0; z-index: 1;
  background: var(--topbar-bg);
  text-align: left; white-space: nowrap;
  padding: 14px 16px;
  font-size: 0.72rem; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase;
  color: var(--content-text-dim);
  border-bottom: 1px solid var(--surface-border);
}
tbody td {
  padding: 12px 16px; vertical-align: middle;
  border-bottom: 1px solid rgba(73, 73, 73, 0.55);
}
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: rgba(73, 73, 73, 0.35); }

/* The pill a row's action takes: a link to somewhere, or a button that acts. */
.link-btn {
  display: inline-flex; align-items: center; justify-content: center;
  height: 36px; padding: 0 16px; margin-left: 6px;
  border: 1px solid var(--success); border-radius: 9999px;
  background: transparent; color: var(--content-text);
  font-size: 0.85rem; font-weight: 600; text-decoration: none; white-space: nowrap;
  transition: background 0.12s ease;
}
.link-btn:hover { background: rgba(0, 132, 64, 0.16); }
.dash { color: var(--content-text-dim); font-size: 0.85rem; }

/* --- Dialogs --- */

.modal-backdrop {
  position: fixed; inset: 0; z-index: 2000; padding: 16px;
  background: rgba(7, 13, 27, 0.72);
  display: flex; align-items: center; justify-content: center;
}
.modal {
  background: var(--surface); border: 1px solid var(--surface-border);
  border-radius: 10px; padding: 22px 24px; width: 100%; max-width: 460px;
  max-height: calc(100vh - 32px); max-height: calc(100dvh - 32px); overflow: auto;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.55);
}
.modal h2 { margin: 0 0 6px; font-size: 1.1rem; font-weight: 600; }
.modal p { margin: 0 0 14px; font-size: 0.9rem; line-height: 1.45; color: var(--content-text-dim); }
.modal ol { margin: 0 0 16px; padding-left: 20px; color: var(--content-text-dim); font-size: 0.9rem; line-height: 1.7; }
.modal ol b { color: var(--content-text); }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; }

/* --- Small shared pieces --- */

.note {
  margin: 0 0 14px; padding: 10px 12px; border-radius: 8px; font-size: 0.85rem; line-height: 1.45;
  background: var(--input-bg); border: 1px solid var(--input-border); color: var(--content-text-dim);
}
.note.warn { background: var(--warning-bg); border-color: var(--warning-border); color: #fde68a; }
.note.bad { background: var(--danger-bg); border-color: var(--danger); color: #ffc9c9; }
.note code { font-family: var(--mono); color: var(--content-text); }

.bar {
  height: 8px; border-radius: 9999px; background: var(--input-bg);
  border: 1px solid var(--surface-border); overflow: hidden; margin-bottom: 14px;
}
.bar > span { display: block; height: 100%; width: 0; background: var(--success); transition: width 0.2s ease; }

/* --- Small screens ---------------------------------------------------------

   The layout above is desktop-first. Three things have to change on a phone,
   and they are the reason for nearly every rule below:

     * The 146px nav rail has nowhere to go, so it lies down along the bottom
       instead - same markup, other axis, thumb-height.
     * iOS Safari zooms the whole page whenever a focused control is smaller
       than 16px, and the operator cannot undo it. Every control is lifted to
       16px rather than trying to suppress the zoom.
     * A dialog centred in the viewport is a stretch away from the thumb, so
       dialogs meet the bottom edge as sheets.                                */

/* Two labels for one control: the long one on a wide screen, the short one when
   the top bar is fighting for room. */
.narrow-only { display: none; }

@media (max-width: 900px) {
  #main-row { flex-direction: column; }
  #content-area {
    order: 1; overscroll-behavior-y: contain;
    padding: 16px max(16px, env(safe-area-inset-right))
             16px max(16px, env(safe-area-inset-left));
  }

  /* The rail, laid down. `order` puts it after the content without moving it in
     the markup, so it stays the second landmark for a screen reader. */
  #sidebar {
    order: 2; flex: 0 0 auto; width: auto;
    flex-direction: row; align-items: stretch; justify-content: center;
    gap: 8px;
    /* As thin as its two buttons. The strip under a home indicator is kept
       clear of them, less the part of it no thumb reaches anyway: the whole
       inset on top of the bar read as a bar twice the height. */
    padding: 2px max(6px, env(safe-area-inset-right))
             calc(2px + max(env(safe-area-inset-bottom) - 12px, 0px)) max(6px, env(safe-area-inset-left));
    border-top: 1px solid var(--sidebar-border);
  }
  .nav-item {
    flex: 0 1 180px; width: auto; min-width: 0;
    flex-direction: column; justify-content: center; gap: 2px;
    min-height: 44px; padding: 4px;
    font-size: 0.68rem; letter-spacing: 0.01em; text-align: center;
  }
  .nav-icon { width: 20px; height: 20px; }

  .topbar {
    gap: 10px; min-height: 50px;
    padding: calc(6px + env(safe-area-inset-top)) max(12px, env(safe-area-inset-right))
             6px max(12px, env(safe-area-inset-left));
  }
  .topbar-title { font-size: 1.15rem; gap: 8px; }
  .brand-mark { width: 29px; height: 29px; padding: 2px; border-radius: 7px; }
  .topbar-right { gap: 10px; min-width: 0; }
  /* Secondary by definition, so it is the part that gives way: it truncates
     before anything else on the bar starts to overlap. */
  .topbar-meta { font-size: 0.62rem; max-width: 32vw; min-width: 0; }
  .topbar-meta span { overflow: hidden; text-overflow: ellipsis; }
  .avatar { width: 34px; height: 34px; font-size: 0.78rem; }
  .account-item { min-height: 44px; }

  /* 16px exactly - see the note at the top of this block. Page stylesheets that
     size a control more specifically repeat this in their own mobile section. */
  .search-input, .control-select, .control-input,
  input[type="text"], input[type="search"], input[type="password"],
  input[type="date"], input[type="time"], input[type="number"],
  select, textarea { font-size: 16px; }
  .search-input { flex: 1 1 100%; min-width: 0; }
  .control-select { flex: 1 1 150px; min-width: 0; }
  .controls .picker { flex: 1 1 150px; min-width: 0; }
  /* Two filters share a phone's width, and the magnifier would cost "All customers"
     its last letters. The box still takes typing; tapping it says as much. */
  .controls .picker-icon { display: none; }
  .controls .picker-input { padding-left: 14px; }
  .picker-input { font-size: 16px; padding-right: 48px; }
  .picker-opt { padding: 10px 12px; min-height: 48px; }
  .picker-clear { width: 34px; height: 34px; }
  .toggle-btn { flex: 1 1 auto; padding: 0 14px; }
  .link-btn { height: 40px; }

  /* Dialogs become sheets on the bottom edge. */
  .modal-backdrop { padding: 0; align-items: flex-end; }
  .modal {
    max-width: none; border-radius: 14px 14px 0 0;
    padding: 18px 16px calc(16px + env(safe-area-inset-bottom));
    max-height: 92vh; max-height: 92dvh;
  }
  .modal-actions > * { flex: 1 1 auto; min-width: 44%; }
}

@media (max-width: 560px) {
  .topbar-meta { display: none; }
  .wide-only { display: none; }
  .narrow-only { display: inline; }
  /* At 320px the top bar is title + button + avatar with ~1px to spare, so the
     pill buttons give back the padding they do not need. */
  .primary-btn, .ghost-btn { padding: 0 16px; }
}

/* A phone on its side has almost no height to spare, so the tab bar gives up
   its labels rather than the map or the table giving up a third of the screen. */
@media (max-width: 900px) and (max-height: 460px) {
  .nav-item { min-height: 36px; font-size: 0; gap: 0; }
  .nav-icon { width: 19px; height: 19px; }
  .topbar { min-height: 44px; }
}
