:root {
    --bg: #10151b;
    --surface: #171d24;
    --surface-2: #1f2730;
    --line: #303944;
    --line-soft: rgba(255, 255, 255, 0.08);
    --text: #eef3f7;
    --muted: #98a6b3;
    --btc: #f59e0b;
    --eur: #38bdf8;
    --usdt: #2dd4bf;
    --pending: #f97316;
    --ok: #34d399;
    --warn: #fbbf24;
    --danger: #fb7185;
    --shadow: rgba(0, 0, 0, 0.24);
}

* {
    box-sizing: border-box;
}

html {
    min-height: 100%;
    background: var(--bg);
}

body {
    min-height: 100vh;
    margin: 0;
    background:
        linear-gradient(180deg, rgba(255, 255, 255, 0.03), transparent 260px),
        var(--bg);
    color: var(--text);
    font: 15px/1.45 Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
a,
input {
    font: inherit;
}

button,
a {
    -webkit-tap-highlight-color: transparent;
}

.app-shell {
    width: min(1180px, calc(100% - 28px));
    margin: 0 auto;
    padding: max(16px, env(safe-area-inset-top)) 0 max(28px, env(safe-area-inset-bottom));
}

.pull-refresh {
    --pull-offset: 0px;
    display: none;
    position: fixed;
    z-index: 100;
    top: max(8px, calc(env(safe-area-inset-top) + 4px));
    left: 50%;
    min-height: 38px;
    align-items: center;
    gap: 8px;
    padding: 0 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--surface-2);
    box-shadow: 0 8px 22px var(--shadow);
    color: var(--muted);
    font-size: 12px;
    font-weight: 700;
    pointer-events: none;
    opacity: 0;
    transform: translate(-50%, calc(-52px + var(--pull-offset)));
    transition: opacity 140ms ease, transform 180ms ease;
}

.pull-refresh.is-visible {
    opacity: 1;
}

.pull-refresh.is-pulling {
    transition: opacity 80ms linear;
}

.pull-refresh.is-ready {
    color: var(--text);
}

.pull-refresh-icon {
    display: inline-flex;
    font-size: 19px;
    line-height: 1;
    transform: rotate(0deg);
    transition: transform 160ms ease;
}

.pull-refresh.is-ready .pull-refresh-icon {
    transform: rotate(180deg);
}

.pull-refresh.is-refreshing .pull-refresh-icon {
    animation: pull-refresh-spin 800ms linear infinite;
}

@keyframes pull-refresh-spin {
    to {
        transform: rotate(360deg);
    }
}

.topbar {
    min-height: 74px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 8px 0 18px;
}

.title-block {
    width: 100%;
}

.title-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
}

.title-row h1 {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
}

.topbar h1,
.admin-header h1,
.access-panel h1 {
    margin: 0;
    font-size: 26px;
    line-height: 1.1;
    letter-spacing: 0;
}

.eyebrow {
    margin: 0 0 5px;
    color: var(--muted);
    font-size: 12px;
    font-weight: 800;
    letter-spacing: 0;
    text-transform: uppercase;
}

.top-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
}

.status-pill,
.icon-button,
button,
.admin-panel,
.access-panel,
.panel,
.metric-card {
    border: 1px solid var(--line);
    border-radius: 8px;
}

.status-pill {
    min-width: 96px;
    min-height: 38px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0 12px;
    background: var(--surface);
    color: var(--muted);
    font-size: 13px;
    font-weight: 700;
    white-space: nowrap;
}

.status-pill.ok {
    color: var(--ok);
}

.status-pill.warn {
    color: var(--warn);
}

.extra-info-toggle {
    min-height: 38px;
    padding: 0 12px;
    background: var(--surface);
    color: var(--muted);
    font-size: 13px;
    font-weight: 800;
    white-space: nowrap;
}

.extra-info-toggle.active {
    background: var(--surface-2);
    color: var(--text);
}

.icon-button,
button {
    min-width: 38px;
    min-height: 38px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-color: var(--line);
    background: var(--surface);
    color: var(--text);
    text-decoration: none;
    cursor: pointer;
}

.icon-button:hover,
button:hover {
    border-color: rgba(255, 255, 255, 0.24);
    background: var(--surface-2);
}

button:disabled {
    cursor: wait;
    opacity: 0.68;
}

.primary {
    background: #2563eb;
    border-color: #2563eb;
    color: #fff;
    font-weight: 800;
    padding: 0 14px;
}

.secondary {
    padding: 0 14px;
}

.danger {
    color: #ffd7dd;
    border-color: rgba(251, 113, 133, 0.55);
}

.wide {
    width: 100%;
}

.portfolio-tabs {
    display: inline-grid;
    grid-template-columns: repeat(4, minmax(92px, 1fr));
    gap: 4px;
    width: min(520px, 100%);
    margin: 0 0 12px;
    padding: 4px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: rgba(0, 0, 0, 0.16);
}

.portfolio-tab {
    min-height: 36px;
    border: 0;
    background: transparent;
    color: var(--muted);
    font-size: 13px;
    font-weight: 900;
}

.portfolio-tab.active {
    background: var(--surface-2);
    color: var(--text);
}

.metric-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 12px;
    margin-bottom: 12px;
}

.metric-card {
    min-height: 132px;
    position: relative;
    overflow: hidden;
    background: var(--surface);
    padding: 16px;
    box-shadow: 0 14px 30px var(--shadow);
}

.metric-card::before {
    content: "";
    position: absolute;
    inset: 0 auto 0 0;
    width: 4px;
    background: var(--muted);
}

.metric-card span,
.metric-card small,
.panel-head span,
.source-row p,
.key-row p,
.admin-panel p {
    color: var(--muted);
}

.metric-card span {
    display: block;
    margin-bottom: 10px;
    font-size: 13px;
    font-weight: 800;
    text-transform: uppercase;
}

.metric-card strong {
    display: block;
    min-height: 39px;
    font-size: 31px;
    line-height: 1.18;
    letter-spacing: 0;
    word-break: break-word;
}

.metric-card small {
    display: block;
    margin-top: 7px;
}

.accent-btc::before { background: var(--btc); }
.accent-eur::before { background: var(--eur); }
.accent-usdt::before { background: var(--usdt); }

.content-grid {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 12px;
    margin-bottom: 12px;
}

.equity-panel {
    margin-bottom: 12px;
}

.chart-controls {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px;
    border: 1px solid var(--line-soft);
    border-radius: 8px;
    background: rgba(0, 0, 0, 0.16);
}

.chart-tab {
    min-width: 58px;
    min-height: 30px;
    border: 0;
    background: transparent;
    color: var(--muted);
    font-size: 12px;
    font-weight: 900;
}

.chart-tab.active {
    background: var(--surface-2);
    color: var(--text);
}

.chart-wrap {
    height: clamp(240px, 34vw, 360px);
    padding: 12px 12px 2px;
}

.chart-wrap canvas {
    width: 100%;
    height: 100%;
    display: block;
}

.chart-footer {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 12px;
    min-height: 42px;
    padding: 0 16px 12px;
    color: var(--muted);
    font-size: 13px;
}

.chart-footer strong {
    color: var(--text);
}

.panel {
    background: rgba(23, 29, 36, 0.94);
    box-shadow: 0 12px 26px var(--shadow);
}

.panel-head {
    min-height: 54px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 14px 16px;
    border-bottom: 1px solid var(--line-soft);
}

.panel-head h2 {
    margin: 0;
    font-size: 16px;
    line-height: 1.2;
    letter-spacing: 0;
}

.lightweight-chart-wrap {
    position: relative;
}

.lightweight-chart {
    width: 100%;
    height: 100%;
    overflow: hidden;
}

.lightweight-chart-tooltip {
    position: absolute;
    z-index: 4;
    min-width: 190px;
    display: grid;
    gap: 4px;
    padding: 10px 12px;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: rgba(5, 8, 12, 0.94);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.34);
    color: var(--text);
    font-size: 12px;
    pointer-events: none;
}

.lightweight-chart-tooltip span {
    color: var(--muted);
}

.sources-list,
.setup-list {
    display: grid;
    gap: 10px;
    padding: 12px;
}

.sources-list {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

.source-row,
.setup-row,
.key-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: 12px;
    min-height: 64px;
    padding: 12px;
    border: 1px solid var(--line-soft);
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.025);
}

.source-row strong,
.setup-row strong,
.key-row strong {
    display: block;
    margin-bottom: 3px;
}

.source-row p,
.setup-row p,
.key-row p,
.admin-panel p {
    margin: 0;
    font-size: 13px;
}

.badge {
    min-width: 82px;
    min-height: 30px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0 10px;
    border-radius: 999px;
    background: rgba(152, 166, 179, 0.14);
    color: var(--muted);
    font-size: 12px;
    font-weight: 800;
    white-space: nowrap;
}

.badge.ok {
    background: rgba(52, 211, 153, 0.16);
    color: var(--ok);
}

.badge.warn,
.badge.config_missing,
.badge.empty {
    background: rgba(251, 191, 36, 0.16);
    color: var(--warn);
}

.badge.error {
    background: rgba(251, 113, 133, 0.16);
    color: var(--danger);
}

.holdings-panel {
    margin-bottom: 12px;
}

.holdings-actions {
    display: inline-flex;
    align-items: center;
    justify-content: flex-end;
    gap: 10px;
}

.holdings-toggle {
    display: none;
    min-height: 32px;
    padding: 0 12px;
    font-size: 12px;
    font-weight: 800;
}

.table-wrap {
    overflow-x: auto;
}

table {
    width: 100%;
    min-width: 780px;
    border-collapse: collapse;
}

th,
td {
    padding: 12px 14px;
    border-bottom: 1px solid var(--line-soft);
    text-align: left;
    vertical-align: middle;
    white-space: nowrap;
}

th {
    color: var(--muted);
    font-size: 12px;
    font-weight: 900;
    text-transform: uppercase;
}

td.numeric,
th.numeric {
    text-align: right;
}

tbody tr:last-child td {
    border-bottom: 0;
}

.position-row {
    cursor: pointer;
}

.position-row td {
    transition: background-color 140ms ease;
}

.position-row:hover td,
.position-row:focus-visible td,
.position-row.is-expanded td {
    background: rgba(255, 255, 255, 0.035);
}

.position-row:focus-visible {
    outline: 2px solid var(--eur);
    outline-offset: -2px;
}

.position-row.is-expanded td {
    border-bottom-color: transparent;
}

.position-source {
    display: inline-flex;
    align-items: center;
    gap: 12px;
}

.position-chevron {
    width: 8px;
    height: 8px;
    flex: 0 0 8px;
    border-right: 2px solid var(--muted);
    border-bottom: 2px solid var(--muted);
    transform: rotate(-45deg);
    transition: transform 140ms ease, border-color 140ms ease;
}

.position-row.is-expanded .position-chevron {
    border-color: var(--text);
    transform: rotate(45deg);
}

.position-chart-row td {
    padding: 0;
    background: rgba(9, 13, 18, 0.38);
    white-space: normal;
}

.position-chart-shell {
    width: min(100%, calc(100vw - 46px));
    padding: 14px 16px 16px;
}

.position-chart-head {
    min-height: 38px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 8px;
}

.position-chart-head strong {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
}

.position-chart-controls .chart-tab {
    min-width: 52px;
    min-height: 28px;
}

.position-chart-wrap {
    position: relative;
    height: clamp(210px, 26vw, 290px);
}

.position-lightweight-chart {
    width: 100%;
    height: 100%;
    overflow: hidden;
    display: block;
}

.position-chart-footer {
    min-height: 34px;
    display: flex;
    align-items: flex-end;
    justify-content: flex-end;
    color: var(--muted);
    font-size: 13px;
}

.position-chart-footer strong {
    color: var(--text);
}

.position-chart-status {
    min-height: 210px;
    display: grid;
    place-items: center;
    color: var(--muted);
}

.position-chart-status.error {
    color: var(--danger);
}

.empty-cell {
    color: var(--muted);
    text-align: center;
}

.setup-panel.hidden,
.hidden {
    display: none;
}

.access-page,
.admin-page {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 20px;
}

.admin-page {
    place-items: start center;
}

.access-panel,
.admin-wrap {
    width: min(460px, 100%);
}

.admin-wrap {
    width: min(820px, 100%);
}

.access-panel,
.admin-panel {
    background: var(--surface);
    padding: 18px;
    box-shadow: 0 16px 34px var(--shadow);
}

.access-panel p {
    color: var(--muted);
}

.admin-header {
    margin: 12px 0 14px;
}

.admin-panel {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 12px;
}

.admin-panel input {
    min-width: 220px;
    flex: 1;
    min-height: 40px;
    padding: 0 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #0d1117;
    color: var(--text);
}

.form-error {
    min-height: 20px;
    color: var(--danger);
    font-size: 13px;
}

@media (max-width: 980px) {
    .content-grid {
        grid-template-columns: 1fr;
    }
}

@media (min-width: 641px) {
    .desktop-extra-panel.desktop-extra-hidden {
        display: none;
    }
}

@media (max-width: 640px) {
    .app-shell {
        width: min(100% - 18px, 1180px);
    }

    .topbar {
        min-height: 58px;
        padding: 8px 0 10px;
    }

    .title-row {
        gap: 8px;
    }

    .topbar h1 {
        flex: 1 1 auto;
        font-size: clamp(20px, 5.7vw, 23px);
        white-space: nowrap;
    }

    .eyebrow {
        margin-bottom: 3px;
        font-size: 11px;
    }

    .top-actions {
        flex: 0 0 auto;
        gap: 6px;
    }

    .extra-info-toggle {
        display: none;
    }

    .status-pill {
        min-width: 66px;
        min-height: 32px;
        padding: 0 8px;
        font-size: 12px;
    }

    .icon-button,
    button {
        min-width: 32px;
        min-height: 32px;
    }

    .metric-grid,
    .content-grid {
        grid-template-columns: 1fr;
    }

    .metric-grid {
        gap: 8px;
        margin-bottom: 8px;
    }

    .lightweight-chart-wrap {
        padding-right: 4px;
    }

    .portfolio-tabs {
        display: grid;
        grid-template-columns: repeat(4, minmax(72px, 1fr));
        width: 100%;
        margin-bottom: 8px;
    }

    .portfolio-tab {
        min-width: 0;
        min-height: 32px;
        font-size: 12px;
    }

    .metric-card {
        min-height: 86px;
        padding: 11px 12px;
    }

    .metric-card span {
        margin-bottom: 6px;
        font-size: 11px;
    }

    .metric-card strong {
        min-height: 27px;
        font-size: 22px;
        line-height: 1.12;
    }

    .metric-card small {
        margin-top: 4px;
        font-size: 13px;
    }

    .sources-list {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 8px;
        padding: 10px;
    }

    .source-row,
    .setup-row,
    .key-row,
    .admin-panel {
        grid-template-columns: 1fr;
        display: grid;
    }

    .source-row {
        align-content: space-between;
        min-height: 96px;
        padding: 10px;
        gap: 8px;
    }

    .source-row strong {
        margin-bottom: 2px;
        font-size: 14px;
    }

    .source-row p {
        font-size: 12px;
        line-height: 1.25;
    }

    .source-row .badge {
        width: 100%;
        min-width: 0;
        min-height: 26px;
        font-size: 11px;
    }

    .panel-head {
        align-items: flex-start;
        flex-direction: column;
    }

    .holdings-panel .panel-head,
    .sources-panel .panel-head {
        align-items: center;
        flex-direction: row;
    }

    .holdings-actions {
        gap: 8px;
    }

    .holdings-toggle {
        display: inline-flex;
    }

    .holdings-panel.is-collapsed .table-wrap {
        display: none;
    }

    .sources-panel.is-collapsed .sources-list {
        display: none;
    }

    .position-chart-shell {
        padding: 12px 10px 14px;
    }

    .position-chart-head {
        align-items: stretch;
        flex-direction: column;
    }

    .position-chart-controls {
        width: 100%;
        display: grid;
        grid-template-columns: repeat(3, 1fr);
    }

    .position-chart-wrap {
        height: 220px;
    }

    .chart-controls {
        width: 100%;
        display: grid;
        grid-template-columns: repeat(3, 1fr);
    }

    .admin-panel input,
    .admin-panel button {
        width: 100%;
    }
}

@media (max-width: 640px) and (pointer: coarse) {
    body.pull-refresh-enabled .pull-refresh {
        display: flex;
    }
}

@media (prefers-reduced-motion: reduce) {
    .pull-refresh,
    .pull-refresh-icon {
        transition: none;
    }

    .pull-refresh.is-refreshing .pull-refresh-icon {
        animation: none;
    }
}
