/* ── Variables ─────────────────────────────────────────────── */
:root {
    --bg: #f4f6f9;
    --surface: #ffffff;
    --surface-2: #f8fafc;
    --border: #e4e7ec;
    --border-subtle: #f1f3f6;
    --border-row: #f3f4f6;
    --ctrl-border: #d1d5db;
    --text: #111827;
    --text-2: #374151;
    --text-3: #6b7280;
    --text-4: #9ca3af;
    --accent: #3b6aff;
    --accent-muted: rgba(59, 106, 255, 0.07);
    --row-hover: rgba(59, 106, 255, 0.05);
    --shadow: rgba(0, 0, 0, 0.08);
    --shadow-lg: rgba(0, 0, 0, 0.16);
    --color-buy: #dc2626;
    --color-buy-bg: #fef2f2;
    --color-sell: #16a34a;
    --color-sell-bg: #f0fdf4;
    --color-up: #d97706;
    --color-down: #2563eb;
    --color-good: #16a34a;
    --color-good-bg: rgba(22, 163, 74, 0.10);
    --color-good-border: rgba(22, 163, 74, 0.30);
    --color-wait: #b45309;
    --color-wait-bg: rgba(180, 83, 9, 0.10);
    --color-wait-border: rgba(180, 83, 9, 0.30);
    --color-avoid: #dc2626;
    --color-avoid-bg: rgba(220, 38, 38, 0.10);
    --color-avoid-border: rgba(220, 38, 38, 0.30);
    --color-overlap: #7c3aed;
    --color-watchlist: var(--accent);
}

[data-theme="dark"] {
    --bg: #141414;
    --surface: #1e1e1e;
    --surface-2: #242424;
    --border: #2d2d2d;
    --border-subtle: #252525;
    --border-row: #252525;
    --ctrl-border: #3a3a3a;
    --text: #f0f0ee;
    --text-2: #b0b0ae;
    --text-3: #787876;
    --text-4: #525250;
    --accent: #5b84ff;
    --accent-muted: rgba(91, 132, 255, 0.12);
    --row-hover: rgba(91, 132, 255, 0.09);
    --shadow: rgba(0, 0, 0, 0.35);
    --shadow-lg: rgba(0, 0, 0, 0.55);
    --color-buy: #f87171;
    --color-buy-bg: rgba(220, 38, 38, 0.18);
    --color-sell: #4ade80;
    --color-sell-bg: rgba(22, 163, 74, 0.18);
    --color-up: #fbbf24;
    --color-down: #60a5fa;
    --color-good: #4ade80;
    --color-good-bg: rgba(74, 222, 128, 0.12);
    --color-good-border: rgba(74, 222, 128, 0.35);
    --color-wait: #fbbf24;
    --color-wait-bg: rgba(251, 191, 36, 0.12);
    --color-wait-border: rgba(251, 191, 36, 0.35);
    --color-avoid: #f87171;
    --color-avoid-bg: rgba(248, 113, 113, 0.12);
    --color-avoid-border: rgba(248, 113, 113, 0.35);
    --color-overlap: #a78bfa;
    --color-watchlist: var(--accent);
}

/* ── Base ───────────────────────────────────────────────────── */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    font-size: 16px;
}

/* ── Header ─────────────────────────────────────────────────── */
header {
    background: var(--surface);
    border-bottom: 0.5px solid var(--border);
    padding: 0.9rem 1.5rem;
    display: flex;
    align-items: center;
    gap: 1rem;
}

header h1 {
    font-size: 22px;
    font-weight: 600;
}

header span {
    font-size: 15px;
    color: var(--text-3);
}

.header-controls {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 8px;
}

/* ── Control bar ─────────────────────────────────────────────── */
.control-bar {
    background: var(--surface);
    border-bottom: 0.5px solid var(--border);
    padding: 0.7rem 1.5rem;
    display: flex;
    align-items: center;
    gap: 1rem;
    flex-wrap: wrap;
}

.control-bar label {
    font-size: 15px;
    color: var(--text-3);
}

select {
    font-size: 15px;
    padding: 6px 12px;
    border: 0.5px solid var(--ctrl-border);
    border-radius: 8px;
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
}

.ctrl-sep {
    width: 0;
    border-left: 1px solid var(--border);
    align-self: stretch;
    margin: 0 0.1rem;
}

.mode-buttons {
    display: flex;
    gap: 8px;
}

/* ── Buttons ─────────────────────────────────────────────────── */
.icon-btn {
    font-size: 15px;
    padding: 6px 14px;
    border: 0.5px solid var(--ctrl-border);
    border-radius: 8px;
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
    line-height: 1;
    white-space: nowrap;
}

.icon-btn:hover {
    border-color: var(--text-3);
}

.icon-btn.active {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
}

/* ── Container ───────────────────────────────────────────────── */
.container {
    max-width: 960px;
    margin: 1.25rem auto;
    padding: 0 1rem;
}

/* ── Summary grid ────────────────────────────────────────────── */
.summary-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 10px;
    margin-bottom: 1.25rem;
}

.summary-card {
    background: var(--surface);
    border: 0.5px solid var(--border);
    border-radius: 10px;
    padding: 1rem;
    text-align: center;
    transition: box-shadow 0.15s, transform 0.15s;
}

.summary-card:hover {
    box-shadow: 0 4px 14px var(--shadow);
    transform: translateY(-2px);
}

.summary-card .num {
    font-size: 34px;
    font-weight: 600;
    margin-top: 6px;
}

.summary-card .lbl {
    font-size: 14px;
    color: var(--text-3);
}

.bought .num {
    color: var(--color-buy);
}

.sold .num {
    color: var(--color-sell);
}

.increased .num {
    color: var(--color-up);
}

.decreased .num {
    color: var(--color-down);
}

/* ── Section ─────────────────────────────────────────────────── */
section {
    background: var(--surface);
    border: 0.5px solid var(--border);
    border-radius: 12px;
    margin-bottom: 1rem;
    overflow: hidden;
}

.section-header {
    padding: 0.85rem 1.25rem;
    border-bottom: 0.5px solid var(--border-subtle);
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 17px;
    font-weight: 600;
}

.dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    flex-shrink: 0;
}

.dot-buy {
    background: var(--color-buy);
}

.dot-sell {
    background: var(--color-sell);
}

.dot-up {
    background: var(--color-up);
}

.dot-down {
    background: var(--color-down);
}

.dot-flat {
    background: var(--text-4);
}

/* ── Table ───────────────────────────────────────────────────── */
table {
    width: 100%;
    border-collapse: collapse;
    font-size: 15px;
}

th {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-4);
    text-align: left;
    padding: 9px 1.25rem;
    border-bottom: 0.5px solid var(--border-subtle);
    background: var(--surface-2);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    white-space: nowrap;
}

td {
    padding: 11px 1.25rem;
    border-bottom: 0.5px solid var(--border-row);
    vertical-align: middle;
}

tr:last-child td {
    border-bottom: none;
}

tbody tr {
    transition: background 0.1s;
}

tbody tr.clickable {
    cursor: pointer;
}

tbody tr.highlight td {
    background: var(--row-hover);
}

.code {
    font-family: ui-monospace, monospace;
    font-size: 14px;
    color: var(--text-3);
}

.name {
    font-weight: 500;
    font-size: 15px;
}

.weight {
    color: var(--text-4);
    font-size: 14px;
}

.arrow {
    font-size: 14px;
    color: var(--text-4);
    margin: 0 4px;
}

.diff-pos {
    color: var(--color-buy);
    font-weight: 500;
}

.diff-neg {
    color: var(--color-down);
    font-weight: 500;
}

/* ── Badges ──────────────────────────────────────────────────── */
.badge {
    display: inline-block;
    font-size: 12px;
    padding: 2px 7px;
    border-radius: 4px;
    margin-left: 6px;
    font-weight: 500;
}

.badge-new {
    background: var(--color-buy-bg);
    color: var(--color-buy);
}

.badge-sold {
    background: var(--color-sell-bg);
    color: var(--color-sell);
}

.etf-badge {
    display: inline-block;
    font-size: 12px;
    background: var(--surface-2);
    border: 0.5px solid var(--border);
    padding: 2px 6px;
    border-radius: 4px;
    white-space: nowrap;
    margin-right: 3px;
}

/* ── Quality pill ────────────────────────────────────────────── */
.quality-pill {
    display: inline-block;
    font-size: 13px;
    font-weight: 600;
    padding: 3px 10px;
    border-radius: 20px;
    white-space: nowrap;
    border: 1px solid transparent;
}

.quality-good {
    background: var(--color-good-bg);
    color: var(--color-good);
    border-color: var(--color-good-border);
}

.quality-wait {
    background: var(--color-wait-bg);
    color: var(--color-wait);
    border-color: var(--color-wait-border);
}

.quality-avoid {
    background: var(--color-avoid-bg);
    color: var(--color-avoid);
    border-color: var(--color-avoid-border);
}

.quality-nodata {
    background: var(--surface-2);
    color: var(--text-4);
    border-color: var(--border);
}

/* ── Stage-1 dots ────────────────────────────────────────────── */
.s1-dots {
    display: inline-flex;
    gap: 3px;
    align-items: center;
    vertical-align: middle;
}

.s1-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--border);
    flex-shrink: 0;
}

/* ── Score bar ───────────────────────────────────────────────── */
.score-cell {
    display: flex;
    align-items: center;
    gap: 6px;
    cursor: default;
    user-select: none;
}

.score-bar-wrap {
    width: 48px;
    height: 6px;
    background: var(--border);
    border-radius: 3px;
    overflow: hidden;
    flex-shrink: 0;
}

.score-bar-fill {
    height: 100%;
    background: var(--color-good);
    border-radius: 3px;
    transition: width 0.3s;
}

.score-num {
    font-size: 14px;
    color: var(--text-2);
    font-weight: 500;
}

.score-max {
    font-size: 12px;
    color: var(--text-4);
}

.score-hint {
    font-size: 14px;
    color: var(--text-4);
    cursor: pointer;
    transition: color 0.15s;
}

.score-hint:hover {
    color: var(--accent);
}

/* ── Score tooltip (desktop) ─────────────────────────────────── */
.score-tooltip {
    position: absolute;
    z-index: 400;
    background: var(--surface);
    border: 0.5px solid var(--border);
    border-radius: 12px;
    box-shadow: 0 6px 24px var(--shadow-lg);
    padding: 14px 16px;
    min-width: 300px;
    max-width: 380px;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.15s;
    font-size: 14px;
}

.score-tooltip.visible {
    opacity: 1;
    pointer-events: auto;
}

.tt-header {
    font-weight: 600;
    margin-bottom: 10px;
    font-size: 15px;
}

.tt-code {
    font-family: ui-monospace, monospace;
    color: var(--text-3);
    margin-right: 6px;
    font-size: 14px;
}

.tt-stage-label {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.07em;
    color: var(--text-4);
    margin-bottom: 7px;
    font-weight: 700;
}

.tt-row {
    display: flex;
    align-items: baseline;
    gap: 6px;
    margin-bottom: 4px;
}

.tt-factor {
    color: var(--text-3);
    min-width: 72px;
    flex-shrink: 0;
    font-size: 13px;
}

.tt-detail {
    color: var(--text-2);
    flex: 1;
    font-size: 13px;
}

.tt-pts {
    font-weight: 700;
    min-width: 26px;
    text-align: right;
    font-size: 13px;
}

.tt-pts--pos {
    color: var(--color-good);
}

.tt-pts--mid {
    color: var(--color-wait);
}

.tt-pts--zero {
    color: var(--text-4);
}

.tt-subtotal {
    font-size: 12px;
    color: var(--text-3);
    text-align: right;
    margin-top: 5px;
    border-top: 0.5px solid var(--border-row);
    padding-top: 5px;
}

.tt-divider {
    border-top: 0.5px solid var(--border);
    margin: 10px 0;
}

.tt-footer {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 10px;
    font-size: 13px;
    color: var(--text-3);
    border-top: 0.5px solid var(--border-row);
    padding-top: 8px;
}

.tt-veto {
    margin-top: 6px;
    font-size: 12px;
    color: var(--color-avoid);
    padding-top: 6px;
    border-top: 0.5px solid var(--border-row);
}

/* ── Score sheet (mobile bottom sheet) ───────────────────────── */
.score-sheet-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    z-index: 300;
    align-items: flex-end;
}

.score-sheet-backdrop.open {
    display: flex;
}

.score-sheet {
    background: var(--surface);
    border-radius: 20px 20px 0 0;
    padding: 1.25rem 1.25rem calc(1.25rem + env(safe-area-inset-bottom));
    width: 100%;
    max-height: 80vh;
    overflow-y: auto;
    font-size: 14px;
    position: relative;
}

.score-sheet-close {
    position: absolute;
    top: 1rem;
    right: 1rem;
    background: none;
    border: none;
    font-size: 20px;
    cursor: pointer;
    color: var(--text-4);
    line-height: 1;
}

.score-sheet-close:hover {
    color: var(--text-2);
}

.score-sheet-handle {
    width: 36px;
    height: 4px;
    background: var(--border);
    border-radius: 2px;
    margin: 0 auto 16px;
}

/* ── Empty / loading ─────────────────────────────────────────── */
.empty {
    padding: 1.25rem;
    color: var(--text-4);
}

.loading {
    padding: 2rem 1.25rem;
    color: var(--text-4);
    text-align: center;
}

/* ── Date picker ─────────────────────────────────────────────── */
.date-picker {
    position: relative;
}

.date-toggle {
    font-size: 14px;
    padding: 6px 12px;
    border: 0.5px solid var(--ctrl-border);
    border-radius: 8px;
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
    min-width: 110px;
    text-align: left;
}

.date-toggle:hover {
    border-color: var(--text-3);
}

.cal-panel {
    display: none;
    position: absolute;
    top: calc(100% + 6px);
    left: 0;
    background: var(--surface);
    border: 0.5px solid var(--border);
    border-radius: 12px;
    box-shadow: 0 4px 20px var(--shadow-lg);
    z-index: 150;
    padding: 1rem;
    min-width: 252px;
}

.cal-panel.open {
    display: block;
}

.cal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0.75rem;
}

.cal-month-label {
    font-size: 14px;
    font-weight: 500;
}

.cal-nav {
    background: none;
    border: none;
    font-size: 20px;
    cursor: pointer;
    color: var(--text-4);
    padding: 0 4px;
    line-height: 1;
}

.cal-nav:hover {
    color: var(--text);
}

.cal-weekdays {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    margin-bottom: 3px;
}

.cal-weekdays span {
    text-align: center;
    font-size: 12px;
    color: var(--text-4);
    font-weight: 500;
    padding: 3px 0;
}

.cal-grid {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 2px;
}

.cal-day {
    text-align: center;
    padding: 7px 2px;
    border-radius: 6px;
    font-size: 13px;
    color: var(--border);
    line-height: 1;
}

.cal-day.available {
    color: var(--text);
    cursor: pointer;
}

.cal-day.available:hover {
    background: var(--accent-muted);
}

.cal-day.selected {
    background: var(--accent) !important;
    color: #fff !important;
    font-weight: 600;
}

/* ── Chart modal ─────────────────────────────────────────────── */
.modal-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    z-index: 200;
    align-items: center;
    justify-content: center;
}

.modal-backdrop.open {
    display: flex;
}

.modal-card {
    background: var(--surface);
    border-radius: 16px;
    padding: 1.5rem 1.75rem 1.75rem;
    width: 90%;
    max-width: 720px;
    position: relative;
    max-height: 90vh;
    overflow-y: auto;
}

.modal-close {
    position: absolute;
    top: 1rem;
    right: 1rem;
    background: none;
    border: none;
    font-size: 22px;
    cursor: pointer;
    color: var(--text-4);
    line-height: 1;
}

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

.modal-title {
    font-size: 17px;
    font-weight: 600;
    margin-bottom: 1.25rem;
}

.modal-title .modal-code {
    font-family: ui-monospace, monospace;
    font-size: 14px;
    color: var(--text-3);
    margin-right: 6px;
}

/* ── Mobile tab bar ──────────────────────────────────────────── */
.tab-bar {
    display: none;
}

/* ── Responsive ──────────────────────────────────────────────── */
@media (max-width: 600px) {
    .mode-buttons {
        display: none;
    }

    .ctrl-sep {
        display: none;
    }

    .container {
        padding-bottom: calc(60px + env(safe-area-inset-bottom));
    }

    .summary-grid {
        grid-template-columns: repeat(2, 1fr);
    }

    .summary-card .num {
        font-size: 28px;
    }

    th,
    td {
        padding: 9px 0.75rem;
    }

    .modal-card {
        padding: 1.1rem 0.9rem 1.5rem;
    }

    .tab-bar {
        display: flex;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: 56px;
        background: var(--surface);
        border-top: 0.5px solid var(--border);
        z-index: 100;
        padding-bottom: env(safe-area-inset-bottom);
    }

    .tab-bar-item {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 2px;
        font-size: 10px;
        color: var(--text-3);
        cursor: pointer;
        border: none;
        background: none;
        padding: 0;
        -webkit-tap-highlight-color: transparent;
    }

    .tab-bar-item.active {
        color: var(--accent);
    }

    .tab-icon {
        font-size: 18px;
        line-height: 1;
    }
}