/* ============================================================
   pwa-mobile.css
   PWA Install Banner + Mobile UI polish
   Import trong index.html SAU canvas-mobile.css
   ============================================================ */

/* ── CSS custom property: real viewport height ────────────────────────── */
:root {
    --vh: 1vh; /* override bằng JS: setVhVar() trong mobile-helpers.js */
    --keyboard-height: 0px;
    --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);
}

/* Dùng --vh thay vì 100vh để tránh iOS Safari address bar bug */
@media (max-width: 768px) {
    .canvas-app {
        height: calc(var(--vh, 1vh) * 100) !important;
    }

    .um-page {
        height: calc(var(--vh, 1vh) * 100) !important;
    }
}

/* ── PWA Install Banner ────────────────────────────────────────────────── */
.pwa-install-banner {
    position: fixed;
    bottom: calc(16px + var(--safe-bottom));
    left: 16px;
    right: 16px;
    z-index: 9000;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 14px;
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 14px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.15), 0 2px 8px rgba(0,0,0,0.08);
    animation: bannerSlideUp 0.3s cubic-bezier(.4,0,.2,1);
    max-width: 480px;
    margin: 0 auto;
}

@keyframes bannerSlideUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.pwa-install-icon {
    flex-shrink: 0;
    width: 40px;
    height: 40px;
    background: #eff6ff;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.pwa-install-text {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
}

.pwa-install-text strong {
    font-size: 13px;
    font-weight: 600;
    color: #111827;
}

.pwa-install-text span {
    font-size: 11px;
    color: #6b7280;
}

.pwa-install-btn {
    flex-shrink: 0;
    padding: 7px 14px;
    border-radius: 8px;
    border: none;
    background: #2563eb;
    color: #fff;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s;
    white-space: nowrap;
}

.pwa-install-btn:hover {
    background: #1d4ed8;
}

.pwa-install-dismiss {
    flex-shrink: 0;
    width: 28px;
    height: 28px;
    border: none;
    background: transparent;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #9ca3af;
    padding: 0;
    transition: background 0.1s, color 0.1s;
}

.pwa-install-dismiss:hover {
    background: #f3f4f6;
    color: #374151;
}

/* Dark theme */
[data-theme="dark"] .pwa-install-banner,
.dark-theme .pwa-install-banner {
    background: var(--bg-primary, #1e293b);
    border-color: var(--border, #334155);
}

[data-theme="dark"] .pwa-install-text strong,
.dark-theme .pwa-install-text strong {
    color: var(--text-primary, #f1f5f9);
}

[data-theme="dark"] .pwa-install-icon,
.dark-theme .pwa-install-icon {
    background: rgba(37,99,235,0.15);
}

/* ── SW Update Banner ──────────────────────────────────────────────────── */
.sw-update-banner {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 9100;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    padding: 10px 16px;
    padding-top: calc(10px + var(--safe-top));
    background: #2563eb;
    color: #fff;
    font-size: 13px;
    font-weight: 500;
    animation: slideDown 0.3s ease;
}

@keyframes slideDown {
    from { transform: translateY(-100%); }
    to { transform: translateY(0); }
}

.sw-update-btn {
    padding: 5px 12px;
    border-radius: 6px;
    border: 1.5px solid rgba(255,255,255,0.5);
    background: transparent;
    color: #fff;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s;
}

.sw-update-btn:hover {
    background: rgba(255,255,255,0.15);
}

/* ── PWA Standalone mode adjustments ──────────────────────────────────── */
.pwa-standalone .topbar {
    padding-top: var(--safe-top);
    height: calc(44px + var(--safe-top));
}

.pwa-standalone .sidebar {
    padding-top: var(--safe-top);
}

/* ── Offline indicator ────────────────────────────────────────────────── */
.offline-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 2px 8px;
    border-radius: 10px;
    background: #fef3c7;
    color: #92400e;
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .04em;
}

.offline-badge::before {
    content: '';
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #f59e0b;
    flex-shrink: 0;
}

/* ── Touch optimizations ──────────────────────────────────────────────── */

/* Minimum 44px touch targets (Apple HIG / WCAG) */
@media (hover: none) {
    .tool-btn,
    .icon-btn,
    .btn-primary,
    .btn-outline,
    .doc-item,
    .page-item,
    .um-page-btn {
        /*min-height: 44px;
        min-width: 44px;*/
    }

    /* Exceptions: purely decorative / inline */
    .icon-btn.sm {
        min-height: 36px;
        min-width: 36px;
    }

    /* Tap highlight */
    button, a, [role="button"] {
        -webkit-tap-highlight-color: rgba(37,99,235,0.1);
    }

    /* Remove tap delay */
    * {
        touch-action: manipulation;
    }

    /* Canvas workspace: let Fabric handle touch */
    .canvas-workspace,
    .canvas-workspace * {
        touch-action: none;
    }
}

/* ── Keyboard-open state ──────────────────────────────────────────────── */
.keyboard-open .bottom-toolbar {
    /* Di lên khi keyboard mở */
    transform: translateY(calc(-1 * var(--keyboard-height)));
    transition: transform 0.25s ease;
}

.keyboard-open .mobile-bottom-sheet-overlay {
    /* Điều chỉnh overlay khi keyboard mở */
    bottom: var(--keyboard-height);
}

/* ── Mobile sidebar close button ─────────────────────────────────────── */
.mobile-sidebar-close {
    display: none;
}

@media (max-width: 768px) {
    .mobile-sidebar-close {
        display: flex;
    }
}

/* ── Orientation: landscape phone ────────────────────────────────────── */
@media (max-width: 896px) and (orientation: landscape) and (max-height: 500px) {
    /* Landscape phone: giảm chiều cao các element */
    .topbar {
        height: 36px !important;
    }

    .bottom-toolbar {
        min-height: 44px !important;
        padding: 4px 8px !important;
    }

    .tool-btn {
        width: 30px !important;
        height: 30px !important;
    }

    /* Sidebar chiếm toàn chiều cao */
    .sidebar.open {
        width: 200px !important;
    }

    /* Mobile panel: smaller */
    .mobile-bottom-sheet {
        max-height: 80vh;
    }
}

/* ── Tablet (768px - 1024px): Hybrid layout ─────────────────────────── */
@media (min-width: 769px) and (max-width: 1024px) {
    /* Sidebar vẫn overlay nhưng rộng hơn */
    .sidebar.open {
        width: 240px;
    }

    /* Right panel thu hẹp */
    .right-panel {
        width: 44px;
    }
}

/* ── Accessibility: focus visible trên touch ─────────────────────────── */
@media (hover: none) {
    :focus-visible {
        outline: 2px solid #2563eb;
        outline-offset: 2px;
    }

    :focus:not(:focus-visible) {
        outline: none;
    }
}
