:root {
    --bg-gradient: radial-gradient(circle at center, #5d4037 0%, #281812 100%); /* 夕暮れの書斎 */
    --paper-color: #f4e4bc; /* 古紙のような色 */
    --paper-shadow: rgba(0, 0, 0, 0.5);
    --ink-color: #3e2723;
    --ink-error-color: #c0392b;
    --text-muted: #8d6e63;
    --primary-color: #d4af37; /* アンティークゴールド */
    --ui-border-color: #8d6e63;
    --font-handwriting: 'Zen Kurenaido', sans-serif; /* 手書き風日本語フォント */
    --font-ui: 'IM Fell English SC', serif; /* アンティークなUIフォント */
}

body {
    margin: 0;
    padding: 0;
    font-family: var(--font-handwriting);
    background: var(--bg-gradient);
    color: #e0e0e0;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
}

#app {
    width: 100%;
    height: 100%;
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

/* 画面切り替え用 */
.screen {
    display: none;
    width: 100%;
    height: 100%;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: absolute;
    top: 0;
    left: 0;
    padding: 20px;
    box-sizing: border-box;
}

.screen.active {
    display: flex;
}

.hidden {
    display: none !important;
}

/* コンテンツボックス (スタート画面・リザルト画面用) */
.content-box {
    background-color: rgba(30, 20, 15, 0.9);
    padding: 20px 40px;
    border-radius: 8px;
    text-align: center;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.8), 0 0 20px rgba(212, 175, 55, 0.1);
    border: 3px double var(--primary-color);
    max-width: 500px;
    width: 100%;
    font-family: var(--font-ui);
}

/* 幅広のコンテンツボックス（設定画面用） */
.content-box.wide-box {
    max-width: 900px;
    width: 95%;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
}

h1, h2, h3 {
    margin-bottom: 20px;
    font-weight: normal;
    letter-spacing: 2px;
    color: var(--primary-color);
    text-shadow: 0 2px 4px rgba(0,0,0,0.5);
}

.form-group {
    margin-bottom: 20px;
    text-align: left;
}

label {
    display: block;
    margin-bottom: 8px;
    font-size: 1.1rem;
    color: var(--primary-color);
}

input[type="text"],
input[type="number"] {
    width: 100%;
    padding: 12px;
    border: none;
    border-radius: 4px;
    font-size: 1rem;
    box-sizing: border-box;
    font-family: inherit;
    background-color: rgba(255, 255, 255, 0.1);
    border: 1px solid var(--ui-border-color);
    color: #fff;
}

input[type="text"]:focus,
input[type="number"]:focus {
    border-color: var(--primary-color);
    outline: none;
    box-shadow: 0 0 8px rgba(212, 175, 55, 0.3);
}

.btn-primary {
    background-color: var(--primary-color);
    color: white;
    border: none;
    padding: 12px 30px;
    font-size: 1.1rem;
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.2s, transform 0.1s;
    font-family: inherit;
    font-family: var(--font-ui);
    letter-spacing: 1px;
    box-shadow: 0 4px 10px rgba(0,0,0,0.3);
    border: 1px solid rgba(255,255,255,0.2);
}

.btn-primary:hover {
    background-color: #b8860b;
    transform: translateY(-2px);
    box-shadow: 0 6px 15px rgba(212, 175, 55, 0.4);
}

/* セカンダリボタン (Solo Play, Backなど) */
.btn-secondary {
    background-color: transparent;
    color: var(--text-muted);
    border: 1px solid var(--text-muted);
    padding: 10px 25px;
    font-size: 1rem;
    border-radius: 4px;
    cursor: pointer;
    font-family: var(--font-ui);
    transition: all 0.2s;
    margin-top: 10px;
}

.btn-secondary:hover {
    background-color: rgba(255, 255, 255, 0.05);
    color: #e0e0e0;
    border-color: #e0e0e0;
}

/* 区切り線 */
.divider {
    display: flex;
    align-items: center;
    text-align: center;
    margin: 15px 0;
    color: var(--primary-color);
    font-family: var(--font-ui);
    font-size: 0.9rem;
}

.divider::before,
.divider::after {
    content: '';
    flex: 1;
    border-bottom: 1px solid rgba(212, 175, 55, 0.3);
}

.divider span {
    padding: 0 10px;
}

/* お題選択リスト */
.question-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-bottom: 20px;
    height: auto;
    flex: 1; /* 利用可能な高さを埋める */
    width: 100%;
    overflow-y: auto;
    overflow-x: hidden;
    padding-right: 10px; /* スクロールバーとの隙間 */
    box-sizing: border-box;
}

.question-item {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--ui-border-color);
    color: var(--primary-color);
    padding: 15px;
    cursor: pointer;
    font-family: var(--font-ui);
    font-size: 1.2rem;
    transition: all 0.2s;
    text-align: left;
    width: 100%;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.question-info {
    font-size: 0.9rem;
    color: var(--text-muted);
    font-family: var(--font-handwriting);
}

.question-item:hover {
    background: rgba(212, 175, 55, 0.1);
    transform: translateX(5px);
    border-color: var(--primary-color);
}

/* 設定画面レイアウト */
.setup-container {
    display: flex;
    flex-direction: column;
    gap: 20px;
    margin-bottom: 20px;
    flex: 1; /* 親要素の残りの高さを埋める */
    min-height: 0; /* 縮小を許可 */
    overflow-y: auto; /* モバイル時は全体スクロール */
}

@media (min-width: 768px) {
    .setup-container {
        flex-direction: row;
        align-items: stretch;
        overflow: hidden; /* PC時はコンテナ自体のスクロールを無効化 */
    }
    
    .setup-left, .setup-right {
        flex: 1;
        display: flex;
        flex-direction: column;
        min-height: 0; /* 子要素のスクロールを有効にするため */
    }
    
    .setup-left {
        border-right: 1px solid rgba(212, 175, 55, 0.3);
        padding-right: 20px;
    }
    
    .setup-right {
        padding-left: 20px;
    }
}

.settings-panel {
    text-align: left;
    background: rgba(0, 0, 0, 0.2);
    padding: 20px;
    border-radius: 4px;
    height: auto;
    flex: 1; /* 利用可能な高さを埋める */
    box-sizing: border-box;
    overflow-y: auto;
}

.setting-item {
    margin-bottom: 0px;
}

.checkbox-label {
    display: flex;
    align-items: center;
    cursor: pointer;
    font-size: 1.2rem;
    color: var(--primary-color);
}

input[type="checkbox"] {
    appearance: none;
    -webkit-appearance: none;
    width: 20px;
    height: 20px;
    background-color: rgba(255, 255, 255, 0.1);
    border: 2px solid var(--ui-border-color);
    border-radius: 4px;
    cursor: pointer;
    position: relative;
    margin-right: 10px;
    vertical-align: middle;
    transition: all 0.2s ease;
    flex-shrink: 0;
}

input[type="checkbox"]:checked {
    background-color: var(--primary-color);
    border-color: var(--primary-color);
}

input[type="checkbox"]:checked::after {
    content: '';
    position: absolute;
    left: 5px;
    top: 1px;
    width: 5px;
    height: 10px;
    border: solid #3e2723;
    border-width: 0 2px 2px 0;
    transform: rotate(45deg);
}

input[type="checkbox"]:hover {
    border-color: var(--primary-color);
    box-shadow: 0 0 5px rgba(212, 175, 55, 0.3);
}

.checkbox-label input {
    margin-right: 10px;
}

.range-wrapper {
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--primary-color);
    font-weight: bold;
}

.hazard-list {
    display: grid;
    grid-template-columns: 1fr;
    gap: 10px;
    margin-top: 5px;
}

.hazard-row {
    display: flex;
    flex-direction: column;
    background: rgba(255, 255, 255, 0.05);
    padding: 10px;
    border-radius: 4px;
}

.hazard-params {
    display: flex;
    gap: 15px;
    margin-top: 5px;
    font-size: 1.0rem;
    color: var(--text-muted);
}

.checkbox-label.small {
    font-size: 1.0rem;
}

.small-input {
    width: 50px !important;
    padding: 4px !important;
    font-size: 1.0rem !important;
    text-align: center;
    margin-left: 5px;
}

/* ゲーム画面レイアウト */
#game-screen {
    flex-direction: row;
    justify-content: center;
    align-items: stretch;
    padding: 20px;
    gap: 20px;
}

.game-header {
    display: flex;
    justify-content: space-between;
    width: 100%;
    max-width: 700px;
    margin-bottom: 15px;
    font-size: 1.3rem;
    color: #f0f0f0;
    text-shadow: 1px 1px 2px rgba(0,0,0,0.8);
    background: linear-gradient(to right, transparent, rgba(0, 0, 0, 0.6), transparent);
    padding: 10px 20px;
    font-family: var(--font-ui);
    border-top: 1px solid rgba(212, 175, 55, 0.3);
    border-bottom: 1px solid rgba(212, 175, 55, 0.3);
}

.status-item span {
    font-weight: normal;
    color: var(--primary-color);
}

/* エフェクト表示エリア */
#effect-container {
    height: 50px;
    width: 100%;
    max-width: 700px;
    display: flex;
    gap: 10px;
    justify-content: center;
    align-items: center;
    margin-bottom: 10px;
}

/* タイピングエリア (手紙) */
.typing-container {
    flex-grow: 1;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 20px;
    position: relative;
}

.paper-texture {
    background-color: var(--paper-color);
    color: var(--ink-color);
    width: 100%;
    max-width: 650px;
    height: 350px;
    padding: 50px;
    box-sizing: border-box;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.5), 0 0 60px rgba(0, 0, 0, 0.1) inset;
    border-radius: 2px;
    position: relative;
    overflow: hidden;
    font-size: 1.8rem;
    line-height: 2.5rem; /* 罫線の高さに合わせる */
    text-align: left;
    /* 罫線 */
    background-image: linear-gradient(transparent 2.4rem, rgba(141, 110, 99, 0.15) 2.4rem);
    background-size: 100% 2.5rem;
    background-position: 0 0.6rem; /* 罫線の開始位置調整 (テキストのベースラインに合わせる) */
    border: 1px solid #d7ccc8;
}

/* 日本語のお題表示 */
#japanese-text {
    position: absolute;
    top: 3rem;
    left: 50px;
    right: 50px;
    font-size: 1.4rem;
    color: var(--text-muted);
    font-weight: bold;
    pointer-events: none;
    line-height: 2.5rem;
}

/* お題と入力の重ね合わせ */
#target-text, #user-input {
    position: absolute;
    top: 5.5rem; /* 日本語の1行下 (3rem + 2.5rem) */
    left: 50px;
    right: 50px;
    bottom: 50px;
    white-space: pre-wrap; /* 改行を維持 */
    word-break: break-word;
    font-family: 'Courier New', Courier, monospace; /* 等幅フォントに変更 */
    font-size: 1.5rem;
    letter-spacing: 0.1em;
    line-height: 2.5rem;
}

#target-text {
    color: rgba(62, 39, 35, 0.3); /* 薄い茶色でお題を表示 */
    z-index: 1;
    user-select: none;
}

#user-input {
    color: var(--ink-color);
    z-index: 2;
}

/* 次に入力すべき文字のハイライト */
.current-char {
    color: var(--primary-color) !important;
    border-bottom: 3px solid var(--primary-color);
    opacity: 1 !important;
}

/* ミスタイプ時のスタイル（JSで適用予定） */
.mistake {
    color: var(--ink-error-color);
    text-decoration: line-through;
}

/* バーチャルキーボードエリア */
#virtual-keyboard {
    width: 100%;
    max-width: 850px;
    height: auto;
    margin-bottom: 20px;
}

/* --- 3カラムレイアウト用スタイル --- */

.main-game-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    max-width: 900px;
    width: 100%;
}

/* サイドバー共通 */
.sidebar {
    width: 300px;
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid var(--ui-border-color);
    border-radius: 8px;
    padding: 15px;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    max-height: 90vh; /* 画面からはみ出さないように */
}

.sidebar h3 {
    font-size: 1.2rem;
    margin-top: 0;
    margin-bottom: 15px;
    border-bottom: 1px solid var(--primary-color);
    padding-bottom: 5px;
    text-align: center;
}

/* 左サイドバー: クールタイムリスト */
#active-effects-list {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

.effect-status-item {
    display: flex;
    align-items: center;
    background: rgba(255, 255, 255, 0.05);
    padding: 8px;
    padding-top: 5px;
    border-radius: 4px;
}

.effect-icon {
    font-size: 1.5rem;
    width: 30px;
    text-align: center;
    color: var(--primary-color);
}

.effect-bar-container {
    flex: 1;
    height: 6px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 3px;
    overflow: hidden;
}

.effect-bar {
    height: 100%;
    background: var(--primary-color);
    width: 100%;
    transform-origin: left;
    transition: width linear;
}

/* 左サイドバー: 設定リスト */
#hazard-config-list {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 8px;
    font-size: 0.85rem;
}

.hazard-config-item {
    display: flex;
    align-items: center;
    gap: 8px;
    background: rgba(255, 255, 255, 0.05);
    padding: 6px;
    border-radius: 4px;
    color: var(--text-muted);
    border: 1px solid transparent;
}

/* 右サイドバー: アクションログ */
#action-log {
    flex: 1;
    overflow-y: auto;
    font-family: 'Courier New', monospace;
    font-size: 0.85rem;
    text-align: left;
    display: flex;
    flex-direction: column;
    gap: 5px;
}

.log-entry {
    padding: 5px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    animation: fadeIn 0.3s ease;
    word-break: break-all;
}

.log-user {
    color: var(--primary-color);
    font-weight: bold;
}

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

/* レスポンシブ対応: 画面が狭いときは縦並びに */
@media (max-width: 1200px) {
    #game-screen {
        flex-direction: column;
        justify-content: flex-start;
        overflow-y: auto;
    }
    .sidebar {
        width: 100%;
        height: 200px;
        order: 2;
    }
    .main-game-area {
        width: 100%;
        order: 1;
    }
}

/* リザルト画面 */
#result-screen {
    flex-direction: column;
    align-items: center;
    gap: 20px;
    overflow-y: auto; /* 画面が小さい場合にスクロール可能にする */
}

/* 画面幅が広い場合は横並びにして手紙を右側に表示 */
@media (min-width: 768px) {
    #result-screen {
        flex-direction: row;
        align-items: stretch;
        justify-content: center; /* コンテンツを中央に寄せる */
        overflow-y: hidden; /* PC画面では全体スクロールを消す */
    }
}

.result-left {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    max-width: 450px; /* 左カラムの最大幅制限 */
}

.result-right {
    flex: 2;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    height: auto; /* モバイル向けに自動調整 */
    padding: 20px 0;
    min-width: 0; /* Flexアイテムのはみ出し防止 */
    width: 100%;
    max-width: 800px; /* 右カラムの最大幅制限 */
}

@media (min-width: 768px) {
    .result-right {
        height: 100%; /* PC画面では高さを親に合わせる */
    }
}

.score-summary {
    margin: 30px 0;
    font-size: 1.2rem;
    line-height: 1.5;
}

.total-score {
    display: block;
    font-size: 2.5rem;
    font-weight: bold;
    color: var(--primary-color);
    margin-top: 15px;
}

#final-letter-content {
    width: 100%;
    height: 50vh; /* モバイル時の高さ */
    max-height: none; /* 高さ制限を解除 */
    max-width: 100%; /* .paper-textureのmax-widthを上書きして広げる */
    overflow-y: auto;
    font-size: 1.1rem;
    color: var(--ink-color);
    padding: 10px 50px; /* .paper-textureのパディングに合わせて罫線ずれを防ぐ */
    box-sizing: border-box;
}

@media (min-width: 768px) {
    #final-letter-content {
        height: 100%; /* PC画面では高さを一杯に */
    }
}

/* --- Custom Scrollbar --- */
/* For Webkit-based browsers (Chrome, Safari, Edge) */
::-webkit-scrollbar {
    width: 14px;
}

::-webkit-scrollbar-track {
    background: transparent; /* トラックは紙のテクスチャが見えるように透明に */
}

::-webkit-scrollbar-thumb {
    background-color: var(--text-muted); /* つまみの色をテーマに合わせる */
    border-radius: 20px;
    border: 4px solid var(--paper-color); /* つまみの周りに紙と同じ色の余白を作る */
}

::-webkit-scrollbar-thumb:hover {
    background-color: var(--primary-color); /* ホバー時にゴールドに光る */
}

/* --- Result Screen Letter Styles --- */
.result-line {
    margin-bottom: 2.5rem; /* 手紙の1行ごとに空白行を設ける */
}

.result-jp,
.result-hiragana {
    line-height: 2.5rem; /* 罫線の高さに合わせる */
    display: block;
}

.result-jp {
    font-family: var(--font-handwriting);
    font-weight: bold;
    font-size: 1.4rem;
    color: var(--text-muted);
}

.result-hiragana {
    font-family: var(--font-handwriting);
    font-weight: bold;
    font-size: 1.6rem;
    color: var(--ink-color);
}

.result-hiragana.mistake {
    color: var(--ink-error-color);
}

.result-romaji {
    font-family: 'Courier New', Courier, monospace;
    font-size: 0.8rem;
    word-break: break-all;
    line-height: 1.2;
    margin-top: 0.5rem;
    color: var(--text-muted);
}

.result-romaji .mistake-char {
    color: var(--ink-error-color);
}

/* サイドバー内のスクロールバー調整 (ログエリアなど) */
#active-effects-list::-webkit-scrollbar,
#action-log::-webkit-scrollbar,
#hazard-config-list::-webkit-scrollbar,
.question-list::-webkit-scrollbar,
.settings-panel::-webkit-scrollbar {
    width: 8px;
}

#active-effects-list::-webkit-scrollbar-track,
#action-log::-webkit-scrollbar-track,
#hazard-config-list::-webkit-scrollbar-track,
.question-list::-webkit-scrollbar-track,
.settings-panel::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.2);
}

#active-effects-list::-webkit-scrollbar-thumb,
#action-log::-webkit-scrollbar-thumb,
#hazard-config-list::-webkit-scrollbar-thumb,
.question-list::-webkit-scrollbar-thumb,
.settings-panel::-webkit-scrollbar-thumb {
    background-color: rgba(255, 255, 255, 0.2);
    border: none;
    border-radius: 4px;
}

#active-effects-list::-webkit-scrollbar-thumb:hover,
#action-log::-webkit-scrollbar-thumb:hover,
#hazard-config-list::-webkit-scrollbar-thumb:hover,
.question-list::-webkit-scrollbar-thumb:hover,
.settings-panel::-webkit-scrollbar-thumb:hover {
    background-color: var(--primary-color);
}