/**
 * Principia Metaphysica - UX/UI Consistency Enhancements
 * =======================================================
 *
 * Comprehensive UX polish for consistent, professional appearance:
 * - Dark theme consistency
 * - Loading/error/empty states
 * - Dropdown and form controls
 * - Button standardization
 * - Hover and focus states
 * - Panel and background fixes
 * - Accessibility improvements
 *
 * Copyright (c) 2025-2026 Andrew Keith Watts. All rights reserved.
 */

/* ============================================
   ENHANCED CSS VARIABLES
   ============================================ */

:root {
    /* Background layers - proper dark theme */
    --bg-base: #0a0a0f;
    --bg-elevated: #1a1a2e;
    --bg-panel: #16213e;
    --bg-card: rgba(26, 31, 58, 0.8);
    --bg-hover: rgba(139, 127, 255, 0.1);
    --bg-active: rgba(139, 127, 255, 0.2);

    /* Text hierarchy */
    --text-primary: #f8f9fa;
    --text-secondary: rgba(255, 255, 255, 0.85);
    --text-muted: rgba(255, 255, 255, 0.6);
    --text-disabled: rgba(255, 255, 255, 0.4);

    /* Interactive elements */
    --accent-primary: #8b7fff;
    --accent-hover: #a394ff;
    --accent-active: #7565e8;
    --accent-secondary: #ff7eb6;

    /* Semantic colors */
    --success: #51cf66;
    --success-bg: rgba(81, 207, 102, 0.15);
    --warning: #ffd43b;
    --warning-bg: rgba(255, 212, 59, 0.15);
    --error: #f87171;
    --error-bg: rgba(248, 113, 113, 0.15);
    --info: #60a5fa;
    --info-bg: rgba(96, 165, 250, 0.15);

    /* Borders and dividers */
    --border-subtle: rgba(255, 255, 255, 0.08);
    --border-default: rgba(255, 255, 255, 0.12);
    --border-strong: rgba(255, 255, 255, 0.2);
    --border-accent: rgba(139, 127, 255, 0.3);

    /* Shadows */
    --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.2);
    --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.4);
    --shadow-xl: 0 16px 48px rgba(0, 0, 0, 0.5);

    /* Timing */
    --duration-fast: 150ms;
    --duration-normal: 250ms;
    --duration-slow: 350ms;
    --easing: cubic-bezier(0.4, 0, 0.2, 1);
}

/* ============================================
   FIX WHITE BACKGROUNDS - DARK THEME ENFORCEMENT
   ============================================ */

/* Force dark backgrounds on all panels and containers */
.panel,
.content-panel,
.section-panel,
.data-panel,
.paper-container,
.content-container,
.main-content {
    background: var(--bg-elevated) !important;
    color: var(--text-primary) !important;
}

/* Cards should have proper glass morphism */
.card,
.info-card,
.parameter-card,
.formula-card,
.section-card {
    background: var(--bg-card) !important;
    backdrop-filter: blur(16px) saturate(180%);
    -webkit-backdrop-filter: blur(16px) saturate(180%);
    border: 1px solid var(--border-default);
    border-radius: 12px;
    color: var(--text-primary) !important;
}

/* Paper content areas - should be white only when explicitly marked as .paper */
body:not(.paper) .content-area,
body:not(.paper) .text-content,
body:not(.paper) .section-content {
    background: transparent !important;
    color: var(--text-primary) !important;
}

/* NOTE: Removed overly broad rule that was causing black backgrounds
   on transparent elements. Background should be set explicitly on
   containers that need it, not forced on all elements. */

/* Ensure panels extend to edges properly */
.panel,
.section-panel {
    margin: 0 -1rem;
    padding: 2rem 1rem;
    border-radius: 0;
}

@media (min-width: 768px) {
    .panel,
    .section-panel {
        margin: 0;
        padding: 2rem;
        border-radius: 12px;
    }
}

/* ============================================
   LOADING STATES - SPINNERS & SKELETONS
   ============================================ */

/* Loading spinner */
.loading-spinner {
    display: inline-block;
    width: 40px;
    height: 40px;
    border: 3px solid var(--border-default);
    border-top-color: var(--accent-primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

.loading-spinner.small {
    width: 20px;
    height: 20px;
    border-width: 2px;
}

.loading-spinner.large {
    width: 60px;
    height: 60px;
    border-width: 4px;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* Loading container */
.loading-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    padding: 3rem 1rem;
    min-height: 200px;
}

.loading-text {
    color: var(--text-muted);
    font-size: 0.95rem;
    animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 0.6; }
    50% { opacity: 1; }
}

/* Skeleton loader for cards */
.skeleton {
    background: linear-gradient(
        90deg,
        var(--bg-elevated) 0%,
        var(--bg-hover) 50%,
        var(--bg-elevated) 100%
    );
    background-size: 200% 100%;
    animation: skeleton-shimmer 1.5s ease-in-out infinite;
    border-radius: 8px;
}

.skeleton-text {
    height: 1em;
    margin-bottom: 0.5rem;
}

.skeleton-title {
    height: 1.5em;
    width: 60%;
    margin-bottom: 1rem;
}

.skeleton-card {
    height: 200px;
    border-radius: 12px;
}

@keyframes skeleton-shimmer {
    0% { background-position: -200% 0; }
    100% { background-position: 200% 0; }
}

/* ============================================
   ERROR STATES - HELPFUL MESSAGES
   ============================================ */

.error-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    padding: 3rem 2rem;
    text-align: center;
    min-height: 200px;
}

.error-icon {
    width: 64px;
    height: 64px;
    background: var(--error-bg);
    border: 2px solid var(--error);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2rem;
    color: var(--error);
}

.error-title {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--error);
    margin: 0;
}

.error-message {
    color: var(--text-secondary);
    font-size: 0.95rem;
    max-width: 500px;
    line-height: 1.6;
}

.error-action {
    margin-top: 0.5rem;
}

/* Inline error messages */
.field-error {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1rem;
    background: var(--error-bg);
    border: 1px solid var(--error);
    border-radius: 8px;
    color: var(--error);
    font-size: 0.9rem;
    margin-top: 0.5rem;
}

/* ============================================
   EMPTY STATES - WHEN NO DATA
   ============================================ */

.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    padding: 4rem 2rem;
    text-align: center;
    min-height: 300px;
}

.empty-icon {
    width: 80px;
    height: 80px;
    background: var(--bg-hover);
    border: 2px dashed var(--border-strong);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2.5rem;
    color: var(--text-muted);
    opacity: 0.6;
}

.empty-title {
    font-size: 1.35rem;
    font-weight: 600;
    color: var(--text-primary);
    margin: 0;
}

.empty-message {
    color: var(--text-muted);
    font-size: 1rem;
    max-width: 400px;
    line-height: 1.7;
}

.empty-action {
    margin-top: 1rem;
}

/* ============================================
   BUTTONS - STANDARDIZED STYLES
   ============================================ */

/* Base button */
button,
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.75rem 1.5rem;
    font-size: 0.95rem;
    font-weight: 500;
    font-family: 'Source Sans Pro', sans-serif;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: all var(--duration-normal) var(--easing);
    position: relative;
    overflow: hidden;
    white-space: nowrap;
}

/* Primary button */
.btn-primary,
button:not([class]) {
    background: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
    color: #fff;
    box-shadow: var(--shadow-sm);
}

.btn-primary:hover,
button:not([class]):hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
    filter: brightness(1.1);
}

.btn-primary:active,
button:not([class]):active {
    transform: translateY(0);
    box-shadow: var(--shadow-sm);
}

/* Secondary button */
.btn-secondary {
    background: var(--bg-hover);
    border: 1px solid var(--border-accent);
    color: var(--accent-primary);
}

.btn-secondary:hover {
    background: var(--bg-active);
    border-color: var(--accent-hover);
    color: var(--accent-hover);
}

/* Outline button */
.btn-outline {
    background: transparent;
    border: 1px solid var(--border-default);
    color: var(--text-primary);
}

.btn-outline:hover {
    background: var(--bg-hover);
    border-color: var(--border-strong);
}

/* Ghost button */
.btn-ghost {
    background: transparent;
    border: none;
    color: var(--text-secondary);
}

.btn-ghost:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

/* Small button */
.btn-sm {
    padding: 0.5rem 1rem;
    font-size: 0.875rem;
}

/* Large button */
.btn-lg {
    padding: 1rem 2rem;
    font-size: 1.05rem;
}

/* Full width button */
.btn-block {
    width: 100%;
    display: flex;
}

/* Disabled button */
button:disabled,
.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    pointer-events: none;
}

/* Button with loading state */
.btn.loading {
    position: relative;
    color: transparent;
    pointer-events: none;
}

.btn.loading::after {
    content: '';
    position: absolute;
    width: 16px;
    height: 16px;
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-top-color: #fff;
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}

/* ============================================
   FORM CONTROLS - INPUTS, SELECTS, TEXTAREAS
   ============================================ */

/* Input fields */
input[type="text"],
input[type="email"],
input[type="password"],
input[type="search"],
input[type="number"],
textarea,
select {
    width: 100%;
    padding: 0.75rem 1rem;
    font-size: 0.95rem;
    font-family: 'Source Sans Pro', sans-serif;
    background: var(--bg-elevated);
    border: 1px solid var(--border-default);
    border-radius: 8px;
    color: var(--text-primary);
    transition: all var(--duration-normal) var(--easing);
}

input:focus,
textarea:focus,
select:focus {
    outline: none;
    border-color: var(--accent-primary);
    box-shadow: 0 0 0 3px rgba(139, 127, 255, 0.1);
}

input::placeholder,
textarea::placeholder {
    color: var(--text-muted);
}

/* Input with icon */
.input-group {
    position: relative;
    display: flex;
    align-items: center;
}

.input-icon {
    position: absolute;
    left: 1rem;
    color: var(--text-muted);
    pointer-events: none;
}

.input-group input {
    padding-left: 3rem;
}

/* ============================================
   DROPDOWNS - DARK BACKGROUND, WHITE TEXT
   ============================================ */

/* Select dropdown */
select {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%238b7fff' d='M6 9L1 4h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 1rem center;
    padding-right: 3rem;
    cursor: pointer;
}

select option {
    background: var(--bg-elevated);
    color: var(--text-primary);
    padding: 0.75rem 1rem;
}

/* Custom dropdown */
.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-toggle {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1rem;
    background: var(--bg-elevated);
    border: 1px solid var(--border-default);
    border-radius: 8px;
    color: var(--text-primary);
    cursor: pointer;
    transition: all var(--duration-normal) var(--easing);
}

.dropdown-toggle:hover {
    background: var(--bg-hover);
    border-color: var(--border-strong);
}

.dropdown-menu {
    position: absolute;
    top: 100%;
    left: 0;
    margin-top: 0.5rem;
    min-width: 200px;
    background: var(--bg-elevated);
    border: 1px solid var(--border-default);
    border-radius: 8px;
    box-shadow: var(--shadow-lg);
    opacity: 0;
    visibility: hidden;
    transform: translateY(-10px);
    transition: all var(--duration-normal) var(--easing);
    z-index: 1000;
    overflow: hidden;
}

.dropdown.active .dropdown-menu,
.dropdown:hover .dropdown-menu {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.dropdown-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem 1rem;
    color: var(--text-primary);
    text-decoration: none;
    transition: all var(--duration-fast) var(--easing);
    cursor: pointer;
}

.dropdown-item:hover {
    background: var(--bg-hover);
    color: var(--accent-primary);
}

.dropdown-divider {
    height: 1px;
    background: var(--border-subtle);
    margin: 0.5rem 0;
}

/* ============================================
   HOVER STATES - CONSISTENT ACROSS ALL ELEMENTS
   ============================================ */

/* Links */
a {
    color: var(--accent-primary);
    text-decoration: none;
    transition: all var(--duration-fast) var(--easing);
}

a:hover {
    color: var(--accent-hover);
    text-decoration: underline;
}

/* Cards */
.card:hover,
.info-card:hover,
.parameter-card:hover,
.formula-card:hover,
.section-card:hover {
    transform: translateY(-4px);
    box-shadow: var(--shadow-lg);
    border-color: var(--border-accent);
}

/* Interactive elements */
.interactive:hover,
.clickable:hover {
    cursor: pointer;
    background: var(--bg-hover);
}

/* Tags and badges */
.tag:hover,
.badge:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-sm);
}

/* ============================================
   FOCUS STATES - ACCESSIBILITY
   ============================================ */

/* Keyboard focus indicator */
*:focus-visible {
    outline: 2px solid var(--accent-primary);
    outline-offset: 2px;
    border-radius: 4px;
}

/* Remove outline for mouse users */
*:focus:not(:focus-visible) {
    outline: none;
}

/* Focus within containers */
.card:focus-within,
.panel:focus-within {
    border-color: var(--border-accent);
    box-shadow: 0 0 0 3px rgba(139, 127, 255, 0.1);
}

/* Skip to content link */
.skip-link {
    position: absolute;
    top: -100px;
    left: 0;
    background: var(--accent-primary);
    color: #fff;
    padding: 0.75rem 1.5rem;
    border-radius: 0 0 8px 0;
    z-index: 10000;
    transition: top var(--duration-normal) var(--easing);
}

.skip-link:focus {
    top: 0;
    outline: 3px solid #fff;
    outline-offset: 2px;
}

/* ============================================
   CONTRAST FIXES - READABILITY
   ============================================ */

/* Ensure sufficient contrast */
.text-on-dark {
    color: var(--text-primary);
}

.text-muted-accessible {
    color: rgba(255, 255, 255, 0.75); /* WCAG AA compliant */
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    :root {
        --text-primary: #ffffff;
        --text-secondary: #e0e0e0;
        --border-default: rgba(255, 255, 255, 0.3);
        --border-strong: rgba(255, 255, 255, 0.5);
    }

    .card,
    .panel {
        border-width: 2px;
    }
}

/* ============================================
   Z-INDEX MANAGEMENT
   ============================================ */

.z-base { z-index: 0; }
.z-dropdown { z-index: 1000; }
.z-sticky { z-index: 1020; }
.z-fixed { z-index: 1030; }
.z-modal-backdrop { z-index: 1040; }
.z-modal { z-index: 1050; }
.z-popover { z-index: 1060; }
.z-tooltip { z-index: 1070; }

/* Header should be above content but below modals */
header,
.pm-header {
    z-index: 1020 !important;
}

/* Tooltips should be above everything except modals */
.tooltip,
.pm-tooltip,
.var-tooltip {
    z-index: 1070 !important;
}

/* Modals should be on top */
.modal,
.auth-overlay {
    z-index: 1050 !important;
}

/* ============================================
   RESPONSIVE DESIGN
   ============================================ */

@media (max-width: 768px) {
    /* Larger touch targets */
    button,
    .btn,
    a,
    input,
    select {
        min-height: 44px;
        min-width: 44px;
    }

    /* Adjust padding */
    .panel,
    .card {
        padding: 1rem;
    }

    /* Full width buttons on mobile */
    .btn-responsive {
        width: 100%;
    }

    /* Stack dropdowns */
    .dropdown-menu {
        left: 0;
        right: 0;
        width: 100%;
    }
}

/* ============================================
   UTILITY CLASSES
   ============================================ */

/* Display */
.hidden { display: none !important; }
.block { display: block !important; }
.flex { display: flex !important; }
.inline-flex { display: inline-flex !important; }

/* Spacing */
.m-0 { margin: 0 !important; }
.mt-1 { margin-top: 1rem !important; }
.mb-1 { margin-bottom: 1rem !important; }
.p-0 { padding: 0 !important; }
.p-1 { padding: 1rem !important; }

/* Text alignment */
.text-center { text-align: center !important; }
.text-left { text-align: left !important; }
.text-right { text-align: right !important; }

/* Colors */
.text-primary { color: var(--text-primary) !important; }
.text-secondary { color: var(--text-secondary) !important; }
.text-muted { color: var(--text-muted) !important; }
.text-accent { color: var(--accent-primary) !important; }
.text-success { color: var(--success) !important; }
.text-warning { color: var(--warning) !important; }
.text-error { color: var(--error) !important; }

/* Backgrounds */
.bg-elevated { background: var(--bg-elevated) !important; }
.bg-panel { background: var(--bg-panel) !important; }
.bg-card { background: var(--bg-card) !important; }

/* Borders */
.border { border: 1px solid var(--border-default) !important; }
.border-0 { border: none !important; }
.rounded { border-radius: 8px !important; }
.rounded-lg { border-radius: 12px !important; }

/* ============================================
   ANIMATIONS
   ============================================ */

/* Fade in */
@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.fade-in {
    animation: fadeIn var(--duration-normal) var(--easing);
}

/* Slide in from right */
@keyframes slideInRight {
    from {
        opacity: 0;
        transform: translateX(20px);
    }
    to {
        opacity: 1;
        transform: translateX(0);
    }
}

.slide-in-right {
    animation: slideInRight var(--duration-normal) var(--easing);
}

/* Reduce motion for accessibility */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* ============================================
   PRINT STYLES
   ============================================ */

@media print {
    .loading-spinner,
    .loading-container,
    .error-container,
    .empty-state,
    button,
    .btn {
        display: none !important;
    }

    .card,
    .panel {
        border: 1px solid #000 !important;
        box-shadow: none !important;
        background: #fff !important;
        color: #000 !important;
    }
}
