/* =================================================================
   CSS/SIDEBAR.CSS — 全響應式抽屜側邊欄導航 (對齊 house-expense 規格)
   - 側欄收起時隱藏至左側螢幕外 (transform: translateX(-100%))
   - 全螢幕統一頂部導航，配備手動漢堡按鈕 (☰) 隨時展開側欄
   - 側欄展開時背後呈現黑色半透明遮罩背景 (rgba(0, 0, 0, 0.55))
   - 涵蓋所有主要模組與子分頁 (存款/股票/保險/設定/系統)
   ================================================================= */

/* --- 黑色半透明背景設計 (點擊背景可收合側欄) --- */
.sidebar-backdrop {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 999;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.25s cubic-bezier(0.16, 1, 0.3, 1), visibility 0.25s;
}

.sidebar-backdrop.active {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

/* --- 手動漢堡按鈕 (☰) --- */
.btn-hamburger {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  border: 1px solid var(--color-border);
  background: var(--color-bg-primary, #ffffff);
  color: var(--color-text-primary, #0f172a);
  cursor: pointer;
  transition: all 0.15s ease;
  padding: 0;
  flex-shrink: 0;
}

.btn-hamburger:hover {
  background: var(--color-bg-secondary, #f8fafc);
  border-color: rgba(37, 99, 235, 0.3);
  color: #2563eb;
}

/* --- 側邊欄抽屜容器 (平時收合隱藏) --- */
.desktop-sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 268px;
  max-width: 85vw;
  height: 100vh;
  height: 100dvh;
  background: #ffffff;
  border-right: 1px solid rgba(226, 232, 240, 0.8);
  padding: 16px 14px 14px 14px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  z-index: 1000;
  transform: translateX(-100%);
  visibility: hidden;
  box-shadow: 8px 0 32px -4px rgba(15, 23, 42, 0.12), 2px 0 8px -2px rgba(15, 23, 42, 0.04);
  transition: transform 0.28s cubic-bezier(0.16, 1, 0.3, 1), visibility 0.28s, width 0.25s cubic-bezier(0.4, 0, 0.2, 1), padding 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  overflow-y: auto;
}

.desktop-sidebar.open {
  transform: translateX(0);
  visibility: visible;
}

/* --- 側邊欄頂部品牌列 --- */
.sidebar-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  width: 100%;
  padding: 0 2px;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 0;
  padding: 0 4px;
  min-width: 0;
  text-decoration: none;
}

.sidebar-logo-icon {
  width: 36px;
  height: 36px;
  background: linear-gradient(135deg, #2563eb, #1d4ed8);
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 18px;
  flex-shrink: 0;
  box-shadow: 0 4px 10px rgba(37, 99, 235, 0.22);
}

.sidebar-title {
  font-size: 14.5px;
  font-weight: 700;
  color: var(--color-text-primary, #0f172a);
  line-height: 1.25;
  letter-spacing: -0.01em;
}

.sidebar-subtitle {
  font-size: 11px;
  font-weight: 500;
  color: var(--color-text-muted, #64748b);
  margin-top: 1px;
}

.sidebar-toggle-btn {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  border: 1px solid rgba(226, 232, 240, 0.8);
  background: #ffffff;
  color: #64748b;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.15s ease;
  flex-shrink: 0;
}

.sidebar-toggle-btn:hover {
  background: #f8fafc;
  color: #0f172a;
  border-color: #cbd5e1;
}

/* --- 側邊欄選單模組與子分頁項目 --- */
.sidebar-menu {
  display: flex;
  flex-direction: column;
  gap: 3px;
  width: 100%;
  flex: 1;
  overflow-y: auto;
  padding-right: 2px;
}

.sidebar-group {
  margin-bottom: 3px;
  width: 100%;
}

/* 手風琴 header row */
.sidebar-group-header {
  display: flex;
  align-items: center;
  width: 100%;
  border-radius: 8px;
  transition: background 0.15s ease;
}
.sidebar-group-header:hover {
  background: #f8fafc;
}
.sidebar-group-header .sidebar-nav-item {
  flex: 1;
  min-width: 0;
}

.sidebar-chevron-btn {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  background: transparent;
  border: none;
  cursor: pointer;
  border-radius: 6px;
  color: #94a3b8;
  margin-right: 4px;
  transition: background 0.15s, color 0.15s;
}
.sidebar-chevron-btn:hover {
  background: #f1f5f9;
  color: #334155;
}
.sidebar-chevron {
  transition: transform 0.22s ease;
}
.sidebar-group.open .sidebar-chevron {
  transform: rotate(180deg);
}

/* 垂直目錄樹狀 (Tree Line) 子選單 */
.sidebar-sub-menu {
  display: flex;
  flex-direction: column;
  margin-left: 20px;
  padding-left: 12px;
  border-left: 1.5px solid #e2e8f0;
  gap: 2px;
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.28s ease, padding 0.2s ease, margin 0.2s ease;
  padding-top: 0;
  padding-bottom: 0;
  margin-top: 0;
  margin-bottom: 0;
}
.sidebar-group.open .sidebar-sub-menu {
  max-height: 400px;
  padding-top: 3px;
  padding-bottom: 5px;
  margin-top: 2px;
  margin-bottom: 3px;
}

.sidebar-sub-item {
  display: flex;
  align-items: center;
  padding: 6.5px 10px;
  font-size: 13px;
  font-weight: 500;
  color: #64748b;
  text-decoration: none;
  border-radius: 7px;
  cursor: pointer;
  transition: all 0.15s ease;
  width: 100%;
  box-sizing: border-box;
}

.sidebar-sub-item::before {
  content: '';
  display: inline-block;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: #cbd5e1;
  margin-right: 8px;
  transition: all 0.15s ease;
  flex-shrink: 0;
}

.sidebar-sub-item:hover {
  color: #0f172a;
  background: #f8fafc;
}

.sidebar-sub-item:hover::before {
  background: #94a3b8;
}

/* 全側欄唯一被點選頁面呈現淺藍色高亮 (確保單一視圖焦點) */
.sidebar-sub-item.active {
  color: #2563eb !important;
  background: #eff6ff !important;
  font-weight: 600 !important;
}

.sidebar-sub-item.active::before {
  background: #2563eb !important;
  box-shadow: 0 0 0 2.5px rgba(37, 99, 235, 0.2) !important;
  transform: scale(1.1);
}

/* 父選單項目樣式：作為手風琴分類標題，背景永遠保持透明，絕不重複呈現淺藍色 */
.sidebar-nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 8px 10px;
  border-radius: 8px;
  font-size: 13.5px;
  font-weight: 600;
  color: #475569;
  background: transparent !important;
  border: none;
  cursor: pointer;
  text-align: left;
  text-decoration: none;
  transition: color 0.15s ease;
  box-sizing: border-box;
}

.sidebar-nav-item:hover {
  color: #0f172a;
}

/* 當前分類被選中時：僅圖示與字體維持微高亮，背景保持透明無色塊 */
.sidebar-nav-item.active {
  color: #0f172a !important;
  background: transparent !important;
  font-weight: 700;
}

.sidebar-icon {
  font-size: 18px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  color: #64748b;
  transition: color 0.15s ease;
}

.sidebar-nav-item.active .sidebar-icon {
  color: #2563eb !important;
}

.sidebar-label {
  font-size: 13.5px;
  flex: 1;
}

/* --- 側邊欄底部使用者卡片 --- */
.sidebar-footer {
  border-top: 1px solid var(--color-border);
  padding-top: 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  position: relative;
  width: 100%;
}

.sidebar-user-info {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  flex: 1;
  border-radius: 10px;
  padding: 6px 8px;
  cursor: pointer;
  transition: background 0.15s ease;
}

.sidebar-user-info:hover {
  background: #f1f5f9;
}

.sidebar-user-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: linear-gradient(135deg, #3b82f6, #6366f1);
  color: white;
  font-weight: 700;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  overflow: hidden;
}

.sidebar-user-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.sidebar-user-details {
  min-width: 0;
  flex: 1;
}

.sidebar-user-name {
  font-size: 13px;
  font-weight: 700;
  color: var(--color-text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sidebar-user-email {
  font-size: 11px;
  color: var(--color-text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sidebar-logout-btn {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  border: none;
  background: transparent;
  color: #94a3b8;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.15s ease;
  flex-shrink: 0;
}

.sidebar-logout-btn:hover {
  background: #fee2e2;
  color: #ef4444;
}

/* 側邊欄個人頭像彈出選單 */
#sidebar-user-dropdown {
  display: none;
  position: absolute;
  bottom: calc(100% + 12px);
  left: 8px;
  right: 8px;
  background: #ffffff;
  border: 1px solid var(--color-border);
  border-radius: 14px;
  box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.1);
  z-index: 100;
  overflow: hidden;
  animation: dropUp 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

#sidebar-user-dropdown.open {
  display: block;
}

.sidebar-dropdown-header {
  padding: 12px 14px;
  background: #f8fafc;
  border-bottom: 1px solid var(--color-border);
}

.sidebar-dropdown-name {
  font-size: 13px;
  font-weight: 700;
  color: var(--color-text-primary);
}

.sidebar-dropdown-email {
  font-size: 11px;
  color: var(--color-text-muted);
  margin-top: 2px;
}

.sidebar-dropdown-item {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  font-size: 13px;
  font-weight: 600;
  color: var(--color-text-primary);
  background: transparent;
  border: none;
  text-decoration: none;
  cursor: pointer;
  box-sizing: border-box;
  transition: background 0.15s ease;
}

.sidebar-dropdown-item:hover {
  background: #f1f5f9;
}

.sidebar-dropdown-item.danger {
  color: #ef4444;
  border-top: 1px solid var(--color-border);
}

.sidebar-dropdown-item.danger:hover {
  background: #fee2e2;
}

@keyframes dropUp {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* --- 全域佈局相容性 --- */
.app-content-wrapper {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
  width: 100%;
  /* mobile default: allow body to scroll naturally */
  overflow-y: visible;
}

/* Mobile: ensure content area scrolls when body is taller than viewport */
@media (max-width: 991px) {
  html, body {
    height: auto;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  #app {
    min-height: 100vh;
    height: auto;
    overflow: visible;
    padding: 0 !important;
  }
  .app-content-wrapper {
    overflow-y: visible;
    min-height: calc(100vh - 56px);
    padding: calc(var(--nav-height, 56px) + 10px) 12px 48px 12px !important;
    box-sizing: border-box !important;
  }
}

#top-nav {
  display: flex !important;
}

#bottom-nav {
  display: none !important;
}

@media (min-width: 992px) {
  #app {
    display: flex !important;
    flex-direction: row !important;
    max-width: 100% !important;
    margin: 0 !important;
    padding: 0 !important;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    background: #f8fafc;
  }

  .sidebar-backdrop {
    display: none !important;
  }

  .desktop-sidebar {
    position: relative !important;
    top: 0 !important;
    left: 0 !important;
    bottom: 0 !important;
    transform: none !important;
    visibility: visible !important;
    width: 256px !important;
    height: 100% !important;
    box-shadow: none !important;
    border-right: 1px solid var(--color-border, #e2e8f0) !important;
    padding: 24px 16px !important;
    flex-shrink: 0 !important;
    z-index: 50 !important;
    transition: width 0.25s cubic-bezier(0.4, 0, 0.2, 1), padding 0.25s cubic-bezier(0.4, 0, 0.2, 1) !important;
  }

  /* 側邊欄收合模式 (Collapsed Mode) - 寬度精確為 72px，所有單元居中對齊於同一垂直中線 */
  .desktop-sidebar.collapsed {
    width: 72px !important;
    padding: 24px 0 !important;
    align-items: center !important;
  }

  .desktop-sidebar.collapsed > div:first-child {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
  }

  .desktop-sidebar.collapsed .sidebar-title,
  .desktop-sidebar.collapsed .sidebar-subtitle,
  .desktop-sidebar.collapsed .sidebar-label,
  .desktop-sidebar.collapsed .sidebar-user-details,
  .desktop-sidebar.collapsed .sidebar-brand > div:last-child {
    display: none !important;
  }

  .desktop-sidebar.collapsed .sidebar-header-row {
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 14px;
    margin-bottom: 24px;
    width: 100%;
    padding: 0;
  }

  .desktop-sidebar.collapsed .sidebar-brand {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0;
    margin: 0;
    width: 100%;
    gap: 0;
  }

  .desktop-sidebar.collapsed .sidebar-logo-icon {
    width: 40px;
    height: 40px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
  }

  .desktop-sidebar.collapsed .sidebar-toggle-btn {
    margin: 0 auto;
  }

  .desktop-sidebar.collapsed .sidebar-menu {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 0;
  }

  .desktop-sidebar.collapsed .sidebar-nav-item {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    padding: 0 !important;
    margin: 0 auto 4px auto;
    border-radius: 10px;
  }

  .desktop-sidebar.collapsed .sidebar-icon {
    margin: 0 auto;
  }

  .desktop-sidebar.collapsed .sidebar-footer {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 14px 0 0 0;
    border-top: 1px solid var(--color-border, #e2e8f0);
  }

  .desktop-sidebar.collapsed .sidebar-user-info {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    padding: 0 !important;
    margin: 0 auto;
  }

  .desktop-sidebar.collapsed .sidebar-user-avatar {
    margin: 0 auto;
  }

  .desktop-sidebar.collapsed .sidebar-logout-btn {
    display: none !important;
  }

  .app-content-wrapper {
    /* flex: 1 + min-height: 0 是讓 overflow-y: auto 在 flex 子元素中真正生效的關鍵 */
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: calc(var(--nav-height, 56px) + 12px) 20px 48px 20px !important;
    box-sizing: border-box;
  }

  /* 側欄已包含所有分頁與子分頁，頁面頂部不再呈現分頁按鈕 */
  .app-content-wrapper > section > .sub-nav,
  .app-content-wrapper > .sub-nav,
  .sub-nav,
  #dash-tab-btns,
  .dash-tab-btns {
    display: none !important;
  }
}

/* 全域隱藏頁面頂部次導航分頁（已全數收納於抽屜側欄） */
.sub-nav,
#dash-tab-btns,
.dash-tab-btns {
  display: none !important;
}
