/* §12.3 视觉规范(壳层 UI) — dark shell + light chart canvas */

:root {
    --bg: #0f1419;
    --bg-elev: #1a222d;
    --bg-elev-2: #232d3b;
    --border: #2e3a4a;
    --text: #e7ecf3;
    --muted: #8b9bb0;
    --accent: #3d8bfd;
    --accent-dim: #2563b8;
    --ok: #3ecf8e;
    --warn: #f0a030;
    --danger: #e85d5d;
    --radius: 10px;
    --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
    --font:
        "Segoe UI", "PingFang SC", "Noto Sans CJK SC", "Microsoft YaHei",
        sans-serif;
    --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
    /* Chart pane (intentional light mix on dark shell) */
    --chart-bg: #f7f7f7;
    --chart-text: #202124;
}

*,
*::before,
*::after {
    box-sizing: border-box;
}

html,
body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}

body {
    overflow: hidden;
    background: var(--bg);
    color: var(--text);
    font-family: var(--font);
    font-size: 14px;
}

.app {
    position: fixed;
    inset: 0;
    display: flex;
    flex-direction: column;
    padding: 14px 16px 10px;
    gap: 12px;
    min-height: 0;
}

/* ——— Header / brand ——— */

.header {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    gap: 16px;
    flex: 0 0 auto;
}

.brand {
    display: flex;
    gap: 12px;
    align-items: center;
    min-width: 0;
    justify-self: start;
}

/* Centered feature tabs (same row as search / symbol) */
.page-tabs {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px;
    background: var(--bg-elev);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: var(--shadow);
    justify-self: center;
}

.page-tab {
    height: 30px;
    min-width: 56px;
    padding: 0 14px;
    border: 1px solid transparent;
    border-radius: 7px;
    background: transparent;
    color: var(--muted);
    font-weight: 600;
    letter-spacing: 0.04em;
    cursor: pointer;
    transition:
        color 0.15s,
        background 0.15s,
        border-color 0.15s;
}

.page-tab:hover:not(.is-active) {
    color: var(--text);
    background: var(--bg);
}

.page-tab.is-active {
    color: var(--text);
    background: var(--bg-elev-2);
    border-color: var(--accent);
    box-shadow: inset 0 0 0 1px rgba(61, 139, 253, 0.25);
}

.logo {
    width: 44px;
    height: 44px;
    border-radius: 12px;
    background: linear-gradient(135deg, #2b6cb0, #1a365d);
    display: grid;
    place-items: center;
    font-size: 20px;
    box-shadow: var(--shadow);
    flex-shrink: 0;
}

.header h1 {
    margin: 0;
    font-size: 1.25rem;
    font-weight: 650;
    letter-spacing: 0.02em;
    line-height: 1.2;
}

.subtitle {
    margin: 3px 0 0;
    color: var(--muted);
    font-size: 0.82rem;
}

.header-controls {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
    justify-self: end;
}

.control-group {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 8px;
    background: var(--bg-elev);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: var(--shadow);
}

.ctrl-label {
    color: var(--muted);
    font-size: 0.78rem;
    letter-spacing: 0.04em;
    white-space: nowrap;
}

/* ——— Form controls ——— */

select,
input,
button {
    font-family: inherit;
    font-size: 0.82rem;
    color: var(--text);
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 6px;
}

select,
input {
    height: 30px;
    padding: 0 8px;
    min-width: 72px;
    outline: none;
}

select:hover,
input:hover,
button:hover:not(:disabled) {
    border-color: #4a5d75;
}

select:focus,
input:focus {
    border-color: var(--accent);
}

#searchWindowSize {
    width: 56px;
    min-width: 56px;
    text-align: center;
    font-family: var(--mono);
}

#symbolSelect {
    width: 150px;
    min-width: 120px;
}

.icon-btn {
    width: 30px;
    height: 30px;
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: var(--muted);
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    cursor: pointer;
    line-height: 1;
    transition:
        color 0.15s,
        border-color 0.15s,
        background 0.15s;
}

.icon-btn:hover:not(:disabled) {
    color: var(--text);
    border-color: var(--accent);
    background: var(--bg-elev-2);
}

.icon-btn:active:not(:disabled) {
    transform: translateY(1px);
}

.icon-btn:disabled {
    opacity: 0.55;
    cursor: wait;
}

/* ——— Workspace / panels ——— */

/* Two columns: chart + Terminal on the left, AI chat on the right. */
.workspace {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: row;
    gap: 12px;
    position: relative;
}

.workspace-main {
    flex: 1 1 auto;
    min-width: 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.feature-host {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
    position: relative;
}

.feature-page {
    flex: 1 1 auto;
    min-height: 0;
    flex-direction: column;
    overflow: hidden;
}

/* Only the active tab page is shown. !important so panel helpers
   (e.g. .chart-panel { display: flex }) cannot leak the market「图表」
   head into the data/charts tabs. */
.feature-page:not(.is-active),
.feature-page[hidden] {
    display: none !important;
}

.feature-page.is-active {
    display: flex;
}

.panel {
    background: var(--bg-elev);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    min-width: 0;
    min-height: 0;
}

.panel-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    border-bottom: 1px solid var(--border);
    flex: 0 0 auto;
    min-width: 0;
}

.chart-panel > .panel-head {
    gap: 10px;
    justify-content: flex-start;
    /* Fixed single-row height so PA flags show/hide never reflows the chart */
    height: 2.5rem;
    box-sizing: border-box;
    overflow: hidden;
    align-items: center;
}

.chart-panel > .panel-head h2 {
    flex: 0 0 auto;
    line-height: 1.2;
}

/* PA flags sit right after「图表」as rounded pills (same row as OHLC) */
#barFlags {
    display: flex;
    flex: 1 1 auto;
    flex-wrap: nowrap;
    align-items: center;
    gap: 6px;
    min-width: 0;
    height: 1.5rem;
    overflow: hidden;
    pointer-events: none;
    font-size: 0.72rem;
}

.flag {
    flex: 0 0 auto;
    box-sizing: border-box;
    height: 1.5rem;
    padding: 0 8px;
    display: inline-flex;
    align-items: center;
    color: var(--text);
    background: rgba(61, 139, 253, 0.16);
    border: 1px solid rgba(61, 139, 253, 0.35);
    border-radius: 999px;
    font-family: var(--mono);
    font-weight: 500;
    line-height: 1;
    white-space: nowrap;
}

/* OHLC badge: size to content, push to the right edge of the header */
.chart-panel > .panel-head .badge {
    flex: 0 1 auto;
    width: max-content;
    max-width: min(100%, 42rem);
    margin-left: auto;
    text-align: left;
    /* Match flag / head row so presence of either never changes height */
    height: 1.5rem;
    line-height: 1.5rem;
    padding-top: 0;
    padding-bottom: 0;
    box-sizing: border-box;
}

.panel-head h2 {
    margin: 0;
    font-size: 0.92rem;
    font-weight: 600;
    letter-spacing: 0.02em;
}

.badge {
    font-size: 0.72rem;
    color: var(--muted);
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 2px 10px;
    font-family: var(--mono);
    white-space: nowrap;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    font-variant-numeric: tabular-nums;
}

/* Connected status + OHLC legend share this green pill */
.badge.ok {
    color: #0d2b1c;
    background: var(--ok);
    border-color: transparent;
    font-weight: 600;
}

.badge.err {
    color: #fff;
    background: var(--danger);
    border-color: transparent;
    font-weight: 600;
}

.muted-badge {
    color: var(--muted);
    font-weight: 500;
}

/* ——— Chart pane (light canvas on dark shell) ——— */
/* Visibility is owned by .feature-page / .is-active above — do not set
   display here or inactive market page will stack over other tabs. */

.chart-panel {
    flex: 1 1 auto;
    flex-direction: column;
    overflow: hidden;
}

/* Market page body: the chart. */
.market-body {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    position: relative;
}

.chat-panel {
    flex: 0 0 var(--chat-width, 380px);
    width: var(--chat-width, 380px);
    min-width: 300px;
    max-width: 600px;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    background: var(--bg-elev);
    color: var(--text);
}

.chat-panel[hidden] {
    display: none;
}

#chat-root {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.chat-panel.is-collapsed {
    flex: 0 0 auto;
    width: auto;
    min-width: 0;
    border: 0;
    box-shadow: none;
    overflow: visible;
    background: transparent;
    position: absolute;
    right: 12px;
    bottom: 12px;
    z-index: 30;
}

/* Narrow viewports: bottom drawer over the chart instead of squeezing it. */
@media (max-width: 900px) {
    .chat-panel:not(.is-collapsed) {
        position: absolute;
        left: 0;
        right: 0;
        bottom: 0;
        width: auto;
        max-width: none;
        min-width: 0;
        height: min(70vh, 560px);
        z-index: 30;
    }
}

.chart-wrap {
    position: relative;
    flex: 1 1 auto;
    min-width: 0;
    min-height: 180px;
    overflow: hidden;
    background: var(--chart-bg);
    border-radius: 0 0 calc(var(--radius) - 1px) calc(var(--radius) - 1px);
}

#container {
    width: 100%;
    height: 100%;
    display: block;
}

/* ENV floating card — chart canvas top-left (draggable) */
#env {
    position: absolute;
    z-index: 6;
    top: 10px;
    left: 10px;
    right: auto;
    min-width: 168px;
    padding: 10px 12px;
    background: rgba(26, 34, 45, 0.96);
    color: var(--text);
    font-family: var(--mono);
    font-size: 0.78rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: var(--shadow);
    cursor: move;
    user-select: none;
}

#env:empty {
    display: none;
    padding: 0;
    border: none;
    min-width: 0;
}

.env-row {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 16px;
    line-height: 1.65;
}

.env-key {
    color: var(--muted);
    letter-spacing: 0.4px;
}

.env-val {
    color: var(--accent);
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

/* ——— Terminal (under the chart, left column) ——— */

.bottom {
    flex: 0 0 168px;
    display: flex;
    gap: 12px;
    min-height: 140px;
    max-height: 40vh;
}

.log-panel {
    flex: 1 1 50%;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.log {
    flex: 1 1 auto;
    overflow-y: auto;
    padding: 8px 12px;
    color: var(--text);
    white-space: pre-wrap;
    word-break: break-word;
    line-height: 1.45;
    font-family: var(--mono);
    font-size: 0.78rem;
    background: var(--bg);
    min-height: 0;
}

.log-time {
    color: var(--muted);
    margin-right: 8px;
}

.term-cmd {
    color: var(--accent);
}

.term-err {
    color: var(--danger);
}

#termInput {
    flex: 0 0 auto;
    width: 100%;
    height: 34px;
    padding: 8px 12px;
    color: var(--accent);
    background: var(--bg-elev-2);
    border: none;
    border-top: 1px solid var(--border);
    border-radius: 0 0 calc(var(--radius) - 1px) calc(var(--radius) - 1px);
    outline: none;
    font-family: var(--mono);
    font-size: 0.82rem;
}

#termInput::placeholder {
    color: var(--muted);
    opacity: 0.85;
}

#termInput:focus {
    background: #1e2836;
}

/* ——— Data page ——— */
/* Single panel-head (「数据」) replaces market's 「图表」 head in the same slot.
   Inactive #page-market is fully hidden (see .feature-page:not(.is-active)). */

.data-panel > .panel-head.data-toolbar {
    /* Match chart-panel head height so layout doesn't jump on tab switch */
    height: 2.5rem;
    box-sizing: border-box;
    gap: 10px;
    justify-content: flex-start;
    overflow: hidden;
    align-items: center;
}

.data-panel > .panel-head.data-toolbar h2 {
    flex: 0 0 auto;
    line-height: 1.2;
}

.data-tools {
    display: flex;
    align-items: center;
    gap: 4px;
    flex: 0 0 auto;
}

.data-panel > .panel-head .badge {
    margin-left: auto;
    flex: 0 1 auto;
    max-width: min(100%, 28rem);
    height: 1.5rem;
    line-height: 1.5rem;
    padding-top: 0;
    padding-bottom: 0;
    box-sizing: border-box;
}

.data-split {
    flex: 1 1 auto;
    min-height: 0;
    display: grid;
    grid-template-columns: minmax(0, 2fr) minmax(0, 1fr);
    gap: 0;
    border-top: none;
}

.data-chart-pane {
    position: relative;
    min-width: 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--border);
    background: var(--chart-bg);
    overflow: hidden;
}

/* Bottom-center: selection actions (copy / snippet / clear) */
.data-selection-bar {
    position: absolute;
    z-index: 8;
    left: 50%;
    bottom: 12px;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 4px;
    background: rgba(26, 34, 45, 0.94);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: var(--shadow);
}

.data-selection-bar[hidden] {
    display: none !important;
}

.icon-tool-btn {
    width: 28px;
    height: 28px;
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: var(--muted);
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    cursor: pointer;
    line-height: 1;
    transition:
        color 0.15s,
        border-color 0.15s,
        background 0.15s;
}

.icon-tool-btn:hover:not(:disabled) {
    color: var(--text);
    background: var(--bg-elev-2);
    border-color: var(--accent);
}

.icon-tool-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.icon-tool-btn.is-active,
.icon-tool-btn[aria-pressed="true"] {
    color: #fff;
    background: var(--accent);
    border-color: var(--accent-dim);
}

.icon-tool-btn:active:not(:disabled) {
    transform: translateY(1px);
}

/* Selection bar sits on light chart canvas — invert chrome slightly */
.data-selection-bar .icon-tool-btn {
    background: transparent;
    border-color: transparent;
    color: var(--muted);
}

.data-selection-bar .icon-tool-btn:hover:not(:disabled) {
    color: var(--text);
    background: var(--bg-elev-2);
    border-color: var(--border);
}

.data-chart-host {
    position: relative;
    flex: 1 1 auto;
    width: 100%;
    height: 100%;
    min-height: 180px;
}

.data-selection-band {
    position: absolute;
    z-index: 3;
    top: 0;
    bottom: 25px;
    left: 0;
    width: 0;
    pointer-events: none;
    background: rgba(90, 98, 110, 0.28);
}

.data-selection-band.is-strong {
    background: rgba(90, 98, 110, 0.38);
}

.data-selection-band[hidden] {
    display: none !important;
}

.data-chart-host.pick-mode {
    cursor: cell;
}

.data-table-pane {
    min-width: 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
    background: var(--bg);
    overflow: hidden;
}

.data-table-scroll {
    flex: 1 1 auto;
    overflow: auto;
    min-height: 0;
}

.data-table {
    width: 100%;
    border-collapse: collapse;
    font-family: var(--mono);
    font-size: 0.75rem;
    font-variant-numeric: tabular-nums;
}

.data-table thead th {
    position: sticky;
    top: 0;
    z-index: 1;
    background: var(--bg-elev-2);
    color: var(--muted);
    font-weight: 600;
    text-align: right;
    padding: 8px 10px;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}

.data-table thead th:first-child {
    text-align: left;
}

.data-table tbody td {
    padding: 5px 10px;
    text-align: right;
    border-bottom: 1px solid rgba(46, 58, 74, 0.55);
    color: var(--text);
    white-space: nowrap;
}

.data-table tbody td:first-child {
    text-align: left;
    color: var(--muted);
}

.data-table tbody tr {
    cursor: default;
}

.data-table tbody tr.is-selected {
    background: rgba(120, 132, 150, 0.55);
    box-shadow: inset 3px 0 0 0 #a8b0bc;
}

.data-table tbody tr.is-selected td {
    color: #f1f3f5;
    border-bottom-color: rgba(168, 176, 188, 0.35);
}

.data-table tbody tr.is-selected td:first-child {
    color: #dce1e8;
}

.data-table tbody tr.is-anchor {
    background: rgba(140, 152, 170, 0.72);
    box-shadow: inset 3px 0 0 0 #d0d6de;
}

.data-table tbody tr.is-anchor td {
    font-weight: 600;
}

.data-table tbody tr:hover:not(.is-selected) {
    background: rgba(255, 255, 255, 0.03);
}

.data-panel.pick-mode .data-table tbody tr {
    cursor: cell;
}

/* ——— Placeholder page ——— */

.placeholder-page {
    align-items: center;
    justify-content: center;
}

.placeholder-body {
    text-align: center;
    color: var(--muted);
    padding: 32px;
}

.placeholder-body h2 {
    margin: 0 0 8px;
    color: var(--text);
    font-size: 1.1rem;
}

.placeholder-body p {
    margin: 0;
    font-size: 0.9rem;
}

/* ——— Modal (snippet chart) ——— */

.modal {
    position: fixed;
    inset: 0;
    z-index: 40;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
}

.modal[hidden] {
    display: none !important;
}

.modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
}

.modal-dialog {
    position: relative;
    z-index: 1;
    width: min(920px, 100%);
    max-height: min(90vh, 640px);
    display: flex;
    flex-direction: column;
    background: var(--bg-elev);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden;
}

.modal-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
    flex: 0 0 auto;
}

.modal-head h3 {
    margin: 0;
    font-size: 0.95rem;
    font-weight: 600;
}

.modal-actions {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    flex: 0 0 auto;
}

.modal-action-btn {
    width: auto;
    min-width: 30px;
    padding: 0 8px;
    gap: 4px;
}

.modal-close {
    font-size: 1.1rem;
    line-height: 1;
}

.modal-body {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
    padding: 12px;
    gap: 8px;
    background: var(--bg);
    overflow: auto;
}

.snippet-chart-host {
    flex: 0 0 auto;
    min-width: 40px;
    height: 320px;
    background: var(--chart-bg);
    border-radius: 8px;
    border: 1px solid var(--border);
    overflow: hidden;
}

.snippet-meta {
    margin: 0;
    color: var(--muted);
    font-family: var(--mono);
    font-size: 0.78rem;
}

/* ——— Responsive ——— */

@media (max-width: 820px) {
    .app {
        padding: 10px;
        gap: 10px;
    }

    .header {
        grid-template-columns: 1fr;
        justify-items: stretch;
    }

    .brand,
    .page-tabs,
    .header-controls {
        justify-self: stretch;
    }

    .page-tabs {
        justify-content: center;
    }

    .header-controls {
        justify-content: flex-start;
    }

    .data-split {
        grid-template-columns: 1fr;
        grid-template-rows: minmax(200px, 45%) minmax(160px, 1fr);
    }

    .data-chart-pane {
        border-right: none;
        border-bottom: 1px solid var(--border);
    }

    .bottom {
        flex-direction: column;
        flex: 0 0 auto;
        max-height: none;
        height: auto;
    }

    .log-panel {
        flex: 0 0 120px;
    }

    #barFlags,
    .chart-wrap:has(#env:not(:empty)) #barFlags {
        top: auto;
        left: 10px;
        bottom: 10px;
        max-width: calc(100% - 20px);
    }

    #env {
        top: 10px;
        left: 10px;
        right: auto;
        bottom: auto;
    }
}
