:root {
  --bg: #0d1117;
  --panel: #161b22;
  --panel-2: #1c2330;
  --border: #2a313c;
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #2f81f7;
  --accent-2: #238636;
  --danger: #da3633;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 18px; font-size: 13px; margin-top: 6px; }

/* Login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; }
.login-card {
  background: var(--panel); border: 1px solid var(--border);
  padding: 32px; border-radius: 12px; width: 320px; text-align: center;
}
.login-card h1 { margin: 0; font-size: 28px; letter-spacing: .5px; }
.login-card input {
  width: 100%; margin-top: 18px; padding: 11px 12px;
  background: var(--bg); border: 1px solid var(--border);
  border-radius: 8px; color: var(--text); font-size: 14px;
}
.login-card button {
  width: 100%; margin-top: 14px; padding: 11px;
  background: var(--accent); color: #fff; border: 0;
  border-radius: 8px; font-size: 14px; cursor: pointer;
}

/* Topbar */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 20px; background: var(--panel); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 5;
}
.brand { font-size: 18px; font-weight: 600; }
.status { display: flex; align-items: center; gap: 10px; font-size: 13px; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); display: inline-block; }
.dot.on { background: var(--accent-2); }
.dot.off { background: var(--danger); }

button.ghost { background: transparent; color: var(--text); border: 1px solid var(--border); border-radius: 7px; padding: 7px 12px; cursor: pointer; }
button.primary { background: var(--accent); color: #fff; border: 0; border-radius: 7px; padding: 8px 14px; cursor: pointer; }

/* Grid */
main { padding: 20px; }
.empty { color: var(--muted); text-align: center; padding: 60px 0; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 16px; }

.card {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 12px; display: flex; flex-direction: column;
}
.thumb {
  aspect-ratio: 16 / 10; background: #000 center/contain no-repeat;
  display: grid; place-items: center; color: var(--muted); font-size: 13px;
  overflow: hidden; border-radius: 12px 12px 0 0;   /* card no longer clips (menu can overflow) */
}
.thumb img { width: 100%; height: 100%; object-fit: contain; display: block; }
.card-body { padding: 12px 14px; }
.card-title { font-weight: 600; display: flex; align-items: center; gap: 8px; }
.card-sub { color: var(--muted); font-size: 12px; margin-top: 3px; }
.card-actions { display: flex; gap: 8px; padding: 0 14px 10px; }
.card-actions button { flex: 1; }
.card-lifecycle { display: flex; gap: 6px; padding: 0 14px 14px; }
.card-lifecycle button { flex: 1; font-size: 12px; padding: 6px 4px; }
button:disabled { opacity: .45; cursor: not-allowed; }
.pill { font-size: 11px; padding: 2px 8px; border-radius: 999px; border: 1px solid var(--border); }
.pill.online { color: #3fb950; border-color: #23502f; }
.pill.offline { color: var(--muted); }
.pill.busy { color: #d29922; border-color: #5a4211; }
.pill.stopped { color: #f85149; border-color: #5a2320; }

/* Viewer */
.viewer { position: fixed; inset: 0; background: rgba(0,0,0,.9); z-index: 20; display: flex; flex-direction: column; }
.viewer:fullscreen { background: #000; }
.viewer:-webkit-full-screen { background: #000; }
.viewer-bar { display: flex; align-items: center; gap: 12px; padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--border); }
.viewer-bar .spacer { flex: 1; }
.badge { font-size: 11px; padding: 3px 9px; border-radius: 999px; background: var(--panel-2); border: 1px solid var(--border); }
.badge.controlling { background: #143a1c; border-color: #23502f; color: #3fb950; }
.viewer-stage { flex: 1; min-height: 0; position: relative; display: flex; justify-content: center; align-items: center; overflow: hidden; }
#screen { max-width: 100%; max-height: 100%; width: auto; height: auto; object-fit: contain; cursor: default; }
#screen.scale-fill { width: 100%; height: 100%; max-width: none; max-height: none; object-fit: fill; position: absolute; }
#screen.controlling { cursor: default; }
.viewer-hint { position: absolute; bottom: 16px; left: 50%; transform: translateX(-50%); background: rgba(0,0,0,.6); padding: 8px 14px; border-radius: 8px; font-size: 13px; color: var(--muted); pointer-events: none; }

/* Shortcuts Dropdown */
.dropdown { position: relative; display: inline-block; }
.dropdown-content {
  position: absolute;
  top: 100%;
  right: 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  min-width: 220px;
  max-height: 380px;
  overflow-y: auto;
  box-shadow: 0 8px 24px rgba(0,0,0,.5);
  z-index: 30;
  display: flex;
  flex-direction: column;
  padding: 6px;
  margin-top: 4px;
}
.dropdown-content button {
  text-align: left;
  padding: 8px 12px;
  font-size: 13px;
  border-radius: 6px;
  background: transparent;
  border: 0;
  color: var(--text);
  cursor: pointer;
  width: 100%;
}
.dropdown-content button:hover {
  background: var(--panel-2);
}

/* Remote command runner */
.run-console { position: absolute; left: 16px; right: 16px; bottom: 16px; max-width: 820px; margin: 0 auto; background: rgba(13,17,23,.96); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; box-shadow: 0 8px 30px rgba(0,0,0,.5); }
.run-head { display: flex; align-items: center; justify-content: space-between; font-size: 12px; color: var(--muted); margin-bottom: 8px; }
.run-head button { padding: 0 8px; line-height: 1; font-size: 18px; }
.run-row { display: flex; gap: 8px; }
.run-row input { flex: 1; background: var(--bg); border: 1px solid var(--border); border-radius: 7px; color: var(--text); font-size: 13px; padding: 8px 10px; font-family: ui-monospace, Consolas, monospace; }
.run-out { margin: 8px 0 0; max-height: 220px; overflow: auto; background: #000; border: 1px solid var(--border); border-radius: 7px; padding: 10px; font-family: ui-monospace, Consolas, monospace; font-size: 12px; line-height: 1.45; color: #c9d1d9; white-space: pre-wrap; word-break: break-word; }

/* Management tools console */
.tools-console { max-width: 640px; }
.tools-tabs { display: flex; gap: 6px; margin-bottom: 10px; }
.tools-tab { background: transparent; color: var(--muted); border: 1px solid var(--border); border-radius: 7px; padding: 6px 12px; cursor: pointer; font-size: 13px; }
.tools-tab.active { background: var(--panel-2); color: var(--text); }
.tools-row { display: flex; gap: 8px; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.tools-row input { flex: 1; background: var(--bg); border: 1px solid var(--border); border-radius: 7px; color: var(--text); font-size: 13px; padding: 8px 10px; }
.tools-panel input[type="text"], .tools-panel textarea, .tools-panel input[type="number"] { background: var(--bg); border: 1px solid var(--border); border-radius: 7px; color: var(--text); font-size: 13px; padding: 8px 10px; width: 100%; box-sizing: border-box; }
.tools-panel textarea { resize: vertical; margin: 8px 0; font-family: inherit; }
.tools-panel > input[type="text"] { margin-bottom: 8px; }
.tools-list { max-height: 260px; overflow: auto; background: var(--bg); border: 1px solid var(--border); border-radius: 7px; padding: 6px; font-size: 13px; color: var(--muted); }
.app-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 7px 8px; border-bottom: 1px solid var(--border); }
.app-row:last-child { border-bottom: 0; }
.app-meta { display: flex; flex-direction: column; min-width: 0; }
.app-name { color: var(--text); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.app-sub { color: var(--muted); font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.app-row button { flex: 0 0 auto; padding: 5px 10px; font-size: 12px; }
.app-row button:disabled { opacity: .45; cursor: default; }
.tools-subhead { margin: 12px 0 6px; font-size: 12px; color: var(--muted); }
.kv-row { display: flex; gap: 10px; padding: 6px 8px; border-bottom: 1px solid var(--border); font-size: 13px; }
.kv-row:last-child { border-bottom: 0; }
.kv-key { flex: 0 0 130px; color: var(--muted); }
.kv-val { color: var(--text); word-break: break-word; }
.tools-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 10px; }
.tools-grid label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.tools-status { margin-top: 8px; font-size: 12px; min-height: 16px; color: var(--muted); }
.tools-status.ok { color: #3fb950; }
.tools-status.err { color: #f85149; }

/* SMTP settings modal */
.smtp-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 10px 0; text-align: left; }
.smtp-grid label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
#smtp-form input[type="text"], #smtp-form input[type="password"], #smtp-form input[type="number"] { background: var(--bg); border: 1px solid var(--border); border-radius: 7px; color: var(--text); font-size: 13px; padding: 8px 10px; }
.chk { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--text); margin: 8px 0; text-align: left; }
.chk input { width: auto; }
.chk-inline { align-self: end; }

/* Password fields: reveal toggle + Caps Lock warning.
   Selectors are intentionally specific (.pw-field .pw-toggle) so they win over
   container button rules like `.login-card button` / modal button styling that
   would otherwise stretch the eye to full width. */
.pw-wrap { display: block; }
.pw-field { position: relative; display: block; }
.pw-field > input.pw-input { width: 100%; box-sizing: border-box; padding-right: 40px; }
.pw-field .pw-toggle {
  position: absolute; top: 0; right: 0; height: 100%; width: 38px; min-width: 0;
  margin: 0; padding: 0; border: 0; border-radius: 0; box-shadow: none;
  background: transparent; color: inherit; cursor: pointer;
  display: grid; place-items: center;
  font-size: 15px; line-height: 1; opacity: .55; filter: grayscale(1);
}
.pw-field .pw-toggle:hover, .pw-field .pw-toggle.on { opacity: 1; filter: none; background: transparent; }
.caps-warn { margin-top: 6px; font-size: 12px; color: #d29922; text-align: left; }
.caps-warn::before { content: "⚠ "; }

/* Add-device (enrollment) modal */
.enroll-card { max-width: 640px; text-align: left; }
.enroll-card h2 { margin: 0 0 10px; }
.enroll-card p { font-size: 13px; line-height: 1.5; }
.enroll-card code { background: var(--bg); border: 1px solid var(--border); border-radius: 5px; padding: 1px 6px; font-family: ui-monospace, Consolas, monospace; }
.enroll-cmd { display: flex; gap: 8px; align-items: center; background: #000; border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; margin: 12px 0; }
.enroll-cmd code { flex: 1; background: transparent; border: 0; padding: 0; color: #3fb950; font-size: 13px; overflow-x: auto; white-space: nowrap; }
.enroll-cmd button { flex: 0 0 auto; }

/* Modal Dialog */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  display: grid;
  place-items: center;
  z-index: 50;
  backdrop-filter: blur(2px);
}
.modal-card {
  background: var(--panel);
  border: 1px solid var(--border);
  padding: 24px;
  border-radius: 12px;
  width: 90%;
  max-width: 400px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
  animation: modalSlide 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}
.modal-card h2 {
  margin: 0 0 10px 0;
  font-size: 20px;
  font-weight: 600;
}
.modal-card p {
  margin: 0 0 20px 0;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.5;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
.modal-actions button {
  padding: 8px 16px;
  font-size: 14px;
  border-radius: 6px;
  cursor: pointer;
}
button.primary-btn {
  background: var(--accent);
  color: #fff;
  border: 0;
}
button.danger-btn {
  background: var(--danger);
  color: #fff;
  border: 0;
}

@keyframes modalSlide {
  from { transform: translateY(15px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

/* Card 3-dot menu (Lock / Sleep / Restart / Remove) */
.card-spacer { flex: 1; }
.card-menu { position: relative; flex: 0 0 auto; }
.btn-menu {
  width: 28px; height: 28px; min-height: 28px; padding: 0;
  border-radius: 6px; background: transparent; border: 1px solid var(--border);
  color: var(--muted); cursor: pointer; font-size: 18px; line-height: 1;
  display: grid; place-items: center; transition: all 0.15s ease;
}
.btn-menu:hover { background: var(--panel-2); color: var(--text); }
.menu-dropdown {
  position: absolute; top: 34px; right: 0; z-index: 20; min-width: 180px;
  max-height: calc(100vh - 24px); overflow-y: auto;
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 10px;
  padding: 6px; box-shadow: 0 10px 30px rgba(0,0,0,.5); display: flex; flex-direction: column;
}
.menu-dropdown button {
  text-align: left; background: transparent; border: 0; color: var(--text);
  padding: 9px 10px; border-radius: 6px; cursor: pointer; font-size: 13px; min-height: 0;
}
.menu-dropdown button:hover { background: var(--panel); }
.menu-dropdown .menu-danger { color: var(--danger); }
.menu-dropdown .menu-danger:hover { background: var(--danger); color: #fff; }
.menu-sep { height: 1px; background: var(--border); margin: 5px 4px; }
.card { position: relative; }

/* Invisible but focusable input used to summon the mobile keyboard when the user
   taps the remote screen. It must be in the viewport (NOT off-screen / aria-hidden)
   or Android/iOS refuse to open the keyboard; opacity:0 hides it and
   pointer-events:none lets taps pass through to the screen underneath. font-size
   16px stops iOS auto-zoom. */
#kbd-capture {
  position: absolute; left: 0; bottom: 0; width: 100%; height: 44px;
  opacity: 0; pointer-events: none;
  border: 0; padding: 0; margin: 0; background: transparent;
  color: transparent; caret-color: transparent; font-size: 16px;
}

/* ---------- Responsive / mobile ---------- */
html, body { max-width: 100%; overflow-x: hidden; touch-action: manipulation; }
button { min-height: 34px; }
/* The remote screen must not pan/zoom under touch — every gesture is control input. */
#screen { touch-action: none; }
.viewer-stage { touch-action: none; }

@media (max-width: 720px) {
  .topbar { padding: 10px 12px; gap: 8px; flex-wrap: wrap; }
  .brand { font-size: 16px; }
  .status { gap: 8px; font-size: 12px; flex-wrap: wrap; }
  main { padding: 12px; }
  .grid { grid-template-columns: 1fr; gap: 12px; }

  /* Viewer top bar: let controls wrap and stay tappable */
  .viewer-bar { flex-wrap: wrap; gap: 8px; padding: 8px 10px; }
  #viewer-title { font-size: 13px; max-width: 40vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .viewer-bar .spacer { flex-basis: 100%; height: 0; order: 5; }
  .viewer-bar button { padding: 8px 10px; font-size: 13px; }

  .run-console { left: 8px; right: 8px; bottom: 8px; }
  .run-out { max-height: 30vh; }
  .modal-card { padding: 18px; }
  .enroll-cmd code { font-size: 12px; }

  .viewer-hint { display: none; }  /* saves space on small screens */
  .chat-console { left: 8px; right: 8px; bottom: 8px; width: auto; height: 350px; }
}

/* Drag and Drop Upload Overlay */
.drop-overlay {
  position: absolute;
  inset: 0;
  background: rgba(13, 17, 23, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 30;
  backdrop-filter: blur(4px);
  border: 3px dashed var(--accent);
  border-radius: 8px;
  margin: 16px;
  animation: fadeIn 0.15s ease-out;
}
.drop-message {
  font-size: 20px;
  font-weight: 600;
  color: var(--accent);
  text-align: center;
  background: var(--panel);
  padding: 24px 36px;
  border-radius: 12px;
  border: 1px solid var(--border);
  box-shadow: 0 10px 30px rgba(0,0,0,.5);
}

/* Toast notifications */
.toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  z-index: 100;
  pointer-events: none;
}
.toast {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 12px 18px;
  border-radius: 8px;
  font-size: 14px;
  box-shadow: 0 8px 24px rgba(0,0,0,.4);
  pointer-events: auto;
  min-width: 250px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  animation: toastSlideIn 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}
.toast.info {
  border-left: 4px solid var(--accent);
}
.toast.success {
  border-left: 4px solid var(--accent-2);
}
.toast.error {
  border-left: 4px solid var(--danger);
}
.toast-close {
  background: transparent;
  border: 0;
  color: var(--muted);
  cursor: pointer;
  font-size: 18px;
  padding: 0 4px;
  line-height: 1;
}
.toast-close:hover {
  color: var(--text);
}

/* Progress Toast styling */
.toast.progress-toast {
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
  min-width: 280px;
}
.toast-progress-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
  font-weight: 500;
}
.progress-bar-bg {
  height: 6px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 3px;
  overflow: hidden;
  position: relative;
  width: 100%;
}
.progress-bar-fill {
  height: 100%;
  background: var(--accent);
  width: 0%;
  border-radius: 3px;
  transition: width 0.15s ease-out;
}
.toast-cancel-btn {
  background: rgba(219, 72, 72, 0.15);
  border: 1px solid rgba(219, 72, 72, 0.3);
  color: var(--danger);
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 11px;
  cursor: pointer;
  align-self: flex-end;
  font-weight: 600;
  transition: background 0.15s, border-color 0.15s;
}
.toast-cancel-btn:hover {
  background: rgba(219, 72, 72, 0.3);
  border-color: var(--danger);
}

/* Chat box styling */
.chat-console {
  position: absolute;
  right: 16px;
  bottom: 80px;
  width: 340px;
  height: 400px;
  background: rgba(13, 17, 23, 0.96);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  box-shadow: 0 8px 30px rgba(0,0,0,.5);
  display: flex;
  flex-direction: column;
  z-index: 40;
  gap: 10px;
  transition: opacity 0.2s, transform 0.2s;
}
.chat-console.hidden {
  display: none !important;
}
.chat-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}
.chat-head button {
  background: transparent;
  border: 0;
  color: var(--muted);
  font-size: 18px;
  cursor: pointer;
  line-height: 1;
}
.chat-head button:hover {
  color: var(--text);
}
.chat-out {
  flex: 1;
  overflow-y: auto;
  background: #000;
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 10px;
  font-size: 13px;
  color: var(--text);
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.chat-msg-row {
  display: flex;
  flex-direction: column;
}
.chat-msg-row.sent {
  align-items: flex-end;
}
.chat-msg-row.received {
  align-items: flex-start;
}
.chat-bubble {
  max-width: 80%;
  padding: 8px 12px;
  border-radius: 12px;
  word-break: break-word;
  line-height: 1.4;
}
.chat-msg-row.sent .chat-bubble {
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 2px;
}
.chat-msg-row.received .chat-bubble {
  background: var(--panel-2);
  color: var(--text);
  border-bottom-left-radius: 2px;
}
.chat-msg-meta {
  font-size: 10px;
  color: var(--muted);
  margin-top: 2px;
}
.chat-row {
  display: flex;
  gap: 8px;
}
.chat-row input {
  flex: 1;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 7px;
  color: var(--text);
  font-size: 13px;
  padding: 8px 10px;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes toastSlideIn {
  from { transform: translateY(20px) scale(0.9); opacity: 0; }
  to { transform: translateY(0) scale(1); opacity: 1; }
}

/* Organization Views */
.orgs-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
  margin-top: 20px;
}
.org-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  min-height: 160px;
  cursor: pointer;
  transition: transform 0.2s, border-color 0.2s;
}
.org-card:hover {
  transform: translateY(-2px);
  border-color: var(--accent);
}
.org-card-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 8px 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.org-card-stats {
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 12px;
}
.org-card-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: auto;
}
.org-card-actions button {
  min-height: 30px;
  padding: 4px 10px;
  font-size: 12px;
}
.org-code-badge {
  font-family: ui-monospace, Consolas, monospace;
  font-size: 11px;
  padding: 2px 6px;
  border-radius: 4px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: #58a6ff;
}

.view-header {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 20px;
}
.btn-back {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  color: var(--muted);
  background: transparent;
  border: 0;
  cursor: pointer;
  padding: 0;
}
.btn-back:hover {
  color: var(--text);
}
.view-title {
  font-size: 22px;
  font-weight: 600;
  margin: 0;
}

/* Form Styles for Modals */
.form-group {
  margin-bottom: 16px;
  text-align: left;
}
.form-group label {
  display: block;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 6px;
}
.form-group input {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 14px;
}
.form-group input:focus {
  border-color: var(--accent);
  outline: none;
}



