/* ============================================
   Responsive Styles v2.1 — Mobile-First Design
   ============================================ */

/* Base mobile styles are in style.css — these are enhancements and overrides */

/* ============================================
   Extra Small Devices (< 375px)
   ============================================ */
@media screen and (max-width: 374px) {
    :root {
        --text-2xl: 1.25rem;
        --text-3xl: 1.5rem;
    }

    .banner-section,
    .carousel-slides-wrapper.carousel-has-images {
        min-height: 160px;
    }

    .banner-content h1 {
        font-size: var(--text-xl);
    }

    .stats-grid {
        grid-template-columns: repeat(3, 1fr);
        gap: var(--space-2);
    }

    .stat-value {
        font-size: var(--text-xl);
    }

    .input-group {
        flex-direction: column;
    }

    .activation-btn {
        width: 100%;
        justify-content: center;
    }

    .fault-card-simple {
        flex-direction: column;
        align-items: flex-start;
        gap: var(--space-3);
    }

    .btn-detail-view {
        width: 100%;
        justify-content: center;
    }

    .fault-code-main {
        font-size: var(--text-2xl);
    }

    .message-bubble {
        max-width: 90%;
    }

    .message-avatar {
        width: 32px;
        height: 32px;
        margin: 0 var(--space-1);
    }

    /* 关键修复：超小屏下搜索面板三级联动换行显示 */
    .select-row {
        flex-wrap: wrap;
        gap: var(--space-2);
    }

    .select-connector {
        display: none;
    }

    .select-group {
        flex: 1 1 calc(50% - var(--space-2));
        min-width: calc(50% - var(--space-2));
    }

    .select-group:last-child {
        flex: 1 1 100%;
        min-width: 100%;
    }

    .keyword-row {
        flex-direction: column;
    }

    .search-btn {
        width: 100%;
        height: 44px;
    }

    /* 模态框适配 */
    .modal-content {
        max-width: 100%;
        border-radius: var(--radius-2xl) var(--radius-2xl) 0 0;
    }

    .uc-quick-grid {
        gap: var(--space-2);
    }

    .uc-quick-value {
        font-size: var(--text-base);
    }

    .uc-quick-label {
        font-size: 10px;
    }

    /* 快捷提问在小屏下一列 */
    .quick-prompts {
        grid-template-columns: 1fr;
    }

    /* 聊天气泡更宽以利用空间 */
    .chat-msg-bubble {
        max-width: 88%;
    }
}

/* ============================================
   Small Mobile Devices (375px - 414px)
   ============================================ */
@media screen and (min-width: 375px) and (max-width: 414px) {
    .user-center {
        padding: var(--space-5);
    }

    /* 确保 select-row 不溢出 */
    .select-row {
        gap: var(--space-1);
    }

    .custom-select {
        height: 38px;
        padding: 0 28px 0 10px;
        font-size: var(--text-xs);
    }
}

/* ============================================
   Large Mobile / Small Tablet (415px - 767px)
   ============================================ */
@media screen and (min-width: 415px) and (max-width: 767px) {
    .banner-section,
    .carousel-slides-wrapper.carousel-has-images {
        min-height: 220px;
    }

    .banner-content h1 {
        font-size: var(--text-2xl);
    }

    .message-bubble {
        max-width: 70%;
    }

    .message-avatar {
        width: 30px;
        height: 30px;
    }
}

/* ============================================
   Tablet (768px - 1024px)
   ============================================ */
@media screen and (min-width: 768px) and (max-width: 1024px) {
    .banner-section,
    .carousel-slides-wrapper.carousel-has-images {
        min-height: 240px;
    }

    .filter-form {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: var(--space-4);
    }

    .filter-form .form-group:last-of-type {
        grid-column: 1 / -1;
    }

    .search-btn {
        max-width: 280px;
        margin: var(--space-2) auto 0;
    }

    .stats-grid {
        gap: var(--space-6);
    }

    .message-bubble {
        max-width: 60%;
    }
}

/* ============================================
   Desktop (1025px+) — Full-width Desktop Layout
   详细桌面端样式见 css/desktop.css
   ============================================ */
@media screen and (min-width: 1025px) {
    /* 桌面端使用全宽布局，不再模拟手机容器 */
    /* 所有桌面端样式已迁移到 css/desktop.css */
}

/* ============================================
   Landscape Mode (max-height: 500px)
   ============================================ */
@media screen and (max-height: 500px) and (orientation: landscape) {
    :root {
        --header-height: 44px;
        --tab-nav-height: 50px;
    }

    .banner-section,
    .carousel-slides-wrapper.carousel-has-images {
        min-height: 120px;
    }

    .avatar-container {
        width: 70px;
        height: 70px;
    }

    .chat-app-layout {
        height: calc(100vh - var(--header-height) - var(--tab-nav-height));
    }

    .profile-card {
        padding: var(--space-4);
    }

    .bottom-tab-nav {
        height: var(--tab-nav-height);
    }

    .tab-icon {
        font-size: 18px;
    }

    .tab-label {
        font-size: 10px;
    }
}

/* ============================================
   Safe Area (iPhone X+ Notch)
   ============================================ */
@supports (padding: env(safe-area-inset-bottom)) {
    .bottom-tab-nav {
        padding-bottom: env(safe-area-inset-bottom);
        height: var(--tab-nav-height);
        box-sizing: content-box;
    }

    body {
        padding-bottom: calc(var(--tab-nav-height) + env(safe-area-inset-bottom));
    }

    .chat-app-layout .chat-input-area {
        /* safe area handled by chat-body flex */
    }

    .chat-messages {
        padding-bottom: calc(var(--space-6) + env(safe-area-inset-bottom));
    }
}

/* ============================================
   Touch Optimization
   ============================================ */
@media (hover: none) and (pointer: coarse) {
    /* Ensure minimum 44px touch targets */
    .tab-item {
        min-height: 44px;
    }

    .menu-item {
        min-height: 48px;
    }

    .form-select,
    .form-input {
        min-height: 48px;
    }

    /* Disable hover effects that cause sticky states on touch */
    .fault-card:hover {
        transform: none;
        box-shadow: var(--shadow-sm);
    }

    .fault-card:hover::after {
        opacity: 0;
    }

    .fault-card-simple:hover {
        transform: none;
        border-color: var(--color-border);
    }

    .btn-detail-view:hover:not(:disabled) {
        transform: none;
        box-shadow: var(--shadow-primary);
    }

    .avatar-container:hover {
        transform: none;
    }

    .avatar-container:hover .avatar-overlay {
        opacity: 1; /* Keep this for touch usability */
    }

    /* 修复触屏设备上 :hover 粘滞问题 */
    .search-btn:hover:not(:disabled) {
        transform: none;
    }

    .mode-btn:hover {
        background: transparent;
        color: var(--color-text-tertiary);
    }

    .mode-btn.active:hover {
        background: var(--color-bg-elevated);
        color: var(--color-primary);
    }

    .load-more-btn:hover {
        border-color: var(--color-border);
        background: var(--color-bg-elevated);
    }

    /* 轮播图触屏优化 */
    .carousel-slides-wrapper {
        /* 防止触屏滑动时触发页面横向滚动 */
        touch-action: pan-y;
        -webkit-user-select: none;
        user-select: none;
    }

    /* 公告栏触屏优化 */
    .announcement-scroll {
        -webkit-user-select: none;
        user-select: none;
    }

    .announcement-item:hover {
        /* 触屏设备不显示hover变色，防止粘滞 */
        color: var(--color-text-secondary);
    }

    /* Hero装饰球在触屏设备减少动画，防止性能问题 */
    .hero-orb-1 {
        animation-duration: 12s;
    }
    .hero-orb-2 {
        animation-duration: 10s;
    }
}

/* ============================================
   Retina / High DPI Display
   ============================================ */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    body {
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }
}

/* ============================================
   Real Mobile Device Fixes
   浏览器模拟器 vs 真实手机的关键差异修复
   ============================================ */

/* 修复：移动端地址栏收缩/展开导致的视口高度变化
   模拟器使用固定 viewport 高度，真实手机 viewport 是动态的
   100dvh (dynamic viewport height) 更符合真实手机行为 */
@supports (height: 100dvh) {
    .chat-app-layout {
        height: calc(100dvh - var(--header-height) - var(--tab-nav-height));
    }

    .login-page {
        min-height: 100dvh;
    }

    .page-wrapper {
        min-height: calc(100dvh - var(--tab-nav-height));
    }

    body {
        min-height: 100dvh;
    }
}

/* 修复：iOS Safari 弹性滚动(overscroll)导致背景露出
   真实手机有 rubber-band 效果，模拟器没有 */
@supports (-webkit-overflow-scrolling: touch) {
    html {
        overflow: hidden;
        position: fixed;
        width: 100%;
        height: 100%;
    }

    body {
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
        position: absolute;
        width: 100%;
        height: 100%;
    }
}

/* 修复：iOS Safari 自动缩放表单元素的问题
   font-size < 16px 的 input 在 iOS 上会触发自动缩放 */
@supports (-webkit-touch-callout: none) {
    .keyword-input,
    .chat-input,
    .custom-select,
    .login-input,
    .uc-form-input,
    .uc-activate-input {
        font-size: 16px;
    }
}

/* 修复：Android Chrome 底部工具栏遮挡问题 */
@media screen and (max-width: 767px) {
    .site-header {
        /* 确保固定定位的 header 不被移动端工具栏影响 */
        position: fixed;
        safe-area-inset-top: env(safe-area-inset-top, 0px);
    }

    .hero-section {
        /* hero 的 margin-top 需要匹配实际 header 高度 */
        margin-top: var(--header-height);
    }

    /* 确保固定底部元素不被键盘推出视口 */
    .bottom-tab-nav {
        transition: transform 0.2s ease;
    }

    .chat-input-area {
        transition: bottom 0.2s ease;
    }

    /* 修复：picker-sheet 在移动端的定位问题 */
    .picker-sheet {
        width: min(calc(100vw - 32px), 420px);
        max-height: 70dvh;
    }

    .picker-body {
        max-height: calc(70dvh - 56px);
    }
}

/* ============================================
   Print Styles
   ============================================ */
@media print {
    .site-header,
    .bottom-tab-nav,
    .chat-input-area,
    .send-btn,
    .search-btn,
    .filter-section,
    .tab-switch-section,
    .modal,
    .toast {
        display: none !important;
    }

    body {
        background: white;
        color: black;
        padding-bottom: 0;
    }

    .page-wrapper {
        max-width: none;
        box-shadow: none;
    }

    .fault-card {
        box-shadow: none;
        border: 1px solid #ccc;
        break-inside: avoid;
    }
}

/* ============================================
   Reduced Motion Preferences
   ============================================ */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }

    .banner-section::before,
    .banner-section::after,
    .carousel-slide::before {
        animation: none !important;
    }

    .fault-card:hover {
        transform: none !important;
    }
}

/* ============================================
   High Contrast Mode
   ============================================ */
@media (prefers-contrast: high) {
    :root {
        --color-border: #94a3b8;
        --color-border-light: #cbd5e1;
        --color-text-secondary: #334155;
        --color-text-tertiary: #475569;
    }

    .fault-card {
        border: 2px solid var(--color-border);
    }

    .form-select,
    .form-input {
        border-width: 2px;
    }

    .bottom-tab-nav {
        border-top: 2px solid var(--color-border);
    }

    .site-header {
        border-bottom: 2px solid var(--color-border);
    }

    .btn-detail-view {
        border: 2px solid transparent;
    }

    .badge {
        border: 2px solid currentColor;
    }
}

/* ============================================
   Dark Mode Support (follows system preference)
   ============================================ */
@media (prefers-color-scheme: dark) {
    :root {
        --color-bg: #0f172a;
        --color-bg-elevated: #1e293b;
        --color-bg-muted: #334155;
        --color-bg-overlay: rgba(0, 0, 0, 0.7);

        --color-text-primary: #f1f5f9;
        --color-text-secondary: #94a3b8;
        --color-text-tertiary: #64748b;
        --color-text-inverse: #0f172a;

        --color-border: #334155;
        --color-border-light: #1e293b;

        --shadow-xs: 0 1px 2px rgba(0, 0, 0, 0.2);
        --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px 2px rgba(0, 0, 0, 0.2);
        --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.4), 0 2px 4px -2px rgba(0, 0, 0, 0.3);
        --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.5), 0 4px 6px -4px rgba(0, 0, 0, 0.3);
        --shadow-xl: 0 20px 25px -5px rgba(0, 0, 0, 0.5), 0 8px 10px -6px rgba(0, 0, 0, 0.4);
    }

    .site-header {
        background: rgba(30, 41, 59, 0.82);
        border-bottom-color: rgba(51, 65, 85, 0.6);
    }

    .bottom-tab-nav {
        background: rgba(30, 41, 59, 0.85);
        border-top-color: rgba(51, 65, 85, 0.5);
    }

    .site-title {
        background: linear-gradient(135deg, #818cf8 0%, #a78bfa 50%, #c084fc 100%);
        -webkit-background-clip: text;
        background-clip: text;
    }

    .message-item.user .message-bubble {
        background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 50%, #a855f7 100%);
    }

    .message-item.ai .message-bubble {
        background: var(--color-bg-elevated);
        color: var(--color-text-primary);
        border-color: var(--color-border);
    }

    .form-select,
    .form-input,
    .chat-input,
    .activation-input {
        background: var(--color-bg-muted);
        border-color: var(--color-border);
        color: var(--color-text-primary);
    }

    .form-select:focus,
    .form-input:focus,
    .chat-input:focus,
    .activation-input:focus {
        border-color: var(--color-primary-light);
        box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2);
        background: var(--color-bg-elevated);
    }

    .form-input,
    .profile-form .form-input {
        background: var(--color-bg-muted);
    }

    .history-item {
        background: var(--color-bg-muted);
    }

    .stat-item {
        background: var(--color-bg-muted);
    }

    .fault-code {
        background: rgba(99, 102, 241, 0.15);
    }

    .category-tag {
        background: var(--color-bg-muted);
    }

    .modal-content {
        background: var(--color-bg-elevated);
    }

    .modal-backdrop {
        background: rgba(0, 0, 0, 0.7);
    }

    .search-btn:disabled {
        background: var(--color-bg-muted);
        color: var(--color-text-tertiary);
    }

    .load-more-btn {
        background: var(--color-bg-elevated);
        border-color: var(--color-primary-light);
        color: var(--color-primary-light);
    }

    .load-more-btn:hover {
        background: var(--color-primary);
        color: var(--color-text-inverse);
    }

    .level-tag.level-critical {
        background: rgba(239, 68, 68, 0.15);
        border-color: rgba(239, 68, 68, 0.3);
    }

    .level-tag.level-warning {
        background: rgba(245, 158, 11, 0.15);
        border-color: rgba(245, 158, 11, 0.3);
    }

    .level-tag.level-info {
        background: rgba(59, 130, 246, 0.15);
        border-color: rgba(59, 130, 246, 0.3);
    }

    .level-badge.level-critical {
        background: linear-gradient(135deg, rgba(239, 68, 68, 0.15) 0%, rgba(239, 68, 68, 0.05) 100%);
        border-color: rgba(239, 68, 68, 0.3);
    }

    .level-badge.level-warning {
        background: linear-gradient(135deg, rgba(245, 158, 11, 0.15) 0%, rgba(245, 158, 11, 0.05) 100%);
        border-color: rgba(245, 158, 11, 0.3);
    }

    .level-badge.level-info {
        background: linear-gradient(135deg, rgba(59, 130, 246, 0.15) 0%, rgba(59, 130, 246, 0.05) 100%);
        border-color: rgba(59, 130, 246, 0.3);
    }

    .solution-text {
        background: linear-gradient(135deg, rgba(34, 197, 94, 0.1) 0%, var(--color-bg-elevated) 100%);
    }

    .detail-notice {
        background: linear-gradient(135deg, rgba(59, 130, 246, 0.1) 0%, rgba(59, 130, 246, 0.05) 100%);
    }

    .permission-notice {
        background: rgba(245, 158, 11, 0.1);
        border-color: rgba(245, 158, 11, 0.2);
    }

    .detail-meta {
        background: var(--color-bg-muted);
    }

    .detail-level {
        background: linear-gradient(135deg, rgba(239, 68, 68, 0.1) 0%, var(--color-bg-elevated) 100%);
    }

    .toast {
        background: rgba(30, 41, 59, 0.9);
    }

    @media screen and (min-width: 1025px) {
        body {
            background: #020617;
        }

        .page-wrapper {
            background: var(--color-bg);
        }
    }
}
