/**
 * pgInvesting 공통 CSS
 *
 * 모든 페이지에서 공유하는 스타일
 */

/* ============================================================
   CSS 변수 (기본값 - JS에서 테마에 따라 변경)
   ============================================================ */
:root {
    --bg-primary: #131722;
    --bg-secondary: #1e222d;
    --bg-tertiary: #2a2e39;
    --text-primary: #d1d4dc;
    --text-secondary: #787b86;
    --accent: #2962ff;
    --positive: #26a69a;
    --negative: #ef5350;
    --border: #363a45;
    --highlight-volume: #2d4a3e;
    --highlight-leading: #2d3a5e;
    --highlight-both: #4a4a2d;
}

/* ============================================================
   기본 스타일
   ============================================================ */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body {
    max-width: 100vw;
    overflow-x: hidden;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    font-size: 11px;
    line-height: 1.6;
    transition: background 0.3s, color 0.3s;
}

.container {
    padding: 5px;
    max-width: 100%;
}

/* ============================================================
   타이포그래피
   ============================================================ */
h1 {
    margin-bottom: 10px;
    font-size: 16px;
    color: var(--text-primary);
}

h2 {
    margin-bottom: 10px;
    font-size: 14px;
    color: var(--text-primary);
}

h3 {
    margin-bottom: 15px;
    color: var(--text-secondary);
}

/* ============================================================
   네비게이션
   ============================================================ */
nav {
    margin-bottom: 10px;
}

nav a {
    color: var(--text-secondary);
    text-decoration: none;
    margin-right: 10px;
    font-size: 12px;
}

nav a:hover {
    color: var(--text-primary);
}

nav a.active {
    color: var(--accent);
}

/* ============================================================
   테마 선택기
   ============================================================ */
.theme-selector {
    position: absolute;
    top: 10px;
    right: 10px;
    z-index: 50;
}

.theme-selector select {
    padding: 5px 10px;
    background: var(--bg-secondary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 11px;
    cursor: pointer;
}

/* ============================================================
   필터 정보
   ============================================================ */
.filter-info {
    margin-bottom: 10px;
    padding: 8px;
    background: var(--bg-secondary);
    border-radius: 4px;
    font-size: 12px;
}

.filter-info span {
    color: var(--accent);
}

.filter-info .highlight-legend {
    color: #4ade80;
}

.filter-info .highlight-legend-blue {
    color: #60a5fa;
}

.filter-info .highlight-legend-gold {
    color: #fbbf24;
}

.filter-btn {
    padding: 4px 10px;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 4px;
    cursor: pointer;
    font-size: 11px;
}

.filter-btn:hover {
    background: var(--accent);
}

/* ============================================================
   필터 모달
   ============================================================ */
.filter-modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    z-index: 2000;
    overflow-y: auto;
}

.filter-modal.show {
    display: flex;
    align-items: center;
    justify-content: center;
}

.filter-modal-content {
    background: var(--bg-primary);
    padding: 20px;
    border-radius: 8px;
    max-width: 600px;
    width: 90%;
    max-height: 80vh;
    overflow-y: auto;
}

.filter-section {
    margin-bottom: 15px;
}

.filter-section-title {
    font-weight: bold;
    margin-bottom: 8px;
    color: var(--text-primary);
}

.filter-row {
    display: flex;
    gap: 8px;
    align-items: center;
    margin-bottom: 8px;
    flex-wrap: wrap;
}

.filter-row select,
.filter-row input {
    padding: 5px 8px;
    background: var(--bg-secondary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 12px;
}

.filter-row input[type="number"] {
    width: 70px;
}

.filter-row .remove-btn {
    padding: 4px 8px;
    background: var(--negative);
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 11px;
}

.filter-add-btn {
    padding: 5px 12px;
    background: var(--positive);
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 11px;
    margin-top: 5px;
}

.filter-actions {
    display: flex;
    gap: 10px;
    margin-top: 20px;
    justify-content: flex-end;
}

.filter-actions button {
    padding: 8px 16px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
}

.filter-save {
    background: var(--accent);
    color: white;
}

.filter-reset {
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border) !important;
}

.filter-cancel {
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border) !important;
}

.logic-select {
    font-weight: bold;
    color: var(--accent);
}

/* ============================================================
   테이블
   ============================================================ */
.table-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

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

th, td {
    padding: 4px 5px;
    text-align: right;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}

th {
    background: var(--bg-secondary);
    color: var(--text-secondary);
    font-weight: 600;
    position: sticky;
    top: 0;
    z-index: 10;
}

th:first-child,
td:first-child {
    text-align: left;
    position: sticky;
    left: 0;
    background: var(--bg-secondary);
    z-index: 5;
    max-width: 120px;
    border-right: 1px solid var(--border);
}

th:first-child {
    z-index: 15;
}

td:first-child {
    background: var(--bg-primary);
    overflow: visible;
}

tr:hover td:first-child {
    background: var(--bg-tertiary);
}

tr:hover {
    background: var(--bg-tertiary);
}

/* 정렬 */
th.sortable {
    cursor: pointer;
    user-select: none;
}

th.sortable:hover {
    background: var(--bg-tertiary);
}

th.sort-asc::after {
    content: ' ▲';
    font-size: 8px;
}

th.sort-desc::after {
    content: ' ▼';
    font-size: 8px;
}

/* 모달 테이블 */
.modal-table {
    font-size: 10px;
}

.modal-table th,
.modal-table td {
    padding: 3px 4px;
}

/* ============================================================
   색상 클래스
   ============================================================ */
.positive {
    color: var(--positive);
}

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

.highlight {
    background: var(--bg-tertiary);
}

.flag-true {
    color: var(--accent);
}

/* 하이라이트 행 */
.high-volume {
    background: var(--highlight-volume) !important;
}

.high-volume td:first-child {
    background: var(--highlight-volume) !important;
}

.leading-stock {
    background: var(--highlight-leading) !important;
}

.leading-stock td:first-child {
    background: var(--highlight-leading) !important;
}

.both-conditions {
    background: var(--highlight-both) !important;
}

.both-conditions td:first-child {
    background: var(--highlight-both) !important;
}

/* ============================================================
   로딩
   ============================================================ */
.loading {
    text-align: center;
    padding: 40px;
    color: var(--text-secondary);
}

/* ============================================================
   모달
   ============================================================ */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    z-index: 1100;
    overflow-y: auto;
}

/* 차트 모달은 다른 모달 위에 표시 */
#chart-modal {
    z-index: 1200;
}

.modal.show {
    display: block;
}

.modal-content {
    background: var(--bg-primary);
    margin: 10px auto;
    padding: 10px;
    max-width: 95%;
    border-radius: 8px;
    overflow-x: auto;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
}

.modal-header h2 {
    font-size: 16px;
    color: var(--text-primary);
}

.modal-close {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 24px;
    cursor: pointer;
}

.modal-close:hover {
    color: var(--text-primary);
}

/* 페이지 범례 (종목/테마 페이지) */
.legend-box {
    margin-bottom: 10px;
    padding: 8px 12px;
    background: var(--bg-secondary);
    border-radius: 4px;
    font-size: 11px;
    display: flex;
    gap: 15px;
    flex-wrap: wrap;
}

/* 모달 범례 */
.modal-legend {
    margin-bottom: 10px;
    padding: 8px 12px;
    background: var(--bg-secondary);
    border-radius: 4px;
    font-size: 11px;
    display: flex;
    gap: 15px;
    flex-wrap: wrap;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 5px;
}

.legend-color {
    width: 14px;
    height: 14px;
    border-radius: 2px;
    border: 1px solid var(--border);
}

/* ============================================================
   차트
   ============================================================ */
.chart-wrapper {
    position: relative;
}

.chart-section {
    margin-bottom: 4px;
}

.chart-section-label {
    font-size: 11px;
    font-weight: bold;
    color: var(--text-secondary);
    margin-bottom: 4px;
    padding-left: 4px;
    display: flex;
    align-items: center;
    gap: 6px;
}

.chart-info-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: var(--text-secondary);
    color: var(--bg-primary);
    font-size: 9px;
    font-weight: bold;
    cursor: help;
    position: relative;
}

.chart-info-icon:hover .chart-info-tooltip {
    display: block;
}

.chart-info-tooltip {
    display: none;
    position: absolute;
    top: 20px;
    left: 0;
    width: 280px;
    padding: 10px 12px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-size: 11px;
    font-weight: normal;
    color: var(--text-primary);
    line-height: 1.5;
    z-index: 1000;
    box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}

.chart-info-tooltip strong {
    color: var(--accent-color);
}

.chart-section-inner {
    position: relative;
}

#weekly-chart-container {
    width: 100%;
    height: clamp(220px, 37vh, 400px);
}

#chart-container {
    width: 100%;
    height: clamp(220px, 37vh, 400px);
}

.chart-legend {
    position: absolute;
    top: 4px;
    left: 4px;
    right: 100px;
    z-index: 10;
    font-size: 10px;
    font-family: monospace;
    color: var(--text-secondary);
    pointer-events: none;
    line-height: 1.3;
    overflow-wrap: break-word;
    overflow: hidden;
}

.chart-legend .legend-row {
    white-space: normal;
}

@media (max-width: 600px) {
    #weekly-chart-container,
    #chart-container {
        height: clamp(195px, 33vh, 300px);
    }
    .chart-legend {
        font-size: 8px;
    }
    .chart-legend .legend-ohlc,
    .chart-legend .legend-rs-bp {
        display: block;
    }
    .chart-legend .ma-rs {
        display: none;
    }
}

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

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

/* ============================================================
   NXT 뱃지
   ============================================================ */
.nxt-badge {
    font-size: 7px;
    margin-left: 4px;
}

.nxt-tradeable {
    color: var(--positive);
}

.nxt-restricted {
    color: var(--text-secondary);
}

/* ============================================================
   테마명/종목명 셀
   ============================================================ */
.theme-link,
.stock-link {
    color: var(--text-primary);
    cursor: pointer;
    text-decoration: none;
    padding: 2px 4px;
    border-radius: 3px;
    transition: background 0.2s, color 0.2s;
}

.theme-link:hover,
.stock-link:hover {
    background: var(--accent);
    color: #fff;
}

.theme-cell,
.stock-cell {
    display: flex;
    align-items: center;
    gap: 4px;
}

.theme-name,
.stock-name {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.stock-name-clickable {
    cursor: pointer;
    transition: color 0.2s;
}

.stock-name-clickable:hover {
    color: var(--accent);
}

.chart-icon {
    cursor: pointer;
    font-size: 12px;
    flex-shrink: 0;
    padding: 2px 4px;
    border-radius: 3px;
    transition: background 0.2s;
}

.chart-icon:hover {
    background: var(--accent);
}

/* ============================================================
   커스텀 툴팁
   ============================================================ */
.custom-tooltip {
    position: fixed;
    background: var(--bg-secondary);
    color: var(--text-primary);
    padding: 8px 12px;
    border-radius: 4px;
    white-space: nowrap;
    z-index: 9999;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.6);
    border: 1px solid var(--border);
    font-size: 12px;
    pointer-events: none;
    display: none;
}

/* ============================================================
   종목 설명 팝업
   ============================================================ */
.stock-popup {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: var(--bg-secondary);
    color: var(--text-primary);
    padding: 20px;
    border-radius: 8px;
    width: min(90%, 80vw);
    min-width: 300px;
    max-width: 1000px;
    z-index: 10000;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.8);
    border: 1px solid var(--accent);
    display: none;
}

.stock-popup.show {
    display: block;
}

.stock-popup-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    z-index: 9999;
    display: none;
}

.stock-popup-overlay.show {
    display: block;
}

.stock-popup-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
    padding-bottom: 10px;
    border-bottom: 1px solid var(--border);
}

.stock-popup-title {
    font-size: 14px;
    font-weight: bold;
    color: var(--accent);
}

.stock-popup-close {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 20px;
    cursor: pointer;
}

.stock-popup-close:hover {
    color: var(--text-primary);
}

.stock-popup-content {
    font-size: 12px;
    line-height: 1.6;
    white-space: pre-wrap;
    word-break: break-word;
}

/* ============================================================
   종목 테마 목록 팝업 (stock-popup 스타일 상속)
   ============================================================ */
.stock-themes-popup {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: var(--bg-secondary);
    color: var(--text-primary);
    padding: 20px;
    border-radius: 8px;
    width: min(90%, 80vw);
    min-width: 300px;
    max-width: 1000px;
    max-height: 80vh;
    z-index: 10000;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.8);
    border: 1px solid var(--accent);
    display: none;
}

.stock-themes-popup.show {
    display: block;
}

.stock-themes-content {
    max-height: 60vh;
    overflow-y: auto;
}

.stock-themes-content .no-themes {
    text-align: center;
    color: var(--text-secondary);
    padding: 30px;
    font-size: 13px;
}

.stock-themes-content .themes-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.stock-themes-content .theme-item {
    background: var(--bg-tertiary);
    border-radius: 6px;
    padding: 12px;
    border-left: 3px solid var(--accent);
}

.stock-themes-content .theme-item-header {
    margin-bottom: 6px;
}

.stock-themes-content .theme-item-name {
    font-size: 13px;
    font-weight: bold;
    color: var(--accent);
}

.stock-themes-content .theme-item-description {
    font-size: 11px;
    line-height: 1.5;
    color: var(--text-secondary);
    white-space: pre-wrap;
    word-break: break-word;
}

/* ============================================================
   고정 헤더
   ============================================================ */
.fixed-header-container {
    position: fixed;
    top: 0;
    left: 5px;
    right: 5px;
    z-index: 100;
    background: var(--bg-primary);
    display: none;
    overflow: hidden;
}

.fixed-header-container.show {
    display: block;
}

.fixed-header-wrapper {
    overflow-x: hidden;
}

.fixed-header-wrapper table {
    border-collapse: collapse;
    width: auto;
    min-width: 100%;
}

.fixed-header-wrapper th {
    padding: 4px 5px;
    text-align: right;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
    background: var(--bg-secondary);
    color: var(--text-secondary);
    font-weight: 600;
}

.fixed-header-wrapper th:first-child {
    text-align: left;
    position: sticky;
    left: 0;
    background: var(--bg-secondary);
    z-index: 15;
    max-width: 120px;
    border-right: 1px solid var(--border);
}

.fixed-header-wrapper th.sortable {
    cursor: pointer;
    user-select: none;
}

.fixed-header-wrapper th.sortable:hover {
    background: var(--bg-tertiary);
}

.fixed-header-wrapper th.sort-asc::after {
    content: ' ▲';
    font-size: 8px;
}

.fixed-header-wrapper th.sort-desc::after {
    content: ' ▼';
    font-size: 8px;
}

/* ============================================================
   유틸리티
   ============================================================ */
.hidden {
    display: none;
}
