:root {
    --bg: #ffffff;
    --card: #f8f8f8;
    --accent: #e31b23;
    --text: #333333;
    --danger: #b71c1c;
    --border: #dddddd;
}

html {
    height: 100%;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: 'Helvetica Neue', Arial, sans-serif;
    overflow: hidden;
    height: 100%;
    display: flex;
    flex-direction: column;
}

* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

/* メインコンテンツラッパー */
#main-content-wrapper {
    flex: 1;
    overflow: hidden;
    position: relative;
    width: 100%;
}

/* 広告フッターエリア */
#sticky-ad-footer {
    flex-shrink: 0;
    width: 100%;
    background: #f8f8f8;
    z-index: 9000;
    min-height: 100px;
    /* Reduced from 280px to save space */
    display: flex;
    justify-content: center;
    align-items: center;
    border-top: 1px solid var(--border);
    padding-bottom: env(safe-area-inset-bottom);
}

/* --- AdSense Stability & Z-Index Safety --- */
/* Ensure controls are clickable even if ads encroach */
.key,
.btn-action,
.tap-area,
.footer button,
.manual-row input,
.start-btn,
.control-btn,
.settings-btn {
    position: relative;
    z-index: 9005;
    /* Higher than some ad layers if possible, but mainly to keep stack order */
}

/* Safe Area for Anchor Ads */
#mode0 {
    padding-bottom: 30px !important;
}

.footer {
    padding-bottom: 20px !important;
    /* Ensure footer buttons aren't covered by anchor ads */
}

/* Ensure scrollable areas have clearance */
.mode3-scrollable,
.mode4-scrollable {
    padding-bottom: 30px !important;
}

/* ナビゲーション */
/* ナビゲーション */
.menu-icon-btn {
    position: fixed;
    top: 30px;
    /* 15pxから30pxへ：ノッチや広告対策 */
    left: 15px;
    z-index: 9999;
    background: rgba(255, 255, 255, 0.95);
    border: 1px solid #eee;
    padding: 10px;
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity 0.3s;
    color: #333;
}

.menu-icon-btn svg {
    width: 24px;
    height: 24px;
    fill: currentColor;
}

.nav-menu {
    position: fixed;
    top: 0;
    left: -280px;
    width: 280px;
    height: 100%;
    background: var(--bg);
    transition: 0.3s;
    z-index: 9998;
    box-shadow: 2px 0 15px rgba(0, 0, 0, 0.1);
    display: flex;
    flex-direction: column;
}

.nav-menu.open {
    left: 0;
}

.nav-logo-area {
    padding: 60px 20px 20px;
    text-align: center;
    border-bottom: 1px solid var(--border);
    background: #fdfdfd;
}

.nav-logo-area img {
    max-width: 80%;
    height: auto;
}

.nav-logo-placeholder {
    font-weight: bold;
    font-size: 1.2rem;
    color: #000;
    letter-spacing: 1px;
}

.nav-item {
    padding: 18px 25px;
    border-bottom: 1px solid #f0f0f0;
    cursor: pointer;
    font-size: 1rem;
    color: #555;
    font-weight: 500;
    transition: 0.2s;
}

.nav-item:active {
    background: #f0f0f0;
    color: var(--accent);
}

/* アイコン用のスタイル追加 */
.nav-item {
    display: flex;
    align-items: center;
    gap: 15px;
    /* アイコンと文字の間隔 */
    padding: 18px 25px;
    border-bottom: 1px solid #f0f0f0;
    cursor: pointer;
    font-size: 1rem;
    color: #555;
    font-weight: 500;
    transition: 0.2s;
}

.nav-icon {
    width: 24px;
    height: 24px;
    fill: #888;
    /* アイコンの色 */
    transition: 0.2s;
}

.nav-item:active .nav-icon {
    fill: var(--accent);
    /* タップ時に赤くなる */
}

/* モード共通 */
.mode-container {
    display: none;
    height: 100%;
    flex-direction: column;
    box-sizing: border-box;
}

.mode-container.active {
    display: flex;
}

#mode0.active {
    display: block;
    /* Ensure block so scrolling works */
}

/* 共通ページタイトル */
.page-title {
    font-size: 1.2rem;
    text-align: center;
    margin: 0;
    padding-top: 35px;
    /* メニューボタンの高さに合わせる */
    padding-bottom: 20px;
    background: var(--bg);
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 10px;
}

.title-icon {
    width: 24px;
    height: 24px;
    fill: #333;
}

/* Mode 1 & 2 Styles */
#mode1 .tap-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    transition: background 0.3s ease;
    cursor: pointer;
    /* padding-top 削除 */
}

.count-large {
    font-size: 10rem;
    font-weight: bold;
    color: #222;
    pointer-events: none;
}

.stats-bar {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
    row-gap: 15px;
    padding: 15px;
    background: #f8f8f8;
    /* margin-top 削除 */
    border-bottom: 1px solid var(--border);
}

.history-bar {
    background: #eee;
    padding: 10px;
    display: flex;
    gap: 8px;
    overflow-x: auto;
    min-height: 40px;
    align-items: center;
}

.history-chip {
    background: white;
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 0.9rem;
    border: 1px solid var(--border);
    white-space: nowrap;
    color: #444;
    display: flex;
    align-items: center;
    gap: 6px;
    cursor: pointer;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.history-chip:active {
    background-color: #ffebee;
    border-color: #ffcdd2;
}

.delete-x {
    color: #ccc;
    font-weight: bold;
    font-size: 1.1em;
    line-height: 1;
    margin-left: 2px;
}

.history-chip:hover .delete-x {
    color: var(--danger);
}

.input-view {
    height: 80px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 4rem;
    color: var(--accent);
    background: #fff;
    font-weight: bold;
}

.keypad {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    flex: 1;
    background: var(--border);
    gap: 1px;
    padding-bottom: 30px;
    /* 下部ボタンが端に行き過ぎないように */
}

.key {
    background: #fff;
    border: none;
    color: #333;
    font-size: 1.5rem;
    cursor: pointer;
    outline: none;
    -webkit-tap-highlight-color: transparent;
}

.key:active {
    background: #f0f0f0;
}

.key.enter {
    background: #333;
    color: white;
    grid-column: span 2;
    font-weight: bold;
}

/* Mode 3 & 4 Styles */
.calc-content {
    padding: 80px 20px 20px;
    flex: 1;
    overflow-y: auto;
    background: var(--bg);
}

.display-box {
    background: #fcfcfc;
    border-radius: 12px;
    padding: 25px;
    text-align: center;
    margin-bottom: 25px;
    border: 1px solid var(--border);
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.03);
}

.preset-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
}

.btn-action {
    background: white;
    border: 1px solid var(--border);
    color: #333;
    padding: 15px;
    border-radius: 8px;
    font-size: 1rem;
    cursor: pointer;
    font-weight: 500;
    -webkit-tap-highlight-color: transparent;
}

.btn-action:active {
    background: #f0f0f0;
    border-color: #bbb;
}

.btn-bar {
    background: #f0f0f0;
    border-color: #ccc;
}

#mode3 .calc-content {
    padding-bottom: 20px;
    /* 最下部のボタンがバーに被らないように */
}

/* Mode 4 Specific */
.manual-row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 15px;
    margin-bottom: 15px;
}

.manual-row input {
    width: 140px;
    background: #fff;
    border: 2px solid var(--border);
    color: #000;
    font-size: 2.5rem;
    text-align: center;
    border-radius: 10px;
    padding: 10px;
    font-weight: bold;
}

.footer {
    padding: 20px 15px 20px;
    /* 下部を40pxに：Safariのバー対策 */
    background: #f8f8f8;
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

/* PWA Promotion */
.add-home-banner {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    background: #222;
    color: white;
    padding: 15px 20px;
    box-sizing: border-box;
    display: none;
    justify-content: space-between;
    align-items: center;
    z-index: 10000;
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.3);
    border-top: 2px solid var(--accent);
}

.add-home-text {
    font-size: 0.9rem;
    flex: 1;
    line-height: 1.4;
}

.add-home-btn {
    background: var(--accent);
    color: white;
    border: none;
    padding: 8px 15px;
    border-radius: 20px;
    font-size: 0.8rem;
    margin-left: 10px;
    cursor: pointer;
    font-weight: bold;
    white-space: nowrap;
}

.add-home-close {
    background: none;
    border: none;
    color: #888;
    font-size: 1.5rem;
    margin-left: 10px;
    cursor: pointer;
    padding: 0 5px;
    line-height: 1;
}

.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    z-index: 10001;
    display: none;
    justify-content: center;
    align-items: center;
    backdrop-filter: blur(2px);
}

.modal-box {
    background: white;
    width: 85%;
    max-width: 400px;
    padding: 30px 25px;
    border-radius: 12px;
    text-align: center;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
    animation: popIn 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@keyframes popIn {
    from {
        transform: scale(0.9);
        opacity: 0;
    }

    to {
        transform: scale(1);
        opacity: 1;
    }
}

.modal-title {
    font-size: 1.3rem;
    font-weight: bold;
    margin-bottom: 20px;
    color: #222;
}

.modal-desc {
    font-size: 1rem;
    color: #555;
    line-height: 1.8;
    margin-bottom: 30px;
    text-align: left;
    background: #f5f5f5;
    padding: 20px;
    border-radius: 8px;
}

.modal-close-btn {
    background: #333;
    color: white;
    border: none;
    padding: 12px 40px;
    border-radius: 30px;
    font-size: 1.1rem;
    cursor: pointer;
    font-weight: bold;
    width: 100%;
}

.share-icon-inline {
    display: inline-block;
    width: 18px;
    vertical-align: -3px;
    fill: #007aff;
}

.menu-icon-inline {
    display: inline-block;
    width: 18px;
    vertical-align: -3px;
    fill: #555;
}

/* Help Page Styles */
.help-card {
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 20px;
    margin-bottom: 20px;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.02);
}

.help-card h3 {
    margin-top: 0;
    margin-bottom: 10px;
    font-size: 1.1rem;
    color: var(--accent);
    border-bottom: 1px solid #f0f0f0;
    padding-bottom: 8px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.help-desc {
    font-size: 0.95rem;
    color: #555;
    line-height: 1.6;
}

.help-sub {
    font-size: 0.8rem;
    color: #999;
    margin-top: 5px;
    display: block;
}

.help-img {
    width: 100%;
    max-width: 360px;
    margin: 0 auto 10px;
    height: auto;
    border-radius: 6px;
    border: 1px solid #eee;
    display: block;
}

/* --- Dashboard View (Home) --- */
/* --- Dashboard View (Home) --- */
.dashboard-title {
    text-align: center;
    font-size: 1.5rem;
    margin: 0 0 25px;
    color: var(--text);
    font-weight: bold;
    line-height: 1.4;
}

@media (max-width: 600px) {
    .dashboard-title {
        font-size: 1.2rem;
        margin-bottom: 20px;
    }

    .dashboard-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        /* Force 2 columns */
    }
}

.dashboard-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 15px;
    width: 100%;
    max-width: 500px;
    margin: 0 auto;
}

.dash-card {
    background: #fff;
    border-radius: 16px;
    padding: 20px 10px;
    /* Reduced side padding */
    text-align: center;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
    border: 1px solid var(--border);
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 160px;
    /* Ensure tall enough */
    height: auto;
    /* Allow growth */
}

.dash-card:active {
    transform: scale(0.98);
    background-color: #f0f0f0;
}

.dash-icon {
    width: 50px;
    /* Slightly smaller icon container */
    height: 50px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 10px;
}

.dash-icon svg {
    width: 28px;
    height: 28px;
    fill: white;
}

/* Mobile Responsiveness for Dashboard */
@media (max-width: 600px) {
    .dashboard-grid {
        grid-template-columns: 1fr;
        /* Single column on mobile */
        max-width: 100%;
    }

    .dash-card {
        flex-direction: column !important;
        /* Force Tile UP, Text DOWN */
        min-height: auto;
        padding: 20px;
        align-items: center;
        text-align: center;
        height: auto;
    }

    .dash-icon {
        margin-bottom: 10px;
        margin-right: 0;
    }

    /* Ensure container allows clear display */
    #mode0 {
        padding-top: 80px !important;
    }
}

.dash-card h2 {
    font-size: 1.1rem;
    margin: 0 0 5px;
    color: var(--text);
}

.dash-card p {
    font-size: 0.8rem;
    color: #888;
    margin: 0;
}

/* --- Mode 3 (Barbell Calc) Layout --- */
#mode3.active {
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
}

#mode3 .calc-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    height: 100%;
    width: 100%;
    max-width: 600px;
    margin: 0 auto;
}

/* Scrollable area for history and buttons */
.mode3-scrollable {
    flex: 1;
    overflow-y: auto;
    width: 100%;
    padding: 10px 0 30px;
    /* Bottom padding for easy access */
    -webkit-overflow-scrolling: touch;
}

/* --- Mode 4 (Weight Conv) Layout --- */
#mode4.active {
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
}

#mode4 .calc-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    height: 100%;
    width: 100%;
    max-width: 600px;
    margin: 0 auto;
}

.mode4-scrollable {
    flex: 1;
    overflow-y: auto;
    width: 100%;
    padding: 10px 0 30px;
    -webkit-overflow-scrolling: touch;
}

/* Tabs for Mode 4 */
.tab-header {
    display: flex;
    justify-content: center;
    gap: 20px;
    margin-bottom: 20px;
    border-bottom: 1px solid #eee;
}

.tab-btn {
    background: transparent;
    border: none;
    font-size: 0.95rem;
    font-weight: bold;
    color: #aaa;
    padding: 10px 15px;
    cursor: pointer;
    border-bottom: 3px solid transparent;
    transition: all 0.2s;
    outline: none;
}

.tab-btn.active {
    color: var(--accent);
    border-bottom-color: var(--accent);
}

.tab-content {
    animation: fadeIn 0.3s ease;
}

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

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* RM Matrix Styles */
.rm-matrix-wrapper {
    overflow-x: auto;
    width: 100%;
    margin-bottom: 20px;
    -webkit-overflow-scrolling: touch;
    border-radius: 8px;
    border: 1px solid var(--border);
}

.rm-matrix-table {
    width: 100%;
    border-collapse: separate;
    /* Required for sticky */
    border-spacing: 0;
    font-size: 0.9rem;
    background: #fff;
    min-width: 500px;
    /* Ensure it scrolls on small screens */
}

.rm-matrix-table th,
.rm-matrix-table td {
    padding: 10px 8px;
    text-align: center;
    border-right: 1px solid #eee;
    border-bottom: 1px solid #eee;
    vertical-align: middle;
}

/* Sticky Headers */
.rm-matrix-table thead th {
    position: sticky;
    top: 0;
    background: #f9f9f9;
    z-index: 10;
    font-weight: bold;
    color: #555;
}

/* Sticky First Column */
.rm-matrix-table tbody th {
    position: sticky;
    left: 0;
    background: #f9f9f9;
    z-index: 5;
    font-weight: bold;
    color: var(--accent);
    border-right: 2px solid #ddd;
    /* Separator */
}

.rm-matrix-table thead th:first-child {
    position: sticky;
    left: 0;
    top: 0;
    z-index: 15;
    background: #f0f0f0;
    border-right: 2px solid #ddd;
}

.cell-diagonal {
    background-color: #fff0f0;
    /* Gentle highlight */
    border: 2px solid var(--accent) !important;
    /* Emphasize */
}

.val-lb {
    display: block;
    font-size: 1.05rem;
    font-weight: bold;
    color: #333;
    line-height: 1.2;
}

.val-kg {
    display: block;
    font-size: 0.75rem;
    color: #999;
    margin-top: 2px;
}