/* 入力モード専用スタイル */

/* モード切り替えによる表示/非表示 */
.mode-hidden {
    display: none !important;
}

/* 問題未生成時のメッセージ */
.no-data-message {
    text-align: center;
    padding: 60px 30px;
    background-color: #f5f5f5;
    border-radius: 12px;
    margin: 30px auto;
    max-width: 500px;
}

.no-data-message p {
    font-size: 18px;
    color: #666;
    margin-bottom: 25px;
}

.no-data-message button {
    font-size: 16px;
    padding: 12px 30px;
}

/* モード切り替えタブ */
.mode-tabs {
    display: flex;
    justify-content: center;
    gap: 0;
    margin: 30px auto;
    max-width: 500px;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.mode-tab {
    flex: 1;
    padding: 15px 30px;
    border: none;
    background-color: #e0e0e0;
    color: #666;
    font-size: 18px;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.3s;
}

.mode-tab:hover {
    background-color: #d0d0d0;
}

.mode-tab.active {
    background-color: #4CAF50;
    color: white;
}

/* 入力エリア */
.input-area {
    max-width: 1400px;
    margin: 30px auto;
    background-color: white;
    padding: 30px;
    border-radius: 12px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

/* タイマーヘッダー */
.input-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;
    padding: 20px;
    background-color: #f9f9f9;
    border-radius: 8px;
}

.timer-display {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 36px;
    font-weight: bold;
    color: #333;
}

.timer-icon {
    font-size: 40px;
}

.timer-controls {
    display: flex;
    gap: 10px;
}

/* 入力設定 */
.input-settings {
    margin-bottom: 20px;
    padding: 15px 20px;
    background-color: #f5f5f5;
    border-radius: 8px;
}

.setting-group {
    display: flex;
    align-items: center;
    gap: 20px;
}

.setting-group label:first-child {
    font-weight: 500;
    color: #333;
}

.radio-label {
    display: flex;
    align-items: center;
    gap: 5px;
    cursor: pointer;
    font-size: 15px;
}

.radio-label input[type="radio"] {
    cursor: pointer;
}

.btn-success {
    background-color: #4CAF50;
    color: white;
}

.btn-success:hover {
    background-color: #45a049;
}

.btn-warning {
    background-color: #ff9800;
    color: white;
}

.btn-warning:hover {
    background-color: #e68900;
}

.btn-large {
    font-size: 24px;
    padding: 20px 60px;
}

/* メインコンテンツエリア */
.input-main {
    display: grid;
    grid-template-columns: 1fr;
    gap: 30px;
    margin-bottom: 30px;
}

/* 入力グリッド */
.input-grid-container {
    display: flex;
    justify-content: center;
    align-items: flex-start;
    max-width: 100%;
    width: 100%;
}

#inputGrid {
    display: block;
    overflow: auto;
    max-height: calc(100dvh - 250px); /* 100dvh for better iOS Safari support */
    min-height: 400px;
    max-width: 100%;
    width: auto;
    position: relative;
    -webkit-overflow-scrolling: touch; /* iOS Safari smooth scrolling */
}

/* 入力用の表スタイル - iOS Safari完全対応 */
.input-table {
    margin: 0 auto;
    width: auto;
    table-layout: fixed;
    border-collapse: separate; /* iOS Safari sticky support */
    border-spacing: 0; /* No gaps between cells */
}

.input-table td {
    border: 2px solid #333;
    text-align: center;
    vertical-align: middle;
    font-weight: bold;
    box-sizing: border-box;
    background-color: white; /* デフォルト背景色 */
}

/* Stickyモードの時は二重線を防ぐ */
.input-table.sticky-headers td {
    border-right-width: 1px;
    border-bottom-width: 1px;
}

.input-table.sticky-headers td:last-child {
    border-right-width: 2px;
}

.input-table.sticky-headers tr:last-child td {
    border-bottom-width: 2px;
}

.input-table td.corner {
    background-color: #333;
    color: white;
    font-size: 20px;
}

.input-table td.header {
    background-color: #e0e0e0;
    font-weight: bold;
    font-size: 18px;
}

/* ヘッダー固定モードがONの時のみsticky適用 - iOS Safari完全対応 */
.input-table.sticky-headers td.corner {
    position: -webkit-sticky;
    position: sticky;
    top: 0;
    left: 0;
    z-index: 5; /* iOS Safari最適化 */
    background-color: #333; /* 背景色必須 */
}

.input-table.sticky-headers tr:first-child td.header {
    position: -webkit-sticky;
    position: sticky;
    top: 0;
    z-index: 4; /* cornerより低く */
    background-color: #e0e0e0; /* 背景色必須 */
}

.input-table td.input-cell {
    background-color: white;
    padding: 0;
}

/* 各行の最初のセル（行ヘッダー）を固定 - すべての行 */
/* ⚠️ 重要: input-cellのルールより後に定義 */
.input-table.sticky-headers td.header:first-child,
.input-table.sticky-headers td.corner:first-child {
    position: -webkit-sticky; /* Safari対応 */
    position: sticky;
    left: 0;
}

/* 左上のコーナーは最前面 */
.input-table.sticky-headers td.corner:first-child {
    z-index: 5; /* iOS Safari最適化 */
    background-color: #333; /* 背景色必須 */
}

/* 行ヘッダーの背景色とz-index */
.input-table.sticky-headers td.header:first-child {
    z-index: 4; /* cornerより低く */
    background-color: #e0e0e0; /* 背景色必須 */
}

.input-table td.input-cell input {
    width: 100%;
    height: 100%;
    border: none;
    text-align: center;
    font-size: 16px;
    font-weight: bold;
    padding: 0;
    background-color: transparent;
}

.input-table td.input-cell input:focus {
    outline: none;
    background-color: #fffde7;
}

.input-table td.input-cell.active {
    border: 3px solid #4CAF50;
    box-shadow: 0 0 10px rgba(76, 175, 80, 0.5);
}

.input-table td.input-cell.filled input {
    background-color: #e8f5e9;
    color: #2e7d32;
}

.input-table td.input-cell.correct {
    background-color: #4CAF50 !important;
}

.input-table td.input-cell.correct input {
    background-color: #4CAF50 !important;
    color: white !important;
    font-weight: bold;
}

.input-table td.input-cell.incorrect {
    background-color: #f44336 !important;
}

.input-table td.input-cell.incorrect input {
    background-color: #f44336 !important;
    color: white !important;
    font-weight: bold;
}

/* 割り切れないマス（無効セル） */
.input-table td.input-cell.disabled {
    background-color: #e0e0e0;
    position: relative;
    pointer-events: none;
}

/* 斜線表示 */
.diagonal-line {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

.diagonal-line::before,
.diagonal-line::after {
    content: '';
    position: absolute;
    background-color: #999;
}

/* 左上から右下への斜線 */
.diagonal-line::before {
    top: 50%;
    left: 0;
    width: 100%;
    height: 2px;
    transform: translateY(-50%) rotate(-45deg);
    transform-origin: center;
}

/* 入力グリッドのサイズ調整 - すべてのサイズで40px×40pxを維持 */
.input-table.size-3 td { width: 60px; height: 60px; font-size: 20px; }
.input-table.size-4 td { width: 55px; height: 55px; font-size: 19px; }
.input-table.size-5 td { width: 50px; height: 50px; font-size: 18px; }
.input-table.size-6 td { width: 48px; height: 48px; font-size: 17px; }
.input-table.size-7 td { width: 45px; height: 45px; font-size: 16px; }
.input-table.size-8 td { width: 43px; height: 43px; font-size: 16px; }
.input-table.size-9 td { width: 41px; height: 41px; font-size: 15px; }
/* 10×10以上は40px×40pxで統一 - グリッド全体が大きくなる */
.input-table.size-10 td, 
.input-table.size-11 td, 
.input-table.size-12 td, 
.input-table.size-13 td, 
.input-table.size-14 td, 
.input-table.size-15 td { 
    width: 40px; 
    height: 40px; 
    font-size: 15px; 
}

/* テンキー */
.numpad {
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 20px;
    background-color: #f5f5f5;
    border-radius: 8px;
    max-width: 300px;
    min-width: 250px;
    margin: 0;
    position: sticky;
    top: 20px;
}

.numpad-row {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 10px;
}

.numpad-btn {
    padding: 20px;
    font-size: 24px;
    font-weight: bold;
    border: 2px solid #ddd;
    border-radius: 8px;
    background-color: white;
    cursor: pointer;
    transition: all 0.2s;
    min-height: 70px;
}

.numpad-btn:hover {
    background-color: #e8f5e9;
    border-color: #4CAF50;
}

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

.numpad-zero {
    grid-column: span 2;
}

.numpad-enter {
    background-color: #4CAF50;
    color: white;
    border-color: #4CAF50;
}

.numpad-enter:hover {
    background-color: #45a049;
}

.numpad-backspace,
.numpad-delete {
    background-color: #ff9800;
    color: white;
    border-color: #ff9800;
}

.numpad-backspace:hover,
.numpad-delete:hover {
    background-color: #e68900;
}

.numpad-move {
    background-color: #2196F3;
    color: white;
    border-color: #2196F3;
}

.numpad-move:hover {
    background-color: #0b7dda;
}

/* 採点セクション */
.grade-section {
    text-align: center;
    margin: 30px 0;
}

/* 採点結果エリア */
.results-area {
    margin-top: 30px;
    padding: 30px;
    background-color: #f9f9f9;
    border-radius: 8px;
    border: 2px solid #4CAF50;
}

.results-area h3 {
    text-align: center;
    color: #333;
    margin-bottom: 20px;
    font-size: 24px;
}

.results-content {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;
    max-width: 600px;
    margin: 0 auto;
}

.result-item {
    padding: 15px;
    background-color: white;
    border-radius: 8px;
    text-align: center;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.result-item span:first-child {
    display: block;
    font-size: 16px;
    color: #666;
    margin-bottom: 5px;
}

.result-value {
    display: block;
    font-size: 28px;
    font-weight: bold;
    color: #4CAF50;
}

/* 成績記録エリア */
.history-area {
    margin-top: 30px;
    padding: 30px;
    background-color: #f9f9f9;
    border-radius: 8px;
}

.history-area h3 {
    text-align: center;
    color: #333;
    margin-bottom: 20px;
    font-size: 22px;
}

.history-list {
    max-width: 800px;
    margin: 0 auto;
}

.history-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px;
    background-color: white;
    border-radius: 8px;
    margin-bottom: 10px;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.history-item.best {
    border: 2px solid #FFD700;
    background-color: #FFFEF0;
}

.history-date {
    font-size: 14px;
    color: #666;
}

.history-details {
    font-size: 16px;
    font-weight: bold;
    color: #333;
}

.history-score {
    font-size: 18px;
    font-weight: bold;
    color: #4CAF50;
}

.best-badge {
    display: inline-block;
    padding: 3px 8px;
    background-color: #FFD700;
    color: #333;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
}

/* 成長グラフコンテナ */
.chart-container {
    margin-top: 30px;
    padding: 20px;
    background-color: #ffffff;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.chart-title {
    font-size: 20px;
    font-weight: bold;
    color: #333;
    margin-bottom: 20px;
    text-align: center;
}

.chart-wrapper {
    position: relative;
    height: 300px;
    margin-bottom: 30px;
}

.chart-wrapper:last-child {
    margin-bottom: 0;
}

/* レスポンシブ対応 */
@media (max-width: 768px) {
    .chart-container {
        padding: 15px;
    }
    
    .chart-title {
        font-size: 18px;
        margin-bottom: 15px;
    }
    
    .chart-wrapper {
        height: 250px;
        margin-bottom: 25px;
    }
}

@media (max-width: 480px) {
    .chart-container {
        padding: 10px;
        margin-top: 20px;
    }
    
    .chart-title {
        font-size: 16px;
        margin-bottom: 10px;
    }
    
    .chart-wrapper {
        height: 200px;
        margin-bottom: 20px;
    }
}

/* 横持ち推奨メッセージ */
.rotate-message {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: rgba(0, 0, 0, 0.9);
    color: white;
    padding: 30px;
    border-radius: 12px;
    text-align: center;
    z-index: 9999;
    max-width: 80%;
    opacity: 1;
    transition: opacity 0.5s ease;
}

.rotate-message.fade-out {
    opacity: 0;
}

.rotate-message-content {
    position: relative;
}

.rotate-message-close {
    position: absolute;
    top: -20px;
    right: -20px;
    background: white;
    color: black;
    border: none;
    border-radius: 50%;
    width: 32px;
    height: 32px;
    font-size: 24px;
    font-weight: bold;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    line-height: 1;
    padding: 0;
}

.rotate-message-close:hover {
    background-color: #f44336;
    color: white;
    transform: scale(1.1);
}

.rotate-message p {
    font-size: 18px;
    margin: 8px 0;
    line-height: 1.6;
}

.rotate-message p:first-child {
    font-size: 20px;
    font-weight: bold;
    margin-bottom: 15px;
}

/* タブレット横持ち（768px以上） */
@media screen and (min-width: 768px) {
    .input-main {
        grid-template-columns: 60% 40%;
    }

    .numpad {
        display: flex;
    }
}

/* スマホ横持ち（481px - 767px） */
@media screen and (min-width: 481px) and (max-width: 767px) {
    .input-main {
        grid-template-columns: 50% 50%;
    }

    .numpad {
        display: flex;
    }

    .timer-display {
        font-size: 24px;
    }

    .numpad-btn {
        padding: 15px;
        font-size: 20px;
        min-height: 60px;
    }
}

/* スマホ縦持ち（480px以下） */
@media screen and (max-width: 480px) {
    .input-header {
        flex-direction: column;
        gap: 15px;
    }

    .timer-display {
        font-size: 28px;
    }

    .timer-controls {
        width: 100%;
        justify-content: center;
    }

    .input-main {
        grid-template-columns: 1fr;
    }

    .numpad {
        display: none; /* 縦持ち時は非表示 */
    }

    .results-content {
        grid-template-columns: 1fr;
    }
}

/* タブレット・スマホ横向き（481px～1023px） */
@media screen and (min-width: 481px) and (max-width: 1023px) {
    /* グリッドとテンキーを左右に配置 */
    .input-main {
        display: grid !important;
        grid-template-columns: 1fr auto !important;
        gap: 20px;
        align-items: start;
    }
    
    .input-grid-container {
        justify-content: flex-start;
        /* overflow は #inputGrid で制御 */
    }
    
    #inputGrid {
        max-height: calc(100dvh - 300px); /* iOS Safari対応: dvhを使用 */
        min-height: 300px; /* 最小高さも調整 */
    }
    
    .numpad {
        display: flex !important;
        max-width: 280px;
        min-width: 250px;
        margin: 0;
    }
    
    .numpad-btn {
        padding: 15px 10px;
        font-size: 20px;
        min-height: 65px;
    }
    
    /* 入力グリッドを少し小さく */
    .input-table.size-10 td { 
        width: 35px; 
        height: 35px; 
        font-size: 14px; 
    }
    
    .input-table.size-8 td { 
        width: 40px; 
        height: 40px; 
        font-size: 15px; 
    }
}

/* PC（1024px以上） */
@media screen and (min-width: 1024px) {
    .input-main {
        grid-template-columns: 1fr;
    }

    .numpad {
        display: none; /* PCはキーボード使用 */
    }
}
