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

:root {
    --bg: #f5f5f5;
    --panel: #ffffff;
    --panel-soft: #f8f8f8;
    --brand: #9aca38;
    --brand-deep: #2d5f4d;
    --ink: #333333;
    --line: #e0e0e0;
    --accent: #7e5eff;
    --accent-hover: #6f47e2;
    --viewer-bg: #e8e8e8;
}

body {
    font-family: Arial, sans-serif;
    height: 100vh;
    overflow: hidden;
    background-color: var(--bg);
}

#header-container {
    display: flex;
    background-color: var(--brand);
    height: 6.5vh;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    z-index: 10;
}

#logo-area {
    width: 70%;
    display: flex;
    align-items: center;
    padding: 8px 0;
    gap: 20px;
}

#logo-area img:first-child {
    height: 100%;
    max-height: 100%;
    object-fit: contain;
    padding-left: 20px;
    flex-shrink: 0;
}

#logo-area img:nth-child(2) {
    height: 100%;
    max-height: 100%;
    object-fit: contain;
    flex-shrink: 0;
}

#chat-panel-header {
    width: 30%;
    background-color: var(--brand-deep);
    color: #fff;
    font-weight: bold;
    font-size: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-left: 1px solid rgba(255, 255, 255, 0.1);
}

#main-container {
    display: flex;
    height: calc(93.5vh);
}

#pdf-panel {
    width: 70%;
    background-color: var(--panel);
    display: flex;
    flex-direction: column;
    border-right: 2px solid var(--line);
    position: relative;
}

#pdf-toolbar {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 50px;
    padding: 8px 12px;
    background: var(--panel-soft);
    border-bottom: 1px solid var(--line);
    flex-shrink: 0;
}

.pdf-toolbar-btn {
    border: 1px solid #c8c8c8;
    background: #ffffff;
    color: var(--ink);
    border-radius: 6px;
    padding: 6px 10px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    line-height: 1;
    transition: background 0.2s, border-color 0.2s;
}

.pdf-toolbar-btn:hover {
    background: #f0f0f0;
    border-color: #b0b0b0;
}

.pdf-toolbar-btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

#pdf-zoom-level {
    min-width: 54px;
    text-align: center;
    font-size: 13px;
    color: #4a4a4a;
    font-weight: 600;
}

.pdf-tabbar {
    display: flex;
    align-items: stretch;
    gap: 6px;
    padding: 8px 12px;
    border-bottom: 1px solid var(--line);
    background: linear-gradient(180deg, #f5f5f5 0%, #efefef 100%);
    overflow-x: auto;
}

.pdf-tabbar:empty::before {
    content: "Nessun PDF aperto";
    font-size: 13px;
    color: #6b6b6b;
    padding: 8px 4px;
}

.pdf-tab {
    border: 1px solid #d0d0d0;
    background: #f8f8f8;
    border-radius: 10px 10px 0 0;
    min-width: 180px;
    max-width: 260px;
    padding: 8px 10px;
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    transition: background 0.2s, border-color 0.2s;
}

.pdf-tab:hover {
    background: #ffffff;
    border-color: #b8b8b8;
}

.pdf-tab.active {
    background: #ffffff;
    border-color: #b8b8b8;
    box-shadow: inset 0 -2px 0 var(--brand);
}

.pdf-tab-label {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 13px;
    font-weight: 600;
    color: var(--ink);
}

.pdf-tab-close {
    border: none;
    background: transparent;
    color: #666666;
    font-size: 16px;
    line-height: 1;
    cursor: pointer;
    padding: 0;
}

.pdf-tab-close:hover {
    color: #111111;
}

#pdf-viewer-container {
    flex: 1;
    overflow: auto;
    background-color: var(--viewer-bg);
    padding: 16px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    cursor: grab;
}

#pdf-viewer-container.is-panning {
    cursor: grabbing;
}

.pdf-empty-state {
    margin: auto;
    color: #666666;
    font-size: 16px;
}

.pdf-page-wrapper {
    position: relative;
    display: inline-block;
    flex-shrink: 0;
    background: #ffffff;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
    border-radius: 3px;
}

.pdf-page-canvas {
    display: block;
    border-radius: 3px;
}

.pdf-highlight-canvas {
    position: absolute;
    top: 0;
    left: 0;
    pointer-events: none;
}

#pdf-viewer-container::-webkit-scrollbar,
#chat-messages::-webkit-scrollbar,
.pdf-tabbar::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

#pdf-viewer-container::-webkit-scrollbar-track,
#chat-messages::-webkit-scrollbar-track,
.pdf-tabbar::-webkit-scrollbar-track {
    background: #d0d0d0;
}

#pdf-viewer-container::-webkit-scrollbar-thumb,
#chat-messages::-webkit-scrollbar-thumb,
.pdf-tabbar::-webkit-scrollbar-thumb {
    background: #aaaaaa;
    border-radius: 4px;
}

#chat-panel {
    width: 30%;
    min-width: 0;
    display: flex;
    flex-direction: column;
    background-color: var(--panel);
    overflow: hidden;
}

#chat-messages {
    flex: 1;
    min-width: 0;
    padding: 8px 20px 20px 20px;
    background-color: #f9f9f9;
    overflow-y: auto;
    overflow-x: hidden;
    display: flex;
    flex-direction: column;
}

#chat-messages-wrapper {
    position: relative;
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

#chat-messages-wrapper::after {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 10px;
    background: linear-gradient(to bottom, #f9f9f9 0%, transparent 100%);
    pointer-events: none;
    z-index: 1;
}

.message-wrapper {
    margin: 8px 0;
    display: flex;
    align-items: flex-end;
    width: fit-content;
    max-width: 100%;
    min-width: 0;
}

.bot-wrapper { align-self: flex-start; flex-direction: row; }
.user-wrapper { align-self: flex-end; flex-direction: row-reverse; }

.message-content {
    min-width: 0;
    max-width: 100%;
    padding: 12px 15px;
    font-size: 18px;
    line-height: 1.4;
    position: relative;
    overflow-wrap: anywhere;
    word-break: break-word;
    transition: background-color 0.2s, border 0.2s;
}

.message-bot {
    background-color: var(--brand-deep);
    color: #dddddd;
    border-radius: 15px 15px 15px 2px;
    margin-right: 8px;
}

.message-bot.clickable:hover {
    background-color: #3a7d5c !important;
    border: 1px solid rgba(255, 255, 255, 0.3);
    cursor: pointer;
}

.message-bot.clickable {
    border: 1px solid transparent;
}

.message-user {
    background-color: #3b3b5c;
    color: #dddddd;
    border-radius: 15px 15px 2px 15px;
    margin-left: 8px;
}

.message-expired {
    background-color: #7a2020;
    color: #f5c6c6;
    border-radius: 15px 15px 15px 2px;
    margin-right: 8px;
    font-style: italic;
}

.message-error {
    background-color: #7a4020;
    color: #f5d6c6;
    border-radius: 15px 15px 15px 2px;
    margin-right: 8px;
    font-style: italic;
}

.timestamp {
    font-size: 12px;
    color: #aaaaaa;
    white-space: nowrap;
    margin-bottom: 2px;
}

#chat-input-container {
    display: flex;
    border-top: 2px solid var(--line);
    background-color: var(--panel);
}

#chat-input {
    flex: 1;
    padding: 15px;
    font-size: 16px;
    border: none;
    outline: none;
    color: var(--ink);
}

#chat-input:disabled {
    background-color: #f0f0f0;
    color: #aaaaaa;
    cursor: not-allowed;
}

#chat-send {
    background-color: var(--accent);
    color: #ffffff;
    border: none;
    padding: 0 25px;
    cursor: pointer;
    font-size: 16px;
    font-weight: bold;
    transition: 0.3s;
}

#chat-send:hover {
    background-color: var(--accent-hover);
}

#chat-send:disabled {
    background: #cccccc;
    cursor: not-allowed;
}

.document-list {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-top: 10px;
}

.document-chip {
    max-width: 100%;
    border: 1px solid rgba(255, 255, 255, 0.24);
    background: rgba(255, 255, 255, 0.08);
    color: #ffffff;
    border-radius: 8px;
    padding: 6px 8px;
    text-align: left;
    font-size: 12px;
    white-space: normal;
    overflow-wrap: anywhere;
    cursor: pointer;
}

.document-chip:hover {
    background: rgba(255, 255, 255, 0.16);
}

.debug-info {
    font-size: 11px;
    color: #aaaaaa;
    margin-top: 8px;
    padding: 8px;
    background-color: #2a2a3a;
    border-radius: 5px;
    text-align: left;
}

.debug-field { margin: 3px 0; }
.found-true { color: #4ade80; font-weight: bold; }
.found-false { color: #f87171; font-weight: bold; }

.loading {
    color: var(--brand-deep);
    padding: 10px;
    font-style: italic;
    font-size: 14px;
}

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

.token-span {
    animation: tokenFadeIn 0.18s ease-in forwards;
    display: inline;
    white-space: pre-wrap;
}

#pdf-loading {
    display: none;
    position: absolute;
    top: 55%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 110px;
    height: 110px;
    background-color: rgba(40, 40, 40, 0.72);
    border-radius: 18px;
    z-index: 100;
    align-items: center;
    justify-content: center;
}

#pdf-loading.visible {
    display: flex;
}

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

#pdf-loading-spinner {
    width: 52px;
    height: 52px;
    border: 5px solid rgba(255, 255, 255, 0.2);
    border-top-color: #ffffff;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@media (max-width: 1100px) {
    #main-container {
        flex-direction: column;
        height: auto;
    }

    #pdf-panel,
    #chat-panel,
    #logo-area,
    #chat-panel-header {
        width: 100%;
    }

    #pdf-panel {
        min-height: 55vh;
    }

    #chat-panel {
        min-height: 38vh;
    }

    #header-container {
        height: auto;
        flex-direction: column;
    }
}
