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

:root {
    --blue: #2563EB;
    --blue-dark: #1D4ED8;
    --blue-light: #DBEAFE;
    --blue-50: #EFF6FF;
    --purple: #7C3AED;
    --green: #10B981;
    --red: #EF4444;
    --orange: #F59E0B;
    --dark: #0F172A;
    --gray-50: #F8FAFC;
    --gray-100: #F1F5F9;
    --gray-200: #E2E8F0;
    --gray-300: #CBD5E1;
    --gray-400: #94A3B8;
    --gray-500: #64748B;
    --gray-700: #334155;
    --white: #FFFFFF;
    --gradient: linear-gradient(135deg, #2563EB, #7C3AED);
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.06);
    --shadow-md: 0 4px 16px rgba(0,0,0,0.08);
    --shadow-lg: 0 12px 40px rgba(0,0,0,0.1);
}

html, body {
    height: 100%;
    font-family: 'Inter', -apple-system, sans-serif;
    color: var(--dark);
    background: var(--gray-50);
    overflow: hidden;
}

h2, h3, h4 { font-family: 'Space Grotesk', sans-serif; }

/* ===== Top Bar ===== */
.topbar {
    height: 56px;
    background: var(--white);
    border-bottom: 1px solid var(--gray-200);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 100;
}

.topbar-logo {
    display: flex;
    align-items: center;
    gap: 8px;
    font-family: 'Space Grotesk', sans-serif;
    font-size: 1rem;
    color: var(--dark);
    text-decoration: none;
}

.topbar-logo strong { font-weight: 700; }

.topbar-center { display: flex; align-items: center; }

.tool-badge {
    padding: 4px 14px;
    background: var(--blue-50);
    color: var(--blue);
    border-radius: 50px;
    font-size: 0.8rem;
    font-weight: 600;
}

.topbar-actions {
    display: flex;
    align-items: center;
    gap: 10px;
}

.btn-action {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    background: var(--white);
    border: 1px solid var(--gray-200);
    border-radius: 8px;
    font-family: 'Inter', sans-serif;
    font-size: 0.82rem;
    font-weight: 600;
    color: var(--gray-700);
    cursor: pointer;
    transition: all 0.2s;
}

.btn-action:hover {
    background: var(--gray-50);
    border-color: var(--gray-300);
}

.btn-action.danger { color: var(--red); }
.btn-action.danger:hover { background: #FEF2F2; border-color: #FECACA; }

.btn-download {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 20px;
    background: var(--gradient);
    color: var(--white);
    border: none;
    border-radius: 8px;
    font-family: 'Inter', sans-serif;
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-download:hover { box-shadow: 0 4px 16px rgba(37,99,235,0.3); }
.btn-download:disabled { opacity: 0.5; cursor: not-allowed; }

/* ===== Tool Selector ===== */
.tool-selector {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: calc(100vh - 56px);
    margin-top: 56px;
    text-align: center;
    padding: 24px;
}

.tool-selector h2 {
    font-size: 2rem;
    margin-bottom: 8px;
}

.tool-selector > p {
    color: var(--gray-500);
    margin-bottom: 40px;
}

.tool-selector-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 16px;
    max-width: 800px;
}

.tool-select-card {
    background: var(--white);
    border: 2px solid var(--gray-200);
    border-radius: 16px;
    padding: 28px 20px;
    text-align: center;
    cursor: pointer;
    transition: all 0.25s;
    font-family: inherit;
}

.tool-select-card:hover {
    border-color: var(--blue);
    transform: translateY(-4px);
    box-shadow: var(--shadow-md);
}

.tsc-icon {
    width: 52px;
    height: 52px;
    border-radius: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 14px;
}

.tsc-icon.merge { background: var(--blue-light); color: var(--blue); }
.tsc-icon.edit { background: #FCE7F3; color: #EC4899; }
.tsc-icon.organize { background: #FEF3C7; color: #F59E0B; }
.tsc-icon.split { background: #EDE9FE; color: #7C3AED; }

.tool-select-card h3 {
    font-family: 'Inter', sans-serif;
    font-size: 0.95rem;
    font-weight: 600;
    margin-bottom: 4px;
}

.tool-select-card p {
    font-size: 0.78rem;
    color: var(--gray-500);
}

/* ===== Upload Area ===== */
.editor-upload {
    display: flex;
    align-items: center;
    justify-content: center;
    height: calc(100vh - 56px);
    margin-top: 56px;
    padding: 24px;
}

.upload-box {
    width: 100%;
    max-width: 560px;
    border: 2px dashed var(--gray-300);
    border-radius: 20px;
    padding: 8px;
    transition: all 0.3s;
    cursor: pointer;
}

.upload-box:hover, .upload-box.dragover {
    border-color: var(--blue);
    box-shadow: 0 0 0 4px rgba(37,99,235,0.08);
}

.upload-box.dragover { background: var(--blue-50); }

.upload-box-inner {
    background: var(--gray-100);
    border-radius: 14px;
    padding: 56px 32px;
    text-align: center;
}

.upload-box-inner h3 {
    font-size: 1.2rem;
    margin: 16px 0 6px;
    color: var(--dark);
}

.upload-box-inner p {
    font-size: 0.9rem;
    color: var(--gray-500);
}

.browse-link {
    color: var(--blue);
    font-weight: 600;
    cursor: pointer;
}

.browse-link:hover { text-decoration: underline; }

.upload-hint {
    display: block;
    margin-top: 16px;
    font-size: 0.75rem;
    color: var(--gray-400);
}

/* ===== Workspace Layout ===== */
.workspace {
    display: flex;
    height: calc(100vh - 56px);
    margin-top: 56px;
}

/* Sidebar */
.sidebar {
    width: 200px;
    background: var(--white);
    border-right: 1px solid var(--gray-200);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.sidebar-header {
    padding: 14px 16px;
    border-bottom: 1px solid var(--gray-200);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.sidebar-header h4 {
    font-family: 'Inter', sans-serif;
    font-size: 0.85rem;
    font-weight: 600;
}

.page-count {
    font-size: 0.72rem;
    color: var(--gray-400);
    font-weight: 500;
}

.thumbnail-list {
    flex: 1;
    overflow-y: auto;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.thumbnail-item {
    position: relative;
    border: 2px solid var(--gray-200);
    border-radius: 8px;
    overflow: hidden;
    cursor: pointer;
    transition: all 0.2s;
    background: white;
}

.thumbnail-item:hover { border-color: var(--gray-300); }
.thumbnail-item.active { border-color: var(--blue); box-shadow: 0 0 0 2px rgba(37,99,235,0.15); }

.thumbnail-item canvas {
    width: 100%;
    display: block;
}

.thumbnail-label {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0,0,0,0.6);
    color: white;
    font-size: 0.65rem;
    font-weight: 600;
    text-align: center;
    padding: 3px 0;
}

/* ===== Main Canvas ===== */
.main-canvas {
    flex: 1;
    overflow: auto;
    display: flex;
    flex-direction: column;
}

/* Edit Workspace */
.edit-workspace {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.edit-toolbar {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 8px 16px;
    background: var(--white);
    border-bottom: 1px solid var(--gray-200);
}

.edit-tool {
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    background: transparent;
    border-radius: 8px;
    cursor: pointer;
    color: var(--gray-500);
    transition: all 0.15s;
}

.edit-tool:hover { background: var(--gray-100); color: var(--dark); }
.edit-tool.active { background: var(--blue-light); color: var(--blue); }

.edit-divider {
    width: 1px;
    height: 24px;
    background: var(--gray-200);
    margin: 0 8px;
}

.color-picker-wrap, .font-size-wrap {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.78rem;
    font-weight: 500;
    color: var(--gray-500);
}

.color-input {
    width: 28px;
    height: 28px;
    border: 2px solid var(--gray-200);
    border-radius: 6px;
    cursor: pointer;
    padding: 0;
    background: none;
}

.size-select {
    padding: 4px 8px;
    border: 1px solid var(--gray-200);
    border-radius: 6px;
    font-size: 0.8rem;
    font-family: 'Inter', sans-serif;
    color: var(--dark);
    cursor: pointer;
}

.canvas-container {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: auto;
    padding: 24px;
    position: relative;
    background: var(--gray-100);
}

.canvas-container canvas {
    box-shadow: var(--shadow-lg);
    background: white;
}

.annotation-layer {
    position: absolute;
    top: 0;
    left: 0;
    pointer-events: none;
}

.annotation-layer.active { pointer-events: auto; }

.annotation-text {
    position: absolute;
    background: transparent;
    border: 1px dashed var(--blue);
    padding: 2px 4px;
    font-family: 'Inter', sans-serif;
    outline: none;
    cursor: move;
    min-width: 20px;
    min-height: 20px;
    z-index: 5;
}

.annotation-text:focus {
    border-color: var(--blue);
    background: rgba(37, 99, 235, 0.03);
}

.annotation-highlight {
    position: absolute;
    background: rgba(252, 211, 77, 0.35);
    border-radius: 2px;
    z-index: 4;
}

.annotation-drawing {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 3;
}

.page-nav {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 16px;
    padding: 10px;
    background: var(--white);
    border-top: 1px solid var(--gray-200);
}

.page-nav-btn {
    width: 34px;
    height: 34px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--gray-200);
    border-radius: 8px;
    background: var(--white);
    cursor: pointer;
    color: var(--gray-500);
    transition: all 0.15s;
}

.page-nav-btn:hover { background: var(--gray-50); color: var(--dark); }

#pageInfo {
    font-size: 0.82rem;
    font-weight: 600;
    color: var(--gray-700);
}

/* ===== Merge Workspace ===== */
.merge-workspace {
    flex: 1;
    padding: 32px;
    overflow: auto;
}

.merge-file-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
    max-width: 600px;
    margin: 0 auto;
}

.merge-file-item {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 16px 20px;
    background: var(--white);
    border: 1px solid var(--gray-200);
    border-radius: 12px;
    cursor: grab;
    transition: all 0.2s;
}

.merge-file-item:hover {
    border-color: var(--gray-300);
    box-shadow: var(--shadow-sm);
}

.merge-file-item.dragging {
    opacity: 0.5;
    border-color: var(--blue);
}

.merge-file-item .drag-handle {
    color: var(--gray-400);
    cursor: grab;
    display: flex;
}

.merge-file-icon {
    width: 40px;
    height: 40px;
    background: #FEE2E2;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.merge-file-icon svg { color: #EF4444; }

.merge-file-info {
    flex: 1;
}

.merge-file-info strong {
    display: block;
    font-size: 0.88rem;
    font-weight: 600;
}

.merge-file-info span {
    font-size: 0.75rem;
    color: var(--gray-400);
}

.merge-file-remove {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    background: transparent;
    border-radius: 6px;
    cursor: pointer;
    color: var(--gray-400);
    transition: all 0.15s;
}

.merge-file-remove:hover { background: #FEE2E2; color: var(--red); }

.merge-actions {
    text-align: center;
    margin-top: 24px;
}

/* ===== Organize Workspace ===== */
.organize-workspace {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.organize-toolbar {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 20px;
    background: var(--white);
    border-bottom: 1px solid var(--gray-200);
}

.organize-hint {
    margin-left: auto;
    font-size: 0.78rem;
    color: var(--gray-400);
}

.organize-grid, .split-grid {
    flex: 1;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 16px;
    padding: 24px;
    overflow: auto;
    align-content: start;
}

.page-thumb {
    position: relative;
    border: 2px solid var(--gray-200);
    border-radius: 10px;
    overflow: hidden;
    cursor: pointer;
    transition: all 0.2s;
    background: white;
    aspect-ratio: 0.75;
    display: flex;
    align-items: center;
    justify-content: center;
}

.page-thumb:hover { border-color: var(--gray-300); }
.page-thumb.selected { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(37,99,235,0.15); }
.page-thumb.dragging { opacity: 0.4; }
.page-thumb.drag-over { border-color: var(--blue); border-style: dashed; }

.page-thumb canvas {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
}

.page-thumb-label {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0,0,0,0.65);
    color: white;
    font-size: 0.7rem;
    font-weight: 600;
    text-align: center;
    padding: 4px 0;
}

.page-thumb-check {
    position: absolute;
    top: 6px;
    right: 6px;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    border: 2px solid var(--gray-300);
    background: white;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s;
}

.page-thumb.selected .page-thumb-check {
    background: var(--blue);
    border-color: var(--blue);
}

.page-thumb.selected .page-thumb-check::after {
    content: '';
    width: 6px;
    height: 10px;
    border: solid white;
    border-width: 0 2px 2px 0;
    transform: rotate(45deg) translate(-1px, -1px);
}

.page-thumb-rotate {
    position: absolute;
    top: 6px;
    left: 6px;
    width: 24px;
    height: 24px;
    border-radius: 6px;
    background: rgba(0,0,0,0.5);
    color: white;
    border: none;
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
    font-size: 0.7rem;
}

.page-thumb:hover .page-thumb-rotate { display: flex; }

/* ===== Split Workspace ===== */
.split-workspace {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.split-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 20px;
    background: var(--white);
    border-bottom: 1px solid var(--gray-200);
}

.split-hint {
    font-size: 0.82rem;
    color: var(--gray-500);
}

/* ===== Loading ===== */
.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: 1000;
    gap: 16px;
}

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

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

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

/* ===== Toast ===== */
.toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%) translateY(80px);
    background: var(--dark);
    color: white;
    padding: 12px 24px;
    border-radius: 10px;
    font-size: 0.85rem;
    font-weight: 500;
    z-index: 2000;
    transition: transform 0.3s ease;
    box-shadow: var(--shadow-lg);
}

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

/* ===== Responsive ===== */
@media (max-width: 768px) {
    .tool-selector-grid {
        grid-template-columns: repeat(2, 1fr);
    }

    .sidebar { width: 140px; }

    .organize-grid, .split-grid {
        grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
        gap: 10px;
        padding: 16px;
    }

    .topbar-logo span { display: none; }
    .btn-action span { display: none; }
}

@media (max-width: 480px) {
    .tool-selector-grid { grid-template-columns: 1fr; max-width: 300px; }
    .sidebar { display: none; }
    .edit-toolbar { flex-wrap: wrap; }
}
