/* =============================================================================
   Wikiすごろく - iframe-based Wikipedia Game
   ============================================================================= */

:root {
    --ui-neutral: #78716c;
    --color-error: #b91c1c;
    --color-info: #1d4ed8;
    --color-warning: #f59e0b;
    --color-success: #10b981;
    --font-sans: 'Inter', 'Noto Sans JP', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    --text-xs: 0.75rem;
    --text-sm: 0.875rem;
    --radius-lg: 0.5rem;
    --radius-full: 9999px;
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
}

[data-player="red"] { --player-color: #b91c1c; --player-bg: #fef2f2; --player-border: #fca5a5; }
[data-player="blue"] { --player-color: #1d4ed8; --player-bg: #eff6ff; --player-border: #93c5fd; }

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
button { font: inherit; }

body {
    font-family: var(--font-sans);
    color: #18181b;
    background: #f8fafc;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    height: 100dvh;
    overflow: hidden;
}

/* =============================================================================
   Setup Screen
   ============================================================================= */

.setup-screen {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100dvh;
    background: #fafaf9;
}

.setup-container {
    max-width: 520px;
    width: 90%;
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 3rem;
}

.setup-title {
    font-size: clamp(2.5rem, 7vw, 4rem);
    font-weight: 400;
    letter-spacing: 0.05em;
    line-height: 1.1;
    color: #1c1917;
}

.title-jp {
    font-weight: 400;
}

/* --- Difficulty Selector --- */
.mode-selector {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.mode-option {
    cursor: pointer;
    font-size: var(--text-sm);
    font-weight: 400;
    color: #a1a1aa;
    transition: color 0.2s;
    border: none;
    background: transparent;
    padding: 0.25rem 0;
}

.mode-option input { display: none; }

.mode-option:hover {
    color: #71717a;
}

.mode-option:has(input:checked) {
    color: #18181b;
}

.mode-sep {
    color: #d4d4d8;
    font-size: var(--text-sm);
    user-select: none;
}

/* --- Start Button --- */
.btn-start {
    display: inline-block;
    background: transparent;
    color: #71717a;
    border: 1px solid #d4d4d8;
    padding: 0.625rem 2rem;
    border-radius: 4px;
    font-size: var(--text-sm);
    font-weight: 400;
    letter-spacing: 0.05em;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-start:hover {
    border-color: #18181b;
    color: #18181b;
}

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

/* --- Endless Option --- */
.endless-option {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.8rem;
    color: #a1a1aa;
    cursor: pointer;
    transition: color 0.15s;
}

.endless-option:hover { color: #71717a; }
.endless-option:has(input:checked) { color: var(--ui-neutral); }

.endless-option input[type="checkbox"] {
    accent-color: var(--ui-neutral);
    width: 14px;
    height: 14px;
    margin: 0;
}

/* --- Entrance Animation --- */
@keyframes fadeUp {
    from { opacity: 0; transform: translateY(12px); }
    to { opacity: 1; transform: translateY(0); }
}

.fade-in {
    opacity: 0;
    animation: fadeUp 0.5s ease-out both;
    animation-delay: calc(var(--delay, 0) * 0.1s);
}

.progress-text {
    margin-top: 0.75rem;
    text-align: center;
    color: #71717a;
    font-size: var(--text-sm);
}

/* =============================================================================
   Game Screen (two-column)
   ============================================================================= */

.game-screen {
    display: none;
    height: 100dvh;
}

.game-screen.active {
    display: grid;
    grid-template-columns: 1fr 6px 340px;
}

.resizer {
    background: #e4e4e7;
    cursor: col-resize;
    transition: background 0.15s;
    z-index: 10;
}
.resizer:hover, .resizer.active {
    background: var(--ui-neutral);
}

/* --- Wiki Panel (left) --- */
.wiki-panel {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    border-right: 1px solid #e4e4e7;
}

.wiki-header {
    display: flex;
    flex-direction: column;
    background: white;
    border-bottom: 1px solid #e4e4e7;
    flex-shrink: 0;
}

.header-main {
    display: flex;
    align-items: center;
    gap: 0.625rem;
    padding: 0.5rem 1rem;
}

.turn-badge {
    font-size: var(--text-xs);
    font-weight: 700;
    padding: 0.25rem 0.625rem;
    border-radius: var(--radius-full);
    white-space: nowrap;
    flex-shrink: 0;
}

.turn-badge[data-player] {
    background: var(--player-bg);
    color: var(--player-color);
    border: 1.5px solid var(--player-border);
}

.header-title-group {
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: baseline;
    gap: 0.5rem;
}

.current-pos-badge {
    font-size: 1rem;
    font-weight: 700;
    color: #18181b;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    letter-spacing: -0.01em;
}

.nav-status {
    font-size: var(--text-xs);
    color: #a1a1aa;
    white-space: nowrap;
    flex-shrink: 0;
}

.nav-status.blocked { color: var(--color-error); font-weight: 600; }
.nav-status.scoring { color: var(--color-warning); font-weight: 700; }

.turns-left {
    font-size: var(--text-xs);
    color: #71717a;
    font-weight: 600;
    white-space: nowrap;
    flex-shrink: 0;
    background: #f4f4f5;
    padding: 0.2rem 0.5rem;
    border-radius: var(--radius-full);
}

.header-action {
    overflow: hidden;
    transition: max-height 0.25s ease, opacity 0.2s ease;
    max-height: 0;
    opacity: 0;
}

.header-action.visible {
    max-height: 60px;
    opacity: 1;
}

.btn-confirm {
    display: block;
    width: calc(100% - 2rem);
    margin: 0 1rem 0.5rem;
    background: var(--color-success);
    color: white;
    border: none;
    padding: 0.625rem 1rem;
    border-radius: var(--radius-lg);
    font-size: var(--text-sm);
    font-weight: 700;
    cursor: pointer;
    transition: background 0.2s, box-shadow 0.2s, transform 0.15s;
    white-space: nowrap;
    text-align: center;
}

.btn-confirm:hover {
    background: #059669;
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

.btn-confirm:active {
    transform: translateY(0);
}

/* Scoring article: gold/amber highlight */
.btn-confirm.scoring-cta {
    background: linear-gradient(135deg, #f59e0b, #d97706);
}

.btn-confirm.scoring-cta:hover {
    background: linear-gradient(135deg, #d97706, #b45309);
}

.wiki-frame {
    flex: 1;
    width: 100%;
    border: none;
    background: white;
}

/* --- Game Panel (right) --- */
.game-panel {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    overflow-y: auto;
    overscroll-behavior-y: contain;
    touch-action: pan-y;
    scrollbar-gutter: stable;
    background: white;
    padding: 1rem 1rem 0;
    gap: 1rem;
}

.score-row {
    display: flex;
    gap: 0.5rem;
}

.score-card {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 0.75rem;
    border-radius: var(--radius-lg);
    border: none;
    background: var(--player-bg);
    transition: all 0.3s;
}

.score-card.current { box-shadow: var(--shadow-md); transform: scale(1.03); }

.score-icon { font-size: 1.25rem; color: var(--player-color); }
.score-label { font-size: var(--text-xs); font-weight: 600; color: #52525b; }
.score-num { font-size: 2.5rem; font-weight: 800; margin-left: auto; color: var(--player-color); }

.panel-section h3 {
    font-size: var(--text-sm);
    font-weight: 600;
    color: #71717a;
    margin-bottom: 0.5rem;
}

/* --- Board (compact 3x3) --- */
.board-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.5rem;
}

.board-card {
    background: #fafaf9;
    border: 1px solid #d4d4d8;
    border-radius: 8px;
    padding: 0.375rem;
    text-align: center;
    transition: all 0.3s;
    position: relative;
    min-height: 56px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 1px;
}

.board-card .article-name {
    font-size: 0.7rem;
    font-weight: 600;
    color: #18181b;
    word-break: break-all;
    line-height: 1.2;
    text-decoration: none;
}

.board-card .article-name:hover { color: var(--ui-neutral); }

.board-card .genre-label {
    font-size: 0.6rem;
    color: #a1a1aa;
    line-height: 1.1;
}

.board-card .point-badge {
    position: absolute;
    top: -5px;
    right: -5px;
    width: 24px;
    height: 24px;
    border-radius: var(--radius-full);
    background: var(--color-warning);
    color: white;
    font-size: 0.7rem;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
}

.board-card.start { border-color: var(--player-border); background: var(--player-bg); }
.board-card.start .point-badge { display: none; }

.board-card .start-label { font-size: 0.55rem; font-weight: 600; color: var(--player-color); }

.board-card.owned { border-color: var(--player-color); background: var(--player-bg); }
.board-card.owned .point-badge { background: var(--player-color); }

.board-card .pos-indicator {
    position: absolute;
    bottom: -4px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 10px;
    animation: bounce 0.6s ease infinite alternate;
}

@keyframes bounce {
    from { transform: translateX(-50%) translateY(0); }
    to { transform: translateX(-50%) translateY(-2px); }
}

/* --- Timeline (side-by-side grid) --- */
.tl-header {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    gap: 0.25rem;
    margin-bottom: 0.25rem;
}

.tl-label {
    font-size: var(--text-xs);
    font-weight: 700;
    text-align: center;
    padding: 0.125rem 0;
}

.tl-label[data-player] { color: var(--player-color); }

.timeline-grid {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    max-height: 260px;
    overflow-y: auto;
    overscroll-behavior-y: contain;
}

.tl-row {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    gap: 0.25rem;
    align-items: stretch;
}

.tl-turn {
    font-size: 0.6rem;
    font-weight: 700;
    color: #a1a1aa;
    display: flex;
    align-items: center;
    padding: 0 0.25rem;
    white-space: nowrap;
}

.tl-cell {
    font-size: 0.7rem;
    padding: 0.25rem 0.375rem;
    border-radius: 4px;
    word-break: break-all;
    line-height: 1.3;
    min-height: 1.5rem;
}

.tl-cell strong { font-weight: 700; }
.tl-cell[data-player] { background: var(--player-bg); color: var(--player-color); }
.tl-cell.scored { font-weight: 600; }
.tl-cell.empty { color: #d4d4d8; text-align: center; background: #fafafa; }

/* --- Result --- */
.result-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 100;
    align-items: center;
    justify-content: center;
}

.result-overlay.active { display: flex; }

.result-card {
    background: white;
    border-radius: 1rem;
    padding: 2.5rem;
    text-align: center;
    box-shadow: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);
    max-width: 400px;
    width: 90%;
    animation: resultIn 0.4s ease-out;
}

@keyframes resultIn {
    from { transform: scale(0.8); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

.result-card h2 { font-size: 1.875rem; font-weight: 700; margin-bottom: 1rem; }
.result-card .final-score { font-size: 1.25rem; color: #52525b; margin-bottom: 1.5rem; }

.panel-footer {
    position: sticky;
    bottom: 0;
    padding: 0.5rem 0;
    background: white;
    border-top: 1px solid #f4f4f5;
    margin-top: auto;
    flex-shrink: 0;
}

.btn-newgame {
    display: block;
    width: 100%;
    background: #f4f4f5;
    color: #52525b;
    border: 1px solid #e4e4e7;
    padding: 0.5rem;
    border-radius: 0.375rem;
    font-size: var(--text-sm);
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s;
}

.btn-newgame:hover { background: #e4e4e7; color: #18181b; }

.btn-replay {
    background: var(--ui-neutral);
    color: white;
    border: none;
    padding: 0.75rem 2rem;
    border-radius: 0.75rem;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-replay:hover { background: #57534e; }

/* --- Responsive --- */

/* Tablet (≤1024px) */
@media (max-width: 1024px) {
    .game-screen.active {
        grid-template-columns: 1fr 6px 280px;
    }

    .game-panel {
        padding: 0.75rem;
        gap: 0.75rem;
    }

    .score-num { font-size: 1.5rem; }
    .score-card { padding: 0.375rem 0.5rem; }

    .board-card { min-height: 48px; padding: 0.25rem; }
    .board-card .article-name { font-size: 0.65rem; }
    .board-card .genre-label { font-size: 0.55rem; }

    .tl-cell { font-size: 0.65rem; padding: 0.2rem 0.3rem; }
    .tl-turn { font-size: 0.55rem; }
    .timeline-grid { max-height: 200px; }

}

/* Mobile (≤768px) */
@media (max-width: 768px) {
    body { height: 100dvh; overflow: hidden; }

    .game-screen.active {
        grid-template-columns: 1fr;
        grid-template-rows: 50dvh 6px 1fr;
        height: 100dvh;
    }

    .wiki-panel {
        height: 100%;
        min-height: 0;
        border-right: none;
        border-bottom: none;
        overflow: hidden;
    }

    .wiki-frame {
        overflow-y: auto;
        overscroll-behavior-y: contain;
    }

    .resizer {
        cursor: row-resize;
        height: 6px;
        width: 100%;
        touch-action: none;
    }

    .game-panel {
        height: 100%;
        min-height: 0;
    }

    .header-main {
        padding: 0.375rem 0.75rem;
        gap: 0.5rem;
    }

    .current-pos-badge { font-size: var(--text-sm); }

    .btn-confirm {
        width: calc(100% - 1.5rem);
        margin: 0 0.75rem 0.375rem;
    }

    .board-grid {
        overflow-x: auto;
        min-width: 0;
    }

    .score-row { gap: 0.375rem; }
    .score-num { font-size: 1.125rem; }
    .score-icon { font-size: 1rem; }

    .panel-section h3 { font-size: var(--text-xs); margin-bottom: 0.375rem; }

    .timeline-grid { max-height: 220px; }

    .result-card { padding: 2rem 1.5rem; }
    .result-card h2 { font-size: 1.5rem; }
}

/* Small mobile (≤480px) */
@media (max-width: 480px) {
    .game-screen.active {
        grid-template-rows: 45dvh 6px 1fr;
    }

    .header-main {
        padding: 0.25rem 0.5rem;
        gap: 0.375rem;
    }

    .turn-badge { font-size: 0.65rem; padding: 0.2rem 0.5rem; }
    .current-pos-badge { font-size: var(--text-xs); font-weight: 600; }
    .nav-status { font-size: 0.6rem; }

    .btn-confirm {
        font-size: var(--text-xs);
        padding: 0.375rem 0.75rem;
        width: calc(100% - 1rem);
        margin: 0 0.5rem 0.25rem;
    }

    .game-panel { padding: 0.5rem; gap: 0.5rem; }

    .score-card { padding: 0.3rem 0.4rem; gap: 0.375rem; }
    .score-num { font-size: 1rem; }
    .score-label { font-size: 0.65rem; }
    .score-icon { font-size: 0.875rem; }

    .board-grid { gap: 0.25rem; }
    .board-card { min-height: 42px; padding: 0.2rem; }
    .board-card .article-name { font-size: 0.6rem; }
    .board-card .genre-label { font-size: 0.5rem; }
    .board-card .point-badge { width: 16px; height: 16px; font-size: 0.5rem; top: -4px; right: -4px; }

    .tl-cell {
        font-size: 0.6rem;
        padding: 0.175rem 0.25rem;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    .tl-turn { font-size: 0.5rem; }
    .tl-header .tl-label { font-size: 0.65rem; }
    .timeline-grid { max-height: 180px; }

    .btn-start { padding: 0.5rem 1.5rem; }

    .result-card { padding: 1.5rem 1rem; }
    .result-card h2 { font-size: 1.25rem; }
    .result-card .final-score { font-size: 1rem; }
    .btn-replay { padding: 0.625rem 1.5rem; font-size: var(--text-sm); }
}

/* Accessibility: respect reduced motion preference */
@media (prefers-reduced-motion: reduce) {
    .fade-in {
        animation: none;
        opacity: 1;
    }

    .board-card .pos-indicator {
        animation: none;
    }

    .result-card {
        animation: none;
    }
}
