/* ==========================================================================
   GLOBAL APPLICATION SHELL STYLES
   - Top Header Bar (Sticky, 64px)
   - Left Desktop Sidebar (240px, Collapsible to 72px)
   - Responsive Mobile Bottom Navigation (< 760px)
   - Command Palette Search Dialog
   - Account Popover Menu
   ========================================================================== */

/* Layout Shell Container */
#workspace {
  position: relative;
  min-height: 100vh;
  padding-left: var(--sidebar-width);
  transition: padding-left 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  box-sizing: border-box;
}

#workspace.sidebar-collapsed {
  --sidebar-width: var(--sidebar-width-collapsed);
}

/* ==========================================================================
   TOP APPLICATION HEADER
   ========================================================================== */
#workspace > .app-header {
  position: sticky;
  top: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: var(--space-3);
  height: var(--header-height);
  min-height: var(--header-height);
  padding: 0 var(--space-6);
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  box-shadow: var(--shadow-xs);
  box-sizing: border-box;
}

/* Header Brand (Shown on Mobile when sidebar is hidden) */
.app-header .brand {
  display: none;
  font-weight: 800;
  font-size: 15px;
  letter-spacing: 0.04em;
  color: var(--color-navy);
  margin-right: auto;
}

.app-header .brand small {
  font-size: 10px;
  font-weight: 600;
  color: var(--color-muted);
  display: block;
}

/* ==========================================================================
   DESKTOP SIDEBAR NAVIGATION (.primary-nav)
   ========================================================================== */
#workspace > .app-header .primary-nav {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  width: var(--sidebar-width);
  z-index: 50;
  display: flex;
  flex-direction: column;
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  padding: var(--space-4) var(--space-3);
  overflow-y: auto;
  overflow-x: hidden;
  box-sizing: border-box;
  box-shadow: var(--shadow-xs);
  transition: width 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Sidebar Brand Header */
.sidebar-brand-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-2) var(--space-2) var(--space-4);
  margin-bottom: var(--space-2);
  border-bottom: 1px solid var(--color-border-subtle);
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.brand-badge {
  background: var(--color-primary);
  color: #FFFFFF;
  font-size: 11px;
  font-weight: 800;
  padding: 3px 7px;
  border-radius: var(--radius-xs);
  letter-spacing: 0.05em;
}

.brand-text {
  font-size: 16px;
  font-weight: 800;
  letter-spacing: 0.02em;
  color: var(--color-text);
}

#sidebar-collapse-btn {
  min-height: 32px;
  min-width: 32px;
  padding: 0;
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  color: var(--color-muted);
  font-size: 12px;
  display: grid;
  place-items: center;
  box-shadow: none;
}

#sidebar-collapse-btn:hover {
  background: var(--color-surface-alt);
  color: var(--color-text);
}

/* Sidebar Section Titles */
.sidebar-section-title {
  font-size: 11px;
  font-weight: 700;
  color: var(--color-muted);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  padding: var(--space-3) var(--space-3) var(--space-1);
}

/* Sidebar Nav Buttons (Refined Linear-style item) */
.primary-nav button {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  width: 100%;
  min-height: 36px;
  padding: 6px 10px;
  margin-bottom: 2px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--color-text-secondary);
  font-size: 13px;
  font-weight: 500;
  text-align: left;
  box-shadow: none;
  transition: all 0.12s ease;
  white-space: nowrap;
}

.primary-nav button:before {
  content: attr(data-icon);
  font-size: 15px;
  width: 20px;
  margin-right: 8px;
  text-align: center;
  flex-shrink: 0;
}

.primary-nav button:hover {
  background: var(--color-surface-alt);
  color: var(--color-text);
}

/* Active Nav State */
.primary-nav button[aria-current="page"] {
  background: var(--color-primary-light) !important;
  color: var(--color-primary) !important;
  font-weight: 600 !important;
  border-color: rgba(28, 103, 229, 0.12) !important;
  border-left: 3px solid var(--color-primary) !important;
  box-shadow: 0 1px 2px rgba(28, 103, 229, 0.05) !important;
}

/* Navigation Groups (Details / Summary) */
.primary-nav .nav-group {
  margin-top: var(--space-2);
}

.primary-nav .nav-group > summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 10.5px;
  font-weight: 700;
  color: var(--color-muted);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  padding: var(--space-2) var(--space-2);
  cursor: pointer;
  list-style: none;
}

.primary-nav .nav-group > summary::-webkit-details-marker {
  display: none;
}

.nav-chevron {
  font-size: 11px;
  transition: transform 0.2s ease;
}

.nav-group[open] .nav-chevron {
  transform: rotate(180deg);
}

.nav-panel {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding-left: 0;
}

/* Sidebar Collapsed State */
#workspace.sidebar-collapsed,
#workspace[data-collapsed="true"] {
  --sidebar-width: var(--sidebar-width-collapsed);
}

#workspace.sidebar-collapsed .sidebar-brand .brand-text,
#workspace.sidebar-collapsed .sidebar-section-title,
#workspace.sidebar-collapsed .nav-group > summary > span:first-child,
#workspace.sidebar-collapsed .nav-group .nav-chevron,
#workspace[data-collapsed="true"] .sidebar-brand .brand-text,
#workspace[data-collapsed="true"] .sidebar-section-title,
#workspace[data-collapsed="true"] .nav-group > summary > span:first-child,
#workspace[data-collapsed="true"] .nav-group .nav-chevron {
  display: none !important;
}

#workspace.sidebar-collapsed .primary-nav button,
#workspace[data-collapsed="true"] .primary-nav button {
  justify-content: center;
  padding: 8px 0;
  font-size: 0; /* Hides label text */
}

#workspace.sidebar-collapsed .primary-nav button:before,
#workspace[data-collapsed="true"] .primary-nav button:before {
  margin-right: 0;
  font-size: 18px;
}

/* Tablet Responsive: collapse desktop sidebar to icon rail (760px - 1024px) */
@media (max-width: 1024px) and (min-width: 760px) {
  #workspace {
    --sidebar-width: var(--sidebar-width-collapsed);
  }

  .sidebar-brand .brand-text,
  .sidebar-section-title,
  .nav-group > summary > span:first-child,
  .nav-group .nav-chevron {
    display: none !important;
  }

  .primary-nav button {
    justify-content: center !important;
    padding: 8px 0 !important;
    font-size: 0 !important;
  }

  .primary-nav button:before {
    margin-right: 0 !important;
    font-size: 18px !important;
  }
}

/* ==========================================================================
   TOP HEADER ACTIONS
   ========================================================================== */

/* Ask AI Button */
#global-ai-btn {
  background: var(--color-primary-light);
  color: var(--color-primary);
  border: 1px solid rgba(28, 103, 229, 0.25);
  border-radius: var(--radius-full);
  font-weight: 600;
  font-size: 13px;
  padding: 6px 14px;
  min-height: 36px;
  box-shadow: 0 1px 2px rgba(28, 103, 229, 0.1);
}

#global-ai-btn:hover {
  background: #DFEAFE;
  border-color: var(--color-primary);
}

/* Global Search Trigger */
#global-search {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-height: 36px;
  padding: 6px 14px;
  background: var(--color-surface-alt);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  color: var(--color-muted);
  font-size: 13px;
  box-shadow: none;
}

#global-search:hover {
  background: var(--color-surface);
  border-color: var(--color-border-strong);
  color: var(--color-text);
}

.search-placeholder {
  font-size: 13px;
}

.search-kbd {
  display: inline-block;
  font-family: var(--font-mono);
  font-size: 11px;
  padding: 1px 5px;
  background: var(--color-surface);
  border: 1px solid var(--color-border-strong);
  border-radius: 4px;
  color: var(--color-text-secondary);
}

/* Compact Sync Status */
#compact-sync {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-height: 36px;
  padding: 4px 10px;
  border-radius: var(--radius-sm);
  background: var(--color-surface-alt);
  border: 1px solid var(--color-border);
  font-size: 12px;
  color: var(--color-muted);
}

.sync-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--color-success);
  flex-shrink: 0;
}

#compact-sync[data-state="pending"] .sync-dot {
  background: var(--color-warning);
  animation: syncPulse 1.5s infinite;
}

#compact-sync[data-state="error"] .sync-dot {
  background: var(--color-danger);
}

#compact-sync[data-state="synced"] #study-sync-state {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
}

@keyframes syncPulse {
  0% { opacity: 0.4; }
  50% { opacity: 1; }
  100% { opacity: 0.4; }
}

/* ==========================================================================
   ACCOUNT & DATA DROPDOWN POPOVER
   ========================================================================== */
.account-menu {
  position: relative;
}

.account-menu > summary {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-height: 36px;
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  color: var(--color-text);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  list-style: none;
  box-shadow: var(--shadow-xs);
  user-select: none;
}

.account-menu > summary::-webkit-details-marker {
  display: none;
}

.account-menu > summary:hover {
  background: var(--color-surface-alt);
  border-color: var(--color-border-strong);
}

.account-avatar {
  font-size: 14px;
}

.account-chevron {
  font-size: 11px;
  color: var(--color-muted);
}

.account-panel {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  width: 290px;
  padding: var(--space-4);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-surface);
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  z-index: 100;
}

.account-header {
  padding-bottom: var(--space-2);
  border-bottom: 1px solid var(--color-border);
}

.account-title {
  font-size: 13px;
  font-weight: 700;
  color: var(--color-text);
}

.account-panel #account {
  font-size: 12px;
  font-weight: 500;
  color: var(--color-muted);
  overflow-wrap: anywhere;
  margin: var(--space-1) 0 var(--space-2);
}

.account-panel label {
  font-size: 12px;
  color: var(--color-text-secondary);
  margin-top: var(--space-1);
}

.account-panel select {
  width: 100%;
  margin-top: 4px;
}

.account-panel button {
  width: 100%;
  text-align: left;
  justify-content: flex-start;
  min-height: 38px;
  padding: 6px 12px;
  font-size: 13px;
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  color: var(--color-text);
  border-radius: var(--radius-sm);
  box-shadow: none;
}

.account-panel button:hover {
  background: var(--color-surface-alt);
}

.account-panel #logout {
  color: var(--color-danger);
  border-color: var(--color-danger-soft);
  margin-top: var(--space-2);
}

.account-panel #logout:hover {
  background: var(--color-danger-soft);
}

/* ==========================================================================
   GLOBAL SEARCH DIALOG (Ctrl + K)
   ========================================================================== */
#global-search-dialog {
  width: min(720px, calc(100% - 32px));
  max-height: 82vh;
  padding: var(--space-6);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xl);
  overflow-y: auto;
}

.palette-title-box {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.palette-sub {
  font-size: 12px;
  color: var(--color-muted);
}

.sheet-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-4);
  padding-bottom: var(--space-3);
  border-bottom: 1px solid var(--color-border);
}

.sheet-heading h2 {
  font-size: var(--type-h2);
  margin: 0;
}

.sheet-heading button {
  min-height: 36px;
  min-width: 36px;
  padding: 0;
  border-radius: var(--radius-sm);
  font-size: 20px;
}

#palette-commands {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding-bottom: var(--space-4);
  margin-bottom: var(--space-4);
  border-bottom: 1px solid var(--color-border);
}

#palette-commands button {
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-surface-alt);
  font-size: 12.5px;
  font-weight: 500;
  min-height: 34px;
  padding: 6px 12px;
  box-shadow: none;
  transition: all 0.12s ease;
}

#palette-commands button:hover,
#palette-commands button:focus-visible {
  background: var(--color-primary-light);
  border-color: var(--color-primary);
  color: var(--color-primary);
  transform: translateY(-1px);
}

#global-search-content form {
  display: flex;
  gap: var(--space-2);
  margin-bottom: var(--space-4);
}

#global-search-content input[type="search"] {
  flex: 1;
}

#global-search-content mark {
  background: var(--color-warning-soft);
  color: var(--color-warning-text);
  padding: 0 4px;
  border-radius: 2px;
}

.palette-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: var(--space-4);
  padding-top: var(--space-3);
  margin-top: var(--space-4);
  border-top: 1px solid var(--color-border);
}

.kbd-hint {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11.5px;
  color: var(--color-muted);
}

.kbd-hint kbd {
  font-family: var(--font-mono);
  font-size: 11px;
  padding: 1px 6px;
  background: var(--color-surface-alt);
  border: 1px solid var(--color-border-strong);
  border-radius: 4px;
  color: var(--color-text-secondary);
}

/* ==========================================================================
   MOBILE BOTTOM NAVIGATION (< 760px)
   ========================================================================== */
#mobile-tabs {
  display: none;
}

@media (max-width: 759px) {
  #workspace {
    padding-left: 0 !important;
    padding-bottom: 78px;
    overflow-x: hidden;
  }

  #workspace > .app-header {
    height: 60px;
    min-height: 60px;
    padding: 0 var(--space-4);
    gap: var(--space-2);
  }

  .app-header .brand {
    display: block;
  }

  #workspace > .app-header .primary-nav {
    display: none !important;
  }

  #global-search {
    padding: 6px 8px;
    min-width: 38px;
  }

  .search-placeholder, .search-kbd {
    display: none;
  }

  #compact-sync {
    max-width: 110px;
    padding: 4px 6px;
    font-size: 11px;
  }

  .account-label, .account-chevron {
    display: none;
  }

  .account-menu > summary {
    padding: 6px 8px;
  }

  /* Fixed Bottom Tab Bar */
  #mobile-tabs {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 62px;
    z-index: 60;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    background: var(--color-surface);
    border-top: 1px solid var(--color-border);
    box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.05);
    padding: 4px 4px max(4px, env(safe-area-inset-bottom));
    box-sizing: border-box;
  }

  #mobile-tabs button {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    min-height: 52px;
    padding: 4px 0;
    background: transparent;
    border: 0;
    box-shadow: none;
    color: var(--color-muted);
    font-size: 11px;
    font-weight: 500;
  }

  #mobile-tabs button .tab-icon {
    font-size: 18px;
    line-height: 1;
  }

  #mobile-tabs button[aria-current="page"] {
    color: var(--color-primary);
    font-weight: 700;
  }

  /* More Bottom Sheet */
  #more-sheet {
    margin: auto 0 0;
    width: 100%;
    max-width: none;
    border-bottom: 0;
    border-radius: 20px 20px 0 0;
    padding: var(--space-5);
    max-height: 80dvh;
    box-sizing: border-box;
  }

  #more-links section {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--space-2);
    margin: var(--space-4) 0;
  }

  #more-links h3 {
    grid-column: 1 / -1;
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    color: var(--color-muted);
    letter-spacing: 0.05em;
    margin-bottom: var(--space-1);
  }

  #more-links button {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: 10px 12px;
    min-height: 44px;
    text-align: left;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    background: var(--color-surface-alt);
    font-size: 13px;
    font-weight: 500;
  }
}

@media print {
  .app-header, #mobile-tabs, #more-sheet, .primary-nav {
    display: none !important;
  }
  #workspace {
    padding: 0 !important;
  }
}
.account-avatar.has-profile, .account-profile-avatar { display: inline-grid; place-items: center; width: 24px; height: 24px; border-radius: 50%; overflow: hidden; color: #fff; font-size: 10px; font-weight: 700; flex: none; background: var(--color-primary); }
.account-avatar img, .account-profile-avatar img { width: 100%; height: 100%; object-fit: cover; }
.account-profile { display: flex; align-items: center; gap: 10px; width: 100%; padding: 10px; border: 1px solid var(--color-border); border-radius: var(--radius-md); background: var(--color-surface-alt); color: var(--color-text); text-align: left; cursor: pointer; font: inherit; }
.account-profile:hover { border-color: var(--color-primary); }
.account-profile .account-profile-avatar { width: 40px; height: 40px; font-size: 15px; }
.account-profile-text { display: flex; flex-direction: column; min-width: 0; }
.account-profile-text strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.account-profile-text small { color: var(--color-muted); font-size: 12px; }
