/* ========================================================================= */
/* APPLE DESIGN LANGUAGE LAYER                                               */
/* Loaded after Tailwind, style.css and page <style> blocks. Colors, radii   */
/* and fonts come from assets/js/tailwind-theme.js; this file adds the       */
/* materials, motion, feedback and typography rules.                         */
/* ========================================================================= */

:root {
    --apple-blue: #007AFF;
    --apple-green: #34C759;
    --apple-red: #FF3B30;
    --apple-orange: #FF9500;
    --apple-label: #1D1D1F;
    --apple-secondary-label: #6E6E73;
    --apple-separator: rgba(60, 60, 67, 0.14);
    --apple-grouped-bg: #F2F2F7;
    --apple-card: #FFFFFF;
    --apple-focus-ring: 0 0 0 4px rgba(0, 122, 255, 0.24);

    /* SF Pro on Apple devices (system font); Inter, its closest open equivalent, everywhere else.
       SF Pro itself may not be served as a web font to non-Apple platforms (Apple font licence). */
    --font-text: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro", "Inter", "Segoe UI Variable Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --font-display: -apple-system, BlinkMacSystemFont, "SF Pro Display", "SF Pro", "Inter", "Segoe UI Variable Display", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --font-mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;

    /* Materials: heavier for structure (sidebar), lighter for floating chrome */
    --material-thick: rgba(246, 246, 248, 0.78);
    --material-regular: rgba(255, 255, 255, 0.72);
    --material-sheet: rgba(255, 255, 255, 0.92);
    --material-blur: blur(30px) saturate(180%);

    /* Springs approximated for CSS (damping 1.0, response ~0.35-0.4s) */
    --ease-spring: cubic-bezier(0.2, 0.8, 0.2, 1);
    --ease-press: cubic-bezier(0.3, 0, 0.2, 1);
}

@supports (transition-timing-function: linear(0, 1)) {
    :root {
        --ease-spring: linear(0, 0.009, 0.035 2.1%, 0.141 4.4%, 0.281 6.7%, 0.723 12.9%, 0.938 16.7%,
            1.017 19.4%, 1.077, 1.121 24%, 1.149 26.3%, 1.159, 1.163 29.7%, 1.154 32%, 1.111 36.8%,
            1.045 43.6%, 1.012 48.3%, 0.996 53.1%, 0.989 59.5%, 0.999 78.3%, 1);
        /* critically damped (no overshoot) — used for UI that was not flicked */
        --ease-spring-calm: linear(0, 0.034, 0.126 4.3%, 0.256 7%, 0.53 12.7%, 0.749 18.6%, 0.877 24.5%,
            0.946 30.7%, 0.98 37.5%, 0.995 46.4%, 1);
    }
}

/* ---------- Typography: system font, size-specific tracking & leading ---------- */
html {
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
}

body {
    font-family: var(--font-text);
    font-optical-sizing: auto; /* Inter's opsz axis switches to display cuts at large sizes, like SF */
    letter-spacing: -0.01em;
    color: var(--apple-label);
    background: var(--apple-grouped-bg);
    background-image: none;
}

.font-display,
h1, h2, h3 {
    font-family: var(--font-display);
    letter-spacing: -0.022em;
}

/* Every control, alert and chart label uses the same family */
input, select, textarea, button, .swal2-popup, svg text {
    font-family: var(--font-text);
}

code, kbd, pre, samp, .font-mono {
    font-family: var(--font-mono);
}

/* Numbers in stats and tables line up like Apple's tabular figures */
.font-display, .tabular-nums, td, th {
    font-variant-numeric: tabular-nums;
}

/* Large text tightens, small text opens up slightly */
.text-2xl, .text-3xl, .text-4xl, .text-5xl { letter-spacing: -0.025em; line-height: 1.12; }
.text-xl, .text-lg { letter-spacing: -0.018em; }
.text-xs { letter-spacing: 0; }
.text-\[10px\], .text-\[9px\], .text-\[11px\] { letter-spacing: 0.01em; }

/* Apple avoids ultra-heavy weights in UI; cap black at heavy bold */
.font-black { font-weight: 800; }

/* Uppercase micro-labels read better with positive tracking */
.uppercase.tracking-wider, .uppercase.tracking-widest { letter-spacing: 0.06em; }

/* ---------- Materials ---------- */
#mainSidebar {
    background: var(--material-thick) !important;
    -webkit-backdrop-filter: var(--material-blur);
    backdrop-filter: var(--material-blur);
    border-right: 0.5px solid var(--apple-separator) !important;
    box-shadow: none !important;
}

main > header,
header.sticky {
    background: var(--material-regular) !important;
    -webkit-backdrop-filter: var(--material-blur);
    backdrop-filter: var(--material-blur);
    border-bottom: 0.5px solid var(--apple-separator) !important;
}

/* Cards: solid, quiet, hairline edge (no gradients) */
.card-3d {
    background: var(--apple-card);
    border: 0.5px solid rgba(0, 0, 0, 0.06);
    border-radius: 18px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 4px 16px -6px rgba(0, 0, 0, 0.06);
    transition: box-shadow 0.35s var(--ease-spring), transform 0.35s var(--ease-spring);
}

.card-3d:hover {
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 12px 32px -10px rgba(0, 0, 0, 0.12);
}

.table-container {
    border: 0.5px solid rgba(0, 0, 0, 0.06);
    border-radius: 16px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}

.table-header-sticky th {
    background: rgba(249, 249, 251, 0.85);
    -webkit-backdrop-filter: blur(20px) saturate(180%);
    backdrop-filter: blur(20px) saturate(180%);
    border-bottom: 0.5px solid var(--apple-separator);
}

/* ---------- Sidebar navigation (source-list style) ---------- */
.sidebar-item {
    border-radius: 10px !important;
    transition: background-color 0.2s var(--ease-press), color 0.2s var(--ease-press), transform 0.16s var(--ease-press);
}

.sidebar-item:hover {
    background: rgba(0, 0, 0, 0.04) !important;
}

.sidebar-item-active,
.sidebar-item.sidebar-item-active {
    background: rgba(0, 122, 255, 0.12) !important;
    color: var(--apple-blue) !important;
    border-right: 0 !important;
    font-weight: 600;
}

.sidebar-item-active svg { color: var(--apple-blue) !important; }

/* ---------- Collapsed sidebar: icon rail (desktop / tablet only) ---------- */
@media (min-width: 768px) {
    #mainSidebar {
        transition: width 0.42s var(--ease-spring-calm, var(--ease-spring));
        overflow-x: hidden;
    }

    #mainSidebar.is-rail { width: 72px !important; }

    #mainSidebar.is-rail nav { padding-left: 8px; padding-right: 8px; overflow-x: hidden; }

    /* Labels, chevrons and section titles disappear; icons stay */
    #mainSidebar.is-rail .sidebar-label,
    #mainSidebar.is-rail .sidebar-item span:not([id^="badge"]),
    #mainSidebar.is-rail #settingsMenuChevron,
    #mainSidebar.is-rail #settingsSubmenu {
        display: none !important;
    }

    #mainSidebar.is-rail .sidebar-item {
        position: relative;
        justify-content: center !important;
        gap: 0 !important;
        padding: 10px 0 !important;
    }

    #mainSidebar.is-rail .sidebar-item svg { width: 20px; height: 20px; }

    /* Section title becomes a hairline divider */
    #mainSidebar.is-rail .sidebar-section {
        margin: 6px 12px;
        padding: 0;
        border-top: 0.5px solid var(--apple-separator);
    }

    /* Counts sit on the icon's corner, like a Dock badge */
    #mainSidebar.is-rail .sidebar-item span[id^="badge"]:not(.hidden) {
        position: absolute;
        top: 3px;
        right: 9px;
        min-width: 16px;
        height: 16px;
        padding: 0 4px;
        font-size: 9px;
        line-height: 16px;
        text-align: center;
        box-shadow: 0 0 0 2px #F6F6F8;
    }

    #mainSidebar.is-rail .sidebar-brand {
        justify-content: center;
        padding-left: 12px;
        padding-right: 12px;
    }

    #mainSidebar.is-rail .sidebar-footer { padding: 10px 8px; }

    #mainSidebar.is-rail .sidebar-footer-row {
        flex-direction: column;
        gap: 4px;
    }

    #mainSidebar.is-rail .sidebar-footer-row > button {
        flex: none;
        margin: 0;
        justify-content: center;
    }
}

/* ---------- Response: feedback on press, instantly (skill §1) ---------- */
button,
[role="button"],
.sidebar-item,
.btn-primary-gradient {
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation; /* removes the ~300ms tap delay */
}

button:not(:disabled):active,
[role="button"]:active,
a.sidebar-item:active,
.btn-primary-gradient:active {
    transform: scale(0.97);
    transition: transform 0.1s var(--ease-press);
}

button,
.btn-primary-gradient {
    transition: transform 0.35s var(--ease-spring), background-color 0.2s ease, color 0.2s ease,
                box-shadow 0.2s ease, opacity 0.2s ease, border-color 0.2s ease;
}

/* Primary action: solid systemBlue, no gradient, no lift */
.btn-primary-gradient {
    background: var(--apple-blue);
    border-radius: 12px;
    box-shadow: 0 1px 2px rgba(0, 122, 255, 0.25);
}

.btn-primary-gradient:hover {
    transform: none;
    background: #0071EB;
    box-shadow: 0 4px 14px -4px rgba(0, 122, 255, 0.45);
}

/* ---------- Inputs ---------- */
.input-3d,
input[type="text"], input[type="email"], input[type="password"], input[type="search"],
input[type="tel"], input[type="number"], input[type="date"], input[type="datetime-local"],
select, textarea {
    border-radius: 10px;
    transition: box-shadow 0.2s ease, border-color 0.2s ease, background-color 0.2s ease;
}

.input-3d {
    background: #FFFFFF;
    border: 0.5px solid rgba(60, 60, 67, 0.22);
    box-shadow: none;
}

.input-3d:focus,
input:focus-visible, select:focus-visible, textarea:focus-visible {
    outline: none;
    border-color: var(--apple-blue) !important;
    box-shadow: var(--apple-focus-ring) !important;
}

/* Keyboard focus for everything else */
:focus-visible {
    outline: 2px solid var(--apple-blue);
    outline-offset: 2px;
}

/* ---------- Status badges: flat tinted fills ---------- */
.badge-done      { background: rgba(52, 199, 89, 0.14);  color: #1E7A34; border: 0; }
.badge-awaiting  { background: rgba(175, 82, 222, 0.14); color: #8A3DB8; border: 0; }
.badge-planned,
.badge-progress  { background: rgba(0, 122, 255, 0.12);  color: #0066D6; border: 0; }
.badge-pending   { background: rgba(255, 149, 0, 0.16);  color: #A35A00; border: 0; }
.badge-overdue   { background: rgba(255, 59, 48, 0.12);  color: #C4190E; border: 0; }

/* ---------- Sheets / modals: dim to focus, materialize in (skill §7, §12) ---------- */
div[id$="Modal"].fixed.inset-0 {
    background-color: rgba(0, 0, 0, 0.32) !important;
    -webkit-backdrop-filter: blur(6px);
    backdrop-filter: blur(6px);
    animation: appleScrimIn 0.28s ease-out both;
}

div[id$="Modal"].fixed.inset-0 > div:first-child {
    border-radius: 22px;
    background-color: var(--material-sheet);
    -webkit-backdrop-filter: var(--material-blur);
    backdrop-filter: var(--material-blur);
    border: 0.5px solid rgba(255, 255, 255, 0.6);
    box-shadow: 0 32px 80px -20px rgba(0, 0, 0, 0.35), 0 0 0 0.5px rgba(0, 0, 0, 0.08);
    animation: appleSheetIn 0.5s var(--ease-spring-calm, var(--ease-spring)) both;
    transform-origin: 50% 60%;
}

/* Full-screen media & camera keep their dark stage */
#mediaViewerModal > div:first-child,
#cameraCaptureModal > div:first-child {
    background-color: rgba(28, 28, 30, 0.94);
    border: 0.5px solid rgba(255, 255, 255, 0.1);
}
#mediaViewerModal.fixed.inset-0,
#cameraCaptureModal.fixed.inset-0 {
    background-color: rgba(0, 0, 0, 0.86) !important;
}

#permissionsModalCard.modal-fullscreen { border-radius: 0 !important; }

@keyframes appleScrimIn {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Surface arrives as a material: scale + blur + opacity together */
@keyframes appleSheetIn {
    from { opacity: 0; transform: translateY(18px) scale(0.965); filter: blur(6px); }
    60%  { filter: blur(0); }
    to   { opacity: 1; transform: none; filter: none; }
}

/* Phones: sheets rise from the bottom edge, matching iOS */
@media (max-width: 640px) {
    div[id$="Modal"].fixed.inset-0:not(#mediaViewerModal):not(#cameraCaptureModal):not(#permissionsModal) {
        align-items: flex-end !important;
        padding: 0 !important;
    }
    div[id$="Modal"].fixed.inset-0:not(#mediaViewerModal):not(#cameraCaptureModal):not(#permissionsModal) > div:first-child {
        border-radius: 22px 22px 0 0;
        margin: 0 !important;
        max-height: 92vh;
        padding-bottom: max(1.25rem, env(safe-area-inset-bottom));
        animation-name: appleSheetUp;
        animation-duration: 0.45s;
    }
    /* Scroll inside the sheet, except where a card needs overflow for dropdowns */
    div[id$="Modal"].fixed.inset-0 > div:first-child:not(.overflow-visible) {
        overflow-y: auto;
    }
    @keyframes appleSheetUp {
        from { transform: translateY(100%); }
        to   { transform: none; }
    }
}

/* Mobile drawer backdrop */
#mobileSidebarBackdrop {
    background-color: rgba(0, 0, 0, 0.28) !important;
}

/* ---------- SweetAlert2 → iOS alerts & toasts ---------- */
.swal2-popup {
    font-family: inherit !important;
    border-radius: 20px !important;
    background: var(--material-sheet) !important;
    -webkit-backdrop-filter: var(--material-blur);
    backdrop-filter: var(--material-blur);
    box-shadow: 0 32px 80px -20px rgba(0, 0, 0, 0.35), 0 0 0 0.5px rgba(0, 0, 0, 0.08) !important;
    color: var(--apple-label) !important;
}

/* Dim only behind real alerts; toasts float over the page with no backdrop */
body:not(.swal2-toast-shown) .swal2-container.swal2-backdrop-show {
    background: rgba(0, 0, 0, 0.3) !important;
}

body.swal2-toast-shown .swal2-container {
    background: transparent !important;
    -webkit-backdrop-filter: none !important;
    backdrop-filter: none !important;
}

.swal2-show {
    animation: appleSheetIn 0.45s var(--ease-spring-calm, var(--ease-spring)) both !important;
}

.swal2-hide {
    animation: appleSheetOut 0.2s ease-in both !important;
}

@keyframes appleSheetOut {
    to { opacity: 0; transform: scale(0.97); }
}

.swal2-title {
    font-size: 1.125rem !important;
    font-weight: 600 !important;
    letter-spacing: -0.018em;
    color: var(--apple-label) !important;
}

.swal2-html-container {
    font-size: 0.875rem !important;
    color: var(--apple-secondary-label) !important;
    line-height: 1.45;
}

.swal2-styled {
    border-radius: 12px !important;
    font-weight: 600 !important;
    padding: 0.65em 1.4em !important;
    box-shadow: none !important;
    transition: transform 0.35s var(--ease-spring), filter 0.2s ease !important;
}

/* Default confirm = systemBlue; an explicit confirmButtonColor (inline style) still wins */
.swal2-actions .swal2-styled.swal2-confirm { background-color: var(--apple-blue); }

.swal2-styled:active { transform: scale(0.97); transition: transform 0.1s var(--ease-press) !important; }
.swal2-styled:focus  { box-shadow: var(--apple-focus-ring) !important; }

.swal2-input, .swal2-textarea, .swal2-select {
    border-radius: 10px !important;
    border: 0.5px solid rgba(60, 60, 67, 0.22) !important;
    box-shadow: none !important;
    font-size: 0.875rem !important;
}

.swal2-input:focus, .swal2-textarea:focus { box-shadow: var(--apple-focus-ring) !important; border-color: var(--apple-blue) !important; }

/* SweetAlert hides parts of the success ring with solid-white mask shapes; on the translucent
   material those masks show as white blobs beside the tick, so make them see-through */
.swal2-icon.swal2-success [class^="swal2-success-circular-line"],
.swal2-icon.swal2-success .swal2-success-fix {
    background-color: transparent !important;
}

.swal2-popup.swal2-toast {
    border-radius: 14px !important;
    padding: 0.6rem 0.9rem !important;
    box-shadow: 0 12px 32px -8px rgba(0, 0, 0, 0.22), 0 0 0 0.5px rgba(0, 0, 0, 0.06) !important;
}

/* ---------- Loaders & progress ---------- */
#topProgressBar {
    height: 2px;
    background: var(--apple-blue);
    box-shadow: none;
}

#globalPageLoader { background: rgba(242, 242, 247, 0.6); }

.loader-card-popup {
    background: var(--material-sheet);
    -webkit-backdrop-filter: var(--material-blur);
    backdrop-filter: var(--material-blur);
    border: 0.5px solid rgba(0, 0, 0, 0.06);
    border-radius: 20px;
    box-shadow: 0 24px 60px -18px rgba(0, 0, 0, 0.3);
}

.fs-pill-loader rect { fill: #8E8E93; }

.skeleton-shimmer {
    background: linear-gradient(90deg, #F2F2F7 25%, #E5E5EA 50%, #F2F2F7 75%);
    background-size: 200% 100%;
}

/* No slow looping pulses (skill §14). Text badges stay still so they stay readable;
   empty status dots and the recording / live indicators keep their pulse. */
.pulse-glow,
span.animate-pulse:not(:empty) { animation: none; }

/* ---------- Scrollbars: thin, overlay-like ---------- */
* { scrollbar-width: thin; scrollbar-color: rgba(0, 0, 0, 0.22) transparent; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.2);
    border-radius: 999px;
    border: 3px solid transparent;
    background-clip: padding-box;
}
::-webkit-scrollbar-thumb:hover { background-color: rgba(0, 0, 0, 0.35); }
::-webkit-scrollbar-track { background: transparent; }

/* ---------- Accessibility: reduced motion / transparency / more contrast (skill §14) ---------- */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        scroll-behavior: auto !important;
    }
    div[id$="Modal"].fixed.inset-0 > div:first-child,
    .swal2-show {
        animation: appleScrimIn 0.2s ease both !important;
    }
    button:active, [role="button"]:active, a.sidebar-item:active, .btn-primary-gradient:active {
        transform: none;
    }
}

@media (prefers-reduced-transparency: reduce) {
    #mainSidebar, main > header, header.sticky,
    div[id$="Modal"].fixed.inset-0 > div:first-child, .swal2-popup, .loader-card-popup {
        background: #FFFFFF !important;
        -webkit-backdrop-filter: none !important;
        backdrop-filter: none !important;
    }
    #mainSidebar { background: #F6F6F8 !important; }
}

@media (prefers-contrast: more) {
    .card-3d, .table-container, .input-3d,
    div[id$="Modal"].fixed.inset-0 > div:first-child {
        border: 1px solid rgba(0, 0, 0, 0.45) !important;
    }
    #mainSidebar, main > header { background: #FFFFFF !important; }
}
