/**
 * 移动端响应式样式
 * 
 * 针对小屏幕设备（手机、平板）优化布局和交互体验。
 */

/* ==================== 基础响应式断点 ==================== */
/* 
 * 断点定义：
 * - mobile: < 768px (手机)
 * - tablet: 768px - 1024px (平板)
 * - desktop: > 1024px (桌面)
 */

/* ==================== 移动端基础优化 ==================== */

@media (max-width: 768px) {
  /* 基础字体和间距调整 */
  :root {
    font-size: 14px;
  }

  body {
    padding: 0;
  }

  /* ==================== 头部导航优化 ==================== */
  header {
    padding: 1rem 0.75rem;
  }

  .header-content {
    flex-direction: column;
    gap: 1rem;
  }

  .brand-wrapper {
    flex-direction: row;
    align-items: center;
    gap: 0.75rem;
    width: 100%;
  }

  .header-icon {
    width: 32px;
    height: 32px;
  }

  .brand-text h1 {
    font-size: 1.1rem;
    margin: 0;
  }

  .brand-text p {
    font-size: 0.85rem;
    margin: 0.25rem 0 0 0;
  }

  /* 隐藏二维码（移动端不需要） */
  #qrcode-container {
    display: none;
  }

  /* 导航栏优化 */
  nav {
    width: 100%;
    flex-wrap: wrap;
    gap: 0.5rem;
    justify-content: flex-start;
  }

  .nav-link {
    font-size: 0.85rem;
    padding: 0.5rem 0.75rem;
    flex: 1 1 auto;
    min-width: calc(50% - 0.25rem);
    text-align: center;
  }

  /* 确保AI按钮也遵循相同的flex规则，不会独占一行 */
  .ai-diagnose-btn {
    flex: 1 1 auto !important;
    min-width: calc(50% - 0.25rem) !important;
  }

  /* 移动端：隐藏"更多"菜单，所有按钮直接显示 */
  .nav-more-menu {
    display: none !important;
  }

  .nav-link-primary,
  .nav-link-secondary {
    display: inline-flex !important;
  }

  /* 用户菜单优化 */
  .user-menu {
    width: 100%;
    justify-content: center;
  }

  .user-info-trigger {
    font-size: 0.85rem;
    padding: 0.5rem 0.75rem;
  }

  .user-menu-dropdown {
    left: 0;
    right: 0;
    width: 100%;
    max-width: none;
  }

  /* ==================== 主内容区域优化 ==================== */
  main {
    padding: 1rem 0.75rem;
    max-width: 100%;
  }

  /* 卡片间距调整 */
  .card {
    margin-bottom: 1rem;
    padding: 1rem;
  }

  .section-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.75rem;
  }

  .section-header h2 {
    font-size: 1.2rem;
  }

  /* ==================== 网格布局优化 ==================== */
  /* 首页顶部三栏布局改为单列 */
  main > div:first-child {
    grid-template-columns: 1fr !important;
    gap: 1rem;
  }

  /* 财务续航卡片宽度恢复 */
  section.card[style*='max-width: 320px'] {
    max-width: 100% !important;
  }

  /* ==================== 按钮优化 ==================== */
  .btn-submit,
  .btn-secondary,
  .btn-link,
  button {
    min-height: 44px; /* 触摸友好尺寸 */
    padding: 0.75rem 1rem;
    font-size: 0.9rem;
  }

  /* 按钮组优化 */
  .section-header > div {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    width: 100%;
  }

  .section-header .btn-link,
  .section-header button {
    flex: 1 1 auto;
    min-width: calc(50% - 0.25rem);
  }

  /* ==================== 表单优化 ==================== */
  .field {
    margin-bottom: 1rem;
  }

  .field label {
    font-size: 0.9rem;
    margin-bottom: 0.5rem;
  }

  input[type='text'],
  input[type='number'],
  input[type='date'],
  input[type='email'],
  input[type='password'],
  select,
  textarea {
    font-size: 16px; /* 防止iOS自动缩放 */
    padding: 0.75rem;
    min-height: 44px;
    width: 100%;
  }

  textarea {
    min-height: 100px;
  }

  /* 表单帮助文本 */
  .field-help,
  .field-hint {
    font-size: 0.8rem;
    margin-top: 0.25rem;
  }

  /* ==================== 资产列表优化 ==================== */
  .asset-list {
    gap: 0.75rem;
  }

  .asset-group-header {
    padding: 0.75rem;
    font-size: 0.95rem;
  }

  .asset-row {
    padding: 0.75rem;
    flex-direction: column;
    gap: 0.5rem;
  }

  .asset-info {
    width: 100%;
  }

  .asset-actions {
    width: 100%;
    justify-content: space-between;
    align-items: center;
  }

  .asset-value {
    font-size: 1rem;
  }

  .asset-edit-btn {
    min-width: 44px;
    min-height: 44px;
    padding: 0.5rem;
  }

  /* ==================== 金字塔图表优化 ==================== */
  .pyramid-container {
    padding: 1rem 0.5rem;
  }

  .pyramid-row {
    flex-direction: column;
    gap: 0.5rem;
    padding: 0.75rem;
  }

  .pyramid-tier {
    width: 100% !important;
    min-width: 100% !important;
    max-width: 100% !important;
    padding: 0.75rem;
  }

  .pyramid-row-label {
    width: 100%;
    text-align: left;
    margin-bottom: 0.5rem;
  }

  .pyramid-row-percent {
    width: auto !important;
    margin-left: auto;
  }

  /* ==================== 图表优化 ==================== */
  .chart-container {
    padding: 0.5rem;
  }

  #networth-chart-container {
    position: relative;
    height: 250px !important;
    max-height: 50vh;
  }

  .chart-controls {
    flex-direction: column;
    gap: 0.5rem;
    width: 100%;
  }

  .chart-controls label {
    width: 100%;
    padding: 0.5rem;
    background: var(--alipay-bg);
    border-radius: 4px;
  }

  /* ==================== 开销汇总优化 ==================== */
  .expense-summary-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.25rem;
  }

  /* ==================== 财务续航优化 ==================== */
  .runway-info {
    grid-template-columns: 1fr !important;
    gap: 1rem;
  }

  .info-card {
    padding: 1rem;
  }

  .info-card .value {
    font-size: 1.5rem;
  }

  /* 电池图标优化 */
  .battery-container {
    padding: 1rem 0.5rem;
  }

  .battery {
    width: 100%;
    max-width: 300px;
  }

  /* ==================== 模态框优化 ==================== */
  .modal-overlay {
    padding: 0.5rem;
    align-items: flex-start;
    overflow-y: auto;
  }

  .modal-content {
    width: 100% !important;
    max-width: 100% !important;
    max-height: 90vh;
    margin: auto;
    border-radius: 8px 8px 0 0;
    overflow-y: auto;
  }

  .modal-header {
    padding: 1rem;
    position: sticky;
    top: 0;
    background: white;
    z-index: 10;
    border-bottom: 1px solid var(--alipay-border);
  }

  .modal-header h3 {
    font-size: 1.1rem;
  }

  .modal-form {
    padding: 1rem;
  }

  .modal-actions {
    flex-direction: column-reverse;
    gap: 0.75rem;
    padding-top: 1rem;
    position: sticky;
    bottom: 0;
    background: white;
    border-top: 1px solid var(--alipay-border);
  }

  .modal-actions .btn-submit,
  .modal-actions .btn-cancel {
    width: 100%;
    margin: 0;
  }

  /* ==================== AI 诊断模态框优化 ==================== */
  #ai-modal .modal-content {
    max-height: 95vh;
  }

  .ai-controls {
    flex-direction: column;
    gap: 0.75rem;
  }

  .ai-controls select,
  .ai-controls button {
    width: 100%;
  }

  .ai-result-content {
    font-size: 0.9rem;
    line-height: 1.6;
  }

  /* ==================== 确认对话框优化 ==================== */
  .confirm-dialog {
    width: 95% !important;
    max-width: 95% !important;
    margin: 1rem auto;
  }

  .confirm-actions {
    flex-direction: column;
    gap: 0.75rem;
  }

  .confirm-ok,
  .confirm-cancel {
    width: 100%;
  }

  /* ==================== 撤销提示条优化 ==================== */
  .undo-bar {
    left: 0.5rem;
    right: 0.5rem;
    width: auto;
    max-width: none;
    transform: none;
    bottom: 1rem;
  }

  /* ==================== 空状态优化 ==================== */
  .empty-state {
    padding: 2rem 1rem;
  }

  .empty-state-icon {
    font-size: 3rem;
  }

  .empty-state-title {
    font-size: 1.1rem;
  }

  .empty-state-message {
    font-size: 0.9rem;
  }

  /* ==================== 骨架屏优化 ==================== */
  .skeleton-card,
  .skeleton-item {
    padding: 1rem;
  }

  /* ==================== 通知优化 ==================== */
  .toast {
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%) scale(0.9);
    width: auto;
    max-width: 85%;
    font-size: 0.9rem;
    padding: 0.875rem 1.25rem;
  }

  .toast.show {
    transform: translate(-50%, -50%) scale(1);
  }

  /* ==================== 本地模式警告优化 ==================== */
  #local-mode-warning {
    flex-direction: column;
    gap: 0.5rem;
    padding: 0.75rem;
  }

  #local-mode-warning p {
    font-size: 0.85rem;
    line-height: 1.5;
  }

  #dismiss-warning {
    align-self: flex-end;
  }

  /* ==================== 隐私声明优化 ==================== */
  .privacy-statement {
    padding: 1rem 0.75rem;
  }

  .privacy-toggle {
    font-size: 0.9rem;
    padding: 0.75rem;
  }

  .privacy-content {
    font-size: 0.85rem;
    line-height: 1.6;
  }

  /* ==================== 表格优化（如果有） ==================== */
  table {
    font-size: 0.85rem;
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  thead {
    display: none; /* 移动端隐藏表头，使用数据属性显示 */
  }

  tbody {
    display: block;
  }

  tr {
    display: block;
    margin-bottom: 1rem;
    border: 1px solid var(--alipay-border);
    border-radius: 8px;
    padding: 0.75rem;
  }

  td {
    display: flex;
    justify-content: space-between;
    padding: 0.5rem 0;
    border: none;
  }

  td::before {
    content: attr(data-label);
    font-weight: 600;
    color: var(--alipay-text-secondary);
    margin-right: 1rem;
  }
}

/* ==================== 平板端优化 (768px - 1024px) ==================== */

@media (min-width: 768px) and (max-width: 1024px) {
  /* 首页顶部三栏布局改为两列 */
  main > div:first-child {
    grid-template-columns: 1.5fr 1fr !important;
  }

  /* 导航栏优化 */
  nav {
    flex-wrap: wrap;
  }

  .nav-link {
    min-width: calc(33.333% - 0.5rem);
  }

  /* 确保AI按钮也遵循相同的flex规则 */
  .ai-diagnose-btn {
    min-width: calc(33.333% - 0.5rem) !important;
  }

  /* 平板端：显示"更多"菜单，隐藏次要按钮 */
  .nav-more-menu {
    display: inline-block !important;
  }

  .nav-link-primary {
    display: inline-flex !important;
  }

  .nav-link-secondary {
    display: none !important;
  }

  /* 金字塔图表优化 */
  .pyramid-row {
    gap: 0.75rem;
  }

  .pyramid-tier {
    min-width: 180px;
  }
}

/* ==================== 横屏模式优化 ==================== */

@media (max-width: 768px) and (orientation: landscape) {
  /* 横屏时减少垂直空间占用 */
  header {
    padding: 0.75rem;
  }

  .brand-text h1 {
    font-size: 1rem;
  }

  .brand-text p {
    display: none; /* 隐藏副标题节省空间 */
  }

  nav {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .nav-link {
    flex: 0 0 auto;
    white-space: nowrap;
  }

  #networth-chart-container {
    height: 200px !important;
  }
}

/* ==================== 触摸设备优化 ==================== */

@media (hover: none) and (pointer: coarse) {
  /* 触摸设备专用优化 */

  /* 增大可点击区域 */
  button,
  .btn-submit,
  .btn-secondary,
  .btn-link,
  a {
    min-height: 44px;
    min-width: 44px;
  }

  /* 移除悬停效果，使用点击反馈 */
  button:active,
  .btn-submit:active,
  .btn-secondary:active {
    transform: scale(0.98);
    opacity: 0.8;
  }

  /* 优化下拉菜单 */
  select {
    padding: 0.75rem 2.5rem 0.75rem 0.75rem;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23333' d='M6 9L1 4h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 0.75rem center;
    background-size: 12px;
    -webkit-appearance: none;
    appearance: none;
  }

  /* 优化输入框焦点 */
  input:focus,
  textarea:focus,
  select:focus {
    outline: 2px solid var(--alipay-blue);
    outline-offset: 2px;
  }
}

/* ==================== 高DPI屏幕优化 ==================== */

@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
  /* 高DPI屏幕字体和图标优化 */
  .header-icon {
    image-rendering: -webkit-optimize-contrast;
  }
}

/* ==================== 打印样式 ==================== */

@media print {
  header,
  nav,
  .btn-submit,
  .btn-secondary,
  .btn-link,
  button,
  #local-mode-warning,
  .privacy-statement {
    display: none !important;
  }

  .card {
    page-break-inside: avoid;
    box-shadow: none;
    border: 1px solid #ddd;
  }

  body {
    background: white;
  }
}
