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

body {
    font-family: 'Georgia', serif;
    background: 
        repeating-linear-gradient(
            90deg,
            #3d2817 0px,
            #4a3320 2px,
            #3d2817 4px
        ),
        linear-gradient(180deg, #5c4a37 0%, #4a3826 50%, #5c4a37 100%);
    height: 100vh;
    width: 100vw;
    max-width: 100vw;
    display: flex;
    flex-direction: row;
    gap: 20px;
    padding: 20px;
    box-sizing: border-box;
    overflow-x: auto;
    overflow-y: hidden;
}

.grimoire-container {
    flex: 1 1 0;
    min-width: 0;
}

.book {
    display: flex;
    position: relative;
    width: 100%;
    height: 85vh;
    max-width: 1400px;
    max-height: 800px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.8);
    border-radius: 8px;
}

.page {
    flex: 1;
    background: #e8ddc0;
    padding: 40px;
    position: relative;
    overflow: hidden;
    border: 5px solid #6b2e2e;
    box-shadow: 
        inset 0 0 0 2px #c5a572,
        inset 0 0 30px rgba(107, 46, 46, 0.1),
        0 8px 20px rgba(0, 0, 0, 0.5);
}

.page-left {
    border-radius: 8px 0 0 8px;
    border-right: none;
    background-image: 
        radial-gradient(ellipse at 20% 30%, rgba(139, 90, 43, 0.08) 0%, transparent 30%),
        radial-gradient(ellipse at 80% 70%, rgba(139, 90, 43, 0.06) 0%, transparent 25%),
        repeating-linear-gradient(
            0deg,
            transparent,
            transparent 30px,
            rgba(139, 90, 43, 0.12) 30px,
            rgba(139, 90, 43, 0.12) 31px
        ),
        radial-gradient(circle at top left, rgba(197, 165, 114, 0.15) 0%, transparent 60px),
        radial-gradient(circle at bottom left, rgba(197, 165, 114, 0.15) 0%, transparent 60px);
}

.page-right {
    border-radius: 0 8px 8px 0;
    border-left: none;
    background-image: 
        radial-gradient(ellipse at 75% 40%, rgba(139, 90, 43, 0.08) 0%, transparent 30%),
        radial-gradient(ellipse at 15% 65%, rgba(139, 90, 43, 0.06) 0%, transparent 25%),
        repeating-linear-gradient(
            0deg,
            transparent,
            transparent 30px,
            rgba(139, 90, 43, 0.12) 30px,
            rgba(139, 90, 43, 0.12) 31px
        ),
        radial-gradient(circle at top right, rgba(197, 165, 114, 0.15) 0%, transparent 60px),
        radial-gradient(circle at bottom right, rgba(197, 165, 114, 0.15) 0%, transparent 60px);
}

.book-spine {
    width: 30px;
    background: 
        linear-gradient(to right, #5a2e2e 0%, #7a3e3e 50%, #5a2e2e 100%),
        repeating-linear-gradient(
            0deg,
            transparent,
            transparent 20px,
            rgba(197, 165, 114, 0.3) 20px,
            rgba(197, 165, 114, 0.3) 22px
        );
    box-shadow: inset 0 0 15px rgba(0, 0, 0, 0.7), inset 2px 0 3px rgba(197, 165, 114, 0.4);
    position: relative;
    border-left: 2px solid #c5a572;
    border-right: 2px solid #c5a572;
}

.book-spine::before {
    content: '';
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 3px;
    height: 100%;
    background: linear-gradient(to bottom, 
        transparent 10%,
        #c5a572 15%,
        transparent 20%,
        transparent 30%,
        #c5a572 35%,
        transparent 40%,
        transparent 50%,
        #c5a572 55%,
        transparent 60%,
        transparent 70%,
        #c5a572 75%,
        transparent 80%,
        transparent 90%
    );
    box-shadow: 0 0 4px rgba(197, 165, 114, 0.6);
}

.page-content {
    height: 100%;
    display: flex;
    flex-direction: column;
    position: relative;
}

.page-title {
    font-size: 28px;
    color: #3d2817;
    margin-bottom: 20px;
    text-align: center;
    font-weight: bold;
    text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
    border-bottom: 3px double #c5a572;
    padding-bottom: 10px;
    position: relative;
}

#leftPageCanvas,
#rightPageCanvas {
    flex: 1;
    position: relative;
    overflow: hidden;
    background: rgba(255, 255, 255, 0.15);
    border: 2px solid #a89168;
    border-radius: 4px;
    cursor: grab;
}

#leftPageCanvas:active,
#rightPageCanvas:active {
    cursor: grabbing;
}

.page-nav {
    position: absolute;
    bottom: 20px;
    width: 50px;
    height: 50px;
    background: linear-gradient(135deg, #a89168 0%, #8b6f47 100%);
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    transition: all 0.3s ease;
    color: #f5f1e8;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.4);
    border: 2px solid #6d5a3d;
    z-index: 10;
}

.page-nav:hover {
    background: linear-gradient(135deg, #c5a572 0%, #a89168 100%);
    transform: scale(1.1);
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.5);
}

.page-nav-next {
    right: 20px;
}

.page-nav-prev {
    left: 20px;
    bottom: 20px;
}

.page-nav.disabled {
    opacity: 0.3;
    cursor: not-allowed;
    pointer-events: none;
}

/* Skill Tree Nodes */
.skill-node {
    position: absolute;
    background: linear-gradient(135deg, #8b6f47 0%, #a89168 100%);
    color: #f5f1e8;
    padding: 12px 18px;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.4), inset 0 1px 0 rgba(255, 255, 255, 0.2);
    font-size: 14px;
    font-weight: bold;
    border: 2px solid #6d5a3d;
    text-align: center;
    min-width: 100px;
    user-select: none;
    white-space: nowrap;
    text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.3);
}

.skill-node:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.5), 0 0 15px rgba(197, 165, 114, 0.5), inset 0 1px 0 rgba(255, 255, 255, 0.3);
    background: linear-gradient(135deg, #a89168 0%, #c5a572 100%);
    z-index: 100;
}

.skill-node.root {
    background: linear-gradient(135deg, #5a4a32 0%, #6d5a3d 100%);
    font-size: 18px;
    padding: 16px 24px;
    border: 3px solid #3d2f1f;
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.6), 0 0 20px rgba(197, 165, 114, 0.3), inset 0 2px 0 rgba(255, 255, 255, 0.1);
}

.skill-node.has-notes::after {
    content: '📖';
    position: absolute;
    top: -8px;
    right: -8px;
    font-size: 16px;
    background: #f5f1e8;
    border: 2px solid #a89168;
    border-radius: 50%;
    width: 24px;
    height: 24px;
    display: flex;
    justify-content: center;
    align-items: center;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.4);
}

/* SVG Canvas for connections */
.connections-svg {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 0;
}

.skill-connection {
    stroke: #8b6f47;
    stroke-width: 2.5;
    fill: none;
    opacity: 0.6;
}

/* Modal */
.modal {
    display: none;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.7);
    animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.modal-content {
    background: #e8ddc0;
    margin: 5% auto;
    padding: 30px;
    border: 5px solid #6b2e2e;
    border-radius: 8px;
    width: 90%;
    max-width: 600px;
    box-shadow: 
        inset 0 0 0 2px #c5a572,
        0 20px 60px rgba(0, 0, 0, 0.8);
    animation: slideDown 0.3s ease;
}

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

.close {
    color: #5a4a32;
    float: right;
    font-size: 32px;
    font-weight: bold;
    cursor: pointer;
    line-height: 20px;
}

.close:hover {
    color: #3d2817;
}

#modalTitle {
    color: #3d2817;
    margin-bottom: 20px;
    font-size: 24px;
    border-bottom: 3px double #c5a572;
    padding-bottom: 10px;
}

.modal-body label {
    display: block;
    color: #3d2817;
    font-weight: bold;
    margin-top: 15px;
    margin-bottom: 5px;
}

.modal-body input,
.modal-body textarea {
    width: 100%;
    padding: 10px;
    border: 2px solid #a89168;
    border-radius: 4px;
    background: rgba(255, 255, 255, 0.9);
    font-family: 'Georgia', serif;
    font-size: 14px;
    color: #3d2817;
}

.modal-body input:focus,
.modal-body textarea:focus {
    outline: none;
    border-color: #c5a572;
    background: rgba(255, 255, 255, 1);
    box-shadow: 0 0 8px rgba(197, 165, 114, 0.3);
}

.modal-actions {
    margin-top: 20px;
    display: flex;
    gap: 10px;
    justify-content: flex-end;
}

/* Buttons */
.btn {
    padding: 10px 20px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-family: 'Georgia', serif;
    font-weight: bold;
    font-size: 14px;
    transition: all 0.3s ease;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.btn-primary {
    background: linear-gradient(135deg, #8b6f47 0%, #a89168 100%);
    color: #f5f1e8;
    border: 2px solid #6d5a3d;
}

.btn-primary:hover {
    background: linear-gradient(135deg, #a89168 0%, #c5a572 100%);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.4);
}

.btn-secondary {
    background: linear-gradient(135deg, #5a4a32 0%, #6d5a3d 100%);
    color: #f5f1e8;
    border: 2px solid #4a3826;
}

.btn-secondary:hover {
    background: linear-gradient(135deg, #6d5a3d 0%, #8b6f47 100%);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.4);
}

.btn-danger {
    background: linear-gradient(135deg, #8b4e5e 0%, #a0566b 100%);
    color: #f5f1e8;
    border: 2px solid #6d3e4d;
}

.btn-danger:hover {
    background: linear-gradient(135deg, #a0566b 0%, #b55e78 100%);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.4);
}

/* Control Panel */
.control-panel {
    position: fixed;
    top: 20px;
    right: 20px;
    display: flex;
    gap: 10px;
    flex-direction: column;
    z-index: 500;
}

.control-panel .btn {
    min-width: 150px;
}

/* Table of Contents */
.toc-list {
    list-style: none;
    padding: 0;
}

.toc-item {
    padding: 15px;
    margin: 10px 0;
    background: rgba(139, 111, 71, 0.15);
    border: 2px solid #a89168;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.3s ease;
    color: #3d2817;
    font-weight: bold;
}

.toc-item:hover {
    background: rgba(139, 111, 71, 0.25);
    transform: translateX(5px);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.toc-item.active {
    background: rgba(139, 111, 71, 0.35);
    border-color: #8b6f47;
    box-shadow: 0 0 12px rgba(197, 165, 114, 0.4);
}

/* Zoom indicator */
.zoom-indicator {
    position: absolute;
    bottom: 80px;
    right: 20px;
    background: rgba(139, 111, 71, 0.9);
    color: #f5f1e8;
    padding: 8px 12px;
    border-radius: 4px;
    border: 2px solid #6d5a3d;
    font-size: 12px;
    pointer-events: none;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.4);
}

/* AI Chat Panel */
.chat-panel {
    width: 100%;
    max-width: 400px;
    min-width: 280px;
    flex-basis: 400px;
    height: 85vh;
    max-height: 800px;
    background: #e8ddc0;
    border: 5px solid #6b2e2e;
    border-radius: 8px;
    box-shadow: 
        inset 0 0 0 2px #c5a572,
        0 10px 30px rgba(0, 0, 0, 0.6);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    overflow: hidden;
}

.chat-panel.active {
    display: flex; /* Always visible now */
}

.chat-header {
    background: linear-gradient(135deg, #6b2e2e 0%, #7a3e3e 100%);
    color: #f5f1e8;
    padding: 15px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 3px solid #c5a572;
    border-radius: 5px 0 0 0;
    box-shadow: inset 0 1px 0 rgba(197, 165, 114, 0.3);
    flex-shrink: 0;
}

.chat-header h3 {
    margin: 0;
    font-size: 18px;
}

.close-chat {
    background: none;
    border: none;
    color: #f0ede4;
    font-size: 28px;
    cursor: pointer;
    line-height: 1;
    padding: 0;
    width: 30px;
    height: 30px;
}

.close-chat:hover {
    color: #fff;
}

.chat-messages {
    flex: 1;
    min-height: 0;
    max-height: 100%;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 20px;
    background: rgba(255, 255, 255, 0.3);
    width: 100%;
    display: flex;
    flex-direction: column;
}

.chat-message {
    margin-bottom: 15px;
    animation: messageSlideIn 0.3s ease;
}

@keyframes messageSlideIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.chat-message.user {
    text-align: right;
}

.chat-message.assistant {
    text-align: left;
}

.chat-message.system {
    text-align: center;
}

.message-bubble {
    display: inline-block;
    max-width: 85%;
    padding: 12px 16px;
    border-radius: 8px;
    word-wrap: break-word;
    word-break: break-word;
    overflow-wrap: break-word;
    white-space: pre-wrap;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.chat-message.user .message-bubble {
    background: linear-gradient(135deg, #8b6f47 0%, #a89168 100%);
    color: #f5f1e8;
    border: 2px solid #6d5a3d;
}

.chat-message.assistant .message-bubble {
    background: rgba(255, 255, 255, 0.95);
    color: #3d2817;
    border: 2px solid #a89168;
}

.chat-message.system .message-bubble {
    background: rgba(139, 111, 71, 0.2);
    color: #5a4a32;
    border: 1px solid #a89168;
    font-style: italic;
    font-size: 12px;
    padding: 8px 12px;
}

.message-label {
    font-size: 11px;
    color: #5a4a32;
    margin-bottom: 4px;
    font-weight: bold;
}

.chat-input-container {
    padding: 15px;
    border-top: 2px solid #a89168;
    background: rgba(255, 255, 255, 0.3);
    display: flex;
    gap: 10px;
    flex-shrink: 0;
    position: sticky;
    bottom: 0;
    z-index: 2;
}

#chatInput {
    flex: 1;
    padding: 10px;
    border: 2px solid #a89168;
    border-radius: 4px;
    background: rgba(255, 255, 255, 0.95);
    font-family: 'Georgia', serif;
    font-size: 14px;
    color: #3d2817;
    resize: none;
}

#chatInput:focus {
    outline: none;
    border-color: #c5a572;
    box-shadow: 0 0 8px rgba(197, 165, 114, 0.3);
}

#sendChatBtn {
    align-self: flex-end;
    padding: 10px 20px;
}

#sendChatBtn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.chat-footer {
    padding: 10px 15px;
    border-top: 1px solid #a89168;
    display: flex;
    gap: 10px;
    justify-content: space-between;
    background: rgba(139, 111, 71, 0.1);
}

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

/* Skill update notification */
.skill-notification {
    position: fixed;
    top: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: linear-gradient(135deg, #8b6f47 0%, #a89168 100%);
    color: #f5f1e8;
    padding: 15px 25px;
    border-radius: 6px;
    border: 2px solid #6d5a3d;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5), 0 0 20px rgba(197, 165, 114, 0.3);
    z-index: 1001;
    animation: notificationSlide 0.5s ease;
    font-weight: bold;
}

@keyframes notificationSlide {
    from {
        opacity: 0;
        transform: translateX(-50%) translateY(-20px);
    }
    to {
        opacity: 1;
        transform: translateX(-50%) translateY(0);
    }
}

.skill-notification.fade-out {
    animation: notificationFadeOut 0.5s ease forwards;
}

@keyframes notificationFadeOut {
    to {
        opacity: 0;
        transform: translateX(-50%) translateY(-20px);
    }
}

/* Loading indicator */
.loading-dots {
    display: inline-block;
}

.loading-dots::after {
    content: '...';
    animation: loadingDots 1.5s infinite;
}

@keyframes loadingDots {
    0%, 20% { content: '.'; }
    40% { content: '..'; }
    60%, 100% { content: '...'; }
}

/* API Key Modal adjustments */
#apiKeyModal .modal-content {
    max-width: 500px;
}

#apiKeyModal a {
    text-decoration: underline;
}

#apiKeyModal a:hover {
    color: #c5a572;
}

#apiKeyModal ol li {
    margin-bottom: 8px;
}

/* Mobile Responsive Design */
@media (max-width: 768px) {
    body {
        padding: 10px;
        overflow-x: auto;
        flex-direction: column;
        gap: 10px;
    }
    
    .grimoire-container {
        width: 1200px;
        min-width: 1200px;
        overflow-x: visible;
    }
    
    .book {
        width: 1200px;
        min-width: 1200px;
    }
    
    .chat-panel {
        width: 100%;
        min-width: 100%;
        height: 500px;
        position: relative;
        top: 0;
    }
    
    .controls {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        top: auto;
        gap: 5px;
        padding: 10px;
        background: rgba(232, 221, 192, 0.98);
        border-top: 3px solid #6b2e2e;
        box-shadow: 0 -5px 15px rgba(0, 0, 0, 0.3);
        z-index: 700;
    }
    
    .controls > div {
        font-size: 11px;
        padding: 5px;
    }
    
    #syncStatus {
        margin-top: 5px;
        padding: 5px;
        font-size: 10px;
    }
    
    /* Mobile Modal */
    .modal-content {
        width: 95%;
        margin: 10% auto;
        padding: 20px;
        max-width: none;
    }
    
    #modalTitle {
        font-size: 20px;
    }
    
    .modal-body input,
    .modal-body textarea {
        font-size: 16px; /* Prevent zoom on iOS */
    }
    
    .modal-actions {
        flex-direction: column;
    }
    
    .modal-actions .btn {
        width: 100%;
    }
    
    /* Mobile Skill Nodes */
    .skill-node {
        font-size: 12px;
        padding: 10px 14px;
        min-width: 80px;
        cursor: pointer;
        touch-action: none;
    }
    
    .skill-node.root {
        font-size: 16px;
        padding: 14px 20px;
    }
    
    .skill-node.has-notes::after {
        font-size: 14px;
        width: 20px;
        height: 20px;
        top: -6px;
        right: -6px;
    }
    
    /* Mobile TOC */
    .toc-item {
        padding: 12px;
        margin: 8px 0;
        font-size: 14px;
    }
    
    /* Notifications on mobile */
    .skill-notification {
        top: 10px;
        padding: 10px 15px;
        font-size: 12px;
        max-width: 90%;
    }
}

/* Small phones */
@media (max-width: 480px) {
    .page-title {
        font-size: 18px;
    }
    
    .skill-node {
        font-size: 11px;
        padding: 8px 12px;
        min-width: 70px;
    }
    
    .skill-node.root {
        font-size: 14px;
        padding: 12px 16px;
    }
    
    .controls .btn {
        font-size: 11px;
        padding: 6px 10px;
    }
}
