/**
 * Chatbot Mobile UI Optimization
 * Responsive styles for touch interfaces, mobile keyboards, and various screen sizes
 */

/* ========== CSS Variables for Mobile ========== */
:root {
    --mobile-touch-target: 44px;
    --mobile-padding: 12px;
    --safe-area-inset-bottom: env(safe-area-inset-bottom, 0px);
    --safe-area-inset-top: env(safe-area-inset-top, 0px);
    --safe-area-inset-left: env(safe-area-inset-left, 0px);
    --safe-area-inset-right: env(safe-area-inset-right, 0px);
}

/* ========== iOS Safari Dynamic Viewport Height ========== */
@supports (height: 100dvh) {
    body.chatbot-page {
        height: 100dvh;
    }

    .app-container {
        height: 100dvh;
    }
}

/* ========== Base Mobile Reset (768px and below) ========== */
@media (max-width: 768px) {
    /* Remove min-width restrictions for mobile */
    body.chatbot-page {
        min-width: unset;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
        /* Prevent pull-to-refresh */
        overscroll-behavior-y: contain;
    }

    .app-container {
        min-width: unset;
        min-height: unset;
        height: 100vh;
        height: 100dvh;
    }

    /* Touch action optimization */
    .chat-messages,
    .chat-list,
    .section-content {
        touch-action: pan-y;
        -webkit-overflow-scrolling: touch;
    }

    /* Larger scrollbar for touch */
    ::-webkit-scrollbar {
        width: 4px;
    }
}

/* ========== Small Mobile (480px and below) ========== */
@media (max-width: 480px) {
    :root {
        --mobile-padding: 6px;
        --mobile-touch-target: 38px;
    }

    body.chatbot-page {
        font-size: 14px;
    }

    /* Tighter layout for small screens */
    .chat-messages {
        padding: 10px 6px;
    }

    .message {
        max-width: 92%;
        margin-bottom: 4px;
    }

    .message .bubble {
        padding: 8px 10px;
        font-size: 0.82rem;
        line-height: 1.35;
        border-radius: 14px;
    }

    /* Code blocks more compact */
    .message.ai .bubble pre {
        padding: 8px;
        font-size: 0.7rem;
        border-radius: 8px;
        margin: 0.4em 0;
    }

    .message.ai .bubble code {
        font-size: 0.75em;
        padding: 1px 3px;
    }

    /* Tables more compact */
    .message.ai .bubble table {
        font-size: 0.7rem;
    }

    .message.ai .bubble th,
    .message.ai .bubble td {
        padding: 3px 5px;
    }

    /* Welcome screen compact */
    .welcome-screen h2 {
        font-size: 1.2rem;
        margin-bottom: 12px;
    }

    .welcome-textarea {
        font-size: 15px;
        padding: 10px 12px;
    }

    .welcome-action-btn {
        padding: 8px 12px;
        font-size: 0.8rem;
    }
}

/* ========== Touch Target Size (Minimum 44px) ========== */
@media (max-width: 768px) {
    /* All interactive elements should be at least 44px */
    .sidebar-header .actions button,
    .chat-header .actions button,
    .project-header .actions button,
    .icon-sidebar-footer button,
    .icon-new-chat,
    .icon-chat-item,
    .new-item-btn,
    .btn-secondary,
    .btn-danger,
    .attach-btn,
    .task-btn,
    .analysis-btn,
    .stop-btn,
    .notification-clear-btn,
    .notification-close-btn,
    .scroll-to-bottom-btn,
    .back-btn {
        min-width: var(--mobile-touch-target);
        min-height: var(--mobile-touch-target);
    }

    /* Send button */
    .chat-input-row > button {
        width: var(--mobile-touch-target);
        height: var(--mobile-touch-target);
    }

    /* Provider select dropdown */
    .provider-select {
        height: var(--mobile-touch-target);
        min-height: var(--mobile-touch-target);
    }

    /* Sidebar items */
    .sidebar-item,
    .chat-item {
        min-height: 56px;
        padding: 12px 16px;
    }

    /* Section headers */
    .section-header {
        min-height: 48px;
        padding: 12px 16px;
    }

    /* Delete buttons - larger tap target */
    .chat-item .delete-btn,
    .sidebar-item .delete-btn {
        min-width: 40px;
        min-height: 40px;
        display: flex;
        align-items: center;
        justify-content: center;
        /* Always visible on mobile for accessibility */
        opacity: 0.7;
    }

    /* Message copy button */
    .message.ai .copy-btn {
        width: 36px;
        height: 36px;
        right: -40px;
        /* Always visible on mobile */
        opacity: 0.7;
    }

    /* Tab buttons */
    .chat-tab {
        min-height: var(--mobile-touch-target);
        padding: 10px 12px;
    }
}

/* ========== Mobile Header Simplification ========== */
@media (max-width: 768px) {
    /* Compact header */
    .chat-header,
    .project-header,
    .unified-header {
        padding: 8px 12px;
        min-height: 52px;
        gap: 8px;
    }

    .chat-header .avatar,
    .project-header .avatar,
    .unified-header .avatar {
        width: 32px;
        height: 32px;
        font-size: 16px;
    }

    .chat-header .info .name,
    .project-header .info .name,
    .unified-header .info .name {
        font-size: 0.9rem;
        max-width: 120px;
    }

    .chat-header .info .status {
        font-size: 0.7rem;
    }

    /* Hide less important header elements on mobile */
    .turn-count-badge,
    .metadata-bar {
        display: none;
    }

    .working-dir-badge {
        max-width: 140px;
        font-size: 0.65rem;
        padding: 2px 6px;
    }

    /* Compact mode selector */
    .chat-mode-selector {
        margin-left: 4px;
    }

    .chat-mode-selector select {
        padding: 4px 6px;
        font-size: 0.7rem;
    }

    /* Header actions - compact layout */
    .unified-header .actions {
        gap: 2px;
    }

    .unified-header .actions > button {
        padding: 6px;
        font-size: 1rem;
        min-width: 36px;
        min-height: 36px;
    }

    /* Unified controls compact */
    .unified-controls {
        gap: 4px;
    }

    .settings-toggle-btn,
    .prompt-toggle-btn {
        padding: 6px 8px;
        font-size: 0.9rem;
    }
}

@media (max-width: 480px) {
    /* Extra compact header for small screens */
    .chat-header,
    .project-header,
    .unified-header {
        gap: 6px;
        padding: 6px 10px;
        min-height: 48px;
    }

    .chat-header .avatar,
    .project-header .avatar,
    .unified-header .avatar {
        width: 28px;
        height: 28px;
        font-size: 14px;
    }

    .unified-header .info .name {
        font-size: 0.85rem;
        max-width: 100px;
    }

    /* Hide mode selector on very small screens */
    .chat-mode-selector {
        display: none;
    }

    /* Hide working dir on small screens */
    .working-dir-badge {
        display: none !important;
    }

    /* Smaller header buttons */
    .unified-header .actions > button {
        padding: 5px;
        font-size: 0.95rem;
        min-width: 32px;
        min-height: 32px;
    }

    /* Hide some action buttons on small screens */
    .unified-header .actions .webagent-btn,
    .unified-header .actions button[onclick*="openHealthCheck"],
    .unified-header .actions button[onclick*="showShareUrl"] {
        display: none;
    }

    /* Unified controls more compact */
    .unified-controls {
        gap: 2px;
    }

    .settings-toggle-btn,
    .prompt-toggle-btn {
        padding: 4px 6px;
    }
}

/* ========== Mobile Actions Menu (1024px and below) ========== */
/* 모바일 메뉴 토글 버튼 - 기본 숨김 */
.mobile-actions-toggle {
    display: none;
    background: none;
    border: none;
    color: var(--wa-text-secondary);
    font-size: 1.2rem;
    padding: 6px;
    cursor: pointer;
    min-width: 36px;
    min-height: 36px;
    align-items: center;
    justify-content: center;
}

.mobile-actions-toggle:hover {
    color: var(--wa-text);
    background: var(--wa-bg-hover);
    border-radius: 50%;
}

/* 모바일 액션 드롭다운 */
.mobile-actions-dropdown {
    position: absolute;
    top: 100%;
    right: 10px;
    background: var(--wa-bg-panel);
    border: 1px solid var(--wa-border);
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    min-width: 200px;
    max-width: 280px;
    z-index: 1000;
    overflow: hidden;
    margin-top: 8px;
}

.mobile-action-item {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    padding: 12px 16px;
    background: none;
    border: none;
    color: var(--wa-text);
    font-size: 0.95rem;
    cursor: pointer;
    text-align: left;
    transition: background 0.2s;
}

.mobile-action-item:hover {
    background: var(--wa-bg-hover);
}

.mobile-action-item i {
    font-size: 1.1rem;
    width: 20px;
    text-align: center;
    color: var(--wa-text-secondary);
}

.mobile-action-item span {
    flex: 1;
}

.mobile-action-item.delete-action {
    color: #dc3545;
    border-top: 1px solid var(--wa-border);
}

.mobile-action-item.delete-action i {
    color: #dc3545;
}

.attach-badge-mobile {
    background: var(--wa-green);
    color: white;
    font-size: 0.65rem;
    padding: 2px 6px;
    border-radius: 10px;
    font-weight: 600;
}

@media (max-width: 1024px) {
    /* 모바일에서 기존 액션 버튼들 숨김 */
    .unified-header .actions > button:not(.mobile-actions-toggle) {
        display: none;
    }

    /* 모바일 메뉴 버튼 표시 */
    .mobile-actions-toggle {
        display: flex;
    }
}

/* ========== Mobile Input Area Optimization ========== */
@media (max-width: 768px) {
    .chat-input {
        padding: 8px 12px;
        padding-bottom: calc(8px + var(--safe-area-inset-bottom));
        /* Sticky at bottom */
        position: sticky;
        bottom: 0;
        background: var(--wa-bg-panel);
        z-index: 10;
    }

    .chat-input-row {
        gap: 8px;
    }

    .chat-input textarea {
        padding: 10px 12px;
        font-size: 16px; /* Prevent iOS zoom on focus */
        min-height: var(--mobile-touch-target);
        border-radius: 22px;
    }

    /* Prevent iOS zoom */
    .chat-input textarea:focus {
        font-size: 16px;
    }

    /* Image preview compact */
    .image-preview-container {
        padding: 6px;
    }

    .image-preview-item {
        width: 60px;
        height: 60px;
    }
}

@media (max-width: 480px) {
    .chat-input {
        padding: 6px 8px;
        padding-bottom: calc(6px + var(--safe-area-inset-bottom));
    }

    .chat-input-row {
        gap: 6px;
    }

    .chat-input textarea {
        padding: 8px 12px;
    }

    /* Hide provider select on very small screens - use header dropdown instead */
    .provider-select {
        display: none;
    }
}

/* ========== Mobile Message Bubbles ========== */
@media (max-width: 768px) {
    .message {
        max-width: 85%;
        margin-bottom: 6px;
    }

    .message .bubble {
        padding: 10px 12px;
        font-size: 0.88rem;
    }

    .message .time {
        font-size: 0.6rem;
    }

    /* Code blocks in messages */
    .message.ai .bubble pre {
        padding: 10px;
        font-size: 0.75rem;
        margin: 0.5em 0;
    }

    .message.ai .bubble code {
        font-size: 0.8em;
        padding: 1px 4px;
    }

    /* Tables in messages */
    .message.ai .bubble table {
        font-size: 0.75rem;
    }

    .message.ai .bubble th,
    .message.ai .bubble td {
        padding: 4px 6px;
    }

    /* Message images */
    .message-image {
        max-width: 180px;
        max-height: 180px;
    }
}

@media (max-width: 480px) {
    .message {
        max-width: 92%;
    }

    .message .bubble {
        padding: 8px 10px;
        font-size: 0.85rem;
        line-height: 1.4;
    }

    /* Very compact code blocks */
    .message.ai .bubble pre {
        padding: 8px;
        font-size: 0.7rem;
    }

    .message-image {
        max-width: 150px;
        max-height: 150px;
    }
}

/* ========== Quick Prompts Mobile Layout ========== */
@media (max-width: 768px) {
    .quick-prompts-grid,
    .project-rooms-grid {
        grid-template-columns: 1fr 1fr;
        gap: 10px;
    }

    .quick-prompt-card,
    .project-room-card {
        padding: 12px;
        min-height: 100px;
    }
}

@media (max-width: 480px) {
    .quick-prompts-grid,
    .project-rooms-grid {
        grid-template-columns: 1fr;
        gap: 8px;
    }

    .quick-prompt-card,
    .project-room-card {
        padding: 10px;
        min-height: 80px;
    }

    .project-room-card-title {
        font-size: 0.9rem;
    }

    .project-room-card-preview {
        font-size: 0.8rem;
    }
}

/* ========== Mobile Sidebar / Panel ========== */
@media (max-width: 768px) {
    /* Full width sidebar overlay */
    .sidebar.show-mobile {
        left: 0;
        width: 100%;
        animation: slideInLeft 0.25s ease;
    }

    /* Icon sidebar */
    .icon-sidebar {
        width: 56px;
        padding: 8px 0;
        z-index: 100;
    }

    .icon-new-chat,
    .icon-chat-item {
        width: 40px;
        height: 40px;
    }

    /* Sidebar header compact */
    .sidebar-header {
        padding: 8px 12px;
        min-height: 52px;
    }

    .sidebar-header .logo {
        font-size: 1rem;
    }

    .sidebar-header .logo i {
        font-size: 1.3rem;
    }

    /* Search box */
    .search-box {
        padding: 8px;
    }

    .search-box input {
        padding: 10px 12px 10px 36px;
        font-size: 16px; /* Prevent iOS zoom */
    }
}

@media (max-width: 480px) {
    .icon-sidebar {
        width: 52px;
    }

    .icon-new-chat,
    .icon-chat-item {
        width: 36px;
        height: 36px;
        font-size: 0.9rem;
    }
}

/* ========== Mobile Landscape Mode ========== */
@media (max-width: 900px) and (orientation: landscape) {
    /* Reduce heights in landscape */
    .app-container {
        height: 100vh;
        height: 100dvh;
    }

    .chat-header,
    .project-header {
        min-height: 48px;
        padding: 6px 12px;
    }

    .chat-header .avatar,
    .project-header .avatar {
        width: 32px;
        height: 32px;
    }

    .sidebar-header {
        min-height: 48px;
        padding: 6px 12px;
    }

    /* Two-column layout for chat in landscape */
    .message {
        max-width: 70%;
    }

    /* Smaller input area */
    .chat-input {
        padding: 6px 12px;
    }

    .chat-input textarea {
        min-height: 38px;
        max-height: 100px;
    }

    /* Smaller messages */
    .message .bubble {
        padding: 8px 12px;
        font-size: 0.85rem;
    }

    /* Hide icon sidebar in landscape if chat is visible */
    .icon-sidebar {
        width: 50px;
    }

    .icon-new-chat,
    .icon-chat-item {
        width: 38px;
        height: 38px;
    }
}

/* ========== Mobile Notification Panel ========== */
@media (max-width: 768px) {
    .notification-panel {
        top: 60px;
        right: 8px;
        left: 8px;
        width: auto;
        max-height: 50vh;
    }

    .notification-item {
        padding: 8px;
    }
}

/* ========== Mobile Diff Panel ========== */
@media (max-width: 768px) {
    .diff-panel {
        max-height: 40vh;
    }

    .diff-header {
        padding: 8px 12px;
    }

    .diff-meta {
        padding: 6px 12px;
        flex-wrap: wrap;
        gap: 8px;
    }

    .diff-content {
        font-size: 0.7rem;
    }

    .permission-panel {
        max-height: 40vh;
    }

    .permission-header-bar {
        padding: 10px 12px;
    }

    .permission-item {
        padding: 10px;
    }
}

/* ========== Mobile Modal Styles ========== */
@media (max-width: 768px) {
    .rename-modal-content {
        width: 95%;
        padding: 20px 16px;
    }

    .rename-modal input {
        padding: 12px;
        font-size: 16px; /* Prevent iOS zoom */
    }

    .rename-modal-actions {
        flex-direction: column;
        gap: 8px;
    }

    .rename-modal-actions .btn-cancel,
    .rename-modal-actions .btn-confirm {
        width: 100%;
        padding: 12px;
    }

    /* Image lightbox */
    .image-lightbox .close-btn {
        top: 10px;
        right: 10px;
    }
}

/* ========== Mobile Toast Notifications ========== */
@media (max-width: 768px) {
    .toast-notification {
        top: auto;
        bottom: calc(80px + var(--safe-area-inset-bottom));
        right: 12px;
        left: 12px;
        max-width: none;
    }
}

/* ========== Mobile Swipe Indicator ========== */
.swipe-indicator {
    display: none;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 4px;
    height: 60px;
    background: var(--wa-green);
    border-radius: 2px;
    opacity: 0;
    transition: opacity 0.2s;
}

.swipe-indicator.left {
    left: 0;
}

.swipe-indicator.right {
    right: 0;
}

@media (max-width: 768px) {
    .swipe-indicator {
        display: block;
    }

    .swipe-indicator.active {
        opacity: 0.5;
    }
}

/* ========== Hamburger Menu Button ========== */
.hamburger-btn {
    display: none;
    background: none;
    border: none;
    color: var(--wa-text-secondary);
    font-size: 1.3rem;
    padding: 8px;
    cursor: pointer;
    min-width: var(--mobile-touch-target);
    min-height: var(--mobile-touch-target);
    align-items: center;
    justify-content: center;
}

.hamburger-btn:hover {
    color: var(--wa-text);
}

@media (max-width: 768px) {
    .hamburger-btn {
        display: flex;
    }

    /* Hide icon sidebar by default on mobile */
    .icon-sidebar {
        display: none !important;
    }

    /* Show icon sidebar when toggled */
    .icon-sidebar.show-mobile {
        display: flex !important;
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        z-index: 100;
        animation: slideInLeft 0.25s ease;
    }

    /* Full sidebar slides from icon sidebar */
    .sidebar.show-mobile {
        left: 0;
        width: 100%;
    }
}

/* ========== Mobile Overlay for Sidebar ========== */
.sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 89;
    opacity: 0;
    transition: opacity 0.25s;
}

.sidebar-overlay.show {
    opacity: 1;
}

@media (max-width: 768px) {
    .sidebar-overlay {
        display: block;
        pointer-events: none;
    }

    .sidebar-overlay.show {
        pointer-events: auto;
    }
}

/* ========== Animations ========== */
@keyframes slideInLeft {
    from {
        transform: translateX(-100%);
        opacity: 0;
    }
    to {
        transform: translateX(0);
        opacity: 1;
    }
}

@keyframes slideOutLeft {
    from {
        transform: translateX(0);
        opacity: 1;
    }
    to {
        transform: translateX(-100%);
        opacity: 0;
    }
}

/* ========== Accessibility: Reduce Motion ========== */
@media (prefers-reduced-motion: reduce) {
    .sidebar.show-mobile,
    .swipe-indicator,
    .sidebar-overlay {
        animation: none;
        transition: none;
    }

    .toast-notification {
        animation: none;
    }

    .typing-indicator span {
        animation: none;
    }
}

/* ========== Print Styles - Hide mobile elements ========== */
@media print {
    .icon-sidebar,
    .hamburger-btn,
    .swipe-indicator,
    .sidebar-overlay,
    .back-btn,
    .chat-input {
        display: none !important;
    }

    .sidebar {
        display: block !important;
        width: auto !important;
        position: static !important;
    }

    .chat-main {
        min-width: auto !important;
    }

    .message {
        max-width: 100% !important;
    }
}
