/* =========================================================
   AgroSI - kompletny arkusz stylow
   Układ odporny na ucinanie dolnej części czatu
   ========================================================= */

:root {
    --agrosi-primary: #2f8737;
    --agrosi-primary-hover: #26742e;
    --agrosi-primary-dark: #1e5a30;
    --agrosi-sidebar: #194b31;
    --agrosi-sidebar-light: #245f3e;
    --agrosi-accent: #94d45b;
    --agrosi-background: #f4f8f4;
    --agrosi-surface: #ffffff;
    --agrosi-text: #25332c;
    --agrosi-muted: #6d7971;
    --agrosi-border: #d8e3db;
    --agrosi-border-strong: #bdd0c1;
    --agrosi-user: #e3f5e4;
    --agrosi-shadow: 0 10px 30px rgba(24, 70, 42, 0.10);
    --agrosi-content-width: 1000px;
    --agrosi-sidebar-width: 310px;
}

*,
*::before,
*::after {
    box-sizing: border-box;
}

html {
    min-height: 100%;
    height: auto;
    overflow-x: hidden;
    overflow-y: auto;
    scroll-behavior: smooth;
}

body {
    min-height: 100%;
    height: auto;
    margin: 0;
    padding: 0 0 48px;
    overflow-x: hidden;
    overflow-y: auto;
    color: var(--agrosi-text);
    background: var(--agrosi-background);
    font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
    font-size: 16px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

button,
textarea,
input {
    font: inherit;
}

button:focus-visible,
textarea:focus-visible,
.suggestion-card:focus-visible {
    outline: 3px solid rgba(47, 135, 55, 0.23);
    outline-offset: 2px;
}

[hidden] {
    display: none !important;
}

.visually-hidden {
    position: absolute !important;
    width: 1px !important;
    height: 1px !important;
    margin: -1px !important;
    padding: 0 !important;
    overflow: hidden !important;
    clip: rect(0, 0, 0, 0) !important;
    clip-path: inset(50%) !important;
    white-space: nowrap !important;
    border: 0 !important;
}

/* Główny kontener. Wysokość jest kontrolowana, ale strona może się przewijać. */
.app {
    display: flex;
    width: calc(100% - 32px);
    max-width: 1500px;
    height: min(820px, calc(100dvh - 32px));
    min-height: 650px;
    margin: 16px auto 48px;
    overflow: hidden;
    background: var(--agrosi-background);
    border: 1px solid var(--agrosi-border);
    border-radius: 20px;
    box-shadow: var(--agrosi-shadow);
}

.sidebar {
    display: flex;
    flex: 0 0 var(--agrosi-sidebar-width);
    flex-direction: column;
    width: var(--agrosi-sidebar-width);
    min-width: var(--agrosi-sidebar-width);
    min-height: 0;
    padding: 28px 24px;
    overflow: hidden;
    color: #ffffff;
    background: linear-gradient(180deg, var(--agrosi-sidebar-light), var(--agrosi-sidebar));
}

.logo {
    display: flex;
    align-items: center;
    gap: 12px;
    margin: 0 8px 30px;
    font-size: 28px;
    font-weight: 800;
    letter-spacing: -0.5px;
}

.new-chat-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 9px;
    width: 100%;
    min-height: 58px;
    margin: 0 0 34px;
    padding: 13px 18px;
    color: #ffffff;
    background: var(--agrosi-primary);
    border: 1px solid rgba(255, 255, 255, 0.14);
    border-radius: 16px;
    font-size: 17px;
    font-weight: 750;
    cursor: pointer;
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.13);
    transition: transform 0.2s ease, background-color 0.2s ease;
}

.new-chat-btn:hover {
    background: #35953e;
    transform: translateY(-1px);
}

.history {
    min-height: 0;
    overflow-x: hidden;
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: rgba(255, 255, 255, 0.25) transparent;
}

.history-title {
    margin: 0 4px 16px;
    color: rgba(255, 255, 255, 0.66);
    font-size: 13px;
    font-weight: 800;
    letter-spacing: 0.08em;
    text-transform: uppercase;
}

.history-item {
    display: block;
    width: 100%;
    margin: 0 0 10px;
    padding: 15px 16px;
    overflow: hidden;
    color: rgba(255, 255, 255, 0.94);
    background: rgba(255, 255, 255, 0.07);
    border: 1px solid transparent;
    border-radius: 13px;
    font-size: 14px;
    font-weight: 550;
    line-height: 1.35;
    text-align: left;
    text-overflow: ellipsis;
    white-space: nowrap;
    cursor: pointer;
    transition: background-color 0.2s ease, border-color 0.2s ease;
}

.history-item:hover,
.history-item.active {
    background: rgba(255, 255, 255, 0.14);
    border-color: rgba(255, 255, 255, 0.12);
}

.history-empty {
    padding: 14px 4px;
    color: rgba(255, 255, 255, 0.58);
    font-size: 13px;
}

.chat-container {
    position: relative;
    display: flex;
    flex: 1 1 auto;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
    height: 100%;
    overflow: hidden;
    background: radial-gradient(circle at 50% 0, rgba(148, 212, 91, 0.08), transparent 38%), var(--agrosi-background);
}

.header {
    flex: 0 0 auto;
    padding: 20px 24px 8px;
    text-align: center;
}

.header h1 {
    margin: 0;
    color: var(--agrosi-primary-dark);
    font-size: clamp(28px, 3vw, 37px);
    font-weight: 800;
    line-height: 1.15;
    letter-spacing: -1px;
}

.header p {
    margin: 6px 0 0;
    color: var(--agrosi-muted);
    font-size: 15px;
}

.welcome-section {
    flex: 0 0 auto;
    width: 100%;
    max-width: var(--agrosi-content-width);
    margin: 0 auto;
    padding: 8px 24px 14px;
    text-align: center;
}

.welcome-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 58px;
    height: 58px;
    margin: 0 auto 9px;
    background: linear-gradient(145deg, #f1fae9, #e2f3d7);
    border: 1px solid #d1e6c6;
    border-radius: 18px;
    font-size: 31px;
    box-shadow: 0 3px 10px rgba(30, 90, 48, 0.07);
}

.welcome-section h2 {
    margin: 0 0 6px;
    font-size: clamp(22px, 2.5vw, 28px);
    font-weight: 750;
    letter-spacing: -0.4px;
}

.welcome-section > p {
    margin: 0 auto 16px;
    color: var(--agrosi-muted);
    font-size: 14px;
}

.suggestions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
    width: 100%;
}

.suggestion-card {
    position: relative;
    display: flex;
    align-items: center;
    min-height: 68px;
    padding: 14px 18px 14px 24px;
    color: #314139;
    background: rgba(255, 255, 255, 0.97);
    border: 1px solid var(--agrosi-border);
    border-radius: 14px;
    font-size: 14px;
    font-weight: 650;
    line-height: 1.4;
    text-align: left;
    cursor: pointer;
    box-shadow: 0 3px 10px rgba(24, 70, 42, 0.06);
    transition: transform 0.2s ease, border-color 0.2s ease, box-shadow 0.2s ease;
}

.suggestion-card::before {
    position: absolute;
    top: 15px;
    bottom: 15px;
    left: 0;
    width: 5px;
    content: "";
    background: var(--agrosi-accent);
    border-radius: 0 5px 5px 0;
}

.suggestion-card:hover {
    border-color: #9bc9a0;
    transform: translateY(-2px);
    box-shadow: 0 10px 24px rgba(24, 70, 42, 0.10);
}

/* Ten element przejmuje pozostałą wysokość i jako jedyny przewija rozmowę. */
.chat-messages {
    display: flex;
    flex: 1 1 0;
    flex-direction: column;
    width: 100%;
    max-width: var(--agrosi-content-width);
    min-height: 0;
    margin: 0 auto;
    padding: 14px 24px 18px;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    scroll-behavior: smooth;
    scrollbar-width: thin;
    scrollbar-color: #bdc9c0 transparent;
}

.message {
    position: relative;
    width: fit-content;
    max-width: min(84%, 780px);
    margin-bottom: 14px;
    padding: 14px 16px;
    overflow-wrap: anywhere;
    border-radius: 16px;
    font-size: 15px;
    line-height: 1.62;
}

.message.ai {
    align-self: flex-start;
    color: var(--agrosi-text);
    background: var(--agrosi-surface);
    border: 1px solid var(--agrosi-border);
    border-bottom-left-radius: 5px;
    box-shadow: 0 3px 12px rgba(24, 70, 42, 0.06);
}

.message.ai::before {
    display: block;
    margin-bottom: 6px;
    color: var(--agrosi-primary-dark);
    content: "AgroSI";
    font-size: 12px;
    font-weight: 800;
    letter-spacing: 0.02em;
}

.message.user {
    align-self: flex-end;
    margin-left: auto;
    color: #173c24;
    background: var(--agrosi-user);
    border: 1px solid #bee0c3;
    border-bottom-right-radius: 5px;
}

.message strong {
    color: #193e27;
    font-weight: 750;
}

.ai-list-item {
    display: block;
    margin: 4px 0;
}

.message.typing {
    display: flex;
    align-items: center;
    gap: 6px;
    min-width: 205px;
    color: var(--agrosi-muted);
}

.message.typing::before {
    margin: 0 5px 0 0;
}

.typing-dots span {
    display: inline-block;
    color: var(--agrosi-primary);
    font-size: 19px;
    font-weight: 800;
    animation: agrosiTyping 1.2s infinite;
}

.typing-dots span:nth-child(2) { animation-delay: 0.18s; }
.typing-dots span:nth-child(3) { animation-delay: 0.36s; }

/* Pole wiadomości jest częścią układu flex i nigdy nie jest pozycjonowane fixed/absolute. */
.chat-input-section {
    position: relative;
    z-index: 10;
    flex: 0 0 auto;
    width: 100%;
    padding: 12px 24px 18px;
    background: var(--agrosi-background);
    border-top: 1px solid rgba(189, 208, 193, 0.7);
}

.chat-input-wrapper {
    display: flex;
    align-items: flex-end;
    gap: 10px;
    width: 100%;
    max-width: var(--agrosi-content-width);
    margin: 0 auto;
    padding: 8px;
    background: #ffffff;
    border: 1px solid var(--agrosi-border-strong);
    border-radius: 18px;
    box-shadow: 0 7px 22px rgba(24, 70, 42, 0.10);
}

.chat-input-wrapper:focus-within {
    border-color: #73ad79;
    box-shadow: 0 0 0 4px rgba(47, 135, 55, 0.10), 0 7px 22px rgba(24, 70, 42, 0.10);
}

#messageInput {
    flex: 1;
    width: 100%;
    min-width: 0;
    min-height: 46px;
    max-height: 160px;
    margin: 0;
    padding: 11px 13px;
    resize: none;
    overflow-y: hidden;
    color: var(--agrosi-text);
    background: transparent;
    border: 0;
    border-radius: 10px;
    outline: none;
    font-size: 15px;
    line-height: 1.5;
}

#messageInput::placeholder {
    color: #909a93;
}

#sendButton {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
    min-width: 120px;
    min-height: 46px;
    padding: 10px 19px;
    color: #ffffff;
    background: var(--agrosi-primary);
    border: 0;
    border-radius: 13px;
    font-size: 14px;
    font-weight: 750;
    cursor: pointer;
    box-shadow: 0 6px 15px rgba(47, 135, 55, 0.22);
    transition: transform 0.2s ease, background-color 0.2s ease, opacity 0.2s ease;
}

#sendButton:hover:not(:disabled) {
    background: var(--agrosi-primary-hover);
    transform: translateY(-1px);
}

#sendButton:disabled {
    opacity: 0.58;
    cursor: not-allowed;
    box-shadow: none;
}

@keyframes agrosiTyping {
    0%, 60%, 100% { opacity: 0.25; transform: translateY(0); }
    30% { opacity: 1; transform: translateY(-3px); }
}

@media (max-width: 1050px) {
    :root {
        --agrosi-sidebar-width: 270px;
    }

    .app {
        width: calc(100% - 20px);
        margin-top: 10px;
    }
}

@media (max-width: 820px) {
    .app {
        height: min(780px, calc(100dvh - 16px));
        min-height: 620px;
        margin: 8px auto 40px;
        border-radius: 16px;
    }

    .sidebar {
        display: none;
    }
}

@media (max-width: 700px) {
    body {
        padding-bottom: max(20px, env(safe-area-inset-bottom));
    }

    .app {
        width: 100%;
        height: calc(100dvh - 8px);
        min-height: 580px;
        margin: 0 0 28px;
        border-right: 0;
        border-left: 0;
        border-radius: 0;
        box-shadow: none;
    }

    .header {
        padding: 13px 14px 6px;
    }

    .header h1 {
        font-size: 26px;
    }

    .header p {
        font-size: 13px;
    }

    .welcome-section {
        padding: 7px 12px 10px;
    }

    .welcome-icon {
        width: 50px;
        height: 50px;
        font-size: 28px;
    }

    .welcome-section h2 {
        font-size: 21px;
    }

    .suggestions {
        grid-template-columns: 1fr;
        gap: 8px;
    }

    .suggestion-card {
        min-height: 54px;
        padding: 11px 14px 11px 18px;
        font-size: 13px;
    }

    .chat-messages {
        padding: 10px 10px 14px;
    }

    .message {
        max-width: 92%;
        padding: 12px 13px;
        font-size: 14px;
    }

    .chat-input-section {
        padding: 8px 8px calc(10px + env(safe-area-inset-bottom));
    }

    .chat-input-wrapper {
        gap: 7px;
        padding: 6px;
        border-radius: 15px;
    }

    #messageInput {
        min-height: 44px;
        padding: 10px;
        font-size: 16px;
    }

    #sendButton {
        min-width: 76px;
        min-height: 44px;
        padding: 9px 12px;
        font-size: 13px;
    }
}

@media (max-height: 720px) and (min-width: 821px) {
    .app {
        height: calc(100dvh - 20px);
        min-height: 600px;
    }

    .header {
        padding-top: 12px;
    }

    .welcome-icon {
        width: 48px;
        height: 48px;
        font-size: 27px;
    }

    .welcome-section {
        padding-top: 4px;
        padding-bottom: 8px;
    }

    .suggestion-card {
        min-height: 56px;
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        scroll-behavior: auto !important;
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}
