/* ─── Reset & Base ──────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --sidebar-w: 240px;
  --radius: 8px;

  /* Dark theme (default) */
  --bg: #0f1117;
  --bg-card: #1a1d27;
  --bg-hover: #242836;
  --border: #2a2e3a;
  --text: #e4e4e7;
  --text-muted: #8a8a94;
  --focus-ring: #60a5fa;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --success: #22c55e;
  --warning: #f59e0b;
  --warning-text: var(--warning);
  --danger: #ef4444;
  --accent: #8b5cf6;
}

/* Light theme override — deliberately re-tuned rather than a straight
   invert: dark-mode's pastel accent shades (e.g. alert text, badge
   colors) don't have enough contrast against a white background. */
:root[data-theme="light"] {
  --bg: #f3f4f7;
  --bg-card: #ffffff;
  --bg-hover: #eef0f4;
  --border: #dde1e8;
  --text: #1a1d27;
  --text-muted: #5b5f6b;
  --focus-ring: #1d4ed8;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --success: #15803d;
  --warning-text: #b45309;
  --danger: #b91c1c;
  --accent: #6d28d9;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  min-height: 100vh;
  line-height: 1.5;
  font-size: 14px;
}

a { color: var(--primary); }
a.link { text-decoration: underline; }

.link-btn {
  background: none;
  border: none;
  padding: 0;
  margin: 0;
  font: inherit;
  font-weight: 600;
  color: var(--primary);
  text-decoration: underline;
  cursor: pointer;
  text-align: left;
}
.link-btn:hover { color: var(--primary-hover); }

/* ─── Keyboard Focus ────────────────────────────────────────
   Only shown for keyboard navigation (:focus-visible), not mouse
   clicks — buttons/links/checkboxes/table rows had no visible focus
   indicator at all before this. */
a:focus-visible,
button:focus-visible,
.btn:focus-visible,
.nav-link:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
[tabindex]:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

/* ─── Sidebar ───────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-w);
  background: var(--bg-card);
  border-right: 1px solid var(--border);
  position: fixed;
  top: 0; left: 0; bottom: 0;
  display: flex;
  flex-direction: column;
  z-index: 100;
}

.sidebar-header {
  padding: 24px 20px;
  border-bottom: 1px solid var(--border);
}

.sidebar-header h2 { font-size: 20px; font-weight: 700; color: var(--primary); }
.sidebar-header .subtitle { font-size: 12px; color: var(--text-muted); margin-top: 2px; display: block; }

.nav-links { list-style: none; padding: 12px 8px; flex: 1; }

.nav-link {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  color: var(--text-muted);
  text-decoration: none;
  border-radius: var(--radius);
  font-size: 14px;
  transition: all 0.15s;
  margin-bottom: 2px;
}

.nav-link:hover, .nav-link.active { background: var(--bg-hover); color: var(--text); }
.nav-link .icon { font-size: 16px; width: 20px; text-align: center; }

.sidebar-footer { padding: 16px 20px; border-top: 1px solid var(--border); }

.theme-toggle-btn { width: 100%; justify-content: center; margin-bottom: 10px; }

.sidebar-user {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 10px;
  gap: 8px;
}
.sidebar-user span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-user a { flex-shrink: 0; }
.sidebar-user a:hover { color: var(--text); }

/* ─── Main Content ──────────────────────────────────────── */
.content {
  margin-left: var(--sidebar-w);
  flex: 1;
  padding: 32px;
  min-height: 100vh;
  max-width: 100%;
  overflow-x: hidden;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  flex-wrap: wrap;
  gap: 12px;
}

.page-header h1 { font-size: 24px; font-weight: 700; }
.header-actions { display: flex; gap: 8px; }

/* ─── Cards ─────────────────────────────────────────────── */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  gap: 12px;
}

.card-header h3 { font-size: 16px; font-weight: 600; white-space: nowrap; }
.card-body { padding: 20px; }

/* ─── Stats Grid ────────────────────────────────────────── */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.stats-small { margin-bottom: 0; }

.stat-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  text-align: center;
}

.stat-card.accent { border-color: var(--accent); }
.stat-value { font-size: 32px; font-weight: 700; color: var(--primary); }
.stat-card.accent .stat-value { color: var(--accent); }
.stat-label { font-size: 13px; color: var(--text-muted); margin-top: 4px; }

/* ─── Grid Layouts ──────────────────────────────────────── */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }

.mapping-layout {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* ─── Tables ────────────────────────────────────────────── */
.table-scroll { max-height: 500px; overflow-y: auto; padding: 0; }

/* Mappings' two stacked panels are the only content on the page, so each
   one's box can safely take a generous chunk of the window's height
   instead of stopping at a fixed height — grows/shrinks as the window is
   resized. Scoped here rather than in .table-scroll itself, since other
   pages stack several sections that need normal page scrolling instead. */
.mapping-panel .table-scroll {
  max-height: calc(50vh - 100px);
  min-height: 300px;
}

/* Collapsible panel title -- click anywhere on it to hide/show the body
   below, without disturbing the filter controls that share the header.
   .mapping-panel scopes the Mappings page's own panels; .collapsible-header
   is the same behaviour for any other page's card (e.g. Tariffs). */
.mapping-panel .card-header h3,
.card-header h3.collapsible-header {
  cursor: pointer;
  user-select: none;
}
.mapping-panel .card-header h3:hover,
.card-header h3.collapsible-header:hover { color: var(--primary); }
.panel-collapse-arrow {
  display: inline-block;
  transition: transform 0.15s ease;
  margin-right: 4px;
}
.panel-collapse-arrow.collapsed { transform: rotate(-90deg); }

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.data-table th {
  text-align: left;
  padding: 10px 12px;
  font-weight: 600;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 1;
}

.data-table td {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

.sortable-th { padding: 6px 12px 8px !important; }

.th-sort-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  width: 100%;
  background: none;
  border: none;
  padding: 0;
  margin: 0 0 4px;
  font: inherit;
  color: inherit;
  text-transform: inherit;
  letter-spacing: inherit;
  cursor: pointer;
  text-align: left;
}

.th-sort-btn:hover { color: var(--text); }
.sort-arrow { font-size: 9px; min-width: 8px; }

.th-filter {
  width: 100%;
  padding: 3px 6px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  font-size: 11px;
  font-weight: 400;
  text-transform: none;
  letter-spacing: normal;
  outline: none;
}

.th-filter:focus { border-color: var(--primary); }

.data-table tbody tr:hover { background: var(--bg-hover); }
.data-table .row-selected { background: rgba(59, 130, 246, 0.15) !important; }
.data-table .row-mapped { background: rgba(34, 197, 94, 0.05); }
.data-table .row-changed { background: rgba(245, 158, 11, 0.08); }

.actions-cell { white-space: nowrap; }
.actions-cell .btn { margin-right: 4px; }

/* ─── Buttons ───────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
  background: var(--bg-hover);
  color: var(--text);
  text-decoration: none;
  white-space: nowrap;
}

.btn:hover { background: var(--border); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-primary { background: var(--primary); border-color: var(--primary); color: white; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }

.btn-danger { background: var(--danger); border-color: var(--danger); color: white; }
.btn-danger:hover:not(:disabled) { background: #dc2626; }

.btn-outline { background: transparent; border-color: var(--border); color: var(--text); }
.btn-outline:hover { background: var(--bg-hover); }

.btn-sm { padding: 4px 10px; font-size: 12px; }

/* ─── Badges ────────────────────────────────────────────── */
.badge {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
}

.badge-green { background: rgba(34, 197, 94, 0.15); color: var(--success); }
.badge-red { background: rgba(239, 68, 68, 0.15); color: var(--danger); }
.badge-yellow { background: rgba(245, 158, 11, 0.15); color: var(--warning-text); }
.badge-blue { background: rgba(59, 130, 246, 0.15); color: var(--primary); }
.badge-grey { background: rgba(113, 113, 122, 0.15); color: var(--text-muted); }

:root[data-theme="light"] .badge-green { background: rgba(21, 128, 61, 0.12); }
:root[data-theme="light"] .badge-red { background: rgba(185, 28, 28, 0.12); }
:root[data-theme="light"] .badge-yellow { background: rgba(180, 83, 9, 0.12); }
:root[data-theme="light"] .badge-blue { background: rgba(37, 99, 235, 0.12); }
:root[data-theme="light"] .badge-grey { background: rgba(91, 95, 107, 0.12); }

/* Status badges (success/danger/warning) also carry a glyph, not just a
   color, so they still read correctly for colorblind users. Informational
   badges (blue/grey) are labels rather than pass/fail signals, so they're
   left plain. */
.badge-green::before { content: '\2713\a0'; }
.badge-red::before { content: '\2715\a0'; }
.badge-yellow::before { content: '\26A0\a0'; }

/* ─── Forms ─────────────────────────────────────────────── */
.form-group { margin-bottom: 16px; }
.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.form-input, .form-select, .search-input {
  width: 100%;
  padding: 8px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 13px;
  outline: none;
  transition: border-color 0.15s;
}

.form-input:focus, .form-select:focus, .search-input:focus { border-color: var(--primary); }
.search-input { max-width: 220px; }
.form-select { cursor: pointer; appearance: auto; }

/* The native calendar icon defaults to dark-on-dark and is barely visible
   in dark mode — invert it and give it a bigger, obviously-clickable
   target. Light mode already gets a visible dark-on-white icon by
   default, so this is scoped to dark only. */
:root[data-theme="dark"] input[type="date"]::-webkit-calendar-picker-indicator {
  filter: invert(1);
  opacity: 0.75;
  cursor: pointer;
  padding: 4px;
  margin-right: -4px;
  border-radius: 4px;
}
:root[data-theme="dark"] input[type="date"]::-webkit-calendar-picker-indicator:hover {
  opacity: 1;
  background: rgba(255, 255, 255, 0.1);
}
input[type="date"]::-webkit-calendar-picker-indicator {
  cursor: pointer;
  padding: 4px;
  margin-right: -4px;
  border-radius: 4px;
}

/* ─── Alerts ────────────────────────────────────────────── */
.alert {
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 13px;
  line-height: 1.6;
}

.alert-info { background: rgba(59, 130, 246, 0.1); border: 1px solid rgba(59, 130, 246, 0.3); color: #93c5fd; }
.alert-success { background: rgba(34, 197, 94, 0.1); border: 1px solid rgba(34, 197, 94, 0.3); color: #86efac; }
.alert-danger { background: rgba(239, 68, 68, 0.1); border: 1px solid rgba(239, 68, 68, 0.3); color: #fca5a5; }
.alert-warning { background: rgba(245, 158, 11, 0.1); border: 1px solid rgba(245, 158, 11, 0.3); color: #fcd34d; }
.alert ul { margin: 8px 0 0 20px; }
.alert li { margin-bottom: 4px; }

/* The pastel text colors above are tuned for a dark background and
   are barely legible on white. */
:root[data-theme="light"] .alert-info { background: rgba(37, 99, 235, 0.08); border-color: rgba(37, 99, 235, 0.35); color: #1d4ed8; }
:root[data-theme="light"] .alert-success { background: rgba(21, 128, 61, 0.08); border-color: rgba(21, 128, 61, 0.35); color: #15803d; }
:root[data-theme="light"] .alert-danger { background: rgba(185, 28, 28, 0.08); border-color: rgba(185, 28, 28, 0.35); color: #b91c1c; }
:root[data-theme="light"] .alert-warning { background: rgba(180, 83, 9, 0.08); border-color: rgba(180, 83, 9, 0.35); color: #b45309; }

/* ─── Modal ─────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-overlay.hidden { display: none; }

.modal {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  width: 90%;
  max-width: 640px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}

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

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.modal-header h3 { font-size: 16px; }

.modal-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 24px;
  cursor: pointer;
  padding: 0 4px;
}

.modal-body { padding: 20px; overflow-y: auto; flex: 1; }
.modal-body .data-table { font-size: 12px; }
.modal-body .data-table.svc-table td { vertical-align: top; padding-top: 10px; }

.modal-footer {
  padding: 16px 20px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  flex-shrink: 0;
}

/* ─── Toast ─────────────────────────────────────────────── */
#toast-container {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 2000;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  position: relative;
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 12px 36px 12px 20px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  animation: slideIn 0.3s ease;
  max-width: 420px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.toast-success { background: var(--success); color: white; }
.toast-error { background: var(--danger); color: white; }
.toast-info { background: var(--primary); color: white; }
.toast-warning { background: var(--warning); color: #1a1a1a; }

.toast-close {
  position: absolute;
  top: 6px;
  right: 6px;
  background: none;
  border: none;
  color: inherit;
  opacity: 0.7;
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  padding: 4px;
}

.toast-close:hover { opacity: 1; }

@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

/* ─── Connection Status ─────────────────────────────────── */
.connection-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}

.connection-row:last-child { border-bottom: none; }

/* ─── Activity Rows ─────────────────────────────────────── */
.activity-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}

.activity-row:last-child { border-bottom: none; }
.activity-detail { flex: 1; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.activity-time { color: var(--text-muted); font-size: 12px; white-space: nowrap; }

/* ─── Steps / Guide ─────────────────────────────────────── */
.steps { display: flex; flex-direction: column; gap: 16px; }

.step {
  display: flex;
  align-items: flex-start;
  gap: 16px;
}

.step-num {
  width: 32px;
  height: 32px;
  background: var(--primary);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 14px;
  flex-shrink: 0;
  color: white;
}

.step p { margin-top: 2px; font-size: 13px; }

/* ─── Config Guide ──────────────────────────────────────── */
.config-guide h4 { font-size: 15px; font-weight: 600; margin-bottom: 8px; }
.config-guide p { font-size: 13px; color: var(--text-muted); margin-bottom: 8px; }
.config-guide ol { margin-left: 20px; color: var(--text-muted); font-size: 13px; }
.config-guide ol li { margin-bottom: 6px; }

pre {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px 16px;
  font-size: 12px;
  overflow-x: auto;
  color: var(--text-muted);
  margin-bottom: 8px;
}

/* ─── Input with toggle (show/hide password) ──────────── */
.input-with-toggle {
  position: relative;
  display: flex;
}

.input-with-toggle .form-input {
  padding-right: 42px;
}

.btn-toggle {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 16px;
  padding: 4px 8px;
  border-radius: 4px;
  line-height: 1;
}

.btn-toggle:hover { color: var(--text); background: var(--bg-hover); }

/* ─── Button rows ──────────────────────────────────────── */
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }

/* ─── Utilities ─────────────────────────────────────────── */
.text-center { text-align: center; }
.text-muted { color: var(--text-muted); }
.text-danger { color: var(--danger); }
.hidden { display: none !important; }
.mt-2 { margin-top: 8px; }
.mt-4 { margin-top: 16px; }
.mb-4 { margin-bottom: 16px; }
code { background: var(--bg); padding: 2px 6px; border-radius: 4px; font-size: 12px; }

/* ─── Responsive ────────────────────────────────────────── */
@media (max-width: 1200px) {
  .grid-2 { grid-template-columns: 1fr; }
}

@media (max-width: 768px) {
  .sidebar { width: 60px; }
  .sidebar-header h2, .sidebar-header .subtitle, .nav-text, .sidebar-footer { display: none; }
  .content { margin-left: 60px; padding: 16px; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .page-header { flex-direction: column; align-items: flex-start; }
}
