/* ==========================================================================
   TWI Sandbox Dashboard — Shared Styles
   Theme customizable via branding.json (injected as CSS variables)
   ========================================================================== */

:root {
  --bg-start: #0f1419;
  --bg-mid: #1a2130;
  --bg-accent: #252d3e;
  --bg-input: #0f1419;
  --border: #2a3142;
  --text: #e6e6e6;
  --text-muted: #8a94a6;
  --text-hint: #a0a8b8;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --success: #4ade80;
  --success-bg: #0f3d23;
  --warning: #fbbf24;
  --warning-bg: #3d3a0f;
  --danger: #f87171;
  --danger-bg: #3d0f0f;
  --info: #93c5fd;
  --orange: #fb923c;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
               Oxygen-Sans, Ubuntu, Cantarell, sans-serif;
  background: var(--bg-start);
  color: var(--text);
  margin: 0;
  padding: 20px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ===== Topbar / Header ===== */
.topbar {
  max-width: 1400px;
  margin: 0 auto 15px;
  display: flex;
  align-items: center;
  gap: 15px;
  flex-wrap: wrap;
}
.topbar .brand {
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  color: var(--text);
}
.topbar .brand img {
  width: 36px;
  height: 36px;
  display: block;
}
.topbar .brand-text { display: flex; flex-direction: column; line-height: 1.1; }
.topbar .brand-name { font-size: 17px; font-weight: 700; }
.topbar .brand-sub { font-size: 11px; color: var(--text-muted); }
.topbar nav {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-left: auto;
  align-items: center;
}
.topbar nav a, .topbar nav button {
  padding: 6px 12px;
  background: var(--bg-mid);
  border-radius: 5px;
  text-decoration: none;
  color: var(--info);
  font-size: 13px;
  border: none;
  cursor: pointer;
}
.topbar nav a:hover { background: var(--bg-accent); }
.topbar .user-info {
  color: var(--text-muted);
  font-size: 12px;
  font-family: monospace;
}
.topbar nav .logout {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
}
.topbar nav .logout:hover { color: var(--danger); border-color: var(--danger); }

/* ===== Container ===== */
.container { max-width: 1400px; margin: 0 auto; }

/* ===== Headings ===== */
h1 { margin: 0 0 20px 0; font-size: 22px; display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
h2 { margin: 0; font-size: 16px; }
.back { color: var(--info); text-decoration: none; font-size: 13px; }
.back:hover { text-decoration: underline; }

/* ===== Buttons ===== */
.btn {
  background: var(--accent);
  color: white;
  border: none;
  padding: 8px 14px;
  border-radius: 5px;
  font-size: 13px;
  cursor: pointer;
  text-decoration: none;
  display: inline-block;
  min-height: 36px;
  line-height: 1.4;
  font-family: inherit;
}
.btn:hover:not(:disabled) { background: var(--accent-hover); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-danger { background: #dc2626; }
.btn-danger:hover:not(:disabled) { background: #b91c1c; }
.btn-warn { background: #d97706; }
.btn-warn:hover:not(:disabled) { background: #b45309; }
.btn-sm { padding: 4px 10px; font-size: 12px; min-height: 28px; }
.btn-lg { padding: 12px 18px; font-size: 14px; min-height: 44px; }

/* ===== Forms ===== */
input[type="text"], input[type="email"], input[type="password"],
input[type="number"], select, textarea {
  padding: 10px 12px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 5px;
  font-size: 14px;
  font-family: inherit;
  min-height: 40px;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
}
@supports (-webkit-touch-callout: none) {
  /* iOS Safari: prevent zoom on input focus */
  input, select, textarea { font-size: 16px; }
}

input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--accent); cursor: pointer; }

/* ===== Flash messages ===== */
.flash {
  padding: 10px 14px;
  border-radius: 5px;
  margin-bottom: 10px;
  font-size: 14px;
  border-left: 3px solid;
}
.flash.success { background: var(--success-bg); color: var(--success); border-color: var(--success); }
.flash.danger  { background: var(--danger-bg);  color: var(--danger);  border-color: var(--danger); }
.flash.warning { background: var(--warning-bg); color: var(--warning); border-color: var(--warning); }

/* ===== Cards ===== */
.card {
  background: var(--bg-mid);
  border-radius: 6px;
  padding: 18px;
  margin-bottom: 15px;
}
.card h2 {
  margin: 0 0 12px 0;
  font-size: 13px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.card-danger { border-left: 3px solid #dc2626; }

/* ===== Tables ===== */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table {
  width: 100%;
  background: var(--bg-mid);
  border-collapse: collapse;
  border-radius: 6px;
  overflow: hidden;
}
th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); }
th { background: var(--bg-accent); font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--text-muted); white-space: nowrap; }
tr:last-child td { border-bottom: none; }
tr:hover td { background: #202738; }
tr.selected td { background: #1e3a5f; }

/* ===== Badges ===== */
.http-badge, .mw-badge, .role-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 600;
  font-family: monospace;
  transition: background-color 0.2s;
}
.http-200 { background: var(--success-bg); color: var(--success); }
.http-3xx { background: var(--warning-bg); color: var(--warning); }
.http-4xx { background: #3d1f0f; color: var(--orange); }
.http-5xx { background: var(--danger-bg); color: var(--danger); }
.http-0   { background: var(--border); color: var(--text-muted); }
.mw-ok     { background: var(--success-bg); color: var(--success); }
.mw-warn   { background: var(--warning-bg); color: var(--warning); }
.mw-danger { background: var(--danger-bg); color: var(--danger); }
.mw-unknown{ background: var(--border); color: var(--text-muted); }
.role-administrator { background: var(--danger-bg); color: var(--danger); }
.role-editor { background: var(--warning-bg); color: var(--warning); }
.role-author { background: #0f2a3d; color: #60a5fa; }
.role-contributor { background: #1a3d0f; color: #84cc16; }
.role-subscriber { background: var(--border); color: var(--text-muted); }

/* ===== Pagination ===== */
.pagination { display: flex; gap: 5px; margin: 20px 0; flex-wrap: wrap; }
.pagination a, .pagination span {
  padding: 6px 11px;
  background: var(--bg-mid);
  border-radius: 4px;
  text-decoration: none;
  color: var(--text);
  font-size: 13px;
  min-height: 36px;
  display: inline-flex;
  align-items: center;
}
.pagination a:hover { background: var(--bg-accent); }
.pagination .current { background: var(--accent); color: white; }

/* ===== Helpers ===== */
.muted { color: var(--text-muted); font-size: 12px; }
.domain-link { color: var(--info); text-decoration: none; }
.domain-link:hover { text-decoration: underline; }

/* ===== Modal ===== */
#modal-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  z-index: 200;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
#modal-backdrop.visible { display: flex; }
.modal {
  background: var(--bg-mid);
  border-radius: 8px;
  padding: 22px;
  max-width: 500px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
}

/* ===== Full-screen Loading Overlay (used for long-running reset operations) ===== */
#loading-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(10, 14, 20, 0.88);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  z-index: 9999;
  align-items: center;
  justify-content: center;
  padding: 20px;
  animation: overlay-fade-in 0.18s ease-out;
}
#loading-overlay.visible { display: flex; }
@keyframes overlay-fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes spin { to { transform: rotate(360deg); } }

.loading-card {
  background: var(--bg-mid);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 28px 32px;
  max-width: 480px;
  width: 100%;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,0.6);
}
.loading-spinner-lg {
  width: 56px; height: 56px;
  border: 4px solid var(--border);
  border-top-color: var(--warning);
  border-radius: 50%;
  margin: 0 auto 18px;
  animation: spin 0.9s linear infinite;
}
.loading-title {
  font-size: 17px;
  font-weight: 600;
  color: var(--warning);
  margin-bottom: 6px;
  min-height: 24px;
}
.loading-subtitle {
  color: var(--text-muted);
  font-size: 13px;
  margin-bottom: 16px;
  min-height: 18px;
}
.loading-timer {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
  color: var(--text-hint);
  background: var(--bg-input);
  padding: 7px 14px;
  border-radius: 6px;
  display: inline-block;
  margin-bottom: 14px;
}
.loading-timer .elapsed { color: var(--success); font-weight: 600; }
.loading-timer .estimate { color: var(--text-muted); }
.loading-timer .separator { color: var(--border); margin: 0 6px; }
.loading-progress {
  height: 6px;
  background: var(--bg-input);
  border-radius: 3px;
  overflow: hidden;
  margin-bottom: 14px;
}
.loading-progress-bar {
  height: 100%;
  background: linear-gradient(90deg, var(--warning), var(--orange));
  width: 0%;
  border-radius: 3px;
  /* CSS animation drives the fill — works on mobile even when JS timers are
   * paused during form-submit navigation. --fill-duration is set inline. */
  animation-name: fill-progress;
  animation-duration: var(--fill-duration, 3s);
  animation-timing-function: linear;
  animation-fill-mode: forwards;
  animation-play-state: paused;
}
.loading-progress-bar.running { animation-play-state: running; }
@keyframes fill-progress {
  0%   { width: 0%; }
  100% { width: 95%; }
}
.loading-progress-bar.stalled {
  background: linear-gradient(90deg, var(--orange), var(--danger));
  animation: stall-pulse 1.4s ease-in-out infinite;
  width: 95%;
}
@keyframes stall-pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.55; } }

/* Pure-CSS "ticking" indicator — animated dot that keeps breathing even
 * when JS timers are paused during mobile form-submit navigation. */
.loading-pulse {
  display: inline-block;
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--success);
  margin-right: 6px;
  vertical-align: 0;
  animation: pulse-dot 1s ease-in-out infinite;
}
@keyframes pulse-dot {
  0%, 100% { opacity: 1; transform: scale(1); }
  50%      { opacity: 0.35; transform: scale(0.7); }
}
.loading-sites {
  max-height: 140px;
  overflow-y: auto;
  background: var(--bg-input);
  padding: 8px 10px;
  border-radius: 6px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  color: var(--text-muted);
  text-align: left;
  margin-bottom: 12px;
}
.loading-sites div { padding: 1px 0; }
.loading-dots::after {
  display: inline-block;
  content: '';
  animation: dots 1.3s steps(4, end) infinite;
  width: 1em;
  text-align: left;
}
@keyframes dots {
  0%, 20% { content: ''; }
  40% { content: '.'; }
  60% { content: '..'; }
  80%, 100% { content: '...'; }
}
.loading-hint {
  font-size: 11px;
  color: var(--text-muted);
  font-style: italic;
}
.loading-hint strong { color: var(--warning); font-style: normal; }

/* Inline button spinner (smaller, for simple button-level feedback) */
.btn-spinner {
  display: inline-block;
  width: 12px; height: 12px;
  border: 2px solid rgba(255,255,255,0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  vertical-align: -2px;
  margin-right: 6px;
}
.btn.loading { opacity: 0.85; cursor: wait; pointer-events: none; }

/* ===== Credentials flash (shown after Reset Password / Add Site) =========== */
.creds-flash { font-size: 14px; }
.creds-flash .creds-title { font-size: 15px; margin-bottom: 10px; }
.creds-flash .creds-grid {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 6px 10px;
  align-items: center;
  background: rgba(0,0,0,0.25);
  border-radius: 6px;
  padding: 10px 12px;
  margin-bottom: 8px;
}
.creds-flash .creds-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-muted);
  font-weight: 600;
}
.creds-flash .creds-val {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 14px;
  color: var(--text);
  background: var(--bg-input);
  padding: 5px 9px;
  border-radius: 4px;
  user-select: all;         /* triple-click / double-click selects cleanly */
  overflow-x: auto;
  white-space: nowrap;
}
.creds-flash .creds-copy {
  background: var(--bg-accent);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 4px 10px;
  font-size: 11px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  min-height: 28px;
}
.creds-flash .creds-copy:hover { background: var(--accent); color: #fff; border-color: var(--accent); }
.creds-flash .creds-copy.copied { background: var(--success-bg); color: var(--success); border-color: var(--success); }
.creds-flash .creds-foot { font-size: 12px; color: var(--text-muted); }
.creds-flash .creds-foot a { color: var(--info); text-decoration: none; }
.creds-flash .creds-foot a:hover { text-decoration: underline; }

@media (max-width: 520px) {
  .creds-flash .creds-grid { grid-template-columns: auto 1fr; }
  .creds-flash .creds-copy { grid-column: 1 / -1; justify-self: start; }
}

/* ===== Statusbar ===== */
.statusbar {
  background: var(--bg-mid);
  border-radius: 6px;
  padding: 13px 18px;
  margin-bottom: 15px;
  display: flex;
  gap: 25px;
  align-items: center;
  flex-wrap: wrap;
}
.stat { display: flex; flex-direction: column; }
.stat-label { font-size: 10px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; }
.stat-value { font-size: 17px; font-weight: 600; margin-top: 2px; }
.stat-value.ok { color: var(--success); }
.stat-value.bad { color: var(--danger); }
.stat-monitor { font-size: 12px; color: var(--text-hint); margin-top: 3px; font-family: monospace; }

/* ===== Mobile responsive ===== */
@media (max-width: 768px) {
  /* Prevent any horizontal overflow globally */
  html, body {
    overflow-x: hidden;
    max-width: 100%;
  }
  .container, form, .topbar, .statusbar, .card, .table-wrap {
    max-width: 100%;
  }

  body { padding: 8px 8px 80px 8px; }  /* extra bottom padding for bulk-bar */

  /* Sticky topbar — always visible while scrolling */
  .topbar {
    position: sticky;
    top: -1px;
    z-index: 50;
    background: var(--bg-start);
    margin: -8px -8px 10px -8px;
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
    gap: 8px;
  }
  .topbar .brand img { width: 28px; height: 28px; }
  .topbar .brand-name { font-size: 14px; }
  .topbar .brand-sub { display: none; }
  .topbar nav { margin-left: auto; gap: 4px; }
  .topbar nav a, .topbar nav button { padding: 5px 8px; font-size: 11px; }
  .topbar nav a span, .topbar nav .nav-label { display: none; }  /* emoji-only on mobile */
  .topbar .user-info { display: none; }   /* hide username — shown on tap */

  h1 { font-size: 17px; margin-bottom: 12px; }

  /* Compact statusbar */
  .statusbar {
    gap: 10px 18px;
    padding: 8px 12px;
    margin-bottom: 10px;
    font-size: 12px;
  }
  .stat-label { font-size: 9px; }
  .stat-value { font-size: 14px; }
  .stat-monitor { font-size: 11px; }

  .card { padding: 12px; margin-bottom: 10px; }
  .card h2 { font-size: 12px; }

  /* ===== Tables → Cards pattern (no horizontal scroll!) ===== */
  .table-wrap { overflow: visible; margin: 0; padding: 0; width: 100%; }
  table { background: transparent; border-radius: 0; width: 100%; }
  table thead { display: none; }
  table, table tbody { display: block; width: 100%; }
  table tr {
    display: grid;
    grid-template-columns: 1fr auto auto;
    gap: 4px 10px;
    background: var(--bg-mid);
    border-radius: 6px;
    padding: 10px 14px;
    margin-bottom: 8px;
    border-bottom: none;
    align-items: center;
    max-width: 100%;
    overflow: hidden;
    box-sizing: border-box;
  }
  table td { min-width: 0; }   /* grid children can shrink below content size */
  table tr.selected {
    outline: 2px solid var(--accent);
    background: #1e3a5f;
  }
  table tr:hover td { background: transparent; }
  table td {
    display: block;
    border: none;
    padding: 0;
    font-size: 13px;
  }
  /* Show the pseudo-label from data-label if provided */
  table td[data-label]::before {
    content: attr(data-label) ": ";
    color: var(--text-muted);
    font-size: 11px;
    text-transform: uppercase;
    margin-right: 4px;
  }

  /* Index page: specific card layout — site/http/malware/php/mod/actions */
  #sites-tbody tr {
    grid-template-areas:
      "check  site     http"
      "meta   meta     malware"
      "actions actions actions";
    grid-template-columns: auto 1fr auto;
    gap: 6px 10px;
  }
  #sites-tbody tr > td:nth-child(1) { grid-area: check; }
  #sites-tbody tr > td:nth-child(2) {
    grid-area: site;
    font-weight: 600;
    font-size: 14px;
    min-width: 0;
    overflow: hidden;
  }
  #sites-tbody tr > td:nth-child(2) a {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 100%;
  }
  #sites-tbody tr > td:nth-child(3) { grid-area: http; justify-self: end; }
  #sites-tbody tr > td:nth-child(4) { grid-area: malware; justify-self: end; }
  #sites-tbody tr > td:nth-child(5),
  #sites-tbody tr > td:nth-child(6) {
    grid-area: meta;
    display: inline-block;
    color: var(--text-muted);
    font-size: 11px;
    margin-right: 10px;
  }
  #sites-tbody tr > td:nth-child(5)::before { content: ""; }
  #sites-tbody tr > td:nth-child(6)::before { content: " · "; color: var(--border); }
  #sites-tbody tr > td:nth-child(7) {
    grid-area: actions;
    display: flex;
    gap: 6px;
    margin-top: 4px;
  }
  #sites-tbody tr > td:nth-child(7) .btn {
    flex: 1;
    text-align: center;
    padding: 7px 10px;
    min-height: 34px;
    font-size: 12px;
  }

  /* Audit table: hide low-priority cols, make more compact */
  #audit-table td:nth-child(1) { /* ts */
    font-size: 10px;
    color: var(--text-muted);
  }
  #audit-table td:nth-child(6) { /* detail — can be very long */
    font-size: 10px;
    white-space: normal;
    word-break: break-all;
  }

  /* Forms */
  .add-form, .filter-bar { flex-direction: column; align-items: stretch; gap: 8px; padding: 10px 12px; }
  .add-form input, .filter-bar input, .filter-bar select { width: 100%; }

  /* Bulk bar at bottom: full width */
  #bulk-bar {
    left: 8px !important;
    right: 8px !important;
    bottom: 8px;
    transform: translateY(140px) !important;
    width: calc(100% - 16px);
    flex-wrap: wrap;
    padding: 10px 14px;
    gap: 8px;
  }
  #bulk-bar.visible { transform: translateY(0) !important; }
  #bulk-bar .domains { max-width: 100%; flex-basis: 100%; order: 99; font-size: 11px; }
  #bulk-bar .count { font-size: 14px; }

  /* Modals fullwidth */
  .modal { padding: 18px; max-height: 85vh; border-radius: 10px 10px 0 0; }

  /* Pagination */
  .pagination { gap: 3px; justify-content: center; }
  .pagination a, .pagination span { padding: 6px 10px; font-size: 12px; min-height: 36px; }

  /* Buttons bigger for touch */
  .btn { min-height: 40px; padding: 10px 14px; }
  .btn-sm { min-height: 32px; padding: 6px 10px; }

  /* User-per-site page: more compact */
  .form-grid {
    grid-template-columns: 1fr !important;
    gap: 8px !important;
  }
}

@media (max-width: 480px) {
  body { padding: 6px 6px 80px 6px; }
  .topbar { margin: -6px -6px 8px -6px; padding: 8px 10px; }
  h1 { font-size: 16px; }
  .card { padding: 10px; }
  #sites-tbody tr > td:nth-child(2) { font-size: 13px; }
  .btn { font-size: 13px; }
  .pagination a, .pagination span { padding: 5px 8px; font-size: 11px; min-height: 32px; }
}

/* ===== Print ===== */
@media print {
  .topbar nav, .pagination, #bulk-bar, .btn, form { display: none !important; }
}
