/* 
=========================
Theme Variables
========================= 
*/

:root {
    --sab: env(safe-area-inset-bottom, 0px); /* expose to JS */

/* light theme */
    --bg-main: #F1EAC5;       /* main background */
    --bg-chat: #F1EAC5;       /* chat area background */
    --bg-user: #A9AD60;       /* user messages */
    --bg-bot: #800020;        /* bot messages */
    --bg-input: #ffffff;      /* input area */

    --text-primary: #172F13;  /* main text */
    --text-secondary: #172F13; /* secondary text */

    --border-color: #172F13;  /* borders */
    --accent: #800020;        /* buttons, highlights */

    --radius-sm: 8px;
    --radius-md: 14px;

    --shadow-sm: 0 1px 3px #172F13;

    --font-family: Andale Mono, monospace;
}

/* dark mode */
[data-theme = "dark"] {
    --bg-main: #172F13;       /* dark background */
    --bg-chat: #172F13;       /* chat area */
    --bg-user: #A9AD60;       /* user messages */
    --bg-bot: #800020;        /* bot messages */
    --bg-input: #172F13;      /* input area */

    --text-primary: #AEADAC;  /* text on dark background */
    --text-secondary: #AEADAC;

    --border-color: #AEADAC;
    --accent: #800020;

    --shadow-sm: 0 1px 3px #F1EAC5;
}


/* 
=========================
Base Layout
========================= 
*/

mark {
  color: var(--accent);
  background-color: #ffffff00;
}


body {
    margin: 0;
    padding: 0;
    height: 100vh;
    height: 100dvh; /* shrinks when keyboard appears on mobile */

    display: flex;
    flex-direction: column;

    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Produkt";
    background: var(--bg-main);
    color: var(--text-primary);

    background-image:
        linear-gradient(
            145deg,
            color-mix(in srgb, var(--border-color) 6%, transparent) 25%,
            transparent 25%
        ),
        linear-gradient(
            225deg,
            color-mix(in srgb, var(--border-color) 6%, transparent) 25%,
            transparent 25%
        ),
        linear-gradient(
            55deg,
            color-mix(in srgb, var(--border-color) 6%, transparent) 25%,
            transparent 25%
        ),
        linear-gradient(
            315deg,
            color-mix(in srgb, var(--border-color) 6%, transparent) 25%,
            transparent 25%
        );
    background-position: 10px 0, 10px 0, 0 0, 0 0;
}

/* 
=========================
Accessibility / Focus Styles
=========================
*/

button:focus-visible,
input:focus-visible {
    outline: 3px solid var(--accent);
    outline-offset: 2px;
}

/* 
=========================
Dark Mode Toggle Button
=========================
*/

#themeToggle {
    position: fixed;
    top: 16px;
    right: 16px;

    padding: 8px 12px;
    border: none;
    border-radius: var(--radius-sm);

    background: var(--accent);
    color: #fff;

    font-size: 14px;
    cursor: pointer;
    transition: background 0.15s ease, transform 0.1s ease, color 0.15s ease;
    z-index: 1000;
}

#themeToggle:hover {
    background: color-mix(in srgb, var(--accent) 85%, black);
}

#themeToggle:active {
    transform: scale(0.95);
}

/* 
=========================
Company logo
=========================
*/

#logoContainer {
    position: absolute;
    top: 20px;
    left: 20px;
    padding: 20px;                      /* optional: adds space around logo */
    width: 12.5vw;                      /* adjust the size */
    background-color: var(--bg-main);   /* dark background */
    border-radius: 8px;                 /* optional: rounded corners */
    display: inline-block;              /* ensures container wraps content */
}

/* dark mode */
body[data-theme = "dark"] #logoContainer {
    background-color: #F1EAC5;       /* dark background */
}

#companyLogo {
    width: 100%;                      /* fill container width */
    height: auto;
    position: relative;               /* keep inside container */
}

/* 
=========================
Chat Area
=========================
*/

/* 
=========================
Company logo
=========================
*/

#logoContainer {
    position: absolute;
    top: 20px;
    left: 20px;
    padding: 20px;                      /* optional: adds space around logo */
    width: 12.5vw;                      /* adjust the size */
    background-color: var(--bg-main);   /* dark background */
    border-radius: 8px;                 /* optional: rounded corners */
    display: inline-block;              /* ensures container wraps content */
}

/* dark mode */
body[data-theme = "dark"] #logoContainer {
    background-color: #F1EAC5;       /* dark background */
}

#companyLogo {
    width: 100%;                      /* fill container width */
    height: auto;
    position: relative;               /* keep inside container */
}

/* 
=========================
Chat Area
=========================
*/

#chat {
    flex: 1;
    padding-top: 80px;              /* space so chat bubbles don't overlap the toggle button */
    padding-bottom: 140px;          /* space so chat bubbles don't overlap the input area */
    padding-left: max( 16px, env(safe-area-inset-left) );
    padding-right: max( 16px, env(safe-area-inset-right) );

    background: var(--bg-chat);
    display: flex;
    flex-direction: column;
    gap: 16px;
    overflow-y: auto;

    width: 50vw ;                   /* fixed width = 60% of viewport width */
    min-width: 300px;               /* optional: prevent too small chat on mobile */
    max-width: 900px;
    
    margin: 0 auto;                 /* centers chat horizontally */
}

/* optional: style scrollbars for Chrome/Edge/Safari */
#chat::-webkit-scrollbar {
    width: 8px;
}

#chat::-webkit-scrollbar-thumb {
    background-color: var(--accent);
    border-radius: 4px;
}

#chat::-webkit-scrollbar-track {
    background: var(--bg-chat);
}

/* 
=========================
Messages
========================= 
*/

.message {
    max-width: 70%;
    padding: 14px 18px;

    border-radius: var(--radius-md);
    font-size: 16px;
    line-height: 1.5;

    box-shadow: 0 4px 12px var(--shadow-sm);;
    word-wrap: break-word;

    display: flex;
    flex-direction: column;
}

.msg-time {
    align-self: flex-end;
    font-size: 11px;
    opacity: 0.65;
    margin-top: 6px;
}

.user {
    background: var(--bg-user);
    align-self: flex-end;
    color: #fff;                    /* ensures text is readable on orange */
    border-bottom-right-radius: 4px;
}

.system {
    background: var(--bg-bot);        /* dark red background */
    align-self: flex-start;
    color: #fff;                    /* ensures text is readable on dark red background */
    border-bottom-right-radius: 4px;
}

/* 
=========================
Typing Indicator
========================= 
*/

.message.system.typing {
    display: inline-flex;
    flex-direction: row;
    align-items: center;
    align-self: flex-start;
    gap: 4px;
}

.message.system.typing span {
    width: 6px;
    height: 6px;
    background: currentColor;
    border-radius: 50%;
    animation: blink 1.4s infinite both;
}

.message.system.typing span:nth-child(2) {
    animation-delay: 0.2s;
}

.message.system.typing span:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes blink {
    0%   { opacity: 0.2; }
    20%  { opacity: 1; }
    100% { opacity: 0.2; }
}

/* 
=========================
Input Area
========================= 
*/

#inputArea {
    display: flex;
    gap: 12px;
    padding: 16px;
    
    width: 50vw;                             /* fixed width = % of viewport width */
    min-width: 300px;                        /* optional: prevent too small chat on mobile */
    max-width: 900px;
    padding-left: max( 16px, env(safe-area-inset-left) );
    padding-right: max( 16px, env(safe-area-inset-right) );
    margin: 0 auto 16px;

    background: var(--bg-input);
    border-radius: 20px;                     /* more rounded corners */

    /* float above bottom */
    position: sticky;
    bottom: 0;
    box-shadow: 0 4px 12px var(--shadow-sm); /* optional shadow for "floating" effect */
    
}

#inputArea input {
    flex: 1;
    padding: 40px 16px;

    font-size: 16px;
    color: var(--text-primary);

    border-radius: var(--radius-sm);
    border: 1px solid var(--border-color);
    box-shadow: 0 4px 12px var(--shadow-sm);
    background: transparent;

    outline: none;
}

#inputArea input::placeholder {
    color: var(--text-secondary);
}

#inputArea input:focus {
    border-color: var(--accent);
}

/* 
=========================
Send Button
========================= 
*/

#inputArea input:focus {
    border-color: var(--accent);
}

/* 
=========================
Send Button
========================= 
*/

#inputArea button {
    width: 20%;
    min-width: 30px;
    max-width: 60px;

    border: none;
    border-radius: var(--radius-sm);

    background: var(--accent);
    color: #fff;

    font-size: 16px;
    font-weight: 500;

    cursor: pointer;
    transition: background 0.15s ease, transform 0.1s ease;
}

#inputArea button:hover {
    background: color-mix(in srgb, var(--accent) 90%, black);
}

#inputArea button:active {
    transform: scale(0.97);
}

/* 
=========================
Rightside Panel
========================= 
*/

#right-panel {
  position: fixed;
  top: 80px;
  right: 20px;
  width: 20vw;

  display: flex;
  flex-direction: column;
  gap: 12px; /* space between boxes */
}

/* 
=========================
Query - Explanation Area
========================= 
*/
#query-box, #explanation-box, #bg-concepts-box {
  position: relative; 
  padding-left: max( 16px, env(safe-area-inset-left) );
  padding-right: max( 16px, env(safe-area-inset-right) );
  height: auto;                     /* height adapts to content */
  min-height: 40px;                 /* optional: minimum height */
  max-height: 50vh;                 /* optional: limits height so it doesn't go offscreen */
  background-color: var(--bg-chat);
  border: 2px solid var(--border-color);
  border-radius: 8px;               /* optional: rounded corners */
  box-shadow: 0 4px 12px var(--shadow-sm);
  overflow-y: auto;                 /* allows scrolling if content is long */
  display: flex;    
  flex-direction: column;           /* stack children vertically */
}

.h3-title {
  min-height: 40px;
  font-size: 16px;
  font-weight: 600;
}

#query-box h3,
#explanation-box h3,
#bg-concepts-box h3 {
  padding-right: 82px; /* clears the copy (right:44px) + toggle (right:8px) buttons */
  margin: 10px 0px;
  line-height: 1.5;
  min-height: 40px;
}

#query-box pre {
  white-space: pre-wrap;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 15;
  background-color: var(--bg-chat);
}

#explanation-box p #bg-concepts-box p {
  white-space: pre-wrap;
  font-size: 15;
  background-color: var(--bg-chat);
}

#query-copy, #query-toggle, #explanation-copy, #explanation-toggle, #bg-concepts-copy, #bg-concepts-toggle {
  position: absolute;
  top: 15px;
  right: 8px;
  height: 30px;
  width: 30px;
  font-size: 16px; 
  border-radius: 6px;
  border: 1px solid var(--border-color);
  background: var(--bg-chat);
  cursor: pointer;
  transition: background 0.2s ease, transform 0.1s ease;
  color: var(--text-color);
}

#query-copy, #explanation-copy, #bg-concepts-copy {
  right: 44px;
}

#query-toggle, #explanation-toggle, #bg-concepts-toggle {
  right: 8px;
}

#query-copy:hover, #explanation-copy:hover, #bg-concepts-copy:hover {
  background: rgba(0, 0, 0, 0.05);
}

#query-copy:active, #explanation-copy:active, #bg-concepts-copy:active {
  transform: scale(0.95);
}

/* copied state */
#query-copy.copied, #explanation-copy.copied, #bg-concepts-copy.copied {
  background: #4caf50;
  color: white;
  border-color: #4caf50;
}

/* collapsed state */
#query-box.collapsed, #explanation-box.collapsed, #bg-concepts-box.collapsed {
  min-height: 40px
}

#query-box.collapsed pre {
  display: none;
}

#explanation-box.collapsed p, #bg-concepts-box.collapsed p {
  display: none;
}

/* rotate arrow */
#query-box.collapsed #query-toggle, #explanation-box.collapsed #explanation-toggle, #bg-concepts-box.collapsed #bg-concepts-toggle {
  transform: rotate(-90deg);
  transition: transform 0.2s ease;
}

/*
=========================
Mobile responsiveness
=========================
*/

/* --- tablet portrait: boxes below input, body scrolls --- */
@media (pointer: coarse) and (orientation: portrait) and (max-width: 820px) {
    body {
        height: auto;
        min-height: 100dvh;
        overflow-y: auto;
    }

    #logoContainer {
        position: fixed;
        width: 90px;
        padding: 12px;
        top: 12px;
        left: 12px;
    }

    #themeToggle {
        top: 12px;
        right: 12px;
    }

    /* chat sizes to content, no internal scroll */
    #chat {
        order: 1;
        flex: none;
        overflow-y: visible;
        width: 90vw;
        max-width: 700px;
        min-height: calc(100dvh - 75px - 45px); /* 75px header, 45px inputArea */
        padding-top: 75px;
        padding-bottom: 12px;
        box-sizing: border-box;
        background: transparent;
    }

    /* input scrolls with page, sits below chat */
    #inputArea {
        order: 2;
        position: relative !important;
        bottom: auto !important;
        width: 90vw;
        max-width: 700px;
        margin: 0 auto 24px;
        border-radius: 14px;
        box-sizing: border-box;
    }

    /* boxes come last, below input */
    #right-panel {
        order: 3;
        position: relative;
        top: auto;
        right: auto;
        width: 90vw;
        max-width: 700px;
        margin: 0 auto;
        box-sizing: border-box;
        padding-bottom: 24px;
        display: flex;
        flex-direction: column;
        gap: 12px;
    }

    .message {
        max-width: 80%;
    }
}

/* --- tablet landscape: mobile-like layout --- */
@media (pointer: coarse) and (orientation: landscape) and (max-height: 768px) and (min-height: 481px) {
    body {
        height: auto;
        min-height: 100dvh;
        overflow-y: auto;
    }

    #logoContainer {
        position: fixed;
        width: 70px;
        padding: 10px;
        top: 10px;
        left: 10px;
    }

    #themeToggle {
        top: 10px;
        right: 10px;
        padding: 6px 10px;
        font-size: 13px;
    }

    /* chat sizes to content, no internal scroll */
    #chat {
        order: 1;
        flex: none;
        overflow-y: visible;
        width: 100%;
        max-width: 100%;
        min-height: calc(100dvh - 80px - 45px); /* 80px header, 45px inputArea */
        padding-top: 80px;
        padding-bottom: 12px;
        padding-left: max(16px, env(safe-area-inset-left));
        padding-right: max(16px, env(safe-area-inset-right));
        box-sizing: border-box;
        background: transparent;
    }

    /* input scrolls with the page, sits below chat */
    #inputArea {
        order: 2;
        position: relative !important;
        bottom: auto !important;
        width: 100%;
        max-width: 100%;
        margin: 0 0 24px 0;
        border-radius: 12px;
        box-sizing: border-box;
        padding-left: max(16px, env(safe-area-inset-left));
        padding-right: max(16px, env(safe-area-inset-right));
    }

    /* boxes come last, below input */
    #right-panel {
        order: 3;
        position: relative;
        top: auto;
        right: auto;
        width: 100%;
        box-sizing: border-box;
        padding: 0 max(16px, env(safe-area-inset-left)) 24px;
        display: flex;
        flex-direction: column;
        gap: 10px;
    }

    .message {
        max-width: 75%;
    }
}

/* --- large mobile: 480px --- */
@media (pointer: coarse) and (max-width: 480px) {
    body {
        height: auto;
        min-height: 100dvh;
        overflow-y: auto;
    }

    #logoContainer {
        position: fixed;
        width: 60px;
        padding: 8px;
        top: 8px;
        left: 8px;
    }

    #themeToggle {
        top: 8px;
        right: 8px;
        padding: 6px 10px;
        font-size: 12px;
    }

    /* chat fills the viewport minus the inputArea height at the bottom */
    #chat {
        order: 1;
        flex: none;
        overflow-y: visible;
        width: 100%;
        max-width: 100%;
        min-height: calc(100dvh - 100px - 10px); /* 90px header, 20px inputArea */
        padding-top: 90px;
        padding-bottom: 12px;
        padding-left: max(12px, env(safe-area-inset-left));
        padding-right: max(12px, env(safe-area-inset-right));
        box-sizing: border-box;
        background: transparent;
    }

    /* input sits directly below chat, scrolls with the page */
    #inputArea {
        order: 2;
        position: relative;
        bottom: auto;
        width: 100%;
        max-width: 100%;
        margin: 0 0 24px 0;
        border-radius: 12px;
        box-sizing: border-box;
        padding-left: max(12px, env(safe-area-inset-left));
        padding-right: max(12px, env(safe-area-inset-right));
    }

    /* boxes come last — scroll down past input to see them */
    #right-panel {
        order: 3;
        position: relative;
        top: auto;
        right: auto;
        width: 100%;
        box-sizing: border-box;
        padding: 0 max(12px, env(safe-area-inset-left)) 24px;
        display: flex;
        flex-direction: column;
        gap: 8px;
    }

    #inputArea input {
        padding: 18px 12px;
        font-size: 16px;
    }

    #inputArea button {
        width: 56px;
        min-width: 48px;
        max-width: 72px;
        font-size: 14px;
    }

    .message {
        max-width: 90%;
        padding: 10px 14px;
        font-size: 15px;
    }

    #query-box,
    #explanation-box,
    #bg-concepts-box {
        max-height: 200px;
    }
}

/* --- small mobile: 360px --- */
@media (pointer: coarse) and (max-width: 360px) {
    #logoContainer {
        position: fixed;
        width: 48px;
        padding: 6px;
        top: 6px;
        left: 6px;
    }

    #themeToggle {
        padding: 5px 8px;
        font-size: 11px;
    }

    #chat {
        padding-top: 58px;
        gap: 10px;
    }

    #inputArea {
        gap: 8px;
        padding: 10px;
        padding-left: max(10px, env(safe-area-inset-left));
        padding-right: max(10px, env(safe-area-inset-right));
    }

    #inputArea input {
        padding: 14px 10px;
        font-size: 16px;
    }

    #inputArea button {
        width: 48px;
        min-width: 40px;
        font-size: 13px;
    }

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

    .msg-time {
        font-size: 10px;
    }

    #right-panel {
        padding: 0 max(10px, env(safe-area-inset-left)) 16px;
        gap: 6px;
    }
}

/*
=========================
Welcome Modal
=========================
*/

#welcome-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    z-index: 2000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    box-sizing: border-box;
    animation: fadeIn 0.2s ease;
}

#welcome-backdrop.hidden {
    display: none;
}

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

#welcome-modal {
    position: relative;
    background: var(--bg-main);
    color: var(--text-primary);
    border: 2px solid var(--border-color);
    border-radius: var(--radius-md);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
    padding: 40px 36px 32px;
    width: 100%;
    max-width: 760px;
    max-height: 90vh;
    overflow-y: auto;
    box-sizing: border-box;
    animation: slideUp 0.25s ease;
}

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

#welcome-modal h2 {
    margin: 0 0 12px;
    font-size: 22px;
    color: var(--accent);
}

#welcome-intro {
    margin: 0 0 24px;
    font-size: 15px;
    line-height: 1.6;
}

#welcome-close {
    position: absolute;
    top: 12px;
    right: 14px;
    background: none;
    border: none;
    font-size: 24px;
    line-height: 1;
    cursor: pointer;
    color: var(--text-primary);
    padding: 4px 8px;
    border-radius: var(--radius-sm);
    transition: background 0.15s ease;
}

#welcome-close:hover {
    background: color-mix(in srgb, var(--border-color) 15%, transparent);
}

#welcome-features {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    margin-bottom: 28px;
}

.welcome-card {
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    padding: 14px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.welcome-card img {
    width: 100%;
    height: auto;
    border-radius: 4px;
    border: 1px solid var(--border-color);
    display: block;
}

.welcome-card h4 {
    margin: 0;
    font-size: 14px;
    font-weight: 600;
    color: var(--accent);
}

.welcome-card p {
    margin: 0;
    font-size: 13px;
    line-height: 1.5;
}

#welcome-start {
    display: block;
    margin: 0 auto;
    padding: 12px 32px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 15px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s ease, transform 0.1s ease;
}

#welcome-start:hover {
    background: color-mix(in srgb, var(--accent) 85%, black);
}

#welcome-start:active {
    transform: scale(0.97);
}

#welcome-reopen {
    position: fixed;
    bottom: 16px;
    left: 16px;
    width: 36px;
    height: 36px;
    padding: 0;
    border: none;
    border-radius: 50%;
    background: var(--accent);
    color: #fff;
    font-size: 17px;
    font-weight: 700;
    cursor: pointer;
    z-index: 1000;
    box-shadow: var(--shadow-sm);
    transition: background 0.15s ease, transform 0.1s ease;
}

#welcome-reopen:hover {
    background: color-mix(in srgb, var(--accent) 85%, black);
}

#welcome-reopen:active {
    transform: scale(0.95);
}

/* modal scrollbar */
#welcome-modal::-webkit-scrollbar { width: 6px; }
#welcome-modal::-webkit-scrollbar-thumb { background: var(--accent); border-radius: 3px; }
#welcome-modal::-webkit-scrollbar-track { background: transparent; }

/* mobile: single-column cards */
@media (max-width: 560px) {
    #welcome-features {
        grid-template-columns: 1fr;
    }

    #welcome-modal {
        padding: 32px 20px 24px;
    }
}