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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    background: #f0f2f5;
    color: #1a1a2e;
    line-height: 1.5;
    min-height: 100vh;
}

/* === App Layout === */
.app-header {
    background: #1a1a2e;
    color: #fff;
    padding: 16px 24px;
    text-align: center;
}

.app-header h1 {
    font-size: 22px;
    font-weight: 600;
    letter-spacing: 0.5px;
}

.app-header-inner {
    max-width: 1200px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    position: relative;
}

.app-header-inner h1 {
    flex: 1;
    text-align: center;
}

.header-back {
    position: absolute;
    left: 0;
    color: rgba(255, 255, 255, 0.7);
    text-decoration: none;
    font-size: 14px;
    font-weight: 500;
    transition: color 0.2s;
}

.header-back:hover {
    color: #fff;
}

.app-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 24px;
    display: flex;
    gap: 24px;
    align-items: flex-start;
}

/* === Controls Panel === */
.controls-panel {
    flex: 0 0 340px;
    background: #fff;
    border-radius: 12px;
    padding: 24px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.08);
    position: sticky;
    top: 24px;
}

.control-group {
    margin-bottom: 20px;
}

.control-group:last-child {
    margin-bottom: 0;
}

.control-label {
    display: block;
    font-size: 13px;
    font-weight: 600;
    color: #555;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 8px;
}

/* Radio group for input mode */
.radio-group {
    display: flex;
    gap: 0;
    border-radius: 8px;
    overflow: hidden;
    border: 2px solid #e0e0e0;
}

.radio-group label {
    flex: 1;
    padding: 10px 12px;
    text-align: center;
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    background: #fff;
    transition: all 0.2s;
    border-right: 1px solid #e0e0e0;
}

.radio-group label:last-child {
    border-right: none;
}

.radio-group input {
    display: none;
}

.radio-group input:checked + span {
    /* handled via JS class */
}

.radio-group label.active {
    background: #1a1a2e;
    color: #fff;
}

/* Segmented control for complexity */
.segmented-control {
    display: flex;
    gap: 0;
    border-radius: 8px;
    overflow: hidden;
    border: 2px solid #e0e0e0;
}

.segmented-control label {
    flex: 1;
    padding: 10px 8px;
    text-align: center;
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    background: #fff;
    transition: all 0.2s;
    border-right: 1px solid #e0e0e0;
}

.segmented-control label:last-child {
    border-right: none;
}

.segmented-control input {
    display: none;
}

.segmented-control label.active {
    background: #1a1a2e;
    color: #fff;
}

.complexity-desc {
    font-size: 12px;
    color: #888;
    margin-top: 6px;
    min-height: 32px;
}

/* Text inputs */
input[type="text"],
input[type="number"],
textarea,
select {
    width: 100%;
    padding: 10px 12px;
    border: 2px solid #e0e0e0;
    border-radius: 8px;
    font-size: 14px;
    font-family: inherit;
    transition: border-color 0.2s;
    background: #fff;
}

input[type="text"]:focus,
input[type="number"]:focus,
textarea:focus,
select:focus {
    outline: none;
    border-color: #1a1a2e;
}

textarea {
    resize: vertical;
    min-height: 80px;
}

select {
    cursor: pointer;
}

/* API Key */
.api-key-wrapper {
    display: flex;
    gap: 0;
    position: relative;
}

.api-key-wrapper input {
    flex: 1;
    border-top-right-radius: 0;
    border-bottom-right-radius: 0;
    padding-right: 8px;
}

.btn-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    border: 2px solid #e0e0e0;
    border-left: none;
    border-radius: 0 8px 8px 0;
    background: #f8f8f8;
    cursor: pointer;
    font-size: 16px;
    color: #888;
    transition: background 0.2s;
    padding: 0;
}

.btn-icon:hover {
    background: #eee;
}

.api-key-hint {
    font-size: 11px;
    color: #999;
    margin-top: 4px;
}

/* Inline group */
.inline-group {
    display: flex;
    gap: 12px;
}

.inline-group .control-group {
    flex: 1;
}

/* Word tags */
.word-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 8px;
    min-height: 30px;
}

.word-tag {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px 10px;
    background: #e8eaf6;
    border-radius: 20px;
    font-size: 13px;
    font-weight: 500;
    color: #1a1a2e;
}

.word-tag .remove-word {
    cursor: pointer;
    color: #999;
    font-size: 16px;
    line-height: 1;
    margin-left: 2px;
}

.word-tag .remove-word:hover {
    color: #e74c3c;
}

/* Add word input */
.add-word-row {
    display: flex;
    gap: 8px;
    margin-top: 8px;
}

.add-word-row input {
    flex: 1;
}

.add-word-row button {
    flex-shrink: 0;
    width: auto;
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 10px 20px;
    border: none;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    font-family: inherit;
}

.btn-primary {
    background: #1a1a2e;
    color: #fff;
    width: 100%;
}

.btn-primary:hover {
    background: #2d2d4e;
}

.btn-primary:disabled {
    background: #ccc;
    cursor: not-allowed;
}

.btn-secondary {
    background: #e8eaf6;
    color: #1a1a2e;
    width: 100%;
}

.btn-secondary:hover {
    background: #c5cae9;
}

.btn-small {
    padding: 6px 12px;
    font-size: 13px;
}

.btn-pdf {
    background: #c0392b;
    color: #fff;
    width: 100%;
}

.btn-pdf:hover {
    background: #e74c3c;
}

.btn-save {
    background: #27ae60;
    color: #fff;
    width: 100%;
}

.btn-save:hover {
    background: #2ecc71;
}

/* Saved puzzles */
.saved-puzzles-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 8px;
}

.saved-puzzles-header .control-label {
    margin-bottom: 0;
}

.saved-puzzles-list {
    max-height: 240px;
    overflow-y: auto;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
}

.saved-empty {
    padding: 16px;
    text-align: center;
    color: #bbb;
    font-size: 13px;
}

.saved-puzzle-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border-bottom: 1px solid #f0f0f0;
    cursor: pointer;
    transition: background 0.15s;
}

.saved-puzzle-item:last-child {
    border-bottom: none;
}

.saved-puzzle-item:hover {
    background: #f5f6fa;
}

.saved-puzzle-info {
    flex: 1;
    min-width: 0;
}

.saved-puzzle-title {
    font-size: 13px;
    font-weight: 600;
    color: #1a1a2e;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.saved-puzzle-meta {
    font-size: 11px;
    color: #999;
}

.saved-puzzle-delete {
    flex-shrink: 0;
    background: none;
    border: none;
    color: #ccc;
    font-size: 18px;
    cursor: pointer;
    padding: 0 4px;
    line-height: 1;
}

.saved-puzzle-delete:hover {
    color: #e74c3c;
}

/* Checkbox / toggle */
.checkbox-group {
    display: flex;
    align-items: center;
    gap: 10px;
}

.toggle-switch {
    position: relative;
    width: 44px;
    height: 24px;
    flex-shrink: 0;
}

.toggle-switch input {
    display: none;
}

.toggle-slider {
    position: absolute;
    cursor: pointer;
    top: 0; left: 0; right: 0; bottom: 0;
    background: #ccc;
    border-radius: 24px;
    transition: 0.3s;
}

.toggle-slider::before {
    content: '';
    position: absolute;
    width: 18px;
    height: 18px;
    left: 3px;
    bottom: 3px;
    background: #fff;
    border-radius: 50%;
    transition: 0.3s;
}

.toggle-switch input:checked + .toggle-slider {
    background: #1a1a2e;
}

.toggle-switch input:checked + .toggle-slider::before {
    transform: translateX(20px);
}

.toggle-label {
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
}

/* Spinner */
.spinner-overlay {
    display: none;
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.3);
    z-index: 1000;
    align-items: center;
    justify-content: center;
}

.spinner-overlay.active {
    display: flex;
}

.spinner {
    background: #fff;
    padding: 32px 48px;
    border-radius: 12px;
    text-align: center;
    box-shadow: 0 8px 32px rgba(0,0,0,0.2);
}

.spinner-circle {
    width: 40px;
    height: 40px;
    border: 4px solid #e0e0e0;
    border-top-color: #1a1a2e;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin: 0 auto 12px;
}

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

.spinner-text {
    font-size: 14px;
    color: #555;
}

/* Notification */
.notification {
    padding: 10px 16px;
    border-radius: 8px;
    font-size: 13px;
    margin-bottom: 12px;
    display: none;
}

.notification.error {
    background: #ffeaea;
    color: #c0392b;
    border: 1px solid #f5c6cb;
    display: block;
}

.notification.warning {
    background: #fff8e1;
    color: #856404;
    border: 1px solid #ffeeba;
    display: block;
}

.notification.success {
    background: #e8f5e9;
    color: #2e7d32;
    border: 1px solid #c8e6c9;
    display: block;
}

/* === A4 Preview === */
.preview-area {
    flex: 1;
    min-width: 0;
    display: flex;
    justify-content: center;
}

.a4-sheet {
    width: 794px;
    min-height: 1123px;
    background: #fff;
    box-shadow: 0 4px 20px rgba(0,0,0,0.12);
    padding: 57px; /* ~15mm at 96dpi */
    position: relative;
    display: flex;
    flex-direction: column;
}

.a4-sheet.pdf-render {
    box-shadow: none;
    width: 794px;
    min-height: 1123px;
}

/* Placeholder */
.placeholder {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #bbb;
    font-size: 18px;
    text-align: center;
}

/* Sheet elements */
.sheet-title {
    text-align: center;
    font-size: 26px;
    font-weight: 700;
    color: #1a1a2e;
    margin-bottom: 6px;
}

.sheet-complexity {
    text-align: center;
    font-size: 14px;
    color: #888;
    margin-bottom: 20px;
    letter-spacing: 1px;
}

.sheet-grid-wrapper {
    display: flex;
    justify-content: center;
    margin-bottom: 24px;
}

.sheet-grid {
    border-collapse: collapse;
}

.grid-cell {
    width: 32px;
    height: 32px;
    text-align: center;
    vertical-align: middle;
    font-family: 'Courier New', Courier, monospace;
    font-size: 16px;
    font-weight: 700;
    color: #1a1a2e;
    border: 1px solid #e8e8e8;
    user-select: none;
}

.grid-cell.highlighted {
    border-color: rgba(0,0,0,0.15);
}

/* Word bank */
.sheet-wordbank {
    margin-top: auto;
    padding-top: 16px;
}

.wordbank-title {
    font-size: 14px;
    font-weight: 600;
    color: #555;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 10px;
    text-align: center;
}

.wordbank-list {
    display: grid;
    gap: 4px 16px;
}

.wordbank-list.columns-2 { grid-template-columns: 1fr 1fr; }
.wordbank-list.columns-3 { grid-template-columns: 1fr 1fr 1fr; }
.wordbank-list.columns-4 { grid-template-columns: 1fr 1fr 1fr 1fr; }

.wordbank-item {
    font-family: 'Courier New', Courier, monospace;
    font-size: 13px;
    font-weight: 600;
    padding: 3px 8px;
    color: #1a1a2e;
    display: flex;
    align-items: center;
    gap: 6px;
}

.wordbank-item.solved {
    text-decoration: line-through;
    color: #888;
}

.word-dot {
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    flex-shrink: 0;
}

.sheet-footer {
    text-align: center;
    font-size: 11px;
    color: #bbb;
    margin-top: 20px;
    padding-top: 12px;
    border-top: 1px solid #eee;
}

/* === Responsive === */
@media (max-width: 900px) {
    .app-container {
        flex-direction: column;
    }

    .controls-panel {
        flex: none;
        width: 100%;
        position: static;
    }

    .a4-sheet {
        width: 100%;
        min-height: auto;
        aspect-ratio: 210 / 297;
    }
}

@media (max-width: 600px) {
    .app-container {
        padding: 12px;
    }

    .controls-panel {
        padding: 16px;
    }

    .a4-sheet {
        padding: 24px;
    }
}

/* === Print === */
@media print {
    body {
        background: #fff;
    }

    .app-header,
    .controls-panel,
    .spinner-overlay {
        display: none !important;
    }

    .a4-sheet {
        box-shadow: none;
        margin: 0;
        padding: 15mm;
        width: 210mm;
        min-height: 297mm;
    }
}

/* Page break for PDF */
.html2pdf__page-break {
    page-break-before: always;
}
