/* ═══════════════════════════════════════════════════
   layout.css — Sidebar, main area, grids, responsive
   ═══════════════════════════════════════════════════ */

/* ── Body / Root Layout ─────────────────────────── */
body {
  background: var(--bg-main);
  color: var(--text-primary);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.65;
  height: 100vh;
  height: 100dvh;
  display: flex;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body.sidebar-open-mobile {
  overflow: hidden;
}

/* ── Sidebar Mobile Overlay ─────────────────────── */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 999;
  opacity: 0;
  transition: opacity 0.25s ease;
  pointer-events: none;
  -webkit-tap-highlight-color: transparent;
}

.sidebar-overlay.active {
  display: block;
  opacity: 1;
  pointer-events: auto;
}

/* ── Sidebar Base ────────────────────────────────── */
aside {
  width: 300px;
  background: var(--bg-sidebar, #111111);
  border-right: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  height: 100%;
  flex-shrink: 0;
  color: var(--text-primary);
  transition: width 0.22s cubic-bezier(0.2, 0, 0, 1);
  overflow-x: hidden;
  overflow-y: auto;
  position: relative;
  z-index: 20;
  scrollbar-width: thin;
  scrollbar-color: rgba(255,255,255,0.08) transparent;
}

@media (prefers-reduced-motion: reduce) {
  aside { transition: none !important; }
}

.sidebar-brand-link {
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  color: var(--text-primary, #F5F2F2);
  overflow: hidden;
  flex-shrink: 0;
}

.sidebar-brand-link h1,
.sidebar-brand-link .sidebar-text {
  color: var(--text-primary, #F5F2F2);
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.01em;
  margin: 0;
  line-height: 1;
}

.sidebar-brand-icon {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  object-fit: cover;
  flex-shrink: 0;
  display: block;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 56px;
  padding: 0 14px 0 16px;
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
}

/* ── Collapsed Sidebar Specifics ─────────────────── */
aside.collapsed {
  width: 68px;
}

aside.collapsed .sidebar-text {
  opacity: 0 !important;
  visibility: hidden !important;
  max-width: 0 !important;
  overflow: hidden !important;
  margin: 0 !important;
  padding: 0 !important;
}

aside.collapsed .sidebar-hide-collapsed {
  opacity: 0 !important;
  visibility: hidden !important;
  display: none !important;
}

aside.collapsed .sidebar-menu {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
}

aside.collapsed .sidebar-menu-btn {
  width: 42px !important;
  height: 42px !important;
  padding: 0 !important;
  margin: 0 auto !important;
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  border-radius: 10px !important;
  position: relative;
}

aside.collapsed .sidebar-menu-btn:hover {
  background: rgba(255, 255, 255, 0.08) !important;
  transform: none !important;
}

aside.collapsed .sidebar-menu-btn:hover svg {
  transform: none !important;
}

/* Tooltip on right */
aside.collapsed [data-tooltip]:hover::after {
  content: attr(data-tooltip);
  position: fixed;
  left: 76px;
  background: #202020;
  color: var(--text-primary);
  font-size: 12px;
  font-weight: 500;
  padding: 6px 11px;
  border-radius: 6px;
  white-space: nowrap;
  border: 1px solid var(--border);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.5);
  z-index: 99999;
  pointer-events: none;
  animation: tooltipFadeIn 0.15s ease-out;
}

@keyframes tooltipFadeIn {
  from { opacity: 0; transform: translateX(-4px); }
  to { opacity: 1; transform: translateX(0); }
}

/* Collapsed Footer Rail */
aside.collapsed .sidebar-bottom {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 12px 0 16px;
}

aside.collapsed #sidebar-profile {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
}

aside.collapsed #profile-row {
  width: 42px !important;
  height: 42px !important;
  padding: 0 !important;
  margin: 0 auto !important;
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  border-radius: 10px !important;
}

aside.collapsed .profile-text-col,
aside.collapsed #profile-menu {
  display: none !important;
}

.sidebar-logout-btn {
  display: none;
}

aside.collapsed .sidebar-logout-btn {
  width: 42px !important;
  height: 42px !important;
  padding: 0 !important;
  margin: 0 auto !important;
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  border-radius: 10px !important;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 150ms ease;
}

aside.collapsed .sidebar-logout-btn:hover {
  background: rgba(211, 47, 47, 0.15) !important;
  color: #ff8585 !important;
}

/* Expanded Footer Rail */
.sidebar-bottom {
  margin-top: auto;
  border-top: 1px solid var(--border-subtle);
  padding: 8px 12px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.sidebar-profile-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  transition: background-color 150ms ease;
}

.sidebar-profile-card:hover {
  background: rgba(255, 255, 255, 0.06);
}

.sidebar-avatar-img {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.15);
  background: var(--surface);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 13.5px;
  color: var(--text);
  flex-shrink: 0;
  overflow: hidden;
}

.sidebar-logout-row-btn {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 18px;
  margin: 2px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-secondary);
  background: transparent;
  border: 1px solid transparent;
  width: calc(100% - 20px);
  text-align: left;
  position: relative;
  transition: background-color 150ms ease, color 150ms ease, transform 150ms ease, border-color 150ms ease;
  -webkit-tap-highlight-color: transparent;
}

.sidebar-logout-row-btn:hover {
  background: rgba(211, 47, 47, 0.12);
  color: #ff8585;
}

.sidebar-logout-row-btn:hover svg {
  color: #ff8585;
  transform: translateX(2px);
}

.sidebar-logout-row-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ── Main Content Area ───────────────────────────── */
main {
  flex: 1;
  display: flex;
  flex-direction: column;
  position: relative;
  background: var(--bg-main);
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
}

/* ── Workspace Drag-and-Drop Overlay ────────────── */
.workspace-drop-overlay {
  position: absolute;
  inset: 16px;
  z-index: 900;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(21, 21, 21, 0.85);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 2px dashed var(--accent);
  border-radius: 16px;
  pointer-events: none;
  opacity: 0;
  visibility: hidden;
  transition: opacity 150ms ease, visibility 150ms ease;
}

.workspace-drop-overlay.active {
  opacity: 1;
  visibility: visible;
}

.drop-overlay-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  text-align: center;
  padding: 32px;
}

.drop-overlay-icon {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background: rgba(251, 101, 0, 0.12);
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
}

.drop-overlay-title {
  font-size: 20px;
  font-weight: 600;
  color: var(--text-primary);
  letter-spacing: -0.01em;
}

.drop-overlay-subtitle {
  font-size: 13.5px;
  color: var(--text-muted);
}

@media (prefers-reduced-motion: reduce) {
  .workspace-drop-overlay {
    transition: none !important;
  }
}

/* ── Top Bar ─────────────────────────────────────── */
.top-bar {
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 18px;
  border-bottom: 1px solid var(--border-subtle);
  background: rgba(21, 21, 21, 0.85);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  z-index: 15;
  flex-shrink: 0;
}

.top-bar-left {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
  flex: 1;
}

.top-bar-title {
  font-size: 14px;
  font-weight: 550;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  letter-spacing: -0.01em;
}

.top-bar-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.toggle-sidebar-btn {
  width: 40px;
  height: 40px;
  min-width: 40px;
  border-radius: 8px;
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-secondary);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.15s ease;
}

.toggle-sidebar-btn:hover {
  background: var(--surface);
  color: var(--text-primary);
  border-color: var(--border-subtle);
}

.toggle-sidebar-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.top-doc-viewer-btn {
  height: 38px;
  padding: 0 12px;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-primary);
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
}

.top-doc-viewer-btn:hover {
  background: rgba(255, 255, 255, 0.06);
  border-color: var(--accent);
  color: var(--accent);
}

.top-doc-viewer-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ── Chat Area ───────────────────────────────────── */
.chat-area {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-anchor: auto;
  overscroll-behavior: contain;
  padding: 24px 32px 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  position: relative;
}

.chat-container {
  width: 100%;
  max-width: 800px;
  display: flex;
  flex-direction: column;
  gap: 32px;
  flex: 1;
  /* min-height ensures the empty-state can centre itself even with no messages */
  min-height: 100%;
}

/* Spacer at the bottom of messages list so the last line is never cut off */
.chat-bottom-spacer {
  height: 40px;
  flex-shrink: 0;
  width: 100%;
}

/* ── Input Dock Composer ─────────────────────────── */
.input-area {
  position: relative;
  flex-shrink: 0;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 8px 24px calc(14px + env(safe-area-inset-bottom, 0px));
  background: var(--bg-main);
  z-index: 20;
}

/* ── PDF Pane ────────────────────────────────────── */
#pdf-pane {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: 50vw;
  max-width: 680px;
  background: var(--bg-sidebar);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  flex-shrink: 0;
  transform: translateX(0);
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: 1500;
  box-shadow: -4px 0 24px rgba(0, 0, 0, 0.4);
}
#pdf-pane.collapsed {
  transform: translateX(100%);
  box-shadow: none;
  pointer-events: none;
}

.pdf-pane-header {
  padding: 24px 28px;
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.pdf-pane-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--bg-main);
}

/* ── Floating PDF Toggle ─────────────────────────── */
.floating-pdf-toggle {
  position: absolute;
  top: 50%;
  right: 0;
  transform: translateY(-50%);
  background: var(--bg-sidebar);
  border: 1px solid var(--border);
  border-right: none;
  border-radius: 8px 0 0 8px;
  padding: 12px 8px;
  cursor: pointer;
  color: var(--text-primary);
  z-index: 100;
  display: none;
  align-items: center;
  justify-content: center;
  transition: transform 0.2s, background 0.2s ease;
}
.floating-pdf-toggle:hover { background: var(--bg-hover); }
.floating-pdf-toggle:active { transform: translateY(-50%) scale(0.95); }

/* ── Responsive: Mobile & Tablet Drawer (<1024px) ── */
.mobile-only { display: none; }

@media (max-width: 768px) {
  .desktop-only { display: none !important; }
}

.mobile-sidebar-close-btn {
  width: 44px;
  height: 44px;
  min-width: 44px;
  min-height: 44px;
  border-radius: 8px;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  transition: background 0.15s ease, color 0.15s ease;
  margin-left: auto;
}

.mobile-sidebar-close-btn:hover,
.mobile-sidebar-close-btn:active {
  background: var(--surface);
  color: var(--text-primary);
}

@media (max-width: 1023px) {
  body {
    height: 100vh;
    height: 100dvh;
  }

  body.sidebar-open-mobile {
    overflow: hidden !important;
    touch-action: none;
  }

  main {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
  }

  .mobile-only { display: flex; }

  aside {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 290px;
    max-width: 85vw;
    height: 100vh;
    height: 100dvh;
    z-index: 1000;
    transform: translateX(0);
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.5);
    touch-action: pan-y;
    -webkit-tap-highlight-color: transparent;
  }
  
  aside.collapsed {
    width: 290px;
    max-width: 85vw;
    transform: translateX(-100%);
    box-shadow: none;
    pointer-events: none;
  }

  aside.collapsed .sidebar-text,
  aside.collapsed .sidebar-hide-collapsed {
    opacity: 1 !important;
    visibility: visible !important;
    max-width: none !important;
    display: block !important;
  }

  aside.collapsed .sidebar-menu-btn {
    justify-content: flex-start;
    padding: 12px 16px;
    gap: 12px;
  }

  .top-bar {
    height: 56px;
    justify-content: space-between;
    padding: 0 12px;
    gap: 8px;
    flex-shrink: 0;
    z-index: 50;
  }

  .top-bar-left {
    min-width: 0;
    flex-shrink: 1;
  }

  .top-bar-right {
    min-width: 0;
    flex-shrink: 0;
    gap: 6px;
  }

  .top-doc-viewer-btn {
    padding: 0 8px;
    height: 36px;
  }

  .chat-area {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overflow-anchor: auto;
    overscroll-behavior: contain;
    padding: 16px 12px 0;
    position: relative;
  }

  .chat-container {
    gap: 20px;
    max-width: 100%;
  }

  .input-area {
    position: relative;
    bottom: auto;
    left: auto;
    right: auto;
    padding: 6px 10px calc(8px + env(safe-area-inset-bottom, 0px));
    z-index: 20;
    background: var(--bg-main);
    box-sizing: border-box;
  }

  #pdf-pane {
    width: 100vw;
    max-width: 100vw;
  }

  .empty-state h2 {
    font-size: 28px;
    margin-bottom: 12px;
  }

  .scope-select {
    max-width: 220px;
    font-size: 12px;
    padding: 6px 10px;
    text-overflow: ellipsis;
  }
}
