/* ==========================================================================
   Layout — Sidebar, Header, Content, Responsive
   ========================================================================== */

/* ---------- App Shell ---------- */
.app {
  display: flex;
  min-height: 100vh;
}

/* ---------- Sidebar ---------- */
.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: var(--sidebar-width);
  background: var(--bg-surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: var(--z-sticky);
  overflow-y: auto;
  overflow-x: hidden;
  /* Sidebar width stays fixed; text sizes inherit from global font-size preference */
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-2);
  padding-left: calc(var(--space-2) + var(--space-3));
  border-bottom: 1px solid var(--border);
  text-decoration: none;
  color: var(--text-primary);
  flex-shrink: 0;
  height: 53px;
}

.sidebar-brand svg {
  width: 24px;
  height: 24px;
  flex-shrink: 0;
}

.sidebar-brand-text {
  font-size: var(--text-md);
  font-weight: var(--weight-bold);
  letter-spacing: -0.02em;
}

.sidebar-nav {
  flex: 1;
  padding: var(--space-2) var(--space-2);
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  overflow-y: auto;
  min-height: 0;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-3);
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  color: var(--text-secondary);
  border-radius: var(--radius-md);
  text-decoration: none;
  min-height: 32px;
  transition: all var(--duration-fast) var(--ease-default);
}

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

.nav-item.active {
  background: var(--accent-muted);
  color: var(--accent-text);
}

.nav-item svg {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  opacity: 0.7;
}

.nav-item.active svg {
  opacity: 1;
}

.nav-divider {
  height: 1px;
  background: var(--border);
  margin: var(--space-2) var(--space-3);
}

.nav-section-label {
  font-size: var(--text-2xs);
  font-weight: var(--weight-semibold);
  color: var(--text-tertiary);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: var(--space-4) var(--space-3) var(--space-1);
}

/* Sidebar footer */
.sidebar-footer {
  padding: var(--space-2);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.sidebar-footer {
  font-size: var(--text-xs);
}
.sidebar-footer * {
  font-size: inherit;
}
.sidebar-footer .sidebar-avatar {
  font-size: 8px !important;
  width: 16px !important;
  height: 16px !important;
}
.sidebar-footer .nav-item {
  padding: 4px 8px;
  min-height: 28px;
}
.sidebar-footer .font-size-btn {
  font-size: var(--text-xs);
  width: 22px;
  height: 22px;
}
.sidebar-footer .font-size-btn[data-size="default"] {
  font-size: var(--text-sm);
}
.sidebar-footer .font-size-btn[data-size="large"] {
  font-size: var(--text-base);
}

.sidebar-avatar {
  width: 18px;
  height: 18px;
  border-radius: var(--radius-full);
  background: var(--accent-muted);
  color: var(--accent-text);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 9px;
  font-weight: var(--weight-semibold);
  flex-shrink: 0;
}

.sidebar-user-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: var(--text-sm) !important;
  transition: font-size 150ms ease;
}

.sidebar-footer .nav-item {
  width: 100%;
}

.footer-control {
  gap: var(--space-3);
  min-height: 32px;
}

.font-size-buttons {
  display: flex;
  gap: 2px;
  margin-left: auto;
}

.footer-select {
  flex: 1;
  background: var(--bg-secondary);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 4px 8px;
  font-size: var(--text-sm);
  font-family: var(--font-sans);
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
}

/* Font size buttons */
.font-size-btn {
  width: 26px;
  height: 26px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-tertiary);
  font-weight: var(--weight-semibold);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  transition: all var(--duration-fast);
  font-family: var(--font-sans);
}
.font-size-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}
.font-size-btn.active {
  background: var(--accent-muted);
  border-color: var(--accent);
  color: var(--accent-text);
}

/* ---------- Main Content ---------- */
.main {
  margin-left: var(--sidebar-width);
  width: calc(100vw - var(--sidebar-width));
  max-width: calc(100vw - var(--sidebar-width));
  min-height: 100vh;
  min-width: 0;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
}

/* Page header */
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--space-6);
  height: 53px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-base);
  -webkit-backdrop-filter: none;
  backdrop-filter: none;
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
  flex-shrink: 0;
}

.page-title {
  font-size: var(--text-md);
  font-weight: var(--weight-semibold);
}

.page-subtitle {
  font-size: var(--text-xs);
  color: var(--text-tertiary);
  margin-top: 2px;
}

.page-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

/* Page body */
.page-body {
  flex: 1;
  padding: var(--space-6);
  max-width: var(--content-max-width);
  width: 100%;
}

.page-body-full {
  flex: 1;
  padding: var(--space-6);
  width: 100%;
  min-width: 0;
  max-width: 100%;
  overflow-x: hidden;
  box-sizing: border-box;
}

/* Content footer (disclaimer) */
.content-footer {
  padding: var(--space-1) var(--space-6);
  border-top: none;
  font-size: 11px;
  color: var(--text-quaternary);
  opacity: 0.5;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  flex-shrink: 0;
  letter-spacing: 0.02em;
  transition: opacity 150ms ease;
}
.content-footer:hover {
  opacity: 0.8;
}
.content-footer a {
  color: var(--text-tertiary);
  text-decoration: none;
}
.content-footer a:hover {
  color: var(--text-secondary);
}
.content-footer-sep {
  opacity: 0.4;
}

/* ---------- Grid ---------- */
.grid {
  display: grid;
  gap: var(--space-4);
}

.grid-2 { grid-template-columns: repeat(2, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-4 { grid-template-columns: repeat(4, 1fr); }

/* ---------- Flex Helpers ---------- */
.flex { display: flex; }
.flex-col { flex-direction: column; }
.flex-wrap { flex-wrap: wrap; }
.items-center { align-items: center; }
.items-start { align-items: flex-start; }
.justify-between { justify-content: space-between; }
.justify-center { justify-content: center; }
.justify-end { justify-content: flex-end; }
.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); }
.flex-1 { flex: 1; }

/* ---------- Spacing Helpers ---------- */
.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-5 { margin-top: var(--space-5); }
.mt-6 { margin-top: var(--space-6); }
.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-5 { margin-bottom: var(--space-5); }
.mb-6 { margin-bottom: var(--space-6); }

/* ---------- Text Helpers ---------- */
.text-xs { font-size: var(--text-xs); }
.text-sm { font-size: var(--text-sm); }
.text-base { font-size: var(--text-base); }
.text-md { font-size: var(--text-md); }
.text-lg { font-size: var(--text-lg); }
.text-muted { color: var(--text-secondary); }
.text-tertiary { color: var(--text-tertiary); }
.text-right { text-align: right; }
.text-center { text-align: center; }
.font-medium { font-weight: var(--weight-medium); }
.font-semibold { font-weight: var(--weight-semibold); }
.font-mono { font-family: var(--font-mono); }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- Visibility ---------- */
.hidden { display: none !important; }
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

/* ---------- Mobile Sidebar Overlay ---------- */
.mobile-header {
  display: none;
  position: sticky;
  top: 0;
  left: 0;
  right: 0;
  height: 52px;
  min-height: 52px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  z-index: var(--z-sticky);
  flex-direction: row;
  align-items: center;
  padding: 0 var(--space-4);
  gap: var(--space-3);
}

.mobile-hamburger {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: none;
  background: none;
  color: var(--text-primary);
  cursor: pointer;
  border-radius: var(--radius-sm);
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}
.mobile-hamburger:active { background: var(--bg-hover); }

.mobile-header-action {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  color: var(--text-secondary);
  border-radius: var(--radius-sm);
  -webkit-tap-highlight-color: transparent;
  text-decoration: none;
}
.mobile-header-action:active { background: var(--bg-hover); }

.mobile-brand {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  text-decoration: none;
  color: var(--text-primary);
  font-weight: var(--weight-semibold);
  font-size: var(--text-lg);
}

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: calc(var(--z-modal) - 1);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}
.sidebar-overlay.open { display: block; }

/* ---------- Legacy mobile tab bar (hidden) ---------- */
.mobile-tab-bar { display: none !important; }

/* ---------- Responsive ---------- */
@media (max-width: 1024px) {
  .grid-4 { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 768px) {
  .mobile-header {
    display: flex !important;
    flex-direction: row !important;
    align-items: center !important;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: var(--z-modal);
    transform: translateX(-100%);
    transition: transform 280ms cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: none;
  }
  .sidebar.open {
    transform: translateX(0);
    box-shadow: 4px 0 24px rgba(0,0,0,0.3);
  }

  .app {
    flex-direction: column;
  }

  .main {
    margin-left: 0;
    width: 100vw;
    max-width: 100vw;
    padding-bottom: var(--space-6);
  }

  .page-header {
    padding: var(--space-2) var(--space-3);
    background: var(--bg-base);
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
    height: auto;
    min-height: 0;
  }

  .page-body,
  .page-body-full {
    padding: var(--space-3);
  }

  .grid-2,
  .grid-3,
  .grid-4 {
    grid-template-columns: 1fr;
  }

  .hide-mobile {
    display: none !important;
  }

  /* Tighter page titles on mobile */
  .page-title {
    font-size: var(--text-base) !important;
  }
  .page-subtitle {
    font-size: var(--text-2xs) !important;
    margin-top: 1px !important;
  }

  /* Compact tabs */
  .tabs {
    margin-bottom: var(--space-2);
  }
  .tab {
    padding: var(--space-2) var(--space-2);
    font-size: var(--text-xs);
  }
  .tab svg {
    width: 14px;
    height: 14px;
  }
  .content-footer {
    padding: var(--space-1) var(--space-3);
    font-size: 9px;
    flex-wrap: wrap;
    justify-content: center;
    gap: var(--space-1);
  }
}

@media (max-width: 480px) {
  .page-body,
  .page-body-full {
    padding: var(--space-2);
  }
}

/* Desktop only */
@media (min-width: 769px) {
  .show-mobile-only {
    display: none !important;
  }
}

/* ---------- Search Bar (Global) ---------- */
.search-bar {
  position: relative;
  width: 100%;
  max-width: 600px;
}

.search-bar .input {
  padding-left: 36px;
  height: 40px;
}

.search-bar-icon {
  position: absolute;
  left: var(--space-3);
  top: 50%;
  transform: translateY(-50%);
  width: 16px;
  height: 16px;
  color: var(--text-tertiary);
  pointer-events: none;
}

/* ---------- Content States ---------- */
.loading-state {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-12);
}

.error-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--space-12) var(--space-6);
  text-align: center;
}

.error-state p {
  color: var(--text-tertiary);
  font-size: var(--text-sm);
  max-width: 400px;
}

/* Global SVG constraint — prevent unconstrained SVGs from exploding */
.main svg:not([width]):not(.hero-bg svg):not(.auth-brand svg) {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
}
