/**
 * Puzzle Words Game - Zhween Classroom Tools
 *
 * Modern, clean design with BEM methodology
 * Supports RTL languages (Kurdish, Arabic) and dark mode
 */

/* ==========================================================================
   CSS Custom Properties
   ========================================================================== */

:root {
    /* Brand Colors */
    --puzzle-primary: #15aebf;
    --puzzle-primary-hover: #0e9aa8;
    --puzzle-primary-light: rgba(21, 174, 191, 0.1);

    /* Surface Colors */
    --puzzle-bg: #f8fafc;
    --puzzle-surface: #ffffff;
    --puzzle-surface-elevated: #ffffff;

    /* Text Colors */
    --puzzle-text: #1e293b;
    --puzzle-text-secondary: #64748b;
    --puzzle-text-muted: #94a3b8;

    /* Border & Dividers */
    --puzzle-border: #e2e8f0;
    --puzzle-border-light: #f1f5f9;

    /* Feedback Colors */
    --puzzle-success: #22c55e;
    --puzzle-error: #ef4444;
    --puzzle-warning: #f59e0b;
    --puzzle-info: #3b82f6;

    /* Highlight Colors for Found Words */
    --puzzle-hl-0: #ef4444;
    --puzzle-hl-1: #8b5cf6;
    --puzzle-hl-2: #06b6d4;
    --puzzle-hl-3: #f59e0b;
    --puzzle-hl-4: #22c55e;

    /* Shadows */
    --puzzle-shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.04);
    --puzzle-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.07), 0 2px 4px -1px rgba(0, 0, 0, 0.04);
    --puzzle-shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.08), 0 4px 6px -2px rgba(0, 0, 0, 0.04);

    /* Spacing */
    --puzzle-panel-width: 280px;
    --puzzle-gap: 1rem;
    --puzzle-radius: 12px;
    --puzzle-radius-sm: 8px;

    /* Transitions */
    --puzzle-transition: 150ms ease;
    --puzzle-transition-slow: 300ms ease;
}

/* Dark Mode */
@media (prefers-color-scheme: dark) {
    :root {
        --puzzle-bg: #0f172a;
        --puzzle-surface: #1e293b;
        --puzzle-surface-elevated: #334155;
        --puzzle-text: #f1f5f9;
        --puzzle-text-secondary: #94a3b8;
        --puzzle-text-muted: #64748b;
        --puzzle-border: #334155;
        --puzzle-border-light: #1e293b;
        --puzzle-shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.2);
        --puzzle-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
        --puzzle-shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.4);
    }
}

/* ==========================================================================
   Base Container
   ========================================================================== */

.puzzle {
    display: flex;
    flex-direction: column;
    min-height: 95vh;
    height: 100%;
    background: var(--puzzle-bg);
    color: var(--puzzle-text);
    font-family: 'Nunito', -apple-system, BlinkMacSystemFont, sans-serif;
    overflow-x: hidden;
}

/* ==========================================================================
   Body Layout
   ========================================================================== */

.puzzle__body {
    display: flex;
    flex: 1 1 auto;
    gap: var(--puzzle-gap);
    padding: var(--puzzle-gap);
    overflow: auto;
}

/* ==========================================================================
   Left Panel (Controls)
   ========================================================================== */

.puzzle__panel {
    width: var(--puzzle-panel-width);
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    overflow-y: auto;
    padding-inline-end: 0.25rem;
}

/* Scrollbar Styling */
.puzzle__panel::-webkit-scrollbar {
    width: 4px;
}

.puzzle__panel::-webkit-scrollbar-track {
    background: transparent;
}

.puzzle__panel::-webkit-scrollbar-thumb {
    background: var(--puzzle-border);
    border-radius: 2px;
}

/* ==========================================================================
   Sections
   ========================================================================== */

.puzzle__section {
    background: var(--puzzle-surface);
    border-radius: var(--puzzle-radius);
    padding: 1rem;
    border: 1px solid var(--puzzle-border);
}

.puzzle__section--words {
    flex: 1;
    min-height: 120px;
    display: flex;
    flex-direction: column;
}

.puzzle__section--actions {
    margin-top: auto;
}

.puzzle__section-title {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.8rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.025em;
    color: var(--puzzle-primary);
    margin: 0 0 0.75rem 0;
}

.puzzle__section-title svg {
    opacity: 0.8;
}

/* ==========================================================================
   Form Elements
   ========================================================================== */

.puzzle__input-group {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 0.75rem;
}

.puzzle__input,
.puzzle__select,
.puzzle__textarea {
    width: 100%;
    padding: 0.625rem 0.75rem;
    font-size: 0.9rem;
    font-family: inherit;
    color: var(--puzzle-text);
    background: var(--puzzle-surface);
    border: 2px solid var(--puzzle-border);
    border-radius: var(--puzzle-radius-sm);
    transition: border-color var(--puzzle-transition), box-shadow var(--puzzle-transition);
}

.puzzle__input:focus,
.puzzle__select:focus,
.puzzle__textarea:focus {
    outline: none;
    border-color: var(--puzzle-primary);
    box-shadow: 0 0 0 3px var(--puzzle-primary-light);
}

.puzzle__input::placeholder,
.puzzle__textarea::placeholder {
    color: var(--puzzle-text-muted);
}

.puzzle__textarea {
    min-height: 80px;
    resize: vertical;
    margin-top: 0.5rem;
}

.puzzle__setting {
    margin-bottom: 0.75rem;
}

.puzzle__setting:last-child {
    margin-bottom: 0;
}

.puzzle__label {
    display: block;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--puzzle-text-secondary);
    margin-bottom: 0.375rem;
}

/* ==========================================================================
   Buttons
   ========================================================================== */

.puzzle__btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.625rem 1rem;
    font-size: 0.875rem;
    font-weight: 600;
    font-family: inherit;
    border: none;
    border-radius: var(--puzzle-radius-sm);
    cursor: pointer;
    transition: all var(--puzzle-transition);
    text-decoration: none;
}

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

.puzzle__btn--primary {
    background: var(--puzzle-primary);
    color: white;
}

.puzzle__btn--primary:hover:not(:disabled) {
    background: var(--puzzle-primary-hover);
    transform: translateY(-1px);
}

.puzzle__btn--primary:active:not(:disabled) {
    transform: translateY(0);
}

.puzzle__btn--secondary {
    background: var(--puzzle-border-light);
    color: var(--puzzle-text);
}

.puzzle__btn--secondary:hover:not(:disabled) {
    background: var(--puzzle-border);
}

.puzzle__btn--text {
    background: transparent;
    color: var(--puzzle-text-secondary);
    padding: 0.375rem 0.5rem;
    font-size: 0.8rem;
}

.puzzle__btn--text:hover {
    color: var(--puzzle-primary);
    background: var(--puzzle-primary-light);
}

.puzzle__btn--icon {
    padding: 0.625rem;
    flex-shrink: 0;
}

.puzzle__btn--full {
    width: 100%;
}

.puzzle__btn-group {
    display: flex;
    gap: 0.5rem;
    margin-top: 0.75rem;
    flex-wrap: nowrap;
}

.puzzle__btn-group .puzzle__btn {
    flex: 1 1 0;
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
}

.puzzle__btn-group .puzzle__btn--icon-only {
    flex: 0 0 42px;
    width: 42px;
    padding: 0.625rem;
}

/* ==========================================================================
   Word List
   ========================================================================== */

.puzzle__word-count {
    font-weight: 400;
    color: var(--puzzle-text-muted);
    margin-inline-start: auto;
}

.puzzle__word-list {
    display: flex;
    flex-wrap: wrap;
    gap: 0.375rem;
    flex: 1;
    align-content: flex-start;
    overflow-y: auto;
    max-height: 200px;
    padding: 0.25rem;
}

.puzzle__empty-msg {
    color: var(--puzzle-text-muted);
    font-size: 0.8rem;
    text-align: center;
    width: 100%;
    margin: 1rem 0;
}

.puzzle__word-tag {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    padding: 0.25rem 0.625rem;
    font-size: 0.8rem;
    font-weight: 600;
    background: var(--puzzle-border-light);
    border-radius: 1rem;
    transition: all var(--puzzle-transition);
}

.puzzle__word-tag--found {
    background: var(--puzzle-primary);
    color: white;
    text-decoration: line-through;
    opacity: 0.7;
}

.puzzle__word-remove {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 16px;
    height: 16px;
    padding: 0;
    margin-inline-start: 0.125rem;
    background: transparent;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    opacity: 0.5;
    transition: opacity var(--puzzle-transition);
}

.puzzle__word-remove:hover {
    opacity: 1;
}

.puzzle__word-remove svg {
    width: 12px;
    height: 12px;
    fill: currentColor;
}

/* ==========================================================================
   Main Content Area
   ========================================================================== */

.puzzle__main {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    overflow-y: auto;
    padding: 0.5rem;
    min-height: 0;
}

.puzzle__grid-wrapper {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    flex: 1;
}

/* ==========================================================================
   Puzzle Grid
   ========================================================================== */

.puzzle__grid {
    display: grid;
    gap: 2px;
    background: var(--puzzle-border);
    padding: 2px;
    border-radius: var(--puzzle-radius);
    box-shadow: var(--puzzle-shadow-lg);
    user-select: none;
    touch-action: none;
}

.puzzle__grid--empty {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 300px;
    min-width: 300px;
    background: var(--puzzle-surface);
}

/* Placeholder (Empty State) */
.puzzle__placeholder {
    text-align: center;
    padding: 2rem;
}

.puzzle__placeholder-icon {
    color: var(--puzzle-text-muted);
    opacity: 0.5;
    margin-bottom: 1rem;
}

.puzzle__placeholder-title {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--puzzle-text);
    margin: 0 0 0.5rem 0;
}

.puzzle__placeholder-text {
    font-size: 0.9rem;
    color: var(--puzzle-text-secondary);
    margin: 0;
}

/* Grid Cells */
.puzzle__cell {
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--puzzle-surface);
    font-weight: 800;
    text-transform: uppercase;
    cursor: pointer;
    transition: all 100ms ease;
    position: relative;
}

.puzzle__cell:hover {
    background: var(--puzzle-border-light);
}

/* Selected Cell */
.puzzle__cell--selected {
    background: var(--puzzle-primary) !important;
    color: white;
    transform: scale(1.05);
    z-index: 10;
    border-radius: 4px;
    box-shadow: 0 2px 8px rgba(21, 174, 191, 0.4);
}

/* Found Cell */
.puzzle__cell--found {
    color: white;
    font-weight: 900;
}

.puzzle__cell--found-0 { background: var(--puzzle-hl-0); }
.puzzle__cell--found-1 { background: var(--puzzle-hl-1); }
.puzzle__cell--found-2 { background: var(--puzzle-hl-2); }
.puzzle__cell--found-3 { background: var(--puzzle-hl-3); }
.puzzle__cell--found-4 { background: var(--puzzle-hl-4); }

/* ==========================================================================
   Found Words Section
   ========================================================================== */

.puzzle__found-words {
    margin-top: 1.5rem;
    text-align: center;
    width: 100%;
    max-width: 600px;
}

.puzzle__found-title {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.9rem;
    font-weight: 700;
    color: var(--puzzle-text-secondary);
    margin: 0 0 1rem 0;
}

.puzzle__target-words {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.5rem;
}

.puzzle__progress {
    margin-top: 1rem;
    font-size: 0.85rem;
    color: var(--puzzle-text-muted);
}

/* ==========================================================================
   Toast Notifications
   ========================================================================== */

.puzzle__toast {
    position: fixed;
    bottom: 1.5rem;
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    padding: 0.75rem 1.5rem;
    font-size: 0.9rem;
    font-weight: 600;
    color: white;
    background: var(--puzzle-text);
    border-radius: 2rem;
    box-shadow: var(--puzzle-shadow-lg);
    opacity: 0;
    visibility: hidden;
    transition: all var(--puzzle-transition-slow) cubic-bezier(0.34, 1.56, 0.64, 1);
    z-index: 1000;
}

.puzzle__toast--visible {
    opacity: 1;
    visibility: visible;
    transform: translateX(-50%) translateY(0);
}

.puzzle__toast--success { background: var(--puzzle-success); }
.puzzle__toast--error { background: var(--puzzle-error); }
.puzzle__toast--warning { background: var(--puzzle-warning); }
.puzzle__toast--info { background: var(--puzzle-info); }

/* ==========================================================================
   Confetti
   ========================================================================== */

.puzzle__confetti {
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 2000;
    overflow: hidden;
}

.puzzle__confetti-piece {
    position: absolute;
    width: 10px;
    height: 10px;
    animation: confetti-fall 3s linear forwards;
}

@keyframes confetti-fall {
    0% {
        transform: translateY(-10px) rotate(0deg);
        opacity: 1;
    }
    100% {
        transform: translateY(100vh) rotate(720deg);
        opacity: 0;
    }
}

/* ==========================================================================
   Print View / Download Capture
   ========================================================================== */

.puzzle__capture {
    position: absolute;
    left: -9999px;
    top: 0;
    background: white;
    padding: 40px;
    font-family: 'Nunito', sans-serif;
    width: 800px;
}

.puzzle__capture-header {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 16px;
    margin-bottom: 32px;
}

.puzzle__capture-header img {
    height: 40px;
}

.puzzle__capture-title {
    font-size: 1.75rem;
    font-weight: 700;
    color: var(--puzzle-primary);
    margin: 0;
}

.puzzle__capture-grid {
    display: grid;
    gap: 2px;
    background: #cbd5e0;
    width: fit-content;
    margin: 0 auto;
    border: 2px solid #cbd5e0;
}

.puzzle__capture-cell {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    background: white;
    font-weight: 700;
    font-size: 1.1rem;
}

.puzzle__capture-cell--answer {
    color: white;
}

.puzzle__capture-words {
    margin-top: 32px;
    text-align: center;
}

.puzzle__capture-words h3 {
    font-size: 1rem;
    color: #64748b;
    margin: 0 0 12px 0;
}

.puzzle__capture-word-list {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 8px;
}

.puzzle__capture-word {
    padding: 6px 14px;
    background: #f1f5f9;
    border-radius: 16px;
    font-weight: 600;
    font-size: 0.9rem;
}

.puzzle__capture-footer {
    margin-top: 32px;
    text-align: center;
    color: #94a3b8;
    font-size: 0.85rem;
}

/* ==========================================================================
   Responsive Design
   ========================================================================== */

@media (max-width: 900px) {
    .puzzle__body {
        flex-direction: column;
    }

    .puzzle__panel {
        width: 100%;
        max-height: none;
        flex-direction: row;
        flex-wrap: wrap;
        gap: 0.5rem;
        overflow-y: visible;
    }

    .puzzle__section {
        flex: 1 1 calc(50% - 0.25rem);
        min-width: 200px;
    }

    .puzzle__section--words {
        order: 3;
        flex-basis: 100%;
    }

    .puzzle__section--actions {
        margin-top: 0;
    }

    .puzzle__main {
        padding: 0;
    }
}

@media (max-width: 600px) {
    :root {
        --puzzle-panel-width: 100%;
        --puzzle-gap: 0.75rem;
    }

    .puzzle__header {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.25rem;
        padding: 0.75rem 1rem;
    }

    .puzzle__status {
        font-size: 0.8rem;
    }

    .puzzle__section {
        flex-basis: 100%;
    }

    .puzzle__btn-group {
        flex-wrap: wrap;
    }

    .puzzle__btn-group .puzzle__btn {
        flex: 1 1 auto;
    }

    .puzzle__grid {
        max-width: calc(100vw - 2rem);
    }
}

/* ==========================================================================
   Print Styles
   ========================================================================== */

@media print {
    .puzzle__panel,
    .puzzle__header,
    .puzzle__toast,
    .puzzle__confetti {
        display: none !important;
    }

    .puzzle__body {
        display: block;
        padding: 0;
    }

    .puzzle__main {
        padding: 0;
    }

    .puzzle__grid {
        box-shadow: none;
        page-break-inside: avoid;
    }

    .puzzle__found-words {
        page-break-inside: avoid;
    }
}

/* ==========================================================================
   RTL Support
   ========================================================================== */

[dir="rtl"] .puzzle__input-group {
    flex-direction: row-reverse;
}

[dir="rtl"] .puzzle__word-remove {
    margin-inline-start: 0;
    margin-inline-end: 0.125rem;
}

/* ==========================================================================
   Animation Classes
   ========================================================================== */

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    10%, 30%, 50%, 70%, 90% { transform: translateX(-4px); }
    20%, 40%, 60%, 80% { transform: translateX(4px); }
}

.puzzle--shake {
    animation: shake 0.5s ease;
}

@keyframes pop {
    0% { transform: scale(0.8); opacity: 0; }
    50% { transform: scale(1.05); }
    100% { transform: scale(1); opacity: 1; }
}

.puzzle--pop {
    animation: pop 0.3s ease forwards;
}
