:root {
    --parchment-base: #f4ecd8;
    --parchment-dark: #e6dac3;
    --ink-main: #2c363f;
    --ink-faded: #5a6673;
    --ink-red: #8b3a3a;
    --font-serif: "EB Garamond", serif;
    --font-sans: "Outfit", sans-serif;
}

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

body {
    background-color: var(--parchment-base);
    /* Procedural SVG noise to simulate paper grain */
    background-image: url("data:image/svg+xml,%3Csvg width='200' height='200' viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.65' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noise)' opacity='0.06'/%3E%3C/svg%3E");
    color: var(--ink-main);
    font-family: var(--font-sans);
    display: flex;
    justify-content: center;
    height: 100vh;
    padding: 2vh 2vw;
    overflow: hidden;
    position: relative;
}

/* Faded Vignette Edge */
body::after {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    box-shadow: inset 0 0 150px rgba(44, 54, 63, 0.15);
    pointer-events: none;
    z-index: 100;
}

/* Add this below your body and body::after rules */
.layout-wrapper {
    display: flex;
    gap: 20px;
    width: 100%;
    max-width: 1300px; /* Expanded to fit both chat and sidebar */
    height: 100%;
    z-index: 10;
    position: relative;
}

/* Update your existing .container rule to flex dynamically */
.container {
    flex: 1; /* Takes up remaining space */
    display: flex;
    flex-direction: column;
    background: rgba(244, 236, 216, 0.6);
    backdrop-filter: blur(4px);
    border-radius: 4px;
    border: 1px solid rgba(44, 54, 63, 0.2);
    box-shadow:
        0 20px 40px rgba(0, 0, 0, 0.05),
        inset 0 0 20px rgba(0, 0, 0, 0.02);
    position: relative;
    overflow: hidden;
    max-width: 1100px;
}

/* New Sidebar Styles */
.sidebar {
    width: 280px;
    flex-shrink: 0;
    background: rgba(244, 236, 216, 0.6);
    backdrop-filter: blur(4px);
    border-radius: 4px;
    border: 1px solid rgba(44, 54, 63, 0.2);
    box-shadow:
        0 20px 40px rgba(0, 0, 0, 0.05),
        inset 0 0 20px rgba(0, 0, 0, 0.02);
    padding: 25px 20px;
    display: flex;
    flex-direction: column;
    gap: 25px;
    overflow-y: auto;
}

.sidebar h2 {
    font-family: var(--font-serif);
    color: var(--ink-main);
    font-size: 22px;
    border-bottom: 1px solid rgba(44, 54, 63, 0.15);
    padding-bottom: 10px;
    margin-bottom: 5px;
}

.sidebar-nav {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.sidebar-nav a {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 15px;
    color: var(--ink-main);
    text-decoration: none;
    font-family: var(--font-serif);
    font-size: 18px;
    font-weight: 600;
    border: 1px solid transparent;
    border-radius: 3px;
    transition: all 0.2s;
}

.sidebar-nav a:hover,
.sidebar-nav a.active {
    border-color: rgba(44, 54, 63, 0.3);
    background: rgba(44, 54, 63, 0.05);
}

.sidebar-info {
    font-family: var(--font-sans);
    font-size: 13.5px;
    color: var(--ink-faded);
    line-height: 1.6;
    padding: 15px;
    background: rgba(44, 54, 63, 0.03);
    border: 1px solid rgba(44, 54, 63, 0.1);
    border-radius: 3px;
    margin-top: auto; /* Pushes this box to the bottom */
}

/* Brand Header */
.brand-header {
    padding: 25px 20px;
    text-align: center;
    position: relative;
    border-bottom: 1px solid rgba(44, 54, 63, 0.15);
}

.brand-header h1 {
    font-family: var(--font-serif);
    color: var(--ink-main);
    font-size: 38px;
    letter-spacing: 1px;
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    margin-bottom: 4px;
}

.brand-header p {
    font-family: var(--font-serif);
    font-size: 16px;
    font-style: italic;
    color: var(--ink-faded);
    letter-spacing: 0.5px;
}

/* Config Bar (The Nav Station) */
.config-bar {
    padding: 12px 25px;
    background: rgba(0, 0, 0, 0.02);
    border-bottom: 1px solid rgba(44, 54, 63, 0.15);
    display: flex;
    gap: 15px;
    align-items: flex-start;
    flex-wrap: wrap;
}

select,
input,
textarea {
    padding: 10px 14px;
    border-radius: 3px;
    border: 1px solid rgba(44, 54, 63, 0.3);
    background: transparent;
    color: var(--ink-main);
    font-size: 14px;
    font-family: var(--font-sans);
    outline: none;
    transition: all 0.3s ease;
}

select:focus,
input:focus,
textarea:focus {
    border-color: var(--ink-main);
    background: rgba(255, 255, 255, 0.3);
}

input::placeholder,
textarea::placeholder {
    color: var(--ink-faded);
    opacity: 0.7;
}

button {
    background: transparent;
    color: var(--ink-main);
    font-family: var(--font-serif);
    font-weight: 600;
    font-size: 16px;
    cursor: pointer;
    border: 1px solid var(--ink-main);
    border-radius: 3px;
    padding: 8px 18px;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 8px;
}

button:hover:not(:disabled) {
    background: var(--ink-main);
    color: var(--parchment-base);
}

button:disabled {
    color: rgba(44, 54, 63, 0.4);
    border-color: rgba(44, 54, 63, 0.2);
    cursor: not-allowed;
}

/* Chat Window Background SVG Watermark */
.chat-container-wrapper {
    flex: 1;
    position: relative;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* Faded Compass Rose Watermark */
.watermark {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 350px;
    opacity: 0.04;
    pointer-events: none;
    z-index: 0;
}

.chat-box {
    flex: 1;
    padding: 30px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 24px;
    scroll-behavior: smooth;
    z-index: 1;
}

/* Custom Scrollbar */
.chat-box::-webkit-scrollbar {
    width: 6px;
}
.chat-box::-webkit-scrollbar-track {
    background: transparent;
}
.chat-box::-webkit-scrollbar-thumb {
    background: rgba(44, 54, 63, 0.2);
    border-radius: 3px;
}
.chat-box::-webkit-scrollbar-thumb:hover {
    background: rgba(44, 54, 63, 0.4);
}

.message {
    max-width: 80%;
    padding: 16px 24px;
    border-radius: 2px;
    line-height: 1.6;
    font-size: 15px;
    position: relative;
    animation: fadeIn 0.4s ease-out forwards;
}

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

.message.user {
    align-self: flex-end;
    background: rgba(44, 54, 63, 0.06);
    border: 1px solid rgba(44, 54, 63, 0.15);
    color: var(--ink-main);
    border-bottom-right-radius: 0;
}

.message.ai {
    align-self: flex-start;
    background: transparent;
    border-left: 2px solid var(--ink-red);
    padding-left: 20px;
    color: var(--ink-main);
}

/* Loading Animation - Hourglass */
.thinking-container {
    display: flex;
    align-items: center;
    gap: 12px;
    color: var(--ink-faded);
    font-family: var(--font-serif);
    font-style: italic;
    font-size: 16px;
}

.spin-helm {
    animation: flip 2s cubic-bezier(0.4, 0, 0.2, 1) infinite;
    width: 20px;
    height: 20px;
    stroke: var(--ink-faded);
}

@keyframes flip {
    0% {
        transform: rotate(0deg);
    }
    50% {
        transform: rotate(180deg);
    }
    100% {
        transform: rotate(180deg);
    }
}

/* Welcome Quote Styling */
.quote-box {
    text-align: left;
    font-style: italic;
    color: var(--ink-main);
    font-size: 1.25em;
    padding-bottom: 15px;
    margin-bottom: 15px;
    border-bottom: 1px solid rgba(44, 54, 63, 0.1);
    font-family: var(--font-serif);
}

/* Themed Markdown Elements */
.message.ai p {
    margin-bottom: 14px;
}
.message.ai p:last-child {
    margin-bottom: 0;
}
.message.ai code {
    background: rgba(44, 54, 63, 0.05);
    padding: 2px 6px;
    border-radius: 2px;
    color: var(--ink-red);
    font-family: monospace;
    font-size: 0.9em;
}
.message.ai pre {
    background: rgba(255, 255, 255, 0.4);
    padding: 16px;
    border-radius: 2px;
    overflow-x: auto;
    margin: 12px 0;
    border: 1px solid rgba(44, 54, 63, 0.15);
}
.message.ai h1,
.message.ai h2,
.message.ai h3 {
    margin: 22px 0 12px 0;
    color: var(--ink-main);
    font-family: var(--font-serif);
    font-weight: 600;
}

/* Input Bar */
.input-bar {
    padding: 20px 25px;
    background: transparent;
    border-top: 1px solid rgba(44, 54, 63, 0.15);
    display: flex;
    gap: 15px;
    align-items: flex-end;
    position: relative;
    z-index: 10;
}

.input-bar textarea {
    flex: 1;
    resize: none;
    min-height: 48px;
    max-height: 150px;
    line-height: 1.5;
    background: rgba(255, 255, 255, 0.2);
}

/* Data Tables - Ledger Style */
table.data-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 15px;
    background: transparent;
    border: 1px solid rgba(44, 54, 63, 0.2);
}
table.data-table th,
table.data-table td {
    border: 1px solid rgba(44, 54, 63, 0.2);
    padding: 12px 14px;
}
table.data-table th {
    background: rgba(44, 54, 63, 0.05);
    font-family: var(--font-serif);
    font-weight: 600;
    text-align: left;
    color: var(--ink-main);
}

/* ==================================================
   BEM Classes Extracted from Inline Styles
   ================================================== */
.config-bar__label {
    font-weight: 500;
    font-family: var(--font-sans);
    font-size: 13px;
    color: var(--ink-faded);
    margin-block: auto;
}

.config-bar__key-wrapper {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.config-bar__input {
    width: 100%;
}

.config-bar__input--hidden {
    display: none;
}

.config-bar__link {
    font-size: 12px;
    color: var(--ink-faded);
    font-family: var(--font-sans);
    text-decoration: underline;
}

.config-bar__status {
    font-size: 13px;
    color: var(--ink-faded);
    font-weight: 500;
    width: 100%;
    text-align: right;
}

.config-bar__status--ready {
    color: var(--ink-main);
}

.config-bar__status--error {
    color: var(--ink-red);
}

.input-bar__btn {
    height: 48px;
    padding: 0 25px;
    border-radius: 3px;
}

/* ==================================================
   Responsive Design / Mobile Adaptation
   ================================================== */
@media (max-width: 900px) {
    body {
        height: auto;
        min-height: 100vh;
        overflow-y: auto; /* Allows scrolling the whole page if needed */
        padding: 15px;
    }

    .layout-wrapper {
        flex-direction: column;
        height: auto;
    }

    .container {
        order: 1;
        height: 80vh; /* Keeps the chat area independently scrollable */
        max-width: 100%;
    }

    .sidebar {
        width: 100%;
        order: 2;
        padding: 20px;
    }

    /* Arrange sidebar links horizontally on tablets */
    .sidebar-nav {
        flex-direction: row;
        flex-wrap: wrap;
    }

    .sidebar-nav a {
        flex: 1 1 45%;
        justify-content: center;
    }

    .brand-header h1 {
        font-size: 28px;
    }

    .brand-header p {
        font-size: 14px;
    }

    /* Stack configuration inputs */
    .config-bar {
        flex-direction: column;
        align-items: stretch;
    }

    .config-bar > div {
        display: flex;
        flex-direction: column;
        width: 100%;
        gap: 10px;
    }

    .config-bar__status {
        text-align: left;
    }

    /* Stretch chat input and button */
    .input-bar {
        flex-direction: column;
        align-items: stretch;
    }

    .input-bar__btn {
        width: 100%;
        justify-content: center;
    }

    /* Expand message bubbles */
    .message {
        max-width: 95%;
    }

    .message.ai {
        max-width: 100%;
    }
}

/* Specific tweaks for small phone screens */
@media (max-width: 480px) {
    .sidebar-nav a {
        flex: 1 1 100%; /* Stack navigation links on very small screens */
    }
}
