/* Light mode */
:root {
    --background: #FFFFFF;
    --text: #15141A;
    --text-dim: #5B5B66;
    --formula: #15141A;
    --link: #1870F0;
    --scrollbar-bg: transparant;
    --scrollbar-color: rgba(0, 0, 0, 0.25);
    --elem-radius: 0.25em;
    --quote-bg: #d6d6d6;
    --quote-text: #5B5B66;
    --quote-left: #464646;
    --code-text: #616161;
    --code-bg: #d3d3d3;
    --border: #0f0f0f;
    --border-dim: #8d8d8d;
    --selected-text: #15141A;
    --selected-bg: #73a5f0bf;
    --panel-bg: #d2f3ffbd;
}

.external-link {
    &::after {
        content: url(./web-light.svg);
        display: inline-block;
        width: 0.7em;
        height: 0.7em;
        margin-inline-start: 0.2em;
        margin-inline-end: 0.1em;
    }
    &.preview {
        --delay: 0.5s;
        --height: 1em;
        background: linear-gradient(to right, rgba(255, 0, 0, 1), rgba(255, 0, 180, 1), rgba(0, 100, 200, 1));
        background-size: 0 var(--height);
        background-position: 0 100%;
        background-repeat: no-repeat;
        transition: color var(--delay), background-size var(--delay);
        &.preview-waiting:hover, &.preview-fetching {
            background-size: 100% var(--height);
        }
    }
    &[href^="mailto:"]::after {
        content: url(./email-light.svg);
    }
    &[href^="/attachments/"]::after {
        content: url(./attachment-light.svg);
    }
}

.element-link {
    cursor: pointer;
    text-decoration: none;

    &::after {
        content: url(./element-light.svg);
        display: inline-block;
        width: 0.7em;
        height: 0.7em;
        margin-inline-start: 0.2em;
        margin-inline-end: 0.1em;
    }
}

html {
    color-scheme: light;
}

/* Dark mode */
@media (prefers-color-scheme: dark) {
    :root {
        --background: #15141A;
        --text: #FBFBFE;
        --text-dim: #CFCFD8;
        --link: #74A9f6;
        --formula: #FBFBFE;
        --scrollbar-bg: transparant;
        --scrollbar-color: rgba(255, 255, 255, 0.25);
        --quote-bg: #1f242c;
        --quote-text: #CFCFD8;
        --quote-left: #d3d3d3;
        --code-text: #abb2bf;
        --code-bg: #282c34;
        --border: #fbfbfe;
        --border-dim: #52525e;
        --selected-text: #FBFBFE;
        --selected-bg: #2a76e9a1;
        --panel-bg: #1d263cbd;
    }

    .external-link {
        &::after {
            content: url(./web-dark.svg);
        }
        &[href^="mailto:"]::after {
            content: url(./email-dark.svg);
        }
        &[href^="./attachments/"]::after {
            content: url(./attachment-dark.svg);
        }
    }

    .element-link::after {
        content: url(./element-dark.svg);
    }

    html {
        color-scheme: dark;
    }
}

/* Set the attributes */
html {
    scroll-behavior: smooth;
}

body {
    color: var(--text);
    background: var(--background);
    scrollbar-color: var(--scrollbar-color) var(--scrollbar-bg);
    margin: 0;
}

::selection {
    color: var(--selected-text);
    background: var(--selected-bg);
}

ul {
    padding-inline-start: 1em;
}

body>div>h1 {
    display: inline;
}

header {
    text-align: center;
}

article {
    display: block;
    padding: 0em 1em;
    line-height: 1.5;
    word-break: break-word;
    transition: opacity 0.2s ease-in-out;

    img {
        max-width: 100%;
    }
}

aside {
    &.panel {
        transition: all 0.2s ease-in-out;
        line-height: 1.5;
        padding: 0;
        overflow-y: scroll;
    }
    &.right-panel {
        position: fixed;
        top: -100%;
        background-color: var(--panel-bg);
        backdrop-filter: blur(2px);
        display: flex;
        justify-content: center;
        width: 100%;
        border-radius: 0 0 1em 1em;
        max-height: 70%;
        z-index: 4;
    }
}

div {
    padding: 1em;

    &#overlay {
        position: fixed;
        top: 0px;
        left: 0px;
        width: 100%;
        height: 100%;
        padding: 0;
        z-index: 3;
        display: block;
        filter: alpha(opacity=60);
        background-color: #323232;
        opacity: 0.5;
        -moz-opacity: 0.5;
        transition: opacity 0.2s ease-in-out;
    }
    &#tags {
        margin: 0;
        padding: 0;
        display: flex;
        flex-wrap: wrap;
    }
    &#main-content {
        margin: 0;
        padding: 0;
    }
}

span#from {
    cursor: pointer;
}

blockquote {
    border-radius: 0.25em;
    border-radius: var(--elem-radius);
    border-left: 0.3em solid var(--quote-left);
    padding: 0.2rem 1rem 0.2rem 1rem;
    margin: 0.2em;
    background: var(--quote-bg);
    color: var(--quote-text);
    overflow: auto;
}

table {
    border: 1px solid var(--border-dim);
    border-collapse: collapse;
    margin: 0.5rem 0;
    width: auto;
    font-size: .875em;

    td, th {
        padding: 0.5rem 0.75rem;
        border: 1px solid var(--border-dim);
        vertical-align: middle;
    }
}

input {
    outline: none;
    transition: border-color 0.2s ease-in-out, color 0.2s ease-in-out;
    border: 1px solid;
    border-radius: 0.5em;
    border-color: var(--border-dim);
    color: var(--text-dim);

    &[type="file"] {
        border-color: var(--border-dim);
        border-style: dashed;
        border-radius: 0.5em;
        padding: 0.5em;
        transition: border-color 0.2s ease-in-out, color 0.2s ease-in-out;
        &:focus, &:hover {
            border-color: var(--border);
        }
        &::file-selector-button {
            background-color: transparent;
            border-radius: 8px;
            color: var(--text);
            border: 1px solid;
            border-color: var(--border-dim);
            height: 2em;
            transition: border-color 0.2s ease-in-out, background-color 0.2s ease-in-out;
        }
        &::file-selector-button:hover {
            border-color: var(--border);
            background-color: var(--panel-bg);
        }
    }

    &:hover, &:focus {
        border-color: var(--border);
        color: var(--text);
    }

    &:disabled {
        opacity: 0.5;
        cursor: progress;
    }
}

fieldset {
    margin: 0.5em;
    border: 1px solid var(--border-dim);
    border-radius: 0.5em;
    transition: border-color 0.2s ease-in-out;

    &:hover, &:focus {
        border-color: var(--border);
    }
    > legend {
        padding: 0em 0.5em;
    }
}

details {
    border: 1px solid var(--border-dim);
    border-radius: 1em;
    transition: border-color 0.2s ease-in-out;

    > summary {
        padding: 1em;
        cursor: pointer;
    }

    &:hover, &:focus {
        border-color: var(--border);
    }
    &:open {
        > summary {
            border-bottom: 1px dashed var(--border-dim);
        }
    }
}

ul {
    &.outline {
        padding: 0;
        margin: 0;
        list-style: none;
        color: var(--quote-text);
        > li {
            height: 1.5em;
        }
    }
    &#bookmarks {
        list-style-type: none;
        padding-inline-start: 1em;
        margin: 0.5em 0 0 0;

        > li {
            display: flex;
            justify-content: space-between;
        }
    }
}

div#float-buttons {
    position: fixed;
    bottom: 0;
    right: 0;
    display: flex;
    flex-direction: column;
    gap: 1em;
    user-select: none;
    z-index: 5;
}

a {
    color: var(--text-dim);
    transition: color 0.2s ease-in-out;

    &:hover {
        color: var(--link);
    }
    &.anchor {
        transition-property: opacity;
        opacity: 0;
        color: var(--link);
        text-decoration: none;
        padding: 0px 0.5em;

        &::before {
            content: "#";
        }
    }
    &.outline-link {
        text-decoration: none;
        white-space: pre;
        display: inline-block;
        width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    &.button {
        transition-property: opacity, transform;
        display: flex;
        align-items: center;
        justify-content: center;
        text-decoration: none;
        color: var(--quote-text);
        background-color: var(--quote-bg);
        border-radius: 50%;
        width: 2em;
        height: 2em;
        font-size: 1.5em;
        font-weight: bold;
        opacity: 0.6;

        &:hover {
            opacity: 0.8;
            transform: scale(1.1);
        }
    }
    &.bookmark-op {
        text-decoration: none;
        text-align: center;
        width: 1em;
    }
    &.tag {
        display: block;
        border: 1px solid var(--border-dim);
        border-radius: 1em;
        padding: 0px 0.5em;
        margin: 0.2em 0.2em;
        width: fit-content;
        height: fit-content;
        text-decoration: none;
    }
}

.katex {
    font-size: 1em;
    color: var(--formula);
}

#main-content > aside.panel.left-panel > div:nth-child(2) {
    padding-top: 0px;
    padding-bottom: 0px;
}

/* Anchor display on hover */
 h1, h2, h3, h4, h5, h6 {
    &:hover > a.anchor {
        opacity: 1;
        transition: opacity 0.2s ease-in-out;
    }
 }

footer {
    font-size: small;
    text-align: center;
    opacity: 0.5;
    margin: 1em 0;
    transition: opacity 0.2s ease-in-out;

    &:hover, &:focus {
        opacity: 0.8;
    }
    > svg {
        width: 1.5em;
        height: 1.5em;
        vertical-align: middle;
        fill: var(--text);
    }
    > a {
        text-decoration: none;
    }
}

.github-corner {
    opacity: 0.4;
    transition: opacity 0.2s ease-in-out;

    &:hover, &:focus {
        opacity: 1;
    }
    > svg {
        position: absolute;
        top: 0;
        border: 0;
        right: 0;
        width: 4em;
        height: 4em;
        fill: var(--text);
        color: var(--background);
    }
}

/* Widescreen */
@media screen and (min-width: 767px) {
    div#main-content {
        display: flex;
    }

    blockquote {
        padding: 0.5em 1em 0.5em 1em;
    }

    article {
        flex: 6;
        width: 60%;
    }

    aside.panel {
        display: unset;
        position: sticky;
        top: 0;
        flex: 2;
        align-self: flex-start;
        max-height: 100vh;
    }

    aside.right-panel {
        justify-content: unset;
        transition: none;
        order: 1;
        background-color: unset;
        backdrop-filter: none;
        border-radius: unset;
    }

    ul.outline {
        padding: 0 0 0 1em;
        border-left: 2px solid var(--border-dim);
    }

    a.button {
        font-size: 1.8em;
        color: var(--quote-text);
        background-color: var(--quote-bg);
    }

    a.button#show-sidebar {
        display: none;
    }

    /* Webkit scrollbar (from MDN) */
    ::-webkit-scrollbar-track {
        border-radius: 10px
    }

    ::-webkit-scrollbar {
        width: 12px;
        height: 12px;
        background-color: auto;
        background-color: var(--scrollbar-bg, auto)
    }

    ::-webkit-scrollbar-thumb {
        border-radius: 10px;
        border: 2px auto;
        border: 2px solid var(--scrollbar-bg, auto);
        background: var(--scrollbar-color)
    }
}

/* Can't perform hover action */
@media (any-hover: none) {
    a.anchor {
        opacity: 1;
    }

    a.button {
        opacity: 0.8;
    }
}

/* User prefers no animation */
@media screen and (prefers-reduced-motion: reduce) {
    html {
        scroll-behavior: auto;
    }

    a.anchor,
    h1:hover>a.anchor,
    h2:hover>a.anchor,
    h3:hover>a.anchor,
    h4:hover>a.anchor,
    h5:hover>a.anchor,
    h6:hover>a.anchor {
        transition: none;
    }

    a {
        transition: none;
    }

    a:hover {
        transition: none;
    }

    aside.panel {
        transition: none;
    }

    article {
        transition: none;
    }

    input {
        transition: none;
    }
}

/* Adapted from animate.css - https://animate.style/ */
@keyframes flash {
    from, 50%, to { opacity: 1; }
    25%, 75% { opacity: 0; }
}
.attention { animation-name: flash; animation-duration: 1s; animation-fill-mode: both; }
