: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;
}

[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;
    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;
}

.control-row {
    display: flex;
    align-items: center;
    gap: 5px;
    min-width: fit-content;
    color: var(--tv-muted);
    font-size: 11px;
}

.control-row label {
    white-space: nowrap;
}

.control-chip {
    height: 32px;
    padding: 0 4px 0 8px;
    border: 1px solid transparent;
    border-radius: 6px;
}

.control-chip:hover {
    border-color: var(--tv-border);
    background: var(--tv-control-bg);
}

.interval-control {
    gap: 6px;
}

.control-title {
    color: var(--tv-text);
    font-weight: 600;
}

.custom-label {
    margin-left: 1px;
}

.inline-divider {
    width: 1px;
    height: 20px;
    margin: 0 3px;
    background: var(--tv-border);
}

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);
}

#custom-interval-value {
    width: 48px;
}

#interval-select {
    width: 68px;
}

#custom-interval-unit {
    width: 86px;
}

#custom-interval-apply {
    min-width: 38px;
    padding: 0 8px;
}

#scale-select {
    width: 124px;
}

.theme-toggle {
    position: absolute;
    top: 50%;
    right: 10px;
    transform: translateY(-50%);
    z-index: 20;
    display: grid;
    place-items: center;
    width: 30px;
    min-width: 30px;
    height: 30px;
    padding: 0;
    font-weight: 600;
    border-radius: 999px;
    background: var(--tv-elevated-bg);
    box-shadow: 0 2px 8px var(--tv-shadow);
}

.theme-toggle:hover {
    background: var(--tv-hover);
}

.theme-toggle .theme-icon {
    grid-area: 1 / 1;
    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: 58px;
    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: 58px;
    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;
}

#range-bar {
    position: relative;
    flex: 0 0 42px;
    height: 42px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 0 52px 0 14px;
    background: var(--tv-bg);
    border-top: 1px solid var(--tv-border);
    overflow-x: auto;
    overflow-y: hidden;
    white-space: nowrap;
}

.range-selector {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    gap: 2px;
}

.range-button {
    height: 32px;
    border: 0;
    border-radius: 4px;
    background: transparent;
    color: var(--tv-text);
    cursor: pointer;
    font-size: 15px;
    font-weight: 400;
    line-height: 1;
    padding: 0 10px;
}

.range-button:hover {
    background: var(--tv-hover);
}

.range-button.active {
    background: transparent;
    color: var(--tv-text);
    font-weight: 600;
}

#range-status {
    flex: 0 0 auto;
    min-width: 120px;
    color: var(--tv-muted);
    font-size: 12px;
    text-align: right;
}

@media (max-width: 720px) {
    #ohlc-info {
        top: 58px;
        width: max-content;
        max-width: none;
        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;
        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,
    #range-bar::-webkit-scrollbar {
        display: none;
    }

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

    .symbol-meta,
    #range-status,
    .custom-label {
        display: none;
    }

    .toolbar-divider {
        margin: 0;
    }

    .control-chip {
        height: 30px;
        padding: 0 3px 0 6px;
    }

    .inline-divider {
        margin: 0 1px;
    }

    #interval-select {
        width: 64px;
    }

    #custom-interval-value {
        width: 44px;
    }

    #custom-interval-unit {
        width: 74px;
    }

    #custom-interval-apply {
        min-width: 34px;
        padding: 0 7px;
    }

    #scale-select {
        width: 106px;
    }

    #range-bar {
        flex: 0 0 40px;
        height: 40px;
        padding: 0 46px 0 8px;
        gap: 8px;
        scrollbar-width: none;
    }

    .range-button {
        height: 30px;
        padding: 0 8px;
        font-size: 14px;
    }

    .theme-toggle {
        right: 8px;
    }
}

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

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

    .control-title {
        display: none;
    }

    .control-chip {
        padding-left: 4px;
    }

    #custom-interval-unit {
        width: 68px;
    }
}
