/* Reset and Variables */
:root {
    --bg-color: #0b0f19;
    --panel-bg: rgba(26, 32, 44, 0.75);
    --border-color: rgba(255, 255, 255, 0.12);
    --text-primary: #e2e8f0;
    --text-secondary: #94a3b8;
    --accent-color: #3b82f6;
    --accent-hover: #2563eb;
    --green-accent: #10b981;
    --font-main: 'Noto Sans JP', sans-serif;
    --font-eng: 'Outfit', sans-serif;
}

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

body.dark-theme {
    background-color: var(--bg-color);
    color: var(--text-primary);
    font-family: var(--font-main);
    overflow: hidden;
    height: 100vh;
    display: flex;
    flex-direction: column;
}

/* Typography elements */
h1,
h2,
h3 {
    font-weight: 700;
}

/* Glassmorphism Styles */
.glass-header {
    background: var(--panel-bg);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-bottom: 1px solid var(--border-color);
    padding: 12px 24px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    z-index: 1000;
}

.glass-panel {
    background: var(--panel-bg);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.4);
}

/* Header */
.logo h1 {
    font-size: 1.25rem;
    margin: 0;
    display: flex;
    align-items: center;
    gap: 12px;
}

.version {
    font-family: var(--font-eng);
    font-size: 0.8rem;
    background: rgba(59, 130, 246, 0.2);
    color: #60a5fa;
    padding: 2px 8px;
    border-radius: 4px;
    border: 1px solid rgba(59, 130, 246, 0.4);
}

.subtitle {
    font-size: 0.8rem;
    color: var(--text-secondary);
    margin-top: 4px;
}

/* Main Layout */
main {
    flex: 1;
    position: relative;
    display: flex;
}

#controls-panel {
    position: absolute;
    top: 24px;
    left: 24px;
    width: 320px;
    padding: 24px;
    z-index: 1000;
    /* Above Leaflet */
    display: flex;
    flex-direction: column;
    gap: 20px;
    transition: transform 0.3s ease;
}

#map-container {
    width: 100%;
    height: 100%;
    z-index: 1;
    /* Leaflet base */
}

/* Leaflet dark theme tweaks */
.leaflet-container {
    background: #0f1423 !important;
}

/* Form Controls */
.control-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.control-group label {
    font-size: 0.9rem;
    color: var(--text-secondary);
    font-weight: 500;
}

.select-wrapper {
    position: relative;
    width: 100%;
}

select,
input[type="text"] {
    width: 100%;
    padding: 10px 14px;
    background: rgba(15, 23, 42, 0.6);
    border: 1px solid rgba(148, 163, 184, 0.3);
    border-radius: 8px;
    color: var(--text-primary);
    font-family: var(--font-main);
    font-size: 0.95rem;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
    appearance: none;
}

.select-wrapper::after {
    content: "▼";
    font-size: 0.7rem;
    position: absolute;
    right: 14px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-secondary);
    pointer-events: none;
}

select:hover,
input[type="text"]:focus {
    border-color: var(--accent-color);
}

input[type="text"]:focus {
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.25);
}

option {
    background: #1e293b;
    color: #e2e8f0;
}

/* Buttons */
.primary-btn {
    background: var(--accent-color);
    color: white;
    border: none;
    padding: 12px;
    border-radius: 8px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    font-family: var(--font-main);
}

.primary-btn:disabled {
    background: rgba(255, 255, 255, 0.1);
    color: rgba(255, 255, 255, 0.3);
    cursor: not-allowed;
    transform: none;
}

.primary-btn:hover {
    background: var(--accent-hover);
    transform: translateY(-1px);
}

.primary-btn:active {
    transform: translateY(1px);
}

.secondary-btn {
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    padding: 10px;
    border-radius: 8px;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    font-family: var(--font-main);
    width: 100%;
}

.secondary-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: var(--text-secondary);
}

.button-group {
    display: flex;
    gap: 12px;
}

.icon-btn {
    background: transparent;
    border: 1px solid transparent;
    color: var(--text-secondary);
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
}

.icon-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    color: white;
}

/* Status & Info Box */
.info-box {
    background: rgba(59, 130, 246, 0.1);
    border-left: 4px solid var(--accent-color);
    padding: 12px;
    border-radius: 4px;
    font-size: 0.85rem;
    color: var(--text-secondary);
}

.status-msg {
    font-size: 0.85rem;
    color: #cbd5e1;
    min-height: 20px;
}

/* Modal */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2000;
    opacity: 1;
    transition: opacity 0.3s;
}

.modal.hidden {
    opacity: 0;
    pointer-events: none;
}

.modal-content {
    width: 90%;
    max-width: 500px;
    padding: 0;
    overflow: hidden;
}

.modal-header {
    padding: 16px 24px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal-header h2 {
    font-size: 1.2rem;
    margin: 0;
}

.modal-body {
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.instruction-box {
    background: rgba(234, 179, 8, 0.1);
    border: 1px solid rgba(234, 179, 8, 0.3);
    border-radius: 8px;
    padding: 16px;
    font-size: 0.9rem;
}

.instruction-box h3 {
    color: #fcd34d;
    margin-bottom: 8px;
    font-size: 1rem;
}

.instruction-box li {
    margin-left: 20px;
    margin-top: 6px;
    color: var(--text-secondary);
}

.instruction-box a {
    color: var(--accent-color);
    text-decoration: none;
}

.instruction-box a:hover {
    text-decoration: underline;
}

/* D3 Tooltip */
.d3-tooltip {
    position: absolute;
    background: rgba(15, 23, 42, 0.9);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: white;
    padding: 10px 14px;
    border-radius: 6px;
    font-size: 0.85rem;
    pointer-events: none;
    z-index: 3000;
    /* Above Leaflet and GUI */
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.5);
    transition: opacity 0.2s;
    font-family: var(--font-main);
}

.d3-tooltip.hidden {
    opacity: 0;
}

/* D3 Vector Styles */
.migration-link {
    fill: none;
    stroke-linecap: round;
    transition: stroke-width 0.3s ease;
    stroke-dasharray: 8 16;
    animation: flow 1s linear infinite;
}

@keyframes flow {
    from { stroke-dashoffset: 24; }
    to { stroke-dashoffset: 0; }
}

.leaflet-interactive {
    transition: fill 0.4s ease, fill-opacity 0.4s ease;
}

.migration-link:hover {
    stroke: #60a5fa !important;
    filter: drop-shadow(0 0 6px #3b82f6);
}

.label-bg {
    fill: rgba(15, 23, 42, 0.8);
    rx: 4px;
    ry: 4px;
    stroke: rgba(255, 255, 255, 0.2);
    stroke-width: 1px;
}


.label-text {
    fill: #e2e8f0;
    font-size: 11px;
    font-weight: 600;
    text-anchor: middle;
    font-family: var(--font-eng);
    pointer-events: none;
}

/* Custom Interactive Popups */
.leaflet-popup-content-wrapper {
    background: var(--panel-bg);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    border-radius: 8px;
    box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.4);
}

.leaflet-popup-tip {
    background: var(--panel-bg);
}

.custom-popup {
    text-align: center;
    padding: 4px;
}

.custom-popup h4 {
    margin: 0 0 12px 0;
    font-size: 1.1rem;
    font-family: var(--font-main);
    color: white;
    border-bottom: 1px solid rgba(255,255,255,0.1);
    padding-bottom: 8px;
}

.popup-buttons {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.popup-btn {
    background: rgba(255,255,255,0.05);
    color: var(--text-primary);
    border: 1px solid rgba(255,255,255,0.2);
    padding: 8px 12px;
    border-radius: 6px;
    font-size: 0.9rem;
    cursor: pointer;
    transition: all 0.2s;
    font-family: var(--font-main);
}

.popup-btn.inbound-btn:hover {
    background: rgba(16, 185, 129, 0.2);
    border-color: #10b981;
    color: #10b981;
}

.popup-btn.outbound-btn:hover {
    background: rgba(59, 130, 246, 0.2);
    border-color: #3b82f6;
    color: #60a5fa;
}

/* Stats Panel */
#stats-panel {
    position: absolute;
    top: 50px;
    left: 100px;
    width: 320px;
    z-index: 1000;
    pointer-events: auto;
}

#stats-panel.hidden {
    display: none;
}

#stats-title {
    margin: -20px -20px 15px -20px;
    padding: 10px 15px;
    background: rgba(255, 255, 255, 0.08);
    border-bottom: 1px solid rgba(255, 255, 255, 0.15);
    border-top-left-radius: 12px;
    border-top-right-radius: 12px;
    font-size: 1.05rem;
    color: white;
    cursor: grab;
    display: flex;
    align-items: center;
    justify-content: center;
}

#stats-title::before,
#stats-title::after {
    content: '';
    display: inline-block;
    width: 24px;
    height: 4px;
    border-top: 1px solid rgba(255,255,255,0.4);
    border-bottom: 1px solid rgba(255,255,255,0.4);
    margin: 0 14px;
}

#stats-title:active {
    cursor: grabbing;
    background: rgba(255, 255, 255, 0.12);
}

.stats-overview {
    display: flex;
    justify-content: space-between;
    margin-bottom: 15px;
    background: rgba(0, 0, 0, 0.25);
    padding: 10px;
    border-radius: 6px;
    border: 1px solid rgba(255, 255, 255, 0.05);
}

.stat-box {
    display: flex;
    flex-direction: column;
    cursor: pointer;
    padding: 8px;
    border-radius: 6px;
    border: 1px solid transparent;
    transition: all 0.2s;
}

.stat-box:hover {
    background: rgba(255, 255, 255, 0.05);
}

.stat-box.active-mode.outbound {
    background: rgba(59, 130, 246, 0.2);
    border-color: rgba(59, 130, 246, 0.5);
}

.stat-box.active-mode.inbound {
    background: rgba(16, 185, 129, 0.2);
    border-color: rgba(16, 185, 129, 0.5);
}

.stat-box.right {
    text-align: right;
}

.stat-label {
    font-size: 0.95rem;
    color: #cbd5e1;
    margin-bottom: 4px;
}

.stat-value {
    font-size: 1.35rem;
    font-weight: bold;
    color: #e2e8f0;
    font-family: var(--font-eng);
    font-variant-numeric: tabular-nums;
}

#stats-list-title {
    margin: 0 0 10px 0;
    color: #cbd5e1;
    font-size: 1rem;
    font-weight: 500;
}

.stats-list {
    list-style: none;
    padding: 0;
    margin: 0;
}

.stats-list li {
    display: flex;
    align-items: center;
    padding: 10px 12px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    font-size: 0.95rem;
    transition: all 0.2s ease;
    border-radius: 8px;
    margin: 0 -8px;
}

.stats-list li:hover {
    background: rgba(255, 255, 255, 0.08);
    box-shadow: inset 0 0 10px rgba(255, 255, 255, 0.05);
}

.stats-list li:last-child {
    border-bottom: none;
}

.pref-rank {
    font-weight: bold;
    width: 24px;
    color: #64748b;
    font-family: var(--font-eng);
    font-variant-numeric: tabular-nums;
}

.pref-name {
    font-weight: bold;
    flex-grow: 1;
    color: #f8fafc;
}

.pref-val {
    color: #cbd5e1;
    font-family: var(--font-eng);
    text-align: right;
    font-variant-numeric: tabular-nums;
}

.pref-pct {
    color: #3b82f6;
    font-weight: bold;
    font-family: var(--font-eng);
    margin-left: 12px;
    width: 48px;
    text-align: right;
    font-variant-numeric: tabular-nums;
}

@keyframes pulseFlash {
    0% { box-shadow: 0 0 0 0 rgba(251, 191, 36, 0.7); background: rgba(30, 41, 59, 1.0); }
    50% { box-shadow: 0 0 30px 10px rgba(251, 191, 36, 0.3); background: rgba(50, 60, 80, 1.0); border-color: rgba(251, 191, 36, 0.8); }
    100% { box-shadow: 0 0 0 0 rgba(251, 191, 36, 0); background: rgba(30, 41, 59, 0.85); border-color: rgba(255, 255, 255, 0.1); }
}

.pulse-flash {
    animation: pulseFlash 0.6s ease-out;
}

/* Floating Navigation Bars */
.floating-nav {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    z-index: 1000;
    display: flex;
    gap: 8px;
    padding: 6px;
    background: rgba(15, 23, 42, 0.85);
    backdrop-filter: blur(12px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 40px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
    transition: all 0.3s ease;
}

.viewmode-nav {
    bottom: 25px;
}

.nationality-nav {
    bottom: 75px;
}

.view-btn {
    background: transparent;
    color: var(--text-secondary);
    border: none;
    padding: 8px 18px;
    border-radius: 30px;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.view-btn:hover {
    color: white;
    background: rgba(255, 255, 255, 0.05);
}

.view-btn.active {
    background: white;
    color: #0f172a;
    box-shadow: 0 4px 12px rgba(255, 255, 255, 0.2);
}

/* Heatmap Legend */
.heatmap-legend {
    position: absolute;
    bottom: 130px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 900;
    background: rgba(15, 23, 42, 0.7);
    padding: 10px 16px;
    border-radius: 12px;
    border: 1px solid rgba(255, 255, 255, 0.05);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    transition: all 0.4s ease;
}

.heatmap-legend.hidden {
    opacity: 0;
    transform: translate(-50%, 20px);
    pointer-events: none;
}

.legend-bar {
    width: 240px;
    height: 10px;
    border-radius: 5px;
    background: linear-gradient(to right, #ef4444, #334155, #10b981);
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.legend-labels {
    width: 100%;
    display: flex;
    justify-content: center;
    font-size: 0.75rem;
    color: var(--text-secondary);
    font-weight: 500;
}