/* ===========================================
   BASE.CSS - Foundation Styles
   ===========================================
   
   TABLE OF CONTENTS
   -----------------
   1. Reset & Base
   2. Base Typography
   3. Accessibility
   4. Focus States
   5. Header
   6. Nav Pills
   7. Nav Dropdown
   8. Theme Toggle
   9. Main Layout
   10. Footer
   11. Page Visibility & Transitions
   12. Shared Micro-Animations
   
   =========================================== */

/* -------------------------------------------
   RESET & BASE
   ------------------------------------------- */
*, *::before, *::after {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html {
    background: var(--bg-body, var(--bg-primary));
    transition: background-color var(--transition-slow);
    overscroll-behavior-y: none;
}

body {
    font-family: var(--font-sans);
    font-size: var(--text-base);
    font-weight: var(--font-normal);
    line-height: var(--leading-normal);
    color: var(--text-primary);
    background: var(--bg-primary);
    margin: 0;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* Subtle Background Texture */
body::before {
    content: '';
    position: fixed;
    inset: 0;
    background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.8' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noise)'/%3E%3C/svg%3E");
    opacity: 0.015;
    pointer-events: none;
    z-index: -1;
}

[data-theme="dark"] body::before {
    opacity: 0.03;
}

/* Highlighted text theme */

::selection {
    background: var(--theme-primary);
    color: var(--text-on-theme);
}

::-moz-selection {
    background: var(--theme-primary);
    color: var(--text-on-theme);
}

/* -------------------------------------------
   BASE TYPOGRAPHY
   ------------------------------------------- */
h1, h2, h3, h4, h5, h6 {
    margin: 0;
    font-weight: var(--font-bold);
    line-height: var(--leading-tight);
    letter-spacing: var(--tracking-tight);
    color: var(--text-primary);
}

h1 { font-size: var(--text-3xl); }
h2 { font-size: var(--text-xl); }
h3 { font-size: var(--text-lg); }
h4 { font-size: var(--text-base); }

p {
    margin: 0 0 1rem 0;
    line-height: var(--leading-relaxed);
}

p:last-child {
    margin-bottom: 0;
}

small,
.text-sm {
    font-size: var(--text-sm);
}

.text-xs {
    font-size: var(--text-xs);
}

.text-muted {
    color: var(--text-muted);
}

.text-secondary {
    color: var(--text-secondary);
}

a {
    color: var(--theme-primary);
    text-decoration: none;
    transition: color var(--transition-fast);
}

@media (any-hover: hover) {
    a:hover {
        color: var(--theme-dark);
    }
}

strong, b {
    font-weight: var(--font-semibold);
}

code, 
.font-mono {
    font-family: var(--font-mono);
    font-size: 0.9em;
}

ul, ol {
    margin: 0 0 1rem 0;
    padding-left: 1.5rem;
}

li {
    margin-bottom: 0.375rem;
    line-height: var(--leading-relaxed);
}

li:last-child {
    margin-bottom: 0;
}

.tabular-nums {
    font-variant-numeric: tabular-nums;
}

/* -------------------------------------------
   ACCESSIBILITY
   ------------------------------------------- */
.skip-link {
    position: absolute;
    top: -100%;
    left: 50%;
    transform: translateX(-50%);
    background: var(--text-primary);
    color: var(--bg-primary);
    padding: 0.75rem 1.5rem;
    border-radius: var(--radius-md);
    z-index: 1000;
    text-decoration: none;
    font-weight: var(--font-semibold);
    font-size: var(--text-sm);
    box-shadow: var(--shadow-lg);
    transition: top var(--transition-base);
}

.skip-link:focus {
    top: 1rem;
}

/* -------------------------------------------
   FOCUS STATES
   ------------------------------------------- */
*:focus {
    outline: none;
}

*:focus-visible {
    outline: 2px solid var(--theme-primary);
    outline-offset: 3px;                 /* Was 2px - more visible */
}

input:focus-visible,
select:focus-visible,
textarea:focus-visible {
    outline: none;
    box-shadow:
        0 0 0 3px var(--bg-elevated),    /* Was 2px - larger ring */
        0 0 0 5px var(--theme-primary);  /* Was 4px */
}

.select-btn:focus-visible,
.option-btn:focus-visible,
.toggle-row:focus-visible,
.alt-btn:focus-visible {
    outline: 2px solid var(--theme-primary);
    outline-offset: 2px;
}

/* -------------------------------------------
   HEADER
   ------------------------------------------- */
header {
    background: var(--bg-elevated);
    border-bottom: 1px solid var(--border-color);
    padding: 0.875rem 1.5rem;               /* Was 0.75rem - taller */
    padding-top: calc(0.875rem + env(safe-area-inset-top, 0px));
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 200;
    box-shadow: var(--shadow-sm);
    flex-shrink: 0;
    /* GPU compositing to prevent iOS Safari tap flash */
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
}

[data-theme="dark"] header {
    background: var(--bg-secondary);
}

.header-inner {
    max-width: 1100px;
    margin: 0 auto;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
}

.header-brand {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex-shrink: 0;
}

.header-brand::before {
    content: '';
    width: 3px;                          /* Was 4px - subtler */
    height: 24px;                        /* Was 28px - more proportional */
    background: var(--theme-primary);
    border-radius: 2px;
    flex-shrink: 0;
    transition: background var(--transition-fast);
}

.header-title {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
    display: flex;
    align-items: center;
}

.header-title .separator {
    color: var(--text-muted);
    margin: 0 0.5rem;
    font-weight: 400;
}

.header-subtitle,
.subtitle-separator {
    font-size: 1.0625rem;
    color: var(--theme-primary);
    font-weight: 700;
    display: none;
}

@media (min-width: 480px) {
    .header-subtitle,
    .subtitle-separator {
        display: inline;
    }
}

@media (max-width: 600px) {
    header {
        padding: 0.625rem 1rem;
        padding-top: calc(0.625rem + env(safe-area-inset-top, 0px));
    }
    
    .header-inner {
        flex-direction: column;
        align-items: stretch;
        gap: 0.625rem;
    }
    
    .header-brand {
        justify-content: center;
    }
    
    .header-brand::before {
        display: none;
    }
    
    .header-title {
        font-size: 0.95rem;
    }
}

/* -------------------------------------------
   NAV PILLS
   ------------------------------------------- */
.nav-pills {
    display: flex;
    background: var(--bg-primary);
    border-radius: var(--radius-pill);   /* Full pill shape */
    padding: 3px;                        /* Was 4px - tighter */
    gap: 2px;                            /* Was 4px - tighter */
    margin-left: auto;
    position: relative;
    border: 1px solid var(--border-color);
}

.nav-pill-indicator {
    position: absolute;
    top: 3px;
    left: 3px;
    height: calc(100% - 6px);
    background: var(--theme-primary);
    border-radius: var(--radius-pill);   /* Match container */
    box-shadow: var(--shadow-xs);
    transition: transform 380ms cubic-bezier(0.22, 1, 0.36, 1),
                width 280ms cubic-bezier(0.22, 1, 0.36, 1),
                background-color 180ms ease-out;
    will-change: transform, width, background-color;
    contain: layout style paint;
    z-index: 0;
    pointer-events: none;
}

.nav-pill-indicator.calc-das28 {
    background: var(--das28-primary);
}

.nav-pill-indicator.calc-axspa {
    background: var(--axspa-primary);
}

.nav-pill-indicator.calc-gcaps {
    background: var(--emerald-bright);
}

[data-theme="dark"] .nav-pill-indicator.calc-gcaps {
    background: var(--emerald-primary);
}

.nav-pill-indicator.calc-bvas {
    background: var(--bvas-purple);
}

/* Dropdown open preview state - neutral grey, no calculator selected */
.nav-pill-indicator.calc-preview {
    background: var(--text-muted);
}

.nav-link {
    color: var(--text-secondary);
    text-decoration: none;
    font-size: 0.875rem;                 /* Was 0.9rem - slightly smaller */
    font-weight: var(--font-semibold);
    padding: 0.5rem 1rem;
    border-radius: var(--radius-pill);
    transition: color var(--transition-base);
    cursor: pointer;
    border: none;
    background: none;
    font-family: inherit;
    position: relative;
    z-index: 1;
}

@media (any-hover: hover) {
    .nav-link:hover {
        color: var(--text-primary);
    }
}

.nav-link.active {
    color: var(--text-on-theme);
}

/* -------------------------------------------
   NAV DROPDOWN
   ------------------------------------------- */
.nav-dropdown {
    position: relative;
}

.nav-dropdown-trigger {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.dropdown-chevron {
    transition: transform var(--transition-base);
    margin-left: 0.125rem;
}

.nav-dropdown.open .dropdown-chevron {
    transform: rotate(180deg);
}

.nav-dropdown-menu {
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    min-width: 160px;
    background: var(--bg-elevated);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    opacity: 0;
    visibility: hidden;
    transform: translateY(-8px);
    transition:
        opacity var(--duration-fast) ease-out,
        transform var(--duration-fast) ease-out,
        visibility 0s var(--duration-fast);
    will-change: transform, opacity;
    contain: layout style paint;
    z-index: 100;
    padding: 6px;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.nav-dropdown.open .nav-dropdown-menu {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
    transition:
        opacity var(--duration-fast) ease-out,
        transform var(--duration-fast) ease-out,
        visibility 0s;
}

.nav-dropdown-item {
    display: flex;
    align-items: center;
    gap: 0.625rem;
    padding: 0.75rem 1rem;
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--text-primary);
    background: transparent;
    border: none;
    border-radius: var(--radius-pill);
    cursor: pointer;
    transition: background var(--transition-fast);
    text-align: left;
    width: 100%;
    font-family: inherit;
    min-height: 44px; /* Minimum touch target */
}

@media (any-hover: hover) {
    .nav-dropdown-item:hover {
        background: var(--bg-primary);
    }
}

.nav-dropdown-item.active {
    background: var(--theme-light);
    font-weight: 600;
}

/* Active indicator dot - uses each calc's color */
.dropdown-item-indicator {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: transparent;
    flex-shrink: 0;
    transition: background var(--transition-fast);
}

.nav-dropdown-item.active .dropdown-item-indicator[data-calc="das28"] {
    background: var(--das28-primary);
}

.nav-dropdown-item.active .dropdown-item-indicator[data-calc="axspa"] {
    background: var(--axspa-primary);
}

.nav-dropdown-item.active .dropdown-item-indicator[data-calc="gcaps"] {
    background: var(--emerald-bright);
}

[data-theme="dark"] .nav-dropdown-item.active .dropdown-item-indicator[data-calc="gcaps"] {
    background: var(--emerald-primary);
}

.nav-dropdown-item.active .dropdown-item-indicator[data-calc="bvas"] {
    background: var(--bvas-purple);
}

/* Dropdown backdrop for closing on tap outside (mobile) */
.nav-dropdown-backdrop {
    position: fixed;
    inset: 0;
    z-index: 150;
    background: transparent;
}

@media (max-width: 600px) {
    .nav-pills {
        margin-left: 0;
        justify-content: center;
    }
    
    .nav-link {
        flex: 1;
        text-align: center;
        padding: 0.5rem 0.75rem;
        font-size: 0.85rem;  /* Accessibility: slightly larger for mobile tap targets */
    }
    
    .nav-dropdown {
        flex: 1;
    }
    
    .nav-dropdown-trigger {
        width: 100%;
        justify-content: center;
    }
    
    .nav-dropdown-menu {
        right: 0;
        left: auto;
        transform: translateY(-8px);
        min-width: 140px;
        padding: 4px;
    }

    .nav-dropdown.open .nav-dropdown-menu {
        transform: translateY(0);
    }

    .nav-dropdown-item {
        padding: 0.75rem 1rem;
        min-height: 44px;
    }
}

/* -------------------------------------------
   THEME TOGGLE
   ------------------------------------------- */
.theme-toggle,
.mobile-theme-toggle {
    display: flex;
    gap: 0.25rem;
    background: transparent;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-pill);
    padding: 0.25rem;
    cursor: pointer;
    flex-shrink: 0;
    position: relative;
}

/* Sliding pill indicator */
.theme-toggle::before,
.mobile-theme-toggle::before {
    content: '';
    position: absolute;
    top: 0.25rem;
    left: 0.25rem;
    width: 28px;
    height: 28px;
    background: var(--theme-primary);
    border-radius: 50%;
    transition: transform var(--duration-entrance) var(--ease-out-back),
                background-color var(--transition-base);
    will-change: transform;
    z-index: 0;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
}

/* Slide to dark position (right) */
[data-theme="dark"] .theme-toggle::before,
[data-theme="dark"] .mobile-theme-toggle::before {
    transform: translateX(calc(28px + 0.25rem));
}

@media (any-hover: hover) {
    .theme-toggle:hover,
    .mobile-theme-toggle:hover {
        border-color: var(--theme-primary);
    }
    
    .theme-toggle:hover::before,
    .mobile-theme-toggle:hover::before {
        box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
    }
}

.theme-option {
    display: flex;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    transition: color var(--duration-drawer) var(--ease-out-expo),
                transform var(--transition-base);
    position: relative;
    z-index: 1;
}

/* Active icon styling - white on the pill */
.theme-option.active {
    color: var(--text-on-theme);
}

/* Subtle bounce on the active icon */
.theme-option.active svg {
    animation: iconPop var(--duration-bounce) var(--ease-out-back);
}

@keyframes iconPop {
    0% { transform: scale(0.6) rotate(-15deg); opacity: 0.5; }
    50% { transform: scale(1.1) rotate(5deg); }
    100% { transform: scale(1) rotate(0deg); opacity: 1; }
}

.theme-option svg {
    width: 16px;
    height: 16px;
}

/* Hover effect on inactive option */
@media (any-hover: hover) {
    .theme-option:not(.active):hover {
        color: var(--text-secondary);
        transform: scale(1.1);
    }
}

/* On desktop, mobile-theme-toggle is hidden (use footer-theme-toggle) */
@media (min-width: 601px) {
    .mobile-theme-toggle {
        display: none;
    }
}

@media print {
    .theme-toggle,
    .mobile-theme-toggle {
        display: none !important;
    }
}

/* Respect reduced motion preference */
@media (prefers-reduced-motion: reduce) {
    .theme-toggle::before,
    .mobile-theme-toggle::before {
        transition: none;
    }
    
    .theme-option.active svg {
        animation: none;
    }
}

/* -------------------------------------------
   MAIN LAYOUT
   ------------------------------------------- */
main {
    margin: 0;
    padding: 0;
    width: 100%;
    flex: 1;
    display: flex;
    flex-direction: column;
}

/* Page content containers - handle header offset and centering */
.page-content {
    max-width: var(--layout-narrow);     /* Uses 680px from variables.css */
    margin: 0 auto;
    padding: 2.5rem 1.5rem;
    padding-top: calc(var(--header-height) + 2.5rem);
    width: 100%;
    flex: 1;
    display: flex;
    flex-direction: column;
}

@media (min-width: 768px) {
    .page-content {
        padding: 3rem 2rem;
        padding-top: calc(var(--header-height) + 3rem);
    }

    /* Patient mode: nav pills hidden, minimal top gap */
    body.patient-mode .page-content {
        padding-top: calc(var(--header-height) + 0.25rem);
    }
}

@media (max-width: 600px) {
    .page-content {
        padding: 1.5rem 1rem;
        padding-top: calc(var(--header-height-mobile) + env(safe-area-inset-top, 0px) + 1.5rem);
    }

    /* Patient mode: nav pills hidden, minimal top gap */
    body.patient-mode .page-content {
        padding-top: calc(var(--header-height-mobile-compact) + env(safe-area-inset-top, 0px) + 0.25rem);
    }

    /* PWA patient mode: back button adds ~12px to header height */
    body.is-pwa.patient-mode .page-content {
        padding-top: calc(var(--header-height-mobile-compact) + env(safe-area-inset-top, 0px) + 0.25rem + 12px);
    }
}

/* Patient mode: remove card-header divider line and reduce spacing */
body.patient-mode .card-header {
    border-bottom: none;
    padding-bottom: 0.75rem;
    margin-bottom: 1rem;
}

/* Pages with score strip need more padding on mobile (score strip adds ~36px) */
@media (max-width: 899px) {
    [data-page="gcaps"] .page-content,
    [data-page="bvas"] .page-content,
    [data-page="axspa"] .page-content {
        padding-top: calc(var(--header-height-mobile-with-strip) + env(safe-area-inset-top, 0px) + 2rem);
    }
}

/* -------------------------------------------
   FOOTER
   ------------------------------------------- */
footer {
    max-width: var(--layout-wide);
    margin: 0 auto;
    margin-top: auto;                    /* Push to bottom of scroll content */
    text-align: center;
    width: 100%;
    font-size: 0.8125rem;
    color: var(--text-muted);
    padding: 1.5rem;
    padding-bottom: calc(1.5rem + env(safe-area-inset-bottom, 0px));
    display: flex;
    flex-wrap: wrap;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    gap: 1rem 1.5rem;
    border-top: 1px solid var(--border-subtle);
    background: var(--bg-primary);
}

footer p {
    margin: 0;
}

/* Desktop row: hospital | text | theme - hidden on mobile */
.footer-desktop-row {
    display: none;
}

/* Mobile text - shown on mobile only */
.footer-mobile-text {
    display: block;
    width: 100%;
    order: 1;
}

/* Desktop footer: centered row with all elements inline */
@media (min-width: 601px) {
    .footer-settings {
        display: none; /* Mobile settings cluster hidden on desktop */
    }
    
    .footer-mobile-text {
        display: none; /* Hide mobile text */
    }
    
    .footer-desktop-row {
        display: flex;
        align-items: center;
        gap: 0.75rem;
    }
    
    .footer-divider {
        width: 1px;
        height: 16px;
        background: var(--border-color);
    }
}

/* Mobile: settings row above text */
@media (max-width: 600px) {
    .footer-divider {
        display: none;
    }
}

footer a {
    color: var(--text-secondary);
    text-decoration: none;
    transition: color var(--transition-fast);
}

@media (any-hover: hover) {
    footer a:hover {
        color: var(--theme-primary);
    }
}

/* -------------------------------------------
   PAGE VISIBILITY & TRANSITIONS
   ------------------------------------------- */
.page-content {
    display: none;
}

.page-content.active {
    display: flex;
    flex-direction: column;
    animation: pageEnter var(--transition-slow);
}

@keyframes pageEnter {
    from {
        opacity: 0;
        transform: translateY(8px);      /* Was scale(0.98) - slide up instead */
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@media (prefers-reduced-motion: reduce) {
    .page-content.active {
        animation: none;
    }
}

/* -------------------------------------------
   SHARED MICRO-ANIMATIONS
   ------------------------------------------- */

/* Breathing animation for waiting/empty states */
@keyframes breathe {
    0%, 100% {
        transform: scale(1);
        border-color: var(--border-color);
    }
    50% {
        transform: scale(0.975);
        border-color: color-mix(in srgb, var(--border-color) 50%, transparent);
    }
}

@keyframes breatheIcon {
    0%, 100% {
        opacity: 0.35;
        transform: scale(1);
    }
    50% {
        opacity: 0.6;
        transform: scale(0.92);
    }
}

/* Fade + slide in from below */
@keyframes fadeSlideIn {
    from {
        opacity: 0;
        transform: translateY(8px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Left-to-right reveal for ticker effects */
@keyframes revealLeft {
    from {
        clip-path: inset(0 100% 0 0);
    }
    to {
        clip-path: inset(0 0 0 0);
    }
}

/* Row fade in for table stagger */
@keyframes rowFadeIn {
    from {
        opacity: 0;
        transform: translateX(-4px);
    }
    to {
        opacity: 1;
        transform: translateX(0);
    }
}

/* Icon spin (for reset/refresh actions) */
@keyframes iconSpinOnce {
    from { transform: rotate(0deg); }
    to { transform: rotate(180deg); }
}

.icon-spin {
    /* Uses custom easing for natural deceleration feel */
    animation: iconSpinOnce var(--duration-entrance) cubic-bezier(0.4, 0, 0.2, 1);
}

/* Respect reduced motion for all shared animations */
@media (prefers-reduced-motion: reduce) {
    .animate-breathe,
    .animate-breathe-icon,
    .animate-check-pop,
    .animate-fade-slide-in,
    .animate-reveal-left,
    .animate-row-stagger tr,
    .icon-spin {
        animation: none !important;
    }
}
