/* ============================================
   CROSSWORDY - Main Stylesheet
   Modern dark theme with clean aesthetics
   ============================================ */

/* ---------- CSS Custom Properties ---------- */
:root {
    /* Colors - Dark Theme */
    --bg-primary: #0f0f14;
    --bg-secondary: #1a1a24;
    --bg-tertiary: #252533;
    --bg-hover: #2d2d3d;

    --text-primary: #f0f0f5;
    --text-secondary: #a0a0b0;
    --text-muted: #606070;

    --accent-primary: #6366f1;
    --accent-primary-hover: #818cf8;
    --accent-secondary: #22c55e;
    --accent-warning: #f59e0b;
    --accent-danger: #ef4444;

    --border-color: #3a3a4a;
    --border-light: #2a2a3a;

    /* Grid colors */
    --grid-cell-empty: #6b7280;
    /* Grey for unused cells */
    --grid-cell-filled: #ffffff;
    /* White for cells with letters */
    --grid-cell-text: #1a1a24;
    --grid-cell-blocked: #1a1a24;
    /* Black for blocked squares */
    --grid-cell-selected: #dbeafe;
    --grid-cell-highlight: #fef3c7;
    --grid-border: #3a3a4a;

    /* Sizing */
    --sidebar-width: 320px;
    --header-height: 64px;
    --cell-size: 40px;
    --border-radius: 8px;
    --border-radius-sm: 4px;

    /* Typography */
    --font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;

    /* Transitions */
    --transition-fast: 150ms ease;
    --transition-normal: 250ms ease;

    /* Shadows */
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 10px 25px rgba(0, 0, 0, 0.5);
}

/* ---------- Reset & Base ---------- */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    font-size: 16px;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

body {
    font-family: var(--font-family);
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.5;
    min-height: 100vh;
    overflow-x: hidden;
}

/* ---------- App Layout ---------- */
.app {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

/* ---------- Header ---------- */
.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: var(--header-height);
    padding: 0 24px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    position: sticky;
    top: 0;
    z-index: 100;
}

.logo {
    font-size: 1.5rem;
    font-weight: 700;
    background: linear-gradient(135deg, var(--accent-primary) 0%, #a855f7 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.header-mode-info {
    display: flex;
    align-items: center;
    gap: 12px;
    flex: 1;
    min-width: 0;
    margin: 0 16px;
}

.header-instructions {
    flex: 0 1 auto;
    margin: 0;
    font-size: 0.78rem;
    color: var(--accent-warning);
    text-align: left;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.header-controls {
    display: flex;
    align-items: center;
    gap: 12px;
}

.mode-select {
    padding: 8px 12px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.875rem;
    cursor: pointer;
    transition: border-color var(--transition-fast);
}

.mode-select:hover {
    border-color: var(--accent-primary);
}

.mode-select:focus {
    outline: none;
    border-color: var(--accent-primary);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2);
}

/* ---------- Buttons ---------- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 8px 16px;
    font-family: inherit;
    font-size: 0.875rem;
    font-weight: 500;
    border-radius: var(--border-radius);
    border: none;
    cursor: pointer;
    transition: all var(--transition-fast);
}

.btn-primary {
    background: var(--accent-primary);
    color: white;
}

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

.btn-secondary {
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
}

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

.btn-icon {
    width: 32px;
    height: 32px;
    padding: 0;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    font-size: 1.25rem;
    font-weight: 600;
}

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

.btn-add {
    padding: 8px 12px;
    background: var(--accent-secondary);
    color: white;
}

.btn-add:hover {
    background: #16a34a;
}

.btn-full {
    width: 100%;
}

.btn-danger {
    background: transparent;
    color: var(--text-muted);
    padding: 4px 8px;
    border: none;
}

.btn-danger:hover {
    color: var(--accent-danger);
}

/* ---------- Main Content ---------- */
.main-content {
    display: flex;
    flex: 1;
    gap: 0;
}

/* ---------- Sidebar ---------- */
.sidebar {
    width: var(--sidebar-width);
    min-width: var(--sidebar-width);
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    height: calc(100vh - var(--header-height));
    overflow: hidden;
}

.section-title {
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    margin-bottom: 12px;
}

/* ---------- Clue Editor ---------- */
.clue-editor {
    padding: 20px;
    border-bottom: 1px solid var(--border-color);
}

.clue-editor-content {
    min-height: 120px;
}

.placeholder-text {
    color: var(--text-muted);
    font-style: italic;
    font-size: 0.875rem;
}

.clue-form {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.clue-word-display {
    display: flex;
    align-items: center;
    gap: 12px;
}

.clue-word {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--accent-primary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.clue-direction {
    font-size: 0.75rem;
    padding: 4px 8px;
    background: var(--bg-tertiary);
    border-radius: var(--border-radius-sm);
    color: var(--text-secondary);
}

.clue-input {
    width: 100%;
    padding: 10px 12px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.875rem;
    resize: vertical;
    min-height: 60px;
    transition: border-color var(--transition-fast);
}

.clue-input:focus {
    outline: none;
    border-color: var(--accent-primary);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2);
}

/* Clue display for highlighted words */
.clue-display {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.clue-header {
    display: flex;
    align-items: center;
    gap: 8px;
}

.clue-number {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--accent-primary);
}

.clue-dir {
    font-size: 0.7rem;
    font-weight: 600;
    padding: 2px 6px;
    background: var(--accent-primary);
    color: white;
    border-radius: var(--border-radius-sm);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.clue-word-preview {
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--text-primary);
    text-transform: uppercase;
    letter-spacing: 0.1em;
}

.clue-text {
    font-size: 0.9rem;
    color: var(--text-secondary);
    line-height: 1.4;
}

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

/* ---------- Word Bucket ---------- */
.word-bucket {
    padding: 20px;
    flex: 1;
    min-height: 0;
    /* Critical: allows flex child to shrink below content size */
    display: flex;
    flex-direction: column;
    overflow: hidden;
    border-bottom: 1px solid var(--border-color);
}

.word-input-wrapper {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
}

.word-input {
    flex: 1;
    padding: 10px 12px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.875rem;
    text-transform: uppercase;
    transition: border-color var(--transition-fast);
}

.word-input:focus {
    outline: none;
    border-color: var(--accent-primary);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2);
}

.word-input::placeholder {
    text-transform: none;
    color: var(--text-muted);
}

.word-list {
    list-style: none;
    overflow-y: auto;
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.word-list-action {
    margin-top: 8px;
    padding-top: 8px;
    border-top: 1px solid var(--border-light);
}

.word-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px;
    background: var(--bg-tertiary);
    border-radius: var(--border-radius);
    cursor: pointer;
    transition: all var(--transition-fast);
    border: 2px solid transparent;
}

.word-item:hover {
    background: var(--bg-hover);
}

.word-item.selected {
    border-color: var(--accent-primary);
    background: rgba(99, 102, 241, 0.1);
}

.word-item.placed {
    opacity: 0.7;
}

.word-item.placed::before {
    content: '✓';
    color: var(--accent-secondary);
    margin-right: 8px;
    font-weight: 600;
}

.word-item-text {
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.word-item-actions {
    display: flex;
    gap: 4px;
    opacity: 0;
    transition: opacity var(--transition-fast);
}

.word-item:hover .word-item-actions {
    opacity: 1;
}

/* ---------- Placement Controls ---------- */
.placement-controls {
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

/* ---------- Grid Area ---------- */
.grid-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 24px;
    background: var(--bg-primary);
    overflow: auto;
}

.grid-controls {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 20px;
}

.grid-size-label {
    font-size: 0.875rem;
    color: var(--text-secondary);
}

.grid-size-display {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-primary);
    min-width: 70px;
    text-align: center;
}

/* Timer Display */
.timer-display {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-left: auto;
    padding: 6px 12px;
    background: var(--bg-tertiary);
    border-radius: var(--border-radius);
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--accent-primary);
}

.timer-icon {
    font-size: 1rem;
}

/* ---------- Crossword Grid ---------- */
.crossword-grid {
    display: grid;
    gap: 1px;
    background: var(--grid-border);
    padding: 2px;
    border-radius: var(--border-radius);
    box-shadow: var(--shadow-lg);
}

.grid-cell {
    width: var(--cell-size);
    height: var(--cell-size);
    background: var(--grid-cell-empty);
    /* Grey by default */
    border: 1px solid #1a1a24;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--grid-cell-text);
    cursor: pointer;
    position: relative;
    transition: background-color var(--transition-fast);
    text-transform: uppercase;
}

.grid-cell:hover:not(.blocked) {
    background: var(--grid-cell-selected);
}

.grid-cell.filled {
    background: var(--grid-cell-filled);
    /* White when has letter */
}

.grid-cell.blocked {
    background: var(--grid-cell-blocked);
    /* Black for blocked */
    cursor: default;
}

.grid-cell.selected {
    background: var(--grid-cell-selected);
}

.grid-cell.highlight {
    background: var(--grid-cell-highlight);
}


.grid-cell.word-highlight {
    background: #fef3c7 !important;
    /* Yellow highlight for active word */
}

.grid-cell.active-cell {
    background: #dbeafe !important;
    /* Blue highlight for cursor cell */
}

.grid-cell.placement-option {
    background: #d1fae5 !important;
    /* Green highlight for valid placement */
    box-shadow: inset 0 0 0 2px #10b981;
    cursor: pointer;
}

.grid-cell.placement-option:hover {
    background: #a7f3d0 !important;
}

.grid-cell.preview {
    background: #6ee7b7 !important;
    /* Bright green preview for the full word shape */
    box-shadow: inset 0 0 0 2px #059669 !important;
    border: 1px solid #047857 !important;
}

.grid-cell .cell-number {
    position: absolute;
    top: 2px;
    left: 3px;
    font-size: 0.6rem;
    font-weight: 500;
    color: var(--grid-cell-text);
    opacity: 0.7;
}

.grid-cell .cell-letter {
    font-size: 1.1rem;
    font-weight: 600;
}

/* ---------- Grid Instructions ---------- */
.grid-instructions {
    margin-top: 16px;
    text-align: center;
}

.grid-instructions p {
    font-size: 0.8rem;
    color: var(--text-muted);
}

.grid-instructions kbd {
    display: inline-block;
    padding: 2px 6px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius-sm);
    font-family: inherit;
    font-size: 0.75rem;
}

/* ---------- Scrollbar Styling ---------- */
::-webkit-scrollbar {
    width: 8px;
}

::-webkit-scrollbar-track {
    background: var(--bg-secondary);
}

::-webkit-scrollbar-thumb {
    background: var(--bg-hover);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

/* ---------- Modal ---------- */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.modal.hidden {
    display: none;
}

.modal-content {
    background: var(--bg-secondary);
    border-radius: var(--border-radius);
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-lg);
    min-width: 320px;
    max-width: 450px;
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border-color);
}

.modal-header h2 {
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--text-primary);
}

.btn-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    color: var(--text-muted);
    cursor: pointer;
    padding: 0;
    line-height: 1;
    transition: color var(--transition-fast);
}

.btn-close:hover {
    color: var(--text-primary);
}

.modal-body {
    padding: 20px;
    color: var(--text-secondary);
    font-size: 0.95rem;
    line-height: 1.5;
}

.modal-details {
    margin-top: 12px;
}

.modal-details-text {
    width: 100%;
    min-height: 120px;
    padding: 10px 12px;
    resize: vertical;
    border-radius: var(--border-radius-sm);
    border: 1px solid var(--border-color);
    background: var(--bg-tertiary);
    color: var(--text-primary);
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
    font-size: 0.75rem;
    line-height: 1.4;
}

.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    padding: 16px 20px;
    border-top: 1px solid var(--border-color);
}

.btn-danger-action {
    background: var(--accent-danger) !important;
    color: white !important;
}

.btn-danger-action:hover {
    background: #dc2626 !important;
}

/* ---------- Responsive Adjustments ---------- */
@media (max-width: 768px) {
    .main-content {
        flex-direction: column;
    }

    .sidebar {
        width: 100%;
        min-width: unset;
        border-right: none;
        border-bottom: 1px solid var(--border-color);
    }

    .word-bucket {
        max-height: 200px;
    }

    :root {
        --cell-size: 32px;
    }
}

@media (max-width: 1100px) {
    .header-instructions {
        display: none;
    }
}
