* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px; line-height: 1.5;
  color: #1f2937; background: #f9fafb;
  min-height: 100vh;
}

/* App shell — CSS Grid layout with header / sidebar / main / footer regions. */
.app-shell {
  display: grid;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
  grid-template-rows: 56px 1fr 40px;
  grid-template-columns: 220px 1fr;
  min-height: 100vh;
}
.app-header {
  grid-area: header;
  background: #111827; color: #f9fafb;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 24px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
  z-index: 10;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { width: 28px; height: 28px; border-radius: 6px; object-fit: cover; display: block; }
.brand-name { font-weight: 600; font-size: 16px; letter-spacing: 0.3px; color: #f9fafb; }
.header-right { display: flex; align-items: center; gap: 16px; }
.user-chip {
  display: flex; align-items: center; gap: 8px;
  font-size: 13px; color: #d1d5db;
}
.user-avatar {
  width: 28px; height: 28px; border-radius: 50%;
  background: #3b82f6; color: #fff; font-size: 11px; font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center;
}
.logout-form { margin: 0; }
.logout-btn {
  background: transparent; color: #d1d5db; border: 1px solid #374151;
  padding: 6px 12px; border-radius: 4px; font-size: 12px; cursor: pointer;
  margin: 0;
}
.logout-btn:hover { background: #1f2937; border-color: #4b5563; color: #fff; }

.app-sidebar {
  grid-area: sidebar;
  background: #f3f4f6; border-right: 1px solid #e5e7eb;
  padding: 16px 0;
  overflow-y: auto;
}
.sidenav { display: flex; flex-direction: column; gap: 2px; }
.sidenav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 20px; font-size: 14px;
  color: #4b5563; text-decoration: none;
  border-left: 3px solid transparent;
}
.sidenav-item:hover { background: #e5e7eb; color: #111827; text-decoration: none; }
.sidenav-item.active {
  background: #fff; color: #1d4ed8;
  border-left-color: #2563eb; font-weight: 500;
}
.sidenav-icon {
  width: 20px; text-align: center;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  opacity: 0.7;
}

.app-main {
  grid-area: main;
  padding: 24px 32px; overflow-y: auto;
}
.page-header { margin-bottom: 16px; }
.page-header h1 {
  margin: 0; font-size: 22px; font-weight: 600; color: #111827;
}
.page-body { /* container for the page content */ }

.app-footer {
  grid-area: footer;
  background: #f3f4f6; border-top: 1px solid #e5e7eb;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; color: #6b7280;
}
.app-footer a { color: #4b5563; }

/* Headings */
h1 { font-size: 24px; margin: 0 0 16px 0; color: #111827; }
h2 { font-size: 20px; margin: 24px 0 12px 0; color: #1f2937; }
h3 { font-size: 16px; margin: 20px 0 8px 0; color: #374151; }

/* Tables */
table {
  width: 100%; border-collapse: collapse; background: #fff;
  border: 1px solid #e5e7eb; border-radius: 6px; overflow: hidden;
  box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}
table thead { background: #f3f4f6; position: sticky; top: 0; }
table th {
  text-align: left; font-weight: 600; font-size: 12px;
  text-transform: uppercase; letter-spacing: 0.5px;
  color: #6b7280; padding: 10px 12px; border-bottom: 1px solid #e5e7eb;
}
table td { padding: 10px 12px; border-bottom: 1px solid #f3f4f6; }
table tbody tr:hover { background: #f9fafb; }
table tbody tr:last-child td { border-bottom: none; }

/* Forms */
form { display: block; }
form p { margin: 0 0 12px 0; }
label { display: block; font-weight: 500; font-size: 13px; color: #374151; margin-bottom: 4px; }
input[type="text"], input[type="number"], input[type="password"],
input[type="email"], input[type="date"], input[type="search"],
select, textarea {
  width: 100%; max-width: 480px;
  padding: 8px 10px; font-size: 14px;
  border: 1px solid #d1d5db; border-radius: 4px;
  background: #fff; color: #1f2937;
}
input:focus, select:focus, textarea:focus {
  outline: none; border-color: #3b82f6; box-shadow: 0 0 0 2px rgba(59,130,246,0.2);
}
input[type="file"] { padding: 4px 0; border: none; }
textarea { min-height: 80px; resize: vertical; }
input[type="submit"], button {
  background: #3b82f6; color: #fff; border: none;
  padding: 8px 16px; font-size: 14px; font-weight: 500;
  border-radius: 4px; cursor: pointer; margin-top: 4px;
}
input[type="submit"]:hover, button:hover { background: #2563eb; }
input[type="submit"].danger, button.danger { background: #dc2626; }
input[type="submit"].danger:hover, button.danger:hover { background: #b91c1c; }
input[type="submit"].secondary, button.secondary { background: #6b7280; }
input[type="submit"].secondary:hover, button.secondary:hover { background: #4b5563; }

/* Inline forms (single button per line) */
.actions form { display: inline-block; margin-right: 8px; margin-bottom: 8px; }

/* Links */
a { color: #2563eb; text-decoration: none; }
a:hover { text-decoration: underline; }

/* Status badges */
.badge {
  display: inline-block; padding: 2px 8px; font-size: 11px;
  font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px;
  border-radius: 12px;
}
.badge.online { background: #dcfce7; color: #15803d; }
.badge.offline { background: #f3f4f6; color: #6b7280; }
.badge.claimed { background: #dbeafe; color: #1d4ed8; }
.badge.unclaimed { background: #fef3c7; color: #92400e; }
.badge.released { background: #fce7f3; color: #be185d; }
.badge.disabled { background: #fee2e2; color: #991b1b; }
.badge.killswitch { background: #fee2e2; color: #991b1b; }
.badge.admin { background: #ddd6fe; color: #6d28d9; }
.badge.operator { background: #bfdbfe; color: #1e40af; }
.badge.viewer { background: #e5e7eb; color: #4b5563; }

/* Filter bar */
.filter-bar {
  display: flex; flex-wrap: wrap; gap: 12px; align-items: end;
  padding: 12px; background: #fff; border: 1px solid #e5e7eb;
  border-radius: 6px; margin-bottom: 16px;
}
.filter-bar label { margin-bottom: 4px; font-size: 12px; }
.filter-bar input, .filter-bar select { max-width: 200px; }

/* Pagination */
.pagination {
  display: flex; gap: 4px; justify-content: center;
  margin: 20px 0; align-items: center;
}
.pagination a, .pagination span {
  padding: 6px 12px; border: 1px solid #e5e7eb;
  border-radius: 4px; color: #374151; background: #fff;
  text-decoration: none; font-size: 13px;
}
.pagination a:hover { background: #f3f4f6; }
.pagination .current { background: #3b82f6; color: #fff; border-color: #3b82f6; }
.pagination .disabled { color: #9ca3af; pointer-events: none; }

/* Toolbar (above tables) */
.toolbar {
  display: flex; gap: 12px; margin-bottom: 12px;
  align-items: center; flex-wrap: wrap;
}
.toolbar a, .toolbar form { display: inline-block; }

/* Code/pre */
code { font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  background: #f3f4f6; padding: 2px 4px; border-radius: 3px; font-size: 12px; }

/* Card */
.card {
  background: #fff; border: 1px solid #e5e7eb;
  border-radius: 6px; padding: 16px; margin-bottom: 16px;
}

/* Login */
.login-shell {
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #1f2937 0%, #111827 100%);
  padding: 24px;
}
.login-card {
  background: #fff; border-radius: 8px; padding: 32px;
  width: 100%; max-width: 400px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.3);
}
.login-logo {
  display: block; margin: 0 auto 16px;
  width: 72px; height: 72px;
  border-radius: 14px; object-fit: cover;
  box-shadow: 0 4px 12px rgba(0,0,0,0.12);
}
.login-brand { margin: 0 0 4px 0; font-size: 24px; color: #111827; text-align: center; }
.login-tagline { margin: 0 0 24px 0; color: #6b7280; font-size: 13px; text-align: center; }
.alert.error {
  background: #fee2e2; color: #991b1b; border: 1px solid #fca5a5;
  padding: 10px 12px; border-radius: 4px; margin-bottom: 16px; font-size: 13px;
}
.login-card form input[type="submit"] { width: 100%; margin-top: 12px; }

/* Inline form rendered next to other action buttons. */
.inline-form { display: inline-block; margin-right: 8px; margin-bottom: 8px; }

/* Anchor styled like a button (used for Edit / Download links). */
.btn-link {
  display: inline-block; padding: 6px 12px;
  background: #f3f4f6; border-radius: 4px; font-size: 13px;
  color: #1f2937; text-decoration: none; margin-right: 8px;
}
.btn-link:hover { background: #e5e7eb; text-decoration: none; }

/* APK Store */
.apk-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 16px; margin-top: 12px;
}
.apk-card {
  background: #fff; border: 1px solid #e5e7eb; border-radius: 8px;
  padding: 16px; box-shadow: 0 1px 2px rgba(0,0,0,0.04);
  display: flex; flex-direction: column; gap: 8px;
}
.apk-card.current {
  border-color: #3b82f6; box-shadow: 0 1px 4px rgba(59,130,246,0.18);
}
.apk-version {
  display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap;
}
.apk-name { font-size: 18px; font-weight: 600; color: #111827; }
.apk-code { font-size: 12px; color: #6b7280; }
.apk-meta {
  font-size: 12px; color: #6b7280;
}
.apk-notes {
  font-size: 13px; color: #374151;
  background: #f9fafb; padding: 10px; border-radius: 4px;
  white-space: pre-wrap; max-height: 120px; overflow-y: auto;
}
.apk-actions { margin-top: auto; display: flex; gap: 8px; }
.apk-adoption {
  font-size: 12px; color: #6b7280; padding-top: 8px;
  border-top: 1px solid #f3f4f6; margin-top: 4px;
}

.empty-state { color: #6b7280; font-style: italic; padding: 32px; text-align: center; }
.alert.info { background: #dbeafe; color: #1e40af; padding: 10px 12px; border-radius: 4px; margin-bottom: 12px; }

/* Dashboard */
.stats-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px; margin-bottom: 16px;
}
.stat-card {
  display: flex; align-items: center; gap: 14px;
  background: #fff; border: 1px solid #e5e7eb; border-radius: 8px;
  padding: 16px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.04);
}
a.stat-link {
  color: inherit; text-decoration: none;
  transition: box-shadow 0.12s ease, transform 0.12s ease;
}
a.stat-link:hover {
  box-shadow: 0 4px 10px rgba(0,0,0,0.08);
  transform: translateY(-1px);
}
.stat-icon {
  width: 44px; height: 44px; border-radius: 8px;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 22px; line-height: 1;
}
.stat-card.neutral .stat-icon { background: #f3f4f6; color: #4b5563; }
.stat-card.online  .stat-icon { background: #dcfce7; color: #15803d; }
.stat-card.claimed .stat-icon { background: #dbeafe; color: #1d4ed8; }
.stat-card.warn    .stat-icon { background: #fee2e2; color: #991b1b; }
.stat-value { font-size: 24px; font-weight: 700; color: #111827; line-height: 1.1; }
.stat-label { font-size: 12px; color: #6b7280; margin-top: 2px;
  text-transform: uppercase; letter-spacing: 0.4px; }

.kpi-row { display: flex; gap: 32px; margin-bottom: 12px; flex-wrap: wrap; }
.kpi-num { font-size: 24px; font-weight: 600; color: #111827; }
.kpi-label { font-size: 12px; color: #6b7280; text-transform: uppercase; letter-spacing: 0.4px; }

.chart-wrap { width: 100%; padding: 8px 0; }
.chart-wrap svg { display: block; max-width: 100%; height: auto; }

.bar-track {
  display: inline-block; width: 60%; height: 8px;
  background: #e5e7eb; border-radius: 4px; overflow: hidden;
  vertical-align: middle; margin-right: 8px;
}
.bar-fill {
  height: 100%; background: #3b82f6; border-radius: 4px;
  transition: width 0.3s ease;
}
.bar-label { font-size: 12px; color: #4b5563; }

.two-col {
  display: grid; grid-template-columns: 1fr 1fr;
  gap: 16px;
}
@media (max-width: 900px) {
  .two-col { grid-template-columns: 1fr; }
}

/* ============ Responsive ============ */

/* Wrap tables so they scroll horizontally on narrow screens instead of overflowing the viewport */
.table-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin-bottom: 16px;
}
.table-scroll table { min-width: 600px; }

/* Hamburger toggle — CSS-only sidebar drawer using <input type="checkbox"> sibling state.
   The checkbox itself is hidden; a <label> styled as a button toggles it. */
.nav-toggle { display: none; }
.hamburger {
  display: none;
  width: 36px; height: 36px;
  align-items: center; justify-content: center;
  border-radius: 4px; cursor: pointer;
  color: #f3f4f6;
}
.hamburger:hover { background: #1f2937; }
.hamburger-icon {
  display: inline-block; width: 18px; height: 2px;
  background: #f3f4f6; position: relative;
}
.hamburger-icon::before, .hamburger-icon::after {
  content: ""; position: absolute; left: 0;
  width: 100%; height: 2px; background: #f3f4f6;
}
.hamburger-icon::before { top: -6px; }
.hamburger-icon::after { top: 6px; }

/* Hero stat grid already responsive (auto-fit minmax). Reduce padding on small screens. */
@media (max-width: 768px) {
  .stat-card { padding: 12px; }
  .stat-icon { width: 36px; height: 36px; font-size: 18px; }
  .stat-value { font-size: 20px; }
}

/* The big breakpoint — phones */
@media (max-width: 768px) {
  /* Single-column stacked grid */
  .app-shell {
    grid-template-areas:
      "header"
      "main"
      "footer";
    grid-template-columns: 1fr;
    grid-template-rows: 56px 1fr 40px;
  }
  .app-sidebar {
    position: fixed;
    top: 56px; left: 0; bottom: 0;
    width: 240px;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    z-index: 20;
    box-shadow: 2px 0 8px rgba(0,0,0,0.15);
  }
  /* When the toggle is checked, slide the sidebar in */
  .nav-toggle:checked ~ .app-shell .app-sidebar {
    transform: translateX(0);
  }
  /* Show hamburger in header on mobile */
  .hamburger { display: inline-flex; }

  /* Backdrop overlay when drawer is open */
  .nav-toggle:checked ~ .app-shell::before {
    content: "";
    position: fixed; inset: 56px 0 0 0;
    background: rgba(0,0,0,0.4); z-index: 15;
  }

  /* Tighter padding in main content */
  .app-main { padding: 16px; }
  .page-header h1 { font-size: 18px; }

  /* Filter bars stack vertically */
  .filter-bar { flex-direction: column; align-items: stretch; }
  .filter-bar > div { width: 100%; }
  .filter-bar input, .filter-bar select { max-width: 100%; }

  /* Toolbar wraps */
  .toolbar { flex-direction: column; align-items: stretch; }
  .toolbar a, .toolbar form { width: 100%; }

  /* Forms go full width */
  input[type="text"], input[type="number"], input[type="password"],
  input[type="email"], input[type="date"], input[type="search"],
  select, textarea {
    max-width: 100%;
  }

  /* APK card grid auto-collapses (auto-fill minmax already does this), but tighten gap */
  .apk-grid { gap: 12px; }

  /* Hero stats stack 2-up on small screens */
  .stats-grid { grid-template-columns: repeat(2, 1fr); gap: 12px; }

  /* User chip on header — hide name, keep avatar */
  .user-chip span:not(.user-avatar) { display: none; }

  /* Logout button text shorten — already short */
  .logout-btn { padding: 6px 8px; font-size: 11px; }

  /* Two-col layout collapses (already had a 900px breakpoint, lower it) */
  .two-col { grid-template-columns: 1fr; }

  /* Login card full-width on phone */
  .login-card { padding: 24px; max-width: 100%; }
}

/* Very narrow phones (≤375px) */
@media (max-width: 380px) {
  .stats-grid { grid-template-columns: 1fr; }
  .brand-name { display: none; }
}

/* Tablet sweet spot — sidebar visible but compressed */
@media (min-width: 769px) and (max-width: 1024px) {
  .app-shell { grid-template-columns: 180px 1fr; }
  .sidenav-item { padding: 8px 14px; font-size: 13px; }
  .app-main { padding: 20px; }
}

/* Print: drop the sidebar+header so reports print cleanly */
@media print {
  .app-header, .app-sidebar, .app-footer, .toolbar, .filter-bar,
  .pagination, .actions, .hamburger, .logout-form { display: none !important; }
  .app-shell { display: block; }
  .app-main { padding: 0; }
  body { background: #fff; }
}

.stack-trace { max-height: 400px; overflow-y: auto; font-size: 11px; line-height: 1.45; }

/* Fleet Map + per-terminal map (T15) */
.map-legend { padding: 12px; font-size: 13px; color: #4b5563; }
.legend-pin { font-size: 18px; vertical-align: middle; margin-right: 4px; }
.legend-pin.red { color: #dc2626; }
.legend-pin.gray { color: #6b7280; }
.legend-pin.yellow { color: #eab308; }
.legend-pin.green { color: #16a34a; }
.muted { color: #6b7280; }

/* APK targeted rollout — picker rows (T12) */
.candidate-row { padding: 4px 0; border-bottom: 1px solid #f3f4f6; }
.candidate-row:last-child { border-bottom: none; }
.candidate-row label { cursor: pointer; }
