:root {
    --bg: white;
    --compose-box-bg: hsl(0, 0%, 100%, 0.7);
    --fg: black;
    --my-message-bg: hsl(215, 90%, 50%);
    --my-message-fg: white;
    --response-bg: hsl(0, 0%, 91%);
    --link: hsl(215, 100%, 45%);
    --input-border: hsla(0, 0%, 0%, 20%);
    --settings-icon-border: hsla(0, 0%, 0%, 40%);
    --info: hsl(120, 50%, 80%);
    --info-link: hsl(210, 100%, 30%);
    --error-fg: hsl(5, 70%, 50%);
    --success-fg: hsl(140, 70%, 30%);
    --code-bg: hsla(0, 0%, 100%, 0.7);
    --code-border: hsla(0, 0%, 0%, 0.15);
    --my-message-code-bg: hsla(0, 0%, 0%, 0.2);
    --my-message-code-border: hsla(0, 0%, 0%, 0.15);

    --chat-max-width: 50em;
    --block-border-radius: 0.5em;
}

html {
    -webkit-text-size-adjust: 100%; /* Prevent font scaling in landscape while allowing user zoom */
}

body {
    margin: 0;
    font-family: sans-serif;
    line-height: 140%;
    background-color: var(--bg);
    color: var(--fg);
    display: flex;
    justify-content: center;
}

.container {
    width: var(--chat-max-width);
    padding: 0.7em 1em;
    margin-left: env(safe-area-inset-left);
    margin-right: env(safe-area-inset-right);
    margin-bottom: calc(4em + env(safe-area-inset-bottom));
    max-width: calc(100% - 2em); /* Don't overflow the window with over-sized texts. Subtract paddings */
}

#settings-button, #settings-exit-button {
    float: right;
    margin-right: -0.1em;
    height: 25px;
}

#settings-button:hover, #settings-exit-button:hover {
    opacity: 80%;
    cursor: pointer;
}

/* From https://gist.github.com/MoOx/9137295 */
.reset-button {
    border: none;
    margin: 0;
    padding: 0;
    width: auto;
    overflow: visible;

    background: transparent;

    /* inherit font & color from ancestor */
    color: inherit;
    font: inherit;

    /* Normalize `line-height`. Cannot be changed from `normal` in Firefox 4+. */
    line-height: normal;

    /* Corrects font smoothing for webkit */
    -webkit-font-smoothing: inherit;
    -moz-osx-font-smoothing: inherit;

    /* Corrects inability to style clickable `input` types in iOS */
    -webkit-appearance: none;
}


.full-window-overlay {
    position: absolute;
    box-sizing: border-box;
    min-height: 100vh;
    width: 100%;
    padding-left: calc(max((100vw - var(--chat-max-width))/2, 1em + env(safe-area-inset-left)));
    padding-right: calc(max((100vw - var(--chat-max-width))/2, 1em + env(safe-area-inset-right)));
    padding-bottom: calc(0.8em + env(safe-area-inset-bottom));
    padding-top: 0.7em;
    z-index: 2;
    background-color: var(--compose-box-bg);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px); /* Needed for Safari in 2023 */
}

#settings-view h1 {
    margin-top: 0.3em;
}

#settings-view h2 {
    font-weight: 400;
    font-size: 1.3em;
    margin: 1em 0 0.6em 0;
}

#settings-view input, #settings-view .textarea-border-radius {
    margin: 0.2em 0;
}

#system-message {
    max-height: 20vh;
}

.assistant-icon-container {
    text-align: center;
    margin-top: 2em;
    margin-bottom: 2em;
}

.assistant-icon {
    width: 7em;
    border-radius: 1.5em;
}

.start-view {
    background-color: var(--info);
    padding: 1em;
    border-radius: 1em;
    display: inline-block;
    margin: 1em 0;
    font-size: 1.1em;
}

.start-view a, .start-view a:visited {
    color: var(--info-link);
    text-decoration: underline;
}

#compose-box {
    background-color: var(--compose-box-bg);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px); /* Needed for Safari in 2023 */
    position: fixed;
    bottom: 0;

    width: 100%;
    box-sizing: border-box;
    padding: 0.9em;
    padding-left: calc(max(0.9em + env(safe-area-inset-left), (100% - var(--chat-max-width))/2));
    padding-right: calc(max(0.9em + env(safe-area-inset-right), (100% - var(--chat-max-width))/2));
    padding-bottom: calc(0.8em + env(safe-area-inset-bottom));

    display: flex;
    justify-content: space-between;
}

#prompt {
    font-size: 1.1em;
    margin: 0;
    max-height: 70vh;
}

#submit {
    margin: 0;
}

#prompt-container {
    flex: 1;
    margin-right: 0.5em;
}

input {
    font-size: 16px;
}

.response-container {
    display: flex;
    align-items: start;
    flex-direction: column;
}

.my-message-container {
    display: flex;
    align-items: end;
    flex-direction: column;
}

.message-bubble {
    margin-top: 0.6em;
    display: inline-block;
    padding-top: 0.1em;
    padding-bottom: 0.15em;
    padding-left: 0.8em;
    padding-right: 0.8em;
    border-radius: 1.0em;
    max-width: 100%;
    box-sizing: border-box;
    word-wrap: break-word;
}

.response-bubble {
    background-color: var(--response-bg);
    color: var(--fg);
}

.my-message-bubble {
    background-color: var(--my-message-bg);
    color: var(--my-message-fg);
}

.my-message-bubble a {
    color: var(--my-message-fg);
    text-decoration: underline;
}

.my-message-bubble a:hover {
    text-decoration-thickness: 2px;
}

.error {
    color: var(--error-fg);
}

.success {
    color: var(--success-fg);
}

.copied-indicator {
    font-size: 0.8em;
}

hr {
    border: none;
    border-top: 1px solid var(--input-border);
    background: none;
    margin: 1.4em 0;
}

p {
    margin: 0.5em 0;
}

br {
    line-height: inherit;
}

code {
    white-space: pre-wrap;
    border: 1px solid var(--code-border);
    border-radius: 0.4em;
    padding: 0.1em 0.3em;
    background-color: var(--code-bg);
}

pre > code {
    border: none;
    padding: 0;
    color: var(--fg);
    background: none;
}

pre {
    border: 1px solid var(--code-border);
    border-radius: var(--block-border-radius);
    padding: 0.5em 0.9em;
    background-color: var(--code-bg);
}

pre, code {
    cursor: pointer;
}

.my-message-bubble code {
    color: var(--my-message-fg);
    border: 1px solid var(--my-message-code-border);
    background-color: var(--my-message-code-bg);
}

.my-message-bubble pre > code {
    border: none;
    background: none;
}

.my-message-bubble pre {
    border: 1px solid var(--my-message-code-border);
    background-color: var(--my-message-code-bg);
}


table {
    border-collapse: separate;
    border-spacing: 0;
    margin: 1em 0;
}

th, td {
    border: 1px solid var(--code-border);
    padding: 0.4em 0.7em;
    background-color: var(--code-bg);
}

/* Border drawing logic */
th { border-style: solid solid solid none; }
th:first-child { border-left-style: solid; }
td { border-style: none solid solid none; }
td:first-child { border-left-style: solid; }

/* Rounded corners */
tr:first-child th:first-child { border-top-left-radius: var(--block-border-radius); }
tr:first-child th:last-child { border-top-right-radius: var(--block-border-radius); }
tr:last-child td:first-child { border-bottom-left-radius: var(--block-border-radius); }
tr:last-child td:last-child { border-bottom-right-radius: var(--block-border-radius); }

/* Colors in my messages */
.my-message-bubble th, .my-message-bubble td {
    border-color: var(--my-message-code-border);
    background-color: var(--my-message-code-bg);
}


.message-bubble img {
    width: 100%;
}

a, a:visited {
    text-decoration: none;
    color: var(--link);
}

a:hover {
    text-decoration: underline;
}

.disclaimer {
    font-size: 0.75em;
    line-height: 130%;
    margin: 0.35em 0;
}

footer {
    margin-top: 10px;
    font-size: 0.7em;
    opacity: 70%;
}

input[type=text], input[type=number], textarea {
    font-family: -apple-system, sans-serif;
    line-height: 120%;
    background-color: var(--bg);
    color: var(--fg);
    border: 1px solid var(--input-border);
    padding: 0.2em 0.5em;
    border-radius: 1em;
}

.textarea-border-radius {
    border-radius: 1em;
    overflow: hidden;
    display: flex;
    border: 1px solid var(--input-border);
}

.textarea-border-radius > textarea {
    margin: -1px !important;
    border: none;
    flex: 1;
}

input[type=number] {
    width: 5em;
}

input[type=text]:focus, input[type=number]:focus, textarea:focus {
    outline: none;
}

textarea {
    resize: none;
    height: auto;
    padding: 0.3em 0.7em;
    font-size: 1em;
}

button {
    background-color: var(--my-message-bg);
    color: var(--my-message-fg);
    border: none;
    padding: 0.4em 0.8em;
    border-radius: 1em;
    font-size: 1em;
    margin: 0.2em 0;
    font-weight: 500;
}

button.secondary {
    background-color: var(--response-bg);
    color: var(--fg);
}

select {
    font-family: -apple-system, sans-serif;
    line-height: 120%;
    background-color: var(--bg);
    color: var(--fg);
    border: 1px solid var(--input-border);
    padding: 0.2em 0.5em;
    border-radius: 1em;
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    background-image: url("data:image/svg+xml;utf8,<svg xmlns=%27http://www.w3.org/2000/svg%27 viewBox=%270 0 10 7%27><path fill=%27%23666666%27 d=%27M5 6.8s-.1 0 0 0c-.1 0-.1 0 0 0L1 2.8 2.2 1.6 5 4.4 7.8 1.6 9 2.8z%27/></svg>");
    background-repeat: no-repeat;
    background-position: right 0.5em center;
    background-size: 14px;
    padding-right: 2em;
    font-size: 1em;
}

select:focus {
    outline: none;
}

.hidden {
    display: none !important;
}


.notification {
    visibility: hidden;
    min-width: 100px;
    margin-left: -50px;
    box-sizing: border-box;
    background-color: var(--fg);
    color: var(--bg);
    text-align: center;
    border-radius: 1em;
    padding: 0.8em;
    position: fixed;
    z-index: 1;
    left: 50%;
    top: 1.5em;
    font-size: 1em;
}

.show {
    visibility: visible !important;
    animation: fadein 0.35s ease, fadeout 0.5s ease 3.65s;
}

@keyframes fadein {
    from {top: 0; opacity: 0;}
    to {top: 1.5em; opacity: 1;}
}

@keyframes fadeout {
    from {top: 1.5em; opacity: 1;}
    to {top: 0; opacity: 0;}
}


@media (prefers-color-scheme: dark) {
    :root {
        --bg: hsl(215, 20%, 9%);
        --compose-box-bg: hsl(215, 20%, 9%, 0.7);
        --fg: hsl(0, 0%, 95%);
        --my-message-bg: hsl(215, 75%, 48%);
        --my-message-fg: white;
        --response-bg: hsl(215, 15%, 20%);
        --link: hsl(215, 100%, 65%);
        --input-border: hsla(0, 0%, 90%, 40%);
        --settings-icon-border: hsla(0, 0%, 100%, 50%);
        --info: hsl(130, 30%, 25%);
        --info-link: hsl(220, 100%, 85%);
        --error-fg: hsl(5, 70%, 60%);
        --success-fg: hsl(140, 70%, 45%);
        --code-bg: hsla(0, 0%, 0%, 0.25);
        --code-border: hsla(0, 0%, 0%, 0.15);
        --my-message-code-bg: hsla(0, 0%, 0%, 0.25);
        --my-message-code-border: hsla(0, 0%, 0%, 0.15);
    }

    * {
        color-scheme: dark;
    }
}
