/**
 * Diachronica Research Tools - Shared Styles
 *
 * Common components and patterns for concordance, analysis, and etymology tools.
 * Based on Etymology Visualizer design system.
 *
 * Author: Luke Steuber
 */

/* ==================== DESIGN TOKENS ==================== */

:root {
    /* Colors - Light Mode */
    --primary: #2C2416;
    --secondary: #5C5444;
    --accent: #8B4513;
    --bg: #FFFEF7;
    --bg-secondary: #F5F0E6;
    --border: #E8E0D0;
    --hover: #F5F0E6;
    --card-bg: #FFFFFE;
    --text: #2C2416;
    --text-secondary: #5C5444;
    --input-bg: #FFFFFE;
    --input-border: #E8E0D0;

    /* Typography */
    --serif: 'Crimson Pro', Georgia, serif;
    --sans: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    --mono: 'Courier New', Courier, monospace;

    /* Spacing */
    --space-xs: 8px;
    --space-sm: 16px;
    --space-md: 24px;
    --space-lg: 32px;
    --space-xl: 48px;
    --space-2xl: 64px;
    --space-3xl: 96px;

    /* Border Radius */
    --radius-sm: 6px;
    --radius-md: 8px;
    --radius-lg: 12px;
    --radius-xl: 16px;
    --radius-pill: 50px;

    /* Shadows */
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.08);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.12);

    /* Transitions */
    --transition-fast: 150ms ease;
    --transition-base: 300ms ease;
    --transition-slow: 500ms ease;

    /* Layout */
    --content-max-width: 900px;
    --content-wide: 1200px;
}

/* ==================== BUTTONS ==================== */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: var(--space-sm) var(--space-lg);
    background: var(--accent);
    color: #ffffff;
    border: none;
    border-radius: var(--radius-pill);
    font-family: var(--sans);
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition-base);
    box-shadow: var(--shadow-sm);
}

.btn:hover {
    background: var(--primary);
    box-shadow: var(--shadow-md);
    transform: translateY(-1px);
}

.btn:active {
    transform: translateY(0);
    box-shadow: var(--shadow-sm);
}

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

.btn-outline {
    background: transparent;
    color: var(--accent);
    border: 2px solid var(--accent);
}

.btn-outline:hover {
    background: var(--accent);
    color: #ffffff;
}

.btn-small {
    padding: var(--space-xs) var(--space-md);
    font-size: 0.875rem;
}

/* ==================== INPUTS & FORMS ==================== */

.input {
    width: 100%;
    padding: var(--space-sm) var(--space-md);
    border: 2px solid var(--input-border);
    border-radius: var(--radius-pill);
    font-family: var(--sans);
    font-size: 1rem;
    background: var(--input-bg);
    color: var(--text);
    transition: all var(--transition-fast);
}

.input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(139, 69, 19, 0.1);
}

.input::placeholder {
    color: var(--secondary);
}

.search-form {
    display: flex;
    gap: var(--space-sm);
    align-items: center;
}

/* ==================== CARDS ==================== */

.card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: var(--space-lg);
    box-shadow: var(--shadow-sm);
}

.card-header {
    border-bottom: 1px solid var(--border);
    padding-bottom: var(--space-sm);
    margin-bottom: var(--space-md);
}

.card-title {
    font-size: 1.25rem;
    font-weight: 600;
    margin-bottom: 0;
}

.section {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: var(--space-lg);
    margin-bottom: var(--space-lg);
    box-shadow: var(--shadow-sm);
}

.section-header {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    padding-bottom: var(--space-sm);
    margin-bottom: var(--space-md);
    border-bottom: 1px solid var(--border);
}

.section-title {
    font-family: var(--serif);
    font-size: 1.25rem;
    font-weight: 600;
    margin: 0;
}

/* ==================== KWIC CONCORDANCE DISPLAY ==================== */

.kwic-table {
    width: 100%;
    border-collapse: collapse;
}

.kwic-line {
    font-family: var(--mono);
    font-size: 0.9rem;
    line-height: 2;
    padding: var(--space-xs) 0;
    border-bottom: 1px solid var(--border);
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    gap: var(--space-sm);
}

.kwic-left {
    text-align: right;
    color: var(--secondary);
}

.kwic-keyword {
    font-weight: 600;
    color: var(--accent);
    white-space: nowrap;
}

.kwic-right {
    text-align: left;
    color: var(--secondary);
}

.kwic-meta {
    text-align: right;
    padding-left: var(--space-md);
}

/* ==================== HIGHLIGHTING ==================== */

.highlight,
mark.keyword {
    background: #FFEB3B;
    color: #000;
    font-weight: 600;
    padding: 0 4px;
    border-radius: var(--radius-sm);
}

mark.context {
    background: rgba(139, 69, 19, 0.1);
    color: var(--text);
}

/* ==================== METADATA & TAGS ==================== */

.meta-tag {
    font-size: 0.8125rem; /* 13px - improved readability */
    padding: 4px var(--space-sm);
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius-pill);
    font-weight: 500;
}

.meta-row {
    display: flex;
    gap: var(--space-xs);
    flex-wrap: wrap;
    margin-top: var(--space-sm);
}

/* ==================== LOADING & ERROR STATES ==================== */

.loading-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: var(--space-2xl);
    text-align: center;
    color: var(--text-secondary);
}

.spinner,
.loading {
    display: inline-block;
    width: 48px;
    height: 48px;
    border: 4px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

.loading-state p {
    margin-top: var(--space-md);
    font-size: 1rem;
}

.error-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: var(--space-2xl);
    text-align: center;
    color: var(--text);
}

.error-icon {
    font-size: 3rem;
    margin-bottom: var(--space-md);
}

.error-state h3 {
    color: var(--text);
    margin-bottom: var(--space-sm);
}

.error-details {
    color: var(--text-secondary);
    font-size: 0.875rem;
    margin-top: var(--space-xs);
}

.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: var(--space-2xl);
    text-align: center;
    color: var(--text-secondary);
}

.empty-icon {
    font-size: 3rem;
    margin-bottom: var(--space-md);
}

.empty-state p {
    font-size: 1rem;
}

/* ==================== NOTIFICATIONS ==================== */

.toast-container {
    position: fixed;
    bottom: var(--space-lg);
    right: var(--space-lg);
    z-index: 9999;
    display: flex;
    flex-direction: column;
    gap: var(--space-sm);
}

.toast {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: var(--space-md) var(--space-lg);
    box-shadow: var(--shadow-lg);
    min-width: 300px;
    max-width: 500px;
    animation: slideIn 0.3s ease;
}

.toast-info {
    border-left: 4px solid var(--accent);
}

.toast-success {
    border-left: 4px solid #2e7d32;
}

.toast-warning {
    border-left: 4px solid #ed6c02;
}

.toast-error {
    border-left: 4px solid #d32f2f;
}

.toast-fade-out {
    animation: slideOut 0.3s ease forwards;
}

@keyframes slideIn {
    from {
        transform: translateX(400px);
        opacity: 0;
    }
    to {
        transform: translateX(0);
        opacity: 1;
    }
}

@keyframes slideOut {
    from {
        transform: translateX(0);
        opacity: 1;
    }
    to {
        transform: translateX(400px);
        opacity: 0;
    }
}

/* ==================== TABLES ==================== */

.data-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
}

.data-table thead {
    background: var(--bg-secondary);
    border-bottom: 2px solid var(--border);
}

.data-table th {
    text-align: left;
    padding: var(--space-sm) var(--space-md);
    font-weight: 600;
    color: var(--text);
}

.data-table td {
    padding: var(--space-sm) var(--space-md);
    border-bottom: 1px solid var(--border);
}

.data-table tr:hover {
    background: var(--hover);
}

/* Sortable table headers */
.sortable {
    cursor: pointer;
    user-select: none;
}

.sortable:hover {
    background: var(--hover);
}

.sortable::after {
    content: '⇅';
    margin-left: var(--space-xs);
    opacity: 0.3;
}

.sortable.asc::after {
    content: '↑';
    opacity: 1;
}

.sortable.desc::after {
    content: '↓';
    opacity: 1;
}

/* ==================== FREQUENCY VISUALIZATIONS ==================== */

.freq-bar-container {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    margin-bottom: var(--space-sm);
}

.freq-bar-label {
    min-width: 120px;
    font-size: 0.875rem;
    font-weight: 600;
}

.freq-bar-track {
    flex: 1;
    height: 32px;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    position: relative;
    overflow: hidden;
}

.freq-bar-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--accent), var(--primary));
    transition: width var(--transition-base);
    display: flex;
    align-items: center;
    justify-content: flex-end;
    padding: 0 var(--space-sm);
}

.freq-bar-value {
    color: #ffffff;
    font-size: 0.8125rem; /* 13px */
    font-weight: 600;
}

/* ==================== NAVIGATION ==================== */

.nav-link {
    color: var(--text-secondary);
    text-decoration: none;
    font-weight: 500;
    font-size: 0.9rem;
    padding: var(--space-xs) var(--space-sm);
    border-radius: var(--radius-sm);
    transition: color var(--transition-fast), background-color var(--transition-fast);
}

.nav-link:hover {
    color: var(--primary);
    background-color: var(--hover);
}

.nav-link.active {
    color: var(--accent);
    font-weight: 600;
}

/* ==================== UTILITIES ==================== */

.flex {
    display: flex;
}

.flex-column {
    flex-direction: column;
}

.flex-center {
    align-items: center;
    justify-content: center;
}

.flex-between {
    justify-content: space-between;
}

.gap-xs { gap: var(--space-xs); }
.gap-sm { gap: var(--space-sm); }
.gap-md { gap: var(--space-md); }
.gap-lg { gap: var(--space-lg); }

.hidden {
    display: none !important;
}

.text-center {
    text-align: center;
}

.text-right {
    text-align: right;
}

.mt-sm { margin-top: var(--space-sm); }
.mt-md { margin-top: var(--space-md); }
.mt-lg { margin-top: var(--space-lg); }

.mb-sm { margin-bottom: var(--space-sm); }
.mb-md { margin-bottom: var(--space-md); }
.mb-lg { margin-bottom: var(--space-lg); }

/* ==================== ACCESSIBILITY ==================== */

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border-width: 0;
}

/* Reduce motion for users who prefer it */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

/* ==================== MOBILE RESPONSIVE ==================== */

@media (max-width: 768px) {
    /* KWIC converts to single column card layout */
    .kwic-line {
        grid-template-columns: 1fr;
        gap: 0;
    }

    .kwic-left,
    .kwic-right {
        text-align: left;
    }

    /* Forms stack vertically */
    .search-form {
        flex-direction: column;
    }

    /* Tables become cards on mobile */
    .data-table thead {
        display: none;
    }

    .data-table tr {
        display: block;
        background: var(--card-bg);
        border: 1px solid var(--border);
        border-radius: var(--radius-md);
        padding: var(--space-sm);
        margin-bottom: var(--space-sm);
    }

    .data-table td {
        display: flex;
        justify-content: space-between;
        padding: var(--space-xs) 0;
        border: none;
    }

    .data-table td::before {
        content: attr(data-label);
        font-weight: 600;
        color: var(--text-secondary);
        margin-right: var(--space-md);
    }

    /* Toast notifications */
    .toast-container {
        left: var(--space-sm);
        right: var(--space-sm);
    }

    .toast {
        min-width: 0;
    }
}

@media (max-width: 480px) {
    /* Smaller spacing on mobile */
    .section,
    .card {
        padding: var(--space-md);
    }

    /* Buttons full width */
    .btn {
        width: 100%;
    }

    .btn-small {
        width: auto;
    }
}
