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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: #f1f5f9;
    color: #1e293b;
    padding-bottom: 80px;
}

header {
    background: #1e3a8a;
    color: white;
    padding: 16px 20px;
    font-size: 18px;
    font-weight: 700;
    letter-spacing: 0.02em;
}

#mode-tabs {
    display: flex;
    padding: 14px 16px 0;
    max-width: 480px;
    margin: 0 auto;
    gap: 0;
}

.tab-btn {
    flex: 1;
    padding: 10px;
    border: none;
    background: #e2e8f0;
    color: #64748b;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.12s;
}
.tab-btn:first-child { border-radius: 8px 0 0 8px; }
.tab-btn:last-child  { border-radius: 0 8px 8px 0; }
.tab-btn.active {
    background: #1d4ed8;
    color: white;
}

/* ── Game context ── */
#game-context {
    display: none;
    max-width: 480px;
    margin: 10px auto 0;
    padding: 12px 16px;
    background: white;
    border-radius: 10px;
    box-shadow: 0 1px 4px rgba(0,0,0,0.08);
    /* margin-left/right same as #mode-tabs */
    margin-left: 16px;
    margin-right: 16px;
}
#game-context.visible { display: block; }
#win-rows { display: none; }
#win-rows.visible { display: block; }

.ctx-row {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 8px;
}
.ctx-row.hidden { display: none; }
.ctx-row:last-child { margin-bottom: 0; }

.ctx-label {
    font-size: 12px;
    color: #64748b;
    width: 52px;
    flex-shrink: 0;
}

.ctx-btns {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
}

.ctx-btn {
    min-width: 28px;
    height: 28px;
    padding: 0 6px;
    border: 1.5px solid #e2e8f0;
    border-radius: 6px;
    background: white;
    color: #64748b;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.12s;
}
.ctx-btn.active {
    background: #1d4ed8;
    border-color: #1d4ed8;
    color: white;
}

.score-ctrl {
    display: flex;
    align-items: center;
    gap: 10px;
}
.score-step {
    width: 32px; height: 32px;
    border: 1.5px solid #e2e8f0;
    border-radius: 8px;
    background: white;
    color: #475569;
    font-size: 18px;
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: all 0.12s;
}
.score-step:hover { border-color: #94a3b8; }
#score-display {
    font-size: 16px;
    font-weight: 700;
    color: #1e293b;
    min-width: 36px;
    text-align: center;
}

#graph {
    padding: 16px 16px 24px;
}

.graph-inner {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    max-width: 480px;
    margin: 0 auto;
    gap: 0;
}

/* ── Connector ── */
.connector {
    text-align: center;
    color: #94a3b8;
    font-size: 20px;
    padding: 6px 0;
    line-height: 1;
}

/* ── Action sections ── */
.action-section {
    border-radius: 14px;
    padding: 14px;
    margin-top: 8px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.action-0 {
    background: #eff6ff;
    border: 1.5px solid #bfdbfe;
}

.action-1 {
    background: #fff7ed;
    border: 1.5px solid #fed7aa;
}

.action-2 {
    background: #ecfdf5;
    border: 1.5px solid #bbf7d0;
}

.action-label {
    font-size: 16px;
    font-weight: 700;
    text-align: center;
    padding: 6px 12px;
    border-radius: 8px;
}

.action-0 .action-label {
    background: #dbeafe;
    color: #1d4ed8;
}

.action-1 .action-label {
    background: #ffedd5;
    color: #c2410c;
}

.action-2 .action-label {
    background: #dcfce7;
    color: #15803d;
}

/* ── Node ── */
.node {
    background: white;
    border-radius: 12px;
    padding: 18px;
    width: 100%;
    box-shadow: 0 1px 4px rgba(0,0,0,0.08);
    position: relative;
    padding-top: 78px;
}

.node-title {
    position: absolute;
    top: 16px;
    left: 18px;
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 0.07em;
    text-transform: uppercase;
    margin-bottom: 0;
}
.before-node .node-title { color: #1d4ed8; }
.after-node  .node-title { color: #15803d; }

.node-metric {
    position: absolute;
    top: 34px;
    right: 18px;
    text-align: right;
}

.node-metric-label {
    font-size: 10px;
    color: #94a3b8;
    line-height: 1.1;
    margin-bottom: 3px;
}

.node-metric-value {
    font-size: 24px;
    font-weight: 800;
    line-height: 1;
    color: #0f172a;
    letter-spacing: -0.03em;
}

.field-label {
    font-size: 11px;
    color: #94a3b8;
    margin-bottom: 6px;
}

/* Outs */
.outs-row {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
}
.out-btn {
    width: 28px; height: 28px;
    border-radius: 50%;
    border: 2px solid #e2e8f0;
    background: white;
    cursor: pointer;
    padding: 0;
    user-select: none;
}
.out-btn.active {
    background: #ef4444;
    border-color: #ef4444;
    box-shadow: 0 0 0 5px rgba(239, 68, 68, 0.12);
}
.out-btn.animate { animation: btn-pop 0.25s ease-out; }
.out-btn:hover {
    border-color: #f87171;
}

@keyframes btn-pop {
    0%   { transform: scale(1); }
    40%  { transform: scale(1.5); }
    100% { transform: scale(1); }
}

.base.base-pop {
    animation: base-pop 0.25s ease-out;
}

@keyframes base-pop {
    0%   { transform: scale(1); }
    40%  { transform: scale(1.3); }
    100% { transform: scale(1); }
}

/* ── Diamond ── */
/*
  3ベースを密着配置（ホーム・ラベルなし）

  Base: 58×36px  half=(29,18)
  方向(1,-1)/√2 での各ベースのリーチ ≈ 15.7px
  中心間距離 ≈ 35px → 隙間 約3px

  3B center: (28, 70) → left=-1, top=52
  2B center: (53, 45) → left=24, top=27
  1B center: (78, 70) → left=49, top=52
  Container: 110×92px
*/
.diamond-wrap {
    width: 220px; height: 184px;
    position: relative;
    margin: 0 auto 6px;
}

.base {
    width: 116px; height: 72px;
    position: absolute;
    cursor: pointer;
}

/* Border layer */
.base::before {
    content: '';
    position: absolute; inset: 0;
    clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
    background: #cbd5e1;
    transition: background 0.12s;
}

/* Fill layer */
.base::after {
    content: '';
    position: absolute; inset: 6px 10px;
    clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
    background: #f1f5f9;
    transition: background 0.12s;
}

.base.on::before { background: #f59e0b; }
.base.on::after  { background: #fbbf24; }

/* Base positions (all coords ×2) */
.b-3b { left: -20px; top: 104px; }
.b-2b { left:  48px; top:  54px; }
.b-1b { left: 116px; top: 104px; }

/* After node wrapper */
.after-wrapper { position: relative; }

.score-pills {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.score-pill {
    min-width: 36px;
    height: 32px;
    border: 1.5px solid #cbd5e1;
    border-radius: 999px;
    background: white;
    color: #475569;
    font-size: 13px;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.12s;
}

.score-pill:hover {
    border-color: #60a5fa;
    color: #1d4ed8;
}

.score-pill.active {
    background: #1d4ed8;
    border-color: #1d4ed8;
    color: white;
}

/* ── Connector (vertical arrow between before and actions) ── */
.edge {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 2px 0;
}

.arrow-line {
    width: 2px;
    height: 20px;
    background: #cbd5e1;
    position: relative;
}
.arrow-line::after {
    content: '';
    position: absolute;
    bottom: -6px; left: -4px;
    border: 5px solid transparent;
    border-top: 7px solid #cbd5e1;
}

/* ── Simulate bar ── */
#result-sheet {
    --result-peek: 26px;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    z-index: 50;
    pointer-events: none;
}

#result-panel {
    width: min(100%, 560px);
    pointer-events: auto;
}

#result-panel {
    background: rgba(255, 255, 255, 0.96);
    backdrop-filter: blur(14px);
    border: 1px solid rgba(226, 232, 240, 0.96);
    border-bottom: none;
    border-radius: 22px 22px 0 0;
    box-shadow: 0 -14px 34px rgba(15, 23, 42, 0.16);
    overflow: hidden;
    max-height: min(50vh, 420px);
    transform: translateY(calc(100% - var(--result-peek)));
    transition: transform 0.28s ease, box-shadow 0.28s ease;
}

#result-sheet.open #result-panel {
    transform: translateY(0);
    box-shadow: 0 -18px 42px rgba(15, 23, 42, 0.22);
}

#result-panel.dragging {
    transition: none;
}

#result-handle {
    width: 100%;
    border: none;
    background: transparent;
    padding: 12px 18px 10px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    color: #334155;
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    cursor: grab;
    touch-action: none;
}

#result-handle:active {
    cursor: grabbing;
}

.result-grabber {
    width: 52px;
    height: 6px;
    border-radius: 999px;
    background: #cbd5e1;
}

#sim-bar {
    width: 100%;
    pointer-events: auto;
    padding: 0;
    padding-bottom: max(0px, env(safe-area-inset-bottom));
}

#sim-btn {
    display: block;
    width: 100%;
    border: none;
    padding: 20px;
    font-size: 18px;
    font-weight: 800;
    letter-spacing: 0.06em;
    cursor: pointer;
    color: white;
    background: linear-gradient(90deg,
        #1d4ed8 0%,
        #6d28d9 25%,
        #db2777 50%,
        #1d4ed8 75%,
        #6d28d9 100%
    );
    background-size: 300% 100%;
    animation: shimmer 3s linear infinite;
    position: relative;
    overflow: hidden;
    box-shadow: 0 -4px 24px rgba(109, 40, 217, 0.35);
    transition: opacity 0.12s, box-shadow 0.12s;
}

#sim-btn::before {
    content: '';
    position: absolute;
    top: -50%;
    left: -60%;
    width: 40%;
    height: 200%;
    background: rgba(255, 255, 255, 0.22);
    transform: skewX(-20deg);
    animation: glare 3s ease-in-out infinite;
}

#sim-btn:hover {
    opacity: 0.92;
    box-shadow: 0 -6px 32px rgba(109, 40, 217, 0.5);
}

#sim-btn:active {
    opacity: 0.85;
}

@keyframes shimmer {
    0%   { background-position: 100% 0; }
    100% { background-position: -100% 0; }
}

@keyframes glare {
    0%   { left: -60%; }
    40%  { left: 120%; }
    100% { left: 120%; }
}

#sim-status {
    display: none;
}

#result {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    font-size: 14px;
    color: #334155;
    line-height: 1.65;
    padding: 0 18px 22px;
    overflow: auto;
    max-height: calc(min(50vh, 420px) - 54px);
}
#result.has-result { color: #0f172a; }

.result-summary {
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.result-heading {
    margin: 0;
    padding: 14px 16px;
    border-radius: 16px;
    background: linear-gradient(135deg, #dbeafe 0%, #eff6ff 52%, #fff7ed 100%);
    color: #0f172a;
    font-size: 24px;
    line-height: 1.35;
    font-weight: 800;
    box-shadow: inset 0 0 0 1px rgba(59, 130, 246, 0.14);
}

.result-details {
    margin: 0;
    padding-left: 22px;
    color: #334155;
}

.result-details li {
    margin-bottom: 8px;
}

.result-details li:last-child {
    margin-bottom: 0;
}

@media (max-width: 640px) {
    #result-panel {
        width: 100%;
        border-left: none;
        border-right: none;
        border-radius: 20px 20px 0 0;
    }
}
