/* ============================================================
   FreeStory · H5 移动端适配
   桌面端隐藏移动专用元素；<=820px 切换为移动应用布局：
   固定顶栏 + 侧边抽屉 + 沉浸对话页 + 安全区。
   ============================================================ */

/* 桌面端：移动专用元素默认隐藏 */
.mobile-topbar { display: none; }
.drawer-backdrop { display: none; }
.chat-back-button { display: none; }
.chat-more-button { display: none; }
.chat-more-menu { display: none; }

@media (max-width: 820px) {
  :root {
    --sidebar-width: 288px;
    --mobile-topbar-h: 52px;
  }

  /* ---------- 顶部移动栏 ---------- */
  .mobile-topbar {
    position: fixed;
    top: 0; left: 0; right: 0;
    z-index: 60;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: calc(var(--mobile-topbar-h) + env(safe-area-inset-top));
    padding: env(safe-area-inset-top) 10px 0;
    background: rgb(20 19 23 / 82%);
    -webkit-backdrop-filter: blur(16px);
    backdrop-filter: blur(16px);
    border-bottom: 1px solid rgb(255 255 255 / 6%);
  }
  .mobile-icon-button {
    display: grid;
    place-items: center;
    width: 40px;
    height: 40px;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: transparent;
    color: var(--text);
  }
  .mobile-icon-button:active { background: rgb(255 255 255 / 8%); }
  .mobile-brand {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 6px 10px;
    border: 0;
    border-radius: 10px;
    background: transparent;
    color: #fff;
    font-size: 18px;
    font-weight: 750;
    letter-spacing: -.6px;
  }
  .mobile-avatar-button {
    display: grid;
    place-items: center;
    width: 40px;
    height: 40px;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: transparent;
  }
  .mobile-avatar-mark {
    width: 34px;
    height: 34px;
    flex: 0 0 34px;
    font-size: 14px;
  }

  /* ---------- 抽屉遮罩 ---------- */
  .drawer-backdrop {
    position: fixed;
    inset: 0;
    z-index: 65;
    display: block;
    background: rgb(0 0 0 / 52%);
    opacity: 0;
    pointer-events: none;
    transition: opacity .25s ease;
  }
  body.nav-open .drawer-backdrop { opacity: 1; pointer-events: auto; }

  /* ---------- 整体单列 ---------- */
  .app-shell {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    padding-top: calc(var(--mobile-topbar-h) + env(safe-area-inset-top));
  }

  /* ---------- 侧边栏 → 抽屉 ---------- */
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: min(var(--sidebar-width), 84vw);
    height: 100dvh;
    z-index: 70;
    background: #16141a;
    box-shadow: none;
    transform: translateX(-100%);
    visibility: visible;
    transition: transform .3s cubic-bezier(.2,.8,.2,1);
  }
  body.nav-open .sidebar { transform: translateX(0); box-shadow: 24px 0 60px rgb(0 0 0 / 40%); }
  /* 恢复抽屉完整导航（覆盖旧的 767px 窄条规则） */
  .sidebar-header { padding: calc(28px + env(safe-area-inset-top)) 20px 0; }
  .brand { justify-content: flex-start; gap: 9px; }
  .brand > span,
  .nickname,
  .nav-item > span:not(.nav-icon):not(.history-cover),
  .history-copy { display: block; }
  .search { margin-top: 20px; }
  .search input { padding: 0 12px 0 40px; color: var(--text); }
  .search input::placeholder { color: var(--text-muted); }
  .sidebar-navigation { padding: 20px var(--nav-inset) 20px; }
  .nav-item { justify-content: flex-start; padding-inline: 13px; }
  .nav-group h2 { padding-inline: 13px; text-align: left; font-size: 10px; letter-spacing: 1px; }
  .group-heading { display: flex; padding-right: 13px; }
  .history-all { display: flex; width: auto; padding: 4px 0 4px 8px; }
  .profile { justify-content: flex-start; padding-inline: 16px; }
  .selection-indicator { display: none; }

  /* ---------- 对话页：全屏沉浸 ---------- */
  /* 对话激活时隐藏全局顶栏，对话页占满整屏 */
  body.in-chat .mobile-topbar { display: none; }
  body.in-chat .app-shell { padding-top: 0; }
  body.in-chat .workspace { min-height: 100dvh; }

  .chat-view {
    height: 100dvh;
    grid-template-rows: auto minmax(0, 1fr) auto;
  }
  .chat-header {
    min-height: calc(52px + env(safe-area-inset-top));
    padding: env(safe-area-inset-top) 8px 6px;
    gap: 4px;
  }
  /* 返回按钮显示 */
  .chat-back-button {
    display: grid;
    place-items: center;
    flex: 0 0 38px;
    width: 38px;
    height: 40px;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: transparent;
    color: var(--text);
  }
  .chat-back-button:active { background: rgb(255 255 255 / 8%); }
  .chat-identity { gap: 9px; min-width: 0; }
  .chat-identity > img { flex: 0 0 26px; width: 26px; height: 39px; }
  .chat-identity h1 { font-size: 15px; }
  /* 桌面端工具折叠：模型选择、设置、检查器隐藏，更多按钮显示 */
  .chat-actions .model-picker,
  .chat-actions .chat-tool-button { display: none; }
  .chat-actions { gap: 2px; }
  .chat-more-button { display: grid; place-items: center; width: 40px; height: 40px; padding: 0; border: 0; border-radius: 50%; background: transparent; color: var(--text); }
  .chat-more-button:active { background: rgb(255 255 255 / 8%); }

  /* 更多菜单（从顶部下拉） */
  .chat-more-menu {
    position: fixed;
    top: calc(env(safe-area-inset-top) + 6px);
    right: 10px;
    z-index: 80;
    display: flex;
    flex-direction: column;
    min-width: 230px;
    padding: 6px;
    border: 1px solid rgb(255 255 255 / 12%);
    border-radius: 14px;
    background: #242129;
    box-shadow: 0 18px 48px rgb(0 0 0 / 45%);
  }
  .chat-more-menu[hidden] { display: none; }
  .chat-more-item {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    padding: 12px 12px;
    border: 0;
    border-radius: 9px;
    background: transparent;
    color: var(--text);
    font-size: 14px;
    font-weight: 550;
    text-align: left;
  }
  .chat-more-item:active { background: rgb(255 255 255 / 8%); }
  .chat-more-item img { opacity: .8; }
  .chat-more-sublist {
    display: grid;
    gap: 2px;
    margin: 2px 0 4px;
    padding: 6px 0 6px 30px;
    border-left: 1px solid rgb(255 255 255 / 12%);
  }
  .chat-more-sublist[hidden] { display: none; }
  .chat-more-subitem {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    padding: 9px 12px;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: var(--text-secondary);
    font-size: 13px;
    text-align: left;
  }
  .chat-more-subitem:active { background: rgb(255 255 255 / 8%); }
  .chat-more-subitem.is-current { color: #fff; font-weight: 600; }
  .chat-more-subitem:disabled { opacity: .4; }

  /* 消息区：全宽、更紧凑 */
  .chat-messages {
    width: 100%;
    padding: 16px 14px 14px;
  }
  .chat-message + .chat-message { margin-top: 22px; }
  .message-body { font-size: 14.5px; line-height: 1.8; }
  .message-user { max-width: 88%; padding: 11px 15px; }

  /* 输入区：全宽、贴底、安全区 */
  .composer-dock {
    padding: 10px 10px max(10px, env(safe-area-inset-bottom));
  }
  .chat-bottom-controls {
    width: 100%;
    padding-inline: 14px;
  }
  .message-form {
    width: 100%;
    min-height: 0;
    padding: 8px 8px 8px 14px;
    border-radius: 22px;
  }
  .message-form textarea { min-height: 38px; font-size: 15px; }
  .composer-hint { display: none; }
  /* 次要工具（星号/斜杠）在移动端只保留建议回复，其余收进输入体验 */
  .composer-tools > #composer-emphasis,
  .composer-tools > #composer-command-toggle { display: none; }
  .composer-tools { gap: 6px; }

  /* ---------- 通用页面内边距 ---------- */
  .home-view {
    width: 100%;
    max-width: 100%;
    padding: 20px 14px 32px;
  }

  /* 首页：特色区纵向 */
  .home-hero {
    display: flex;
    flex-direction: column;
    gap: 18px;
    min-height: 0;
  }
  .hero-copy { order: 2; text-align: center; }
  .hero-copy h1 {
    white-space: normal;
    font-size: 22px;
    text-align: center;
  }
  .hero-copy > p {
    white-space: normal;
    font-size: 14px;
    text-align: center;
  }
  .cover-carousel {
    order: 1;
    height: 320px;
    --cover-width: 150px;
    --cover-height: 225px;
  }
  .cover-stage { inset: 0; }
  .play-button {
    min-width: 130px;
    height: 46px;
    font-size: 14px;
  }
  /* 分类胶囊 */
  .story-filters {
    gap: 8px;
    padding-bottom: 10px;
  }
  .genre-filter { min-height: 36px; padding: 7px 16px; font-size: 13px; }
  /* 故事网格 2 列 */
  .story-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    column-gap: 14px;
    row-gap: 22px;
  }
  .story-card-title { font-size: 14px; height: 38px; line-height: 19px; }

  /* 故事详情弹窗：全屏化（移动端 sheet 感） */
  .story-preview {
    max-width: 100%;
    width: 100%;
    max-height: 92dvh;
    padding: 0;
    border-radius: 20px 20px 0 0;
    align-self: end;
    margin-bottom: 0;
  }
  .story-preview[open] {
    grid-template-columns: 1fr;
    gap: 0;
  }
  .story-details-scroll { padding: 18px 18px 8px; }
  .story-details-hero { gap: 14px; }
  .preview-cover { width: 96px; height: 144px; }
  .story-details-footer {
    position: sticky;
    bottom: 0;
    padding: 12px 18px max(14px, env(safe-area-inset-bottom));
    background: linear-gradient(transparent, #1c1a20 28%);
  }
  .story-play-button { padding: 13px; font-size: 14px; }

  /* 弹窗类通用：接近全屏 */
  .points-dialog {
    max-width: 100%;
    width: 100%;
    max-height: 92dvh;
    border-radius: 20px 20px 0 0;
    align-self: end;
    margin-bottom: 0;
  }
  .points-dialog-body { padding: 16px 18px max(20px, env(safe-area-inset-bottom)); }
  .auth-dialog {
    max-width: 100%;
    width: 100%;
    height: 100dvh;
    max-height: 100%;
    border-radius: 0;
  }

  /* ---------- Creator 工具栏 ---------- */
  .creator-toolbar { flex-wrap: wrap; gap: 12px; margin-bottom: 20px; }
  .creator-toolbar-filters { flex-wrap: wrap; gap: 12px; }
  .creator-toolbar-filters .creator-search { flex: 1 1 100%; }
  .creator-status-filters {
    width: 100%;
    gap: 6px;
    padding-bottom: 2px;
    overflow-x: auto;
    scrollbar-width: none;
  }
  .creator-status-filters::-webkit-scrollbar { display: none; }
  .creator-status-filter { flex: 0 0 auto; padding-inline: 14px; }
  .creator-toolbar-actions { width: 100%; justify-content: flex-end; gap: 8px; }
  .creator-create { height: 42px; padding-inline: 18px; font-size: 13px; }
  .creator-layout-toggle { display: none; }
  .creator-story { grid-template-columns: 56px minmax(0, 1fr); column-gap: 14px; padding: 18px 0; }
  .creator-artwork { grid-row: 1 / 3; width: 56px; height: 84px; }
  .creator-cover { width: 56px; height: 84px; }
  .creator-title { font-size: 14px; }
  .creator-description { font-size: 12px; }
  .creator-row-actions { grid-column: 1 / -1; }
  .creator-prompt, .creator-import {
    width: 100%;
    max-width: 100%;
    max-height: 92dvh;
    border-radius: 20px 20px 0 0;
    align-self: end;
  }

  /* ---------- Profile ---------- */
  .profile-view { gap: 14px; }
  .profile-card { padding: 18px 16px; }

  /* ---------- Analytics ---------- */
  .analytics-summary {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 18px 14px;
    padding: 22px 0;
  }
  .analytics-kpi-value { font-size: 24px; }
  .analytics-presets { width: 100%; overflow-x: auto; scrollbar-width: none; }
  .analytics-presets::-webkit-scrollbar { display: none; }
  .analytics-dates { width: 100%; flex-wrap: wrap; }
  .analytics-dates label { flex: 1; }
  .analytics-dates input { width: 100%; min-width: 0; }
  .analytics-table { min-width: 620px; }

  /* ---------- Prompt / Model 页 ---------- */
  .prompt-heading { flex-wrap: wrap; gap: 12px; }
  .prompt-heading h1 { font-size: 22px; }
  .purpose-tabs { overflow-x: auto; scrollbar-width: none; }
  .purpose-tabs::-webkit-scrollbar { display: none; }
  .purpose-tab { flex: 0 0 auto; }
  .prompt-editor { min-height: 42dvh; font-size: 13px; }

  /* ---------- Notices ---------- */
  .notice-card { padding: 20px 0; }
  .notice-body { font-size: 13px; }

  /* ---------- Chat tools 弹窗（Play settings / Inspector） ---------- */
  .chat-tools-dialog,
  .chat-tools-dialog.inspector {
    width: 100%;
    max-width: 100%;
    height: 100dvh;
    max-height: 100%;
    border-radius: 0;
  }
  .chat-tools-header { padding: 16px 18px; }
  .chat-tools-navigation {
    flex-wrap: wrap;
    gap: 10px;
    padding: 10px 14px;
  }
  .chat-tools-tabs {
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
  }
  .chat-tools-tabs::-webkit-scrollbar { display: none; }
  .chat-tools-body { padding: 16px 14px; }
  .chat-tools-footer { padding: 12px 14px max(12px, env(safe-area-inset-bottom)); }

  /* ---------- Builder（create.html） ---------- */
  .builder-shell { padding: 0 14px max(40px, env(safe-area-inset-bottom)); }
  .builder-header {
    min-height: calc(64px + env(safe-area-inset-top));
    padding-top: env(safe-area-inset-top);
    flex-wrap: wrap;
    gap: 12px;
  }
  .builder-header-actions { width: 100%; justify-content: flex-end; }
  .builder-steps { gap: 6px; padding-bottom: 14px; margin-bottom: 20px; }
  .builder-step { padding-inline: 12px; }
  .builder-columns { grid-template-columns: minmax(0, 1fr); gap: 28px; }
  .builder-preview { position: static; max-height: none; padding: 18px; }
  .builder-step-intro h2 { font-size: 21px; }
  .builder-step-footer {
    position: sticky;
    bottom: 0;
    gap: 12px;
    margin: 0 -14px;
    padding: 12px 14px max(12px, env(safe-area-inset-bottom));
    background: linear-gradient(transparent, #151515 32%);
  }
  .builder-step-footer .builder-button { flex: 1; }
  .builder-success {
    width: 100%;
    max-width: 100%;
    height: 100dvh;
    max-height: 100%;
    border-radius: 0;
  }
}

/* ---------- 更小屏（<=380px）微调 ---------- */
@media (max-width: 380px) {
  .story-grid { column-gap: 10px; }
  .cover-carousel { --cover-width: 138px; --cover-height: 207px; height: 290px; }
  .chat-identity h1 { font-size: 14px; }
}
