* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: #0e1116; color: #e6edf3;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

header {
  position: sticky; top: 0; z-index: 10;
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 20px; background: #161b22; border-bottom: 1px solid #30363d;
}
header h1 { margin: 0; font-size: 18px; letter-spacing: .5px; }
.meta { display: flex; gap: 12px; align-items: center; color: #8b949e; font-size: 13px; }
/* On desktop the wrapper is invisible to layout — buttons flow in .meta as before */
.header-actions { display: contents; }
#conn-status.on { color: #3fb950; }
#conn-status.off { color: #f85149; }
.badge {
  font-size: 11px; padding: 3px 8px; border-radius: 10px;
  border: 1px solid #30363d;
}
.badge.on { color: #3fb950; border-color: #238636; background: rgba(63,185,80,.1); }
.badge.off { color: #8b949e; }
#bulk-rec-btn.recording { background: #da3633; border-color: #da3633; }
button {
  background: #21262d; color: #e6edf3; border: 1px solid #30363d;
  padding: 6px 12px; border-radius: 6px; cursor: pointer; font-size: 13px;
}
button:hover { background: #30363d; }

/* Video-wall: --cols and grid-template-rows/height are set by JS from the
   "Screens/view" count, so the visible tiles fill the screen (fewer = bigger). */
main#grid {
  display: grid;
  grid-template-columns: repeat(var(--cols, 4), 1fr);
  grid-auto-flow: row dense;   /* a drag-enlarged tile spans cells; others fill the gaps */
  gap: 10px;
  padding: 12px;
}

/* Pagination bar */
nav#pager {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-wrap: wrap;
  padding: 10px 16px;
  background: #161b22;
  border-bottom: 1px solid #30363d;
  position: sticky;
  top: 53px;
  z-index: 9;
}
nav#pager button {
  min-width: 36px; min-height: 36px;
  padding: 6px 12px; font-size: 14px;
}
nav#pager button.active {
  background: #1f6feb; border-color: #1f6feb; font-weight: 600;
}
nav#pager button:disabled { opacity: .4; cursor: not-allowed; }
nav#pager .page-info { margin-left: auto; color: #8b949e; font-size: 12px; }
nav#pager .page-ellipsis { color: #6e7681; padding: 0 4px; }

/* ============================================================ */
/*  DESKTOP CARD (default — used on wide screens)                */
/* ============================================================ */
.card {
  position: relative;
  background: #0d1117;
  border: 1px solid #30363d;
  border-radius: 10px;
  overflow: hidden;
  min-width: 0; min-height: 0;          /* let tiles shrink to fill wall cells */
  grid-column: span var(--cspan, 1);    /* drag-resize sets these */
  grid-row: span var(--rspan, 1);
}
/* Resize control — click to cycle tile size. A clear corner icon pill, tucked
   into the corner with a small inset so it doesn't crowd the action buttons. */
.card .resize-handle {
  position: absolute; right: 8px; bottom: 8px; width: 26px; height: 26px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; z-index: 20; opacity: .5; transition: opacity .15s, background .15s;
  border-radius: 6px; background: rgba(0,0,0,.55); color: #fff; font-size: 14px; line-height: 1;
}
.card .resize-handle::after { content: "⇲"; }   /* resize-to-corner glyph */
.card:hover .resize-handle { opacity: 1; }
.card .resize-handle:hover { opacity: 1; background: rgba(31,111,235,.75); }
.card .thumb {
  width: 100%; height: 100%; object-fit: cover; display: block;   /* fills, no black bars */
  background: #000;
}
.card .overlay {
  position: absolute; left: 0; right: 0; bottom: 0;
  display: flex; justify-content: space-between; align-items: flex-end;
  padding: 8px 10px;
  background: linear-gradient(to top, rgba(0,0,0,.85), transparent);
}
.card .label strong { display: block; font-size: 14px; }
.card .label .user { font-size: 12px; color: #8b949e; }
/* margin-right leaves a gap so the last icon (zoom) clears the resize dragger */
.card .actions { display: flex; gap: 4px; opacity: 0; transition: opacity .15s; margin-right: 30px; }
.card:hover .actions { opacity: 1; }
.card .actions button {
  width: 32px; height: 32px; padding: 0; font-size: 14px; line-height: 1;
}
.card .status {
  position: absolute; top: 8px; right: 8px;
  font-size: 11px; padding: 3px 7px; border-radius: 12px;
  background: rgba(0,0,0,.6);
}
.card.recording .status { background: #da3633; }
.card.hd .status { background: #1f6feb; }
.card.stale { opacity: .45; }
.card .label .version {
  display: inline-block; font-size: 10px; color: #8b949e;
  margin-top: 2px; padding: 1px 6px; border-radius: 8px;
  background: rgba(255,255,255,.08);
}
.card .label .version.outdated {
  color: #fff; background: #d29922; font-weight: 600;
}
.card.outdated { outline: 2px solid #d29922; }

/* Offline tile (desktop) — keep visible, dim thumbnail, big chip and last-seen text */
.card.offline { background: #0d1117; }
.card.offline .thumb { opacity: .15; filter: grayscale(1); }
.card.offline .status {
  background: #6e7681; color: #fff; font-weight: 600;
}
.card.offline .actions button[data-action="record"],
.card.offline .actions button[data-action="screenshot"] { display: none; }
.card .offline-banner { display: none; }
.card.offline .offline-banner {
  display: flex; flex-direction: column;
  position: absolute; top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  text-align: center; pointer-events: none;
  background: rgba(110, 118, 129, .85);
  padding: 8px 18px; border-radius: 8px;
  color: #fff;
}
.card.offline .offline-banner .big { font-size: 16px; font-weight: 700; letter-spacing: 1px; }
.card.offline .offline-banner .sub { font-size: 11px; color: #d0d7de; margin-top: 2px; }

/* Archived / hidden tile — the PC has been told to stop sharing its screen */
.card .archived-banner { display: none; }
.card.archived { background: #0d1117; outline: 1px solid #475569; }
.card.archived .thumb { opacity: 0; }               /* frozen frame hidden (src also cleared in JS) */
.card.archived .archived-banner {
  display: flex; flex-direction: column;
  position: absolute; top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  text-align: center; pointer-events: none;
  background: rgba(51, 65, 85, .9);
  padding: 8px 18px; border-radius: 8px; color: #fff;
}
.card.archived .archived-banner .big { font-size: 16px; font-weight: 700; letter-spacing: 1px; }
.card.archived .archived-banner .sub { font-size: 11px; color: #cbd5e1; margin-top: 2px; }
/* On a dark PC these actions make no sense — leave hide (to un-hide), zoom, pin */
.card.archived .actions button[data-action="record"],
.card.archived .actions button[data-action="screenshot"],
.card.archived .actions button[data-action="alert"] { display: none; }
.card .actions button[data-action="hide"].active,
#archived-btn.active { background: #475569; border-color: #64748b; }

/* Pin button + pinned-state visual */
.card .actions button[data-action="pin"].pinned {
  background: #d29922; color: #000; border-color: #d29922;
}
.card.pinned { outline: 2px solid #1f6feb; }
.card .pin-mark {
  position: absolute; top: 8px; left: 8px;
  font-size: 14px; pointer-events: none;
  text-shadow: 0 1px 3px rgba(0,0,0,.7);
}

dialog {
  border: 1px solid #30363d; background: #0d1117; color: #e6edf3;
  border-radius: 12px; padding: 0; max-width: 90vw; max-height: 90vh;
}
dialog::backdrop { background: rgba(0,0,0,.7); }
/* Simple, consistent open animation for every dialog. Opacity-only on purpose:
   animating transform on #zoom would make it a containing block and break the
   position:fixed #zoom-window inside it. */
dialog[open] { animation: dlg-in .16s ease-out; }
@keyframes dlg-in { from { opacity: 0; } to { opacity: 1; } }
dialog header {
  position: static; padding: 10px 16px; min-height: 52px;
  border-bottom: 1px solid #30363d; background: #161b22;
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
}
dialog header button { padding: 2px 10px; font-size: 18px; }
dialog header [data-close] { width: 36px; height: 36px; padding: 0; border-radius: 8px; }

/* ---- Reusable custom modal (prompt / confirm / info) ---- */
.app-modal { border: none; background: transparent; padding: 0; }
.dialog-panel {
  background: #0d1117; color: #e6edf3; border: 1px solid #30363d;
  border-radius: 12px; overflow: hidden; box-shadow: 0 20px 60px rgba(0,0,0,.55);
}
.dialog-header {
  min-height: 52px; padding: 10px 16px; background: #161b22;
  border-bottom: 1px solid #30363d;
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
}
.dialog-title { font-weight: 600; }
.dialog-close { width: 36px; height: 36px; padding: 0; border-radius: 8px; font-size: 18px; line-height: 1; }
.modal-body { padding: 16px; min-width: 340px; max-width: 90vw; }
.modal-message { margin: 0 0 12px; color: #c9d1d9; font-size: 14px; line-height: 1.4; }
.modal-message:last-child { margin-bottom: 0; }
.modal-input {
  width: 100%; background: #0d1117; color: #e6edf3; border: 1px solid #30363d;
  border-radius: 8px; padding: 10px 12px; font: inherit;
}
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 16px; border-top: 1px solid #30363d; }
.modal-btn { min-height: 38px; padding: 0 16px; border-radius: 8px; }
.modal-btn.primary { background: #1f6feb; border-color: #1f6feb; color: #fff; }
.modal-btn.primary:hover { background: #388bfd; }
/* Zoom = resizable, draggable multi-monitor viewer (desktop).
   #zoom is just a transparent, collapsed container — the backdrop dims the page
   and the actual floating box is #zoom-window (position:fixed, moved/resized by JS). */
#zoom {
  border: none; background: transparent; padding: 0; overflow: visible;
  max-width: 100vw; max-height: 100vh;
}
#zoom-window {
  position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%);
  width: 86vw; height: 80vh;
  min-width: 360px; min-height: 240px;      /* minimum size limit */
  max-width: 96vw; max-height: 92vh;
  background: #0d1117; border: 1px solid #30363d; border-radius: 12px;
  overflow: hidden; box-shadow: 0 20px 60px rgba(0,0,0,.55);
}
/* Custom resize handle (pointer-event driven) — bottom-right corner of the window */
#zoom-resize-handle {
  position: absolute; right: 0; bottom: 0; width: 24px; height: 24px;
  cursor: nwse-resize; z-index: 5; touch-action: none;
  background:
    linear-gradient(135deg, transparent 40%, rgba(255,255,255,.5) 40%, rgba(255,255,255,.5) 52%, transparent 52%,
                    transparent 62%, rgba(255,255,255,.5) 62%, rgba(255,255,255,.5) 74%, transparent 74%);
}
#zoom-header { height: 56px; display: flex; align-items: center; gap: 10px; cursor: move; user-select: none; touch-action: none; }
#zoom-title { min-width: 0; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.zoom-monitors { display: flex; gap: 4px; margin-left: auto; }
.zoom-monitors .mon-chip {
  width: 26px; height: 26px; padding: 0; font-size: 12px; border-radius: 6px;
  background: #21262d; color: #8b949e;
}
.zoom-monitors .mon-chip.active { background: #1f6feb; border-color: #1f6feb; color: #fff; }
.zoom-actions { display: flex; gap: 6px; }
.zoom-actions button { width: 40px; height: 40px; padding: 0; font-size: 16px; line-height: 1; }
#zoom-rec.recording { background: #da3633; border-color: #da3633; }
/* Pane grid — columns/rows follow how many screens are selected */
/* Grid follows the zoom-window size (header is 56px), not the viewport */
.zoom-grid { display: grid; gap: 3px; background: #000; width: 100%; height: calc(100% - 56px); }
.zoom-grid.cols-1 { grid-template-columns: 1fr; }
.zoom-grid.cols-2 { grid-template-columns: 1fr 1fr; }
.zoom-grid.cols-2.rows-2 { grid-template-rows: 1fr 1fr; }
.zoom-pane { position: relative; overflow: hidden; background: #000; display: flex; }
.zoom-pane img { width: 100%; height: 100%; object-fit: contain; display: block; }
.zoom-pane .mon-label {
  position: absolute; top: 4px; left: 4px; font-size: 11px;
  padding: 2px 6px; border-radius: 6px; background: rgba(0,0,0,.6); color: #fff;
  pointer-events: none;
}

.tabs { display: flex; gap: 4px; padding: 10px 16px; }
.tab { background: transparent; border: 1px solid #30363d; }
.tab.active { background: #1f6feb; border-color: #1f6feb; }

#files-list { list-style: none; margin: 0; padding: 10px 16px; max-height: 70vh; overflow: auto; min-width: 480px; }
#files-list li { padding: 8px 0; border-bottom: 1px solid #21262d; display: flex; justify-content: space-between; }
#files-list a { color: #58a6ff; text-decoration: none; }
#files-list a:hover { text-decoration: underline; }

/* "Screens/view" count control — themed dropdown popup */
#per-view-ctl { position: relative; display: flex; align-items: center; gap: 6px; color: #8b949e; font-size: 12px; }
#per-view-btn {
  min-width: 46px; padding: 5px 8px; font-size: 13px;
  background: #21262d; color: #e6edf3; border: 1px solid #30363d; border-radius: 6px;
}
#per-view-menu {
  position: absolute; top: calc(100% + 6px); right: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px;
  padding: 8px; width: 210px;
  background: #161b22; border: 1px solid #30363d; border-radius: 10px;
  box-shadow: 0 8px 28px rgba(0,0,0,.55);
}
#per-view-menu[hidden] { display: none; }
#per-view-menu button {
  height: 34px; padding: 0; font-size: 14px; border-radius: 6px;
  background: #21262d; color: #e6edf3; border: 1px solid #30363d;
}
#per-view-menu button:hover { background: #30363d; }
#per-view-menu button.active { background: #1f6feb; border-color: #1f6feb; color: #fff; font-weight: 600; }

/* Screen cycling: focused card (3px so it wins over pinned/outdated 2px) + screen badge */
.card.focused { outline: 3px solid #1f6feb; outline-offset: -3px; }
.card .mon-badge {
  position: absolute; top: 8px; left: 8px; z-index: 6;
  padding: 2px 8px; font-size: 12px; font-weight: 600;
  background: rgba(31,111,235,.92); color: #fff; border-radius: 6px;
  pointer-events: none;
}
#slide-ctl { position: relative; display: flex; align-items: center; gap: 6px; color: #8b949e; font-size: 12px; }
#slide-btn, #slide-gap-btn {
  padding: 5px 8px; font-size: 13px;
  background: #21262d; color: #e6edf3; border: 1px solid #30363d; border-radius: 6px;
}
#slide-btn.active { background: #1f6feb; border-color: #1f6feb; color: #fff; font-weight: 600; }
#slide-gap-menu {
  position: absolute; top: calc(100% + 6px); right: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px;
  padding: 8px; width: 150px;
  background: #161b22; border: 1px solid #30363d; border-radius: 10px;
  box-shadow: 0 8px 28px rgba(0,0,0,.55);
}
#slide-gap-menu[hidden] { display: none; }
#slide-gap-menu button {
  height: 34px; padding: 0; font-size: 13px; border-radius: 6px;
  background: #21262d; color: #e6edf3; border: 1px solid #30363d;
}
#slide-gap-menu button:hover { background: #30363d; }
#slide-gap-menu button.active { background: #1f6feb; border-color: #1f6feb; color: #fff; font-weight: 600; }

/* Removed-screens retrieve list */
#removed-list { list-style: none; margin: 0; padding: 10px 16px; max-height: 70vh; overflow: auto; min-width: 340px; }
#removed-list li { padding: 8px 0; border-bottom: 1px solid #21262d; display: flex; justify-content: space-between; align-items: center; gap: 12px; }

.alert-body { padding: 12px 16px; min-width: 420px; display: flex; flex-direction: column; gap: 10px; }
.alert-tpl-row { display: flex; gap: 6px; }
.alert-tpl-row select { flex: 1; background: #0d1117; color: #e6edf3; border: 1px solid #30363d; border-radius: 6px; padding: 6px; }
#alert-text { background: #0d1117; color: #e6edf3; border: 1px solid #30363d; border-radius: 6px; padding: 8px; font: inherit; resize: vertical; }
.alert-opts { display: flex; gap: 16px; flex-wrap: wrap; }
.alert-opts label { display: flex; align-items: center; gap: 8px; font-size: 13px; color: #9da7b3; }
.alert-opts input[type=number] { width: 70px; background: #0d1117; color: #e6edf3; border: 1px solid #30363d; border-radius: 6px; padding: 6px; }
.alert-opts input[type=color] { width: 44px; height: 32px; padding: 2px; background: #0d1117; border: 1px solid #30363d; border-radius: 6px; }
.alert-send { display: flex; gap: 8px; }
.alert-send button { flex: 1; }
#alert-send-one { background: #b62324; border-color: #b62324; }
@media (max-width: 720px) { .alert-body { min-width: unset; } }


/* ============================================================ */
/*  MOBILE — completely different layout                         */
/* ============================================================ */
@media (max-width: 720px) {
  header {
    padding: 10px 14px;
    flex-wrap: wrap; gap: 8px;
  }
  header h1 { font-size: 17px; font-weight: 700; }
  .meta {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    font-size: 12px;
  }
  .meta > #agent-count { font-weight: 600; color: #e6edf3; }
  .meta > #conn-status { font-size: 12px; }
  /* Action buttons drop to their own full-width row below the status line */
  .header-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    width: 100%;
    margin-top: 2px;
  }
  /* Keep the labels — auto-width pills that grow to fill each row, real tap height */
  .header-actions > button {
    height: 40px;
    padding: 0 14px;
    font-size: 14px;
    border-radius: 10px;
    flex: 1 1 calc(50% - 4px);   /* two per row, wraps predictably */
    white-space: nowrap;
  }
  .header-actions > button#logout-btn { flex: 0 0 auto; width: 44px; padding: 0; font-size: 16px; }

  nav#pager {
    top: 0;                /* mobile: pager scrolls with content, not sticky to bottom of header */
    padding: 8px 10px;
    gap: 4px;
    overflow-x: auto;
    flex-wrap: nowrap;
    border-bottom: 1px solid #30363d;
    -webkit-overflow-scrolling: touch;
  }
  nav#pager::-webkit-scrollbar { display: none; }
  nav#pager button {
    min-width: 42px; min-height: 42px;
    font-size: 14px; padding: 6px 10px;
  }
  nav#pager .page-info {
    width: 100%; margin-left: 0; order: -1;
    text-align: center; font-size: 12px; margin-bottom: 4px;
  }

  /* Mobile grid → single full-width column, scroll (video-wall is desktop-only) */
  main#grid {
    grid-template-columns: 1fr;
    grid-template-rows: none;
    height: auto !important;
    gap: 12px;
    padding: 12px;
    justify-content: stretch;
  }

  /* Mobile card — bigger, list-style with image + info row */
  .card {
    grid-column: auto;      /* ignore desktop drag-resize spans on mobile */
    grid-row: auto;
    aspect-ratio: 16 / 9;
    border-radius: 14px;
    border: 1px solid #30363d;
    background: #161b22;
    box-shadow: 0 2px 12px rgba(0,0,0,.35);
  }
  .card .resize-handle { display: none; }
  .card .thumb {
    object-fit: cover;
    background: #0d1117;
  }
  /* Bigger touch-friendly overlay on mobile */
  .card .overlay {
    padding: 12px 14px;
    background: linear-gradient(to top,
      rgba(0,0,0,.92) 0%,
      rgba(0,0,0,.65) 60%,
      transparent 100%);
  }
  .card .label strong { font-size: 16px; }
  .card .label .user { font-size: 13px; }
  .card .label .version { font-size: 11px; }
  .card .status {
    font-size: 12px; padding: 5px 10px;
    top: 10px; right: 10px;
  }
  /* Always-visible larger touch buttons; no resize dragger on mobile → no gap */
  .card .actions { opacity: 1 !important; gap: 6px; margin-right: 0; }
  .card .actions button {
    width: 44px; height: 44px;
    font-size: 18px; line-height: 1;
    border-radius: 10px;
    background: rgba(33, 38, 45, .9);
    backdrop-filter: blur(4px);
  }

  /* Mobile offline banner — bigger, easy to read */
  .card.offline { background: #161b22; }
  .card.offline .thumb { opacity: .1; }
  .card.offline .offline-banner {
    padding: 12px 24px;
  }
  .card.offline .offline-banner .big { font-size: 20px; letter-spacing: 2px; }
  .card.offline .offline-banner .sub { font-size: 13px; margin-top: 4px; }

  /* Dialog full-screen */
  dialog {
    max-width: 100vw; max-height: 100vh;
    border-radius: 0; margin: 0;
    width: 100vw; height: 100vh;
  }
  /* Mobile: full-screen viewer, no floating/resize (touch) — panes fill the screen */
  #zoom {
    width: 100vw; height: 100vh; max-width: 100vw; max-height: 100vh;
    padding: 0; border: none; background: transparent;
  }
  #zoom-window {
    left: 0 !important; top: 0 !important; transform: none !important;
    width: 100vw !important; height: 100vh !important;
    min-width: 0; min-height: 0; max-width: 100vw; max-height: 100vh;
    border-radius: 0;
  }
  #zoom-header { cursor: default; }
  #zoom-resize-handle { display: none; }
  .zoom-grid { width: 100%; height: calc(100vh - 56px); }
  #files-list { min-width: unset; }
  .modal-body { min-width: 0; }
}

/* Very narrow phones */
@media (max-width: 380px) {
  header h1 { font-size: 15px; }
  .header-actions > button { height: 38px; padding: 0 10px; font-size: 13px; }
  .header-actions > button#logout-btn { width: 40px; padding: 0; }
  main#grid { padding: 8px; gap: 10px; }
  .card .label strong { font-size: 15px; }
}
