/**
 * CoreSense Base Styles
 * =====================
 * Reset, typography, and foundational styles.
 * Requires: tokens.css
 *
 * Usage:
 *   <link rel="stylesheet" href="/design-system/tokens.css">
 *   <link rel="stylesheet" href="/design-system/base.css">
 */

/* ============================================
   CSS RESET
   ============================================ */

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  -webkit-text-size-adjust: 100%;
  tab-size: 4;
}

body {
  font-family: var(--font-sans);
  font-size: var(--text-base);
  font-weight: var(--font-normal);
  line-height: var(--leading-normal);
  color: var(--text-secondary);
  background-color: var(--bg-page);
  min-height: 100vh;
  min-height: 100dvh;
}

/* Remove default list styles */
ul, ol {
  list-style: none;
}

/* Remove default anchor styles */
a {
  color: inherit;
  text-decoration: none;
}

/* Remove default button styles */
button {
  font: inherit;
  color: inherit;
  background: none;
  border: none;
  cursor: pointer;
}

/* Remove default input styles */
input, textarea, select {
  font: inherit;
  color: inherit;
}

/* Remove default table styles */
table {
  border-collapse: collapse;
  border-spacing: 0;
}

/* Make images easier to work with */
img, picture, video, canvas, svg {
  display: block;
  max-width: 100%;
}

/* Avoid text overflows */
p, h1, h2, h3, h4, h5, h6 {
  overflow-wrap: break-word;
}

/* ============================================
   FOCUS STYLES
   ============================================ */

*:focus {
  outline: none;
}

/* Modern ring focus pattern */
*:focus-visible {
  outline: none;
  box-shadow: 0 0 0 var(--ring-offset) var(--bg-page),
              0 0 0 calc(var(--ring-offset) + var(--ring-width)) var(--ring-color);
}

/* ============================================
   SELECTION
   ============================================ */

::selection {
  background-color: var(--gray-200);
  color: var(--text-primary);
}

/* ============================================
   SCROLLBAR STYLING
   ============================================ */

/* WebKit browsers - thin, subtle scrollbar */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--gray-300);
  border-radius: var(--radius-full);
}

::-webkit-scrollbar-thumb:hover {
  background: var(--gray-400);
}

/* Firefox - thin scrollbar */
* {
  scrollbar-width: thin;
  scrollbar-color: var(--gray-300) transparent;
}

/* ============================================
   TYPOGRAPHY
   ============================================ */

/* Headings */
h1, .h1 {
  font-size: var(--text-3xl);
  font-weight: var(--font-bold);
  letter-spacing: var(--tracking-tighter);
  line-height: var(--leading-tight);
  color: var(--text-primary);
}

h2, .h2 {
  font-size: var(--text-2xl);
  font-weight: var(--font-semibold);
  letter-spacing: var(--tracking-tight);
  line-height: var(--leading-tight);
  color: var(--text-primary);
}

h3, .h3 {
  font-size: var(--text-xl);
  font-weight: var(--font-semibold);
  letter-spacing: var(--tracking-tight);
  line-height: var(--leading-snug);
  color: var(--text-primary);
}

h4, .h4 {
  font-size: var(--text-lg);
  font-weight: var(--font-medium);
  line-height: var(--leading-snug);
  color: var(--text-secondary);
}

h5, .h5 {
  font-size: var(--text-base);
  font-weight: var(--font-medium);
  line-height: var(--leading-normal);
  color: var(--text-secondary);
}

h6, .h6 {
  font-size: var(--text-sm);
  font-weight: var(--font-semibold);
  letter-spacing: var(--tracking-wide);
  text-transform: uppercase;
  line-height: var(--leading-normal);
  color: var(--text-tertiary);
}

/* Body text */
p {
  margin-bottom: var(--space-4);
}

p:last-child {
  margin-bottom: 0;
}

/* Small text */
small, .text-small, .text-sm {
  font-size: var(--text-sm);
}

/* Muted text */
.text-muted {
  color: var(--text-muted);
}

.text-tertiary {
  color: var(--text-tertiary);
}

/* Links */
a:not(.btn):not(.nav-item):not(.logo-link) {
  color: var(--color-accent);
  transition: color var(--duration-instant) var(--ease-linear);
}

a:not(.btn):not(.nav-item):not(.logo-link):hover {
  color: var(--color-accent-hover);
  text-decoration: underline;
}

/* Code */
code, kbd, samp {
  font-family: var(--font-mono);
  font-size: 0.9em;
}

code {
  background: var(--bg-elevated);
  padding: var(--space-0-5) var(--space-1);
  border-radius: var(--radius-sm);
}

pre {
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  background: var(--bg-elevated);
  padding: var(--space-4);
  border-radius: var(--radius-lg);
  overflow-x: auto;
}

pre code {
  background: none;
  padding: 0;
}

/* ============================================
   DATA FORMATTING
   ============================================ */

/* Monospace data values */
.data, .sku, .price, .quantity {
  font-family: var(--font-mono);
  font-size: var(--text-data);
  font-variant-numeric: tabular-nums;
}

/* SKU styling */
.sku {
  color: var(--text-secondary);
  letter-spacing: 0.01em;
}

/* Price styling */
.price {
  color: var(--text-secondary);
}

.price-positive {
  color: var(--color-price-up);
}

.price-negative {
  color: var(--color-price-down);
}

/* Quantity styling */
.quantity {
  color: var(--text-secondary);
}

/* Column headers */
.column-header, th {
  font-size: var(--text-xs);
  font-weight: var(--font-semibold);
  letter-spacing: var(--tracking-wider);
  text-transform: uppercase;
  color: var(--text-muted);
}

/* ============================================
   LAYOUT UTILITIES
   ============================================ */

.container {
  width: 100%;
  max-width: var(--width-content-max);
  margin-left: auto;
  margin-right: auto;
  padding-left: var(--space-6);
  padding-right: var(--space-6);
}

/* Page layout with sidebar */
.layout {
  display: flex;
  min-height: 100vh;
  min-height: 100dvh;
}

.layout-sidebar {
  width: var(--width-sidebar);
  flex-shrink: 0;
  background: var(--bg-card);
  border-right: 1px solid var(--border-subtle);
  position: sticky;
  top: 0;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.layout-main {
  flex: 1;
  min-width: 0;
  padding: var(--space-6);
}

/* ============================================
   FLEXBOX UTILITIES
   ============================================ */

.flex { display: flex; }
.inline-flex { display: inline-flex; }
.flex-col { flex-direction: column; }
.flex-row { flex-direction: row; }
.flex-wrap { flex-wrap: wrap; }
.flex-nowrap { flex-wrap: nowrap; }
.flex-1 { flex: 1 1 0%; }
.flex-auto { flex: 1 1 auto; }
.flex-none { flex: none; }

.items-start { align-items: flex-start; }
.items-center { align-items: center; }
.items-end { align-items: flex-end; }
.items-stretch { align-items: stretch; }
.items-baseline { align-items: baseline; }

.justify-start { justify-content: flex-start; }
.justify-center { justify-content: center; }
.justify-end { justify-content: flex-end; }
.justify-between { justify-content: space-between; }
.justify-around { justify-content: space-around; }
.justify-evenly { justify-content: space-evenly; }

.gap-1 { gap: var(--space-1); }
.gap-2 { gap: var(--space-2); }
.gap-3 { gap: var(--space-3); }
.gap-4 { gap: var(--space-4); }
.gap-5 { gap: var(--space-5); }
.gap-6 { gap: var(--space-6); }
.gap-8 { gap: var(--space-8); }

/* ============================================
   GRID UTILITIES
   ============================================ */

.grid { display: grid; }
.grid-cols-1 { grid-template-columns: repeat(1, minmax(0, 1fr)); }
.grid-cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid-cols-6 { grid-template-columns: repeat(6, minmax(0, 1fr)); }
.grid-cols-12 { grid-template-columns: repeat(12, minmax(0, 1fr)); }

.col-span-1 { grid-column: span 1 / span 1; }
.col-span-2 { grid-column: span 2 / span 2; }
.col-span-3 { grid-column: span 3 / span 3; }
.col-span-4 { grid-column: span 4 / span 4; }
.col-span-6 { grid-column: span 6 / span 6; }
.col-span-full { grid-column: 1 / -1; }

/* ============================================
   SPACING UTILITIES
   ============================================ */

/* Margin */
.m-0 { margin: 0; }
.m-1 { margin: var(--space-1); }
.m-2 { margin: var(--space-2); }
.m-3 { margin: var(--space-3); }
.m-4 { margin: var(--space-4); }
.m-auto { margin: auto; }

.mt-0 { margin-top: 0; }
.mt-1 { margin-top: var(--space-1); }
.mt-2 { margin-top: var(--space-2); }
.mt-3 { margin-top: var(--space-3); }
.mt-4 { margin-top: var(--space-4); }
.mt-6 { margin-top: var(--space-6); }
.mt-8 { margin-top: var(--space-8); }

.mb-0 { margin-bottom: 0; }
.mb-1 { margin-bottom: var(--space-1); }
.mb-2 { margin-bottom: var(--space-2); }
.mb-3 { margin-bottom: var(--space-3); }
.mb-4 { margin-bottom: var(--space-4); }
.mb-6 { margin-bottom: var(--space-6); }
.mb-8 { margin-bottom: var(--space-8); }

.ml-auto { margin-left: auto; }
.mr-auto { margin-right: auto; }

/* Padding */
.p-0 { padding: 0; }
.p-1 { padding: var(--space-1); }
.p-2 { padding: var(--space-2); }
.p-3 { padding: var(--space-3); }
.p-4 { padding: var(--space-4); }
.p-6 { padding: var(--space-6); }
.p-8 { padding: var(--space-8); }

.px-2 { padding-left: var(--space-2); padding-right: var(--space-2); }
.px-3 { padding-left: var(--space-3); padding-right: var(--space-3); }
.px-4 { padding-left: var(--space-4); padding-right: var(--space-4); }
.px-6 { padding-left: var(--space-6); padding-right: var(--space-6); }

.py-2 { padding-top: var(--space-2); padding-bottom: var(--space-2); }
.py-3 { padding-top: var(--space-3); padding-bottom: var(--space-3); }
.py-4 { padding-top: var(--space-4); padding-bottom: var(--space-4); }
.py-6 { padding-top: var(--space-6); padding-bottom: var(--space-6); }

/* ============================================
   TEXT UTILITIES
   ============================================ */

.text-left { text-align: left; }
.text-center { text-align: center; }
.text-right { text-align: right; }

.font-mono { font-family: var(--font-mono); }
.font-normal { font-weight: var(--font-normal); }
.font-medium { font-weight: var(--font-medium); }
.font-semibold { font-weight: var(--font-semibold); }
.font-bold { font-weight: var(--font-bold); }

.uppercase { text-transform: uppercase; }
.lowercase { text-transform: lowercase; }
.capitalize { text-transform: capitalize; }

.truncate {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ============================================
   VISIBILITY UTILITIES
   ============================================ */

.hidden { display: none; }
.invisible { visibility: hidden; }
.visible { visibility: visible; }

.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;
}

/* ============================================
   RESPONSIVE UTILITIES
   ============================================ */

@media (max-width: 640px) {
  .sm\:hidden { display: none; }
  .sm\:flex { display: flex; }
  .sm\:grid-cols-1 { grid-template-columns: repeat(1, minmax(0, 1fr)); }
  .sm\:flex-col { flex-direction: column; }
}

@media (max-width: 768px) {
  .md\:hidden { display: none; }
  .md\:flex { display: flex; }
  .md\:grid-cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 1024px) {
  .lg\:hidden { display: none; }
  .lg\:flex { display: flex; }
  .lg\:grid-cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

/* ============================================
   COMPACT SIDEBAR (768px - 1024px)
   Icons only, 64px width
   ============================================ */

@media (min-width: 769px) and (max-width: 1024px) {
  .layout-sidebar {
    width: var(--width-sidebar-collapsed);
  }

  /* Center nav items and hide text labels */
  .layout-sidebar .nav-item {
    justify-content: center;
    padding: var(--space-3);
  }

  .layout-sidebar .nav-item-text,
  .layout-sidebar .nav-category-header,
  .layout-sidebar .sidebar-footer,
  .layout-sidebar .sidebar-logo-text,
  .layout-sidebar .sidebar-subtitle {
    display: none;
  }

  /* Always show nav items in compact mode (even if category was collapsed) */
  .layout-sidebar .nav-category-items {
    display: flex !important;
    flex-direction: column;
    max-height: none !important;
    overflow: visible !important;
  }

  /* Make icons slightly larger in compact mode */
  .layout-sidebar .nav-item-icon,
  .layout-sidebar .nav-item > svg {
    width: 24px;
    height: 24px;
  }

  /* Tooltip on hover for nav items */
  .layout-sidebar .nav-item {
    position: relative;
  }

  .layout-sidebar .nav-item::after {
    content: attr(data-title);
    position: absolute;
    left: calc(100% + var(--space-2));
    top: 50%;
    transform: translateY(-50%);
    background: var(--gray-11);
    color: var(--gray-1);
    padding: var(--space-1) var(--space-2);
    border-radius: var(--radius-sm);
    font-size: var(--text-sm);
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--duration-fast) var(--ease-out);
    z-index: var(--z-tooltip);
  }

  .layout-sidebar .nav-item:hover::after {
    opacity: 1;
  }

  /* Adjust main content to account for smaller sidebar */
  .layout-main {
    padding-left: var(--space-4);
  }
}

/* ============================================
   MOBILE SIDEBAR (<768px)
   Hidden by default, slide-in drawer
   ============================================ */

@media (max-width: 768px) {
  .layout-sidebar {
    position: fixed;
    left: -100%;
    top: 0;
    z-index: var(--z-fixed);
    width: var(--width-sidebar);
    height: 100vh;
    height: calc(100dvh - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
    transition: left var(--duration-normal) var(--ease-out);
  }

  .layout-sidebar.open {
    left: 0;
  }

  /* Backdrop when sidebar is open */
  .layout-sidebar.open::after {
    content: '';
    position: fixed;
    top: 0;
    left: var(--width-sidebar);
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.3);
    z-index: -1;
  }

  /* Mobile menu toggle button (shown by cn-shell) */
  .mobile-menu-toggle {
    display: flex;
    position: fixed;
    top: calc(var(--space-3) + env(safe-area-inset-top, 0px));
    left: calc(var(--space-3) + env(safe-area-inset-left, 0px));
    z-index: var(--z-fixed);
    width: 40px;
    height: 40px;
    align-items: center;
    justify-content: center;
    background: var(--bg-card);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    cursor: pointer;
  }

  /* Give main content some breathing room for mobile toggle */
  .layout-main {
    padding-top: calc(var(--space-6) + 48px);
  }
}

/* Hide mobile toggle on larger screens */
@media (min-width: 769px) {
  .mobile-menu-toggle {
    display: none;
  }
}

/* ============================================
   CN-SHELL PRE-DEFINITION (FOUC Prevention)
   ============================================ */

/* Reserve layout space before JavaScript defines the component */
cn-shell:not(:defined) {
  display: flex;
  min-height: 100vh;
  min-height: 100dvh;
}

/* Create skeleton sidebar placeholder */
cn-shell:not(:defined)::before {
  content: '';
  width: var(--width-sidebar);
  flex-shrink: 0;
  background: var(--bg-card);
  border-right: 1px solid var(--border-subtle);
}

/* Position original content in main area */
cn-shell:not(:defined) > * {
  flex: 1;
  min-width: 0;
  padding: var(--space-6);
}

/* Once defined, reset display (component handles its own layout) */
cn-shell:defined {
  display: block;
}

/* FOUC skeleton for compact mode */
@media (min-width: 769px) and (max-width: 1024px) {
  cn-shell:not(:defined)::before {
    width: var(--width-sidebar-collapsed);
  }
}

/* FOUC skeleton for mobile - no sidebar placeholder */
@media (max-width: 768px) {
  cn-shell:not(:defined)::before {
    display: none;
  }

  cn-shell:not(:defined) > * {
    padding-top: calc(var(--space-6) + 48px);
  }
}
