/* ── App Shell Layout ────────────────────────────────────────────────────── */

/* Root shell grid */
#app {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ── Sidebar ─────────────────────────────────────────────────────────────── */
#sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  height: 100vh;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  overflow-x: hidden;
  flex-shrink: 0;
  position: relative;
  z-index: 20;
}

/* Logo / wordmark */
.sidebar-logo {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-5) var(--sp-4);
  text-decoration: none;
  margin-bottom: var(--sp-1);
}

.sidebar-logo-glyph {
  width: 28px;
  height: 28px;
  background: var(--accent);
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.sidebar-logo-glyph svg { display: block; }

.sidebar-logo-text {
  font-size: var(--fs-subhead);
  font-weight: 700;
  color: var(--text);
  letter-spacing: -0.3px;
}

/* Nav section label */
.sidebar-section-label {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--text-muted);
  padding: var(--sp-3) var(--sp-4) var(--sp-2);
  margin-top: var(--sp-1);
}

/* Nav item */
.sidebar-nav {
  padding: 0 var(--sp-2);
  display: flex;
  flex-direction: column;
  gap: 1px;
  flex: 1;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: 7px 10px;
  border-radius: var(--radius-sm);
  font-size: var(--fs-body-sm);
  font-weight: 500;
  color: var(--text-secondary);
  cursor: pointer;
  text-decoration: none;
  position: relative;
  transition: background 0.12s, color 0.12s;
  user-select: none;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
}

.nav-item:hover {
  background: var(--surface-muted);
  color: var(--text);
  text-decoration: none;
}

.nav-item.active {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}

.nav-item.active::before {
  content: '';
  position: absolute;
  left: -8px;
  top: 50%;
  transform: translateY(-50%);
  width: 3px;
  height: 18px;
  background: var(--accent);
  border-radius: 0 2px 2px 0;
}

.nav-item-icon {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  opacity: .7;
}

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

.sidebar-footer {
  padding: var(--sp-4);
  border-top: 1px solid var(--border);
  margin-top: auto;
}

/* ── Main content wrapper ────────────────────────────────────────────────── */
#main-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

/* ── Top bar ─────────────────────────────────────────────────────────────── */
#topbar {
  height: var(--topbar-height);
  min-height: var(--topbar-height);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: var(--sp-4);
  padding: 0 var(--sp-8);
  position: sticky;
  top: 0;
  z-index: 10;
}

.topbar-breadcrumb {
  display: flex;
  align-items: center;
  flex-shrink: 0;
  min-width: 0;
}

/* Universal search */
.topbar-search {
  flex: 1;
  max-width: 480px;
  position: relative;
}

.topbar-search-input {
  width: 100%;
  height: 34px;
  padding: 0 var(--sp-4) 0 36px;
  background: var(--surface-muted);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: var(--fs-body-sm);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s, background 0.15s;
}

.topbar-search-input:focus {
  background: var(--surface);
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(47,111,237,.12);
}

.topbar-search-input::placeholder { color: var(--text-muted); }

.topbar-search-icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
}

/* Search dropdown */
.search-dropdown {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index: 100;
  overflow: hidden;
  display: none;
}

.search-dropdown.open { display: block; }

.search-dropdown-section { padding: var(--sp-2) 0; }

.search-dropdown-label {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--text-muted);
  padding: var(--sp-1) var(--sp-4);
}

.search-dropdown-item {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: 7px var(--sp-4);
  font-size: var(--fs-body-sm);
  cursor: pointer;
  transition: background 0.1s;
}

.search-dropdown-item:hover { background: var(--surface-muted); }

.search-dropdown-item-label { font-weight: 500; }

.search-dropdown-item-sub {
  font-size: var(--fs-caption);
  color: var(--text-muted);
}

/* Top bar right cluster */
.topbar-actions {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  flex-shrink: 0;
}

/* Notification bell */
.topbar-bell {
  position: relative;
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  cursor: pointer;
  background: var(--surface);
  color: var(--text-secondary);
  transition: background 0.12s;
}

.topbar-bell:hover { background: var(--surface-muted); }

.topbar-bell-badge {
  position: absolute;
  top: 3px;
  right: 3px;
  width: 16px;
  height: 16px;
  background: var(--status-danger-fg);
  color: #fff;
  font-size: 9px;
  font-weight: 700;
  border-radius: 99px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Create menu */
.create-menu-wrapper {
  position: relative;
}

.create-menu-dropdown {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index: 100;
  min-width: 180px;
  display: none;
  overflow: hidden;
}

.create-menu-dropdown.open { display: block; }

.create-menu-item {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: 9px var(--sp-4);
  font-size: var(--fs-body-sm);
  cursor: pointer;
  transition: background 0.1s;
  color: var(--text);
}

.create-menu-item:hover { background: var(--surface-muted); }

/* Sourcing platform link */
.topbar-platform-link {
  display: flex;
  align-items: center;
  gap: var(--sp-1);
  font-size: var(--fs-body-sm);
  font-weight: 500;
  color: var(--text-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 5px 10px;
  background: var(--surface);
  cursor: pointer;
  transition: background 0.12s, color 0.12s;
  white-space: nowrap;
}

.topbar-platform-link:hover {
  background: var(--surface-muted);
  color: var(--text);
  text-decoration: none;
}

/* ── Content area ────────────────────────────────────────────────────────── */
#content {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  background: var(--bg);
}

.page-container {
  max-width: 1440px;
  padding: var(--sp-6) var(--sp-8);
  margin: 0 auto;
}

/* Narrower layout variant */
.page-container-sm {
  max-width: 960px;
}

/* ═══════════════════════════════════════════════════════════════════════════
   RESPONSIVE — sidebar collapse + 1280px → 1024px usable range
   ═══════════════════════════════════════════════════════════════════════════ */

/* Sidebar collapse toggle button — hidden on wide screens */
.sidebar-collapse-btn {
  display: none;
  position: absolute;
  top: 50%;
  right: -12px;
  transform: translateY(-50%);
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  cursor: pointer;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  color: var(--text-secondary);
  z-index: 30;
  box-shadow: 0 1px 4px rgba(0,0,0,.12);
  transition: background 0.12s;
}
.sidebar-collapse-btn:hover { background: var(--surface-muted); color: var(--text); }

/* Hamburger button — hidden on desktop, shown via media query */
.sidebar-hamburger { display: none; }
.sidebar-backdrop { display: none; }

/* ── At 1280px: tighten page padding ────────────────────────────────────── */
@media (max-width: 1280px) {
  .page-container { padding: var(--sp-5) var(--sp-6); }
}

/* ── At 1100px: show collapse button, reduce sidebar ────────────────────── */
@media (max-width: 1100px) {
  .sidebar-collapse-btn { display: flex; }

  .page-container { padding: var(--sp-4) var(--sp-5); }

  /* Tighten tab bars */
  .tab-bar { overflow-x: auto; }
  .tab-bar-item { padding: 10px 12px; font-size: 12px; }
}

/* ── At 1024px: icon-only sidebar ───────────────────────────────────────── */
@media (max-width: 1024px) {
  :root { --sidebar-width: 52px; }

  #sidebar {
    width: 52px;
    min-width: 52px;
    overflow: visible;  /* allow tooltip on hover */
  }

  /* Hide text labels and section headers */
  .sidebar-logo-text,
  .sidebar-section-label,
  .nav-item-label,
  .sidebar-footer-name,
  .sidebar-footer-email { display: none; }

  /* Center logo glyph */
  .sidebar-logo { padding: var(--sp-4) var(--sp-2); justify-content: center; }

  /* Center nav items */
  .nav-item {
    justify-content: center;
    padding: 8px;
    gap: 0;
    position: relative;
  }

  /* Tooltip on hover */
  .nav-item[title]:hover::after {
    content: attr(title);
    position: absolute;
    left: calc(100% + 8px);
    top: 50%;
    transform: translateY(-50%);
    background: var(--text);
    color: var(--surface);
    font-size: 11px;
    font-weight: 500;
    padding: 4px 8px;
    border-radius: var(--radius-sm);
    white-space: nowrap;
    pointer-events: none;
    z-index: 100;
    box-shadow: 0 2px 8px rgba(0,0,0,.2);
  }

  .nav-item-icon {
    width: 20px;
    height: 20px;
    opacity: 1;
  }

  /* Footer avatar only */
  .sidebar-footer { padding: var(--sp-2); display: flex; justify-content: center; }

  /* Page padding tighter */
  .page-container { padding: var(--sp-4); }

  /* Settings nav stacks under content on narrow */
  .settings-layout { flex-direction: column; }
  .settings-nav    { width: 100%; position: static; flex-direction: row; overflow-x: auto; padding: 0; border-radius: var(--radius); }
  .settings-nav-item { padding: 8px 12px; border-left: none; border-bottom: 3px solid transparent; flex-shrink: 0; }
  .settings-nav-item.active { border-bottom-color: var(--accent); border-left-color: transparent; }
  .settings-nav-sep { display: none; }
  .settings-content { margin-left: 0; margin-top: var(--sp-3); }
}

/* ── At 768px: hide sidebar, show hamburger, single column ──────────────── */
@media (max-width: 768px) {
  /* Hide sidebar off-screen by default */
  #sidebar {
    position: fixed;
    left: 0;
    top: 0;
    width: 260px;
    min-width: 260px;
    height: 100vh;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    z-index: 1000;
    box-shadow: none;
  }

  /* Show sidebar when .open class is added */
  #sidebar.open {
    transform: translateX(0);
    box-shadow: 4px 0 24px rgba(0,0,0,.18);
  }

  /* Restore sidebar text labels when open on mobile */
  #sidebar.open .sidebar-logo-text,
  #sidebar.open .sidebar-section-label,
  #sidebar.open .nav-item-label,
  #sidebar.open .sidebar-footer-name,
  #sidebar.open .sidebar-footer-email { display: unset; }

  #sidebar.open .sidebar-logo { padding: var(--sp-5) var(--sp-4); justify-content: flex-start; }
  #sidebar.open .nav-item { justify-content: flex-start; padding: 7px 10px; gap: var(--sp-3); }
  #sidebar.open .nav-item-icon { width: 18px; height: 18px; }
  #sidebar.open .sidebar-footer { justify-content: flex-start; padding: var(--sp-4); }

  /* Overlay backdrop */
  .sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.4);
    z-index: 999;
  }

  .sidebar-backdrop.open { display: block; }

  /* Hamburger button */
  .sidebar-hamburger {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    cursor: pointer;
    color: var(--text-secondary);
    flex-shrink: 0;
    transition: background 0.12s;
  }

  .sidebar-hamburger:hover { background: var(--surface-muted); color: var(--text); }

  /* Topbar adjustments */
  .topbar-platform-link { display: none; }
  .topbar-search { max-width: 140px; }
  .topbar-search input { font-size: 12px; }
  #topbar { padding: 0 var(--sp-3); gap: var(--sp-2); }

  /* Stack workspace header */
  .ws-header { flex-wrap: wrap; gap: var(--sp-2); }
  .ws-header-actions { flex-wrap: wrap; }

  /* Narrower compare matrix — horizontal scroll */
  .compare-table-wrap, .rfq-compare-wrap { overflow-x: auto; }

  /* Page container */
  .page-container { padding: var(--sp-3); }
}

/* ── Accessibility: visible focus outlines ───────────────────────────────── */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

button:focus-visible,
a:focus-visible,
[role="button"]:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Ensure modals trap focus visually */
.modal-backdrop { isolation: isolate; }

/* Skip-to-content link (screen readers) */
.skip-link {
  position: absolute;
  top: -40px;
  left: var(--sp-4);
  background: var(--accent);
  color: #fff;
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  font-size: var(--fs-body-sm);
  font-weight: 600;
  text-decoration: none;
  z-index: 9999;
  transition: top 0.12s;
}
.skip-link:focus { top: var(--sp-4); }
