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

:root {
  --bg: #0f172a;
  --bg-card: #1e293b;
  --bg-hover: #334155;
  --text: #e2e8f0;
  --text-muted: #94a3b8;
  --text-dim: #64748b;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --success: #22c55e;
  --danger: #ef4444;
  --warning: #f59e0b;
  --border: #334155;
  --radius: 0.5rem;
}

body {
  font-family:
    system-ui,
    -apple-system,
    sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
}

a {
  color: var(--accent);
  text-decoration: none;

  &:hover {
    color: var(--accent-hover);
  }
}

.mobile-menu-toggle {
  display: none;
}

/* Sidebar */
.sidebar {
  width: 240px;
  background: var(--bg-card);
  border-right: 1px solid var(--border);
  padding: 1.5rem 0 0;
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  bottom: 0;

  nav {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 0.5rem 0.75rem;

    & a {
      display: flex;
      align-items: center;
      gap: 0.75rem;
      padding: 0.625rem 0.75rem;
      border-radius: var(--radius);
      color: var(--text-muted);
      font-size: 0.875rem;
      font-weight: 500;
      transition: all 0.15s;

      &:hover {
        background: var(--bg-hover);
        color: var(--text);
      }

      &.active {
        background: var(--accent);
        color: white;
      }
    }
  }
}

.sidebar-brand {
  padding: 0 1.25rem 1.5rem;
  border-bottom: 1px solid var(--border);
  margin-bottom: 0.5rem;

  h1 {
    font-size: 1.125rem;
    font-weight: 700;
  }

  span {
    font-size: 0.75rem;
    color: var(--text-muted);
  }
}

.sidebar-footer {
  padding: 1rem 1.25rem;
  border-top: 1px solid var(--border);

  .user-info {
    display: flex;
    align-items: center;
    gap: 0.625rem;
    margin-bottom: 0.75rem;

    img {
      width: 32px;
      height: 32px;
      border-radius: 50%;
    }

    .avatar-fallback {
      width: 32px;
      height: 32px;
      border-radius: 50%;
      background: var(--primary);
      color: #fff;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 0.875rem;
      font-weight: 600;
      flex-shrink: 0;
    }

    .name {
      font-size: 0.8125rem;
      font-weight: 500;
    }

    .email {
      font-size: 0.6875rem;
      color: var(--text-dim);
    }
  }

  & > a {
    font-size: 0.8125rem;
    color: var(--text-muted);

    &:hover {
      color: var(--danger);
    }
  }
}

/* Main content */
.main {
  margin-left: 240px;
  flex: 1;
  padding: 2rem;
  min-height: 100vh;
}

@media (max-width: 900px) {
  body {
    display: block;
  }

  .mobile-menu-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    position: fixed;
    top: 0.75rem;
    left: 0.75rem;
    width: 2.5rem;
    height: 2.5rem;
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    background: var(--bg-card);
    color: var(--text);
    z-index: 1100;
    font-size: 1.25rem;
  }

  .sidebar {
    z-index: 1200;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }

  body.sidebar-open .sidebar {
    transform: translateX(0);
  }

  .mobile-sidebar-backdrop {
    display: block;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, 0.45);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
    z-index: 1150;
  }

  body.sidebar-open .mobile-sidebar-backdrop {
    opacity: 1;
    pointer-events: auto;
  }

  .main {
    margin-left: 0;
    padding: 4rem 1rem 1rem;
    width: 100%;
    overflow-x: hidden;
  }

  .page-header h2 {
    font-size: 1.25rem;
  }

  table {
    display: block;
    overflow-x: auto;
    white-space: nowrap;
  }
}

.page-header {
  margin-bottom: 1.5rem;

  h2 {
    font-size: 1.5rem;
    font-weight: 700;
  }
}

/* Cards */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.stat-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;

  .label {
    font-size: 0.75rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 0.25rem;
  }

  .value {
    font-size: 1.75rem;
    font-weight: 700;
  }

  .sub {
    font-size: 0.75rem;
    color: var(--text-dim);
    margin-top: 0.25rem;
  }
}

/* Tables */
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;

  thead th {
    text-align: left;
    padding: 0.75rem 1rem;
    color: var(--text-muted);
    font-weight: 600;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    border-bottom: 1px solid var(--border);
  }

  tbody td {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
  }

  tbody tr:hover {
    background: var(--bg-hover);
  }
}

/* Sortable table */
sortable-table {
  display: contents;
}

thead th[aria-sort] {
  cursor: pointer;
  user-select: none;
}

thead th[aria-sort]:hover {
  color: var(--text);
}

.sort-indicator {
  font-size: 0.65rem;
  opacity: 0.5;
}

thead th[aria-sort='ascending'] .sort-indicator,
thead th[aria-sort='descending'] .sort-indicator {
  opacity: 1;
}

/* Badges */
.badge {
  display: inline-block;
  padding: 0.125rem 0.5rem;
  border-radius: 9999px;
  font-size: 0.6875rem;
  font-weight: 600;

  &-success {
    background: #166534;
    color: #bbf7d0;
  }

  &-danger {
    background: #991b1b;
    color: #fecaca;
  }

  &-warning {
    background: #92400e;
    color: #fde68a;
  }

  &-info {
    background: #1e3a5f;
    color: #bfdbfe;
  }
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  padding: 0.5rem 1rem;
  border-radius: var(--radius);
  font-size: 0.8125rem;
  font-weight: 500;
  border: none;
  cursor: pointer;
  transition: all 0.15s;

  &-primary {
    background: var(--accent);
    color: white;

    &:hover {
      background: var(--accent-hover);
    }
  }

  &-danger {
    background: var(--danger);
    color: white;

    &:hover {
      background: #dc2626;
    }
  }

  &-ghost {
    background: transparent;
    color: var(--text-muted);
    border: 1px solid var(--border);

    &:hover {
      background: var(--bg-hover);
      color: var(--text);
    }
  }

  &-sm {
    padding: 0.25rem 0.625rem;
    font-size: 0.75rem;
  }
}

/* Forms */
input,
select,
textarea {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.5rem 0.75rem;
  border-radius: var(--radius);
  font-size: 0.875rem;
  width: 100%;

  &:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.25);
  }
}

label {
  display: block;
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--text-muted);
  margin-bottom: 0.25rem;
}

.form-group {
  margin-bottom: 1rem;
}

/* Pagination */
.pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 1rem;
  font-size: 0.8125rem;
  color: var(--text-muted);
}

.pagination-buttons {
  display: flex;
  gap: 0.375rem;
}

/* Filters */
.filters {
  display: flex;
  gap: 0.75rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
  align-items: end;

  .form-group {
    margin-bottom: 0;
  }

  input,
  select {
    width: auto;
    min-width: 150px;
  }
}

/* Utility */
.text-muted {
  color: var(--text-muted);
}
.text-success {
  color: var(--success);
}
.text-danger {
  color: var(--danger);
}
.text-sm {
  font-size: 0.8125rem;
}
.mt-1 {
  margin-top: 0.5rem;
}
.mb-1 {
  margin-bottom: 0.5rem;
}
.flex {
  display: flex;
}
.items-center {
  align-items: center;
}
.justify-between {
  justify-content: space-between;
}
.gap-1 {
  gap: 0.5rem;
}

/* Dialog / Modal */
dialog {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 1.5rem;
  max-width: 500px;
  width: 90%;

  &::backdrop {
    background: rgba(0, 0, 0, 0.6);
  }

  h3 {
    margin-bottom: 1rem;
  }
}

/* Real-time new row highlight */
.new-user-highlight {
  animation: highlight-fade 3s ease-out;
}

@keyframes highlight-fade {
  0% {
    background: rgba(59, 130, 246, 0.25);
  }
  100% {
    background: transparent;
  }
}

/* HTMX loading indicator */
.htmx-indicator {
  display: none;
}

.htmx-request {
  .htmx-indicator {
    display: inline-block;
  }

  &.htmx-indicator {
    display: inline-block;
  }
}
