/**
 * @file app-shell.css
 * @description 平台通用 App 外壳：可收起左侧栏 + 右侧工作区 + 视图切换。
 *              对标小云雀的 App 工作台形态——靠侧栏切换视图而非整页上下翻。
 *              暖纸白编辑式主题，叠加克制柔和光感（不要 AI 味）。
 *              仅用 transform / 宽度 过渡，尊重 prefers-reduced-motion。
 */

/* --- 外壳网格：侧栏 + 主区 --- */
.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 100dvh;
  transition: grid-template-columns 0.28s cubic-bezier(0.16, 1, 0.3, 1);
}

.app-shell.is-collapsed {
  grid-template-columns: var(--sidebar-w-collapsed) 1fr;
}

/* --- 左侧栏 --- */
.app-sidebar {
  position: sticky;
  top: 0;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--color-surface);
  background-image: var(--gradient-soft);
  border-right: 1px solid var(--color-border);
  overflow: hidden;
  z-index: 50;
}

/* 顶部：logo + 收起按钮 */
.app-sidebar__head {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  height: var(--header-height);
  padding: 0 0.875rem;
  border-bottom: 1px solid var(--color-border);
}

.app-sidebar__logo {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
  padding: 0.2rem 0.35rem 0.2rem 0.15rem;
  border-radius: var(--radius-md);
  text-decoration: none;
  border: 1px solid transparent;
  transition: background 0.2s ease, border-color 0.2s ease, box-shadow 0.2s ease;
}

.app-sidebar__logo:hover {
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.92) 0%, rgba(243, 241, 236, 0.88) 100%);
  border-color: rgba(28, 27, 25, 0.08);
  box-shadow: var(--shadow-sm);
}

.app-sidebar__logo-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.85rem;
  height: 1.85rem;
  flex-shrink: 0;
  border-radius: var(--radius-sm);
  color: var(--color-accent);
  background: linear-gradient(145deg, rgba(178, 58, 46, 0.16) 0%, rgba(178, 58, 46, 0.08) 100%);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.45);
}

.app-sidebar__logo-name {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1.125rem;
  letter-spacing: 0.03em;
  color: var(--color-text);
  line-height: 1;
  white-space: nowrap;
  overflow: hidden;
}

.app-sidebar__toggle {
  flex: none;
  margin-left: auto;
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  border-radius: var(--radius-md);
  color: var(--color-text-muted);
  transition: background 0.2s, color 0.2s;
}

.app-sidebar__toggle:hover {
  background: var(--color-surface-hover);
  color: var(--color-text);
}

.app-sidebar__toggle svg {
  transition: transform 0.28s cubic-bezier(0.16, 1, 0.3, 1);
}

.app-shell.is-collapsed .app-sidebar__toggle svg {
  transform: rotate(180deg);
}

/* 导航 */
.app-nav {
  flex: none;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  padding: 0.75rem 0.75rem 0.5rem;
}

.app-nav__group-label {
  padding: 0.75rem 0.625rem 0.35rem;
  font-size: 0.6875rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  color: var(--color-text-muted);
  white-space: nowrap;
  overflow: hidden;
}

.app-nav__item {
  position: relative;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.65rem 0.75rem;
  border-radius: var(--radius-pill);
  color: var(--color-text-muted);
  font-size: 0.9375rem;
  font-weight: 500;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
  transition: color 0.2s, background 0.2s;
}

.app-nav__item:hover {
  color: var(--color-text);
  background: var(--color-surface-hover);
}

.app-nav__item.is-active {
  color: var(--color-accent);
  background: var(--gradient-active);
  font-weight: 600;
}

/* active 左侧细赤陶条 */
.app-nav__item.is-active::before {
  content: "";
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 3px;
  height: 18px;
  border-radius: 0 3px 3px 0;
  background: var(--color-accent);
}

.app-nav__icon {
  flex: none;
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  border-radius: var(--radius-md);
  background: color-mix(in srgb, var(--color-surface-muted) 80%, transparent);
  transition: background 0.2s;
}

.app-nav__item.is-active .app-nav__icon {
  background: color-mix(in srgb, var(--color-accent) 12%, var(--color-surface));
}

.app-nav__icon svg {
  width: 18px;
  height: 18px;
}

.app-nav__label {
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 底部用户区 */
.app-sidebar__foot {
  border-top: 1px solid var(--color-border);
  padding: 0.625rem;
}

.app-user {
  display: flex;
  align-items: center;
  gap: 0.625rem;
  padding: 0.4rem;
  border-radius: var(--radius-md);
}

.app-user__avatar {
  flex: none;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 0.9375rem;
  color: var(--color-accent);
  background: var(--color-accent-soft);
}

.app-user__meta {
  min-width: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.app-user__name {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--color-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.app-user__action {
  font-size: 0.75rem;
  color: var(--color-text-muted);
  white-space: nowrap;
}

.app-user__action:hover {
  color: var(--color-accent);
}

/* --- 创作历史面板 --- */
.app-sidebar__history {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 0.5rem 0.75rem 0.75rem;
  border-top: 1px solid var(--color-border);
  overflow: hidden;
}

.app-sidebar__history-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
  flex-shrink: 0;
}

.app-sidebar__history-title {
  font-size: 0.6875rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  color: var(--color-text-muted);
  text-transform: uppercase;
}

.app-sidebar__history-all {
  font-size: 0.75rem;
  color: var(--color-text-muted);
  background: none;
  border: none;
  cursor: pointer;
  padding: 0.15rem 0.35rem;
  border-radius: var(--radius-sm);
}

.app-sidebar__history-all:hover {
  color: var(--color-accent);
  background: var(--color-accent-soft);
}

.app-sidebar__history-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  padding-right: 2px;
}

.app-history-empty {
  font-size: 0.8125rem;
  color: var(--color-text-muted);
  padding: 0.75rem 0.25rem;
  line-height: 1.5;
}

.app-history-card {
  display: flex;
  align-items: stretch;
  gap: 0;
  border-radius: var(--radius-md);
  background: color-mix(in srgb, var(--color-surface-muted) 55%, transparent);
  transition: background 0.2s, box-shadow 0.2s;
}

.app-history-card:hover {
  background: var(--color-surface-muted);
  box-shadow: var(--shadow-sm);
}

.app-history-card__main {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: stretch;
  gap: 0;
  padding: 0;
  border: none;
  background: transparent;
  cursor: pointer;
  text-align: left;
}

.app-history-card__bar {
  width: 3px;
  flex-shrink: 0;
  border-radius: var(--radius-md) 0 0 var(--radius-md);
  background: var(--color-border-strong);
}

.app-history-card[data-phase="script_planning"] .app-history-card__bar {
  background: var(--color-accent);
}

.app-history-card[data-phase="visual_assets"] .app-history-card__bar {
  background: #8b6914;
}

.app-history-card[data-phase="episode_production"] .app-history-card__bar {
  background: #2f6b8a;
}

.app-history-card[data-phase="completed"] .app-history-card__bar,
.app-history-card[data-status="completed"] .app-history-card__bar {
  background: var(--color-success);
}

.app-history-card__body {
  flex: 1;
  min-width: 0;
  padding: 0.5rem 0.35rem 0.5rem 0.55rem;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.app-history-card__title {
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--color-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.app-history-card__sub {
  font-size: 0.6875rem;
  color: var(--color-text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.app-history-card__more {
  flex: none;
  width: 28px;
  align-self: center;
  margin-right: 0.25rem;
  border: none;
  background: transparent;
  color: var(--color-text-muted);
  border-radius: var(--radius-sm);
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s, background 0.15s;
}

.app-history-card:hover .app-history-card__more,
.app-history-card__more:focus {
  opacity: 1;
}

.app-history-card__more:hover {
  background: var(--color-surface-hover);
  color: var(--color-text);
}

.app-history-menu {
  z-index: 200;
  min-width: 148px;
  padding: 0.35rem;
  background: var(--color-surface);
  border: 1px solid color-mix(in srgb, var(--color-border) 80%, transparent);
  border-radius: 12px;
  box-shadow: 0 8px 28px rgb(15 23 42 / 0.12), 0 2px 8px rgb(15 23 42 / 0.06);
}

.app-history-menu button {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  text-align: left;
  padding: 0.45rem 0.65rem;
  font-size: 0.8125rem;
  border: none;
  background: transparent;
  border-radius: var(--radius-sm);
  color: var(--color-text);
  cursor: pointer;
}

.app-history-menu__icon {
  display: inline-flex;
  flex-shrink: 0;
  color: var(--color-text-muted);
}

.app-history-menu__label {
  flex: 1;
  min-width: 0;
}

.app-history-menu button:hover .app-history-menu__icon {
  color: var(--color-text);
}

.app-history-menu button:hover {
  background: var(--color-surface-hover);
}

.app-history-menu button.is-danger {
  color: var(--color-error);
}

.app-history-menu button.is-danger .app-history-menu__icon {
  color: var(--color-error);
}

/* 应用内对话框（替代浏览器原生 prompt/confirm） */
.app-dialog {
  position: fixed;
  inset: 0;
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.25rem;
}

.app-dialog__backdrop {
  position: absolute;
  inset: 0;
  background: rgb(0 0 0 / 0.35);
}

.app-dialog__panel {
  position: relative;
  width: min(420px, 100%);
  padding: 1.25rem;
  border-radius: var(--radius-lg);
  background: var(--color-surface);
  box-shadow: 0 12px 40px rgb(0 0 0 / 0.12);
}

.app-dialog__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.75rem;
}

.app-dialog__title {
  margin: 0;
  font-size: 1.0625rem;
  font-weight: 700;
}

.app-dialog__close {
  border: none;
  background: transparent;
  font-size: 1.35rem;
  line-height: 1;
  color: var(--color-text-muted);
  cursor: pointer;
}

.app-dialog__message {
  margin: 0 0 1rem;
  font-size: 0.9375rem;
  line-height: 1.6;
  color: var(--color-text-muted);
  white-space: pre-wrap;
}

.app-dialog__label {
  display: block;
  margin-bottom: 0.35rem;
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--color-text);
}

.app-dialog__input {
  width: 100%;
  padding: 0.625rem 0.75rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  font-size: 0.9375rem;
}

.app-dialog__foot {
  display: flex;
  justify-content: flex-end;
  margin-top: 1rem;
}

.app-dialog__actions {
  display: flex;
  gap: 0.5rem;
}

.app-dialog__btn-danger {
  background: var(--color-error);
  border-color: var(--color-error);
}

/* 收起态：隐藏文字，仅留图标，水平居中 */
.app-shell.is-collapsed .app-sidebar__logo-name,
.app-shell.is-collapsed .app-nav__label,
.app-shell.is-collapsed .app-nav__group-label,
.app-shell.is-collapsed .app-sidebar__history {
  display: none;
}

.app-shell.is-collapsed .app-sidebar__head {
  padding: 0 0.875rem;
}

.app-shell.is-collapsed .app-sidebar__logo {
  padding: 0.2rem;
}

.app-shell.is-collapsed .app-nav__item {
  justify-content: center;
  gap: 0;
}

/* --- 主工作区 --- */
.app-main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--color-bg);
}

/* 顶栏：当前视图标题 + 主操作 */
.app-topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 1rem;
  height: var(--header-height);
  padding: 0 clamp(1.25rem, 3vw, 2.5rem);
  background: color-mix(in srgb, var(--color-bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(8px);
  border-bottom: 1px solid var(--color-border);
}

.app-topbar__menu {
  display: none;
  flex: none;
  width: 36px;
  height: 36px;
  place-items: center;
  border-radius: var(--radius-md);
  color: var(--color-text);
}

.app-topbar__title {
  font-family: var(--font-display);
  font-size: 1.0625rem;
  font-weight: 700;
  color: var(--color-text);
}

.app-topbar__sub {
  font-size: 0.8125rem;
  color: var(--color-text-muted);
}

.app-topbar__spacer {
  flex: 1;
}

/* 顶栏右上角用户菜单（对标小云雀 headerRight + userMenu） */
.app-topbar__user {
  position: relative;
  flex: none;
}

.app-user-menu {
  position: relative;
}

.app-user-menu--guest {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.25rem 0.5rem 0.25rem 0.25rem;
  border-radius: var(--radius-pill);
  text-decoration: none;
  color: var(--color-text);
  transition: background 0.2s;
}

.app-user-menu--guest:hover {
  background: var(--color-surface-hover);
}

.app-user-menu__guest-label {
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--color-text-muted);
}

.app-user-menu__trigger {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  padding: 0;
  border: 1px solid color-mix(in srgb, var(--color-border) 80%, transparent);
  border-radius: 50%;
  background: color-mix(in srgb, var(--color-surface) 92%, transparent);
  box-shadow: var(--shadow-sm);
  cursor: pointer;
  transition: border-color 0.2s, box-shadow 0.2s, background 0.2s;
}

.app-user-menu__trigger:hover,
.app-user-menu.is-open .app-user-menu__trigger {
  border-color: color-mix(in srgb, var(--color-accent) 35%, var(--color-border));
  background: var(--color-surface);
  box-shadow: var(--shadow-hover);
}

.app-user-menu__avatar {
  flex: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 0.875rem;
  color: var(--color-accent);
  background: var(--color-accent-soft);
  object-fit: cover;
}

.app-user-menu__trigger .app-user-menu__avatar {
  width: 28px;
  height: 28px;
  font-size: 0.8125rem;
}

.app-user-menu__popover {
  position: absolute;
  right: 0;
  top: calc(100% + 10px);
  z-index: 120;
  width: min(320px, calc(100vw - 2rem));
}

.app-user-menu__panel {
  padding: 1.25rem 1.25rem 0.75rem;
  border: 1px solid color-mix(in srgb, var(--color-border) 70%, white);
  border-radius: 22px;
  background: color-mix(in srgb, var(--color-surface) 94%, transparent);
  backdrop-filter: blur(20px) saturate(1.15);
  box-shadow: 0 20px 56px rgb(15 23 42 / 0.14);
}

.app-user-menu__avatar-edit {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.app-user-menu__avatar-btn {
  position: relative;
  display: grid;
  place-items: center;
  width: 72px;
  height: 72px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: transparent;
  cursor: pointer;
}

.app-user-menu__avatar--lg {
  width: 72px;
  height: 72px;
  font-size: 1.5rem;
}

.app-user-menu__avatar-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  border-radius: 50%;
  color: white;
  background: rgb(0 0 0 / 0.38);
  opacity: 0;
  transition: opacity 0.2s;
}

.app-user-menu__avatar-btn:hover .app-user-menu__avatar-overlay,
.app-user-menu__avatar-btn:focus-visible .app-user-menu__avatar-overlay {
  opacity: 1;
}

.app-user-menu__text-btn {
  border: none;
  background: transparent;
  font-size: 0.75rem;
  color: var(--color-text-muted);
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.app-user-menu__text-btn:hover {
  color: var(--color-accent);
}

.app-user-menu__field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-bottom: 0.75rem;
}

.app-user-menu__label {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--color-text-muted);
}

.app-user-menu__input {
  width: 100%;
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-surface);
  font-size: 0.9375rem;
  color: var(--color-text);
}

.app-user-menu__input:focus {
  outline: none;
  border-color: color-mix(in srgb, var(--color-accent) 45%, var(--color-border));
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--color-accent) 14%, transparent);
}

.app-user-menu__credits {
  margin: 0 0 0.25rem;
  font-size: 0.8125rem;
  color: var(--color-text-muted);
}

.app-user-menu__credits strong {
  color: var(--color-text);
  font-weight: 600;
}

.app-user-menu__email {
  margin: 0 0 1rem;
  font-size: 0.75rem;
  color: var(--color-text-muted);
  word-break: break-all;
}

.app-user-menu__actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}

.app-user-menu__save {
  flex: 1;
  min-height: 40px;
}

.app-user-menu__link {
  flex: none;
  font-size: 0.75rem;
  color: var(--color-text-muted);
  text-decoration: none;
  white-space: nowrap;
}

.app-user-menu__link:hover {
  color: var(--color-accent);
}

.app-user-menu__foot {
  padding-top: 0.75rem;
  border-top: 1px solid var(--color-border);
}

.app-user-menu__logout {
  width: 100%;
  min-height: 40px;
  border: none;
  border-radius: 12px;
  background: transparent;
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--color-text-muted);
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}

.app-user-menu__logout:hover {
  background: var(--color-surface-hover);
  color: var(--color-text);
}

/* 视图容器：默认隐藏，仅显示 active */
.app-views {
  flex: 1;
  padding: clamp(1.5rem, 3vw, 2.5rem) clamp(1.25rem, 3vw, 2.5rem) 3rem;
}

.app-view {
  display: none;
}

.app-view.is-active {
  display: block;
}

/* --- 移动端：侧栏退化为抽屉 --- */
.app-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 45;
  background: rgba(28, 27, 25, 0.4);
  opacity: 0;
  transition: opacity 0.25s;
}

@media (max-width: 768px) {
  .app-shell,
  .app-shell.is-collapsed {
    grid-template-columns: 1fr;
  }

  .app-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: var(--sidebar-w);
    transform: translateX(-100%);
    transition: transform 0.28s cubic-bezier(0.16, 1, 0.3, 1);
    box-shadow: var(--shadow-hover);
  }

  /* 移动端始终展开文字（抽屉态） */
  .app-shell.is-collapsed .app-nav__label,
  .app-shell.is-collapsed .app-sidebar__logo-name {
    display: block;
  }

  .app-shell.is-collapsed .app-nav__item {
    justify-content: flex-start;
    gap: 0.75rem;
  }

  .app-shell.is-drawer-open .app-sidebar {
    transform: translateX(0);
  }

  .app-shell.is-drawer-open .app-backdrop {
    display: block;
    opacity: 1;
  }

  /* 抽屉态隐藏桌面收起按钮（用 backdrop 关闭） */
  .app-sidebar__toggle {
    display: none;
  }

  .app-topbar__menu {
    display: grid;
  }
}

@media (prefers-reduced-motion: reduce) {
  .app-shell,
  .app-sidebar,
  .app-sidebar__toggle svg {
    transition: none;
  }
}
