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

/* ── Mode Tab Bar ── */
.mode-bar {
    display: flex;
    gap: 4px;
    justify-content: center;
    padding: 10px 15px 0;
}
.mode-btn {
    padding: 7px 22px;
    border: 2px solid rgba(255,255,255,.2);
    border-radius: 8px;
    background: transparent;
    color: #fff;
    font-size: .92em;
    font-weight: 600;
    cursor: pointer;
    transition: all .25s;
}
.mode-btn.active {
    background: rgba(102,126,234,.35);
    border-color: #667eea;
    color: #a5b4fc;
}
.mode-btn:hover:not(.active) { background: rgba(255,255,255,.08); }

/* Dataset mode */
.ds-panel {
    background: rgba(255,255,255,.08);
    backdrop-filter: blur(10px);
    border-radius: 12px;
    padding: 18px 20px;
    margin-bottom: 12px;
    box-shadow: 0 4px 20px rgba(0,0,0,.25);
}
.ds-form-inline {
    display: flex;
    gap: 15px;
    align-items: flex-end;
    flex-wrap: wrap;
    margin-bottom: 12px;
}
.ds-form-group { display: flex; flex-direction: column; min-width: 100px; }
.ds-form-group label { font-size: .82em; margin-bottom: 3px; opacity: .85; }
.ds-form-group input, .ds-form-group select {
    padding: 7px 10px; border: none; border-radius: 6px;
    background: rgba(255,255,255,.15); color: #fff; font-size: .9em;
}
.ds-presets { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.ds-preset-btn {
    padding: 5px 14px; border: none; border-radius: 6px;
    background: rgba(255,255,255,.12); color: #fff;
    cursor: pointer; font-size: .84em; transition: background .2s;
}
.ds-preset-btn:hover { background: rgba(255,255,255,.22); }
.ds-info {
    padding: 10px 14px;
    background: rgba(251,191,36,.1);
    border-left: 4px solid #fbbf24;
    border-radius: 5px;
    font-size: .88em;
}
.ds-stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 12px;
    margin-bottom: 15px;
}
.ds-stat-card {
    background: rgba(255,255,255,.08);
    border-radius: 10px;
    padding: 15px;
}
.ds-stat-card h4 { color: #fbbf24; font-size: .9em; margin-bottom: 6px; }
.ds-stat-val { font-size: 2em; font-weight: bold; color: #667eea; }
.ds-stat-lbl { font-size: .82em; opacity: .75; }
.ds-viz-wrap {
    background: rgba(255,255,255,.08);
    border-radius: 12px;
    padding: 18px;
    margin-bottom: 12px;
}
.ds-viz-wrap h3 { color: #fbbf24; margin-bottom: 14px; font-size: 1em; }
#ds-movement-graph { width: 100%; height: 560px;
    background: rgba(0,0,0,.2); border-radius: 10px; }
.ds-star-lists {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
    gap: 12px;
}
.ds-list-card {
    background: rgba(255,255,255,.08);
    border-radius: 10px; padding: 15px;
    max-height: 420px; overflow-y: auto;
}
.ds-list-card h4 { color: #fbbf24; font-size: .9em; margin-bottom: 10px;
    position: sticky; top: 0; background: inherit; padding-bottom: 6px; }
.ds-star-item {
    padding: 8px 10px; margin-bottom: 6px;
    background: rgba(255,255,255,.05);
    border-radius: 6px; border-left: 3px solid transparent;
    transition: all .25s;
}
.ds-star-item.fixed  { border-left-color: #3b82f6; }
.ds-star-item.moving { border-left-color: #fbbf24; }
.ds-star-item:hover  { background: rgba(255,255,255,.1); }
.ds-star-name { font-weight: bold; font-size: .95em; margin-bottom: 3px; }
.ds-star-detail { font-size: .8em; opacity: .8; }
.ds-timeline-ctrl {
    display: flex; align-items: center; gap: 10px;
    flex-wrap: wrap; margin-bottom: 12px;
}
.ds-timeline-ctrl button {
    padding: 6px 14px; border: none; border-radius: 6px;
    background: rgba(255,255,255,.15); color: #fff;
    cursor: pointer; font-size: .86em;
}
.ds-tl-slider {
    flex: 1; min-width: 120px; height: 6px;
    -webkit-appearance: none; appearance: none;
    background: rgba(255,255,255,.2); border-radius: 5px; outline: none;
}
.ds-tl-slider::-webkit-slider-thumb {
    -webkit-appearance: none; width: 16px; height: 16px;
    border-radius: 50%; background: #667eea; cursor: pointer;
}
.ds-hour-indicator {
    font-size: .95em; font-weight: bold; min-width: 120px;
    text-align: center; padding: 4px 10px;
    background: rgba(255,255,255,.1); border-radius: 6px;
}
#mode-dataset { display: none; }

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);
    color: #fff;
    min-height: 100vh;
}

.container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 15px;
}

header {
    text-align: center;
    margin-bottom: 15px;
}
header h1 { font-size: 1.8em; text-shadow: 2px 2px 4px rgba(0,0,0,.5); }
header .subtitle { font-size: 1em; opacity: .85; margin-top: 4px; }

/* ── Controls ────────────────────────────── */
.panel {
    background: rgba(255,255,255,.08);
    backdrop-filter: blur(10px);
    border-radius: 12px;
    padding: 15px 20px;
    margin-bottom: 12px;
    box-shadow: 0 4px 20px rgba(0,0,0,.25);
}

.form-inline {
    display: flex;
    gap: 12px;
    align-items: flex-end;
    flex-wrap: wrap;
}

.form-group {
    display: flex;
    flex-direction: column;
    min-width: 80px;
}
.form-group label { font-size: .82em; margin-bottom: 3px; opacity: .85; }
.form-group input, .form-group select {
    padding: 7px 10px;
    border: none;
    border-radius: 6px;
    background: rgba(255,255,255,.15);
    color: #fff;
    font-size: .92em;
}
.form-group input:focus, .form-group select:focus {
    outline: 2px solid rgba(102,126,234,.6);
}

.calendar-toggle { display: flex; gap: 4px; }
.calendar-toggle button {
    padding: 7px 14px;
    border: 2px solid rgba(255,255,255,.2);
    border-radius: 6px;
    background: transparent;
    color: #fff;
    cursor: pointer;
    font-size: .88em;
    transition: all .25s;
}
.calendar-toggle button.active {
    background: rgba(255,255,255,.22);
    border-color: #fff;
}

.btn-analyze {
    padding: 7px 22px;
    border: none;
    border-radius: 8px;
    background: linear-gradient(135deg, #667eea, #764ba2);
    color: #fff;
    font-size: .95em;
    font-weight: bold;
    cursor: pointer;
    transition: transform .2s;
    box-shadow: 0 3px 12px rgba(0,0,0,.3);
}
.btn-analyze:hover { transform: translateY(-2px); }

/* ── View Toggle ─────────────────────────── */
.view-toggle {
    display: none;
    gap: 6px;
    margin-bottom: 12px;
}
.view-toggle button {
    flex: 1;
    padding: 8px;
    border: 2px solid rgba(255,255,255,.2);
    border-radius: 8px;
    background: transparent;
    color: #fff;
    cursor: pointer;
    font-size: .9em;
    transition: all .25s;
}
.view-toggle button.active {
    background: rgba(102,126,234,.35);
    border-color: #667eea;
}

/* ── Timeline ────────────────────────────── */
.timeline-panel { display: none; }
.timeline-controls {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}
.timeline-controls button {
    padding: 6px 14px;
    border: none;
    border-radius: 6px;
    background: rgba(255,255,255,.15);
    color: #fff;
    cursor: pointer;
    font-size: .88em;
    transition: background .2s;
}
.timeline-controls button:hover { background: rgba(255,255,255,.25); }

.timeline-slider {
    flex: 1;
    min-width: 120px;
    height: 6px;
    -webkit-appearance: none;
    appearance: none;
    background: rgba(255,255,255,.2);
    border-radius: 5px;
    outline: none;
}
.timeline-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 18px; height: 18px;
    border-radius: 50%;
    background: #667eea;
    cursor: pointer;
}

.hour-indicator {
    font-size: 1.05em;
    font-weight: bold;
    min-width: 130px;
    text-align: center;
    padding: 4px 10px;
    background: rgba(255,255,255,.1);
    border-radius: 6px;
}

/* ── Grid View (Traditional Tử Vi Layout) ── */
.grid-view { display: none; position: relative; }

.cung-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(4, auto);
    gap: 0;
    max-width: 1100px;
    margin: 0 auto;
    border: 2px solid #444;
    background: #fff;
    box-shadow: 0 4px 24px rgba(0,0,0,.25);
}

.cung-cell {
    background: #fffef8;
    border: 1px solid #999;
    padding: 0;
    min-height: 140px;
    transition: background .3s;
    position: relative;
    display: flex;
    flex-direction: column;
}
.cung-cell:hover {
    background: #fffff0;
}
.cung-cell.is-menh {
    border: 2px solid #c62828;
}
.cung-cell.is-menh .cung-name { color: #c62828; }
.cung-cell.is-than {
    border: 2px solid #1565c0;
}
.cung-cell.is-than .cung-name { color: #1565c0; }

/* ── Header: chi | PALACE NAME | pos ── */
.cung-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 2px 5px;
    border-bottom: 1px solid #bbb;
    background: #f5f5f0;
    min-height: 20px;
}
.cung-chi {
    font-size: .72em;
    color: #388e3c;
    min-width: 24px;
}
.cung-name {
    font-size: .82em;
    font-weight: bold;
    color: #333;
    text-transform: uppercase;
    letter-spacing: .5px;
    flex: 1;
    text-align: center;
}
.cung-pos {
    font-size: .68em;
    color: #999;
    min-width: 16px;
    text-align: right;
}

/* ── Main Star (Chính Tinh) — large centered ── */
.cung-main-star {
    text-align: center;
    padding: 3px 4px 2px;
    min-height: 18px;
    border-bottom: 1px solid #ddd;
}
.cung-main-star:empty { display: none; }
.cung-main-star .main-star-name {
    font-size: .92em;
    font-weight: bold;
    color: #222;
    line-height: 1.3;
}

/* ── Two-column star body (Good | Bad) ── */
.cung-stars {
    display: flex;
    flex-direction: row;
    gap: 0;
    flex: 1;
    min-height: 0;
    overflow: hidden;
}

.stars-col {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 0;
    padding: 2px 4px;
    max-height: 180px;
    overflow-y: auto;
    min-width: 0;
}
.stars-col-good {
    border-right: 1px dashed #ccc;
}
.stars-col:empty { display: none; }
.stars-col-good:empty + .stars-col-bad {
    border-left: none;
}

.star-item {
    font-size: .74em;
    padding: 0 1px;
    background: transparent;
    transition: background .3s;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.45;
}
.star-item.highlight {
    background: rgba(255, 235, 59, .35);
    animation: pulse .8s ease-in-out 2;
}
@keyframes pulse {
    50% { transform: scale(1.02); opacity: .85; }
}

/* Good stars = GREEN */
.star-good { color: #2e7d32; }
/* Bad stars = RED */
.star-bad  { color: #c62828; }
/* Lưu Niên stars = italic + slightly different shade */
.star-luu  { font-style: italic; opacity: 0.85; }
.star-luu.star-good { color: #1b5e20; }
.star-luu.star-bad  { color: #b71c1c; }

/* Element colors (used in D3 graph only) — Đại Việt: Kim=Trắng */
.star-kim  { color: #9E9E9E; }
.star-moc  { color: #2ecc71; }
.star-thuy { color: #2980b9; }
.star-hoa  { color: #e74c3c; }
.star-tho  { color: #f39c12; }

/* Grid scrollbar (light theme) */
.stars-col::-webkit-scrollbar { width: 3px; }
.stars-col::-webkit-scrollbar-track { background: transparent; }
.stars-col::-webkit-scrollbar-thumb { background: #ccc; border-radius: 2px; }

/* ── Footer: Trường Sinh + Tiểu Hạn ── */
.cung-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 2px 5px;
    border-top: 1px solid #bbb;
    background: #f5f5f0;
    min-height: 18px;
    margin-top: auto;
}
.cung-footer:empty { display: none; }
.cung-truong-sinh {
    font-size: .72em;
    font-weight: bold;
    color: #444;
}
.cung-tieu-han {
    font-size: .68em;
    font-weight: bold;
    color: #fff;
    background: #c62828;
    padding: 1px 6px;
    border-radius: 3px;
}
.cung-tieu-han:empty { display: none; }

/* Center cell */
.cung-center {
    grid-column: 2 / 4;
    grid-row: 2 / 4;
    background: #fafaf2;
    border: 2px solid #444;
    padding: 15px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
}
.center-title {
    font-size: 1.1em;
    font-weight: bold;
    margin-bottom: 10px;
    color: #c62828;
}
.center-content {
    line-height: 1.7;
    font-size: .9em;
    color: #333;
}

/* ── D3 Graph View ───────────────────────── */
.graph-view {
    display: none;
    position: relative;
    background: rgba(0,0,0,.2);
    border-radius: 12px;
    overflow: hidden;
    height: 600px;
}
#star-graph { width: 100%; height: 100%; }

.legend {
    position: absolute;
    top: 12px;
    right: 12px;
    background: rgba(0,0,0,.75);
    backdrop-filter: blur(8px);
    padding: 12px;
    border-radius: 8px;
    font-size: .82em;
    z-index: 10;
}
.legend h4 { margin-bottom: 8px; color: #fbbf24; }
.legend-item { display: flex; align-items: center; gap: 6px; margin-bottom: 4px; }
.legend-dot { width: 12px; height: 12px; border-radius: 50%; border: 1px solid rgba(255,255,255,.5); }

/* ── Analysis Section ────────────────────── */
.analysis-section { display: none; }
.analysis-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 15px;
    margin-top: 15px;
}
.analysis-card {
    background: rgba(255,255,255,.08);
    border-radius: 10px;
    padding: 15px;
}
.analysis-card h3 { margin-bottom: 10px; color: #fbbf24; font-size: 1em; }

.star-list { display: flex; flex-wrap: wrap; gap: 6px; }
.star-badge {
    padding: 4px 10px;
    border-radius: 16px;
    background: rgba(255,255,255,.15);
    font-size: .85em;
}
.star-badge.moving { background: rgba(251,191,36,.25); border: 1px solid #fbbf24; }
.star-badge.fixed  { background: rgba(59,130,246,.25); border: 1px solid #3b82f6; }

/* ── Loading ─────────────────────────────── */
.loading-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.6);
    z-index: 999;
    justify-content: center;
    align-items: center;
    flex-direction: column;
}
.spinner {
    width: 48px; height: 48px;
    border: 4px solid rgba(255,255,255,.25);
    border-top-color: #fff;
    border-radius: 50%;
    animation: spin .8s linear infinite;
    margin-bottom: 15px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Tooltip ─────────────────────────────── */
.tooltip {
    position: fixed;
    background: rgba(0,0,0,.92);
    color: #fff;
    padding: 8px 12px;
    border-radius: 6px;
    pointer-events: none;
    opacity: 0;
    transition: opacity .2s;
    z-index: 1000;
    max-width: 280px;
    font-size: .85em;
    box-shadow: 0 4px 15px rgba(0,0,0,.5);
}
.tooltip.show { opacity: 1; }

/* ── View Container (wrapper for overlay) ── */
.view-container {
    position: relative;
}

/* ── Star Filter Panel (overlay inside view) ── */
.filter-panel {
    position: absolute;
    top: 8px;
    left: 8px;
    z-index: 50;
    width: 360px;
    max-height: calc(100% - 16px);
    overflow-y: auto;
    background: rgba(15, 20, 40, .92);
    backdrop-filter: blur(12px);
    border: 1.5px solid rgba(102,126,234,.35);
    border-radius: 10px;
    box-shadow: 0 8px 32px rgba(0,0,0,.45);
    transition: all .35s cubic-bezier(.4,0,.2,1);
    transform-origin: top left;
}
.filter-panel.collapsed {
    width: auto;
    max-height: none;
    overflow: visible;
    background: transparent;
    border: none;
    box-shadow: none;
    backdrop-filter: none;
}
.filter-panel.collapsed .filter-body {
    display: none;
}

.filter-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    cursor: pointer;
    user-select: none;
    border-radius: 10px;
    transition: background .2s;
}
.filter-header:hover {
    background: rgba(255,255,255,.08);
}
.filter-header-title {
    font-size: .88em;
    font-weight: 600;
    color: #fbbf24;
    flex: 1;
}
.filter-header-badge {
    font-size: .72em;
    padding: 2px 8px;
    border-radius: 10px;
    background: rgba(102,126,234,.35);
    color: #a5b4fc;
}
.filter-header-arrow {
    font-size: .75em;
    transition: transform .3s;
    opacity: .6;
}
.filter-panel.collapsed .filter-header-arrow {
    transform: rotate(-90deg);
}
.filter-panel.collapsed .filter-header {
    background: rgba(15, 20, 40, .88);
    backdrop-filter: blur(10px);
    border: 1.5px solid rgba(102,126,234,.3);
    box-shadow: 0 4px 16px rgba(0,0,0,.35);
}

.filter-body {
    padding: 0 12px 12px;
}

.filter-section {
    margin-bottom: 10px;
}
.filter-section h4 {
    font-size: .82em;
    color: #fbbf24;
    margin-bottom: 6px;
    display: flex;
    align-items: center;
    gap: 6px;
}

.filter-groups {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}
.filter-chip {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 5px 10px;
    border-radius: 16px;
    background: rgba(255,255,255,.12);
    font-size: .82em;
    cursor: pointer;
    transition: all .25s;
    user-select: none;
    border: 1.5px solid transparent;
}
.filter-chip:hover {
    background: rgba(255,255,255,.2);
}
.filter-chip.active {
    background: rgba(102,126,234,.3);
    border-color: rgba(102,126,234,.6);
}
.filter-chip input[type="checkbox"] {
    accent-color: #667eea;
    width: 14px;
    height: 14px;
    margin: 0;
}
.filter-chip .chip-count {
    font-size: .75em;
    opacity: .65;
    margin-left: 2px;
}

/* Individual star search */
.star-search-box {
    width: 100%;
    padding: 7px 10px;
    border: none;
    border-radius: 6px;
    background: rgba(255,255,255,.12);
    color: #fff;
    font-size: .88em;
    margin-bottom: 8px;
}
.star-search-box::placeholder { color: rgba(255,255,255,.4); }
.star-search-box:focus { outline: 2px solid rgba(102,126,234,.5); }

.star-checklist {
    max-height: 220px;
    overflow-y: auto;
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
}
.star-check-item {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px 8px;
    border-radius: 4px;
    font-size: .78em;
    cursor: pointer;
    background: rgba(255,255,255,.06);
    transition: background .2s;
    white-space: nowrap;
}
.star-check-item:hover { background: rgba(255,255,255,.14); }
.star-check-item.hidden { display: none; }
.star-check-item input { accent-color: #667eea; width: 13px; height: 13px; margin: 0; }
.star-check-item.dimmed { opacity: .35; }

/* Filter quick actions */
.filter-actions {
    display: flex;
    gap: 6px;
    margin-bottom: 8px;
    flex-wrap: wrap;
}
.filter-actions button {
    padding: 4px 10px;
    border: 1px solid rgba(255,255,255,.2);
    border-radius: 5px;
    background: transparent;
    color: #fff;
    font-size: .78em;
    cursor: pointer;
    transition: background .2s;
}
.filter-actions button:hover { background: rgba(255,255,255,.15); }

.filter-summary {
    font-size: .78em;
    opacity: .65;
    padding: 0 12px 4px;
}

/* ── Scrollbar ───────────────────────────── */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: rgba(255,255,255,.05); }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,.2); border-radius: 3px; }

/* ── Responsive ──────────────────────────── */
@media (max-width: 768px) {
    .cung-grid {
        grid-template-columns: repeat(4, 1fr);
        gap: 4px;
    }
    .cung-cell { min-height: 100px; }
    .star-item { font-size: .66em; }
    .stars-col { padding: 1px 3px; }
    .cung-main-star .main-star-name { font-size: .8em; }
    .cung-chi { font-size: .66em; }
    .cung-name { font-size: .72em; }
    .graph-view { height: 400px; }
    .filter-panel { width: 280px; }
    .filter-panel .star-checklist { max-height: 140px; }
}
