:root {
    --bg-dark: #0a0b10;
    --panel-bg: rgba(25, 27, 38, 0.7);
    --panel-border: rgba(255, 255, 255, 0.1);
    --accent-primary: linear-gradient(135deg, #facc15 0%, #f59e0b 100%);
    --accent-secondary: linear-gradient(135deg, #fde047 0%, #eab308 100%);
    --text-main: #f8fafc;
    --text-dim: #94a3b8;
    --glass-blur: blur(16px);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

body {
    background-color: var(--bg-dark);
    background-image: 
        radial-gradient(circle at 20% 20%, rgba(250, 204, 21, 0.1) 0%, transparent 40%),
        radial-gradient(circle at 80% 80%, rgba(245, 158, 11, 0.1) 0%, transparent 40%);
    color: var(--text-main);
    font-family: 'Inter', sans-serif;
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow-x: hidden;
}

.app-container {
    width: 95%;
    max-width: 900px;
    padding: 40px 20px;
    padding-bottom: calc(40px + env(safe-area-inset-bottom));
}

header {
    text-align: center;
    margin-bottom: 40px;
    animation: fadeInDown 0.8s ease-out;
}

.main-title {
    font-family: 'Outfit', sans-serif;
    font-size: 3rem;
    font-weight: 800;
    letter-spacing: -1px;
    margin-bottom: 8px;
}

.main-title span {
    background: var(--accent-primary);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}

.subtitle {
    color: var(--text-dim);
    font-size: 1.1rem;
    font-weight: 300;
}

.glass-panel {
    background: var(--panel-bg);
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
    border: 1px solid var(--panel-border);
    border-radius: 24px;
    padding: 32px;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
    display: flex;
    flex-direction: column;
    gap: 15px;
}

.input-section {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    min-height: 50px;
}

.mode-select-buttons {
    display: flex;
    gap: 15px;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
}

.controls-group {
    display: flex;
    gap: 12px;
    justify-content: center;
    flex-wrap: wrap;
    animation: fadeIn 0.4s ease-out;
}

.controls-group.vertical {
    position: absolute;
    top: 95px;
    right: 15px;
    flex-direction: column;
    z-index: 20;
}

input[type="number"] {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--panel-border);
    border-radius: 12px;
    padding: 12px 24px;
    color: white;
    font-size: 1.0rem; /* Made smaller */
    font-family: 'Outfit', sans-serif;
    width: 280px; /* Made wider */
    outline: none;
    transition: all 0.3s ease;
}

input[type="number"]:focus {
    border-color: #facc15;
    box-shadow: 0 0 0 4px rgba(250, 204, 21, 0.2);
}

input::placeholder {
    font-size: 0.85rem;
    color: rgba(255, 255, 255, 0.3);
}

.action-btn {
    padding: 12px 24px;
    border: none;
    border-radius: 12px;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    font-family: 'Outfit', sans-serif;
    font-size: 1rem;
    white-space: nowrap;
}

.action-btn.primary {
    background: var(--accent-primary);
    color: white;
}

.action-btn.danger {
    background: linear-gradient(135deg, #ef4444 0%, #b91c1c 100%);
    color: white;
    box-shadow: 0 4px 15px rgba(185, 28, 28, 0.4);
}

.action-btn.danger:not(:disabled):hover {
    box-shadow: 0 10px 20px -10px rgba(185, 28, 28, 0.6);
    filter: brightness(1.1);
}

.action-btn.extreme {
    background: linear-gradient(135deg, #a855f7 0%, #6b21a8 100%);
    color: white;
    box-shadow: 0 4px 15px rgba(107, 33, 168, 0.4);
}

.action-btn.extreme:not(:disabled):hover {
    box-shadow: 0 10px 25px rgba(168, 85, 247, 0.5);
    filter: brightness(1.2);
}

.action-btn.secondary {
    background: var(--accent-secondary);
    color: white;
}

.action-btn.outline {
    background: transparent;
    border: 1px solid var(--panel-border);
    color: var(--text-dim);
}

.action-btn.toggle-btn {
    background: transparent;
    border: 1px solid var(--panel-border);
    color: var(--text-dim);
}

.action-btn.toggle-btn.active {
    background: rgba(250, 204, 21, 0.2);
    border-color: #facc15;
    color: white;
}

.action-btn:disabled {
    opacity: 0.2;
    cursor: not-allowed;
    transform: none !important;
    box-shadow: none !important;
}

@media (hover: hover) {
    .action-btn:not(:disabled):hover {
        transform: translateY(-2px);
    }
    .action-btn.primary:not(:disabled):hover {
        box-shadow: 0 10px 20px -10px rgba(245, 158, 11, 0.5);
    }
    .action-btn.secondary:not(:disabled):hover {
        box-shadow: 0 10px 20px -10px rgba(250, 204, 21, 0.5);
    }
}

/* Floating Icon Buttons */
.icon-btn {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    background: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: var(--text-main);
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    padding: 0;
}

.icon-btn svg {
    width: 31px;
    height: 31px;
    color: #facc15;
    transition: transform 0.3s ease;
}

@media (hover: hover) {
    .icon-btn:not(:disabled):hover {
        background: rgba(250, 204, 21, 0.2);
        border-color: #facc15;
        transform: scale(1.1);
        box-shadow: 0 0 15px rgba(250, 204, 21, 0.4);
    }
    .icon-btn:not(:disabled):hover svg {
        transform: scale(1.1);
    }
}

.icon-btn:not(:disabled):active {
    transform: scale(0.95);
    background: rgba(250, 204, 21, 0.3);
}

.icon-btn:disabled {
    opacity: 0.2;
    cursor: not-allowed;
    filter: grayscale(1);
}

.icon-btn.active {
    background: rgba(250, 204, 21, 0.3);
    border-color: #facc15;
    color: white;
}

.tool-options {
    position: absolute;
    top: 12px;
    left: 12px;
    right: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
    background: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    padding: 8px 16px;
    border-radius: 12px;
    border: 1px solid rgba(255, 255, 255, 0.05);
    z-index: 10;
}

.mode-selector {
    background: rgba(0, 0, 0, 0.2);
    padding: 4px;
    border-radius: 10px;
    display: flex;
    gap: 4px;
}

.mode-btn {
    background: transparent;
    border: none;
    padding: 8px 16px;
    border-radius: 8px;
    color: var(--text-dim);
    cursor: pointer;
    font-weight: 600;
    font-size: 0.9rem;
    transition: all 0.3s ease;
}

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


#viz-container {
    min-height: 450px;
    width: 100%;
    position: relative;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 20px;
    overflow: hidden; /* Prevent container itself from scrolling */
}

#viz-content {
    width: 100%;
    height: 100%;
    overflow: auto; /* Allow both X and Y scrolling for large trees */
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 100px 40px 40px 40px; 
    scrollbar-width: thin;
    scrollbar-color: rgba(255, 255, 255, 0.2) transparent;
}

/* Sudare-zan Styling */
.sudare-container {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    font-family: 'Outfit', sans-serif;
    font-size: 1.8rem;
}

.sudare-step {
    display: flex;
    align-items: baseline;
    gap: 15px;
    position: relative;
    padding-bottom: 5px;
}

.sudare-divisor {
    color: #a855f7;
    font-weight: 700;
    width: 40px;
    text-align: right;
    opacity: 0;
    transition: opacity 0.4s ease-out;
}

.sudare-step.active .sudare-divisor {
    opacity: 1;
}

.sudare-dividend-box {
    border-left: 2px solid transparent;
    border-bottom: 2px solid transparent;
    padding: 0 15px 5px 10px;
    min-width: 80px;
    transition: border-color 0.4s ease;
}

.sudare-step.active .sudare-dividend-box {
    border-color: var(--text-dim);
}

.sudare-dividend.final {
    color: #2dd4bf;
    font-weight: 800;
}

.sudare-final-quotient {
    margin-top: 5px;
    padding-left: 57px; /* Align with dividend box start */
    color: #2dd4bf;
    font-weight: 800;
}

/* Factor Tree Styling */
.tree-container {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    gap: 40px;
    width: max-content; /* Allow tree to determine its own width */
    position: relative;
    transition: padding 0.3s ease; /* Smooth transition for axis stabilization */
}

.tree-node {
    display: flex;
    flex-direction: column;
    align-items: center;
    position: relative;
    width: 0; /* PIVOT: Zero width makes the node a fixed point for its children */
    overflow: visible;
}

.node-value {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--panel-border);
    padding: 10px 20px;
    border-radius: 12px;
    font-size: 1.5rem;
    font-weight: 700;
    min-width: 60px;
    text-align: center;
    box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}

.node-value.prime {
    background: var(--accent-primary);
    border: none;
    color: white;
}

.tree-branches {
    display: block; /* Pure coordinate anchor */
    width: 0;
    margin: 0 auto;
    position: relative;
    margin-top: 30px;
    height: 100px; /* Maintain vertical flow for hierarchy */
}

.tree-node.left-child {
    position: absolute;
    right: 10px; /* Safe distance to prevent overlap */
    width: 60px;
}

.tree-branches > .tree-node.right-child {
    position: absolute;
    left: 10px; /* Safe distance to prevent overlap */
    width: 60px;
}

.branch-line-l, .branch-line-r {
    position: absolute;
    top: -30px;
    width: 50px; /* Pythagorean length: sqrt(40^2 + 30^2) */
    height: 2px;
    background: var(--panel-border);
}

.branch-line-l {
    right: 0px;
    transform-origin: top right;
    transform: rotate(-36.87deg); /* Pointing DOWN and LEFT */
}

.branch-line-r {
    left: 0px;
    transform-origin: top left;
    transform: rotate(36.87deg); /* Pointing DOWN and RIGHT */
}

.hint-panel {
    background: rgba(59, 130, 246, 0.1);
    border-left: 4px solid #3b82f6;
    padding: 16px;
    border-radius: 8px;
    display: flex;
    gap: 12px;
    align-items: center;
    animation: slideInRight 0.5s ease-out;
    margin-top: 0;
    margin-bottom: 5px;
}

.hint-icon {
    font-size: 1.5rem;
}

.hint-text {
    font-size: 0.95rem;
    line-height: 1.5;
}

.result-summary {
    background: rgba(168, 85, 247, 0.1);
    border-radius: 16px;
    padding: 20px;
    text-align: center;
    animation: fadeInUp 0.5s ease-out;
}

.result-label {
    color: var(--text-dim);
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 2px;
    margin-bottom: 8px;
}

.result-expression {
    font-family: 'Outfit', sans-serif;
    font-size: 2rem;
    font-weight: 700;
}


.hidden {
    display: none !important;
}

/* Animations */
@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes fadeInDown {
    from { opacity: 0; transform: translateY(-20px); }
    to { opacity: 1; transform: translateY(0); }
}

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

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

/* Responsive */
/* Sidebar Buttons */
.sidebar-buttons {
    position: fixed;
    top: 24px;
    right: 24px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    z-index: 1000;
}

.side-btn {
    width: 48px;
    height: 48px;
    background: var(--panel-bg);
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
    border: 1px solid var(--panel-border);
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    color: var(--text-main);
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    text-decoration: none;
}

.side-btn svg {
    width: 24px;
    height: 24px;
}

@media (hover: hover) {
    .side-btn.home:hover {
        background: rgba(250, 204, 21, 0.2);
        border-color: #facc15;
        transform: scale(1.1);
        color: #facc15;
        box-shadow: 0 0 15px rgba(250, 204, 21, 0.4);
    }
    .side-btn.challenge:hover {
        background: rgba(239, 68, 68, 0.2);
        border-color: #ef4444;
        transform: scale(1.1);
        color: #ef4444;
        box-shadow: 0 0 15px rgba(239, 68, 68, 0.4);
    }
}

.side-btn:active {
    transform: scale(0.9);
}

.side-btn.challenge.active {
    background: #ef4444;
    color: white;
}

/* Challenge Mode specific */
#challenge-mode {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 500px;
}

.challenge-header {
    background: rgba(0, 0, 0, 0.3);
    border-radius: 12px;
    padding: 12px;
    margin-bottom: 15px;
}

.prime-balls-pool {
    display: flex;
    justify-content: center;
    gap: 10px;
    flex-wrap: wrap;
}

.prime-ball {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    font-family: 'Outfit', sans-serif;
    font-weight: 800;
    font-size: 1.1rem;
    cursor: grab;
    user-select: none;
    box-shadow: 0 4px 6px rgba(0,0,0,0.3);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    touch-action: none; /* Required for custom dragging */
    z-index: 50;
}

.prime-ball:active { cursor: grabbing; }

/* Prime Colors */
.prime-2  { background: #ef4444; color: white; }
.prime-3  { background: #f97316; color: white; }
.prime-5  { background: #f59e0b; color: white; }
.prime-7  { background: #10b981; color: white; }
.prime-11 { background: #3b82f6; color: white; }
.prime-13 { background: #6366f1; color: white; }
.prime-17 { background: #8b5cf6; color: white; }
.prime-19 { background: #d946ef; color: white; }

/* 指数計算（累乗）状態のスタイル */
.prime-ball.is-power {
    box-shadow: 0 0 20px rgba(255, 255, 255, 0.6), 0 4px 10px rgba(0, 0, 0, 0.5);
    transform: scale(1.15);
    border: 2px solid white;
    z-index: 60;
}

.prime-ball.is-power:active {
    transform: scale(1.05);
}

.game-canvas-area {
    flex-grow: 1;
    position: relative;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 16px;
    border: 1px dashed rgba(255, 255, 255, 0.1);
    overflow: hidden;
    min-height: 350px;
}

.falling-number {
    position: absolute;
    padding: 15px 25px;
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(4px);
    border: 2px solid rgba(255, 255, 255, 0.2);
    border-radius: 30px;
    font-family: 'Outfit', sans-serif;
    font-weight: 800;
    font-size: 2.2rem;
    color: white;
    text-shadow: 0 2px 4px rgba(0,0,0,0.5);
    user-select: none;
    pointer-events: none; /* Interaction handled via hitbox check */
    box-shadow: 0 10px 25px rgba(0,0,0,0.3);
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    display: flex;
    align-items: center;
    justify-content: center;
}

.num-display {
    line-height: 1;
}

.falling-hint {
    position: absolute;
    top: 110%;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(4px);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 8px;
    padding: 6px 12px;
    color: #facc15;
    font-size: 0.8rem;
    font-weight: 600;
    white-space: normal;
    width: max-content;
    max-width: 150px;
    line-height: 1.3;
    z-index: 5;
    box-shadow: 0 4px 10px rgba(0,0,0,0.3);
    pointer-events: none;
    text-align: center;
}

/* v8.70: Edge-aware alignments */
.falling-hint.align-left {
    left: 0;
    transform: translateX(0);
    text-align: center;
}
.falling-hint.align-left::before {
    left: 20px;
    transform: none;
}

.falling-hint.align-right {
    left: 100%;
    transform: translateX(-100%);
    text-align: center;
}
.falling-hint.align-right::before {
    left: auto;
    right: 20px;
    transform: none;
}

.falling-hint::before {
    content: '';
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    border: 6px solid transparent;
    border-bottom-color: rgba(0, 0, 0, 0.6);
}

.error-countdown {
    position: absolute;
    bottom: 80%;
    left: 80%;
    background: #ef4444;
    color: white;
    padding: 6px 12px;
    border-radius: 50%;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2rem;
    font-weight: 800;
    box-shadow: 0 4px 12px rgba(239, 68, 68, 0.4);
    border: 2px solid white;
    animation: popIn 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    z-index: 10;
}

.error-countdown::after {
    content: '';
    position: absolute;
    top: 80%;
    right: 80%;
    border: 6px solid transparent;
    border-top-color: #ef4444;
    transform: rotate(45deg);
}

@keyframes popIn {
    0% { transform: scale(0); }
    100% { transform: scale(1); }
}

.falling-number.oni-number {
    border-color: rgba(239, 68, 68, 0.5);
    box-shadow: 0 0 20px rgba(239, 68, 68, 0.3);
    color: #fecaca;
    font-size: 2.4rem;
}

.falling-number.hit {
    animation: bounceScale 0.4s ease-out;
    border-color: #facc15;
}

@keyframes bounceScale {
    0% { transform: scale(1); }
    50% { transform: scale(1.3); }
    100% { transform: scale(1); }
}

.challenge-footer {
    display: flex;
    flex-direction: column;
    gap: 10px;
    width: 100%;
}

.challenge-formulas-container {
    display: flex;
    gap: 15px;
    width: 100%;
}

.challenge-formula {
    flex: 1;
    background: rgba(0,0,0,0.4);
    border-radius: 12px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    padding: 12px;
    min-height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: 'Outfit', sans-serif;
    font-weight: 700;
    font-size: 1.1rem;
    color: #facc15;
}

.game-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.7);
    backdrop-filter: blur(8px);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 100;
    border-radius: 16px;
    padding: 20px;
    text-align: center;
    touch-action: manipulation; /* Enable taps while blocking zooms */
}

.game-overlay h2 {
    font-size: 2.5rem;
    margin-bottom: 20px;
    font-family: 'Outfit', sans-serif;
}

.game-overlay.win h2 { color: #2dd4bf; }
.game-overlay.fail h2 { color: #ef4444; }

.drag-ghost {
    position: fixed;
    pointer-events: none;
    z-index: 9999;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
}

/* Challenge Mode Global Optimization */
body.challenge-active {
    align-items: flex-start;
    overflow: hidden !important;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    touch-action: none; /* Block all browser touch gestures like pull-to-refresh */
}

.challenge-active header {
    position: absolute;
    top: 12px;
    left: 20px;
    z-index: 100;
    margin-bottom: 0;
}

.challenge-active .main-title {
    font-size: 0.85rem;
    opacity: 0.6;
    letter-spacing: 0.05em;
}

.challenge-active .subtitle {
    display: none;
}

.challenge-active .app-container {
    padding-top: 15px;
    padding-bottom: 10px;
    max-width: 1100px;
    width: 95%;
}

.challenge-active .glass-panel {
    padding: 12px 16px;
    gap: 0;
}

.challenge-active .challenge-header {
    margin: 0;
    margin-bottom: 10px;
    background: rgba(255, 255, 255, 0.02);
    padding: 5px;
}

.challenge-active .game-canvas-area {
    min-height: 400px;
    height: calc(100vh - 180px);
    height: calc(100dvh - 180px);
    position: relative;
}

.challenge-hint-btn {
    position: absolute;
    top: 10px;
    left: 10px;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(8px);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: white;
    font-weight: bold;
    cursor: pointer;
    z-index: 100;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

.challenge-hint-btn:hover {
    background: rgba(255, 255, 255, 0.2);
}

.challenge-hint-btn.active {
    background: rgba(250, 204, 21, 0.2);
    border-color: rgba(250, 204, 21, 0.5);
    color: #facc15;
    box-shadow: 0 0 15px rgba(250, 204, 21, 0.3);
}

.challenge-chain-btn {
    position: absolute;
    top: 52px;
    left: 10px;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: white;
    font-size: 1.1rem;
    cursor: pointer;
    z-index: 100;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

.challenge-chain-btn svg {
    width: 20px;
    height: 20px;
    stroke: white;
    transform: translateZ(0);
}

.challenge-chain-btn.active {
    background: rgba(239, 68, 68, 0.2);
    border-color: rgba(239, 68, 68, 0.5);
    box-shadow: 0 0 15px rgba(239, 68, 68, 0.4);
    animation: glowPulse 2s infinite;
}

.challenge-formula.chain-active {
    background: linear-gradient(135deg, #f97316, #ef4444);
    color: white;
    padding: 4px 15px;
    border-radius: 20px;
    font-size: 0.9rem;
    font-weight: 800;
    letter-spacing: 0;
    box-shadow: 0 4px 10px rgba(239, 68, 68, 0.3);
    border: 2px solid rgba(255, 255, 255, 0.3);
    animation: popWobble 3s ease-in-out infinite;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin-right: 10px;
}

.challenge-formula.chain-active.oni-active {
    background: linear-gradient(135deg, #7f1d1d, #450a0a) !important;
    border-color: #ef4444 !important;
    box-shadow: 0 0 15px rgba(239, 68, 68, 0.4) !important;
}

.game-overlay.oni-chain-result {
    background: linear-gradient(135deg, rgba(69, 10, 10, 0.95), rgba(15, 23, 42, 0.95)) !important;
}

@keyframes popWobble {
    0%, 100% { transform: scale(1) translateY(0); }
    50% { transform: scale(1.03) translateY(-2px); }
}

.challenge-formula.chain-hightlight {
    color: #facc15;
    font-size: 1.6rem;
    font-weight: 800;
    text-shadow: 0 0 15px rgba(250, 204, 21, 0.4);
}

.challenge-formula.chain-hightlight span {
    font-size: 0.8rem;
    color: white;
    margin-left: 5px;
    font-weight: 400;
}

.challenge-formula.chain-active.message-pop {
    color: white;
    font-size: 1.0rem;
    animation: messagePop 0.4s ease-out;
}

@keyframes messagePop {
    0% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.2); opacity: 1; color: #facc15; }
    100% { transform: scale(1); opacity: 1; }
}

.result-chain-main {
    font-size: 3.5rem;
    font-weight: 800;
    color: #facc15;
    text-shadow: 0 0 20px rgba(250, 204, 21, 0.5);
    margin: 5px 0;
    animation: scaleIn 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.result-stats-row {
    display: flex;
    justify-content: center;
    gap: 40px;
    margin: 20px 0;
    animation: scaleIn 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.result-stat-item {
    text-align: center;
}

.stat-label {
    font-size: 0.8rem;
    color: #94a3b8;
    margin-bottom: 5px;
    text-transform: uppercase;
    letter-spacing: 1px;
}

.stat-value {
    font-size: 2.4rem;
    font-weight: 800;
    line-height: 1;
}

.stat-value.success {
    color: #facc15;
    text-shadow: 0 0 15px rgba(250, 204, 21, 0.4);
}

.stat-value.error {
    color: #ef4444;
}

.result-rank-box {
    background: rgba(255, 255, 255, 0.05);
    padding: 10px 20px;
    border-radius: 12px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    gap: 10px;
}

.result-rank-title {
    font-size: 1.4rem;
    font-weight: 700;
}

.rank-newbie { color: #94a3b8; }
.rank-rookie { color: #22c55e; }
.rank-pro { color: #0ea5e9; font-weight: 700; }
.rank-expert { color: #3b82f6; text-shadow: 0 0 10px rgba(59, 130, 246, 0.4); }
.rank-master { color: #facc15; text-shadow: 0 0 15px rgba(250, 204, 21, 0.5); }
.rank-wizard { color: #a855f7; text-shadow: 0 0 15px rgba(168, 85, 247, 0.5); }
.rank-god {
    background: linear-gradient(to right, #ef4444, #f59e0b, #10b981, #3b82f6, #8b5cf6, #ef4444);
    background-size: 200% auto;
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
    animation: rainbowCycle 2s linear infinite;
    font-size: 1.8rem;
    font-weight: 900;
}

@keyframes scaleIn {
    from { transform: scale(0.5); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

@keyframes glowPulse {
    0% { transform: scale(1); box-shadow: 0 0 15px rgba(239, 68, 68, 0.4); }
    50% { transform: scale(1.1); box-shadow: 0 0 25px rgba(239, 68, 68, 0.6); }
    100% { transform: scale(1); box-shadow: 0 0 15px rgba(239, 68, 68, 0.4); }
}

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

.spawn-line {
    position: absolute;
    width: 100%;
    height: 0;
    border-top: 2px dashed rgba(255, 255, 255, 0.3);
    z-index: 5;
    pointer-events: none;
    transition: top 0.5s cubic-bezier(0.4, 0, 0.2, 1);
}

.spawn-line::after {
    content: 'NEXT SPAWN';
    position: absolute;
    right: 10px;
    bottom: 5px;
    font-size: 0.6rem;
    color: rgba(255, 255, 255, 0.5);
    letter-spacing: 1px;
}

@media (max-width: 600px) {
    .sidebar-buttons {
        top: 8px;
        right: 8px;
        gap: 6px;
    }
    .side-btn {
        width: 34px;
        height: 34px;
    }
    .side-btn svg {
        width: 16px;
        height: 16px;
    }
    
    .prime-ball {
        width: 36px;
        height: 36px;
        font-size: 0.9rem;
    }

    .falling-number {
        font-size: 1.8rem;
        padding: 10px 20px;
    }
    
    .app-container { padding: 20px 10px; }
    .glass-panel { padding: 20px 12px; }
    
    .main-title { font-size: 1.8rem; }
    .subtitle { font-size: 0.9rem; }
    
    .input-section { gap: 8px; }
    input[type="number"] { width: 180px; font-size: 1.1rem; padding: 10px 15px; }
    .action-btn { padding: 10px 12px; font-size: 0.9rem; }
    
    .tool-options { padding: 6px; scale: 0.9; }
    
    /* Shrink numbers for mobile */
    .falling-number {
        font-size: 1.6rem; 
        padding: 5px 12px;
    }
    .oni-number {
        font-size: 1.5rem;
        padding: 8px 15px;
    }
    .node-value { padding: 8px 15px; font-size: 1.2rem; min-width: 50px; }
    .sudare-container { font-size: 1.4rem; }
    .sudare-divisor { width: 30px; }
    .sudare-dividend-box { min-width: 60px; }
    .result-expression { font-size: 1.5rem; }
    
    .challenge-formula { font-size: 0.85rem; padding: 8px; }
    
    /* Tree-related mobile layout upkeep */
    #viz-content { padding-right: 20px; padding-left: 20px; padding-top: 80px; }
    #viz-container { height: 500px; }
    .controls-group.vertical { right: 8px; scale: 0.75; top: 90px; }

    /* Result Screen Tweaks */
    .game-overlay h2 { font-size: 1.8rem; margin-bottom: 10px; }
    .result-chain-main { font-size: 2.2rem; margin: 4px 0; }
    .result-extra-stats { font-size: 0.85rem; margin-bottom: 10px; }
    .result-rank-box { padding: 8px 15px; margin-bottom: 10px; }
    .result-rank-title { font-size: 1.2rem; }
    .result-label { margin-top: 5px; }
    .game-overlay .action-btn { padding: 10px 20px; font-size: 0.95rem; }
}

.version-tag {
    position: fixed;
    top: 12px;
    left: 15px;
    font-size: 0.75rem;
    color: rgba(255, 255, 255, 0.25);
    font-family: 'Inter', sans-serif;
    letter-spacing: 0.5px;
    pointer-events: none;
    z-index: 2000;
}

.game-bonus-text {
    position: absolute;
    pointer-events: none;
    z-index: 2100;
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 120px;
    animation: bonusRise 1.2s cubic-bezier(0.18, 0.89, 0.32, 1.28) forwards;
    white-space: nowrap;
}

.bonus-main {
    font-weight: 800;
    font-size: 1.8rem;
    letter-spacing: 1px;
    line-height: 1;
}

.bonus-sub {
    font-size: 0.9rem;
    font-weight: 700;
    margin-top: 5px;
    background: rgba(0, 0, 0, 0.5);
    padding: 2px 10px;
    border-radius: 20px;
    backdrop-filter: blur(4px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: #fff;
}

.game-bonus-text.nice { color: #facc15; text-shadow: 0 0 15px rgba(250, 204, 21, 0.8); }
.game-bonus-text.great { color: #8b5cf6; text-shadow: 0 0 15px rgba(139, 92, 246, 0.8); }
.game-bonus-text.excellent { color: #ec4899; text-shadow: 0 0 20px rgba(236, 72, 153, 0.8); font-size: 2.2rem; }
.game-bonus-text.clear { color: #ffffff; text-shadow: 0 0 15px #facc15, 0 0 30px #f59e0b; }
.game-bonus-text.clear .bonus-sub { color: #facc15; border-color: rgba(250, 204, 21, 0.3); }

.game-bonus-text.nice {
    color: #facc15;
}

.game-bonus-text.great {
    font-size: 1.4rem;
    color: #fbbf24;
    text-shadow: 0 0 15px rgba(251, 191, 36, 0.6);
    filter: drop-shadow(0 0 2px white);
}

.game-bonus-text.excellent {
    font-size: 1.8rem;
    background: linear-gradient(
        to right, 
        #ff0000, #ff7f00, #ffff00, #00ff00, #0000ff, #4b0082, #8f00ff
    );
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
    text-shadow: none;
    filter: drop-shadow(0 0 8px rgba(255, 255, 255, 0.6));
    animation: bonusRise 0.8s ease-out forwards, rainbowCycle 1.5s linear infinite;
}

@keyframes rainbowCycle {
    0% { filter: hue-rotate(0deg) drop-shadow(0 0 5px rgba(255, 255, 255, 0.5)); }
    100% { filter: hue-rotate(360deg) drop-shadow(0 0 5px rgba(255, 255, 255, 0.5)); }
}

@keyframes bonusRise {
    0% {
        transform: translateY(0);
        opacity: 0;
    }
    20% {
        opacity: 1;
    }
    100% {
        transform: translateY(-40px);
        opacity: 0;
    }
}

/* v6.92: Mode-specific formula panel styles */
.challenge-formula.mode-normal { 
    background: rgba(59, 130, 246, 0.15); 
    border: 1px solid rgba(59, 130, 246, 0.3);
    color: #e2e8f0;
}
.challenge-formula.mode-oni { 
    background: rgba(239, 68, 68, 0.15); 
    border: 1px solid rgba(239, 68, 68, 0.3);
    color: #fee2e2;
}
.challenge-formula.mode-extreme { 
    background: rgba(168, 85, 247, 0.2); 
    border: 1px solid rgba(168, 85, 247, 0.4);
    color: #f5f3ff;
    box-shadow: inset 0 0 15px rgba(168, 85, 247, 0.1);
}

/* v6.93: Ensure Chain Mode badge takes' precedence when active */
.challenge-formula.chain-active {
    background: linear-gradient(135deg, #f97316, #ef4444) !important;
    box-shadow: 0 4px 10px rgba(239, 68, 68, 0.3) !important;
    border: 2px solid rgba(255, 255, 255, 0.3) !important;
}

/* Onboarding Tooltip */
.onboarding-tooltip {
    position: fixed;
    background: linear-gradient(135deg, #facc15 0%, #f59e0b 100%);
    color: #000;
    padding: 12px 18px;
    border-radius: 14px;
    font-size: 0.9rem;
    font-weight: 700;
    box-shadow: 0 10px 30px rgba(245, 158, 11, 0.5);
    z-index: 3000;
    max-width: 220px;
    animation: tooltipFloat 2.5s ease-in-out infinite, fadeIn 0.4s ease-out;
    pointer-events: auto;
    cursor: default;
    border: 2px solid rgba(255, 255, 255, 0.4);
    line-height: 1.4;
    font-family: 'Outfit', sans-serif;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
}

.onboarding-tooltip::after {
    content: none; /* Default to no arrow */
    position: absolute;
    border: 10px solid transparent;
}

.onboarding-tooltip.arrow-right::after {
    content: '';
    top: 50%;
    left: 100%;
    transform: translateY(-50%);
    border-left-color: #f59e0b;
}

.onboarding-tooltip.arrow-top::after {
    content: '';
    top: auto;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    border-color: transparent transparent #f59e0b transparent;
}

.onboarding-tooltip svg {
    width: 32px;
    height: 32px;
    display: inline-block;
    vertical-align: middle;
    stroke: currentColor;
    fill: none;
    margin-right: 4px;
}

@keyframes tooltipFloat {
    0%, 100% { transform: translate(0, 0); }
    50% { transform: translate(-10px, 0); }
}

.onboarding-close-btn {
    display: inline-block;
    margin-top: 8px;
    background: rgba(0, 0, 0, 0.8);
    color: #fff;
    border: none;
    border-radius: 8px;
    padding: 5px 14px;
    font-family: inherit;
    font-weight: 800;
    font-size: 0.8rem;
    cursor: pointer;
    transition: all 0.2s;
    text-align: center;
    width: 100%;
}

.onboarding-close-btn:hover {
    background: #000;
    transform: translateY(-1px);
}

@media (max-width: 600px) {
    .onboarding-tooltip {
        max-width: 200px;
        font-size: 0.9rem;
        padding: 10px 15px;
    }
    .onboarding-tooltip::after {
        border-width: 8px;
    }
}

.onboarding-tooltip.arrow-bottom::after {
    content: '';
    top: 100%;
    left: 50%;
    transform: translateX(-50%);
    border-color: #f59e0b transparent transparent transparent;
}

@keyframes fadeOut {
    from { opacity: 1; transform: scale(1); }
    to { opacity: 0; transform: scale(0.9); }
}

.fadeOut {
    animation: fadeOut 0.4s ease-in forwards !important;
}
