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

:root {
    --primary: #6366f1;
    --primary-hover: #4f46e5;
    --primary-light: #eef2ff;
    --primary-glow: rgba(99, 102, 241, 0.15);
    --accent: #06b6d4;
    --danger: #ef4444;
    --danger-hover: #dc2626;
    --success: #10b981;
    --warning: #f59e0b;
    --gray-50: #f8fafc;
    --gray-100: #f1f5f9;
    --gray-200: #e2e8f0;
    --gray-300: #cbd5e1;
    --gray-400: #94a3b8;
    --gray-500: #64748b;
    --gray-600: #475569;
    --gray-700: #334155;
    --gray-800: #1e293b;
    --gray-900: #0f172a;
    --radius: 10px;
    --radius-lg: 16px;
    --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 12px rgba(0,0,0,0.07);
    --shadow-lg: 0 12px 40px rgba(0,0,0,0.12);
    --shadow-glow: 0 0 20px var(--primary-glow);
    --transition: 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: #f0f2f8;
    color: var(--gray-800);
    line-height: 1.6;
    direction: rtl;
    min-height: 100vh;
}

/* ===== Header ===== */
.app-header {
    background: linear-gradient(135deg, #0f172a 0%, #1e293b 50%, #0f172a 100%);
    color: white;
    text-align: center;
    padding: 2.5rem 1rem 2rem;
    position: relative;
    overflow: hidden;
}

.app-header::before {
    content: '';
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: radial-gradient(ellipse at 30% 50%, rgba(99, 102, 241, 0.15) 0%, transparent 50%),
                radial-gradient(ellipse at 70% 50%, rgba(6, 182, 212, 0.1) 0%, transparent 50%);
    pointer-events: none;
}

.app-header h1 {
    font-size: 1.85rem;
    font-weight: 700;
    margin-bottom: 0.3rem;
    position: relative;
    letter-spacing: -0.02em;
    background: linear-gradient(135deg, #fff 0%, #c7d2fe 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.app-header .subtitle {
    font-size: 0.9rem;
    opacity: 0.6;
    position: relative;
    letter-spacing: 0.01em;
}

.doc-counter {
    display: inline-block;
    margin-top: 0.75rem;
    padding: 0.35rem 1.25rem;
    background: rgba(255, 255, 255, 0.06);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 999px;
    font-size: 0.75rem;
    color: rgba(255, 255, 255, 0.5);
    backdrop-filter: blur(8px);
    transition: opacity 0.3s;
    position: relative;
}

.doc-counter.loading {
    opacity: 0.3;
}

/* ===== Layout ===== */
.app-container {
    max-width: 720px;
    margin: -1rem auto 0;
    padding: 0 1rem 3rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
    position: relative;
    z-index: 1;
}

/* ===== Panels ===== */
.panel {
    background: white;
    border-radius: var(--radius-lg);
    padding: 1.5rem;
    box-shadow: var(--shadow);
    border: 1px solid var(--gray-200);
    transition: box-shadow var(--transition), transform var(--transition);
}

.panel:hover {
    box-shadow: var(--shadow-md);
}

.panel-title {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--gray-800);
    margin-bottom: 0.6rem;
    letter-spacing: -0.01em;
}

.panel-header-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0.6rem;
}

.panel-header-row .panel-title {
    margin-bottom: 0;
}

.panel-desc {
    font-size: 0.8rem;
    color: var(--gray-400);
    margin-bottom: 0.85rem;
}

.badge {
    font-size: 0.65rem;
    padding: 0.2rem 0.6rem;
    border-radius: 999px;
    font-weight: 600;
    letter-spacing: 0.02em;
    text-transform: uppercase;
}

.badge.optional {
    background: var(--gray-100);
    color: var(--gray-400);
}

/* ===== Filename Input ===== */
.filename-input {
    width: 100%;
    padding: 0.6rem 0.85rem;
    border: 1.5px solid var(--gray-200);
    border-radius: var(--radius);
    font-size: 0.9rem;
    font-family: 'SF Mono', 'Fira Code', monospace;
    color: var(--gray-700);
    background: var(--gray-50);
    transition: all var(--transition);
}

.filename-input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-glow);
    background: white;
}

.filename-input::placeholder {
    color: var(--gray-300);
}

/* ===== Upload Zone ===== */
.upload-zone {
    border: 2px dashed var(--gray-200);
    border-radius: var(--radius-lg);
    padding: 2rem;
    text-align: center;
    cursor: pointer;
    transition: all var(--transition);
    position: relative;
    background: var(--gray-50);
}

.upload-zone:hover,
.upload-zone.drag-over {
    border-color: var(--primary);
    background: var(--primary-light);
    box-shadow: var(--shadow-glow);
}

.upload-zone.has-file {
    border-style: solid;
    border-color: var(--success);
    background: #ecfdf5;
    cursor: default;
}

.upload-placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
    color: var(--gray-400);
}

.upload-icon {
    width: 2rem;
    height: 2rem;
    opacity: 0.5;
}

.upload-placeholder span {
    font-size: 0.85rem;
}

.file-input {
    position: absolute;
    inset: 0;
    opacity: 0;
    cursor: pointer;
}

/* ===== File Info ===== */
.file-info {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-top: 0.6rem;
    padding: 0.6rem 0.85rem;
    background: var(--gray-50);
    border-radius: var(--radius);
    border: 1px solid var(--gray-100);
}

.file-info .file-name {
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--gray-700);
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.file-info .file-pages {
    font-size: 0.75rem;
    color: var(--gray-400);
    white-space: nowrap;
    background: var(--gray-100);
    padding: 0.15rem 0.5rem;
    border-radius: 999px;
}

.btn-remove {
    width: 1.4rem;
    height: 1.4rem;
    border: none;
    background: var(--gray-200);
    color: var(--gray-500);
    border-radius: 50%;
    font-size: 0.85rem;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--transition);
    flex-shrink: 0;
}

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

/* ===== Appendix Config ===== */
.appendix-config {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 0.85rem;
    flex-wrap: wrap;
}

.config-label {
    font-size: 0.8rem;
    font-weight: 500;
    color: var(--gray-500);
    white-space: nowrap;
}

.select-input {
    padding: 0.4rem 0.75rem;
    border: 1.5px solid var(--gray-200);
    border-radius: var(--radius);
    font-size: 0.85rem;
    font-family: inherit;
    background: var(--gray-50);
    color: var(--gray-700);
    cursor: pointer;
    direction: rtl;
    transition: all var(--transition);
}

.select-input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-glow);
    background: white;
}

/* ===== Appendices List ===== */
.appendices-list {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    margin-top: 0.6rem;
}

.appendix-item {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.5rem 0.85rem;
    background: var(--gray-50);
    border: 1px solid var(--gray-100);
    border-radius: var(--radius);
    transition: all var(--transition);
    cursor: grab;
    flex-wrap: wrap;
}

.appendix-item:active { cursor: grabbing; }
.appendix-item.dragging { opacity: 0.5; transform: scale(0.98); }
.appendix-item.drag-target { border-color: var(--primary); background: var(--primary-light); }

.appendix-label {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 2.25rem;
    padding: 0.15rem 0.45rem;
    background: linear-gradient(135deg, var(--primary), var(--accent));
    color: white;
    border-radius: 6px;
    font-size: 0.8rem;
    font-weight: 600;
    flex-shrink: 0;
}

.appendix-name {
    flex: 1;
    font-size: 0.85rem;
    color: var(--gray-600);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.appendix-pages {
    font-size: 0.7rem;
    color: var(--gray-400);
    white-space: nowrap;
    background: var(--gray-100);
    padding: 0.1rem 0.4rem;
    border-radius: 999px;
}

.appendix-title-input {
    width: 100%;
    padding: 0.3rem 0.5rem;
    border: 1.5px solid var(--gray-100);
    border-radius: 6px;
    font-size: 0.8rem;
    font-family: inherit;
    color: var(--gray-700);
    direction: rtl;
    background: white;
    transition: all var(--transition);
}

.appendix-title-input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 2px var(--primary-glow);
}

.appendix-title-input::placeholder { color: var(--gray-300); }

.appendix-drag-handle {
    color: var(--gray-300);
    cursor: grab;
    flex-shrink: 0;
    display: flex;
    align-items: center;
}

.appendix-drag-handle svg {
    width: 1.1rem;
    height: 1.1rem;
}

/* ===== Numbering Config ===== */
.numbering-config {
    display: flex;
    flex-direction: column;
    gap: 0.85rem;
    margin-bottom: 0.85rem;
}

.config-group {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex-wrap: wrap;
}

.position-btns {
    display: flex;
    gap: 0;
    border-radius: var(--radius);
    overflow: hidden;
    border: 1.5px solid var(--gray-200);
}

.position-btn {
    padding: 0.35rem 0.85rem;
    border: none;
    background: white;
    color: var(--gray-500);
    font-size: 0.8rem;
    cursor: pointer;
    transition: all var(--transition);
    font-family: inherit;
    border-radius: 0;
}

.position-btn:not(:last-child) { border-inline-end: 1px solid var(--gray-200); }
.position-btn:first-child { border-radius: 0 var(--radius) var(--radius) 0; }
.position-btn:last-child { border-radius: var(--radius) 0 0 var(--radius); }
.position-btn:hover { background: var(--gray-50); }
.position-btn.active {
    background: var(--primary);
    color: white;
}

.range-input {
    flex: 1;
    max-width: 120px;
    accent-color: var(--primary);
    direction: ltr;
}

#font-size-display {
    font-size: 0.75rem;
    color: var(--gray-400);
    min-width: 2.5rem;
    font-family: monospace;
}

/* ===== Numbering Preview ===== */
.numbering-preview { display: flex; justify-content: center; }

.preview-page {
    width: 100px;
    height: 140px;
    background: white;
    border: 1px solid var(--gray-200);
    border-radius: 6px;
    box-shadow: var(--shadow-sm);
    display: flex;
    flex-direction: column;
    position: relative;
    overflow: hidden;
}

.preview-content {
    flex: 1;
    margin: 0.75rem 0.6rem;
    background: repeating-linear-gradient(transparent, transparent 5px, var(--gray-100) 5px, var(--gray-100) 6px);
}

.preview-number {
    text-align: center;
    font-size: 0.55rem;
    color: var(--gray-400);
    padding: 0.2rem;
    border-top: 1px solid var(--gray-100);
    background: var(--gray-50);
}

.preview-number.pos-right { text-align: right; padding-right: 0.5rem; }
.preview-number.pos-left { text-align: left; padding-left: 0.5rem; }
.preview-number.pos-center { text-align: center; }

/* ===== Toggle Switch ===== */
.toggle-switch {
    position: relative;
    display: inline-block;
    width: 44px;
    height: 24px;
    cursor: pointer;
}

.toggle-switch input { opacity: 0; width: 0; height: 0; }

.toggle-slider {
    position: absolute;
    inset: 0;
    background: var(--gray-200);
    border-radius: 999px;
    transition: background var(--transition);
}

.toggle-slider::before {
    content: '';
    position: absolute;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: white;
    top: 3px;
    right: 3px;
    transition: transform var(--transition);
    box-shadow: 0 1px 3px rgba(0,0,0,0.15);
}

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

/* ===== Assembly Summary ===== */
.assembly-summary { margin-bottom: 1rem; }

.assembly-summary .empty-state {
    text-align: center;
    color: var(--gray-300);
    padding: 1rem;
    font-size: 0.85rem;
}

.summary-list {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
}

.summary-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.85rem;
    padding: 0.3rem 0;
    color: var(--gray-600);
}

.summary-icon {
    width: 1.1rem;
    height: 1.1rem;
    color: var(--success);
    flex-shrink: 0;
}

.summary-total {
    margin-top: 0.6rem;
    padding-top: 0.6rem;
    border-top: 1px solid var(--gray-100);
    font-weight: 600;
    font-size: 0.9rem;
    color: var(--gray-700);
}

/* ===== Buttons ===== */
.action-bar {
    display: flex;
    gap: 0.6rem;
    flex-wrap: wrap;
}

.btn {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.6rem 1.25rem;
    border: none;
    border-radius: var(--radius);
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition);
    font-family: inherit;
}

.btn:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-icon { width: 1.15rem; height: 1.15rem; }
.btn-icon-sm { width: 1rem; height: 1rem; }

.btn-primary {
    background: linear-gradient(135deg, var(--primary), var(--primary-hover));
    color: white;
    box-shadow: 0 2px 8px var(--primary-glow);
}
.btn-primary:hover:not(:disabled) {
    box-shadow: 0 4px 16px rgba(99, 102, 241, 0.3);
    transform: translateY(-1px);
}

.btn-secondary {
    background: white;
    color: var(--gray-600);
    border: 1.5px solid var(--gray-200);
}
.btn-secondary:hover:not(:disabled) {
    background: var(--gray-50);
    border-color: var(--gray-300);
}
.btn-secondary.active { background: var(--primary-light); border-color: var(--primary); color: var(--primary); }

.btn-ghost { background: transparent; color: var(--gray-400); }
.btn-ghost:hover { color: var(--danger); background: #fef2f2; }

.btn-sm { padding: 0.3rem 0.7rem; font-size: 0.8rem; }

/* ===== Modal ===== */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.6);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 1rem;
}

.modal-content {
    background: white;
    border-radius: var(--radius-xl);
    max-width: 90vw;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
    box-shadow: var(--shadow-lg);
    min-width: 500px;
    overflow: hidden;
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--gray-100);
}

.modal-header h3 { font-size: 1rem; font-weight: 600; }

.modal-close {
    width: 2rem;
    height: 2rem;
    border: none;
    background: var(--gray-100);
    border-radius: 50%;
    font-size: 1.15rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--gray-400);
    transition: all var(--transition);
}
.modal-close:hover { background: var(--gray-200); color: var(--gray-700); }

.modal-body {
    flex: 1;
    overflow: auto;
    padding: 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--gray-50);
}

.modal-body canvas {
    max-width: 100%;
    box-shadow: var(--shadow-md);
    border-radius: 4px;
}

.modal-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    padding: 0.75rem 1.5rem;
    border-top: 1px solid var(--gray-100);
}

#preview-page-info {
    font-size: 0.8rem;
    color: var(--gray-500);
    min-width: 100px;
    text-align: center;
    font-family: monospace;
}

/* ===== Loading Overlay ===== */
.loading-overlay {
    position: fixed;
    inset: 0;
    background: rgba(255, 255, 255, 0.85);
    backdrop-filter: blur(8px);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 2000;
    gap: 1rem;
}

.loading-spinner {
    width: 2.5rem;
    height: 2.5rem;
    border: 3px solid var(--gray-100);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
}

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

.loading-text { font-size: 0.9rem; color: var(--gray-500); }

/* ===== Toast Notifications ===== */
.toast {
    position: fixed;
    bottom: 2rem;
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    background: var(--gray-800);
    color: white;
    padding: 0.75rem 1.5rem;
    border-radius: var(--radius);
    font-size: 0.9rem;
    z-index: 3000;
    transition: transform 0.3s ease;
    box-shadow: var(--shadow-lg);
}

.toast.show { transform: translateX(-50%) translateY(0); }
.toast.toast-error { background: var(--danger); }

/* ===== Utilities ===== */
.hidden { display: none !important; }
.empty-state { text-align: center; color: var(--gray-300); padding: 1rem; font-size: 0.85rem; }

/* ===== Responsive ===== */
@media (max-width: 768px) {
    .app-header { padding: 2rem 1rem 1.5rem; }
    .app-header h1 { font-size: 1.4rem; }

    .app-container {
        padding: 0 0.75rem 3rem;
        margin-top: -0.5rem;
    }

    .panel { padding: 1rem; border-radius: var(--radius); }

    .upload-zone { padding: 1.5rem; }

    .modal-content {
        min-width: unset;
        width: 100%;
        border-radius: var(--radius-lg);
    }

    .action-bar { flex-direction: column; }
    .action-bar .btn { justify-content: center; }

    .config-group {
        flex-direction: column;
        align-items: flex-start;
    }
}
