/* ============================================
   Amaker AI 工坊 — 移动端专用样式
   在 muji.css 之后、muji-pc.css 之前加载
   断点: <360px 超窄屏 | 360~767px 标准手机 | ≥768px 交给 PC CSS
   ============================================ */

/* ═══════════════════════════════════════════
   安全区变量
   ═══════════════════════════════════════════ */
:root {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --header-h: 56px;
  --tabbar-h: 52px;
}

/* ═══════════════════════════════════════════
   全局移动端基线
   ═══════════════════════════════════════════ */
@media (max-width: 767px) {
  body {
    overscroll-behavior-y: none;
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
  }

  /* ── Header 精简 ── */
  .header {
    padding: 0 12px;
    padding-top: var(--safe-top);
    height: calc(var(--header-h) + var(--safe-top));
    overflow: hidden;
    gap: 6px;
  }
  .header__logo { font-size: 16px; flex-shrink: 0; }
  .header__logo span { display: none; }
  .header > div { gap: 4px; }

  /* 管理按钮默认隐藏，通过汉堡菜单展开（保留登录/退出/访客按钮） */
  .header > div > .btn-sm[id$="-btn"]:not(#login-btn):not(#visitor-btn):not(#logout-btn) {
    display: none;
  }

  /* 汉堡菜单按钮 */
  .mobile-menu-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border: 1px solid var(--muji-border);
    border-radius: 8px;
    background: white;
    font-size: 18px;
    cursor: pointer;
    flex-shrink: 0;
  }

  /* 汉堡菜单面板 */
  .mobile-menu-panel {
    position: fixed;
    top: calc(var(--header-h) + var(--safe-top));
    right: 8px;
    background: white;
    border: 1px solid var(--muji-border);
    border-radius: 12px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.12);
    padding: 8px;
    z-index: 200;
    display: none;
    flex-direction: column;
    gap: 4px;
    min-width: 140px;
  }
  .mobile-menu-panel.open {
    display: flex;
  }
  .mobile-menu-panel .btn-sm {
    width: 100%;
    justify-content: flex-start;
    padding: 10px 14px;
    font-size: 14px;
    min-height: 40px;
    border: 1px solid transparent;
    background: var(--muji-bg-sub);
  }
  .mobile-menu-panel .btn-sm:active {
    background: var(--muji-border);
  }

  /* ── 底部固定导航栏（微信风格） ── */
  .home-tab-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    top: auto;
    max-width: 100%;
    margin: 0;
    border-radius: 0;
    border-left: none;
    border-right: none;
    border-bottom: none;
    border-top: 1px solid #E8E8E8;
    padding: 0;
    padding-bottom: var(--safe-bottom);
    box-shadow: none;
    background: #FCFCFC;
    z-index: 100;
    display: flex;
    height: calc(var(--tabbar-h) + var(--safe-bottom));
  }
  .home-tab-btn {
    flex: 1;
    min-height: var(--tabbar-h);
    font-size: 13px;
    align-items: center;
    justify-content: center;
    padding: 4px 0;
    line-height: 1.2;
    color: #888;
    background: none;
    border: none;
    border-radius: 0;
    cursor: pointer;
    transition: color 0.15s, background 0.15s;
    -webkit-tap-highlight-color: transparent;
    -webkit-user-select: none;
    user-select: none;
  }
  .home-tab-btn .tab-icon {
    display: none;
  }
  .home-tab-btn.active {
    color: #6B8E6B;
    font-weight: 600;
    background: rgba(107, 142, 107, 0.06);
    box-shadow: 0 1px 0 rgba(107, 142, 107, 0.25);
  }
  .home-tab-btn:active {
    background: rgba(107, 142, 107, 0.1);
  }

  /* 排序标签栏 — 发现页 sticky 在 Header 下方 */
  .discover-sort {
    display: none !important;
    align-items: center;
    gap: 6px;
    padding: 8px 12px;
    background: #FCFCFC;
    border-bottom: 1px solid #F0EDE4;
    position: sticky;
    top: calc(var(--header-h) + var(--safe-top));
    z-index: 49;
  }
  .discover-sort.visible {
    display: flex !important;
  }

  /* 发现页隐藏分类/标签栏 */
  .discover-hidden {
    display: none !important;
  }
  .discover-sort__label {
    font-size: 11px;
    color: #999;
    margin-right: 2px;
  }
  .discover-sort__tab {
    font-size: 12px;
    color: #888;
    padding: 5px 14px;
    border-radius: 14px;
    cursor: pointer;
    background: #F5F0EB;
    transition: all 0.15s;
    min-height: 30px;
    display: inline-flex;
    align-items: center;
  }
  .discover-sort__tab.active {
    background: #6B8E6B;
    color: white;
    font-weight: 500;
  }
  .discover-sort__tab:active {
    background: #E0EDE0;
  }
  .discover-sort__tab.active:active {
    background: #5A7D5A;
  }

  /* 内容区底部留白 */
  #list-section,
  #workshops-tab-content,
  #courses-tab-content,
  #detail-section,
  #pdf-section,
  #create-section,
  #generate-section {
    padding-bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 16px);
  }

  /* ── 分类标签 + 动态标签合并为单行横滚 ── */
  #category-bar .filter-bar__tags {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 4px;
  }
  #category-bar .filter-bar__tags::-webkit-scrollbar { display: none; }
  #category-bar {
    padding: 6px 12px;
    margin-top: 0;
    border-bottom: 1px solid #F0EDE4;
  }
  .filter-bar__sep {
    display: inline-block;
    width: 1px;
    height: 20px;
    background: #D4CFC2;
    margin: 0 6px;
    flex-shrink: 0;
    align-self: center;
  }
  #tag-list {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    flex-wrap: nowrap;
  }

  /* ── 双列卡片网格 ── */
  .project-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
    padding: 8px 10px;
  }

  /* 卡片紧凑化 */
  .project-card__body { padding: 6px 8px; }
  .project-card__name { font-size: 13px; line-height: 1.3; margin-bottom: 2px; }
  .project-card__meta { font-size: 10px; margin-top: 2px; }
  .gallery-card__body { padding: 6px 8px; }
  .gallery-card__title { font-size: 13px; line-height: 1.3; }
  .gallery-card__meta { font-size: 10px; }
  .gallery-card__stats { font-size: 10px; gap: 4px; }
  .project-card__price { font-size: 13px; }
  .project-card__tags { gap: 3px; margin-top: 3px; }
  .project-card__tags .tag { font-size: 10px; padding: 1px 6px; }

  /* 轮播图 4:3 比例，节省纵向空间 */
  .card-carousel { padding-bottom: 75%; }

  /* ── 触摸目标增大 ── */
  .tag {
    padding: 6px 14px;
    font-size: 13px;
    min-height: 32px;
  }
  .btn-sm {
    padding: 8px 14px;
    font-size: 13px;
    min-height: 36px;
  }
  .modal__close {
    width: 36px;
    height: 36px;
  }
  .carousel-dot { width: 8px; height: 8px; }
  .carousel-dot.active { width: 20px; }

  /* ── 表单防 iOS 缩放 ── */
  .muji-input,
  .muji-textarea,
  .edit-textarea,
  input[type="text"],
  input[type="password"],
  input[type="number"],
  input[type="tel"],
  textarea,
  select {
    font-size: 16px !important;
  }
  .form-group { margin-bottom: 14px; }
  .upload-zone { padding: 28px 16px; }

  /* ── 模态弹窗底部滑入 ── */
  .modal-overlay {
    align-items: flex-end;
  }
  .modal {
    width: 100%;
    max-width: 100%;
    border-radius: 16px 16px 0 0;
    padding: 20px 16px;
    padding-bottom: calc(20px + var(--safe-bottom));
    max-height: 85vh;
    animation: mobileSlideUp 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  }
  .modal::before {
    content: '';
    display: block;
    width: 36px;
    height: 4px;
    background: var(--muji-border);
    border-radius: 2px;
    margin: 0 auto 16px;
  }
  @keyframes mobileSlideUp {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
  }

  /* ── 卡片触摸反馈 ── */
  .project-card,
  .gallery-card {
    transition: transform 0.15s, box-shadow 0.15s;
  }
  /* ── 发现页移动端适配 ── */
  #discover-list {
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
    padding: 8px 10px;
  }
  .project-card:active,
  .gallery-card:active {
    transform: scale(0.97);
    box-shadow: 0 1px 4px rgba(0,0,0,0.08);
  }
  .btn:active {
    transform: scale(0.96);
    opacity: 0.85;
  }
  .home-tab-btn:active {
    background: var(--muji-bg-sub);
  }

  /* ── 页面切换淡入 ── */
  #detail-section,
  #pdf-section,
  #create-section,
  #generate-section {
    animation: mobileFadeIn 0.25s ease;
  }
  @keyframes mobileFadeIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
  }

  /* ── muji-card 撑满 ── */
  .muji-card {
    max-width: 100%;
    margin: 0;
    border-radius: 0;
    border: none;
    border-bottom: 1px solid var(--muji-border);
  }
  .muji-card-header { padding: 12px 14px 8px; }
  .muji-card-body { padding: 12px 14px; }
  .muji-card-footer { padding: 10px 14px; font-size: 13px; }
  .muji-card-title { font-size: 16px; }

  /* ── 轮播图适配 ── */
  #purchase-carousel { border-radius: 8px; margin-bottom: 10px; }
  #purchase-carousel > div[style*="min-height"] { min-height: 200px !important; }
  #purchase-carousel img { max-height: 260px !important; }

  /* 轮播箭头增大 */
  #purchase-carousel button[onclick^="purchaseSlide"] {
    width: 36px !important;
    height: 36px !important;
    font-size: 16px !important;
  }

  /* ── 创作卡紧凑化 ── */
  #ai-prompt { min-height: 100px !important; }
  #gen-prompt { min-height: 120px !important; }
  #ai-ref-carousel { border-radius: 8px; }
  #ai-ref-carousel .ai-ref-slide { min-height: 200px !important; }
  #ai-ref-carousel img { max-height: 240px !important; }
  .ai-ref-nav-item { width: 48px !important; height: 48px !important; }
  .diy-btn { padding: 8px 6px; font-size: 13px; }

  /* ── PDF 打印卡 ── */
  .template-selector { grid-template-columns: repeat(3, 1fr) !important; }
  .template-opt { padding: 10px 4px; font-size: 12px; }
  .template-opt .tpl-icon { font-size: 18px; }
  .slot-row { flex-wrap: wrap; padding: 8px; }
  .slot-preview { width: 56px; height: 56px; }
  .slot-info { min-width: calc(100% - 64px - 80px); }
  .slot-actions { flex-direction: row; width: 100%; justify-content: flex-end; margin-top: 4px; }
  .paper-preview { width: 100% !important; max-width: 240px; }
  .paper-preview.a5 { width: 100% !important; max-width: 200px; }

  /* ── 发现页详情 ── */
  .detail-cover { aspect-ratio: 4/3 !important; border-radius: 8px !important; }
  .detail-images img { width: 64px !important; height: 64px !important; border-radius: 6px !important; }
  .detail-interactions { gap: 8px !important; }
  .interact-btn { padding: 6px 10px !important; font-size: 16px !important; }
  .detail-title { font-size: 17px !important; }
  .detail-story { font-size: 14px !important; padding: 10px !important; }

  /* 评论输入固定底部 */
  .comment-input-wrap {
    position: sticky;
    bottom: 0;
    background: var(--muji-bg);
    padding: 8px 12px calc(8px + var(--safe-bottom)) !important;
    z-index: 50;
  }

  /* ── 管理后台 ── */
  .admin-header { flex-wrap: wrap; gap: 8px; padding: 10px 12px; }
  .admin-header h1 { font-size: 16px; }
  .admin-header a { font-size: 12px; }
  .admin-tabs { gap: 4px; overflow-x: auto; flex-wrap: nowrap; scrollbar-width: none; }
  .admin-tabs::-webkit-scrollbar { display: none; }
  .admin-tab { white-space: nowrap; padding: 6px 12px; font-size: 12px; }
  .stat-grid { grid-template-columns: repeat(2, 1fr) !important; gap: 8px; }
  .stat-card { padding: 12px; }
  .stat-card .stat-value { font-size: 20px; }
  .stat-card .stat-label { font-size: 11px; }

  /* ── 骨架屏 ── */
  .skeleton {
    background: linear-gradient(90deg, #F0EDE4 25%, #FAF8F5 50%, #F0EDE4 75%);
    background-size: 200% 100%;
    animation: skeletonShimmer 1.5s infinite;
    border-radius: 8px;
  }
  @keyframes skeletonShimmer {
    0% { background-position: 200% 0; }
    100% { background-position: -200% 0; }
  }
  .skeleton-card {
    background: #fff;
    border-radius: 12px;
    overflow: hidden;
  }
  .skeleton-card .skeleton-img { width: 100%; aspect-ratio: 1; }
  .skeleton-card .skeleton-body { padding: 8px 10px; }
  .skeleton-card .skeleton-title { height: 14px; width: 70%; margin-bottom: 6px; }
  .skeleton-card .skeleton-meta { height: 10px; width: 50%; }

  /* ── 回到顶部按钮 ── */
  .scroll-top-btn {
    position: fixed;
    right: 16px;
    bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 72px);
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: rgba(107, 142, 107, 0.9);
    color: white;
    border: none;
    font-size: 18px;
    cursor: pointer;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s, transform 0.3s;
    z-index: 90;
    box-shadow: 0 2px 12px rgba(0,0,0,0.15);
  }
  .scroll-top-btn.visible {
    opacity: 1;
    pointer-events: all;
  }
  .scroll-top-btn:active {
    transform: scale(0.9);
  }

  /* ── 自由排版编辑器 ── */
  #free-form-editor-container,
  .ff-editor {
    display: flex !important;
    flex-direction: column !important;
    min-height: 0;
  }

  /* 工具栏在画布下方，按钮自适应文字宽度 */
  .ff-toolbar {
    order: 1;
    flex-wrap: nowrap;
    justify-content: center;
    overflow-x: auto;
    scrollbar-width: none;
    padding: 8px 6px;
    gap: 8px;
    border-top: 1px solid #E8E4DB;
    border-bottom: none;
  }
  .ff-toolbar::-webkit-scrollbar { display: none; }
  .ff-toolbar__btn {
    padding: 8px 14px;
    font-size: 14px;
    white-space: nowrap;
    min-height: 40px;
    flex-shrink: 0;
  }
  .ff-toolbar__sep { flex-shrink: 0; }

  /* 文字属性面板独立行 */
  .ff-text-props {
    order: 0;
    width: 100%;
    padding: 6px 10px;
    background: #F5F0EB;
    border-bottom: 1px solid #E8E4DB;
    overflow-x: auto;
    flex-wrap: nowrap;
  }
  .ff-text-props select,
  .ff-text-props input {
    font-size: 14px;
    min-height: 32px;
    flex-shrink: 0;
  }

  /* SVG 画布自适应 */
  .ff-editor__canvas {
    width: 100%;
    max-height: calc(100vh - 280px);
    min-height: 300px;
    touch-action: none;
  }

  /* 选中虚线框加粗 */
  .ff-select-box {
    stroke-width: 0.8;
    stroke-dasharray: 3, 1.5;
  }

  /* 状态提示移动端隐藏 */
  .ff-status {
    display: none;
  }

  /* 控制面板（旋转/缩放/文字编辑） */
  .ff-control-panel {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: white;
    border-radius: 16px 16px 0 0;
    padding: 16px 20px calc(16px + var(--safe-bottom));
    box-shadow: 0 -4px 24px rgba(0,0,0,0.12);
    z-index: 2000;
    animation: ffSlideUp 0.25s ease;
  }
  @keyframes ffSlideUp {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
  }
  .ff-control-panel__header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
    font-size: 15px;
    font-weight: 600;
  }
  .ff-control-panel__value { color: #5B7A5B; font-size: 18px; }
  .ff-control-panel__close {
    width: 32px; height: 32px;
    border-radius: 50%;
    border: none;
    background: #F5F0EB;
    font-size: 16px;
    cursor: pointer;
  }
  .ff-control-panel__slider {
    width: 100%;
    height: 44px;
    margin-bottom: 12px;
    accent-color: #5B7A5B;
  }
  .ff-control-panel__presets {
    display: flex;
    gap: 8px;
    justify-content: center;
  }
  .ff-control-panel__presets button {
    padding: 8px 16px;
    border: 1px solid #D4CFC2;
    border-radius: 8px;
    background: white;
    font-size: 13px;
    cursor: pointer;
    min-height: 40px;
  }

  /* 文字编辑面板 */
  .ff-text-edit-input {
    width: 100%;
    min-height: 80px;
    padding: 12px;
    border: 1px solid #D4CFC2;
    border-radius: 8px;
    font-size: 16px;
    font-family: inherit;
    resize: vertical;
    margin-bottom: 12px;
  }
  .ff-text-edit-props {
    display: flex;
    gap: 8px;
    align-items: center;
  }
  .ff-text-edit-props select,
  .ff-text-edit-props input {
    font-size: 16px;
    padding: 8px 12px;
    border: 1px solid #D4CFC2;
    border-radius: 8px;
    min-height: 40px;
  }

  /* 图片选择面板 */
  .ff-image-picker__grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    margin-bottom: 16px;
  }
  .ff-image-picker__item {
    aspect-ratio: 1;
    border-radius: 8px;
    overflow: hidden;
    border: 2px solid transparent;
    cursor: pointer;
    background: #F5F0EB;
  }
  .ff-image-picker__item:active { border-color: #5B7A5B; }
  .ff-image-picker__item img { width: 100%; height: 100%; object-fit: cover; }
  .ff-image-picker__title {
    font-size: 13px;
    font-weight: 600;
    color: #5B4A3A;
    margin-bottom: 8px;
  }

  /* 空状态引导 */
  .ff-empty-hint {
    text-align: center;
    padding: 60px 20px;
    color: #8C8C8C;
    background: #FFF8F0;
    border: 1px dashed #D4CFC2;
    border-radius: 8px;
    margin: 8px;
    min-height: 200px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }

  /* 编辑器页面 muji-card 撑满 */
  #pdf-content .muji-card {
    max-width: 100%;
    margin: 0;
    border-radius: 0;
    border: none;
  }
  #pdf-content .muji-card-header { padding: 6px 10px; }
  #pdf-content .muji-card-body { padding: 4px 6px; }

  /* 操作按钮行 sticky 底部 */
  #pdf-content .muji-card-body > div[style*="justify-content:flex-end"] {
    position: sticky;
    bottom: 0;
    background: var(--muji-bg);
    padding: 6px 6px calc(8px + var(--safe-bottom)) !important;
    margin-top: 4px;
    z-index: 20;
  }

  /* ── table 转卡片式 ── */
  .table thead { display: none; }
  .table, .table tbody, .table tr, .table td { display: block; width: 100%; }
  .table tr { margin-bottom: 8px; border: 1px solid var(--border, #E8E4DB); border-radius: 8px; padding: 8px; }
  .table td { border: none; padding: 4px 0; }

  /* ── 工坊编辑器表单单列 ── */
  .field-row { flex-direction: column; gap: 0; }
}

/* ═══════════════════════════════════════════
   超窄屏回退 (<360px)
   ═══════════════════════════════════════════ */
@media (max-width: 359px) {
  .project-grid { grid-template-columns: 1fr; }
  .card-carousel { padding-bottom: 100%; }
  .ff-image-picker__grid { grid-template-columns: repeat(2, 1fr); }
}

/* ═══════════════════════════════════════════
   桌面端隐藏移动专用元素
   ═══════════════════════════════════════════ */
@media (min-width: 768px) {
  .mobile-menu-toggle,
  .mobile-menu-panel,
  .scroll-top-btn,
  .ff-control-panel,
  .ff-empty-hint { display: none !important; }
}

/* ── 我的页 ═══════════════════════════════ */
.me-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}
.me-item {
  background: #FAFAF5;
  border-radius: 12px;
  padding: 16px 8px;
  text-align: center;
  cursor: pointer;
  transition: background 0.15s;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}
.me-item:active { background: #F0EDE0; }
.me-item__icon { font-size: 28px; margin-bottom: 6px; }
.me-item__label { font-size: 13px; color: #3D3929; }

/* ── 用户头像下拉菜单 ── */
.user-dropdown {
  position: fixed;
  top: calc(var(--header-h, 56px) + 4px);
  right: 12px;
  min-width: 200px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 24px rgba(0,0,0,0.12);
  z-index: 3000;
  overflow: hidden;
}
.user-dropdown-link {
  display: block;
  width: 100%;
  padding: 14px 16px;
  border: none;
  background: none;
  text-align: left;
  font-size: 15px;
  color: #3D3929;
  cursor: pointer;
  transition: background 0.1s;
  -webkit-tap-highlight-color: transparent;
}
.user-dropdown-link:active { background: #F0EDE0; }
.user-dropdown-link.danger { color: #D4585E; }
.user-dropdown-divider {
  height: 1px;
  background: #E8E8E8;
  margin: 4px 0;
}
.user-dropdown-overlay {
  position: fixed;
  inset: 0;
  z-index: 2999;
}
