:root {
    --primary: #38bdf8;
    --primary-dark: #0ea5e9;
    --secondary: #ec4899;
    --bg-dark: #0f172a;
    --bg-darker: #020617;
    --card-bg: #1e293b;
    --text-main: #f8fafc;
    --text-dim: #94a3b8;
    --accent: #facc15;
    --dist-line: #a855f7; /* New Purple for P-Q distance/relationship */
    --glass: rgba(255, 255, 255, 0.03);
    --glass-border: rgba(255, 255, 255, 0.1);
    
    /* Prediction Line Colors (Specification 2.2) */
    --pred-p: #3b82f6; /* Blue for P's future path */
    --pred-q: #ef4444; /* Red for Q's future path */
    --pred-white: #ffffff; /* White for PQ relation */
}

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

body {
    background-color: var(--bg-dark);
    color: var(--text-main);
    font-family: 'Noto Sans JP', sans-serif;
    line-height: 1.6;
    overflow-x: hidden;
    min-height: 100vh;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
}

input, select, textarea {
    user-select: auto;
    -webkit-user-select: auto;
}

.app-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

/* Header */
header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 0;
}

.logo-section h1 {
    font-size: 1.8rem;
    font-weight: 900;
    color: var(--primary);
}

.btn-back {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    background: var(--glass);
    border: 1px solid var(--glass-border);
    color: var(--text-main);
    border-radius: 50%;
    text-decoration: none;
    transition: all 0.3s;
}

.btn-back svg {
    width: 22px;
    height: 22px;
}

.btn-back:hover {
    background: rgba(255, 255, 255, 0.1);
}

.btn-outline:hover {
    background: rgba(255, 255, 255, 0.05);
}

/* Simulation HUD (Bottom Left) */
.sim-hud {
    position: absolute;
    bottom: 12px;
    left: 12px;
    display: flex;
    flex-direction: row; /* Default horizontal for PC */
    flex-wrap: wrap; 
    gap: 10px;
    pointer-events: none;
    z-index: 10;
}

/* PC HUD Layout */
@media (min-width: 769px) {
    .is-circle-layout .sim-hud {
        left: auto;
        right: 12px;
        bottom: auto;
        top: 50%;
        transform: translateY(-50%);
        flex-direction: column;
        gap: 6px;
    }
}

.hud-line {
    background: rgba(15, 23, 42, 0.7);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 6px;
    padding: 4px 10px;
    font-family: 'Outfit', sans-serif;
    font-size: 0.8rem;
    font-weight: 600;
    color: rgba(255, 255, 255, 0.9);
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    white-space: nowrap;
}

.hud-line span {
    font-size: 0.9rem;
    color: #fff;
    margin: 0 2px;
}

.hud-p { border-left: 3px solid var(--primary); }
.hud-q { border-left: 3px solid var(--secondary); }
.hud-extra { border-left: 3px solid var(--dist-line); }

@media (max-width: 768px) {
    .sim-hud {
        bottom: 12px;
        left: 12px;
        flex-direction: column;
        align-items: flex-start;
        gap: 4px;
    }
    .hud-group-q {
        display: flex;
        flex-direction: row;
        gap: 4px;
    }
    .hud-line {
        padding: 3px 8px;
        font-size: 0.7rem;
    }
    .hud-line span {
        font-size: 0.8rem;
    }
}

/* Layout Grid */
.layout-grid {
    display: grid;
    grid-template-columns: 1fr 320px;
    gap: 12px;
    flex: 1;
}

@media (max-width: 768px) {
    :root { font-size: 14px; }
    .app-container { padding: 8px; gap: 8px; }
    header { padding: 5px 12px; margin-bottom: 3px; }
    header h1 { font-size: 1.1rem !important; }
    .layout-grid { display: flex !important; flex-direction: column !important; gap: 8px !important; }
    .sidebar { order: -1 !important; width: 100% !important; }
    .card { padding: 10px 12px; }
    .sidebar-header { margin-bottom: 10px; padding-bottom: 8px; }
    .sidebar-header h2 { font-size: 0.95rem; }
    .mode-description { font-size: 0.8rem; margin-bottom: 3px; line-height: 1.4; }
    .canvas-wrapper { width: 100%; height: 340px !important; background: var(--bg-darker); border-radius: 12px; position: relative; overflow: visible; }

    .hud-overlay {
        position: absolute !important;
        top: 5px !important;
        left: 5px !important;
        right: 5px !important;
        display: grid !important;
        grid-template-columns: 1fr 1fr !important;
        gap: 2px !important;
        padding: 0 !important;
        width: auto !important;
        pointer-events: none;
        z-index: 10;
    }

    .hud-item {
        background: rgba(15, 23, 42, 0.75);
        padding: 2px 6px;
        border-radius: 3px;
        font-size: 0.7rem;
        display: flex;
        justify-content: space-between;
        align-items: center;
        border: 1px solid rgba(255, 255, 255, 0.05);
        line-height: 1.2;
    }
}

/* Simulation Area */
.main-column {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.card {
    background: #1e293b;
    border: 1px solid rgba(255, 255, 255, 0.05);
    border-radius: 12px;
    padding: 15px 20px;
    height: auto;
    position: relative;
    overflow: hidden;
}

.card h2 {
    font-size: 1.1rem;
    color: var(--text-dim);
    margin-bottom: 15px;
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 8px;
}

.sim-header-flex {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
}

.title-with-icon {
    display: flex;
    align-items: center;
    gap: 8px;
}

/* Floating Controls (Global) */
.sim-floating-controls {
    display: flex;
    position: absolute;
    bottom: 15px;
    right: 15px;
    gap: 12px;
    z-index: 100;
}

.btn-icon-sim {
    background: rgba(15, 23, 42, 0.7);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border: 1px solid var(--glass-border);
    color: var(--primary);
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    padding: 0;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.4);
}

.btn-icon-sim svg {
    width: 20px;
    height: 20px;
}

.btn-icon-sim:hover {
    background: var(--primary);
    color: #fff;
    transform: scale(1.1);
    box-shadow: 0 0 20px rgba(56, 189, 248, 0.4);
    border-color: var(--primary);
}

.btn-icon-sim:active {
    transform: scale(0.95);
}

#btn-reset-mobile {
    color: var(--text-dim);
}

#btn-reset-mobile:hover {
    color: #fff;
    background: #64748b;
    border-color: #64748b;
    box-shadow: 0 0 20px rgba(100, 116, 139, 0.4);
}

.canvas-wrapper {
    width: 100%;
    height: 380px; /* Increased for geometry simulation */
    background: var(--bg-darker);
    border-radius: 12px;
    position: relative;
    overflow: visible;
}

/* Tooltip & HUD Overlay */
.custom-tooltip {
    position: fixed;
    z-index: 9999;
    pointer-events: auto;
    background: #0f172a; /* Explicit bg-dark for reliability */
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 12px;
    padding: 16px;
    width: 280px;
    box-shadow: 0 20px 50px -12px rgba(0, 0, 0, 0.8);
    color: #f8fafc;
    font-size: 0.9rem;
    line-height: 1.6;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    opacity: 0;
    transform: translateY(10px) scale(0.95);
    visibility: hidden;
    cursor: grab;
    touch-action: none;
    will-change: top, left;
    user-select: none;
}

.custom-tooltip.active {
    opacity: 1;
    transform: translateY(0) scale(1);
    visibility: visible;
}

.custom-tooltip.dragging {
    transition: none !important;
    opacity: 0.9;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.6);
    cursor: grabbing;
}

.tooltip-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
    padding-bottom: 8px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

/* Color Rules for Tooltip States - Restored Meaningful Palette (Specification 3.5) */
.custom-tooltip.approaching {
    border: 2px solid #ef4444; /* Tension/Danger Red */
    box-shadow: 0 0 20px rgba(239, 68, 68, 0.3);
}
.custom-tooltip.approaching .tooltip-title { color: #ef4444; }
.custom-tooltip.approaching .tooltip-badge { border-color: #ef4444; color: #ef4444; background: rgba(239, 68, 68, 0.1); }

.custom-tooltip.separating {
    border: 2px solid #10b981; /* Safety Emerald Green */
    box-shadow: 0 0 20px rgba(16, 185, 129, 0.3);
}
.custom-tooltip.separating .tooltip-title { color: #10b981; }
.custom-tooltip.separating .tooltip-badge { border-color: #10b981; color: #10b981; background: rgba(16, 185, 129, 0.1); }

.custom-tooltip.metric-area {
    border: 2px solid #facc15;
    box-shadow: 0 0 20px rgba(250, 204, 21, 0.3);
}
.custom-tooltip.metric-area .tooltip-title { color: #facc15; }
.custom-tooltip.metric-area .tooltip-badge { border-color: #facc15; color: #facc15; background: rgba(250, 204, 21, 0.1); }

.custom-tooltip.event-stop {
    border: 2px solid var(--accent);
    box-shadow: 0 0 20px rgba(234, 179, 8, 0.3);
}
.custom-tooltip.event-stop .tooltip-title { color: var(--accent); }
.custom-tooltip.event-stop .tooltip-badge { 
    border-color: var(--accent); 
    color: var(--accent); 
    background: rgba(234, 179, 8, 0.1); 
}

/* Highlights */
.hl-a { color: var(--primary); font-weight: bold; }
.hl-b { color: var(--secondary); font-weight: bold; }
.hl-dist { color: var(--dist-line); font-weight: bold; }
.hl-time { color: #fff; font-weight: bold; }

/* Graph Interaction Hint */
.graph-hint {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(56, 189, 248, 0.25);
    backdrop-filter: blur(8px);
    border: 1px solid rgba(56, 189, 248, 0.5);
    padding: 10px 20px;
    border-radius: 30px;
    color: #38bdf8;
    font-size: 0.95rem;
    font-weight: bold;
    pointer-events: none;
    z-index: 1000;
    box-shadow: 0 0 20px rgba(56, 189, 248, 0.4);
    animation: floatHint 2.5s ease-in-out infinite;
    text-align: center;
}

@keyframes floatHint {
    0%, 100% { transform: translate(-50%, -50%); }
    50% { transform: translate(-50%, -65%); }
}

.tooltip-close {
    background: none;
    border: none;
    color: var(--text-dim);
    font-size: 1.4rem;
    cursor: pointer;
    line-height: 1;
    padding: 4px;
    border-radius: 50%;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    margin-top: -8px; /* Offset to align with badge */
    margin-right: -4px;
}

.tooltip-close:hover {
    color: var(--text-main);
    background: rgba(255, 255, 255, 0.1);
}

.tooltip-badge {
    display: inline-block;
    padding: 2px 8px;
    background: rgba(56, 189, 248, 0.1);
    border: 1px solid var(--primary);
    border-radius: 4px;
    font-size: 0.7rem;
    font-weight: 700;
    color: var(--primary);
    text-transform: uppercase;
    margin-bottom: 8px;
    letter-spacing: 0.05em;
}

.tooltip-title {
    display: block;
    font-weight: 700;
    font-size: 0.85rem; /* Reduced from 1rem */
    margin-bottom: 8px;
    color: var(--accent);
}

.tooltip-content {
    font-size: 0.85rem;
    color: var(--text-main);
    margin-bottom: 10px;
}

.math-breakdown {
    margin-top: 10px;
    padding: 10px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 6px;
    border-left: 3px solid var(--accent);
    font-family: 'Outfit', sans-serif;
    font-size: 0.85rem;
    line-height: 1.6;
}

.math-step {
    display: inline-block;
    color: var(--accent);
    font-weight: bold;
    margin-top: 8px;
    margin-bottom: 4px;
    border-bottom: 1px solid rgba(234, 179, 8, 0.3);
}

.prediction-box {
    margin-top: 15px;
    padding: 12px;
    background: linear-gradient(135deg, rgba(79, 70, 229, 0.15), rgba(124, 58, 237, 0.15));
    border-radius: 8px;
    border-left: 4px solid #6366f1;
    font-size: 0.85rem;
    line-height: 1.6;
    animation: slideIn 0.3s ease-out;
}

.prediction-badge {
    display: inline-block;
    background: #6366f1;
    color: white;
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 0.7rem;
    font-weight: bold;
    margin-bottom: 6px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

@keyframes slideIn {
    from { opacity: 0; transform: translateY(5px); }
    to { opacity: 1; transform: translateY(0); }
}

.prediction-formula {
    margin-top: 10px;
    padding: 8px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 4px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    font-family: 'JetBrains Mono', 'Roboto Mono', monospace;
    font-size: 0.8rem;
    font-weight: 600;
    color: #fff;
    text-align: center;
}

/* Multi-event Swipe UI (v1.2.0) */
.prediction-swipe-container {
    margin-top: 15px;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    padding-top: 12px;
}

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

.swipe-btn {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: #fff;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 0.9rem;
    transition: all 0.2s;
    font-family: "Outfit", sans-serif;
}

.swipe-btn:hover {
    background: rgba(255, 255, 255, 0.15);
    border-color: var(--primary);
}

.swipe-btn:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

.swipe-dots {
    display: flex;
    gap: 6px;
}

.swipe-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.2);
    transition: all 0.3s;
}

.swipe-dot.active {
    background: var(--primary);
    transform: scale(1.3);
    box-shadow: 0 0 8px var(--primary);
}

.prediction-swipe-content {
    min-height: 80px;
    position: relative;
    overflow: hidden;
}

/* Adjusting prediction box for container */
.prediction-swipe-content .prediction-box {
    margin-top: 0;
    animation: swipeFadeIn 0.4s ease-out;
}

@keyframes swipeFadeIn {
    from { opacity: 0; transform: translateX(10px); }
    to { opacity: 1; transform: translateX(0); }
}

.tooltip-formula {
    display: block;
    margin-top: 8px;
    padding-top: 8px;
    border-top: 1px solid var(--glass-border);
    font-family: "Outfit", sans-serif;
    font-weight: 600;
    color: var(--primary);
}

/* Ripple Animation for Points */
@keyframes ripple {
    0% {
        transform: scale(1);
        opacity: 0.8;
    }
    100% {
        transform: scale(3.5);
        opacity: 0;
    }
}

.ripple-active::after {
    content: "";
    position: absolute;
    width: 20px;
    height: 20px;
    border: 2px solid #fff;
    border-radius: 50%;
    animation: ripple 1.5s infinite;
}

.hud-overlay {
    position: absolute;
    top: 10px;
    left: 20px;
    right: 20px;
    background: rgba(15, 23, 42, 0.7);
    backdrop-filter: blur(8px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 10px;
    padding: 8px 25px;
    z-index: 100;
    pointer-events: none;
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    align-items: center;
    gap: 15px;
}

.hud-val {
    font-family: 'JetBrains Mono', 'Roboto Mono', monospace !important;
    font-size: 1.125rem; /* 18px */
    font-weight: 700;
    text-align: right;
    min-width: 3.5rem;
}

/* Controls */
.sidebar .sidebar-content,
.sidebar .control-group {
    max-height: 2000px; /* 十分な高さ */
    opacity: 1;
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    overflow: hidden;
}

.sidebar.collapsed .sidebar-content,
.sidebar.collapsed .control-group {
    max-height: 0;
    opacity: 0;
    margin-top: 0;
    margin-bottom: 0;
    padding-top: 0;
    padding-bottom: 0;
    pointer-events: none;
}

.sidebar.collapsed {
    gap: 0;
    padding-bottom: 5px;
}

.sidebar-header {
    cursor: pointer;
    user-select: none;
    display: flex;
    justify-content: space-between;
    align-items: center;
    transition: opacity 0.2s;
}

.sidebar-header:hover {
    opacity: 0.8;
}

.toggle-icon {
    font-family: 'Outfit', sans-serif;
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--primary);
    transition: transform 0.3s ease;
}

.sidebar.collapsed .toggle-icon {
    transform: rotate(0deg);
}

.control-group label {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    margin-bottom: 6px;
    font-size: 0.9rem;
    font-weight: 500;
}

.group-p label { color: var(--primary); }
.group-q label { color: var(--secondary); }

.control-group input[type="range"] {
    width: 100%;
    cursor: pointer;
    accent-color: var(--primary);
}

.group-q input[type="range"] { accent-color: var(--secondary); }

select {
    width: 100%;
    padding: 10px 35px 10px 12px;
    background-color: #0a0f1d;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='%2338bdf8'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M19 9l-7 7-7-7'%3E%3C/path%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 10px center;
    background-size: 18px;
    border: 1px solid var(--primary);
    border-radius: 8px;
    color: #fff;
    appearance: none;
    cursor: pointer;
    transition: border-color 0.3s;
}

select:focus {
    outline: none;
    border-color: #fff;
}

#start-q { border-color: var(--secondary); }
#mode-select { border-color: var(--dist-line); }
#shape-select, #area-basis-select { border-color: var(--accent); }

.action-btns {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-top: 25px;
}

.btn {
    padding: 12px;
    border: none;
    border-radius: 10px;
    font-weight: 700;
    cursor: pointer;
    font-size: 1rem;
}

.btn-primary { background: var(--primary); color: white; }
.btn-outline { background: transparent; border: 1px solid var(--glass-border); color: var(--text-main); }

.mode-description {
    background: rgba(56, 189, 248, 0.1);
    border-left: 4px solid var(--primary);
    padding: 12px;
    border-radius: 4px;
    font-size: 0.9rem;
    margin-bottom: 20px;
}

.hidden { display: none; }

.controls-locked .control-group {
    opacity: 0.6;
    pointer-events: none;
    transition: opacity 0.3s ease;
}

.controls-locked select, 
.controls-locked input {
    cursor: not-allowed;
}

/* Toggle Switch Styles */
.switch {
    position: relative;
    display: inline-block;
    width: 40px;
    height: 20px;
}
.switch input { 
    opacity: 0;
    width: 0;
    height: 0;
}
.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #ccc;
    transition: .4s;
}
.slider:before {
    position: absolute;
    content: "";
    height: 16px;
    width: 16px;
    left: 2px;
    bottom: 2px;
    background-color: white;
    transition: .4s;
    border-radius: 50%;
}
input:checked + .slider {
    background-color: var(--primary);
}
input:checked + .slider:before {
    transform: translateX(20px);
}
.slider.round {
    border-radius: 20px;
}

/* Segmented Control UI (P-Direction) */
.segmented-control {
    display: flex;
    width: 100%;
    background: rgba(15, 23, 42, 0.8);
    border: 1px solid var(--glass-border);
    border-radius: 10px;
    padding: 3px;
    margin-top: 8px;
    gap: 2px;
    box-sizing: border-box;
}

.segment-item {
    flex: 1;
    position: relative;
    cursor: pointer;
    text-align: center;
    user-select: none;
    display: flex;
}

.segment-item input[type="radio"] {
    position: absolute;
    opacity: 0;
    width: 0;
    height: 0;
}

.segment-item span {
    display: block;
    width: 100%;
    padding: 8px 0;
    font-size: 0.85rem;
    font-weight: 700;
    color: var(--text-dim);
    border-radius: 8px;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.segment-item input[type="radio"]:checked + span {
    background: var(--primary);
    color: #fff;
    box-shadow: 0 4px 10px rgba(56, 189, 248, 0.25);
}

.segment-item:hover span:not(input:checked + span) {
    background: rgba(255, 255, 255, 0.08);
    color: #fff;
}
.hud-event {
    position: absolute;
    bottom: 60px;
    left: 20px;
    right: 20px;
    background: rgba(2, 6, 23, 0.85);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border: 1px solid var(--accent);
    border-radius: 12px;
    padding: 15px;
    color: #fff;
    font-size: 0.9rem;
    line-height: 1.6;
    z-index: 150;
    pointer-events: none;
    opacity: 0;
    transform: translateY(10px);
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: 0 10px 30px -5px rgba(0, 0, 0, 0.5);
}

.hud-event:not(:empty) {
    opacity: 1;
    transform: translateY(0);
}
