:root {
    --blue: #234d20;
    --dark: #1F2A37;
    --gray: #6B7280;
    --border: #E5E7EB;
    --bg: #F7F9FC;
    --green: #1B8A5A;
    --red: #C0392B;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: -apple-system, "Malgun Gothic", "Apple SD Gothic Neo", Arial, sans-serif;
    background: var(--bg);
    color: var(--dark);
}

.site-header {
    background: linear-gradient(135deg, #14300f, #234d20);
    color: #fff;
    padding: 28px 24px;
}

.site-header h1 {
    margin: 4px 0;
    font-size: 26px;
}

.site-header .subtitle {
    margin: 0;
    opacity: 0.9;
    font-size: 14px;
}

.back-link {
    color: #d7ecd3;
    text-decoration: none;
    font-size: 13px;
}

.ticker-tag {
    font-size: 15px;
    background: rgba(255,255,255,0.2);
    padding: 3px 10px;
    border-radius: 6px;
    margin-left: 8px;
}

.container {
    max-width: 1760px;
    margin: 0 auto;
    padding: 24px;
}

.card {
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 20px;
    margin-bottom: 20px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.04);
}

.card h2 {
    margin-top: 0;
    font-size: 17px;
    color: var(--dark);
}

.filter-bar {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    align-items: center;
    margin-bottom: 14px;
    font-size: 14px;
}

.filter-item {
    display: flex;
    align-items: center;
    gap: 6px;
    white-space: nowrap;
}

.filter-bar select {
    padding: 6px 10px;
    border-radius: 6px;
    border: 1px solid var(--border);
}

.filter-bar button {
    padding: 6px 12px;
    border-radius: 6px;
    border: 1px solid var(--blue);
    background: #fff;
    color: var(--blue);
    cursor: pointer;
    font-size: 13px;
}
.filter-bar button:hover { background: #EAF3E7; }

.table-wrap { overflow-x: auto; cursor: grab; }
.table-wrap.dragging { cursor: grabbing; user-select: none; }

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13.5px;
}

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

th:nth-child(1), th:nth-child(2), th:nth-child(3), th:nth-child(4),
td:nth-child(1), td:nth-child(2), td:nth-child(3), td:nth-child(4) {
    text-align: left;
}

thead th {
    color: var(--gray);
    font-weight: 600;
    font-size: 12.5px;
    border-bottom: 2px solid var(--border);
}
thead th.sortable {
    cursor: pointer;
    user-select: none;
    white-space: nowrap;
}
thead th.sortable:hover { color: var(--dark); }
thead th.sortable::after { content: '\21C5'; margin-left: 4px; opacity: 0.35; font-size: 11px; }
thead th.sortable.sort-desc::after { content: '\25BC'; opacity: 1; color: var(--blue); }
thead th.sortable.sort-asc::after { content: '\25B2'; opacity: 1; color: var(--blue); }

.th-unit {
    display: block;
    font-weight: 400;
    font-size: 10.5px;
    color: var(--gray);
    margin-top: 2px;
}

tbody tr:hover { background: #EFF5EC; }

/* 표 접기: 부수적인 컬럼 숨기고 종목명 폭 제한 + 표 자체도 줄어든 만큼 좁아지게.
   %가 아니라 고정 px로 캡을 걸어야 표 전체 너비가 실제로 줄어든다
   (% 폭은 table-layout:auto에서 오히려 표를 넓히는 역효과가 남). */
#etfTable.compact { width: auto; }
#etfTable.compact .col-secondary { display: none; }
#etfTable.compact td.etf-name { max-width: 200px; }
#etfTable.compact td.etf-name .name-text {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.etf-name { display: flex; flex-direction: column; }
.etf-name .issuer { font-size: 11.5px; color: var(--gray); }

.positive { color: var(--green); }
.negative { color: var(--red); }
.muted { color: var(--gray); }
.strong { font-weight: 700; }

.badge {
    display: inline-block;
    padding: 2px 9px;
    border-radius: 999px;
    font-size: 11.5px;
    color: #fff;
}
.badge-blue   { background: #234d20; }
.badge-purple { background: #3f6b2f; }
.badge-orange { background: #7a8f3d; }
.badge-teal   { background: #1f6f5c; }
.badge-pink   { background: #588157; }
.badge-gray   { background: #9AA4B2; }

.disclaimer {
    font-size: 12px;
    color: var(--gray);
    line-height: 1.6;
}

/* 연금저축/IRP/ISA 가이드 페이지 */
.guide-list {
    margin: 0 0 16px;
    padding-left: 20px;
    font-size: 13.5px;
    line-height: 1.7;
    color: var(--dark);
}
.guide-list li { margin-bottom: 6px; }
.guide-list-warning li::marker { color: var(--red); }

.screener-controls {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
    margin-bottom: 16px;
}
.screener-field {
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.screener-field label { font-size: 12.5px; color: var(--gray); font-weight: 600; }
.screener-field select {
    padding: 7px 10px;
    border-radius: 6px;
    border: 1px solid var(--border);
    font-size: 14px;
}
.screener-range { display: flex; align-items: center; gap: 6px; }
.screener-range input {
    width: 90px;
    padding: 7px 8px;
    border-radius: 6px;
    border: 1px solid var(--border);
    font-size: 14px;
    text-align: right;
}
.screener-range span { color: var(--gray); }

#runScreenerBtn {
    padding: 9px 18px;
    border-radius: 6px;
    border: 1px solid var(--blue);
    background: var(--blue);
    color: #fff;
    cursor: pointer;
    font-size: 14px;
    font-weight: 600;
}
#runScreenerBtn:disabled { opacity: 0.5; cursor: not-allowed; }
#sendToPortfolioBtn:disabled { opacity: 0.5; cursor: not-allowed; }

.metric-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 12px;
}

.metric {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 10px 12px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}
.metric.highlight { border-color: var(--blue); background: #EAF3E7; }
.metric .label { font-size: 12px; color: var(--gray); }
.metric .value { font-size: 17px; font-weight: 700; }
.metric .value.small { font-size: 13px; font-weight: 500; }

.chart-controls { margin-bottom: 10px; }
.chart-controls button {
    border: 1px solid var(--border);
    background: #fff;
    padding: 5px 12px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
    margin-right: 6px;
}
.chart-controls button.active {
    background: var(--blue);
    color: #fff;
    border-color: var(--blue);
}

.current-row { background: #FFF6E5; font-weight: 700; }

.glossary {
    display: grid;
    grid-template-columns: 130px 1fr;
    gap: 6px 16px;
    font-size: 13.5px;
    margin: 0;
}
.glossary dt { font-weight: 700; color: var(--dark); }
.glossary dd { margin: 0; color: var(--gray); }
.term-unit {
    display: block;
    font-weight: 400;
    font-size: 11.5px;
    color: var(--gray);
    margin-top: 1px;
}

.search-bar {
    display: flex;
    gap: 8px;
    align-items: center;
    margin-bottom: 14px;
}
.search-bar input[type="text"] {
    padding: 7px 10px;
    border-radius: 6px;
    border: 1px solid var(--border);
    font-size: 14px;
    width: 200px;
}
.search-bar button {
    padding: 7px 14px;
    border-radius: 6px;
    border: 1px solid var(--blue);
    background: var(--blue);
    color: #fff;
    cursor: pointer;
    font-size: 14px;
}
.search-bar button:disabled { opacity: 0.6; cursor: not-allowed; }
/* 신규 종목 검색(전체 ETF 유니버스 대상) */
.universe-picker {
    position: relative;
    flex: 1;
    max-width: 420px;
}
.universe-picker input[type="text"] {
    width: 100%;
    padding: 7px 10px;
    border-radius: 6px;
    border: 1px solid var(--border);
    font-size: 14px;
}
.universe-results {
    display: none;
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    right: 0;
    max-height: 320px;
    overflow-y: auto;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.08);
    z-index: 20;
}
.universe-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 8px 10px;
    font-size: 13.5px;
    border-bottom: 1px solid var(--border);
}
.universe-item:last-child { border-bottom: none; }
.universe-item-label {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.universe-item-action { flex-shrink: 0; }
.universe-empty { padding: 8px 10px; font-size: 13px; color: var(--gray); }
.universe-action-link { color: var(--blue); font-size: 12.5px; text-decoration: none; font-weight: 600; }
.universe-action-pending { color: var(--gray); font-size: 12.5px; }
.universe-add-btn {
    border: 1px solid var(--blue);
    background: #fff;
    color: var(--blue);
    padding: 4px 10px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 12.5px;
}
.universe-add-btn:disabled { opacity: 0.5; cursor: not-allowed; }

.search-result { font-size: 13px; margin-top: 6px; }
.search-result.ok { color: var(--green); }
.search-result.error { color: var(--red); }

.portfolio-row {
    display: flex;
    gap: 8px;
    align-items: center;
    margin-bottom: 8px;
}
.portfolio-row select.portfolio-etf {
    flex: 1;
    max-width: 420px;
    padding: 7px 10px;
    border-radius: 6px;
    border: 1px solid var(--border);
    font-size: 14px;
}
.portfolio-row input.portfolio-weight {
    width: 70px;
    padding: 7px 8px;
    border-radius: 6px;
    border: 1px solid var(--border);
    font-size: 14px;
    text-align: right;
}
.portfolio-row .weight-pct { color: var(--gray); font-size: 13px; }
.portfolio-row .portfolio-remove {
    border: 1px solid var(--border);
    background: #fff;
    color: var(--gray);
    width: 30px;
    height: 30px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 16px;
    line-height: 1;
}

.portfolio-actions {
    display: flex;
    align-items: center;
    gap: 16px;
    margin: 10px 0 16px;
}
.portfolio-actions button {
    border: 1px solid var(--blue);
    background: #fff;
    color: var(--blue);
    padding: 7px 14px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
}
.weight-total { font-size: 14px; font-weight: 600; color: var(--gray); }
.weight-total.ok { color: var(--green); }
.weight-total.error { color: var(--red); }

/* 포트폴리오 비교검토: 여러 포트폴리오 패널을 데스크톱은 가로로, 모바일은 세로로 배치.
   패널이 3~5개로 늘어나도 검색창이 못 쓸 정도로 짜부라들지 않도록 최소 폭을 두고,
   한 줄에 다 안 들어가면 다음 줄로 넘어가게 한다(잘림/과압축 대신 줄바꿈). */
.portfolio-panels {
    display: flex;
    flex-wrap: wrap;
    gap: 24px;
    align-items: flex-start;
}
.portfolio-panel {
    flex: 1 1 280px;
    min-width: 280px;
}
.portfolio-panel:not(:first-child) {
    border-left: 1px solid var(--border);
    padding-left: 24px;
}
.portfolio-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 8px;
}
.portfolio-panel-header h2 {
    margin: 0;
    font-size: 16px;
    color: var(--dark);
}
.portfolio-panel-remove {
    border: 1px solid var(--border);
    background: #fff;
    color: var(--gray);
    width: 30px;
    height: 30px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 16px;
    line-height: 1;
}

.compare-actions { margin: 14px 0; display: flex; align-items: center; gap: 10px; }
.compare-actions button {
    border: 1px solid var(--blue);
    background: #fff;
    color: var(--blue);
    padding: 7px 14px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
}
.compare-actions button:disabled { opacity: 0.5; cursor: not-allowed; }
.reset-saved-btn {
    border: none !important;
    background: none !important;
    color: var(--gray) !important;
    text-decoration: underline;
    padding: 7px 0 !important;
    font-size: 13px !important;
}

.asset-summary-block { margin-bottom: 16px; }
.asset-summary-block h4 { margin: 0 0 8px; font-size: 13.5px; }

#calcBtn {
    padding: 9px 18px;
    border-radius: 6px;
    border: 1px solid var(--blue);
    background: var(--blue);
    color: #fff;
    cursor: pointer;
    font-size: 14px;
    font-weight: 600;
}
#calcBtn:disabled { opacity: 0.5; cursor: not-allowed; }

.portfolio-dca {
    display: flex;
    flex-wrap: wrap;
    gap: 16px;
    margin-bottom: 14px;
    font-size: 14px;
}
.portfolio-dca label {
    display: flex;
    flex-direction: column;
    gap: 4px;
    color: var(--gray);
    font-size: 13px;
}
.portfolio-dca input {
    padding: 7px 10px;
    border-radius: 6px;
    border: 1px solid var(--border);
    font-size: 14px;
    width: 170px;
}

/* 차트를 고정 높이 컨테이너로 감싸서 모바일에서도 비율이 깨지지 않게 함 */
.chart-wrap {
    position: relative;
    width: 100%;
    height: 320px;
}
.chart-wrap canvas { width: 100% !important; height: 100% !important; }
.chart-wrap-tall { height: 624px; }

/* ---------------------------------------------------------------------
   모바일 대응
--------------------------------------------------------------------- */
@media (max-width: 640px) {
    .site-header { padding: 20px 16px; }
    .container { padding: 14px; }
    .card { padding: 14px; }
    .chart-wrap { height: 240px; }
    .chart-wrap-tall { height: 468px; }
    .search-bar, .portfolio-dca, .portfolio-actions {
        flex-direction: column;
        align-items: flex-start;
    }
    .search-bar input[type="text"] { width: 100%; }
    .universe-picker { max-width: 100%; width: 100%; }
    .portfolio-row { flex-wrap: wrap; }
    .portfolio-row select.portfolio-etf { max-width: 100%; flex-basis: 100%; }
    .etf-picker { max-width: 100%; flex-basis: 100%; }
    .portfolio-panels { flex-direction: column; }
    .portfolio-panel:not(:first-child) {
        border-left: none;
        padding-left: 0;
        border-top: 1px solid var(--border);
        padding-top: 16px;
        margin-top: 4px;
    }
}

.site-footer {
    max-width: 1760px;
    margin: 8px auto 24px;
    padding: 0 24px;
    text-align: center;
    font-size: 12.5px;
    color: var(--gray);
}

.legal-footer {
    background: var(--blue);
    margin-top: 24px;
    padding: 18px 24px;
    text-align: center;
}

.legal-footer p {
    max-width: 900px;
    margin: 0 auto;
    color: rgba(255, 255, 255, 0.85);
    font-size: 12px;
    line-height: 1.7;
}

/* 포트폴리오 종목 검색(자동완성) */
.etf-picker {
    position: relative;
    flex: 1;
    max-width: 420px;
}
.etf-picker input[type="text"] {
    width: 100%;
    padding: 7px 10px;
    border-radius: 6px;
    border: 1px solid var(--border);
    font-size: 14px;
}
.etf-picker-results {
    display: none;
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    right: 0;
    max-height: 260px;
    overflow-y: auto;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.08);
    z-index: 20;
}
.etf-picker-item {
    padding: 8px 10px;
    font-size: 13.5px;
    cursor: pointer;
    border-bottom: 1px solid var(--border);
}
.etf-picker-market {
    display: inline-block;
    min-width: 32px;
    margin-right: 8px;
    font-size: 11px;
    color: var(--gray);
}
.etf-picker-item:last-child { border-bottom: none; }
.etf-picker-item:hover { background: #EAF3E7; }
.etf-picker-empty {
    padding: 8px 10px;
    font-size: 13px;
    color: var(--gray);
}
