:root {
  color-scheme: dark;
  --bg: #101a1a;
  --surface: #182524;
  --surface-alt: #20302e;
  --border: #2c3f3c;
  --text: #edf2f0;
  --text-muted: #8ea19c;
  --primary: #62bb46;
  --primary-hover: #77cc5b;
  --primary-ink: #08150a;
  --primary-dim: rgba(98, 187, 70, 0.16);
  --danger: #f05a5c;
  --danger-hover: #ff7476;
  --danger-dim: rgba(240, 90, 92, 0.16);
  --online: #62bb46;
  --offline: #f05a5c;
  --checking: #5c706b;
  --amber: #e0a83e;
  --shadow: 0 2px 10px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }

/* Guarantee [hidden] always actually hides an element, regardless of any
   component's own `display` rule (this project has hit that conflict
   repeatedly — this closes off the whole class of bug for good). */
[hidden] {
  display: none !important;
}

body {
  margin: 0;
  font-family: "Montserrat", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}

.page {
  max-width: 1112px;
  margin: 0 auto;
  padding: 24px 20px 60px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 24px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px 20px;
  box-shadow: var(--shadow);
}

.topbar-title {
  display: flex;
  align-items: center;
  gap: 12px;
}

.topbar-logo {
  width: 40px;
  height: 40px;
  flex-shrink: 0;
}

.topbar h1 {
  font-size: 18px;
  font-weight: 700;
  margin: 0;
}

.topbar-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

.btn {
  display: inline-block;
  border: none;
  border-radius: 8px;
  padding: 9px 16px;
  font-size: 14px;
  font-weight: 600;
  text-decoration: none;
  text-align: center;
  cursor: pointer;
}

.btn-primary {
  background: var(--primary);
  color: var(--primary-ink);
}
.btn-primary:hover { background: var(--primary-hover); }

.btn-secondary {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-secondary:hover { background: var(--border); }

.btn-danger {
  background: transparent;
  color: var(--danger);
  border: 1px solid var(--danger);
}
.btn-danger:hover { background: var(--danger); color: #fff; }

.btn:disabled { opacity: 0.6; cursor: default; }

.btn-icon {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
  font-size: 13px;
}
.btn-icon:hover { background: var(--border); }

.btn-icon-lg {
  font-size: 16px;
  padding: 8px 12px;
  border-radius: 8px;
  line-height: 1;
}

.tabs {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 16px;
}
.tab-btn {
  appearance: none;
  border: none;
  background: transparent;
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}
.tab-btn:hover { color: var(--text); }
.tab-btn.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}
.tab-count {
  color: var(--text-muted);
  font-weight: 400;
}

.tabs-right {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-left: auto;
}

.notification-bell {
  appearance: none;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-muted);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.notification-bell:hover { background: var(--border); }
.notification-bell.has-issues {
  color: var(--offline);
  border-color: var(--offline);
}
.notification-bell.active {
  background: var(--offline);
  color: #fff;
  border-color: var(--offline);
}

.device-table {
  width: 100%;
  table-layout: fixed;
  border-collapse: collapse;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}

.device-table th {
  text-align: left;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

.device-table td {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

.device-table tr:last-child td { border-bottom: none; }

/* This column only ever holds the small drag-handle icon -- the table's
   default 16px/side cell padding alone would exceed its 28px budget and
   force table-layout: fixed to silently widen it (and the whole table)
   past the page container. */
.device-table td.col-drag,
.device-table th.col-drag {
  padding-left: 6px;
  padding-right: 6px;
}
.col-drag { width: 28px; }
.col-status { width: 36px; }
.col-name { width: 300px; }
.col-model { width: 110px; text-align: center; }
.col-ip { width: 140px; }
.col-location { width: 158px; }
/* Wide enough for all 4 action buttons on one line even at the Montserrat
   theme's slightly wider letterforms -- table-layout: fixed means this is
   a hard budget, not a hint, so it has to fit them for real. */
.col-actions { width: 300px; }

/* "Other" devices have no encoder model -- hide the column/label entirely
   on that tab instead of showing an empty header over blank cells. */
.device-table.hide-model .col-model,
.device-table.hide-model td.device-model {
  display: none;
}

.actions-inner {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: nowrap;
}

.th-label {
  display: inline-flex;
  align-items: center;
  gap: 2px;
}

.sort-arrow {
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 9px;
  padding: 2px;
  line-height: 1;
}
.sort-arrow:hover { color: var(--text); }
.sort-arrow.active { color: var(--primary); }

.drag-handle {
  cursor: grab;
  width: 14px;
  height: 14px;
  display: inline-block;
  position: relative;
}
.drag-handle::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 3px;
  height: 3px;
  border-radius: 50%;
  background: var(--text-muted);
  box-shadow:
    7px 0 0 var(--text-muted),
    0 5px 0 var(--text-muted),
    7px 5px 0 var(--text-muted),
    0 10px 0 var(--text-muted),
    7px 10px 0 var(--text-muted);
}
.drag-handle.disabled {
  cursor: default;
  opacity: 0.35;
}

.device-table tr.dragging { opacity: 0.4; }
.device-table tr.drag-over { box-shadow: inset 0 2px 0 var(--primary); }

.status-stack {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
}

.status-dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--checking);
}
.status-dot.online { background: var(--online); }
.status-dot.offline { background: var(--offline); }

@keyframes live-flash {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.35; }
}

.live-badge {
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.03em;
}
.live-badge.is-active {
  color: var(--offline);
  animation: live-flash 1.8s ease-in-out infinite;
}
.live-badge.is-inactive {
  color: var(--text-muted);
  opacity: 0.5;
}
@media (prefers-reduced-motion: reduce) {
  .live-badge.is-active { animation: none; }
}

a.live-badge-link {
  text-decoration: none;
  cursor: pointer;
}
a.live-badge-link.is-previewing .live-badge {
  outline: 1px solid currentColor;
  outline-offset: 2px;
  border-radius: 2px;
}

/* Inline stream preview, slotted in as its own row right under the
   device's action buttons. Left-aligned on desktop and sized like the
   mobile card's full width, so it's a modest 16:9 player, not a huge one. */
.device-table tr.has-live-preview td { border-bottom: none; }
.device-table tr.live-preview-row td {
  padding: 0 16px 14px;
}
.live-preview {
  width: min(100%, 480px);
}
.live-preview-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 6px;
}
.live-preview-title {
  flex: 1;
  min-width: 0;
  font-size: 12px;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.live-preview-open {
  font-size: 12px;
  white-space: nowrap;
}
.live-preview-frame {
  position: relative;
  aspect-ratio: 16 / 9;
  background: #000;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.live-preview-frame iframe {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: 0;
}

.device-name-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 8px;
  min-width: 0;
}

.device-status-row {
  display: flex;
  align-items: baseline;
  gap: 16px;
  margin-top: 2px;
}

.ems-indicator {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  font-weight: 400;
  color: var(--text-muted);
  vertical-align: middle;
  flex-shrink: 0;
}

.ems-label {
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.ems-status-text {
  color: var(--text);
  font-weight: 600;
}

.ems-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--checking);
}
.ems-dot.ems-connected { background: var(--online); }
.ems-dot.ems-connecting { background: var(--amber); }
.ems-dot.ems-not-connected { background: var(--offline); }

.device-name-text {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

.device-uptime,
.device-firmware {
  font-size: 11px;
  color: var(--text-muted);
  font-family: ui-monospace, SFMono-Regular, monospace;
  white-space: nowrap;
}

.device-firmware { margin-top: 2px; }

.device-model {
  text-align: center;
}

.device-model-text {
  white-space: nowrap;
}

.ping-pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-top: 4px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 11px;
  font-weight: 400;
  color: var(--text-muted);
  font-family: ui-monospace, SFMono-Regular, monospace;
  white-space: nowrap;
  /* The Name column is now a fixed width (table-layout: fixed) at every
     screen size, not just mobile -- let this scroll in place instead of
     forcing the column (and the whole table) wider than its budget. */
  max-width: 100%;
  overflow-x: auto;
}

.ping-pill-value {
  color: var(--text);
  font-weight: 700;
}

.device-ip a {
  color: var(--primary);
  text-decoration: none;
  font-family: ui-monospace, SFMono-Regular, monospace;
}
.device-ip a:hover { text-decoration: underline; }

.device-description {
  color: var(--text);
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.empty-state {
  text-align: center;
  color: var(--text-muted);
  padding: 40px 0;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal {
  position: relative;
  background: var(--surface);
  border-radius: 12px;
  padding: 24px;
  width: 100%;
  max-width: 420px;
  max-height: calc(100vh - 32px);
  overflow-y: auto;
  border: 1px solid var(--border);
}

/* A top-anchored escape hatch for the longer panels (Settings' several
   forms, Presets' device list) -- on mobile, the bottom Close/Save buttons
   can be a long scroll away. */
.modal-close-x {
  position: absolute;
  top: 16px;
  right: 16px;
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  padding: 6px;
  border-radius: 6px;
}
.modal-close-x:hover { background: var(--border); color: var(--text); }

.modal-wide {
  max-width: 560px;
}

/* Settings: a wide panel of section cards laid out two across, instead of
   one long 420px column of eight stacked forms. Collapses back to a single
   column on narrow screens (see the max-width: 860px block below). */
.modal-settings {
  max-width: 1080px;
}
.settings-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
  align-items: start;
}
.settings-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px 18px;
  min-width: 0;
}
.settings-card-wide {
  grid-column: 1 / -1;
}
.settings-stack {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-width: 0;
}
/* Cards sit on --bg, so their inputs step back to --surface to stay distinct. */
.settings-card input[type="text"],
.settings-card input[type="email"],
.settings-card input[type="number"],
.settings-card input[type="time"],
.settings-card input[type="password"],
.settings-card select {
  background: var(--surface);
}
.settings-card .modal-section-title + .modal-subsection-title {
  margin-top: 0;
}
.settings-card .modal-actions {
  margin-top: 12px;
}
.settings-two-col {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  column-gap: 28px;
}
.settings-two-col-span {
  grid-column: 1 / -1;
}
.settings-two-col > div > .modal-subsection-title:first-child {
  margin-top: 0;
}
/* Pairs short fields (frequency + day, host + port, ...) side by side. */
.field-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  column-gap: 12px;
  /* Bottom-align so a label that wraps to two lines doesn't knock its
     input out of line with its neighbors'. */
  align-items: end;
}
.field-grid-3 {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}
.modal-settings > .modal-actions {
  margin-top: 16px;
}
/* Add/Edit Device uses the same card grid as Settings, inside one form.
   The encoder-only cards sit in a wrapper so they can hide together for
   "Other" devices; display: contents keeps them laid out as grid items. */
.settings-grid-group {
  display: contents;
}
.modal-device .modal-actions-footer {
  margin-top: 16px;
}
/* "Other" devices only have the Device and Email cards, so drop to a
   narrower single column instead of a half-empty two-column layout. */
.modal-device:has(#device-ssh-section[hidden]) {
  max-width: 640px;
}
.modal-device:has(#device-ssh-section[hidden]) .settings-grid {
  grid-template-columns: minmax(0, 1fr);
}

@media (max-width: 860px) {
  .settings-grid,
  .settings-two-col {
    grid-template-columns: minmax(0, 1fr);
  }
}
/* On phones the device form's pairs (Name/IP, Sardius ids, ...) get a full
   row each; the ids and IP addresses are too long to share a row. */
@media (max-width: 480px) {
  .modal-device .field-grid {
    grid-template-columns: minmax(0, 1fr);
  }
  /* Boxed cards eat a phone's width and height, so flatten them back into
     divider-separated sections like the old single-column form. */
  .modal-device .settings-grid {
    gap: 0;
  }
  .modal-device .settings-card {
    background: none;
    border: none;
    border-top: 1px solid var(--border);
    border-radius: 0;
    padding: 16px 0;
  }
  .modal-device .settings-grid > .settings-card:first-child {
    border-top: none;
    padding-top: 0;
  }
}
/* Short fields stay paired on phones; only the subject templates (long
   text) need a full-width column each. */
@media (max-width: 480px) {
  .settings-two-col .field-grid {
    grid-template-columns: minmax(0, 1fr);
  }
}
@media (max-width: 860px) {
  .settings-two-col > div + div > .modal-subsection-title:first-child {
    margin-top: 20px;
  }
}

.modal h2 {
  margin-top: 0;
  font-size: 18px;
}

.modal label {
  display: block;
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 14px;
}

.modal input[type="text"],
.modal input[type="email"],
.modal input[type="number"],
.modal input[type="time"],
.modal input[type="password"],
.modal select {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 9px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
}
.checkbox-label input[type="checkbox"] {
  width: 18px;
  height: 18px;
}

.modal-section-title {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  margin: 0 0 14px;
}

.modal-divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 20px 0;
}

.settings-backup-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
  align-items: center;
}

.test-result {
  font-size: 13px;
  font-weight: 600;
}
.test-result.success { color: var(--online); }
.test-result.error { color: var(--offline); }

.modal-hint {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: -6px;
}

/* For a lighter-weight section break within one form/section -- e.g. a
   "manual override" sub-area after a button row -- where a full
   .modal-section-title + .modal-divider would be too heavy. Needs its own
   top margin (unlike .modal-hint's -6px, meant to pull a hint up snug
   under the field it describes) since it usually follows a .modal-actions
   row, not a field. */
.modal-subsection-title {
  font-size: 12px;
  color: var(--text-muted);
  margin: 20px 0 12px;
}
.modal-subsection-title strong {
  color: var(--text);
}
.modal-hint.warning {
  color: var(--offline);
  font-weight: 600;
}

.modal-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 8px;
}

.restart-option-actions {
  justify-content: flex-start;
  margin-bottom: 4px;
}

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

.restart-ems-indicator {
  font-size: 14px;
}
.restart-ems-indicator .ems-dot {
  width: 10px;
  height: 10px;
}
.restart-ems-indicator .ems-status-text {
  font-weight: 700;
}

.log-search {
  display: block;
  width: 100%;
  margin-bottom: 12px;
  padding: 9px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

.log-list {
  max-height: 60vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 16px;
}

.log-entry {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
}

.log-entry-error {
  border-color: var(--danger);
}

.log-time {
  font-size: 11px;
  color: var(--text-muted);
}

.log-text {
  font-size: 14px;
}

.log-entry-error .log-text {
  color: var(--danger);
}

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 14px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.2);
  max-width: calc(100vw - 32px);
  text-align: center;
}

.toast.error { background: var(--danger); color: #fff; }

.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 32px;
  width: 100%;
  max-width: 360px;
}

.login-logo {
  display: block;
  width: 72px;
  height: 72px;
  margin: 0 auto 16px;
}

.login-card h1 {
  font-size: 18px;
  margin: 0 0 20px;
  text-align: center;
}

.login-card label {
  display: block;
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 14px;
}

.login-card input {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 9px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

.login-card .btn {
  width: 100%;
  margin-top: 4px;
}

.login-error {
  color: var(--danger);
  font-size: 13px;
  margin: 12px 0 0;
  text-align: center;
}

.current-user-label {
  margin: 2px 0 0;
  font-size: 12px;
  color: var(--text-muted);
}

.role-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.role-badge.admin {
  background: var(--primary-dim);
  color: var(--primary);
}

.role-badge.user {
  background: var(--border);
  color: var(--text-muted);
}

.role-badge.disabled {
  background: var(--danger-dim);
  color: var(--danger);
}

.user-row:has(.role-badge.disabled) {
  opacity: 0.6;
}

.users-list {
  max-height: 50vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 16px;
}

.user-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  flex-wrap: wrap;
}

.user-row-info {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.user-row-name {
  font-weight: 600;
}

.user-row-access {
  font-size: 12px;
  color: var(--text-muted);
}

.user-row-actions {
  display: flex;
  gap: 6px;
}

.user-device-checkboxes {
  max-height: 220px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  margin-bottom: 14px;
}

.user-device-checkbox-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 0;
  font-size: 14px;
  color: var(--text);
}

.user-device-checkbox-row input {
  width: 16px;
  height: 16px;
}

.modal-actions-top {
  display: flex;
  gap: 8px;
  margin-bottom: 14px;
}

.help-docs-upload {
  padding: 14px;
  margin-bottom: 16px;
  border: 1px dashed var(--border);
  border-radius: 10px;
  background: var(--bg);
}

.help-docs-upload-title {
  margin: 0 0 10px;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}

#help-doc-title-input {
  width: 100%;
  margin: 0 0 10px;
  padding: 9px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
}

.help-docs-upload-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.help-doc-file-label {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 9px 14px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
}
.help-doc-file-label:hover { background: var(--border); }
.help-doc-file-label input[type="file"] {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
}

.help-doc-file-name {
  flex: 1 1 140px;
  min-width: 0;
  font-size: 13px;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.help-docs-upload-row .btn {
  flex-shrink: 0;
  margin-left: auto;
}

.help-docs-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 55vh;
  overflow-y: auto;
}

.help-doc-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
}
.help-doc-row.dragging { opacity: 0.4; }
.help-doc-row.drag-over { box-shadow: inset 0 2px 0 var(--primary); }

.help-doc-drag-handle {
  flex-shrink: 0;
}

.help-doc-icon {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 8px;
  background: var(--primary-dim);
  color: var(--primary);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.help-doc-info {
  flex: 1 1 auto;
  min-width: 0;
}

.help-doc-title {
  font-weight: 600;
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.help-doc-meta {
  margin-top: 2px;
  font-size: 12px;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.help-doc-actions {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}
.help-doc-action-btn {
  width: 30px;
  height: 30px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  background: var(--surface);
}

/* A bit wider than .modal-wide's default 560px -- each row packs a drag
   handle, icon, title/meta, and up to 4 action buttons, so the title
   column needs the extra room to avoid truncating even short titles. */
#help-docs-modal .modal {
  max-width: 640px;
}

@media (max-width: 680px) {
  .help-doc-row {
    flex-wrap: wrap;
  }
  .help-doc-info {
    flex-basis: 100%;
    order: 1;
  }
  .help-doc-actions {
    order: 2;
    flex-wrap: wrap;
  }
}

.presets-list {
  max-height: 55vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 16px;
}

.preset-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  flex-wrap: wrap;
}

.preset-row-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.preset-row-name-line {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.preset-row-name {
  font-weight: 600;
}

.preset-row-location {
  font-size: 12px;
  color: var(--text-muted);
}

.preset-row-meta {
  font-size: 12px;
  color: var(--text-muted);
}

.preset-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}

.preset-badge.fresh {
  background: rgba(34, 197, 94, 0.15);
  color: var(--online);
}

.preset-badge.stale {
  background: rgba(245, 158, 11, 0.18);
  color: var(--amber);
}

.preset-badge.checking {
  background: var(--bg);
  color: var(--text-muted);
  border: 1px solid var(--border);
}

.preset-row-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.restore-source-tabs {
  display: flex;
  gap: 16px;
  margin-bottom: 12px;
}

.preset-row-checkbox {
  width: 16px;
  height: 16px;
  margin-right: 4px;
}

/* Overrides .preset-row's space-between so checkbox / download button /
   timestamp sit tightly together left-aligned, instead of the button
   floating off to the far right with an empty gap before it. */
.preset-row-compact {
  justify-content: flex-start;
  gap: 12px;
}

/* .btn-icon's own color/border would otherwise win the cascade over
   .btn-danger's (it's declared later in this file) and silently turn a
   "dangerous action" button into a plain grey one. */
.btn-icon.btn-danger {
  color: var(--danger);
  border-color: var(--danger);
}
.btn-icon.btn-danger:hover {
  background: var(--danger);
  color: #fff;
}

@media (max-width: 680px) {
  .page {
    padding: 16px 12px 48px;
  }

  .topbar {
    flex-direction: column;
    align-items: stretch;
  }

  .topbar h1 {
    font-size: 19px;
  }

  .topbar-actions {
    justify-content: stretch;
  }

  .topbar-actions .btn {
    flex: 1;
  }

  .device-table thead {
    display: none;
  }

  .device-table, .device-table tbody, .device-table tr, .device-table td {
    display: block;
    width: 100%;
  }

  .device-table {
    border: none;
    background: transparent;
    border-radius: 0;
  }

  /* Grid (not flex-wrap) so model/firmware can be pinned to their own
     top-right column regardless of how wide the name/ping content next to
     them is -- flex-wrap kept shoving that column onto its own full-width
     row since the ping pill made the name column too wide to share a line. */
  .device-table tr {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 10px 8px;
    margin-bottom: 10px;
    display: grid;
    /* Fixed, not auto -- each row is its own independent grid, so an
       auto-sized column resized itself per row based on that row's own
       Model/IP text length. A device with a one-digit-longer IP squeezed
       the shared name column just enough to wrap its EMS badge while
       identical neighboring rows with shorter IPs didn't. 92px (paired
       with the smaller font on .device-ip below) fits every real IP up
       to the maximum possible IPv4 address length without truncating,
       without taking any width from the name column -- it's funded by
       trimming this row's own horizontal padding by the same amount. */
    grid-template-columns: auto 1fr 92px;
    column-gap: 6px;
    align-items: start;
  }

  .device-table td {
    padding: 3px 0;
    border-bottom: none;
  }

  .device-table td[data-label]::before {
    content: attr(data-label) ": ";
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-muted);
  }

  .device-table td.col-drag {
    display: none;
  }

  .device-table td.col-status {
    grid-column: 1;
    grid-row: 1;
  }

  .device-table td.device-name {
    grid-column: 2;
    grid-row: 1 / span 2;
    /* Grid items default to a content-based min-width, which combined with
       the nowrap ping-pill/uptime text refuses to shrink -- that pushed the
       model/ip column right off the edge of the card. */
    min-width: 0;
    padding-left: 6px;
    font-size: 16px;
  }

  .device-table td.device-model {
    grid-column: 3;
    grid-row: 1;
    text-align: right;
    overflow: hidden;
  }

  .device-table td.device-model .device-model-text,
  .device-table td.device-model .device-firmware {
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .device-table td.device-ip {
    grid-column: 3;
    grid-row: 2;
    text-align: right;
    overflow: hidden;
    /* Drop the "IP:" label -- it was just extra width to spare. The
       column itself is now a fixed 92px (see .device-table tr above), so
       this can't grow to fit a longer address like the old auto column
       did; paired with this font size, it fits every valid IPv4 address
       (up to 15 characters) without truncating. */
    font-size: 10px;
  }

  .device-table td.device-ip a {
    display: inline-block;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .device-table td.device-ip::before {
    content: none;
  }

  .device-table td.device-description,
  .device-table td.col-actions {
    grid-column: 1 / -1;
  }

  .device-table td.col-actions {
    padding-top: 10px;
  }

  .device-table td.col-actions .actions-inner {
    flex-wrap: wrap;
  }

  .device-table td.col-actions .btn,
  .device-table td.col-actions .btn-icon {
    margin-left: 0;
    flex: 1;
    text-align: center;
    padding: 10px 12px;
  }

  /* Continue the device's card into the preview below it, so the player
     reads as part of that device rather than a separate card. */
  .device-table tr.has-live-preview {
    margin-bottom: 0;
    border-bottom: none;
    border-radius: 10px 10px 0 0;
  }
  .device-table tr.live-preview-row {
    display: block;
    padding: 0 8px 10px;
    border-top: none;
    border-radius: 0 0 10px 10px;
  }
  .device-table tr.live-preview-row td {
    padding: 0;
  }
  .live-preview {
    width: 100%;
  }

  .modal {
    padding: 20px;
  }
}
