:root {
    --tv-bg: #ffffff;
    --tv-text: #131722;
    --tv-muted: #6a6d78;
    --tv-border: #e0e3eb;
    --tv-hover: #f0f3fa;
    --tv-active: #e7efff;
    --tv-control-bg: #ffffff;
    --tv-elevated-bg: rgba(255, 255, 255, 0.98);
    --tv-shadow: rgba(19, 23, 34, 0.14);
    --tv-blue: #2962ff;
    --tv-green: #089981;
    --tv-red: #f23645;
    --tv-alarm: #f59e0b;
}

[data-theme="dark"] {
    --tv-bg: #0f131a;
    --tv-text: #d7e3f5;
    --tv-muted: #8d9aad;
    --tv-border: #2b3340;
    --tv-hover: #1b2330;
    --tv-active: #16325f;
    --tv-control-bg: #151b24;
    --tv-elevated-bg: rgba(21, 27, 36, 0.98);
    --tv-shadow: rgba(0, 0, 0, 0.36);
    --tv-blue: #5b8cff;
    --tv-green: #22ab94;
    --tv-red: #f7525f;
    --tv-alarm: #fbbf24;
    color-scheme: dark;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    height: var(--app-height, 100vh);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--tv-bg);
    color: var(--tv-text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
    user-select: none;
}

#controls {
    position: relative;
    flex: 0 0 44px;
    height: 44px;
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 0 8px;
    background: var(--tv-bg);
    border-bottom: 1px solid var(--tv-border);
    overflow-x: auto;
    overflow-y: hidden;
    white-space: nowrap;
    scrollbar-width: none;
}

#controls::-webkit-scrollbar {
    display: none;
}

.symbol-block {
    display: flex;
    align-items: baseline;
    gap: 8px;
    min-width: fit-content;
    padding: 0 6px;
}

.symbol-name {
    font-size: 14px;
    font-weight: 700;
    letter-spacing: 0;
}

.symbol-meta {
    color: var(--tv-muted);
    font-size: 12px;
}

.toolbar-divider {
    width: 1px;
    height: 22px;
    background: var(--tv-border);
    margin: 0 2px;
}

/* Interval, scale and MA: what the chart shows. */
#quick-controls {
    flex: none;
    display: flex;
    align-items: center;
    gap: 4px;
}

#interval-button {
    width: auto !important;
    padding: 0 8px !important;
    font-size: 13px;
    font-weight: 600;
}

#scale-button svg {
    fill: none;
    stroke: currentColor;
    stroke-width: 1.6;
    stroke-linecap: round;
    stroke-linejoin: round;
}

#scale-button .scale-axis {
    stroke-width: 1.2;
    opacity: 0.55;
}

#scale-button .scale-log,
#scale-button.log .scale-linear {
    display: none;
}

#scale-button.log .scale-log {
    display: inline;
}

.tools-divider {
    flex: 0 0 1px;
    margin: 0 10px;
}

select,
input,
button {
    height: 28px;
    border: 1px solid var(--tv-border);
    border-radius: 4px;
    background: var(--tv-control-bg);
    color: var(--tv-text);
    font: inherit;
    font-size: 13px;
    outline: none;
}

select,
input {
    padding: 0 8px;
}

button {
    padding: 0 10px;
    cursor: pointer;
}

select:hover,
input:hover,
button:hover {
    background: var(--tv-hover);
}

select:focus,
input:focus,
button:focus-visible {
    border-color: var(--tv-blue);
    box-shadow: 0 0 0 2px rgba(41, 98, 255, 0.16);
}

#interval-menu {
    position: fixed;
    z-index: 60;
    width: 216px;
    max-height: min(70vh, 520px);
    overflow-y: auto;
    padding: 6px;
    background: var(--tv-elevated-bg);
    border: 1px solid var(--tv-border);
    border-radius: 8px;
    box-shadow: 0 12px 32px var(--tv-shadow);
    font-size: 13px;
}

#interval-menu[hidden],
#interval-menu button[hidden],
#interval-custom[hidden] {
    display: none;
}

#interval-menu button {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    height: 30px;
    padding: 0 10px;
    border: 0;
    border-radius: 5px;
    background: transparent;
    color: var(--tv-text);
    text-align: left;
}

#interval-menu button:hover {
    background: var(--tv-hover);
}

#interval-menu button.active {
    background: var(--tv-active);
    color: var(--tv-blue);
}

#interval-menu .interval-add {
    font-weight: 600;
}

#interval-menu .interval-group {
    margin: 6px 0 0;
    padding: 8px 10px 4px;
    border-top: 1px solid var(--tv-border);
    color: var(--tv-muted);
    font-size: 11px;
    letter-spacing: 0.06em;
    text-transform: uppercase;
}

#interval-custom {
    display: flex;
    gap: 6px;
    padding: 4px 4px 2px;
}

/* The browser's own up/down arrows are a light system control that matches
   neither theme; the number is typed. */
#interval-custom input {
    width: 52px;
    text-align: center;
    -moz-appearance: textfield;
    appearance: textfield;
}

#interval-custom input::-webkit-inner-spin-button,
#interval-custom input::-webkit-outer-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

#interval-custom select {
    flex: 1;
    min-width: 0;
}

/* Set is the primary button: filled, and brighter on hover, not darker. */
#interval-custom button {
    width: auto;
    height: 28px;
    justify-content: center;
    background: var(--tv-blue);
    color: #fff;
}

#interval-custom button:hover,
#interval-custom button:focus-visible {
    background: var(--tv-blue);
    color: #fff;
    filter: brightness(1.15);
}

/* Sized and hovered like the toolbar buttons (tool-menu.css), at the row's end. */
#controls #theme-toggle {
    flex: none;
    margin-left: 6px;
}

.theme-toggle .theme-icon {
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.theme-toggle .theme-icon-sun {
    display: none;
}

.theme-toggle[data-theme="dark"] .theme-icon-sun {
    display: block;
}

.theme-toggle[data-theme="dark"] .theme-icon-moon {
    display: none;
}

#chart-container {
    width: 100%;
    flex: 1;
    min-height: 0;
    position: relative;
    background: var(--tv-bg);
}

#chart-container::after {
    content: "";
    position: absolute;
    inset: 0;
    pointer-events: none;
    border: 0 solid transparent;
    transition: border-color 120ms ease, box-shadow 120ms ease;
}

#chart-container.line-selected-mode::after {
    border-top-width: 1px;
    border-color: rgba(41, 98, 255, 0.34);
}

#chart-container.line-dragging-mode::after {
    border-top-width: 2px;
    border-color: rgba(41, 98, 255, 0.9);
}

canvas {
    width: 100%;
    height: 100%;
    display: block;
    touch-action: none;
}

#ohlc-info {
    position: absolute;
    left: 10px;
    top: 10px;
    z-index: 12;
    max-width: calc(100% - 168px);
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 3px 7px;
    color: var(--tv-text);
    font-size: 12px;
    font-weight: 600;
    line-height: 1.35;
    pointer-events: none;
}

#ohlc-info[hidden] {
    display: none;
}

.ohlc-label {
    color: var(--tv-text);
}

.ohlc-value.up,
.ohlc-change.up {
    color: var(--tv-green);
}

.ohlc-value.down,
.ohlc-change.down {
    color: var(--tv-red);
}

#ohlc-info.mobile-box {
    top: 6px;
    width: max-content;
    max-width: none;
    display: flex;
    flex-wrap: nowrap;
    gap: 2px 5px;
    padding: 5px 8px;
    background: var(--tv-elevated-bg);
    border: 1px solid var(--tv-border);
    border-radius: 4px;
    box-shadow: 0 6px 16px var(--tv-shadow);
    white-space: nowrap;
    transform: translateX(-50%);
}

#ohlc-info.mobile-box span {
    flex: 0 0 auto;
    white-space: nowrap;
}

@media (max-width: 720px) {
    /* Small and tucked against the top edge: a 12px bold box held under a finger
       hid a good slice of the chart. */
    #ohlc-info,
    #ohlc-info.mobile-box {
        top: 6px;
        width: max-content;
        max-width: none;
        gap: 0 4px;
        padding: 2px 6px;
        font-size: 10px;
        line-height: 1.3;
        background: var(--tv-elevated-bg);
        border: 1px solid var(--tv-border);
        border-radius: 4px;
        box-shadow: 0 4px 12px var(--tv-shadow);
        white-space: nowrap;
        flex-wrap: nowrap;
        transform: translateX(-50%);
    }

    #ohlc-info.mobile-box {
        display: flex;
        flex-wrap: nowrap;
    }

    #ohlc-info.mobile-box span {
        flex: 0 0 auto;
        white-space: nowrap;
    }

    #controls {
        flex: 0 0 42px;
        height: 42px;
        padding: 0 6px;
        gap: 4px;
        scrollbar-width: none;
    }

    #controls::-webkit-scrollbar {
        display: none;
    }

    .symbol-block {
        gap: 0;
        padding: 0 4px;
    }

    .symbol-meta {
        display: none;
    }

    .toolbar-divider {
        margin: 0;
    }

    .tools-divider {
        margin: 0 6px;
    }
}

/* Phones: the drawing tools wrap onto a row of their own (tool-menu.css); the
   theme switch stays at the end of the first row. */
@media (max-width: 579px) {
    #controls {
        flex: 0 0 auto;
        height: auto;
        flex-wrap: wrap;
        row-gap: 4px;
        padding: 6px;
        overflow: visible;
    }

    .tools-divider {
        display: none;
    }

    #controls #theme-toggle {
        margin-left: auto;
    }
}

@media (max-width: 390px) {
    #controls {
        gap: 3px;
        padding: 6px 5px;
    }

    .symbol-block {
        padding: 0 3px;
    }
}
