:root {
    --bg-color: #121212;
    --card-bg: #1e1e1e;
    --card-active-bg: #252525;
    --text-color: #ffffff;
    --text-muted: #aaaaaa;
    --primary: #00adb5;         /* 追加・フッターボタン：ターコイズブルー */
    --start-btn: #2ecc71;       /* スタート：鮮やかな緑 */
    --stop-btn: #e74c3c;        /* ストップ：鮮やかな赤 */
    --reset-btn: #95a5a6;       /* リセット：スタイリッシュなグレー */
    --lap-btn: #3498db;         /* ラップ：爽やかなブルー */
    --toggle-btn-bg: #2a2a2a;
    
    /* 2行目（テキストリンク）用のすっきりした文字色 */
    --expand-text: #3498db;     /* 全て開く：爽やかなブルー */
    --collapse-text: #e67e22;   /* すべて閉じる：優しいオレンジ */
    --init-text: #95a5a6;       /* 初期状態にする：落ち着いたライトグレー */

    /* SP用レイアウト寸法 */
    --sp-add-btn-h: 54px;
    --sp-scroll-hint-h: 32px;
    --sp-collapsed-card-h: 84px;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    -webkit-tap-highlight-color: transparent;
}

body {
    background-color: var(--bg-color);
    color: var(--text-color);
    display: flex;
    flex-direction: column;
    /* 100vhはブラウザのアドレスバーやアプリ内ブラウザのツールバーが「隠れている」前提の最大高さになるため、
       それらが表示された状態（実際に見えている高さ）より大きくなり、overflow:hiddenと組み合わさると
       最下部（追加ボタン）が画面外にはみ出してスクロールでも出せなくなる。
       dvh（動的ビューポート高さ）に対応しているブラウザでは、実際に見えている高さに追従させて解消する。 */
    height: 100vh; /* dvh未対応ブラウザ向けフォールバック */
    height: 100dvh;
    overflow: hidden;
}

/* ===================================================
   共通ヘッダー：PCベース（幅を適切なサイズに固定）
====================================================== */
header {
    background-color: #000;
    padding: 10px 20px;
    display: flex;
    justify-content: flex-start;
    gap: 12px;
    border-bottom: 1px solid #333;
    z-index: 10;
    height: 54px;
}

.global-btn {
    height: 100%;
    padding: 0 24px;
    border: none;
    border-radius: 6px;
    font-weight: bold;
    cursor: pointer;
    color: #fff;
    font-size: 13px;
    text-align: center;
    white-space: nowrap;
    flex: 0 1 auto; 
    min-width: 120px;
    max-width: 160px;
    transition: background-color 0.2s, opacity 0.2s;
}

/* ボタンごとの色割り当て */
.start-g { background-color: var(--start-btn); }
.stop-g { background-color: var(--stop-btn); }
.reset-g { background-color: var(--reset-btn); }

/* PCでは開閉ボタンは不要なので完全に非表示にする */
.expand-g, .collapse-g {
    display: none;
}

/* PCの「初期状態にする」は、控えめなテキストリンク風にして右端に隔離 */
.init-g {
    background-color: transparent;
    color: var(--init-text);
    font-weight: normal;
    min-width: auto;
    max-width: none;
    padding: 0 10px;
    margin-left: auto; /* これで右端にぴったり寄ります */
    display: flex;
    align-items: center;
}
.init-g::after {
    content: " ↺";
    font-size: 14px;
    margin-left: 4px;
}

.global-btn:active {
    opacity: 0.8;
}

/* ===================================================
   タイマー表示領域（PC）
====================================================== */
.timer-display-area {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

/* PCでは追加ボタンが独立した固定ボタンとして機能するよう、レイアウトに影響しない透過コンテナにする */
.bottom-bar {
    display: contents;
}

.scroll-hint {
    display: none;
}

.header-main {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    gap: 10px;
}

/* ===================================================
   PC環境用のレイアウト（基本設定）
====================================================== */
.container {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-auto-rows: calc((100vh - 54px) / 2);
    height: calc(100vh - 54px);
    gap: 2px;
    background-color: #333;
    overflow-y: auto;
}

/* 💡【修正】タイマー追加ボタンの基本スタイル（通常時の色をここで確定） */
.add-btn {
    background-color: var(--primary); /* ターコイズブルーを適用 */
    color: #ffffff;
    border: none;
    cursor: pointer;
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.2s, opacity 0.2s;
}
.add-btn:active {
    opacity: 0.8;
}

@media (min-width: 769px) {
    .add-btn {
        position: fixed;
        bottom: 20px;
        right: 20px;
        height: 50px;
        padding: 0 24px; /* PCでのクリック領域・幅を確保 */
        border-radius: 25px !important;
        box-shadow: 0 4px 12px rgba(0,0,0,0.3);
        z-index: 100;
    }
}

.stopwatch-card {
    background-color: var(--card-bg);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 20px;
    position: relative;
}

/* PCでは開閉ボタンを完全に隠す */
.toggle-trigger {
    display: none !important;
}

.card-header {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
    gap: 10px;
    position: relative;
}

.delete-card-btn {
    position: absolute;
    top: -10px;
    right: 10px;
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 18px;
    cursor: pointer;
    padding: 5px;
}

.title-input {
    background: transparent;
    border: none;
    border-bottom: 1px dashed var(--text-muted);
    color: var(--text-muted);
    text-align: center;
    font-size: 16px;
    padding: 5px;
    width: 70%;
    outline: none;
}

.title-input:focus {
    border-bottom: 1px solid var(--primary);
    color: var(--text-color);
}

.time-display {
    font-size: clamp(1.8rem, 4vw, 3rem);
    font-family: "Courier New", Courier, monospace;
    font-weight: bold;
    color: var(--text-color);
}

/* PC用：ボタン・グループ */
.btn-group {
    display: flex !important;
    gap: 12px;
    width: 80%;
    max-width: 300px;
    margin-top: 15px;
    opacity: 1 !important;
}

.btn {
    flex: 1;
    padding: 10px 0;
    border: none;
    border-radius: 8px;
    font-size: 14px;
    font-weight: bold;
    cursor: pointer;
    color: white;
}

/* 個別タイマーのスタート／ストップ連動スタイル */
.btn-toggle-run { background-color: var(--start-btn); }
.btn-toggle-run.running { background-color: var(--stop-btn); }
.btn-lap { background-color: var(--lap-btn); }
.btn-lap.lap-open { box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.5); }
.btn-lap:disabled {
    opacity: 0.35;
    cursor: not-allowed;
}
.btn-reset { background-color: var(--reset-btn); }

.title-area {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
}

.lap-count-badge {
    display: none;
    font-size: 11px;
    color: var(--lap-btn);
    line-height: 1.2;
    min-height: 14px;
}

/* ラップ一覧 */
.lap-list {
    display: none;
    width: 80%;
    max-width: 300px;
    margin-top: 10px;
    max-height: 120px;
    overflow-y: auto;
    border-top: 1px solid #333;
    padding-top: 8px;
}

.stopwatch-card.lap-open .lap-list {
    display: block;
}

/* ラップが1件もない場合は領域自体を確保しない */
.lap-list:empty {
    display: none !important;
    margin: 0 !important;
    padding: 0 !important;
    border: none !important;
    max-height: 0 !important;
}

.lap-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 4px 0;
    font-size: 13px;
    color: var(--text-muted);
    border-bottom: 1px solid #2a2a2a;
}

.lap-item:last-child {
    border-bottom: none;
}

.lap-label {
    flex: 1;
    text-align: left;
}

.lap-split {
    flex: 1;
    text-align: center;
    font-family: "Courier New", Courier, monospace;
    color: var(--text-color);
}

.lap-total {
    flex: 1;
    text-align: right;
    font-family: "Courier New", Courier, monospace;
    font-size: 12px;
}

/* 20個以上になった時の追加ボタンのスタイル */
.add-btn.btn-disabled {
    opacity: 0.5;
    cursor: not-allowed;
    background-color: #ccc !important;
    color: #666 !important;
}

/* ===================================================
   モバイル環境（SP）用スタイル
====================================================== */
@media (max-width: 768px) {
    header {
        padding: 8px 6px 4px 6px;
        gap: 6px;
        height: 78px;               
        flex-wrap: wrap !important; 
    }

    .start-g, .stop-g, .reset-g {
        /* .expand-g 系と同じ flex-basis 計算式に揃えることで、
           flex-wrap の折り返しが必ず「3個ごと」になるようにする */
        flex: 1 1 calc(33.33% - 4px) !important;
        min-width: 0 !important;
        max-width: none !important;
        height: 35px !important;
        font-size: 11px;
        padding: 0 4px !important;
        border-radius: 4px;
    }

    /* SPでは、非表示にしていた開閉ボタンを復活させ、3つ綺麗に横並びにします */
    .expand-g, .collapse-g, .init-g {
        display: flex !important; /* PCの display: none を打ち消す */
        flex: 1 1 calc(33.33% - 4px) !important;    
        height: 28px !important;                 
        background-color: transparent !important; 
        font-size: 11px;                         
        font-weight: normal !important;          
        align-items: center !important;
        justify-content: center !important;
        min-width: 0 !important;
        max-width: none !important;
        padding: 0 !important;
        margin-left: 0 !important; /* PCの「右端寄せ」をリセット */
    }

    .expand-g { color: var(--expand-text) !important; }
    .collapse-g { color: var(--collapse-text) !important; }
    .init-g { color: var(--init-text) !important; }

    .expand-g::after { content: " ▼" !important; font-size: 9px; margin-left: 1px; }
    .collapse-g::after { content: " ▲" !important; font-size: 9px; margin-left: 1px; }
    
    /* SP用のアイコンサイズ微調整 */
    .init-g::after { content: " ↺" !important; font-size: 11px; margin-left: 2px; }

    .start-g { order: 1 !important; }
    .stop-g { order: 2 !important; }
    .reset-g { order: 3 !important; }
    .expand-g { order: 4 !important; }
    .collapse-g { order: 5 !important; }
    .init-g { order: 6 !important; }

    /* タイマー表示領域：残りの高さいっぱいに広がる（下のバーの分は自動で圧縮される） */
    .timer-display-area {
        flex: 1 !important;
        min-height: 0 !important;
        height: auto !important;
        margin-bottom: 0 !important;
    }

    /* 最下部：スクロール案内（上）＋ 追加ボタン（下）を縦に並べる */
    .bottom-bar {
        display: flex !important;
        flex-direction: column !important;
        width: 100% !important;
        flex-shrink: 0 !important;
        box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.4);
    }

    .add-btn {
        position: static !important;
        width: 100% !important;
        height: var(--sp-add-btn-h) !important;
        max-width: none !important;
        min-width: 0 !important;
        border-radius: 0 !important;
        box-shadow: none !important;
        font-size: 0 !important;        
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
    }
    
    .add-btn::after {
        content: "＋ タイマーを追加" !important;
        font-size: 14px !important;
        font-weight: bold !important;
        color: #ffffff !important;
        letter-spacing: 1px;
    }

    .scroll-hint {
        display: none;
        width: 100%;
        height: var(--sp-scroll-hint-h);
        align-items: center;
        justify-content: center;
        background-color: var(--card-bg);
        border-top: 1px solid #252525;
        border-bottom: 1px solid #000;
    }

    .scroll-hint.visible {
        display: flex !important;
    }

    .scroll-hint-text {
        color: var(--text-muted);
        font-size: 12px;
        font-weight: bold;
    }

    .container {
        display: flex !important;
        flex-direction: column !important;
        flex: 1 !important;
        min-height: 0 !important;
        height: auto !important;
        margin-bottom: 0 !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch;
        background-color: var(--bg-color);
        gap: 1px;
    }

    .stopwatch-card {
        display: flex !important;
        flex-direction: column !important;
        justify-content: flex-start !important;
        align-items: center !important;
        width: 100% !important;
        height: var(--sp-collapsed-card-h) !important;
        min-height: var(--sp-collapsed-card-h) !important;
        padding: 0 !important;
        overflow: hidden !important;
        border-bottom: 1px solid #252525;
        flex-shrink: 0 !important;
        transition: background-color 0.2s;
    }

    .card-header {
        flex-direction: row !important;
        width: 100% !important;
        height: var(--sp-collapsed-card-h) !important;
        min-height: var(--sp-collapsed-card-h) !important;
        gap: 0 !important;
        justify-content: flex-start !important;
        align-items: stretch !important;
    }

    .header-main {
        order: 2 !important;
        flex: 1 !important;
        min-width: 0 !important;
        height: 100% !important;
        gap: 2px !important;
        justify-content: center !important;
        align-items: center !important;
        padding: 4px 6px !important;
    }

    .toggle-trigger {
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        width: 12% !important;
        min-width: 12% !important;
        height: 100% !important;
        background-color: var(--toggle-btn-bg) !important;
        color: var(--text-muted) !important;
        font-size: 10px !important;
        cursor: pointer !important;
        user-select: none !important;
    }
    
    .toggle-left { order: 1 !important; }

    .title-area {
        width: 100% !important;
        height: auto !important;
        padding-left: 0 !important;
        justify-content: center !important;
        align-items: flex-start !important;
        gap: 2px !important;
    }

    .title-input {
        text-align: left !important;
        width: 100% !important;
        padding: 0 !important;
        border-bottom: none !important;
        font-size: 13px !important;
        line-height: 1.2 !important;
    }

    .lap-count-badge {
        display: block !important;
        width: 100% !important;
        min-height: 14px !important;
    }

    .lap-count-badge:empty {
        visibility: hidden !important;
    }

    .time-display {
        width: 100% !important;
        font-size: clamp(1.85rem, 8vw, 2.4rem) !important;
        text-align: center !important;
        padding: 0 !important;
        line-height: 1.1 !important;
    }

    .delete-card-btn {
        position: static !important;
        order: 3 !important;
        width: 12% !important;
        min-width: 12% !important;
        height: 100% !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        font-size: 18px !important;
        color: var(--text-muted) !important;
        background: transparent !important;
    }

    .btn-group {
        display: flex !important;
        margin-top: 6px !important;
        margin-bottom: 6px !important;
        width: 90% !important;
        max-width: none !important;
        visibility: hidden !important;
        opacity: 0 !important;
        height: 0 !important;
        overflow: hidden !important;
        transition: opacity 0.1s;
    }

    .btn {
        padding: 8px 0 !important;
        font-size: 12px !important;
    }

    /* 開いた状態は、中身（ヘッダー＋ボタン＋ラップ）の実際の高さ分だけ確保する。
       ラップが無い時に余白ができないよう、固定の高さは持たせない */
    .stopwatch-card.active {
        background-color: var(--card-active-bg) !important;
        height: auto !important;
        min-height: 0 !important;
    }

    /* 開いた状態でもヘッダー部分の高さ・間隔は閉じた状態と同じに保つ */
    .stopwatch-card.active .card-header {
        height: var(--sp-collapsed-card-h) !important;
        min-height: var(--sp-collapsed-card-h) !important;
        flex-shrink: 0 !important;
    }

    .stopwatch-card.active .btn-group {
        visibility: visible !important;
        opacity: 1 !important;
        height: auto !important;
        flex-shrink: 0 !important;
    }

    .stopwatch-card.active .toggle-trigger::before { content: "▲" !important; }
    .stopwatch-card:not(.active) .toggle-trigger::before { content: "▼" !important; }
    
    .stopwatch-card.active .title-input {
        border-bottom: 1px dashed var(--text-muted) !important;
    }

    .stopwatch-card.active .lap-count-badge {
        display: none !important;
    }

    .lap-list {
        width: 90% !important;
        max-width: none !important;
        max-height: 140px !important;
        margin-bottom: 6px !important;
        flex-shrink: 1 !important;
    }
}