@import url('https://fonts.googleapis.com/css2?family=Zen+Maru+Gothic:wght@400;500;700&family=Shippori+Mincho:wght@500;700&display=swap');

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

body {
    font-family: 'Zen Maru Gothic', sans-serif;
    background: #EFEDE1;
    color: #2B2B24;
    -webkit-tap-highlight-color: transparent;
    overscroll-behavior: none;
}

.app-container {
    max-width: 640px;
    margin: 0 auto;
    padding: 14px;
    min-height: 100vh;
}

.screen { display: none; }
.screen.active { display: block; }

/* ---------- 共通パネル ---------- */
.hb-panel {
    border: 1px solid #c9c0a8;
    background: #F7F5EC;
    padding: 10px 16px;
    margin-bottom: 10px;
}
.panel-heading {
    font-family: 'Shippori Mincho', serif;
    font-size: 15px;
    margin: 0 0 8px 0;
}

/* ---------- ヘッダー ---------- */
.game-header { text-align: center; margin-bottom: 10px; }
.hb-title {
    font-family: 'Shippori Mincho', serif;
    font-size: 22px;
    font-weight: 700;
    margin: 0;
}
.hb-title span {
    display: block;
    font-size: 15px;
    color: #6b6552;
    font-family: 'Zen Maru Gothic', sans-serif;
    font-weight: 500;
    margin-top: 2px;
}
.hb-subtitle { font-size: 12px; color: #6b6552; margin-top: 4px; }

/* ---------- 遊び方カード ---------- */
.intro-card { padding: 10px 16px; }
.intro-card-header { display: flex; align-items: center; justify-content: space-between; cursor: pointer; }
.intro-card-header .panel-heading { margin: 0; }
.intro-body { margin-top: 10px; }
.intro-card ul { margin: 0; padding-left: 20px; font-size: 12px; line-height: 1.7; }
.intro-card strong { color: #2C4F6B; }
.records-toggle-icon { font-size: 11px; color: #6b6552; transition: transform .15s; }
.records-toggle-icon.open { transform: rotate(90deg); }

/* ---------- 記録カード ---------- */
.progress-card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    row-gap: 8px;
    margin-bottom: 8px;
}
.progress-card-header .panel-heading { margin: 0; }
.progress-stats {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;
}
.progress-stat {
    border: 1px solid #ded6bd;
    background: #EFEDE1;
    padding: 8px 6px;
    text-align: center;
}
.ps-value { font-size: 17px; font-weight: 700; color: #2C4F6B; font-variant-numeric: tabular-nums; }
.ps-label { font-size: 10px; color: #6b6552; margin-top: 2px; }
.progress-reset-btn {
    font-family: 'Zen Maru Gothic', sans-serif;
    border: 1px solid #9a5a4a;
    background: #F7F5EC;
    color: #9a5a4a;
    padding: 5px 12px;
    font-size: 11px;
    cursor: pointer;
    flex-shrink: 0;
}
.progress-reset-btn:hover { background: #9a5a4a; color: #F7F5EC; }

/* ---------- ベスト記録カード ---------- */
.records-card { padding: 10px 16px; }
.records-card-header { display: flex; align-items: center; justify-content: space-between; cursor: pointer; }
.records-card-header .panel-heading { margin: 0; }
.records-body { margin-top: 10px; display: flex; flex-direction: column; gap: 14px; }
.records-hint { font-size: 11px; color: #6b6552; margin: 0 0 2px 0; line-height: 1.5; }
.records-col-title { font-size: 12px; font-weight: 700; color: #2C4F6B; margin-bottom: 6px; }
.records-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.record-item {
    display: flex;
    align-items: center;
    gap: 8px;
    border: 1px solid #ded6bd;
    border-left: 3px solid #b9b09a;
    background: #EFEDE1;
    padding: 6px 8px;
    font-size: 12px;
}
.record-item.rank-1 { border-left-color: #B5652D; }
.record-rank { font-weight: 700; color: #2C4F6B; flex-shrink: 0; width: 30px; }
.record-value { flex: 1; font-variant-numeric: tabular-nums; }
.record-date { color: #6b6552; font-size: 10px; flex-shrink: 0; }
.record-delete-btn {
    font-family: 'Zen Maru Gothic', sans-serif;
    border: none;
    background: transparent;
    color: #9a5a4a;
    cursor: pointer;
    font-size: 12px;
    padding: 2px 4px;
    flex-shrink: 0;
}
.record-delete-btn:hover { color: #6b3a30; }
.record-empty { font-size: 12px; color: #6b6552; padding: 4px 0; }

@media (min-width: 520px) {
    .records-body { flex-direction: row; gap: 16px; }
    .records-col { flex: 1; min-width: 0; }
}

/* ---------- 難易度カード ---------- */
.difficulty-card { text-align: center; }
.difficulty-card .settings-heading { text-align: left; }
.settings-row { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; justify-content: center; margin-bottom: 8px; }
.difficulty-options { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; width: 100%; }
.diff-option {
    border: 1px solid #b9b09a;
    background: #F7F5EC;
    padding: 8px 16px;
    cursor: pointer;
    font-size: 13px;
    flex: 1;
    text-align: center;
    min-width: 80px;
}
.diff-option input { position: absolute; opacity: 0; pointer-events: none; }
.diff-option.active { background: #2C4F6B; border-color: #2C4F6B; color: #F7F5EC; font-weight: 700; }
.diff-explain {
    font-size: 12px;
    color: #4b4636;
    background: #EFEDE1;
    border: 1px solid #ded6bd;
    padding: 8px 10px;
    text-align: left;
    line-height: 1.6;
}

.mode-selection { display: flex; flex-direction: column; gap: 8px; margin-bottom: 8px; }

/* ---------- モード選択（通常／タイムトライアル／タイムアタック：横並び等幅） ---------- */
.game-mode-row { display: flex; gap: 8px; margin-bottom: 8px; }
.game-mode-row .mode-btn {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    padding: 10px 4px;
    text-align: center;
    min-width: 0;
}
.mode-btn .btn-icon { font-size: 20px; }
.mode-btn .mode-btn-label { font-size: 13px; font-weight: 700; white-space: nowrap; }
.mode-btn .btn-best { font-size: 10px; color: #2C4F6B; font-weight: 700; font-variant-numeric: tabular-nums; }

/* ---------- 出題範囲（この級だけ / ミックス） ---------- */
#mode-options.disabled { opacity: 0.4; pointer-events: none; }
.mode-note { display: none; font-size: 11px; color: #6b6552; margin-top: 4px; }

/* ---------- ボタン共通 ---------- */
.hb-btn {
    font-family: 'Zen Maru Gothic', sans-serif;
    border: 1px solid #2C4F6B;
    background: #F7F5EC;
    color: #2B2B24;
    padding: 6px 14px;
    cursor: pointer;
    font-size: 14px;
}
button.hb-btn.btn-primary {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 16px;
    text-align: left;
    width: 100%;
    justify-content: center;
}
.btn-primary { border-color: #2C4F6B; }
.btn-primary:hover { background: #2C4F6B; color: #F7F5EC; }
.btn-secondary { border-color: #B5652D; color: #B5652D; }
.btn-secondary:hover { background: #B5652D; color: #F7F5EC; }
.btn-icon { font-size: 20px; }
.btn-text { display: flex; flex-direction: column; }
.btn-text strong { font-size: 14px; }
.btn-text span { font-size: 11px; color: #6b6552; }

/* ---------- ゲームHUD ---------- */
.game-hud { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; gap: 10px; }
.hud-btn { flex-shrink: 0; }
.hud-stats { display: flex; gap: 14px; }
.stat-item { display: flex; flex-direction: column; align-items: center; }
.stat-label { font-size: 10px; color: #6b6552; }
.stat-value { font-size: 17px; font-weight: 700; color: #2C4F6B; font-variant-numeric: tabular-nums; }
.game-instruction { font-size: 12px; color: #6b6552; text-align: center; margin-bottom: 10px; }

/* ---------- パズル本体 ---------- */
.puzzle-board { padding: 16px; }

/* ---------- ヒント ---------- */
.hint-row { display: flex; align-items: center; justify-content: flex-end; gap: 6px; margin-bottom: 4px; }
.hint-label { font-size: 12px; color: #6b6552; }
.switch { position: relative; display: inline-block; width: 32px; height: 18px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider.round { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background: #ded6bd; border-radius: 22px; transition: .2s; }
.slider.round:before { position: absolute; content: ""; height: 12px; width: 12px; left: 3px; bottom: 3px; background: #F7F5EC; border-radius: 50%; transition: .2s; }
input:checked + .slider.round { background: #2C4F6B; }
input:checked + .slider.round:before { transform: translateX(14px); }
.hint-text-box { font-size: 12px; color: #4b4636; margin-bottom: 8px; min-height: 1.2em; text-align: right; }
.hint-text-box.show { background: #EFEDE1; padding: 6px 8px; border-right: 3px solid #2C4F6B; display: block; text-align: left; }

.expr-area { margin-bottom: 14px; }
.expr-line {
    min-height: 46px;
    border: 2px dashed #b9b09a;
    background: #EFEDE1;
    padding: 8px 10px;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 4px;
    font-size: 20px;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    transition: background .1s, border-color .1s;
}
.expr-line.drop-hover {
    border-color: #2C4F6B;
    border-style: solid;
    background: #dfe9ef;
}
.drag-ghost {
    position: fixed;
    pointer-events: none;
    z-index: 9999;
    opacity: 0.9;
    box-shadow: 0 6px 14px rgba(43, 43, 36, 0.3);
    transform: scale(1.08);
}
.expr-placeholder { color: #a29c88; font-size: 13px; font-weight: 400; }
.expr-token {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 26px;
    padding: 2px 4px;
    cursor: pointer;
    border-radius: 3px;
}
.expr-token.tok-digit { color: #2C4F6B; background: #dfe9ef; }
.expr-token.tok-op { color: #B5652D; }
.expr-token.tok-paren { color: #6b6552; }
.expr-token:hover { background: #f0d9c8; }
.expr-result { margin-top: 6px; font-size: 13px; color: #6b6552; min-height: 16px; text-align: right; }

.tile-pool { display: flex; justify-content: center; gap: 10px; margin-bottom: 14px; min-height: 56px; }
.digit-tile {
    width: 52px;
    height: 52px;
    border: 1px solid #2C4F6B;
    background: #F7F5EC;
    color: #2C4F6B;
    font-size: 24px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: grab;
    font-variant-numeric: tabular-nums;
    transition: transform .1s, background .1s, color .1s;
    touch-action: none;
    user-select: none;
}
.digit-tile:hover:not(:disabled) { background: #2C4F6B; color: #F7F5EC; transform: translateY(-2px); }
.digit-tile.used { visibility: hidden; }
.digit-tile.blocked { opacity: 0.3; cursor: not-allowed; }
.digit-tile.dragging-source { opacity: 0.35; cursor: grabbing; }

.op-row { display: flex; gap: 6px; justify-content: center; margin-bottom: 10px; flex-wrap: wrap; }
.op-btn {
    font-family: 'Zen Maru Gothic', sans-serif;
    width: 48px;
    height: 40px;
    border: 1px solid #B5652D;
    background: #F7F5EC;
    color: #B5652D;
    font-size: 18px;
    cursor: grab;
    touch-action: none;
    user-select: none;
}
.op-btn.dragging-source { opacity: 0.35; cursor: grabbing; }
.op-btn:hover:not(:disabled) { background: #B5652D; color: #F7F5EC; }
.op-btn:disabled { opacity: 0.3; cursor: not-allowed; }
.paren-btn { border-color: #6b6552; color: #6b6552; }
.paren-btn:hover:not(:disabled) { background: #6b6552; color: #F7F5EC; }
.paren-btn.hidden { display: none; }
.muldiv-btn.hidden { display: none; }

.control-row { display: flex; gap: 8px; justify-content: center; margin-bottom: 10px; flex-wrap: wrap; }
.ctrl-btn { font-size: 12px; padding: 7px 10px; }

.feedback-box { min-height: 24px; text-align: center; font-size: 14px; font-weight: 700; margin-bottom: 8px; }
.feedback-box.ok { color: #2C6B3A; }
.feedback-box.ng { color: #9a5a4a; }
.feedback-box.info { color: #2C4F6B; }

.submit-row { display: flex; gap: 8px; }
.submit-row > .hb-btn { width: auto; }
.submit-btn { flex: 4; }
.skip-btn { flex: 1; border-color: #6b6552; color: #6b6552; white-space: nowrap; padding-left: 4px; padding-right: 4px; }
.skip-btn:hover:not(:disabled) { background: #6b6552; color: #F7F5EC; }
.submit-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.skip-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.next-btn { background: #2C6B3A; border-color: #2C6B3A; color: #F7F5EC; flex: 1; }

/* ---------- リザルト画面 ---------- */
.result-card { text-align: center; }
.result-best-badge {
    background: #fdf1d9;
    border: 1px solid #d8a53d;
    color: #7a5b12;
    padding: 8px 10px;
    font-size: 13px;
    font-weight: 700;
    margin-bottom: 10px;
}
.result-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 16px; }
.result-stat { border: 1px solid #ded6bd; background: #EFEDE1; padding: 10px 6px; }
.res-label { font-size: 11px; color: #6b6552; margin-bottom: 4px; }
.res-value { font-size: 15px; font-weight: 700; color: #2C4F6B; }
.result-actions { display: flex; gap: 10px; }
.result-actions .hb-btn { flex: 1; padding: 12px; font-weight: 700; }

@media (max-width: 420px) {
    .digit-tile { width: 44px; height: 44px; font-size: 20px; }
    .op-btn { width: 40px; height: 36px; font-size: 16px; }
    .expr-line { font-size: 17px; }
    .progress-stats { grid-template-columns: repeat(2, 1fr); }
    .mode-btn { padding: 8px 3px; }
    .mode-btn .btn-icon { font-size: 16px; }
    .mode-btn .mode-btn-label { font-size: 11px; }
    .mode-btn .btn-best { font-size: 9px; }
    .result-stats { grid-template-columns: 1fr; }
}

.back-home-container {
    text-align: center;
    margin-top: 24px;
    margin-bottom: 12px;
}

.back-link {
    color: #6b6552;
    text-decoration: none;
    font-size: 14px;
    font-weight: 500;
    transition: color 0.2s ease;
}

.back-link:hover {
    color: #2B2B24;
    text-decoration: underline;
}
