/* ==============================================
   Memoreel Core Design System
   Design tokens, primitives, and base components
   ============================================== */

/* ==============================================
   CSS Custom Properties (Design Tokens)
   ============================================== */

:root {
    /* ---------------------------------------------
       Typography
       --------------------------------------------- */
    --font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, monospace;
    --font-family-mono: 'Monaco', 'Menlo', monospace;

    /* Font Sizes (em-based for scalability) */
    --font-size-xs: 0.75em;     /* 12px at 16px base */
    --font-size-sm: 0.85em;     /* ~14px */
    --font-size-base: 1em;      /* 16px */
    --font-size-md: 1.1em;      /* ~18px */
    --font-size-lg: 1.2em;      /* ~19px */
    --font-size-xl: 1.4em;      /* ~22px */
    --font-size-2xl: 2em;       /* 32px */
    --font-size-3xl: 3em;       /* 48px */

    /* Font Weights */
    --font-weight-normal: 400;
    --font-weight-medium: 500;
    --font-weight-semibold: 600;

    /* Line Heights */
    --line-height-tight: 1.2;
    --line-height-base: 1.5;
    --line-height-relaxed: 1.75;

    /* ---------------------------------------------
       Spacing Scale (4px base unit)
       --------------------------------------------- */
    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 20px;
    --space-6: 24px;
    --space-8: 32px;
    --space-10: 40px;
    --space-12: 48px;
    --space-16: 64px;

    /* ---------------------------------------------
       Colors - Backgrounds
       --------------------------------------------- */
    --color-bg-base: #0c0c0e;
    --color-bg-elevated: #141416;
    --color-bg-surface: #1a1a1e;
    --color-bg-surface-hover: #252528;
    --color-bg-muted: #2a2a2e;

    /* ---------------------------------------------
       Colors - Text
       --------------------------------------------- */
    --color-text-primary: #f2f2f2;
    --color-text-secondary: #a0a0a4;
    --color-text-muted: #8a8a8e;
    --color-text-disabled: #6a6a6e;
    --color-text-subtle: #4a4a4e;

    /* ---------------------------------------------
       Colors - Brand / Accent
       --------------------------------------------- */
    --color-accent: #3CB371;
    --color-accent-bg: #1a3a2a;
    --color-accent-bg-hover: #2a4a3a;
    --color-accent-bg-active: #3a5a4a;

    /* ---------------------------------------------
       Colors - Semantic
       --------------------------------------------- */
    --color-success: #4ade80;
    --color-success-bg: #1e6b3a;
    --color-success-bg-hover: #2a8b4a;

    --color-info: #60a5fa;
    --color-info-bg: rgba(96, 165, 250, 0.15);

    --color-warning: #fbbf24;
    --color-warning-bg: rgba(251, 191, 36, 0.15);

    --color-error: #f87171;
    --color-error-bg: #6b2a2a;
    --color-error-bg-hover: #8b3a3a;

    --color-orange: #fb923c;

    /* ---------------------------------------------
       Colors - Borders & Overlays
       --------------------------------------------- */
    --color-border: #2a2a2e;
    --color-border-light: rgba(255, 255, 255, 0.04);
    --color-border-lighter: rgba(255, 255, 255, 0.06);
    --color-border-focus: var(--color-accent);

    --color-overlay: rgba(0, 0, 0, 0.8);
    --color-overlay-heavy: rgba(0, 0, 0, 0.9);
    --color-overlay-light: rgba(0, 0, 0, 0.5);

    /* ---------------------------------------------
       Shadows
       --------------------------------------------- */
    --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.5);
    --shadow-focus: 0 0 0 3px rgba(60, 179, 113, 0.2);

    /* ---------------------------------------------
       Border Radius
       --------------------------------------------- */
    --radius-sm: 4px;
    --radius-md: 6px;
    --radius-lg: 8px;
    --radius-xl: 12px;
    --radius-full: 9999px;

    /* ---------------------------------------------
       Transitions
       --------------------------------------------- */
    --transition-fast: 0.15s ease;
    --transition-base: 0.2s ease;
    --transition-slow: 0.3s ease-out;

    /* ---------------------------------------------
       Layout
       --------------------------------------------- */
    --nav-height: 56px;
    --z-content: 1;
    --z-sticky: 5;
    --z-scrubber: 10;
    --z-nav: 100;
    --z-modal: 200;
    --z-toast: 300;
}


/* ==============================================
   Glass - shared glassmorphism surface
   ============================================== */

.glass {
    background: rgba(20, 20, 22, 0.85);
    backdrop-filter: saturate(180%) blur(20px);
    -webkit-backdrop-filter: saturate(180%) blur(20px);
    border: 1px solid var(--color-border-light);
}

/* Interactive glass element with hover/press scale feedback */
@media (hover: hover) and (pointer: fine) {
    .glass-interactive:hover {
        background: rgba(30, 30, 34, 0.9);
        transform: scale(1.05);
    }

    .glass-interactive:active {
        transform: scale(0.98);
    }
}

@media (hover: none) and (pointer: coarse) {
    .glass-interactive:active {
        background: rgba(40, 40, 44, 0.95);
        transform: scale(0.9);
        transition: transform 0.06s ease-out;
    }
}


/* ==============================================
   Button Base & Variants
   ============================================== */

.core-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-4);
    border: none;
    border-radius: var(--radius-md);
    font-family: var(--font-family);
    font-size: var(--font-size-sm);
    font-weight: var(--font-weight-medium);
    cursor: pointer;
    transition: background var(--transition-base), transform var(--transition-base);
    text-decoration: none;
    touch-action: manipulation;
}

.core-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Primary Button */
.core-btn-primary {
    background: linear-gradient(to bottom, #455d7f, #3a4f6f);
    color: #fff;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 2px rgba(0, 0, 0, 0.2);
}

.core-btn-primary:hover:not(:disabled) {
    background: linear-gradient(to bottom, #506a8a, #4a6080);
}

/* Secondary Button */
.core-btn-secondary {
    background: linear-gradient(to bottom, #323236, var(--color-bg-muted));
    color: var(--color-text-primary);
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05), 0 1px 2px rgba(0, 0, 0, 0.2);
}

.core-btn-secondary:hover:not(:disabled) {
    background: linear-gradient(to bottom, #3e3e42, #3a3a3e);
}

/* Danger Button */
.core-btn-danger {
    background: linear-gradient(to bottom, #7a3535, var(--color-error-bg));
    color: #fff;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 2px rgba(0, 0, 0, 0.2);
}

.core-btn-danger:hover:not(:disabled) {
    background: linear-gradient(to bottom, #9a4545, var(--color-error-bg-hover));
}

/* Success Button */
.core-btn-success {
    background: linear-gradient(to bottom, #287a45, var(--color-success-bg));
    color: #fff;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 2px rgba(0, 0, 0, 0.2);
}

.core-btn-success:hover:not(:disabled) {
    background: linear-gradient(to bottom, #359a55, var(--color-success-bg-hover));
}

/* Outlined Button */
.core-btn-outlined {
    background: linear-gradient(to bottom, rgba(255, 255, 255, 0.02), transparent);
    border: 1px solid var(--color-border);
    color: var(--color-text-secondary);
}

.core-btn-outlined:hover:not(:disabled) {
    background: linear-gradient(to bottom, rgba(255, 255, 255, 0.05), rgba(255, 255, 255, 0.02));
    border-color: var(--color-text-muted);
    color: var(--color-text-primary);
}

/* Ghost Button (no background) */
.core-btn-ghost {
    background: transparent;
    color: var(--color-text-secondary);
}

.core-btn-ghost:hover:not(:disabled) {
    background: rgba(255, 255, 255, 0.08);
    color: var(--color-text-primary);
}

/* Pill Shape Modifier */
.core-btn-pill {
    border-radius: var(--radius-full);
}

/* Small Size Modifier */
.core-btn-small {
    padding: var(--space-1) var(--space-3);
    font-size: var(--font-size-xs);
}

/* Icon-Only Button */
.core-btn-icon {
    width: 44px;
    height: 44px;
    padding: 0;
    border-radius: var(--radius-md);
}

.core-btn-icon.core-btn-small {
    width: 36px;
    height: 36px;
}

/* Touch devices - press feedback */
@media (hover: none) and (pointer: coarse) {
    .core-btn:active:not(:disabled) {
        transform: scale(0.96);
        transition: transform 0.06s ease-out;
    }

    .core-btn-primary:active:not(:disabled) {
        background: linear-gradient(to bottom, #3a4f6f, #2f4460);
    }

    .core-btn-secondary:active:not(:disabled) {
        background: linear-gradient(to bottom, #2a2a2e, var(--color-bg-muted));
    }

    .core-btn-danger:active:not(:disabled) {
        background: linear-gradient(to bottom, #6a2a2a, var(--color-error-bg));
    }

    .core-btn-success:active:not(:disabled) {
        background: linear-gradient(to bottom, #1f6535, var(--color-success-bg));
    }

    .core-btn-outlined:active:not(:disabled) {
        background: rgba(255, 255, 255, 0.08);
    }

    .core-btn-ghost:active:not(:disabled) {
        background: rgba(255, 255, 255, 0.12);
    }
}

/* Keyboard focus indicator */
.core-btn:focus-visible {
    outline: 2px solid var(--color-accent);
    outline-offset: 2px;
}


/* ==============================================
   Input Base & Variants
   ============================================== */

.core-input {
    width: 100%;
    padding: var(--space-3);
    background: var(--color-bg-base);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    color: var(--color-text-primary);
    font-family: var(--font-family);
    font-size: var(--font-size-base);
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}

.core-input::placeholder {
    color: var(--color-text-disabled);
}

.core-input:focus {
    outline: none;
    border-color: var(--color-accent);
    box-shadow: var(--shadow-focus);
}

.core-input:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Search Input Wrapper */
.core-search {
    position: relative;
    display: flex;
    align-items: center;
}

.core-search-icon {
    position: absolute;
    left: var(--space-3);
    color: var(--color-text-disabled);
    pointer-events: none;
}

.core-search .core-input {
    padding-left: calc(var(--space-3) + 20px + var(--space-2));
    padding-right: calc(var(--space-3) + 20px + var(--space-2));
}

.core-search-clear {
    position: absolute;
    right: var(--space-2);
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    border-radius: var(--radius-sm);
    color: var(--color-text-disabled);
    cursor: pointer;
    opacity: 0;
    transition: opacity var(--transition-fast), color var(--transition-fast);
}

.core-search-clear.visible {
    opacity: 1;
}

.core-search-clear:hover {
    color: var(--color-text-primary);
    background: rgba(255, 255, 255, 0.08);
}

/* Select Dropdown */
.core-select {
    appearance: none;
    padding-right: calc(var(--space-3) + 20px);
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%238a8a8e' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right var(--space-3) center;
    cursor: pointer;
}


/* ==============================================
   Checkbox Component
   ============================================== */

.core-checkbox {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 20px;
    height: 20px;
    flex-shrink: 0;
}

.core-checkbox input[type="checkbox"],
.core-checkbox input[type="radio"] {
    position: absolute;
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    opacity: 0;
    cursor: pointer;
    z-index: 1;
}

.core-checkbox-box {
    width: 18px;
    height: 18px;
    border: 2px solid var(--color-text-muted);
    border-radius: var(--radius-sm);
    background: transparent;
    transition: all var(--transition-fast);
    display: flex;
    align-items: center;
    justify-content: center;
}

.core-checkbox input:checked + .core-checkbox-box {
    background: var(--color-accent);
    border-color: var(--color-accent);
}

.core-checkbox-box svg {
    width: 12px;
    height: 12px;
    stroke: #fff;
    stroke-width: 3;
    opacity: 0;
    transform: scale(0.5);
    transition: all var(--transition-fast);
}

.core-checkbox input:checked + .core-checkbox-box svg {
    opacity: 1;
    transform: scale(1);
}

.core-checkbox input:indeterminate + .core-checkbox-box {
    background: var(--color-accent);
    border-color: var(--color-accent);
}

.core-checkbox input:indeterminate + .core-checkbox-box svg {
    opacity: 0;
}

.core-checkbox input:indeterminate + .core-checkbox-box::after {
    content: '';
    position: absolute;
    width: 8px;
    height: 2px;
    background: #fff;
    border-radius: 1px;
}

.core-checkbox input:focus-visible + .core-checkbox-box {
    outline: 2px solid var(--color-accent);
    outline-offset: 2px;
}

/* Hover state - desktop only */
@media (hover: hover) and (pointer: fine) {
    .core-checkbox:hover .core-checkbox-box {
        border-color: var(--color-text-secondary);
    }

    .core-checkbox input:checked:hover + .core-checkbox-box {
        background: var(--color-accent-bg-hover);
        border-color: var(--color-accent);
    }
}


/* ==============================================
   Tabs Component
   ============================================== */

.core-tabs {
    display: flex;
    gap: var(--space-1);
    padding: 0 var(--space-1);
}

.core-tab {
    padding: var(--space-3) var(--space-6);
    background: transparent;
    border: none;
    border-bottom: 3px solid transparent;
    color: var(--color-text-muted);
    font-family: var(--font-family);
    font-size: var(--font-size-base);
    font-weight: var(--font-weight-medium);
    cursor: pointer;
    transition: all var(--transition-base);
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    touch-action: manipulation;
}

/* Desktop hover */
@media (hover: hover) and (pointer: fine) {
    .core-tab:hover {
        background: rgba(255, 255, 255, 0.05);
        color: var(--color-text-primary);
    }
}

/* Touch feedback */
@media (hover: none) and (pointer: coarse) {
    .core-tab:active {
        background: rgba(255, 255, 255, 0.08);
        transform: scale(0.98);
        transition: transform 0.06s ease-out;
    }
}

.core-tab:focus-visible {
    outline: 2px solid var(--color-accent);
    outline-offset: -2px;
}

.core-tab.active {
    background: var(--color-bg-elevated);
    color: var(--color-accent);
    border-bottom-color: var(--color-accent);
}

.core-tab-content {
    border-radius: 0 var(--radius-lg) var(--radius-lg) var(--radius-lg);
}

/* Compact tabs for title bar */
.core-tabs-compact .core-tab {
    padding: var(--space-2) var(--space-3);
    font-size: var(--font-size-sm);
    border-radius: var(--radius-md) var(--radius-md) 0 0;
}


/* ==============================================
   Chip & Badge Components
   ============================================== */

.core-chip {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
    background: var(--color-bg-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-full);
    color: var(--color-text-secondary);
    font-family: var(--font-family);
    font-size: var(--font-size-sm);
    cursor: pointer;
    transition: all var(--transition-fast);
    touch-action: manipulation;
}

.core-chip:hover {
    background: var(--color-bg-surface-hover);
    border-color: var(--color-text-muted);
    color: var(--color-text-primary);
}

.core-chip.selected {
    background: var(--color-accent-bg);
    border-color: var(--color-accent);
    color: var(--color-accent);
}

.core-chip:focus-visible {
    outline: 2px solid var(--color-accent);
    outline-offset: 2px;
}

/* Touch feedback */
@media (hover: none) and (pointer: coarse) {
    .core-chip:active {
        transform: scale(0.96);
        transition: transform 0.06s ease-out;
    }
}

/* Badge - notification indicator */
.core-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 20px;
    height: 20px;
    padding: 0 var(--space-2);
    background: var(--color-accent);
    border-radius: var(--radius-full);
    color: #fff;
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-semibold);
}

.core-badge-small {
    min-width: 16px;
    height: 16px;
    padding: 0 var(--space-1);
    font-size: 10px;
}

.core-badge-dot {
    width: 8px;
    height: 8px;
    min-width: 8px;
    padding: 0;
}

/* Badge variants */
.core-badge-success {
    background: var(--color-success);
}

.core-badge-warning {
    background: var(--color-warning);
    color: #000;
}

.core-badge-error {
    background: var(--color-error);
}

.core-badge-muted {
    background: var(--color-bg-muted);
    color: var(--color-text-secondary);
}


/* ==============================================
   Spinner & Skeleton Loading
   ============================================== */

.core-spinner {
    width: 40px;
    height: 40px;
    border: 3px solid var(--color-bg-muted);
    border-top-color: var(--color-accent);
    border-radius: 50%;
    animation: core-spin 1s linear infinite;
}

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

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

@keyframes core-spin {
    to { transform: rotate(360deg); }
}

/* Skeleton loading placeholder */
.core-skeleton {
    background: linear-gradient(
        90deg,
        var(--color-bg-surface) 0%,
        var(--color-bg-muted) 50%,
        var(--color-bg-surface) 100%
    );
    background-size: 200% 100%;
    animation: core-shimmer 1.5s ease-in-out infinite;
    border-radius: var(--radius-md);
}

.core-skeleton-text {
    height: 1em;
    border-radius: var(--radius-sm);
}

.core-skeleton-circle {
    border-radius: 50%;
}

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


/* ==============================================
   Person Card Component
   ============================================== */

/* Grid Layout - responsive columns with max 6 */
.core-person-card-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: var(--space-2);
    align-content: start; /* Pack rows at top when container has extra height */
}

@media (min-width: 480px) {
    .core-person-card-grid {
        grid-template-columns: repeat(3, 1fr);
    }
}

@media (min-width: 640px) {
    .core-person-card-grid {
        grid-template-columns: repeat(5, 1fr);
    }
}

@media (min-width: 900px) {
    .core-person-card-grid {
        grid-template-columns: repeat(6, 1fr);
    }
}

@media (min-width: 1100px) {
    .core-person-card-grid {
        grid-template-columns: repeat(8, 1fr);
    }
}

/* Card */
.core-person-card {
    background: var(--color-bg-surface);
    border-radius: var(--radius-lg);
    cursor: pointer;
    overflow: hidden;
    transition: background var(--transition-base), transform var(--transition-base);
}

.core-person-card:hover {
    background: var(--color-bg-surface-hover);
}

.core-person-card:active {
    background: var(--color-bg-muted);
}

/* Touch devices - press feedback */
@media (hover: none) and (pointer: coarse) {
    .core-person-card:active {
        transform: scale(0.98);
        transition: transform 0.06s ease-out;
    }
}

/* Image wrapper - provides reliable aspect-ratio container */
.core-person-card-image-wrapper {
    position: relative;
    width: 100%;
    aspect-ratio: 1.0;
    background: var(--color-bg-muted);
}

/* Image fills wrapper absolutely */
.core-person-card-image {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* Info section */
.core-person-card-info {
    padding: var(--space-3) var(--space-4);
    text-align: left;
}

.core-person-card-name {
    position: relative;
    top: 1px;
    font-weight: var(--font-weight-semibold);
    font-size: var(--font-size-sm);
    color: var(--color-text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.core-person-card-count {
    position: relative;
    top: -1px;
    margin-top: var(--space-1);
    font-size: var(--font-size-xs);
    color: var(--color-text-secondary);
}


/* ==============================================
   Utility Classes
   ============================================== */

/* Typography */
.core-text-xs { font-size: var(--font-size-xs); }
.core-text-sm { font-size: var(--font-size-sm); }
.core-text-base { font-size: var(--font-size-base); }
.core-text-md { font-size: var(--font-size-md); }
.core-text-lg { font-size: var(--font-size-lg); }
.core-text-xl { font-size: var(--font-size-xl); }
.core-text-2xl { font-size: var(--font-size-2xl); }

.core-font-normal { font-weight: var(--font-weight-normal); }
.core-font-medium { font-weight: var(--font-weight-medium); }
.core-font-semibold { font-weight: var(--font-weight-semibold); }

.core-text-primary { color: var(--color-text-primary); }
.core-text-secondary { color: var(--color-text-secondary); }
.core-text-muted { color: var(--color-text-muted); }
.core-text-disabled { color: var(--color-text-disabled); }
.core-text-accent { color: var(--color-accent); }
.core-text-success { color: var(--color-success); }
.core-text-warning { color: var(--color-warning); }
.core-text-error { color: var(--color-error); }


/* ==============================================
   Reduced Motion Support
   ============================================== */

@media (prefers-reduced-motion: reduce) {
    .core-spinner,
    .core-skeleton {
        animation: none;
    }

    .core-btn,
    .core-tab,
    .core-chip {
        transition: none;
    }
}
