/**
 * App Shell — SaaS Guide 三段式後台
 * @see https://saasguideline.zeabur.app/docs/layout
 * viewport 固定 · sidebar / 主內容分離 scroll · topbar + page header 不捲
 */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

*, *::before, *::after { box-sizing: border-box; }

html {
  font-family: var(--font-family);
  font-size: var(--font-body);
  line-height: var(--line-height-body);
  color: var(--color-text-primary);
  -webkit-font-smoothing: antialiased;
}

/* ── Auth layout (standalone, window scroll OK) ── */
html[data-layout="auth"],
html[data-layout="auth"] body {
  height: auto;
  min-height: 100dvh;
}

body[data-layout="auth"] {
  margin: 0;
  background: var(--color-bg-subtle);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-6);
}

.auth-page {
  width: 100%;
  max-width: 400px;
}

.auth-page__logo-row {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  margin-bottom: var(--space-6);
  justify-content: center;
}

.auth-page__title {
  font-size: var(--font-h1);
  font-weight: 600;
  text-align: center;
  margin: 0 0 var(--space-2);
}

.auth-page__subtitle {
  text-align: center;
  color: var(--color-text-secondary);
  margin: 0 0 var(--space-6);
}

/* ── App shell (fixed viewport, no window scroll) ── */
html[data-layout="app"],
html[data-layout="app"] body {
  height: 100dvh;
  overflow: hidden;
}

body[data-layout="app"] {
  margin: 0;
  background: var(--color-bg-subtle);
}

.app-shell {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
}

/* Row 1: brand + topbar — same height, bottom border aligned */
.app-shell__header-row {
  display: flex;
  flex-shrink: 0;
  height: var(--app-shell-header-height);
  border-bottom: 1px solid var(--color-border-default);
  background: var(--color-bg-default);
  position: relative;
  z-index: var(--z-shell-header);
}

/* 收合時展開鈕浮在 logo 下方，需高於 sidebar 才不會被 nav 遮住 */
.app-shell[data-collapsed="true"] .app-shell__header-row {
  overflow: visible;
  z-index: calc(var(--z-shell-sidebar) + 1);
}

.app-shell__brand {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  width: var(--sidebar-width);
  flex-shrink: 0;
  padding: 0 var(--space-4);
  border-right: 1px solid var(--color-border-default);
  background: var(--color-sidebar);
  transition: width var(--duration-base) var(--ease-out);
}

.app-shell[data-collapsed="true"] .app-shell__brand {
  position: relative;
  overflow: visible;
  width: var(--sidebar-collapsed-width);
  min-width: var(--sidebar-collapsed-width);
  max-width: var(--sidebar-collapsed-width);
  height: var(--app-shell-header-height);
  box-sizing: border-box;
  flex: 0 0 var(--sidebar-collapsed-width);
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  justify-content: center;
  align-items: center;
  gap: 0;
  padding: 0 var(--space-2);
}

.app-shell[data-collapsed="true"] .app-shell__brand .app-shell__collapse-btn {
  margin: 0;
  position: absolute;
  top: calc(100% + var(--space-3));
  left: 50%;
  transform: translateX(-50%);
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  min-width: 32px;
  min-height: 32px;
  display: grid;
  place-items: center;
  box-sizing: border-box;
  z-index: 1;
}

.app-shell__brand-logo {
  width: 36px;
  height: 36px;
  flex-shrink: 0;
  border-radius: var(--radius-md);
  background: linear-gradient(135deg, var(--color-primary-gradient-from), var(--color-primary-gradient-to));
  color: var(--color-text-on-brand);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 13px;
}

.app-shell__brand-logo--img {
  display: block;
  object-fit: contain;
  border-radius: 0;
  background: transparent;
}

.app-shell__brand-text {
  min-width: 0;
  overflow: hidden;
}

.app-shell__brand-name {
  font-size: var(--font-body);
  font-weight: 600;
  line-height: 1.2;
  white-space: nowrap;
}

.app-shell__brand-tag {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.08em;
  color: var(--color-text-tertiary);
  text-transform: uppercase;
}

.app-shell[data-collapsed="true"] .app-shell__brand-text {
  display: none;
}

.app-shell__collapse-btn {
  margin-left: auto;
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  border-radius: var(--radius-md);
  color: var(--color-text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.app-shell__collapse-btn:hover {
  background: var(--color-bg-muted);
  color: var(--color-text-primary);
}

.app-shell[data-collapsed="true"] .app-shell__collapse-btn {
  margin-left: 0;
  flex-shrink: 0;
  background: var(--color-bg-default);
  border: 1px solid var(--color-border-default);
  color: var(--color-text-primary);
}

.app-shell[data-collapsed="true"] .app-shell__collapse-btn:hover {
  background: var(--color-bg-muted);
  border-color: var(--color-border-strong, var(--color-border-default));
}

.app-shell[data-collapsed="true"] .app-shell__collapse-btn svg {
  display: block;
  width: 16px;
  height: 16px;
}

.app-shell[data-collapsed="true"] .app-shell__brand-logo,
.app-shell[data-collapsed="true"] .app-shell__brand-logo--img {
  width: 36px;
  height: 36px;
  flex-shrink: 0;
}

.app-shell__topbar {
  flex: 1;
  display: flex;
  align-items: center;
  gap: var(--space-4);
  padding: 0 var(--space-6);
  min-width: 0;
}

.app-shell__search {
  flex: 1;
  max-width: 360px;
  position: relative;
}

.app-shell__search input {
  width: 100%;
  padding: var(--space-2) var(--space-3) var(--space-2) 36px;
  border: 1px solid var(--color-border-default);
  border-radius: var(--radius-md);
  font-size: var(--font-body);
  background: var(--color-bg-subtle);
}

.app-shell__search input:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 1px var(--color-primary);
  background: var(--color-bg-default);
}

.app-shell__search-icon {
  position: absolute;
  left: var(--space-3);
  top: 50%;
  transform: translateY(-50%);
  color: var(--color-text-muted);
  pointer-events: none;
}

.app-shell__search-kbd {
  position: absolute;
  right: var(--space-2);
  top: 50%;
  transform: translateY(-50%);
  font-size: 11px;
  color: var(--color-text-muted);
  border: 1px solid var(--color-border-default);
  border-radius: var(--radius-sm);
  padding: 1px 5px;
  background: var(--color-bg-default);
}

.app-global-search-panel {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  z-index: 120;
  background: var(--color-bg-default);
  border: 1px solid var(--color-border-default);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg, 0 12px 32px rgba(15, 23, 42, 0.12));
  max-height: min(360px, 50vh);
  overflow: auto;
  padding: var(--space-1);
}

.app-global-search-panel__empty {
  padding: var(--space-3) var(--space-4);
  font-size: var(--font-body-sm, 13px);
  color: var(--color-text-muted);
}

.app-global-search-panel__item {
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-rows: auto auto;
  gap: 0 var(--space-2);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  text-decoration: none;
  color: inherit;
}

.app-global-search-panel__item:hover,
.app-global-search-panel__item.is-active {
  background: var(--color-bg-subtle);
}

.app-global-search-panel__type {
  grid-row: 1 / span 2;
  align-self: center;
  font-size: 11px;
  font-weight: 600;
  color: var(--color-text-muted);
  white-space: nowrap;
  padding-right: var(--space-1);
}

.app-global-search-panel__title {
  font-size: var(--font-body);
  font-weight: 600;
  color: var(--color-text-default);
  min-width: 0;
}

.app-global-search-panel__meta {
  grid-column: 2;
  font-size: var(--font-body-sm, 13px);
  color: var(--color-text-muted);
  min-width: 0;
}

.app-shell__topbar-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin-left: auto;
}

.app-shell__icon-btn {
  width: 36px;
  height: 36px;
  border: none;
  background: transparent;
  border-radius: var(--radius-md);
  color: var(--color-text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.app-shell__icon-btn:hover {
  background: var(--color-bg-subtle);
}

.app-shell__user {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-1) var(--space-2);
  border-radius: var(--radius-md);
  cursor: pointer;
  border: none;
  background: transparent;
  font: inherit;
  color: inherit;
}

.app-shell__user:hover {
  background: var(--color-bg-subtle);
}

.app-shell__avatar {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-full);
  background: var(--color-primary-subtle);
  color: var(--color-primary);
  font-size: var(--font-caption);
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
}

.app-shell__avatar--org {
  border-radius: var(--radius-md);
}

.app-shell__user-label {
  font-size: var(--font-body);
  font-weight: 500;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Row 2: sidebar + main */
.app-shell__body {
  display: flex;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.app-shell__sidebar {
  width: var(--sidebar-width);
  flex-shrink: 0;
  background: var(--color-sidebar);
  border-right: 1px solid var(--color-sidebar-border);
  overflow-y: auto;
  overflow-x: hidden;
  transition: width var(--duration-base) var(--ease-out);
  display: flex;
  flex-direction: column;
  position: relative;
  z-index: var(--z-shell-sidebar);
}

.app-shell__sidebar-footer {
  margin-top: auto;
  padding: var(--space-4) var(--space-3) var(--space-5);
  border-top: 1px solid var(--color-sidebar-border);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.app-shell[data-collapsed="true"] .app-shell__sidebar-footer {
  display: none;
}

.app-shell__sidebar-link {
  display: block;
  text-align: center;
  font-size: var(--font-caption);
  color: var(--color-text-secondary);
  text-decoration: none;
  padding: var(--space-2);
  border-radius: var(--radius-md);
}

.app-shell__sidebar-link:hover {
  background: var(--color-bg-muted);
  color: var(--color-text-primary);
  text-decoration: none;
}

.app-shell[data-collapsed="true"] .app-shell__sidebar {
  width: var(--sidebar-collapsed-width);
}

.app-shell__nav-section {
  padding: var(--space-4) var(--space-3) var(--space-2);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--color-text-tertiary);
}

.app-shell[data-collapsed="true"] .app-shell__nav-section {
  display: none;
}

.app-shell__nav-group {
  flex-shrink: 0;
}

.app-shell__nav {
  list-style: none;
  margin: 0;
  padding: 0 var(--space-2) var(--space-2);
}

.app-shell__nav-link {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: 10px var(--space-3);
  margin-bottom: var(--space-1);
  border-radius: var(--radius-md);
  color: var(--color-text-secondary);
  font-size: var(--font-body);
  font-weight: 500;
  text-decoration: none;
  transition: background var(--duration-fast), color var(--duration-fast);
  white-space: nowrap;
}

.app-shell__nav-link:hover {
  background: var(--color-bg-muted);
  color: var(--color-text-primary);
  text-decoration: none;
}

.app-shell__nav-link--active {
  background: var(--color-primary-subtle);
  color: var(--color-primary);
}

.app-shell__nav-icon {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
}

.app-shell[data-collapsed="true"] .app-shell__nav-label {
  display: none;
}

.app-shell[data-collapsed="true"] .app-shell__sidebar-nav {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: var(--space-3);
  padding-bottom: var(--space-4);
}

/* 收合後展開鈕移入側欄頂部（JS reparent），避免 header 絕對定位被裁切 */
.app-shell__collapse-btn--in-sidebar {
  position: static;
  top: auto;
  left: auto;
  transform: none;
  margin: 0 0 var(--space-3);
  flex-shrink: 0;
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  min-width: 32px;
  min-height: 32px;
  box-sizing: border-box;
  background: var(--color-bg-default);
  border: 1px solid var(--color-border-default);
  color: var(--color-text-primary);
}

.app-shell__collapse-btn--in-sidebar:hover {
  background: var(--color-bg-muted);
  border-color: var(--color-border-strong, var(--color-border-default));
}

.app-shell__collapse-btn--in-sidebar svg {
  display: block;
  width: 16px;
  height: 16px;
}

.app-shell[data-collapsed="true"] .app-shell__nav-group {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
}

.app-shell[data-collapsed="true"] .app-shell__nav-group + .app-shell__nav-group {
  margin-top: var(--space-2);
  padding-top: var(--space-2);
  border-top: 1px solid var(--color-border-hairline, var(--color-sidebar-border));
}

.app-shell[data-collapsed="true"] .app-shell__nav {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-2);
  width: 100%;
  padding: 0 var(--space-1) var(--space-1);
}

.app-shell[data-collapsed="true"] .app-shell__nav li {
  display: flex;
  justify-content: center;
  width: 100%;
}

.app-shell[data-collapsed="true"] .app-shell__nav-link {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  min-width: 40px;
  min-height: 40px;
  padding: 0;
  margin: 0;
  border-radius: var(--radius-md);
  box-sizing: border-box;
  gap: 0;
  line-height: 0;
}

.app-shell[data-collapsed="true"] .app-shell__nav-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
}

.app-shell[data-collapsed="true"] .app-shell__nav-icon svg {
  display: block;
}

.app-shell__main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--color-bg-subtle);
  position: relative;
  z-index: var(--z-shell-body);
}

/* Page header — shrink-0, does not scroll */
.page-header {
  flex-shrink: 0;
  padding: var(--space-6) var(--space-6) var(--space-4);
  background: var(--color-bg-subtle);
  border-bottom: 1px solid transparent;
}

.page-header__inner {
  max-width: var(--content-max-width);
  margin: 0 auto;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-4);
}

.page-header__breadcrumb {
  font-size: var(--font-caption);
  color: var(--color-text-muted);
  margin-bottom: var(--space-2);
}

.page-header__breadcrumb a {
  color: var(--color-text-muted);
  text-decoration: none;
}

.page-header__breadcrumb a:hover {
  color: var(--color-primary);
}

.page-header__title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  flex-wrap: wrap;
}

.page-header__title-group {
  display: inline-flex;
  align-items: baseline;
  flex-wrap: wrap;
  min-width: 0;
}

.page-header__title-row .page-header__title {
  margin: 0;
}

.page-header__count {
  padding-left: var(--space-4);
  font-size: var(--font-body);
  font-weight: 500;
  color: var(--color-text-tertiary);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.page-header__title {
  font-size: var(--font-h1);
  font-weight: 600;
  line-height: var(--line-height-heading);
  margin: 0;
}

.page-header__desc {
  font-size: var(--font-body);
  color: var(--color-text-secondary);
  margin: var(--space-2) 0 0;
  max-width: 65ch;
}

.page-header__actions {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex-shrink: 0;
  padding-top: 0;
}

/* Scrollable content only */
.app-shell__content {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0 var(--space-6) var(--space-6);
}

.app-shell__content-inner {
  max-width: var(--content-max-width);
  margin: 0 auto;
}

/* Sticky form footer — form-page pattern */
.form-sticky-footer {
  flex-shrink: 0;
  padding: var(--space-4) var(--space-6);
  background: var(--color-bg-default);
  border-top: 1px solid var(--color-border-default);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--space-4);
}

.form-sticky-footer__inner {
  max-width: var(--content-max-width);
  margin: 0 auto;
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 固定於瀏覽器底端（主內容區域內，避開側欄） */
.app-shell--admin .form-sticky-footer--viewport {
  position: fixed;
  bottom: 0;
  right: 0;
  left: var(--sidebar-width);
  z-index: var(--z-shell-footer);
  flex-shrink: 0;
  box-shadow:
    0 -1px 0 var(--color-border-default),
    0 -8px 24px rgba(17, 17, 17, 0.08);
}

.app-shell--admin[data-collapsed="true"] .form-sticky-footer--viewport {
  left: var(--sidebar-collapsed-width);
}

.app-shell--admin .app-shell__main--viewport-footer .app-shell__content {
  padding-bottom: calc(80px + var(--space-6));
}

@media (max-width: 991px) {
  .app-shell--admin .form-sticky-footer--viewport {
    left: 0;
  }
}

/* Mobile drawer */
.app-shell__overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: calc(var(--z-drawer) - 1);
}

.app-shell__menu-btn {
  display: none;
  width: 36px;
  height: 36px;
  border: none;
  background: transparent;
  border-radius: var(--radius-md);
  cursor: pointer;
  color: var(--color-text-secondary);
}

@media (max-width: 991px) {
  .app-shell__menu-btn { display: flex; align-items: center; justify-content: center; }
  .app-shell__brand { width: auto; flex: 1; border-right: none; }

  .app-shell[data-collapsed="true"] .app-shell__brand {
    width: var(--sidebar-collapsed-width);
    min-width: var(--sidebar-collapsed-width);
    max-width: var(--sidebar-collapsed-width);
    flex: 0 0 var(--sidebar-collapsed-width);
    flex-direction: column;
    border-right: 1px solid var(--color-border-default);
  }

  .app-shell__search { display: none; }
  .app-shell__user-label { display: none; }

  .app-shell__sidebar {
    position: fixed;
    top: var(--app-shell-header-height);
    left: 0;
    bottom: 0;
    z-index: var(--z-drawer);
    transform: translateX(-100%);
    transition: transform var(--duration-base) var(--ease-out);
    box-shadow: var(--shadow-lg);
  }

  .app-shell[data-drawer-open="true"] .app-shell__sidebar {
    transform: translateX(0);
  }

  .app-shell[data-drawer-open="true"] .app-shell__overlay {
    display: block;
  }

  .app-shell[data-collapsed="true"] .app-shell__sidebar {
    width: var(--sidebar-width);
  }
}

/* ═══════════════════════════════════════════
   客戶入口 Client Portal — 無 sidebar，僅 top nav
   企業 B2B 自助預約 / 報告，非正隆內部營運後台
   ═══════════════════════════════════════════ */
html[data-layout="client"],
html[data-layout="client"] body {
  height: 100dvh;
  overflow: hidden;
}

body[data-layout="client"] {
  margin: 0;
  background: var(--color-bg-subtle);
}

.client-portal {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
}

.client-portal__header {
  flex-shrink: 0;
  height: var(--app-shell-header-height);
  background: var(--color-bg-default);
  border-bottom: 1px solid var(--color-border-default);
  display: flex;
  align-items: center;
  padding: 0 var(--space-6);
  gap: var(--space-6);
  z-index: var(--z-sticky);
}

.client-portal__brand {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  text-decoration: none;
  color: inherit;
  flex-shrink: 0;
}

.client-portal__brand:hover { text-decoration: none; }

.client-portal__brand-badge {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--color-primary);
  background: var(--color-primary-subtle);
  padding: 2px var(--space-2);
  border-radius: var(--radius-sm);
}

.client-portal__nav {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  flex: 1;
  min-width: 0;
}

.client-portal__nav-link {
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  font-size: var(--font-body);
  font-weight: 500;
  color: var(--color-text-secondary);
  text-decoration: none;
  white-space: nowrap;
}

.client-portal__nav-link:hover {
  background: var(--color-bg-subtle);
  color: var(--color-text-primary);
  text-decoration: none;
}

.client-portal__nav-link--active {
  background: var(--color-primary-subtle);
  color: var(--color-primary);
}

.client-portal__actions {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  margin-left: auto;
  flex-shrink: 0;
}

.client-portal__org {
  font-size: var(--font-caption);
  color: var(--color-text-muted);
  display: none;
}

@media (min-width: 768px) {
  .client-portal__org { display: block; }
}

.client-portal__main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.client-portal__menu-btn {
  display: none;
  width: 36px;
  height: 36px;
  border: none;
  background: transparent;
  border-radius: var(--radius-md);
  cursor: pointer;
  color: var(--color-text-secondary);
}

.client-portal__overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: calc(var(--z-drawer) - 1);
}

@media (max-width: 767px) {
  .client-portal__menu-btn {
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .client-portal__nav {
    position: fixed;
    top: var(--app-shell-header-height);
    left: 0;
    right: 0;
    background: var(--color-bg-default);
    border-bottom: 1px solid var(--color-border-default);
    flex-direction: column;
    align-items: stretch;
    padding: var(--space-2);
    transform: translateY(-120%);
    opacity: 0;
    pointer-events: none;
    transition: transform var(--duration-base), opacity var(--duration-base);
    z-index: var(--z-drawer);
  }

  .client-portal[data-menu-open="true"] .client-portal__nav {
    transform: translateY(0);
    opacity: 1;
    pointer-events: auto;
  }

  .client-portal[data-menu-open="true"] .client-portal__overlay {
    display: block;
  }
}

/* 正隆內部後台 — 細節見 admin.css */
.app-shell--admin .app-shell__brand-tag {
  color: var(--color-kraft-hover);
}

/* 企業服務 */
.app-shell--member .app-shell__brand-tag--member {
  color: var(--color-primary);
}

.app-shell--member .app-shell__nav-link--active {
  background: var(--color-primary-subtle);
  color: var(--color-primary);
}
