:root {
    color-scheme: light dark;
    --bg: #f3f4f6;
    --panel: #ffffff;
    --text: #15181c;
    --muted: #5b6470;
    --line: #dde0e5;
    --hover: #eef0f3;
    --selected: #e4ecf8;
    --accent: #1f5fbf;
    --up: #0b7a3b;
    --down: #c02626;
    --warn-bg: #fff4d6;
    --warn-text: #5e4700;
}

/* Dark colours apply when the system asks for them, unless the visitor chose light, or when the visitor chose dark. */
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: #0f1216;
        --panel: #171b21;
        --text: #e7e9ec;
        --muted: #98a1ac;
        --line: #2b313a;
        --hover: #20262e;
        --selected: #1d2c42;
        --accent: #7aa9ee;
        --up: #4cc27f;
        --down: #f27a7a;
        --warn-bg: #3a2f0c;
        --warn-text: #f5dd96;
    }
}

:root[data-theme="dark"] {
    color-scheme: dark;
    --bg: #0f1216;
    --panel: #171b21;
    --text: #e7e9ec;
    --muted: #98a1ac;
    --line: #2b313a;
    --hover: #20262e;
    --selected: #1d2c42;
    --accent: #7aa9ee;
    --up: #4cc27f;
    --down: #f27a7a;
    --warn-bg: #3a2f0c;
    --warn-text: #f5dd96;
}

:root[data-theme="light"] { color-scheme: light; }

* { box-sizing: border-box; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 13px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    font-variant-numeric: tabular-nums;
}

a { color: var(--accent); }
h1, h2, p, dl, dd { margin: 0; }
h1 { font-size: 20px; font-weight: 650; }
h2 { font-size: 13px; font-weight: 650; }
[hidden] { display: none !important; }

.muted { color: var(--muted); font-weight: 400; }
.up { color: var(--up); }
.down { color: var(--down); }
.flat { color: var(--muted); }

.visually-hidden, .skip:not(:focus) {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
}

.skip { position: absolute; z-index: 20; padding: 8px; background: var(--panel); }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

button, select, input, .file-button {
    min-height: 30px;
    border: 1px solid var(--line);
    border-radius: 3px;
    background: var(--panel);
    color: var(--text);
    font: inherit;
}

input, select { padding: 4px 8px; width: 100%; }
input:disabled { color: var(--muted); }

button, .file-button { padding: 4px 10px; cursor: pointer; }
button:hover:not(:disabled), .file-button:hover { background: var(--hover); }
button:disabled { color: var(--muted); cursor: default; }
button[type="submit"] { border-color: var(--accent); background: var(--accent); color: var(--panel); font-weight: 600; }
button[type="submit"]:hover:not(:disabled) { background: var(--accent); opacity: 0.9; }

.quiet { color: var(--muted); }

/* Top bar */
.topbar {
    position: sticky;
    top: 0;
    z-index: 10;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px 16px;
    padding: 6px 12px;
    background: var(--panel);
    border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: center; gap: 6px; color: var(--text); font-weight: 700; text-decoration: none; }
.topbar nav { display: flex; gap: 12px; }
.topbar nav a { color: var(--muted); text-decoration: none; padding: 4px 0; }
.topbar nav a:hover, .topbar nav a[aria-current] { color: var(--text); }

.search { position: relative; width: 200px; }
.list-group {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: var(--panel);
    border: 1px solid var(--line);
    border-top: 0;
}
.list-group:empty { display: none; }
.list-group-item { display: block; padding: 7px 8px; color: var(--text); text-decoration: none; }
.list-group-item:hover, .list-group-item:focus { background: var(--hover); }

.status { margin-left: auto; color: var(--muted); }
.status::before { content: ""; display: inline-block; width: 7px; height: 7px; margin-right: 6px; border-radius: 50%; background: var(--muted); }
.status[data-state="ok"]::before { background: var(--up); }
.status[data-state="stale"]::before { background: #d39e00; }
.status[data-state="error"]::before { background: var(--down); }

.languages { display: flex; gap: 8px; }
.languages a { color: var(--muted); text-decoration: none; padding: 4px 0; }
.languages a[aria-current] { color: var(--text); font-weight: 700; }

.theme select { width: auto; min-height: 26px; padding: 2px 4px; color: var(--muted); }
.search + .theme { margin-left: auto; }

.banner { padding: 7px 12px; background: var(--warn-bg); color: var(--warn-text); }

/* Layout */
.layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 500px;
    gap: 8px;
    max-width: 1500px;
    margin: 0 auto;
    padding: 8px;
}

.side { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.ledger { grid-column: 1 / -1; }

.panel { background: var(--panel); border: 1px solid var(--line); padding: 10px 12px; min-width: 0; }
.panel-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 4px 12px; margin-bottom: 8px; }
.note { margin-top: 8px; color: var(--muted); font-size: 12px; }
.note:empty { display: none; }

/* Selected pair */
.pair { display: flex; flex-direction: column; }
.pair-head { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 8px 24px; }
.quote { text-align: right; }
.price { display: block; font-size: 26px; font-weight: 650; line-height: 1.15; }
.moves { display: flex; justify-content: flex-end; gap: 14px; margin-top: 2px; }
.moves div { display: flex; gap: 5px; }
.moves dt { color: var(--muted); }
.moves dd { font-weight: 600; }

.toolbar { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 6px; margin: 10px 0 6px; }
.segments { display: flex; }
.segments button { border-radius: 0; margin-left: -1px; color: var(--muted); }
.segments button:first-child { margin-left: 0; border-radius: 3px 0 0 3px; }
.segments button:last-child { border-radius: 0 3px 3px 0; }
.segments button[aria-pressed="true"] { position: relative; background: var(--selected); border-color: var(--accent); color: var(--text); font-weight: 600; }

/* The canvas is taken out of the flow so it follows the box, instead of holding the box at its old size. */
.chart { position: relative; flex: 1 1 360px; min-height: 360px; }
.chart canvas { position: absolute; inset: 0; }

.stats { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); margin-top: 10px; border-top: 1px solid var(--line); }
.stats > div { padding: 8px 10px 0; border-left: 1px solid var(--line); }
.stats > div:first-child { padding-left: 0; border-left: 0; }
.stats dt { color: var(--muted); font-size: 12px; }
.stats dt[title] { text-decoration: underline dotted; text-underline-offset: 2px; cursor: help; }
.stats dd { font-size: 14px; font-weight: 600; overflow-wrap: anywhere; }
.stats dd.sub { color: var(--muted); font-size: 12px; font-weight: 400; }
.stats dd.sub.up { color: var(--up); }
.stats dd.sub.down { color: var(--down); }

/* Tables */
.data { width: 100%; border-collapse: collapse; }
.data th, .data td { padding: 5px 6px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
.data th { color: var(--muted); font-size: 12px; font-weight: 500; }
.data th:first-child, .data td:first-child { padding-left: 0; }
.data th:last-child, .data td:last-child { padding-right: 0; }
.data .num { text-align: right; }
.data tbody tr:last-child td { border-bottom: 0; }
.data th button { min-height: 0; padding: 0; border: 0; background: none; color: inherit; font: inherit; }
.data th button:hover:not(:disabled) { background: none; color: var(--text); }
.data th[aria-sort="ascending"] button::after { content: " ▲"; font-size: 9px; }
.data th[aria-sort="descending"] button::after { content: " ▼"; font-size: 9px; }
.data th[aria-sort="ascending"], .data th[aria-sort="descending"] { color: var(--text); }

.watchlist tbody tr { cursor: pointer; }
.watchlist tbody tr:hover { background: var(--hover); }
.watchlist tbody tr[aria-current] { background: var(--selected); }
.watchlist td a { color: var(--text); font-weight: 600; text-decoration: none; }
.watchlist td a:hover { text-decoration: underline; }
.trend { width: 88px; }
.trend svg { display: block; width: 80px; height: 20px; margin-left: auto; fill: none; stroke: var(--muted); stroke-width: 1.25; }

.index { display: flex; flex-wrap: wrap; gap: 4px 14px; margin-bottom: 6px; padding: 6px 8px; background: var(--bg); }
.index span { color: var(--muted); }
.index .index-label { margin-right: auto; color: var(--text); font-weight: 600; text-decoration: underline dotted; text-underline-offset: 2px; cursor: help; }

/* Converter */
.converter { display: grid; grid-template-columns: 110px 1fr; gap: 4px; }
.converter button { grid-column: 1; justify-self: start; min-width: 32px; }
.converter button + select { grid-column: 1; }
.converter #target { grid-column: 2; }

/* Holdings */
.ledger-actions { display: flex; gap: 6px; }
.file-button { display: inline-flex; align-items: center; }
.file-button input { position: absolute; width: 1px; min-height: 0; padding: 0; opacity: 0; }
.file-button:focus-within { outline: 2px solid var(--accent); outline-offset: 1px; }
.totals { grid-template-columns: repeat(4, minmax(0, 1fr)); margin: 0 0 10px; padding-bottom: 8px; border-bottom: 1px solid var(--line); }
.lot-form { display: grid; grid-template-columns: 110px minmax(0, 200px) minmax(0, 240px) auto; gap: 8px; align-items: end; justify-content: start; }
.lot-form label { color: var(--muted); font-size: 12px; }
.lot-form select, .lot-form input { display: block; margin-top: 2px; font-size: 13px; color: var(--text); }
.table-scroll { position: relative; margin-top: 10px; overflow-x: auto; }
.table-scroll table { min-width: 720px; }
.remove { min-height: 24px; padding: 0 8px; border: 0; background: none; color: var(--muted); }
.link-button { min-height: 0; padding: 0; border: 0; background: none; color: var(--accent); text-decoration: underline; }
.link-button:hover:not(:disabled) { background: none; }
.empty { color: var(--muted); white-space: normal !important; }
/* The table scrolls sideways on phones, but this message has to stay on screen. */
.empty span { display: block; position: sticky; left: 0; max-width: min(100%, calc(100vw - 32px)); }

.info { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 240px) minmax(0, 240px); gap: 8px 32px; grid-column: 1 / -1; }
.info h2 { margin-bottom: 6px; }
.info p { max-width: 70ch; font-size: 14px; line-height: 1.5; }
.info p + p { margin-top: 6px; }
.info p:empty { display: none; }
.conversions caption { padding-bottom: 4px; color: var(--muted); font-size: 12px; font-weight: 600; text-align: left; }

/* Text pages */
.page { max-width: 760px; margin: 0 auto; padding: 8px; }
.columns { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); align-items: start; gap: 8px; max-width: 1000px; margin: 0 auto; padding: 8px; }
.prose { padding: 16px 20px; font-size: 14px; line-height: 1.55; }
.prose h1 { margin-bottom: 10px; }
.prose h2 { margin: 18px 0 4px; font-size: 14px; }
.prose h2:first-child { margin-top: 0; }
.prose p + p { margin-top: 8px; }
.terms dt { margin-top: 8px; font-weight: 600; }
.code { display: block; width: 100%; margin: 8px 0; padding: 8px; border: 1px solid var(--line); border-radius: 3px; background: var(--bg); color: var(--text); font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; resize: vertical; }
.widget-frame { display: block; width: 100%; height: 150px; border: 0; }

/* Embedded converter */
/* The embed is taller than the widget, so the whole page has to be one colour. */
.widget-page { background: var(--panel); }
.widget { padding: 8px; }
.widget .note { margin-top: 6px; }

.footer { max-width: 1500px; margin: 0 auto; padding: 8px 12px 24px; color: var(--muted); font-size: 12px; }
.footer p + p { margin-top: 2px; }
.pair-links { margin-bottom: 8px; }

.page ~ .footer { max-width: 760px; }
.columns ~ .footer { max-width: 1000px; }

@media (max-width: 1100px) {
    .layout { grid-template-columns: minmax(0, 1fr); }
    .info { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .info > div { grid-column: 1 / -1; }
    .stats { grid-template-columns: repeat(3, minmax(0, 1fr)); }
    .stats > div:nth-child(3n + 1) { padding-left: 0; border-left: 0; }
    .totals { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .totals > div:nth-child(3n + 1) { padding-left: 10px; border-left: 1px solid var(--line); }
    .totals > div:nth-child(odd) { padding-left: 0; border-left: 0; }
}

@media (max-width: 800px) {
    .columns { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 640px) {
    /* A larger type size stops phones zooming the page when a field gets focus. */
    input, select { font-size: 16px; }
    .lot-form select, .lot-form input { font-size: 16px; }
    button, select, input, .file-button { min-height: 40px; }
    .data th button, .remove, .link-button { min-height: 32px; }
    .topbar { padding: 6px 16px; position: static; }
    .languages { order: 1; margin-left: auto; }
    .theme { order: 1; }
    .theme select { min-height: 40px; }
    .topbar nav { order: 2; width: 100%; }
    .search { order: 3; width: 100%; }
    .status { order: 4; width: 100%; margin-left: 0; }
    .layout { padding: 8px 0; }
    .panel { border-left: 0; border-right: 0; padding: 10px 16px; }
    .footer { padding: 8px 16px 24px; }
    .page, .columns { padding: 8px 0; }
    .prose { padding: 14px 16px; }
    .quote { width: 100%; text-align: left; }
    .moves { justify-content: flex-start; }
    .segments { flex: 1 1 100%; }
    .segments button { flex: 1 1 0; min-width: 0; padding: 4px 2px; }
    .chart { flex-basis: 260px; min-height: 260px; }
    .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .stats > div:nth-child(3n + 1) { padding-left: 10px; border-left: 1px solid var(--line); }
    .stats > div:nth-child(odd) { padding-left: 0; border-left: 0; }
    .opt { display: none; }
    .info { grid-template-columns: minmax(0, 1fr); }
    .lot-form { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
    .lot-form label:nth-child(3), .lot-form button { grid-column: 1 / -1; }
}
