:root {
    --bg: #f4f6f9;
    --panel: #fff;
    --fg: #1d2430;
    --muted: #6b7686;
    --line: #e3e8ee;
    --accent: #27a6e8;
    --up: #14a44d;
    --down: #e5484d;
    --hover: rgb(127 127 127 / 0.16);
    --radius: 12px;
    --sbw: 10px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0f141a;
        --panel: #171e27;
        --fg: #e6ebf2;
        --muted: #8a96a6;
        --line: #263140;
        --up: #3ddc84;
        --down: #ff6b6b;
    }
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--fg);
    font:
        14px/1.4 system-ui,
        -apple-system,
        "Segoe UI",
        sans-serif;
}

#grid {
    display: block;
    height: 100dvh;
    padding: 16px;
}

.inner {
    display: flex;
    flex-direction: column;
    height: 100%;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
}

.up {
    --dir: var(--up);
}
.down {
    --dir: var(--down);
}
.flat {
    --dir: var(--muted);
}
.flash-up {
    --flash: var(--up);
}
.flash-down {
    --flash: var(--down);
}

/* header */
.hrow {
    position: relative;
    z-index: 5;
    display: grid;
    grid-template-columns: var(--cols);
    padding-right: var(--sbw);
    background: var(--panel);
    border-bottom: 1px solid var(--line);
    border-radius: var(--radius) var(--radius) 0 0;
}

.th {
    position: relative;
    display: flex;
    align-items: center;
    gap: 4px;
    min-width: 0;
    min-height: 44px;
    padding-inline: 16px;
    color: var(--muted);
    font-size: 13px;
    font-weight: 600;
}
.th.right {
    justify-content: flex-end;
}
.th .title {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.th[data-sortable="1"] .title {
    cursor: pointer;
    user-select: none;
}
.th .sort {
    margin-left: 4px;
    font-size: 10px;
}

.icon {
    display: grid;
    place-items: center;
    flex: none;
    width: 28px;
    height: 28px;
    padding: 0;
    color: inherit;
    background: none;
    border: 0;
    border-radius: 6px;
    opacity: 0.7;
    cursor: pointer;
}
.icon svg {
    width: 18px;
    height: 18px;
}
.icon:hover {
    background: var(--hover);
    opacity: 1;
}
.icon.on {
    color: var(--accent);
    opacity: 1;
}

.th .drop-mark {
    position: absolute;
    inset: 0;
    border-radius: 4px;
    background: var(--hover);
    box-shadow: inset 0 0 0 2px currentColor;
    pointer-events: none;
}

/* rows */
.rows {
    position: relative;
    flex: 1;
    min-height: 0;
    overflow-y: auto;
}
.rows::-webkit-scrollbar {
    width: var(--sbw);
}

@supports not selector(::-webkit-scrollbar) {
    .rows {
        scrollbar-width: none;
    }
    :root {
        --sbw: 0px;
    }
}

.sizer {
    position: relative;
}

.row {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    display: grid;
    grid-template-columns: var(--cols);
    height: var(--row-h);
    background: var(--panel);
    border-bottom: 1px solid var(--line);
    transition: transform 0.35s ease;
    will-change: transform;
}
.row:hover {
    z-index: 1;
    background: color-mix(in srgb, var(--fg) 4%, var(--panel));
}

.cell {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
    padding-inline: 16px;
    overflow: hidden;
}
.cell.right {
    justify-content: flex-end;
    text-align: right;
}

.logo {
    display: grid;
    place-items: center;
    flex: none;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    color: #fff;
    font-size: 12px;
    font-weight: 700;
}
.cell b {
    font-size: 15px;
}
.cell b,
.cell .name {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.cell .name {
    color: var(--muted);
}

.cell .delta {
    margin-right: 8px;
    color: var(--dir);
    font-size: 13px;
}
.cell .delta:empty {
    display: none;
}
.cell .pill {
    padding: 4px 10px;
    border-radius: 999px;
    background: color-mix(in srgb, var(--dir) 12%, transparent);
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

.cell.tl {
    overflow: visible;
}

.spark {
    display: flex;
    align-items: flex-end;
    gap: 2px;
    width: 100%;
    max-width: 274px;
    height: 44px;
}
.spark i {
    position: relative;
    flex: 1;
    background: var(--accent);
    border-radius: 1px;
    opacity: 0.55;
}
.spark i[class],
.spark i:hover {
    opacity: 1;
}
.spark i:hover::after {
    content: attr(data-v);
    position: absolute;
    bottom: calc(100% + 4px);
    left: 50%;
    transform: translateX(-50%);
    z-index: 2;
    padding: 2px 6px;
    border-radius: 4px;
    background: var(--fg);
    color: var(--panel);
    font-size: 11px;
    line-height: 1.3;
    white-space: nowrap;
    pointer-events: none;
}

.cell.f0 .pill {
    animation: flash-0 0.8s ease-out;
}
.cell.f1 .pill {
    animation: flash-1 0.8s ease-out;
}
.spark i.f0 {
    animation: bar-0 0.8s ease-out;
}
.spark i.f1 {
    animation: bar-1 0.8s ease-out;
}

@keyframes flash-0 {
    from {
        background-color: color-mix(in srgb, var(--flash) 55%, transparent);
    }
}
@keyframes flash-1 {
    from {
        background-color: color-mix(in srgb, var(--flash) 55%, transparent);
    }
}
@keyframes bar-0 {
    from {
        background: var(--flash);
        opacity: 1;
    }
}
@keyframes bar-1 {
    from {
        background: var(--flash);
        opacity: 1;
    }
}

/* footer */
.foot {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding: 10px 16px;
    color: var(--muted);
    font-size: 13px;
    background: var(--panel);
    border-top: 1px solid var(--line);
    border-radius: 0 0 var(--radius) var(--radius);
}
.foot .link {
    text-decoration: underline;
    text-underline-offset: 2px;
}

/* popovers: column menu and filter */
.pop {
    position: absolute;
    top: calc(100% + 4px);
    z-index: 30;
    min-width: 190px;
    padding: 4px;
    background: var(--panel);
    color: var(--fg);
    border: 1px solid var(--line);
    border-radius: 8px;
    box-shadow: 0 6px 20px rgb(0 0 0 / 0.16);
    font-size: 14px;
    font-weight: 400;
    text-align: left;
}
.th.right .pop {
    right: 0;
}
.th:not(.right) .pop {
    left: 0;
}
.pop.filter {
    display: flex;
    gap: 4px;
    align-items: center;
}
.pop .sm-only {
    display: none;
}

.pop button,
.foot .link {
    font: inherit;
    color: inherit;
    background: none;
    border: 0;
    cursor: pointer;
}
.pop button {
    display: block;
    width: 100%;
    padding: 7px 10px;
    border-radius: 5px;
    text-align: left;
}
.pop.filter button {
    width: auto;
    flex: none;
}
.pop button:hover {
    background: var(--hover);
}

.pop input {
    flex: 1;
    min-width: 0;
    padding: 6px 8px;
    font: inherit;
    color: inherit;
    background: transparent;
    border: 1px solid var(--line);
    border-radius: 5px;
}

.pop input:focus-visible,
.pop button:focus-visible,
.icon:focus-visible,
.foot .link:focus-visible {
    outline: 2px solid currentColor;
    outline-offset: 1px;
}

.pop .sep {
    position: relative;
    margin-top: 5px;
}
.pop .sep::before {
    content: "";
    position: absolute;
    left: 6px;
    right: 6px;
    top: -3px;
    height: 1px;
    background: var(--line);
}

@media (prefers-reduced-motion: reduce) {
    * {
        animation: none !important;
        transition: none !important;
    }
}

/* phones */
@media (max-width: 640px) {
    :root {
        --sbw: 0px;
        --radius: 0px;
    }

    .sm-hide {
        display: none !important;
    }

    #grid {
        padding: 0;
    }
    .inner {
        border: 0;
    }

    .hrow,
    .row {
        grid-template-columns: var(--cols-sm, var(--cols));
    }
    .th,
    .cell {
        min-width: 0;
        padding-inline: 8px;
    }

    .th {
        gap: 2px;
    }
    .th .title {
        font-size: 12px;
        line-height: 1.15;
        white-space: normal;
    }
    .th .filter-btn {
        display: none;
    }
    .th .icon {
        width: 22px;
        height: 32px;
    }
    .th .icon svg {
        width: 16px;
        height: 16px;
    }
    .th .menu-btn {
        position: relative;
    }
    .th.filtered .menu-btn::after {
        content: "";
        position: absolute;
        top: 4px;
        right: 1px;
        width: 7px;
        height: 7px;
        border-radius: 50%;
        background: var(--accent);
    }

    .cell[data-col="ticker"] {
        display: grid;
        grid-template-columns: auto minmax(0, 1fr);
        grid-template-areas: "logo tick" "logo name";
        column-gap: 8px;
        align-content: center;
        align-items: center;
    }
    .cell[data-col="ticker"] .logo {
        grid-area: logo;
    }
    .cell[data-col="ticker"] b {
        grid-area: tick;
        align-self: end;
        line-height: 1.25;
    }
    .cell[data-col="ticker"] .name {
        grid-area: name;
        align-self: start;
        font-size: 11px;
        line-height: 1.25;
    }

    .cell.right[data-col="pnl"],
    .cell.right[data-col="total"] {
        display: flex;
        flex-direction: column-reverse;
        align-items: flex-end;
        justify-content: center;
        gap: 3px;
    }
    .cell .delta {
        margin: 0;
        font-size: 11px;
        line-height: 1;
    }
    .cell .pill {
        padding: 3px 8px;
        font-size: 13px;
    }

    .spark {
        height: 40px;
    }

    .foot {
        flex-wrap: wrap;
        gap: 2px 12px;
        padding-inline: 10px;
        font-size: 12px;
    }

    .pop,
    .th.right .pop,
    .th:not(.right) .pop {
        position: fixed;
        left: 8px;
        right: 8px;
        min-width: 0;
        padding: 6px;
        z-index: 40;
        border-radius: 14px;
        box-shadow: 0 6px 32px rgb(0 0 0 / 0.28);
    }
    .pop.menu {
        top: auto;
        bottom: max(8px, env(safe-area-inset-bottom));
    }
    .pop.filter {
        top: max(8px, env(safe-area-inset-top));
        bottom: auto;
    }
    .pop button {
        padding: 13px 14px;
    }
    .pop.filter button {
        padding: 10px 14px;
    }
    .pop .sm-only {
        display: block;
    }
    .pop input {
        padding: 10px 12px;
        font-size: 16px;
    }
}
