/* ============================================================
   InvMon.mobile — Responsive layout styles
   Mobile-first, dark/light theme via CSS custom properties.
   Two-panel (portrait) / three-panel (landscape).
   ============================================================ */

/* --- Theme: Dark (default) --- */
/*
 * Colors map to InvMon SwingStyleFactory / IStyleFactory palette:
 *   bg-primary       = im_softBlack       #303030
 *   bg-secondary     = im_black           #2a2a2a (hardBackground)
 *   bg-tertiary      = a touch lighter for hover states
 *   text-primary     = im_textColor_d     #e5e5e5
 *   text-secondary   = im_remarkColor_d   #747474
 *   color-accent     = im_titleColor_d    #93a5cb (InvMon blue, dark variant)
 *   color-positive   = im_green_d         #28712a
 *   color-negative   = im_red_d           #b71f1f
 *   color-watchlist  = im_orange_d        #db7d0b
 */
:root {
    --bg-primary: #303030;
    --bg-secondary: #2a2a2a;
    --bg-tertiary: #3a3a3a;
    --bg-chart: #2a2a2a;
    --bg-overlay: rgba(0, 0, 0, 0.7);
    --text-primary: #e5e5e5;
    --text-secondary: #afb8bd;
    --text-muted: #747474;
    --color-positive: #28712a;
    --color-negative: #b71f1f;
    --color-accent: #93a5cb;
    --color-candidate: #28712a;
    --color-watchlist: #db7d0b;
    /* border-color sits just above the panel bg so dividers are subtly visible */
    --border-color: #3e3e3e;
    --divider-strong: #4a4a4a;
    --header-bg: #2a2a2a;
    --portfolio-title-bg: #2a2a2a;
    --section-header-bg: transparent;
    --section-header-color: #93a5cb;
    --settings-bg: #2a2a2a;
    --settings-input-bg: #1f1f1f;
    --settings-input-border: #546a74;
    --scrollbar-thumb: rgba(255, 255, 255, 0.18);
    --scrollbar-track: transparent;
    --tab-active-bg: #223d54;
    --tab-inactive-color: #afb8bd;
}

/* --- Theme: Light --- */
/*
 *   bg-primary       = im_softWhite       #f5f5f5
 *   bg-secondary     = im_white           #ffffff (hardBackground)
 *   bg-tertiary      = im_lightGray       #dbdfe1
 *   text-primary     = im_textColor       #0a0a0a
 *   text-secondary   = im_remarkColor     #707070
 *   color-accent     = im_titleColor      #274a91 (InvMon blue)
 *   color-positive   = im_green           #2a752c
 *   color-negative   = im_red             #bf2121
 *   color-watchlist  = im_orange          #e2810b
 */
:root[data-theme="light"] {
    --bg-primary: #f5f5f5;
    --bg-secondary: #ffffff;
    --bg-tertiary: #dbdfe1;
    --bg-chart: #ffffff;
    --bg-overlay: rgba(0, 0, 0, 0.4);
    --text-primary: #0a0a0a;
    --text-secondary: #707070;
    --text-muted: #afb8bd;
    --color-positive: #2a752c;
    --color-negative: #bf2121;
    --color-accent: #274a91;
    --color-candidate: #2a752c;
    --color-watchlist: #e2810b;
    --border-color: #dbdfe1;
    --divider-strong: #c5c9cc;
    --header-bg: #ffffff;
    --portfolio-title-bg: #ffffff;
    --section-header-bg: transparent;
    --section-header-color: #274a91;
    --settings-bg: #ffffff;
    --settings-input-bg: #f5f5f5;
    --settings-input-border: #dbdfe1;
    --scrollbar-thumb: rgba(0, 0, 0, 0.18);
    --scrollbar-track: transparent;
    --tab-active-bg: #cde8ff;
    --tab-inactive-color: #707070;
}

/* --- Reset & Base --- */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body {
    height: 100%;
    overflow: hidden;
    font-family: -apple-system, 'Helvetica Neue', Arial, sans-serif;
    font-size: 14px;
    background: var(--bg-primary);
    color: var(--text-primary);
    -webkit-font-smoothing: antialiased;
    -webkit-tap-highlight-color: transparent;
    -webkit-text-size-adjust: 100%;
}

/* --- Scrollbar styling --- */
.scroll-y {
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.scroll-y::-webkit-scrollbar {
    width: 4px;
}

.scroll-y::-webkit-scrollbar-track {
    background: var(--scrollbar-track);
}

.scroll-y::-webkit-scrollbar-thumb {
    background: var(--scrollbar-thumb);
    border-radius: 2px;
}

/* --- App Shell --- */
#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
}

/* --- Header --- */
.header {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 44px;
    padding: 0 16px;
    background: var(--header-bg);
    border-bottom: 1px solid var(--border-color);
    z-index: 20;
}

.header-title {
    font-size: 15px;
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.header-settings {
    flex-shrink: 0;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--text-secondary);
    font-size: 18px;
    border: none;
    background: none;
    border-radius: 6px;
    transition: background 0.15s;
}

.header-settings:hover {
    background: var(--bg-tertiary);
}

/* --- Portfolio Title Bar --- */
.portfolio-title-bar {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    height: 38px;
    padding: 0 16px;
    background: var(--portfolio-title-bg);
    border-bottom: 1px solid var(--border-color);
    z-index: 15;
}

/* Switched mode: narrow — dropdown */
.portfolio-dropdown {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    cursor: pointer;
    position: relative;
}

.portfolio-dropdown-label {
    font-size: 13px;
    font-weight: 700;
    color: var(--color-accent);
}

.portfolio-dropdown-arrow {
    font-size: 12px;
    color: var(--text-secondary);
    margin-left: 8px;
}

.portfolio-dropdown-menu {
    display: none;
    position: absolute;
    top: 100%;
    left: -16px;
    right: -16px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-top: none;
    z-index: 30;
    max-height: 300px;
    overflow-y: auto;
}

.portfolio-dropdown-menu.open {
    display: block;
}

.portfolio-dropdown-item {
    padding: 10px 16px;
    font-size: 13px;
    color: var(--text-primary);
    cursor: pointer;
    border-bottom: 1px solid var(--border-color);
}

.portfolio-dropdown-item:hover {
    background: var(--bg-tertiary);
}

.portfolio-dropdown-item.active {
    color: var(--color-accent);
    font-weight: 600;
}

/* Switched mode: wide — horizontal tabs */
.portfolio-tabs {
    display: none;
    align-items: center;
    gap: 4px;
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.portfolio-tab {
    flex-shrink: 0;
    padding: 6px 14px;
    font-size: 12px;
    color: var(--tab-inactive-color);
    cursor: pointer;
    border-radius: 6px;
    white-space: nowrap;
    position: relative;
    transition: background 0.15s, color 0.15s;
}

.portfolio-tab:hover {
    background: var(--bg-tertiary);
    color: var(--text-primary);
}

.portfolio-tab.active {
    background: transparent;
    color: var(--color-accent);
    font-weight: 700;
}

.portfolio-tab.active::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 8px;
    right: 8px;
    height: 2px;
    background: var(--color-accent);
    border-radius: 1px;
}

/* --- Stacked mode: portfolio title is inline --- */
.portfolio-section-title {
    display: flex;
    align-items: center;
    height: 34px;
    padding: 0 12px;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-primary);
    background: var(--portfolio-title-bg);
    border-bottom: 1px solid var(--border-color);
}

/* --- Content area (scrollable) --- */
.content {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

/* --- Instruments Panel --- */
.instruments-panel {
    display: grid;
    grid-template-columns: 1fr 1fr;
    min-height: 0;
}

/* --- Instrument Column --- */
.instrument-column {
    display: flex;
    flex-direction: column;
    min-width: 0;
    border-right: 1px solid var(--divider-strong);
}

.instrument-column:last-child {
    border-right: none;
}

.column-header {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 24px;
    font-size: 11px;
    font-weight: 700;
    color: var(--section-header-color);
    background: var(--section-header-bg);
    border-bottom: 1px solid var(--border-color);
}

.column-body {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.column-body::-webkit-scrollbar {
    width: 3px;
}

.column-body::-webkit-scrollbar-thumb {
    background: var(--scrollbar-thumb);
    border-radius: 1.5px;
}

/* --- Instrument Card --- */
.instrument-card {
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-secondary);
}

.instrument-title {
    padding: 6px 8px 0 8px;
}

.instrument-title-line1 {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
}

.instrument-ticker {
    font-size: 12px;
    font-weight: 700;
    color: var(--color-accent);
}

.instrument-currency {
    font-size: 10px;
    color: var(--text-secondary);
}

.instrument-title-line2 {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    margin-top: 1px;
}

.instrument-name {
    font-size: 10px;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    margin-right: 8px;
}

.instrument-last-price {
    font-size: 10px;
    color: var(--text-secondary);
    white-space: nowrap;
    flex-shrink: 0;
}

/* --- Chart area --- */
.instrument-chart {
    padding: 4px 6px;
}

.instrument-chart canvas {
    width: 100%;
    display: block;
    border-radius: 3px;
    background: var(--bg-chart);
}

/* --- Performance row --- */
.instrument-perf {
    display: flex;
    gap: 8px;
    padding: 2px 8px 6px 8px;
    font-size: 10px;
}

.perf-value {
    white-space: nowrap;
}

/* --- PnL row (position-only: Avg Cost / Total PnL / Rlzd PnL / Unrlzd PnL) ---
   Layout mirrors InvMon's footerPanel: four equal columns spanning the card width,
   each with a small label above the value. */
.instrument-pnl {
    display: flex;
    align-items: flex-start;
    padding: 4px 8px 6px 8px;
    gap: 6px;
}

.pnl-value {
    flex: 1 1 0;
    min-width: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
}

.pnl-label {
    font-size: 9px;
    color: var(--text-muted);
    line-height: 1.1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
}

.pnl-number {
    font-size: 11px;
    font-variant-numeric: tabular-nums;
    margin-top: 1px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
}

.perf-positive {
    color: var(--color-positive);
}

.perf-negative {
    color: var(--color-negative);
}

.perf-neutral {
    color: var(--text-muted);
}

/* --- Settings Overlay --- */
.settings-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: var(--bg-overlay);
    z-index: 100;
    align-items: flex-end;
    justify-content: center;
}

.settings-overlay.open {
    display: flex;
}

.settings-panel {
    width: 100%;
    max-width: 480px;
    max-height: 85vh;
    overflow-y: auto;
    background: var(--settings-bg);
    border-radius: 16px 16px 0 0;
    padding: 20px 16px 32px 16px;
}

.settings-handle {
    width: 36px;
    height: 4px;
    background: var(--text-muted);
    border-radius: 2px;
    margin: 0 auto 16px auto;
}

.settings-title {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 20px;
    color: var(--text-primary);
}

.settings-group {
    margin-bottom: 16px;
}

.settings-label {
    display: block;
    font-size: 12px;
    color: var(--text-secondary);
    margin-bottom: 6px;
}

.settings-input {
    width: 100%;
    padding: 8px 10px;
    font-size: 14px;
    background: var(--settings-input-bg);
    color: var(--text-primary);
    border: 1px solid var(--settings-input-border);
    border-radius: 8px;
    outline: none;
}

.settings-input:focus {
    border-color: var(--color-accent);
}

.settings-select {
    width: 100%;
    padding: 8px 10px;
    font-size: 14px;
    background: var(--settings-input-bg);
    color: var(--text-primary);
    border: 1px solid var(--settings-input-border);
    border-radius: 8px;
    outline: none;
    -webkit-appearance: none;
    appearance: none;
}

.settings-toggle {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.settings-toggle label {
    font-size: 13px;
    color: var(--text-primary);
    cursor: pointer;
}

.toggle-switch {
    position: relative;
    width: 44px;
    height: 24px;
    flex-shrink: 0;
}

.toggle-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.toggle-slider {
    position: absolute;
    inset: 0;
    background: var(--settings-input-border);
    border-radius: 12px;
    cursor: pointer;
    transition: background 0.2s;
}

.toggle-slider::before {
    content: '';
    position: absolute;
    width: 18px;
    height: 18px;
    left: 3px;
    top: 3px;
    background: white;
    border-radius: 50%;
    transition: transform 0.2s;
}

.toggle-switch input:checked + .toggle-slider {
    background: var(--color-accent);
}

.toggle-switch input:checked + .toggle-slider::before {
    transform: translateX(20px);
}

/* --- Loading / Error states --- */
.loading-indicator {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 32px;
    color: var(--text-muted);
    font-size: 13px;
}

.error-message {
    padding: 32px 16px;
    text-align: center;
    color: var(--color-negative);
    font-size: 13px;
}

/* ============================================================
   Responsive breakpoints
   ============================================================ */

/* --- Two/three panel visibility --- */
/* Narrow (default): show combined column, hide separate cand+watch */
.instruments-panel {
    grid-template-columns: 1fr 1fr;
}

.instrument-column[data-type="candidate"]:not(.combined-column),
.instrument-column[data-type="watchlist"] {
    display: none;
}

.combined-column {
    display: flex;
}

/* Wide (landscape / tablet / desktop): three-panel + tab switcher */
@media (min-width: 640px) {
    .instruments-panel {
        grid-template-columns: 1fr 1fr 1fr;
    }

    /* Show separate columns, hide combined */
    .instrument-column[data-type="candidate"]:not(.combined-column),
    .instrument-column[data-type="watchlist"] {
        display: flex;
    }

    .combined-column {
        display: none;
    }

    /* Show tabs, hide dropdown */
    .portfolio-tabs {
        display: flex;
    }

    .portfolio-dropdown {
        display: none;
    }

    .portfolio-dropdown-arrow {
        display: none;
    }

    /* Slightly larger text on wider screens */
    .instrument-ticker {
        font-size: 13px;
    }

    .instrument-perf {
        font-size: 11px;
    }

    .settings-panel {
        border-radius: 16px;
        margin-bottom: 20px;
    }

    .settings-overlay {
        align-items: center;
    }
}
