/**
 * MOBILE UNIFIED STYLES
 * Consolidated Mobile UI Improvements
 * Replaces: mobile-scaling.css, mobile-enhancements.css, mobile-optimizations.css, ios-enhancements.css, ios-pwa.css
 */

/* ============================================================================
   1. VARIABLES & RESETS
   ============================================================================ */
:root {
    /* Safe Area Variables with Defaults */
    --safe-area-top: env(safe-area-inset-top, 0px);
    --safe-area-bottom: env(safe-area-inset-bottom, 0px);
    --safe-area-left: env(safe-area-inset-left, 0px);
    --safe-area-right: env(safe-area-inset-right, 0px);

    /* Mobile Scale Factors */
    --mobile-scale: 1;
    --header-height: 64px;
    --header-height-mobile: 56px;
}

/* Base Reset for Touch Devices */
* {
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
}

/* Allow text selection on inputs and content that should be selectable */
input,
textarea,
.markdown-body,
.selectable-text {
    -webkit-touch-callout: default;
    user-select: text;
    -webkit-user-select: text;
}

/* Prevent horizontal scroll */
html,
body {
    overflow-x: hidden;
    width: 100vw;
    overscroll-behavior-y: none;
    /* Prevent pull-to-refresh unless explicitly handled */
    -webkit-overflow-scrolling: touch;
}

/* Fix 100vh on Mobile */
body {
    min-height: 100vh;
    min-height: 100dvh;
}

@supports (-webkit-touch-callout: none) {
    body {
        min-height: -webkit-fill-available;
    }
}

/* ============================================================================
   2. TYPOGRAPHY & SCALING (Viewport Specifics)
   ============================================================================ */

/* Base Font Scaling */
html {
    font-size: 16px;
    /* Default */
    -webkit-text-size-adjust: 100%;
}

@media screen and (max-width: 375px) {
    html {
        font-size: 14px;
    }
}

@media screen and (min-width: 376px) and (max-width: 428px) {
    html {
        font-size: 15px;
    }
}

/* Input Font Size Fix (Prevents iOS Zoom) */
@media screen and (max-width: 768px) {

    input,
    select,
    textarea {
        font-size: 16px !important;
    }
}

/* ============================================================================
   3. LAYOUT & CONTAINERS
   ============================================================================ */

/* Safe Area Spacing Utilities */
.safe-pt {
    padding-top: var(--safe-area-top) !important;
}

.safe-pb {
    padding-bottom: var(--safe-area-bottom) !important;
}

.safe-pl {
    padding-left: var(--safe-area-left) !important;
}

.safe-pr {
    padding-right: var(--safe-area-right) !important;
}

/* Header Adjustments */
header[role="banner"] {
    padding-top: var(--safe-area-top);
    transition: height 0.2s ease;
}

@media screen and (max-width: 640px) {
    header[role="banner"] {
        height: calc(var(--header-height-mobile) + var(--safe-area-top)) !important;
        padding-left: 12px !important;
        padding-right: 12px !important;
    }

    /* Compact header elements on small screens */
    header[role="banner"] h1 {
        font-size: 1.1rem !important;
    }
}

/* Main Content Padding for Header - REMOVED (Header is flex child) */
/* Main Content Padding for Header - REMOVED (Header is flex child) */
/* main { padding-top: calc(var(--header-height) + var(--safe-area-top)); } */


@media screen and (max-width: 640px) {
    main {
        padding-top: 0 !important;
    }
}


/* ============================================================================
   4. INTERACTIVE ELEMENTS
   ============================================================================ */

/* Touch Targets - Minimum 44px */
@media (pointer: coarse) {

    button,
    a,
    input[type="checkbox"],
    input[type="radio"],
    select,
    [role="button"] {
        min-width: 44px;
        min-height: 44px;
    }
}

/* Button Active States (Feedback) */
button:active {
    transform: scale(0.97);
    opacity: 0.9;
}

/* Input Fields Styles */
input,
textarea {
    -webkit-appearance: none;
    appearance: none;
    border-radius: 8px;
    /* Standardize border radius */
}

/* Chat Input Area specific optimizations */
.input-area-container,
[class*="input-area"] {
    padding-bottom: max(12px, var(--safe-area-bottom)) !important;
}

/* ============================================================================
   5. COMPONENT OVERRIDES
   ============================================================================ */

/* --- MODALS --- */
/* Full screen modals on mobile */
@media screen and (max-width: 640px) {

    [role="dialog"],
    .modal-content {
        width: 100vw !important;
        height: 100dvh !important;
        max-height: 100dvh !important;
        max-width: 100vw !important;
        margin: 0 !important;
        border-radius: 0 !important;
        display: flex;
        flex-direction: column;
    }

    /* Ensure content within modal scrolls properly */
    [role="dialog"]>div,
    .modal-body {
        flex: 1;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        padding-bottom: calc(20px + var(--safe-area-bottom)) !important;
    }

    /* Close button positioning */
    [role="dialog"] button[aria-label="Close"] {
        top: max(12px, var(--safe-area-top)) !important;
        right: 12px !important;
    }
}

/* --- TOASTS --- */
/* Mobile toast positioning */
@media screen and (max-width: 640px) {

    .toast-container,
    [class*="ToastContainer"] {
        top: auto !important;
        bottom: max(20px, var(--safe-area-bottom)) !important;
        left: 16px !important;
        right: 16px !important;
        transform: none !important;
        width: auto !important;
    }

    .toast,
    [class*="Toast"] {
        width: 100% !important;
        margin-bottom: 8px;
    }
}

/* --- SIDEBAR --- */
/* Mobile Sidebar interaction */
@media screen and (max-width: 768px) {
    aside[class*="Sidebar"] {
        width: 85vw !important;
        max-width: 320px !important;
        padding-top: var(--safe-area-top);
        padding-bottom: var(--safe-area-bottom);
    }
}

/* --- TABLES --- */
/* Responsive Table Wrapper */
.table-responsive {
    display: block;
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

/* Mobile Table Adjustments */
@media screen and (max-width: 640px) {

    /* Force tables to not exceed viewport */
    table {
        width: 100%;
        table-layout: fixed;
    }

    th,
    td {
        padding: 8px 10px !important;
        font-size: 0.85rem !important;
        overflow-wrap: break-word;
    }
}

/* ============================================================================
   6. PWA & STANDALONE SPECIFICS
   ============================================================================ */
@media all and (display-mode: standalone) {
    body {
        /* Prevent selection in UI elements for app-like feel */
        user-select: none;
        -webkit-user-select: none;
    }

    /* Re-enable for text content */
    p,
    h1,
    h2,
    h3,
    h4,
    h5,
    h6,
    span,
    article {
        user-select: text;
        -webkit-user-select: text;
    }
}

/* ============================================================================
   7. UTILITIES
   ============================================================================ */
.no-scroll {
    overflow: hidden !important;
}

.gpu-accelerated {
    transform: translateZ(0);
    will-change: transform;
}

/* App specific height fix */
.ios-h-screen {
    height: 100vh;
    height: 100dvh;
    min-height: 100dvh;
}

@supports (-webkit-touch-callout: none) {
    .ios-h-screen {
        height: -webkit-fill-available;
    }
}/**
 * UX Enhancements v1.0
 * 
 * Comprehensive micro-interaction polish, better focus states,
 * smoother transitions, improved form UX, and visual refinements.
 */

/* ==================== Global Transition Smoothness ==================== */

/* Ensure all interactive elements have smooth transitions */
button,
a,
input,
textarea,
select,
[role="button"],
[tabindex] {
    transition: background-color 0.2s ease, color 0.2s ease, border-color 0.2s ease,
        box-shadow 0.2s ease, opacity 0.2s ease, transform 0.15s ease;
}

/* ==================== Better Focus States ==================== */

/* Modern focus ring for all focusable elements */
button:focus-visible,
a:focus-visible,
input:focus-visible,
textarea:focus-visible,
select:focus-visible,
[role="button"]:focus-visible,
[tabindex]:focus-visible {
    outline: 2px solid var(--luca-primary, #3b82f6);
    outline-offset: 2px;
    border-radius: inherit;
}

/* Remove default outline for mouse users */
button:focus:not(:focus-visible),
a:focus:not(:focus-visible),
input:focus:not(:focus-visible),
textarea:focus:not(:focus-visible),
select:focus:not(:focus-visible) {
    outline: none;
}

/* ==================== Enhanced Input Fields ==================== */

/* Smooth focus glow on inputs */
input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
input[type="search"],
input[type="url"],
input[type="tel"],
textarea,
select {
    transition: border-color 0.25s ease, box-shadow 0.25s ease, background-color 0.2s ease;
}

input[type="text"]:focus,
input[type="email"]:focus,
input[type="password"]:focus,
input[type="number"]:focus,
input[type="search"]:focus,
input[type="url"]:focus,
input[type="tel"]:focus,
textarea:focus,
select:focus {
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
}

/* Dark mode input focus */
.dark input:focus,
[data-dark-mode="true"] input:focus,
.dark textarea:focus,
.dark select:focus {
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.25);
}

/* Placeholder animation */
input::placeholder,
textarea::placeholder {
    transition: opacity 0.2s ease, transform 0.2s ease;
}

input:focus::placeholder,
textarea:focus::placeholder {
    opacity: 0.5;
    transform: translateX(4px);
}

/* ==================== Better Button Interactions ==================== */

/* Subtle press feedback for all buttons */
button:not(:disabled):active,
[role="button"]:not(:disabled):active {
    transform: scale(0.97);
}

/* Disabled button styling */
button:disabled,
[role="button"][aria-disabled="true"] {
    opacity: 0.5;
    cursor: not-allowed;
    filter: grayscale(0.3);
}

/* Primary action button glow on hover */
button[class*="bg-blue-"] {
    transition: all 0.2s ease, box-shadow 0.3s ease;
}

button[class*="bg-blue-"]:hover:not(:disabled) {
    box-shadow: 0 4px 15px rgba(59, 130, 246, 0.35);
}

/* Amber/warning button glow */
button[class*="bg-amber-"]:hover:not(:disabled) {
    box-shadow: 0 4px 15px rgba(245, 158, 11, 0.3);
}

/* Success button glow */
button[class*="bg-emerald-"]:hover:not(:disabled),
button[class*="bg-green-"]:hover:not(:disabled) {
    box-shadow: 0 4px 15px rgba(16, 185, 129, 0.3);
}

/* Danger button glow */
button[class*="bg-red-"]:hover:not(:disabled) {
    box-shadow: 0 4px 15px rgba(239, 68, 68, 0.3);
}

/* ==================== Card & Panel Improvements ==================== */

/* Smooth border highlight on hover for bordered cards */
div[class*="border"][class*="rounded"] {
    transition: border-color 0.2s ease, box-shadow 0.25s ease, transform 0.2s ease;
}

/* ==================== Modal Enhancements ==================== */

/* Smoother modal backdrop */
div[class*="fixed"][class*="inset-0"][class*="bg-black"] {
    transition: opacity 0.25s ease;
}

/* Modal content spring animation */
@keyframes modal-spring-in {
    0% {
        opacity: 0;
        transform: translateY(20px) scale(0.96);
    }
    60% {
        transform: translateY(-3px) scale(1.005);
    }
    100% {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

.modal-spring {
    animation: modal-spring-in 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}

/* ==================== Chat-Specific Enhancements ==================== */

/* Smoother message action buttons - fade in on hover */
.chat-actions {
    opacity: 0;
    transform: translateY(4px);
    transition: opacity 0.2s ease, transform 0.2s ease;
}

/* Show actions on message hover */
.chat-message-wrapper:hover .chat-actions,
.chat-actions:focus-within {
    opacity: 1;
    transform: translateY(0);
}

/* Always show actions on mobile (no hover) */
@media (hover: none) {
    .chat-actions {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Better code block styling in chat */
.prose pre {
    border-radius: 0.75rem;
    border: 1px solid rgba(255, 255, 255, 0.08);
    transition: border-color 0.2s ease;
}

.prose pre:hover {
    border-color: rgba(99, 102, 241, 0.3);
}

/* Inline code better visibility */
.prose code:not(pre code) {
    padding: 0.15em 0.4em;
    border-radius: 0.3em;
    font-size: 0.875em;
    font-weight: 500;
}

/* ==================== Sidebar Enhancements ==================== */

/* Better session item interaction */
.session-item {
    position: relative;
    overflow: hidden;
}

.session-item::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 3px;
    background: var(--luca-primary, #3b82f6);
    transform: scaleY(0);
    transition: transform 0.2s ease;
    border-radius: 0 2px 2px 0;
}

.session-item:hover::before,
.session-item[data-active="true"]::before {
    transform: scaleY(1);
}

/* ==================== Header Enhancements ==================== */

/* Profile avatar ring animation */
@keyframes avatar-ring-pulse {
    0%, 100% {
        box-shadow: 0 0 0 0 rgba(59, 130, 246, 0.4);
    }
    50% {
        box-shadow: 0 0 0 4px rgba(59, 130, 246, 0);
    }
}

.avatar-online {
    animation: avatar-ring-pulse 3s ease-in-out infinite;
}

/* ==================== Toast/Notification Improvements ==================== */

/* Better toast entrance with spring */
@keyframes toast-spring-in {
    0% {
        opacity: 0;
        transform: translateY(-20px) scale(0.95);
    }
    60% {
        transform: translateY(3px) scale(1.01);
    }
    100% {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

@keyframes toast-enter {
    0% {
        opacity: 0;
        transform: translateX(24px) scale(0.95);
    }
    100% {
        opacity: 1;
        transform: translateX(0) scale(1);
    }
}

.toast-spring {
    animation: toast-spring-in 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

/* ==================== Loading State Improvements ==================== */

/* Better skeleton shimmer */
@keyframes enhanced-shimmer {
    0% {
        background-position: -200% 0;
    }
    100% {
        background-position: 200% 0;
    }
}

.skeleton-enhanced {
    background: linear-gradient(
        90deg,
        rgba(255, 255, 255, 0.04) 0%,
        rgba(255, 255, 255, 0.08) 20%,
        rgba(255, 255, 255, 0.12) 40%,
        rgba(255, 255, 255, 0.08) 60%,
        rgba(255, 255, 255, 0.04) 100%
    );
    background-size: 200% 100%;
    animation: enhanced-shimmer 1.8s ease-in-out infinite;
    border-radius: 8px;
}

/* Spinner with better visual */
@keyframes spin-smooth {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.spinner-smooth {
    animation: spin-smooth 0.8s linear infinite;
    border: 2px solid transparent;
    border-top-color: var(--luca-primary, #3b82f6);
    border-right-color: var(--luca-primary, #3b82f6);
    border-radius: 50%;
}

/* ==================== Selection & Text Improvements ==================== */

/* Better text selection colors */
::selection {
    background: rgba(59, 130, 246, 0.3);
    color: inherit;
}

.dark ::selection {
    background: rgba(96, 165, 250, 0.35);
}

/* ==================== Smooth Page/Section Transitions ==================== */

/* Fade transition for tab content */
@keyframes tab-fade-in {
    from {
        opacity: 0;
        transform: translateY(8px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.tab-content-enter {
    animation: tab-fade-in 0.25s ease-out;
}

/* ==================== Tooltip Improvements ==================== */

/* Better tooltip appearance */
[title] {
    position: relative;
}

/* ==================== Badge & Chip Polish ==================== */

/* Subtle pulse for notification badges */
@keyframes badge-pulse {
    0%, 100% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.15);
    }
}

.badge-pulse {
    animation: badge-pulse 2s ease-in-out infinite;
}

/* ==================== Responsive Touch Improvements ==================== */

@media (hover: none) and (pointer: coarse) {
    /* Larger touch targets on mobile */
    button,
    [role="button"],
    a {
        min-height: 44px;
        min-width: 44px;
    }

    /* Exception for inline/icon buttons that are already in a larger container */
    button[class*="p-1"],
    button[class*="p-0"],
    .inline-btn {
        min-height: unset;
        min-width: unset;
    }

    /* Better active states for touch */
    button:active:not(:disabled),
    [role="button"]:active:not(:disabled) {
        transform: scale(0.95);
        opacity: 0.85;
    }
}

/* ==================== Smooth Number Transitions ==================== */

/* For token counts, XP, etc. */
.number-transition {
    transition: all 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
    display: inline-block;
}

/* ==================== Empty State Polish ==================== */

@keyframes empty-float {
    0%, 100% {
        transform: translateY(0);
    }
    50% {
        transform: translateY(-8px);
    }
}

.empty-state-icon {
    animation: empty-float 3s ease-in-out infinite;
    opacity: 0.6;
}

/* ==================== Smoother Modal Backdrops ==================== */

/* Ensure modal backdrops transition smoothly */
.modal-backdrop-enter {
    animation: backdrop-fade-in 0.25s ease-out;
}

@keyframes backdrop-fade-in {
    from { opacity: 0; backdrop-filter: blur(0px); }
    to { opacity: 1; backdrop-filter: blur(8px); }
}

/* Modal content with spring entrance */
.modal-content-enter {
    animation: modal-content-spring 0.35s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes modal-content-spring {
    from {
        opacity: 0;
        transform: translateY(12px) scale(0.97);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

/* ==================== Smooth View Transitions ==================== */

/* Page-level content fade for route/view changes */
.view-transition-enter {
    animation: view-enter 0.3s ease-out;
}

@keyframes view-enter {
    from {
        opacity: 0;
        transform: translateY(6px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* ==================== Better Hover States ==================== */

/* Subtle lift effect for interactive cards */
.interactive-card {
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.interactive-card:hover {
    transform: translateY(-1px);
}

.interactive-card:active {
    transform: translateY(0);
}

/* ==================== Improved Skeleton Loading ==================== */

/* Refined skeleton pulse for content placeholders */
.skeleton-pulse {
    background: linear-gradient(
        90deg,
        rgba(255, 255, 255, 0.03) 0%,
        rgba(255, 255, 255, 0.07) 50%,
        rgba(255, 255, 255, 0.03) 100%
    );
    background-size: 200% 100%;
    animation: skeleton-wave 1.5s ease-in-out infinite;
    border-radius: 6px;
}

@keyframes skeleton-wave {
    0% { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

/* ==================== Smooth Sidebar Transitions ==================== */

/* Sidebar slide with GPU acceleration */
.sidebar-transition {
    transition: transform 0.25s cubic-bezier(0.16, 1, 0.3, 1),
                opacity 0.2s ease;
    will-change: transform;
}

/* ==================== Better Scroll Behavior ==================== */

/* Smooth scroll for chat and content areas */
.smooth-scroll {
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
}

/* Overscroll glow prevention */
.no-overscroll {
    overscroll-behavior: contain;
}

/* ==================== Reduced Motion ==================== */

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.1ms !important;
    }

    .chat-actions {
        opacity: 1;
        transform: none;
    }
}
