  :root {
    --sidebar-width: 260px;
    --header-height: 64px;
    --blue-50: #eff6ff;
    --blue-100: #e8eeff;
    --blue-500: #457AFB;
    --blue-600: #3566e8;
    --blue-700: #2a52c7;
    --slate-50: #f8fafc;
    --slate-100: #f1f5f9;
    --slate-200: #e2e8f0;
    --slate-500: #64748b;
    --slate-700: #334155;
    --slate-800: #1e293b;
    --white: #ffffff;
    --orange: #f97316;
    --amber: #f59e0b;
    --green: #22c55e;
    --red: #ef4444;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
    --shadow: 0 1px 3px rgba(0,0,0,0.08);
    --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.08), 0 2px 4px -2px rgba(0,0,0,0.06);
    --radius: 10px;
    --radius-sm: 6px;
  }

  * {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
  }

  body {
    font-family: 'DM Sans', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--slate-50);
    color: var(--slate-800);
    min-height: 100vh;
    display: flex;
  }

  /* ----- Sidebar ----- */
  .sidebar {
    width: var(--sidebar-width);
    min-height: 100vh;
    background: var(--white);
    border-right: 1px solid var(--slate-200);
    display: flex;
    flex-direction: column;
    position: fixed;
    left: 0;
    top: 0;
    z-index: 40;
  }

  .sidebar-brand {
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--slate-100);
    display: flex;
    align-items: center;
    gap: 0.75rem;
  }

  .sidebar-brand-logo {
    height: 40px;
    width: auto;
    max-width: 160px;
    object-fit: contain;
    display: block;
  }

  .sidebar-brand-icon {
    width: 40px;
    height: 40px;
    background: linear-gradient(135deg, var(--blue-500), var(--blue-700));
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-weight: 700;
    font-size: 1rem;
  }

  .sidebar-brand-text {
    font-weight: 700;
    font-size: 1.1rem;
    color: var(--slate-800);
    line-height: 1.2;
  }

  .sidebar-brand-sub {
    display: block;
    font-size: 0.7rem;
    font-weight: 500;
    color: var(--slate-500);
    line-height: 1.2;
    margin: 0;
    padding: 0;
  }

  .sidebar-nav {
    flex: 1;
    padding: 1rem 0.75rem;
    overflow-y: auto;
  }

  .sidebar-section {
    margin-bottom: 1.5rem;
  }

  .sidebar-section-title {
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--slate-500);
    padding: 0 0.75rem;
    margin-bottom: 0.5rem;
  }

  .sidebar-link {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.65rem 0.75rem;
    border-radius: var(--radius-sm);
    color: var(--slate-700);
    text-decoration: none;
    font-size: 0.9rem;
    font-weight: 500;
    margin-bottom: 2px;
    transition: background 0.15s, color 0.15s;
  }

  .sidebar-link:hover {
    background: var(--slate-100);
    color: var(--slate-800);
  }

  .sidebar-link.active {
    background: var(--blue-500);
    color: white;
  }

  .sidebar-link svg {
    flex-shrink: 0;
    width: 20px;
    height: 20px;
    opacity: 0.9;
  }

  .sidebar-link.active svg {
    opacity: 1;
  }

  .sidebar-link-nested {
    font-size: 0.85rem;
  }

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

  .header {
    height: var(--header-height);
    background: var(--white);
    border-bottom: 1px solid var(--slate-200);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 1.5rem;
    position: sticky;
    top: 0;
    z-index: 30;
  }

  .header-left {
    font-weight: 600;
    color: var(--slate-800);
  }

  .header-right {
    display: flex;
    align-items: center;
    gap: 1rem;
  }

  .header-icon-btn {
    width: 40px;
    height: 40px;
    border-radius: var(--radius-sm);
    border: none;
    background: var(--slate-100);
    color: var(--slate-600);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s;
  }

  .header-icon-btn:hover {
    background: var(--slate-200);
  }

  .header-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--blue-500), var(--blue-700));
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 0.95rem;
  }

  .content {
    flex: 1;
    padding: 1.5rem;
  }

  /* ----- KPI cards ----- */
  .kpi-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1.25rem;
    margin-bottom: 1.5rem;
  }

  .kpi-card {
    background: var(--white);
    border-radius: var(--radius);
    padding: 1.25rem;
    box-shadow: var(--shadow);
    border: 1px solid var(--slate-100);
  }

  .kpi-card-title {
    font-size: 0.8rem;
    color: var(--slate-500);
    margin-bottom: 0.25rem;
  }

  .kpi-card-value {
    font-size: 1.75rem;
    font-weight: 700;
    color: var(--slate-800);
  }

  .kpi-card-icon {
    width: 44px;
    height: 44px;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 0.75rem;
    float: right;
  }

  .kpi-card-icon.purple { background: #ede9fe; color: #7c3aed; }
  .kpi-card-icon.orange { background: #ffedd5; color: var(--orange); }
  .kpi-card-icon.amber { background: #fef3c7; color: var(--amber); }
  .kpi-card-icon.green { background: #dcfce7; color: var(--green); }

  .kpi-grid-compact {
    gap: 0.75rem;
    margin-bottom: 1rem;
  }

  .kpi-grid-compact .kpi-card {
    padding: 0.6rem 1rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
  }

  .kpi-card-clickable {
    cursor: pointer;
    transition: box-shadow 0.15s, border-color 0.15s;
    text-align: left;
  }

  .kpi-card-clickable:hover {
    border-color: var(--blue-200);
    box-shadow: var(--shadow-md);
  }

  .kpi-card-clickable.active {
    border-color: var(--blue-500);
    box-shadow: 0 0 0 2px var(--blue-100);
  }

  .kpi-inline {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex: 1;
    min-width: 0;
  }

  .kpi-grid-compact .kpi-card-title {
    margin-bottom: 0;
    font-size: 0.8rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .kpi-card-icon-sm {
    width: 32px;
    height: 32px;
    min-width: 32px;
    margin: 0;
    float: none;
  }

  .kpi-grid-compact .kpi-card-value {
    font-size: 1.25rem;
    flex-shrink: 0;
  }

  /* ----- Modal ----- */
  .modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
  }

  .modal-overlay[aria-hidden="false"] {
    display: flex;
  }

  .modal-card {
    background: var(--white);
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    padding: 1.25rem;
    max-width: 480px;
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
  }

  /* ----- Welcome + Chart row ----- */
  .welcome-chart-row {
    display: grid;
    grid-template-columns: 1fr 2fr;
    gap: 1.25rem;
    margin-bottom: 1.5rem;
  }

  .welcome-card {
    background: linear-gradient(135deg, var(--blue-50), var(--blue-100));
    border-radius: var(--radius);
    padding: 1.5rem;
    border: 1px solid var(--blue-100);
  }

  .welcome-card h2 {
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--slate-800);
  }

  .chart-card {
    background: var(--white);
    border-radius: var(--radius);
    padding: 1.25rem;
    box-shadow: var(--shadow);
    border: 1px solid var(--slate-100);
  }

  .chart-card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 1rem;
  }

  .chart-card-title {
    font-size: 1rem;
    font-weight: 600;
    color: var(--slate-800);
  }

  .chart-select {
    font-size: 0.85rem;
    padding: 0.4rem 0.75rem;
    border: 1px solid var(--slate-200);
    border-radius: var(--radius-sm);
    background: var(--white);
    color: var(--slate-700);
  }

  .chart-placeholder {
    height: 200px;
    display: flex;
    align-items: flex-end;
    gap: 12px;
    padding: 0 1rem 1rem;
  }

  .chart-bar-group {
    flex: 1;
    display: flex;
    gap: 4px;
    justify-content: center;
    align-items: flex-end;
  }

  .chart-bar {
    width: 20px;
    border-radius: 4px 4px 0 0;
    min-height: 8px;
  }

  .chart-bar.prev { background: var(--blue-500); opacity: 0.7; }
  .chart-bar.curr { background: var(--blue-500); }

  .chart-labels {
    display: flex;
    justify-content: space-around;
    padding: 0.5rem 1rem 0;
    font-size: 0.75rem;
    color: var(--slate-500);
  }

  /* ----- Table card ----- */
  .table-card {
    background: var(--white);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    border: 1px solid var(--slate-100);
    overflow: hidden;
  }

  .table-card-header {
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--slate-100);
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 0.75rem;
  }

  .table-card-title {
    font-size: 1rem;
    font-weight: 600;
    color: var(--slate-800);
  }

  .table-toolbar {
    display: flex;
    align-items: center;
    gap: 0.75rem;
  }

  .btn {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 1rem;
    font-size: 0.875rem;
    font-weight: 500;
    border-radius: var(--radius-sm);
    border: none;
    cursor: pointer;
    font-family: inherit;
    transition: background 0.15s, color 0.15s;
  }

  .btn-primary {
    background: var(--blue-500);
    color: white;
  }

  .btn-primary:hover {
    background: var(--blue-600);
  }

  .btn-outline {
    background: var(--white);
    color: var(--slate-700);
    border: 1px solid var(--slate-200);
  }

  .btn-outline:hover {
    background: var(--slate-50);
  }

  .search-wrap {
    position: relative;
    width: 280px;
  }

  .search-input {
    width: 100%;
    padding: 0.5rem 1rem 0.5rem 2.5rem;
    font-size: 0.875rem;
    border: 1px solid var(--slate-200);
    border-radius: var(--radius-sm);
    background: var(--slate-50);
    color: var(--slate-800);
    font-family: inherit;
  }

  .search-input::placeholder {
    color: var(--slate-500);
  }

  .search-input:focus {
    outline: none;
    border-color: var(--blue-500);
    background: var(--white);
  }

  .search-icon {
    position: absolute;
    left: 0.75rem;
    top: 50%;
    transform: translateY(-50%);
    color: var(--slate-500);
    pointer-events: none;
  }

  /* Type-ahead dropdown */
  .search-results {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    margin-top: 2px;
    background: var(--white);
    border: 1px solid var(--slate-200);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-md);
    max-height: 280px;
    overflow-y: auto;
    z-index: 50;
    display: none;
  }

  .search-results.visible {
    display: block;
  }

  .search-result-item {
    padding: 0.6rem 1rem;
    font-size: 0.875rem;
    cursor: pointer;
    border-bottom: 1px solid var(--slate-100);
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .search-result-item:last-child {
    border-bottom: none;
  }

  .search-result-item:hover {
    background: var(--blue-50);
  }

  .search-result-name {
    font-weight: 500;
    color: var(--slate-800);
  }

  .search-result-number {
    font-size: 0.8rem;
    color: var(--slate-500);
  }

  .table-wrap {
    overflow-x: auto;
  }

  table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.875rem;
  }

  th {
    text-align: left;
    padding: 0.75rem 1.25rem;
    font-weight: 600;
    color: var(--slate-600);
    background: var(--slate-50);
    border-bottom: 1px solid var(--slate-200);
  }

  td {
    padding: 0.85rem 1.25rem;
    border-bottom: 1px solid var(--slate-100);
    color: var(--slate-700);
  }

  .th-center,
  th.th-center {
    text-align: center;
  }

  .td-center,
  td.td-center {
    text-align: center;
  }

  tr:hover td {
    background: var(--slate-50);
  }

  .status-indicator-col {
    width: 1.5rem;
    min-width: 1.5rem;
    padding: 0.85rem 0.5rem;
    text-align: center;
    vertical-align: middle;
  }

  .status-dot {
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 50%;
  }

  .status-dot-green {
    background: var(--green);
  }

  .status-dot-amber {
    background: var(--amber);
  }

  .status-dot-red {
    background: var(--red);
  }

  .sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
  }

  .status-pill {
    display: inline-block;
    padding: 0.25rem 0.6rem;
    border-radius: 999px;
    font-size: 0.75rem;
    font-weight: 500;
  }

  .status-overdue {
    background: #fee2e2;
    color: var(--red);
  }

  .status-due-soon {
    background: #fef3c7;
    color: #b45309;
  }

  .status-ok {
    background: #dcfce7;
    color: #166534;
  }

  .action-btn {
    width: 32px;
    height: 32px;
    border: none;
    background: var(--slate-100);
    border-radius: var(--radius-sm);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: var(--slate-600);
    transition: background 0.15s;
  }

  .action-btn:hover {
    background: var(--slate-200);
  }

  .data-refresh {
    font-size: 0.75rem;
    color: var(--slate-500);
    margin-top: 1rem;
  }

  .data-refresh a {
    color: var(--blue-500);
    text-decoration: none;
  }

  .data-refresh a:hover {
    text-decoration: underline;
  }

  /* ----- Company detail overlay ----- */
  .company-detail-overlay {
    display: none;
    position: fixed;
    inset: 0;
    margin-left: var(--sidebar-width);
    background: var(--slate-50);
    z-index: 25;
    overflow-y: auto;
    animation: fadeIn 0.2s ease;
  }

  .company-detail-overlay.visible {
    display: block;
  }

  @keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
  }

  .company-detail-header {
    background: var(--white);
    border-bottom: 1px solid var(--slate-200);
    padding: 1rem 1.5rem;
    display: flex;
    align-items: center;
    gap: 1rem;
    position: sticky;
    top: 0;
    z-index: 10;
  }

  .company-detail-back {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 0.75rem;
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--slate-700);
    background: var(--slate-100);
    border: none;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-family: inherit;
    transition: background 0.15s;
  }

  .company-detail-back:hover {
    background: var(--slate-200);
  }

  .company-detail-title {
    font-size: 1.15rem;
    font-weight: 600;
    color: var(--slate-800);
  }

  .company-detail-body {
    padding: 1.5rem;
    max-width: 900px;
    margin: 0 auto;
  }

  .detail-section {
    background: var(--white);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    border: 1px solid var(--slate-100);
    padding: 1.25rem 1.5rem;
    margin-bottom: 1.25rem;
  }

  .detail-section h3 {
    font-size: 0.85rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--slate-500);
    margin-bottom: 1rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--slate-100);
  }

  .detail-grid {
    display: grid;
    gap: 0.75rem 1.5rem;
  }

  .detail-grid-2 {
    grid-template-columns: 140px 1fr;
  }

  .detail-label {
    font-size: 0.8rem;
    color: var(--slate-500);
    font-weight: 500;
  }

  .detail-value {
    font-size: 0.9rem;
    color: var(--slate-800);
  }

  .detail-value.address {
    white-space: pre-line;
    line-height: 1.5;
  }

  .detail-company-name {
    font-weight: 600;
    font-size: 1rem;
  }

  .detail-sic-list {
    list-style: none;
  }

  .detail-sic-list li {
    font-size: 0.9rem;
    color: var(--slate-800);
    padding: 0.35rem 0;
    border-bottom: 1px solid var(--slate-100);
  }

  .detail-sic-list li:last-child {
    border-bottom: none;
  }

  .detail-sic-code {
    font-weight: 600;
    color: var(--slate-600);
    margin-right: 0.5rem;
  }

  .detail-sic-desc {
    font-weight: 400;
    color: var(--slate-800);
  }

  .detail-table {
    width: 100%;
    font-size: 0.875rem;
    border-collapse: collapse;
  }

  .detail-table th {
    text-align: left;
    padding: 0.4rem 0.6rem 0.4rem 0;
    font-weight: 600;
    color: var(--slate-500);
    font-size: 0.8rem;
    width: 1%;
    white-space: nowrap;
    vertical-align: top;
  }

  .detail-table td {
    padding: 0.4rem 0.6rem 0.4rem 0;
    border-bottom: 1px solid var(--slate-100);
    color: var(--slate-800);
  }

  .detail-table tr:last-child td {
    border-bottom: none;
  }

  .detail-table tr.detail-table-spacer td {
    height: 1rem;
    border-bottom: none;
    background: var(--white);
    padding: 0;
  }

  .detail-table .detail-status-cell {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
  }

  .detail-table-accounts th,
  .detail-table-accounts td {
    width: 50%;
  }

  .users-access-table th:first-child,
  .users-access-table td:first-child {
    width: 2.5rem;
    text-align: center;
  }

  .detail-link-external {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    color: var(--blue-500);
    font-weight: 500;
    text-decoration: none;
    font-size: 0.9rem;
  }

  .detail-link-external:hover {
    text-decoration: underline;
  }

  .detail-link-external svg {
    flex-shrink: 0;
  }

  .sort-btn {
    background: none;
    border: none;
    padding: 0;
    font: inherit;
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    color: inherit;
    cursor: pointer;
  }

  .sort-icon {
    font-size: 0.7rem;
    opacity: 0.6;
  }

  .sort-btn[data-dir="asc"] .sort-icon::after {
    content: '↑';
    margin-left: 2px;
  }

  .sort-btn[data-dir="desc"] .sort-icon::after {
    content: '↓';
    margin-left: 2px;
  }

  @media (max-width: 1200px) {
    .kpi-grid {
      grid-template-columns: repeat(2, 1fr);
    }
    .welcome-chart-row {
      grid-template-columns: 1fr;
    }
  }

  @media (max-width: 768px) {
    .kpi-grid {
      grid-template-columns: 1fr;
    }
    .sidebar {
      transform: translateX(-100%);
    }
    .company-detail-overlay {
      margin-left: 0;
    }
    .table-toolbar {
      width: 100%;
    }
    .search-wrap {
      width: 100%;
    }
