/* === Design tokens === */
:root {
  --bg-base:        #080c12;
  --bg-surface:     #0e1420;
  --bg-card:        #131926;
  --bg-hover:       #1a2235;
  --border:         #1e2d45;
  --border-bright:  #2a3f60;
  --accent:         #7eb3f7;
  --accent-dim:     #7eb3f720;
  --accent-hover:   #a5c8fb;
  --danger:         #ff4d6d;
  --warning:        #ffd166;
  --text-primary:   #e8edf5;
  --text-secondary: #8896b0;
  --text-muted:     #4a5a75;
  --font-ui:        'Sora', sans-serif;
  --font-mono:      'JetBrains Mono', monospace;
  --sidebar-w:      220px;
  --sidebar-collapsed-w: 56px;
  --topbar-h:       56px;
  --radius:         8px;
  --radius-lg:      14px;
  --transition:     0.18s ease;
}

/* === Reset and base === */
*, *::before, *::after { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg-base);
  color: var(--text-primary);
  font-family: var(--font-ui);
  font-size: 14px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

/* Subtle grid texture overlay on the entire page background */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  background-image:
    linear-gradient(var(--border) 1px, transparent 1px),
    linear-gradient(90deg, var(--border) 1px, transparent 1px);
  background-size: 40px 40px;
  opacity: 0.18;
  pointer-events: none;
  z-index: 0;
}

/* === Layout wrapper === */
.app-layout {
  display: flex;
  min-height: 100vh;
  position: relative;
  z-index: 1;
}

/* === Sidebar === */
.sidebar {
  width: var(--sidebar-w);
  height: 100vh;
  background: var(--bg-surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 100;
  transition: transform var(--transition), width var(--transition);
}

.sidebar-brand {
  height: var(--topbar-h);
  min-height: var(--topbar-h);
  padding: 0 12px 0 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.sidebar-logo-link {
  display: flex;
  align-items: center;
  text-decoration: none;
}

.sidebar-brand .brand-label {
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  color: var(--accent);
  display: block;
}

.sidebar-brand .brand-name {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
  display: block;
  margin-top: 2px;
}

/* Nav section label */
.nav-section-label {
  font-family: var(--font-mono);
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--text-muted);
  padding: 18px 20px 6px;
}

/* Nav links */
.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  padding-bottom: 12px;
}

.sidebar-nav::-webkit-scrollbar { width: 3px; }
.sidebar-nav::-webkit-scrollbar-track { background: transparent; }
.sidebar-nav::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.nav-link-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 20px;
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 13.5px;
  font-weight: 400;
  border-left: 3px solid transparent;
  transition: all var(--transition);
  position: relative;
}

.nav-link-item i {
  font-size: 15px;
  width: 18px;
  text-align: center;
  flex-shrink: 0;
}

.nav-link-item:hover {
  color: var(--text-primary);
  background: var(--bg-hover);
  border-left-color: var(--border-bright);
}

.nav-link-item.active {
  color: var(--accent);
  background: var(--accent-dim);
  border-left-color: var(--accent);
  font-weight: 500;
}

.nav-link-item.active i {
  filter: drop-shadow(0 0 6px var(--accent));
}


/* === Main content area === */
.main-content {
  margin-left: var(--sidebar-w);
  flex: 1;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  transition: margin-left var(--transition);
}

/* No sidebar present (anonymous users / auth pages) */
.main-content.no-sidebar {
  margin-left: 0;
}

/* Top bar */
.topbar {
  height: var(--topbar-h);
  min-height: var(--topbar-h);
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 28px;
  gap: 12px;
  position: sticky;
  top: 0;
  z-index: 50;
}

.topbar-title {
  font-family: var(--font-mono);
  font-size: 12px;
  font-weight: 500;
  letter-spacing: 0.08em;
  color: var(--text-secondary);
  text-transform: uppercase;
}

.topbar-title span {
  color: var(--accent);
}

.topbar-spacer { flex: 1; }

/* Page content padding */
.page-content {
  padding: 28px;
  flex: 1;
}

/* === Flash messages === */
.flash-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: 360px;
}

.flash-msg {
  background: var(--bg-card);
  border: 1px solid var(--border-bright);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius);
  padding: 12px 16px;
  font-size: 13px;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  gap: 10px;
  animation: slideIn 0.25s ease forwards;
  box-shadow: 0 8px 32px rgba(0,0,0,0.4);
}

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

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

.card-header {
  background: transparent;
  border-bottom: 1px solid var(--border);
  padding: 14px 20px;
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  gap: 8px;
}

.card-header i { color: var(--accent); font-size: 14px; }
.card-body { padding: 20px; }

/* === Tables === */
.table-dark-custom {
  width: 100%;
  border-collapse: collapse;
}

.table-dark-custom thead th {
  font-family: var(--font-mono);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  color: var(--text-muted);
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.table-dark-custom tbody tr {
  border-bottom: 1px solid var(--border);
  transition: background var(--transition);
}

.table-dark-custom tbody tr:last-child { border-bottom: none; }

.table-dark-custom tbody tr:hover { background: var(--bg-hover); }

.table-dark-custom tbody td {
  padding: 11px 14px;
  color: var(--text-primary);
  vertical-align: middle;
}

.table-dark-custom .mono {
  font-family: var(--font-mono);
  font-size: 13px;
}

.table-dark-custom .text-accent { color: var(--accent); }
.table-dark-custom .text-danger  { color: var(--danger) !important; }
.table-dark-custom .text-muted   { color: var(--text-muted) !important; }

/* Shared table utilities */
.clickable-row { cursor: pointer; }
.diff-pos { color: #00c9a7; }
.diff-neg { color: var(--danger); }

/* === Buttons === */
.btn-accent {
  background: var(--accent);
  color: #080c12;
  border: none;
  border-radius: var(--radius);
  padding: 8px 18px;
  font-family: var(--font-ui);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  text-decoration: none;
}

.btn-accent:hover {
  background: var(--accent-hover);
  box-shadow: 0 0 16px var(--accent-dim);
  color: #080c12;
}

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 18px;
  font-family: var(--font-ui);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  text-decoration: none;
}

.btn-ghost:hover {
  border-color: var(--border-bright);
  color: var(--text-primary);
  background: var(--bg-hover);
}

.btn-danger {
  background: transparent;
  color: var(--danger);
  border: 1px solid var(--danger)44;
  border-radius: var(--radius);
  padding: 8px 18px;
  font-family: var(--font-ui);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  text-decoration: none;
}

.btn-danger:hover {
  background: var(--danger)18;
  border-color: var(--danger);
}

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

/* === Form controls === */
.form-label {
  font-family: var(--font-mono);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-bottom: 6px;
  display: block;
}

.form-control,
.form-select {
  background: var(--bg-base) !important;
  border: 1px solid var(--border) !important;
  border-radius: var(--radius) !important;
  color: var(--text-primary) !important;
  font-family: var(--font-ui) !important;
  font-size: 13px !important;
  padding: 8px 12px !important;
  transition: border-color var(--transition), box-shadow var(--transition) !important;
  width: 100%;
}

.form-control:focus,
.form-select:focus {
  border-color: var(--accent) !important;
  box-shadow: 0 0 0 3px var(--accent-dim) !important;
  outline: none !important;
}

.form-control::placeholder { color: var(--text-muted) !important; }

/* Select arrow colour fix */
.form-select {
  background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3e%3cpath fill='none' stroke='%238896b0' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M2 5l6 6 6-6'/%3e%3c/svg%3e") !important;
  background-repeat: no-repeat !important;
  background-position: right 12px center !important;
  background-size: 14px !important;
  padding-right: 36px !important;
}

.form-group { margin-bottom: 16px; }

/* === Badges === */
.badge-accent {
  background: var(--accent-dim);
  color: var(--accent);
  border: 1px solid var(--accent)44;
  border-radius: 4px;
  font-family: var(--font-mono);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.05em;
  padding: 2px 7px;
}

.badge-danger {
  background: #ff4d6d18;
  color: var(--danger);
  border: 1px solid var(--danger)44;
  border-radius: 4px;
  font-family: var(--font-mono);
  font-size: 10px;
  font-weight: 700;
  padding: 2px 7px;
}

.badge-muted {
  background: var(--bg-hover);
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: 4px;
  font-family: var(--font-mono);
  font-size: 10px;
  font-weight: 700;
  padding: 2px 7px;
}

/* === Page heading === */
.page-heading {
  margin-bottom: 24px;
}

.page-heading h1 {
  font-size: 22px;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0 0 4px;
  line-height: 1.3;
}

.page-heading p {
  font-size: 13px;
  color: var(--text-secondary);
  margin: 0;
}

/* === Stat chips (used on management page) === */
.stat-chip {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.stat-chip .stat-label {
  font-family: var(--font-mono);
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.stat-chip .stat-value {
  font-family: var(--font-mono);
  font-size: 20px;
  font-weight: 700;
  color: var(--text-primary);
}

.stat-chip .stat-sub {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-secondary);
}

.stat-chip.accent  { border-color: var(--accent)55; }
.stat-chip.danger  { border-color: var(--danger)55; }
.stat-chip.success { border-color: #00c9a755; }

/* === Desktop sidebar collapse === */
.sidebar-collapse-btn {
  display: none;
  background: none;
  border: 1px solid var(--border);
  color: var(--text-secondary);
  cursor: pointer;
  padding: 5px 7px;
  border-radius: var(--radius);
  transition: all var(--transition);
  font-size: 15px;
  line-height: 1;
}
.sidebar-collapse-btn:hover {
  color: var(--accent);
  border-color: var(--accent)66;
  background: var(--accent-dim);
}
.sidebar-collapse-btn i { transition: transform var(--transition); }

@media (min-width: 901px) {
  .sidebar-collapse-btn { display: flex; align-items: center; justify-content: center; }

  .sidebar.collapsed { width: var(--sidebar-collapsed-w); overflow-x: visible; overflow-y: hidden; }
  .sidebar.collapsed .sidebar-nav { overflow-y: auto; overflow-x: visible; }
  .sidebar.collapsed ~ .main-content { margin-left: var(--sidebar-collapsed-w); }

  .sidebar.collapsed .nav-section-label {
    height: 1px;
    background: var(--border);
    margin: 6px 10px;
    padding: 0;
    overflow: hidden;
    font-size: 0;
  }
  .sidebar.collapsed .nav-text { display: none; }
  .sidebar.collapsed .nav-link-item {
    padding: 10px 0;
    justify-content: center;
    gap: 0;
  }
  .sidebar.collapsed .sidebar-brand { padding: 0; justify-content: center; }
  .sidebar.collapsed .sidebar-logo-link { display: none; }

  .sidebar.collapsed .sidebar-collapse-btn i { transform: scaleX(-1); }

  /* Tooltip for icon-only items */
  .sidebar.collapsed .nav-link-item::after {
    content: attr(data-label);
    position: absolute;
    left: calc(100% + 6px);
    top: 50%;
    transform: translateY(-50%);
    background: var(--bg-card);
    border: 1px solid var(--border-bright);
    color: var(--text-primary);
    font-size: 12px;
    padding: 4px 10px;
    border-radius: var(--radius);
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.12s ease;
    z-index: 200;
  }
  .sidebar.collapsed .nav-link-item:hover::after { opacity: 1; }
}

/* === Tile settings popup === */
.tile-settings { position: relative; }
.tile-settings-menu {
  display: none;
  position: absolute;
  right: 0;
  top: calc(100% + 4px);
  background: var(--bg-card);
  border: 1px solid var(--border-bright);
  border-radius: var(--radius);
  z-index: 200;
  min-width: 155px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.5);
  overflow: hidden;
}
.tile-settings-menu.open { display: block; }
.tile-settings-menu button {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 9px 14px;
  background: none;
  border: none;
  border-bottom: 1px solid var(--border);
  color: var(--text-secondary);
  font-size: 12px;
  cursor: pointer;
  text-align: left;
  transition: all 0.12s;
  font-family: var(--font-ui);
}
.tile-settings-menu button:last-child { border-bottom: none; }
.tile-settings-menu button:hover { background: var(--bg-hover); color: var(--text-primary); }

/* === Mobile sidebar toggle === */
.sidebar-toggle {
  display: none;
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
  padding: 6px 10px;
  cursor: pointer;
  font-size: 16px;
}

/* === Responsive: collapse sidebar on mobile === */
@media (max-width: 900px) {
  .sidebar {
    transform: translateX(calc(-1 * var(--sidebar-w)));
    height: 100vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .sidebar.open {
    transform: translateX(0);
    box-shadow: 4px 0 24px rgba(0,0,0,0.6);
  }

  /* Let entire sidebar scroll as one unit; nav no longer clips independently */
  .sidebar-nav {
    flex: none;
    overflow: visible;
  }

  .main-content {
    margin-left: 0;
  }

  .sidebar-toggle {
    display: flex;
    align-items: center;
  }

  .page-content {
    padding: 12px;
  }

  /* Page heading */
  .page-heading h1 { font-size: 20px; }
  .page-heading p  { font-size: 12px; }

  /* Stat chips: equal width AND height regardless of content */
  .stat-chip {
    flex: 1 1 0;
    min-width: 140px;
    overflow: hidden;
    align-self: stretch;
    padding: 12px 14px;
  }
  .stat-value { font-size: 16px; }
  .stat-chip .stat-sub {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  /* Tables: always scrollable */
  .table-dark-custom { font-size: 12px; }
  .table-dark-custom th,
  .table-dark-custom td { padding: 8px 10px; }

  /* Cards */
  .card-body { padding: 14px; }
  .card-header { padding: 10px 14px; }

  /* Topbar */
  .topbar { padding: 0 14px; }

  /* Forms: stack all cols */
  .row.g-3 > [class*="col-"] { padding-left: 6px; padding-right: 6px; }

  /* Buttons: allow wrapping, full-width save */
  .btn-accent, .btn-danger, .btn-ghost { font-size: 12px; padding: 7px 12px; }

  /* Watchlist cards */
  .watchlist-card { padding: 12px 14px; }

  /* Finance/product charts: constrain canvas width */
  canvas { max-width: 100%; }

  /* Flash messages */
  .flash-container { right: 12px; left: 12px; max-width: none; }

  /* Service tile grid: smaller tiles on mobile */
  .service-tile { width: 70px; font-size: 10px; padding: 10px 6px 8px; }
  .service-tile img { width: 30px; height: 30px; }
}

/* === Hide secondary table columns on mobile (portrait + landscape) === */
@media (max-width: 900px) {
  .hide-xs { display: none !important; }
}

/* === Divider === */
.divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 20px 0;
}

/* === Scrollbar global === */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--border-bright); }
