/**
 * design-system.css
 * --------------------------------------------------------------------
 * Extends tokens in app-layout.css (:root HSL components + layout vars).
 * Load order: Bootstrap → page-ux.css → brand-tokens.css → app-layout.css → this file.
 *
 * Purpose: spacing/shadow/motion scale, Bootstrap 5 variable bridge,
 * accessible focus rings, accounting-friendly numerals, reduced motion.
 */

:root {
  /* Spacing scale */
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.25rem;
  --space-6: 1.5rem;
  --space-8: 2rem;
  --space-10: 2.5rem;
  --space-12: 3rem;

  /* Elevation */
  --shadow-xs: 0 1px 2px hsl(var(--foreground) / 0.03);
  --shadow-sm: 0 1px 3px hsl(var(--foreground) / 0.04),
    0 1px 2px hsl(var(--foreground) / 0.03);
  --shadow-md: 0 4px 12px -2px hsl(var(--foreground) / 0.06),
    0 2px 6px -2px hsl(var(--foreground) / 0.04);
  --shadow-lg: 0 12px 24px -4px hsl(var(--foreground) / 0.07),
    0 4px 8px -4px hsl(var(--foreground) / 0.04);

  /* Motion */
  --ease-out: cubic-bezier(0.33, 1, 0.68, 1);
  --duration-fast: 150ms;
  --duration-normal: 200ms;

  /* Content width */
  --content-max-width: 88rem;

  /* Type scale (reference; base body stays in app-layout) */
  --text-xs: 0.75rem;
  --text-sm: 0.875rem;
  --text-base: 1rem;
  --text-lg: 1.125rem;
  --text-xl: 1.25rem;
  --text-2xl: 1.5rem;
  --text-3xl: 1.875rem;
  --text-4xl: 2.25rem;

  /* Heading scale (use with .app-shell or global prose) */
  --text-h1: var(--text-4xl);
  --text-h2: var(--text-3xl);
  --text-h3: var(--text-2xl);
  --text-h4: var(--text-xl);
  --text-h5: var(--text-lg);
  --text-h6: var(--text-base);

  /* Semantic aliases → same HSL component vars as app-layout (use: hsl(var(--color-bg))) */
  --color-bg: var(--background);
  --color-text: var(--foreground);
  --color-surface: var(--card);
  --color-surface-text: var(--card-foreground);
  --color-border: var(--border);
  --color-primary: var(--primary);
  --color-muted: var(--muted-foreground);
  --color-error: var(--destructive);
  --color-success: var(--success);
  --color-warning: var(--warning);

  /*
   * Breakpoints (documented for authors; literal px required in @media queries):
   * xs: 0–480    | sm: 481–768  | md: 769–1024
   * lg: 1025–1440 | xl: 1441+
   * Shell drawer vs desktop sidebar follows Bootstrap lg (max-width: 991.98px).
   */
  --touch-target-min: 2.75rem; /* ~44px — primary tap targets */
  --page-padding-x: clamp(0.75rem, 3vw, 1.5rem); /* 12–24px scale */
  --page-padding-y: clamp(1rem, 2.5vw, 1.5rem);
  --navbar-search-max: 20rem;
}

/* Global heading rhythm (mobile-first; clamp on large titles) */
.app-shell h1,
.ds-heading-1 {
  font-size: clamp(1.5rem, 4vw, var(--text-h1));
  line-height: 1.2;
  font-weight: 700;
  letter-spacing: -0.02em;
}
.app-shell h2,
.ds-heading-2 {
  font-size: clamp(1.35rem, 3vw, var(--text-h2));
  line-height: 1.25;
}
.app-shell h3,
.ds-heading-3 {
  font-size: var(--text-h3);
  line-height: 1.3;
}
.app-shell h4,
.ds-heading-4 {
  font-size: var(--text-h4);
  line-height: 1.35;
}
.app-shell h5,
.ds-heading-5 {
  font-size: var(--text-h5);
  line-height: 1.4;
}
.app-shell h6,
.ds-heading-6 {
  font-size: var(--text-h6);
  line-height: 1.45;
  font-weight: 600;
}
.ds-body {
  font-size: var(--text-base);
  line-height: 1.5;
}
.ds-text-small {
  font-size: var(--text-sm);
  line-height: 1.5;
}

.ds-text-primary {
  color: var(--color-text-primary);
}
.ds-text-secondary {
  color: var(--color-text-secondary);
}
.ds-text-muted {
  color: var(--color-text-muted);
}

/* Design system surfaces + buttons (Bootstrap-compatible) */
.ds-card {
  background-color: hsl(var(--card));
  color: hsl(var(--card-foreground));
  border: 1px solid hsl(var(--border));
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  transition: box-shadow var(--duration-normal) var(--ease-out),
    border-color var(--duration-normal) var(--ease-out);
}
.ds-card--raised {
  box-shadow: var(--shadow-md);
}
.ds-card-header {
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid hsl(var(--border));
  background: linear-gradient(
    90deg,
    hsl(var(--primary) / 0.08),
    hsl(var(--muted) / 0.35) 45%,
    hsl(var(--card)) 100%
  );
  font-weight: 600;
  font-size: var(--text-sm);
}
.ds-card-body {
  padding: var(--space-6);
}
.ds-btn-secondary.btn {
  border-color: hsl(var(--border));
  color: hsl(var(--foreground));
  background-color: hsl(var(--secondary));
}
.ds-btn-secondary.btn:hover {
  background-color: hsl(var(--accent));
  color: hsl(var(--accent-foreground));
  border-color: hsl(var(--border));
}
.ds-btn-danger.btn {
  background-color: hsl(var(--destructive));
  border-color: hsl(var(--destructive));
  color: hsl(var(--destructive-foreground));
}
.ds-btn-danger.btn:hover {
  filter: brightness(1.05);
  color: hsl(var(--destructive-foreground));
}

/* Bootstrap 5.3: map semantic tokens → Bootstrap theme variables */
:root {
  --bs-primary: var(--color-primary-400);
  --bs-secondary: hsl(var(--secondary));
  --bs-success: hsl(var(--success));
  --bs-danger: hsl(var(--destructive));
  --bs-warning: hsl(var(--warning));
  --bs-info: hsl(var(--info));
  --bs-body-bg: hsl(var(--background));
  --bs-body-color: hsl(var(--foreground));
  --bs-border-color: hsl(var(--border));
  --bs-link-color: hsl(var(--primary));
  /* Hover: slightly richer than default link (accent-foreground is readable on bg) */
  --bs-link-hover-color: hsl(var(--accent-foreground));
}

/* Focus: token-based ring (keyboard + mouse where applicable) */
.btn:focus-visible {
  box-shadow: 0 0 0 0.125rem hsl(var(--background)),
    0 0 0 0.25rem hsl(var(--ring));
}

.btn:focus:not(:focus-visible) {
  box-shadow: none;
}

.form-control:focus,
.form-select:focus {
  border-color: var(--color-primary-400);
  box-shadow: 0 0 0 0.2rem color-mix(in srgb, var(--color-primary-400) 22%, transparent);
}

.form-check-input:focus {
  border-color: var(--color-primary-400);
  box-shadow: 0 0 0 0.2rem color-mix(in srgb, var(--color-primary-400) 22%, transparent);
}

.btn-primary:focus-visible {
  box-shadow: 0 0 0 0.125rem hsl(var(--background)),
    0 0 0 0.25rem var(--color-primary-400);
}

.sidebar-item:focus-visible,
.sidebar-group-btn:focus-visible {
  outline: 2px solid hsl(var(--sidebar-ring));
  outline-offset: 2px;
}

/* Accounting / inventory: aligned figures in tables and inputs */
.tabular-nums,
.amount,
[data-numeric="true"] {
  font-variant-numeric: tabular-nums lining-nums;
}

/* Interactive affordance (sidebar) */
.sidebar-item,
.sidebar-group-btn {
  cursor: pointer;
}

@media (prefers-reduced-motion: reduce) {
  .sidebar,
  .sidebar-overlay,
  .sidebar-group-children,
  .top-navbar,
  .ds-card,
  .app-shell .stats-card {
    transition-duration: 0.01ms !important;
  }
}

/* --------------------------------------------------------------------
   UI primitives (Razor partials under Views/Shared/Ui)
---------------------------------------------------------------------*/
.ui-modal-header {
  border-bottom: 1px solid hsl(var(--border));
  padding: var(--space-4) var(--space-4);
}

.ui-modal-header .modal-subtitle {
  font-size: var(--text-sm);
}

.ui-modal-footer {
  border-top: 1px solid hsl(var(--border));
  padding: var(--space-3) var(--space-4);
}

/* Modal footers: consistent spacing; cancel/dismiss left of primary where both exist */
.modal-footer {
  flex-wrap: wrap;
  gap: var(--space-2);
  justify-content: flex-end;
}

.modal-footer .btn {
  margin: 0;
}

.modal-content.ui-modal-content {
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  border: 1px solid hsl(var(--border));
}

.ui-form-section-title {
  font-size: var(--text-sm);
  font-weight: 600;
  letter-spacing: -0.01em;
  color: hsl(var(--foreground));
  margin-bottom: var(--space-2);
}

.ui-form-section-desc {
  margin-top: calc(-1 * var(--space-1));
}

.ui-badge {
  display: inline-flex;
  align-items: center;
  font-weight: 500;
  font-size: var(--text-xs);
  line-height: 1.25;
  padding: 0.2em 0.5em;
  border-radius: var(--radius-sm);
}

.ui-badge-success {
  background-color: hsl(var(--success) / 0.15);
  color: hsl(var(--success));
}

.ui-badge-warning {
  background-color: hsl(var(--warning) / 0.18);
  color: hsl(var(--warning));
}

.ui-badge-danger {
  background-color: hsl(var(--destructive) / 0.15);
  color: hsl(var(--destructive));
}

.ui-badge-info {
  background-color: hsl(var(--info) / 0.15);
  color: hsl(var(--info));
}

.ui-badge-primary {
  background-color: hsl(var(--primary) / 0.18);
  color: hsl(var(--accent-foreground));
}

.ui-badge-neutral {
  background-color: hsl(var(--muted));
  color: hsl(var(--muted-foreground));
}

.ui-empty-state-icon .bi {
  opacity: 0.5;
}

/* --------------------------------------------------------------------
   App shell: readable line length on large screens (SaaS content column)
---------------------------------------------------------------------*/
.main-content-inner {
  width: 100%;
  max-width: var(--content-max-width);
  margin-left: auto;
  margin-right: auto;
  box-sizing: border-box;
  min-width: 0; /* prevent flex children (tables, charts) from widening past column */
}

/* --------------------------------------------------------------------
   App shell: top bar, page header, skip link
---------------------------------------------------------------------*/
.skip-to-content {
  z-index: 1090;
}

.top-navbar {
  box-shadow: var(--shadow-xs);
}

@supports ((-webkit-backdrop-filter: blur(2px)) or (backdrop-filter: blur(2px))) {
  .top-navbar {
    background-color: hsl(var(--card) / 0.88);
    -webkit-backdrop-filter: saturate(180%) blur(12px);
    backdrop-filter: saturate(180%) blur(12px);
  }
}

/* Left cluster grows so search can use available space (up to cap) */
.top-navbar-left {
  flex: 1 1 auto;
  min-width: 0;
}

.top-navbar-search {
  flex: 1 1 auto;
  min-width: 0;
  max-width: min(28rem, 100%);
}

@media (min-width: 576px) {
  .top-navbar-search input {
    width: 100%;
    max-width: none;
  }
}

.top-navbar-icon-btn {
  width: 2.25rem;
  height: 2.25rem;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-md);
  color: hsl(var(--foreground));
  background-color: hsl(var(--background)) !important;
  border: 1px solid hsl(var(--border)) !important;
  line-height: 1;
  cursor: pointer;
  transition: background-color var(--duration-normal) var(--ease-out),
    color var(--duration-normal) var(--ease-out),
    border-color var(--duration-normal) var(--ease-out);
}

.top-navbar-icon-btn:hover {
  background-color: hsl(var(--accent)) !important;
  color: hsl(var(--accent-foreground));
  border-color: hsl(var(--border)) !important;
}

.top-navbar-icon-btn:focus-visible {
  box-shadow: 0 0 0 0.125rem hsl(var(--background)),
    0 0 0 0.25rem hsl(var(--ring));
}

.top-navbar-notifications-menu {
  min-width: 17.5rem;
}

.top-navbar-user-trigger.rounded-3 {
  border-radius: var(--radius-md) !important;
}

.top-navbar-branch-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  max-width: 14rem;
  padding: 0.35rem 0.65rem;
  font-size: var(--text-xs);
  font-weight: 500;
  line-height: 1.2;
  color: hsl(var(--foreground));
  background-color: hsl(var(--muted));
  border: 1px solid hsl(var(--border));
  border-radius: var(--radius-md);
  transition: background-color var(--duration-normal) var(--ease-out),
    border-color var(--duration-normal) var(--ease-out);
}

.top-navbar-branch-pill:hover {
  background-color: hsl(var(--accent));
  border-color: hsl(var(--border));
}

.top-navbar-branch-name {
  display: inline-block;
  max-width: 11rem;
  vertical-align: bottom;
}

/* --------------------------------------------------------------------
   Workspace surface (SaaS module pages — high visual impact)
---------------------------------------------------------------------*/
/* min-height for .main-content is set in app-layout.css (100vh + padding-top clears fixed top bar) */
.app-shell .main-content {
  background: linear-gradient(
    180deg,
    hsl(var(--primary) / 0.06) 0,
    hsl(var(--background)) 10rem,
    hsl(var(--background)) 100%
  );
}

/* Wide media: avoid blowing out layout; print layouts use separate views */
.app-shell .main-content-inner img:not([width]),
.app-shell .main-content-inner video {
  max-width: 100%;
  height: auto;
}

.app-shell .table-responsive {
  min-width: 0;
  -webkit-overflow-scrolling: touch;
}

.app-shell .page-header {
  padding: 0;
  margin-bottom: var(--space-6) !important;
  border-bottom: none;
  /* Full width: override app-layout flex row so inner strip is not shrink-wrapped to copy width */
  display: flex;
  flex-direction: column;
  align-items: stretch;
  width: 100%;
}

.app-shell .page-header-inner {
  background-color: hsl(var(--card));
  border: 1px solid hsl(var(--border));
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  padding: var(--space-5) var(--space-6);
  border-left: 4px solid hsl(var(--primary));
  position: relative;
  width: 100%;
  min-width: 0;
  box-sizing: border-box;
}

.app-shell .page-header-inner--with-aside {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-5);
}

.app-shell .page-header-copy {
  flex: 1 1 auto;
  min-width: min(100%, 12rem);
}

.app-shell .page-header-aside {
  flex: 0 0 auto;
  margin-left: auto;
  text-align: center;
}

@media (min-width: 992px) {
  .app-shell .page-header-aside {
    text-align: right;
  }
}

.app-shell .page-header .page-title {
  font-size: clamp(1.35rem, 2.5vw, var(--text-2xl));
  letter-spacing: -0.02em;
  line-height: 1.2;
}

.app-shell .page-header .page-lead {
  font-size: var(--text-sm);
  line-height: 1.55;
  max-width: 52rem;
  margin-top: var(--space-3) !important;
}

.app-shell .page-header .page-breadcrumb {
  margin-top: var(--space-4);
  padding-top: var(--space-3);
  border-top: 1px solid hsl(var(--border));
}

.app-shell .page-header .breadcrumb {
  flex-wrap: wrap;
  row-gap: var(--space-1);
  margin-bottom: 0;
  max-width: 100%;
}

.app-shell .page-header .breadcrumb-item {
  max-width: 100%;
  word-break: break-word;
}

@media (max-width: 991.98px) {
  .app-shell .page-header-inner--with-aside {
    flex-direction: column;
    align-items: stretch;
  }

  .app-shell .page-header-aside {
    margin-left: 0;
    width: 100%;
    text-align: start;
  }
}

/* Action row under masthead */
.app-shell .page-toolbar {
  background-color: hsl(var(--card));
  border: 1px solid hsl(var(--border));
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: var(--space-3) var(--space-4);
  margin-bottom: var(--space-5);
  align-items: center;
}

.app-shell .toolbar-actions {
  flex-wrap: wrap;
  min-width: 0;
}

/* Primary list card: stronger depth + header strip */
.app-shell .dashboard-card {
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  border: 1px solid hsl(var(--border));
  overflow: hidden;
}

.app-shell .dashboard-card .card-header {
  background: linear-gradient(
    90deg,
    hsl(var(--primary) / 0.08),
    hsl(var(--muted) / 0.35) 40%,
    hsl(var(--card)) 100%
  );
  border-bottom: 1px solid hsl(var(--border));
  padding: var(--space-3) var(--space-5);
}

.app-shell .dashboard-card .card-header h5,
.app-shell .dashboard-card .card-header .card-section-title {
  font-size: var(--text-xs);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: hsl(var(--muted-foreground));
  margin-bottom: 0;
}

/* Capitals & KPI-style stat tiles */
.app-shell .stats-card {
  background-color: hsl(var(--card));
  color: hsl(var(--card-foreground));
  border: 1px solid hsl(var(--border));
  border-radius: var(--radius);
  padding: var(--space-6);
  box-shadow: var(--shadow-sm);
  transition: box-shadow var(--duration-normal) var(--ease-out),
    transform var(--duration-normal) var(--ease-out);
}

.app-shell .stats-card:hover {
  box-shadow: var(--shadow-md);
  transform: translateY(-2px);
}

.app-shell .stats-icon {
  width: 2.75rem;
  height: 2.75rem;
  border-radius: var(--radius-md);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.25rem;
}

.app-shell .stats-icon.income {
  background-color: hsl(var(--success) / 0.15);
  color: hsl(var(--success));
}

.app-shell .stats-icon.expense {
  background-color: hsl(var(--destructive) / 0.15);
  color: hsl(var(--destructive));
}

.app-shell .stats-icon.stats-icon-total {
  background-color: hsl(var(--primary) / 0.15);
  color: hsl(var(--primary));
}

.app-shell .stats-number {
  font-size: 1.5rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums lining-nums;
  margin-top: var(--space-2);
}

.app-shell .stats-label {
  font-size: var(--text-xs);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: hsl(var(--muted-foreground));
  margin-top: var(--space-1);
}

/* Amounts in tables */
.app-shell .data-table .col-amount,
.app-shell .data-table .text-end {
  font-variant-numeric: tabular-nums lining-nums;
}

/* Soft badges inside app (Bootstrap compatibility) */
.app-shell .dashboard-card .badge.bg-success {
  background-color: hsl(var(--success) / 0.18) !important;
  color: hsl(var(--success)) !important;
  border: 1px solid hsl(var(--success) / 0.35);
  font-weight: 600;
}

.app-shell .dashboard-card .badge.bg-secondary {
  background-color: hsl(var(--muted)) !important;
  color: hsl(var(--muted-foreground)) !important;
  border: 1px solid hsl(var(--border));
}

.app-shell .dashboard-card .badge.bg-warning,
.app-shell .dashboard-card .badge.bg-warning.text-dark {
  background-color: hsl(var(--warning) / 0.2) !important;
  color: hsl(var(--foreground)) !important;
  border: 1px solid hsl(var(--warning) / 0.45);
}

.app-shell .dashboard-card .badge.bg-info {
  background-color: hsl(var(--info) / 0.18) !important;
  color: hsl(var(--info)) !important;
  border: 1px solid hsl(var(--info) / 0.35);
}

.app-shell .dashboard-card .badge.bg-light.text-dark,
.app-shell .dashboard-card .badge.bg-light {
  background-color: hsl(var(--muted) / 0.5) !important;
  color: hsl(var(--foreground)) !important;
  border: 1px solid hsl(var(--border));
}

/* Callout for legacy / guidance blocks */
.app-shell .module-callout {
  background: linear-gradient(
    120deg,
    hsl(var(--info) / 0.1),
    hsl(var(--card)) 50%
  );
  border: 1px solid hsl(var(--border));
  border-left: 3px solid hsl(var(--info));
  border-radius: var(--radius);
  padding: var(--space-3) var(--space-4);
  margin-bottom: var(--space-4);
  font-size: var(--text-sm);
  box-shadow: var(--shadow-xs);
}

/* Touch-friendly form controls on narrow viewports (skip dense .form-control-sm tables) */
@media (max-width: 768px) {
  .app-shell .form-control:not(.form-control-sm):not(textarea),
  .app-shell .form-select:not(.form-select-sm) {
    min-height: var(--touch-target-min);
  }
}

/* Toolbar on xs: wrap naturally; avoid stacking every button full-width (reduces clutter) */
@media (max-width: 480px) {
  .app-shell .page-toolbar {
    flex-direction: column;
    align-items: stretch;
    gap: var(--space-3);
  }

  .app-shell .page-toolbar .toolbar-actions {
    width: 100%;
    justify-content: flex-start;
  }
}

@media (max-width: 575.98px) {
  .app-shell .page-header-inner {
    padding: var(--space-4) var(--space-3);
  }

  .app-shell .page-toolbar {
    padding: var(--space-3);
  }
}
