@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

:root {
    --bg: #f8fafc;
    --surface: #ffffff;
    --border: #e2e8f0;
    --border-subtle: #f1f5f9;
    --text: #0f172a;
    --text-secondary: #64748b;
    --text-muted: #94a3b8;
    --primary: #6366f1;
    --primary-hover: #4f46e5;
    --primary-subtle: #eef2ff;
    --primary-ring: rgba(99, 102, 241, 0.25);
    --accent: #f59e0b;
    --accent-hover: #d97706;
    --danger: #ef4444;
    --danger-hover: #dc2626;
    --danger-subtle: #fef2f2;
    --success: #10b981;
    --success-subtle: #ecfdf5;
    --radius-sm: 6px;
    --radius: 10px;
    --radius-lg: 14px;
    --radius-xl: 20px;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.04);
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.06), 0 1px 2px rgba(0, 0, 0, 0.04);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.07), 0 2px 4px -2px rgba(0, 0, 0, 0.05);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.08), 0 4px 6px -4px rgba(0, 0, 0, 0.04);
    --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
    --transition: 150ms cubic-bezier(0.4, 0, 0.2, 1);
}

*,
*::before,
*::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: var(--font-sans);
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ============================================
   TOPBAR
   ============================================ */

.topbar {
    position: sticky;
    top: 0;
    z-index: 50;
    display: flex;
    align-items: center;
    gap: 16px;
    height: 56px;
    padding: 0 24px;
    background: rgba(255, 255, 255, 0.8);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border);
}

.topbar-back {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    color: var(--text-secondary);
    text-decoration: none;
    font-size: 0.8125rem;
    font-weight: 500;
    padding: 6px 10px;
    border-radius: var(--radius-sm);
    transition: all var(--transition);
}

.topbar-back:hover {
    color: var(--text);
    background: var(--border-subtle);
}

.topbar-title {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.9375rem;
    font-weight: 600;
    color: var(--text);
}

.topbar-title svg {
    color: var(--primary);
}

/* ============================================
   LAYOUT
   ============================================ */

.app {
    min-height: 100vh;
}

.layout {
    display: grid;
    grid-template-columns: 460px 1fr;
    gap: 0;
    min-height: calc(100vh - 56px);
}

@media (max-width: 1000px) {
    .layout {
        grid-template-columns: 1fr;
    }
}

/* ============================================
   SIDEBAR
   ============================================ */

.sidebar {
    border-right: 1px solid var(--border);
    background: var(--surface);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    max-height: calc(100vh - 56px);
    position: sticky;
    top: 56px;
}

@media (max-width: 1000px) {
    .sidebar {
        position: static;
        max-height: none;
        border-right: none;
        border-bottom: 1px solid var(--border);
    }
}

.sidebar-section {
    flex: 1;
    padding: 24px;
    overflow-y: auto;
}

.section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;
}

.section-header h2 {
    font-size: 0.8125rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 22px;
    height: 22px;
    padding: 0 7px;
    border-radius: 999px;
    background: var(--primary-subtle);
    color: var(--primary);
    font-size: 0.6875rem;
    font-weight: 700;
}

/* ============================================
   WORD ENTRIES
   ============================================ */

.word-entries {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-bottom: 16px;
}

.word-entry {
    display: flex;
    flex-direction: column;
    gap: 0;
    background: var(--bg);
    border: 1.5px solid transparent;
    border-radius: var(--radius);
    transition: all var(--transition);
    animation: slideIn 0.2s ease-out;
    overflow: hidden;
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(-8px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.word-entry:hover {
    border-color: var(--border);
    box-shadow: var(--shadow-sm);
}

.word-entry-main {
    display: grid;
    grid-template-columns: 120px 1fr 36px 36px;
    gap: 8px;
    align-items: center;
    padding: 10px 12px;
}

.word-entry:focus-within {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-ring);
    background: var(--surface);
}

.word-entry-main input {
    padding: 0;
    border: none;
    background: transparent;
    font-size: 0.875rem;
    font-family: var(--font-sans);
    color: var(--text);
    outline: none;
    width: 100%;
}

.word-entry-main input::placeholder {
    color: var(--text-muted);
}

.word-entry-main .word-input {
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    font-size: 0.8125rem;
}

.word-entry-main .clue-input {
    font-size: 0.875rem;
}

/* ============================================
   PIN BUTTON
   ============================================ */

.btn-pin {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border: none;
    background: transparent;
    color: var(--text-muted);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all var(--transition);
}

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

.btn-pin.active {
    color: var(--primary);
    background: var(--primary-subtle);
}

/* ============================================
   PINNED CONTROLS
   ============================================ */

.pinned-controls {
    display: none;
    grid-template-columns: auto 1fr auto 1fr;
    gap: 8px;
    align-items: center;
    padding: 8px 12px 10px;
    border-top: 1px solid var(--border);
    background: var(--primary-subtle);
}

.word-entry.pinned .pinned-controls {
    display: grid;
}

.word-entry.pinned {
    border-color: var(--primary);
    background: var(--primary-subtle);
}

.pinned-controls label {
    font-size: 0.6875rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.pinned-controls input[type="number"] {
    width: 52px;
    padding: 4px 8px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    font-size: 0.8125rem;
    font-family: var(--font-sans);
    font-weight: 600;
    color: var(--text);
    outline: none;
    text-align: center;
    -moz-appearance: textfield;
}

.pinned-controls input[type="number"]::-webkit-outer-spin-button,
.pinned-controls input[type="number"]::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

.pinned-controls input[type="number"]:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 2px var(--primary-ring);
}

.pinned-dir-group {
    display: flex;
    gap: 2px;
    background: var(--surface);
    border-radius: var(--radius-sm);
    padding: 2px;
    border: 1px solid var(--border);
}

.pinned-dir-btn {
    padding: 3px 8px;
    border: none;
    background: transparent;
    color: var(--text-muted);
    border-radius: 4px;
    font-size: 0.6875rem;
    font-weight: 600;
    font-family: var(--font-sans);
    cursor: pointer;
    transition: all var(--transition);
    line-height: 1.2;
}

.pinned-dir-btn:hover {
    color: var(--text-secondary);
}

.pinned-dir-btn.active {
    background: var(--primary);
    color: white;
    box-shadow: var(--shadow-sm);
}

/* ============================================
   REMOVE BUTTON
   ============================================ */

.btn-remove {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border: none;
    background: transparent;
    color: var(--text-muted);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all var(--transition);
}

.btn-remove:hover {
    background: var(--danger-subtle);
    color: var(--danger);
}

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

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 10px 16px;
    border: none;
    border-radius: var(--radius);
    font-family: var(--font-sans);
    font-size: 0.8125rem;
    font-weight: 600;
    cursor: pointer;
    transition: all var(--transition);
    text-decoration: none;
    white-space: nowrap;
}

.btn:active {
    transform: scale(0.98);
}

.btn-ghost {
    width: 100%;
    background: transparent;
    color: var(--primary);
    border: 1px dashed var(--border);
    margin-bottom: 16px;
}

.btn-ghost:hover {
    background: var(--primary-subtle);
    border-color: var(--primary);
}

.btn-primary {
    width: 100%;
    background: var(--primary);
    color: white;
    box-shadow: var(--shadow-sm), 0 1px 2px rgba(99, 102, 241, 0.2);
}

.btn-primary:hover {
    background: var(--primary-hover);
    box-shadow: var(--shadow-md), 0 2px 4px rgba(99, 102, 241, 0.25);
}

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

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

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

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

.btn-row {
    display: flex;
    gap: 8px;
    margin-top: 8px;
}

.sidebar-actions {
    padding: 16px 24px;
    border-top: 1px solid var(--border);
    background: var(--surface);
}

/* ============================================
   TOGGLE
   ============================================ */

.toggle-row {
    margin-bottom: 4px;
}

.toggle {
    display: flex;
    align-items: center;
    gap: 10px;
    cursor: pointer;
    user-select: none;
}

.toggle input {
    display: none;
}

.toggle-track {
    position: relative;
    width: 36px;
    height: 20px;
    background: #cbd5e1;
    border-radius: 999px;
    transition: background var(--transition);
    flex-shrink: 0;
}

.toggle-thumb {
    position: absolute;
    top: 2px;
    left: 2px;
    width: 16px;
    height: 16px;
    background: white;
    border-radius: 50%;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
    transition: transform var(--transition);
}

.toggle input:checked + .toggle-track {
    background: var(--primary);
}

.toggle input:checked + .toggle-track .toggle-thumb {
    transform: translateX(16px);
}

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

/* ============================================
   MAIN CONTENT
   ============================================ */

.content {
    padding: 32px 40px;
    overflow-y: auto;
    max-height: calc(100vh - 56px);
}

@media (max-width: 1000px) {
    .content {
        max-height: none;
        padding: 20px;
    }
}

/* ============================================
   EMPTY STATE
   ============================================ */

.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 80px 24px;
    color: var(--text-muted);
}

.empty-icon {
    width: 80px;
    height: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-xl);
    background: var(--border-subtle);
    margin-bottom: 20px;
    color: var(--text-muted);
}

.empty-state h3 {
    font-size: 1.0625rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 6px;
}

.empty-state p {
    font-size: 0.8125rem;
    max-width: 300px;
    line-height: 1.6;
}

.empty-state strong {
    color: var(--text-secondary);
}

/* ============================================
   PUZZLE AREA
   ============================================ */

.puzzle-area {
    margin-bottom: 32px;
}

.puzzle-wrapper {
    display: flex;
    justify-content: center;
    padding: 28px;
    background: var(--surface);
    border-radius: var(--radius-lg);
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
    overflow-x: auto;
}

.puzzle-grid {
    display: inline-grid;
    gap: 0;
    border-radius: 4px;
    overflow: hidden;
    box-shadow: 0 0 0 2px var(--cell-color, #334155);
}

.puzzle-cell {
    width: 38px;
    height: 38px;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    user-select: none;
}

.puzzle-cell.black {
    background: var(--cell-color, #1e293b);
}

.puzzle-cell.white {
    background: white;
    border: 1px solid #e2e8f0;
    transition: background var(--transition);
}

.puzzle-cell.white:hover {
    background: #f8fafc;
}

.puzzle-cell.white input {
    width: 100%;
    height: 100%;
    border: none;
    text-align: center;
    font-size: 1rem;
    font-weight: 700;
    text-transform: uppercase;
    background: transparent;
    font-family: var(--font-sans);
    outline: none;
    caret-color: var(--primary);
    color: var(--text);
    transition: all var(--transition);
}

.puzzle-cell.white input:focus {
    background: var(--primary-subtle);
    box-shadow: inset 0 0 0 2px var(--primary);
}

.puzzle-cell .cell-number {
    position: absolute;
    top: 2px;
    left: 3px;
    font-size: 0.5625rem;
    font-weight: 600;
    color: var(--text-muted);
    line-height: 1;
    pointer-events: none;
}

.puzzle-cell.white input.solved {
    color: var(--primary);
    font-weight: 700;
}

.puzzle-cell.white input.wrong {
    color: var(--danger);
    background: var(--danger-subtle);
}

/* ============================================
   CLUES
   ============================================ */

.clues-area {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
}

@media (max-width: 700px) {
    .clues-area {
        grid-template-columns: 1fr;
    }
}

.clue-section {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    overflow: hidden;
    box-shadow: var(--shadow-sm);
}

.clue-section-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 14px 18px;
    background: var(--border-subtle);
    border-bottom: 1px solid var(--border);
}

.clue-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 30px;
    height: 30px;
    border-radius: var(--radius-sm);
    font-size: 0.875rem;
    font-weight: 700;
}

.clue-icon.across {
    background: var(--primary-subtle);
    color: var(--primary);
}

.clue-icon.down {
    background: #fef3c7;
    color: #d97706;
}

.clue-section-header h3 {
    font-size: 0.8125rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.clue-list {
    padding: 8px 6px;
    display: flex;
    flex-direction: column;
    gap: 0;
}

.clue-item {
    display: flex;
    gap: 10px;
    padding: 7px 12px;
    border-radius: var(--radius-sm);
    font-size: 0.8125rem;
    line-height: 1.5;
    transition: background var(--transition);
    cursor: default;
}

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

.clue-number {
    font-weight: 700;
    min-width: 18px;
    color: var(--primary);
    font-variant-numeric: tabular-nums;
    padding-top: 1px;
}

.clue-text {
    flex: 1;
    color: var(--text);
}

/* ============================================
   ERROR
   ============================================ */

.error-message {
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--danger-subtle);
    color: var(--danger);
    padding: 12px 16px;
    border-radius: var(--radius);
    font-size: 0.8125rem;
    font-weight: 500;
    border: 1px solid #fecaca;
}

/* ============================================
   LOADING
   ============================================ */

.pdf-loading {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    background: var(--primary);
    color: white;
    border-radius: var(--radius);
    font-size: 0.8125rem;
    font-weight: 600;
}

.pdf-loading svg {
    animation: spin 1s linear infinite;
}

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

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

/* ============================================
   DRAG AND DROP
   ============================================ */

.color-row {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-top: 12px;
}

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

#cell-color {
    width: 36px;
    height: 28px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    cursor: pointer;
    padding: 2px;
    background: var(--surface);
}

.puzzle-cell.drag-source {
    opacity: 0.3 !important;
}

.puzzle-cell.drag-target-valid {
    background: #dcfce7 !important;
    box-shadow: inset 0 0 0 2px #22c55e !important;
}

.puzzle-cell.drag-target-invalid {
    background: #fef2f2 !important;
    box-shadow: inset 0 0 0 2px #ef4444 !important;
}

.drag-ghost {
    position: fixed;
    display: flex;
    gap: 0;
    z-index: 1000;
    pointer-events: none;
    opacity: 0.85;
    filter: drop-shadow(0 4px 12px rgba(0, 0, 0, 0.2));
    transition: flex-direction 0.15s ease;
}

.ghost-cell {
    width: 38px;
    height: 38px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
    font-weight: 700;
    font-family: var(--font-sans);
    background: white;
    border: 1px solid #cbd5e1;
    color: var(--text);
    text-transform: uppercase;
}

.ghost-cell.grabbed {
    background: var(--primary-subtle);
    border-color: var(--primary);
    color: var(--primary);
    border-width: 2px;
}

body.is-dragging,
body.is-dragging * {
    cursor: grabbing !important;
    user-select: none !important;
}

@media print {
    .drag-ghost,
    .puzzle-cell.drag-source,
    .puzzle-cell.drag-target-valid,
    .puzzle-cell.drag-target-invalid {
        display: none !important;
        opacity: 1 !important;
        box-shadow: none !important;
        background: white !important;
    }
    .sidebar,
    .topbar {
        display: none !important;
    }

    .layout {
        grid-template-columns: 1fr;
    }

    .content {
        max-height: none;
        padding: 0;
    }

    .puzzle-wrapper {
        border: none;
        box-shadow: none;
        padding: 0;
        border-radius: 0;
    }

    .puzzle-cell.white input:focus {
        background: white;
        box-shadow: none;
    }

    .btn-row {
        display: none !important;
    }
}
