:root {
    color-scheme: dark;
    --bg-color: #121212;
    --card-bg: #1e1e1e;
    --text-color: #ffffff;
    --text-muted: #aaaaaa;
    --text-soft: #d4d4d4;
    --time-color: #ffffff;
    --heading-color: #e8e8e8;
    --chrome-bg: #000000;
    --surface-elevated: #1b1b1b;
    --border-color: #333333;
    --border-subtle: #222222;
    --border-muted: #2a2a2a;
    --hover-bg: rgba(255, 255, 255, 0.08);
    --hover-bg-soft: rgba(255, 255, 255, 0.06);
    --guide-label: #a8a8a8;
    --guide-dismiss-border: #555555;
    --guide-dismiss-bg: #2a2a2a;
    --guide-dismiss-hover-bg: #333333;
    --guide-dismiss-hover-border: #777777;
    --primary: #00adb5;
    --start-btn: #2ecc71;
    --stop-btn: #e74c3c;
    --reset-btn: #95a5a6;
    --lap-btn: #3498db;
    --on-primary: #ffffff;
    --disabled-btn-text: #666666;
    --lap-open-ring: rgba(255, 255, 255, 0.5);
    --overlay-scrim: rgba(0, 0, 0, 0.55);
    --font-time: "M PLUS Rounded 1c", "Hiragino Maru Gothic ProN", "Hiragino Sans", sans-serif;
    --pc-heading-h: 40px;
    --sw-stage-min-h: calc(100vh - var(--pc-heading-h));
    --sw-stage-min-h: calc(100dvh - var(--pc-heading-h));
    --sw-stage-gap: 28px;
    --sw-btn-row-h: 48px;
    --sw-time-h: clamp(2.8rem, 12vw, 7rem);
    --sw-lap-list-max-h: min(36vh, 280px);
}

[data-theme='light'] {
    color-scheme: light;
    --bg-color: #eef1f4;
    --card-bg: #ffffff;
    --text-color: #1c1917;
    --text-muted: #475569;
    --text-soft: #334155;
    --time-color: #0a0a0a;
    --heading-color: #0a0a0a;
    --chrome-bg: #f8fafc;
    --surface-elevated: #ffffff;
    --border-color: #e2e8f0;
    --border-subtle: #e8edf2;
    --border-muted: #e2e8f0;
    --hover-bg: rgba(15, 23, 42, 0.06);
    --hover-bg-soft: rgba(15, 23, 42, 0.04);
    --guide-label: #475569;
    --guide-dismiss-border: #cbd5e1;
    --guide-dismiss-bg: #f1f5f9;
    --guide-dismiss-hover-bg: #e2e8f0;
    --guide-dismiss-hover-border: #94a3b8;
    --primary: #129499;
    --start-btn: #31a86a;
    --stop-btn: #c9443f;
    --reset-btn: #7a9097;
    --lap-btn: #3685b0;
    --lap-open-ring: rgba(18, 148, 153, 0.35);
    --disabled-btn-text: #94a3b8;
    --overlay-scrim: rgba(15, 23, 42, 0.4);
}

* {
    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;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
}

.app-shell {
    position: relative;
    display: flex;
    flex: 1;
    flex-direction: column;
    min-height: 0;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
}

/* ===================================================
   見出しバー
====================================================== */
.page-heading-bar {
    position: relative;
    flex-shrink: 0;
    height: var(--pc-heading-h);
    overflow: visible;
    z-index: 6;
    background-color: var(--chrome-bg);
    border-bottom: 1px solid var(--border-subtle);
}

.page-heading {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    color: var(--heading-color);
    font-size: 1rem;
    font-weight: 700;
    letter-spacing: 0.02em;
    line-height: 1.35;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    /* 右アイコン列（36px×4）とぶつからない左右対称パディング。画面中央に置く */
    padding: 0 152px;
    border: none;
    margin: 0;
    pointer-events: none;
}

.page-heading-actions {
    position: absolute;
    right: 2px;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    align-items: center;
    gap: 0;
    z-index: 2;
}

.help-open-btn,
.theme-toggle,
.sw-icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    padding: 0;
    border: none;
    border-radius: 8px;
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
}

.help-open-btn svg,
.theme-toggle svg,
.sw-icon-btn svg {
    width: 20px;
    height: 20px;
}

.help-open-btn:hover,
.theme-toggle:hover,
.sw-icon-btn:hover {
    background: var(--hover-bg-soft);
    color: var(--text-color);
}

.help-open-btn:focus-visible,
.theme-toggle:focus-visible,
.sw-icon-btn:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 1px;
}

.theme-toggle-sun {
    display: none;
}

.theme-toggle-moon {
    display: flex;
}

[data-theme='dark'] .theme-toggle-sun {
    display: flex;
}

[data-theme='dark'] .theme-toggle-moon {
    display: none;
}

.sw-fs-enter {
    display: flex;
}

.sw-fs-exit-icon {
    display: none;
}

.app-shell.is-fullscreen .sw-fs-enter {
    display: none;
}

.app-shell.is-fullscreen .sw-fs-exit-icon {
    display: flex;
}

/* ===================================================
   設定シート
====================================================== */
.sw-settings-overlay {
    position: absolute;
    inset: 0;
    z-index: 110;
    background: var(--overlay-scrim);
}

.sw-settings-sheet {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    z-index: 120;
    width: min(100%, 360px);
    background: var(--surface-elevated);
    color: var(--text-color);
    box-shadow: -4px 0 24px rgba(0, 0, 0, 0.18);
    transform: translateX(100%);
    transition: transform 0.22s ease;
    display: flex;
    flex-direction: column;
}

.sw-settings-sheet.is-open {
    transform: translateX(0);
}

.sw-settings-sheet__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    border-bottom: 1px solid var(--border-subtle);
    flex-shrink: 0;
}

.sw-settings-sheet__title {
    margin: 0;
    font-size: 1.0625rem;
    font-weight: 700;
}

.sw-settings-close-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border: none;
    border-radius: 8px;
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
}

.sw-settings-close-btn svg {
    width: 20px;
    height: 20px;
}

.sw-settings-close-btn:hover {
    background: var(--hover-bg-soft);
    color: var(--text-color);
}

.sw-settings-close-btn:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 1px;
}

.sw-settings-sheet__body {
    flex: 1;
    overflow-y: auto;
    padding: 8px 0 24px;
    -webkit-overflow-scrolling: touch;
}

.sw-settings-section {
    padding: 14px 16px;
    border-bottom: 1px solid var(--border-subtle);
}

.sw-settings-section__title {
    margin: 0 0 10px;
    font-size: 0.8125rem;
    font-weight: 700;
    color: var(--text-muted);
}

.sw-settings-note {
    margin: 0 0 12px;
    font-size: 0.8125rem;
    line-height: 1.5;
    color: var(--text-muted);
}

.sw-settings-action {
    display: block;
    width: 100%;
    margin: 0 0 8px;
    padding: 12px 14px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--card-bg);
    color: var(--text-color);
    font-size: 0.9375rem;
    font-weight: 600;
    line-height: 1.35;
    text-align: left;
    cursor: pointer;
}

.sw-settings-action:last-child {
    margin-bottom: 0;
}

.sw-settings-action:hover {
    background: var(--hover-bg-soft);
}

.sw-settings-action:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 1px;
}

.sw-settings-radios {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.sw-settings-radio {
    display: flex;
    gap: 10px;
    align-items: flex-start;
    margin: 0;
    padding: 10px 12px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--card-bg);
    cursor: pointer;
}

.sw-settings-radio:has(input:checked) {
    border-color: var(--primary);
    background: var(--hover-bg-soft);
}

.sw-settings-radio input {
    margin-top: 3px;
    flex-shrink: 0;
    width: 16px;
    height: 16px;
    accent-color: var(--primary);
}

.sw-settings-radio span {
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-width: 0;
}

.sw-settings-radio strong {
    font-size: 0.875rem;
    font-weight: 700;
    color: var(--text-color);
    line-height: 1.35;
}

.sw-settings-radio__desc {
    font-size: 0.75rem;
    font-weight: 400;
    line-height: 1.45;
    color: var(--text-muted);
}

/* ===================================================
   スクロール領域：ステージ＋ガイド＋フッター
====================================================== */
.timer-display-area {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    background-color: var(--bg-color);
}

.sw-stage {
    position: relative;
    flex: 0 0 auto;
    min-height: var(--sw-stage-min-h);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--sw-stage-gap);
    width: 100%;
    padding: 32px 20px 40px;
    background-color: var(--bg-color);
}

.sw-fs-exit {
    display: none;
    position: absolute;
    top: 12px;
    right: 12px;
    z-index: 2;
    appearance: none;
    border: 1px solid var(--border-color);
    border-radius: 999px;
    background: var(--surface-elevated);
    color: var(--text-color);
    font-size: 0.875rem;
    font-weight: 700;
    padding: 8px 14px;
    cursor: pointer;
}

.sw-fs-exit:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

.sw-stage:fullscreen,
.sw-stage:-webkit-full-screen {
    min-height: 100%;
    min-height: 100dvh;
    width: 100%;
    justify-content: flex-start;
    background-color: var(--bg-color);
    /* 見出しバー分を足して、通常表示と同じ画面上の位置にする */
    padding: calc(var(--sw-stage-pad-top, 32px) + var(--pc-heading-h))
        var(--sw-stage-pad-x, 20px)
        var(--sw-stage-pad-bottom, 40px);
    gap: var(--sw-stage-gap);
}

.sw-stage:fullscreen .sw-fs-exit,
.sw-stage:-webkit-full-screen .sw-fs-exit {
    display: inline-flex;
}

.sw-stage:fullscreen .time-display,
.sw-stage:-webkit-full-screen .time-display {
    /* 16vw / 12rem だと横にはみ出す。通常サイズと「幅に収まるサイズ」の小さい方 */
    font-size: min(var(--sw-time-h), calc((100vw - 3rem) / 7.9));
}

.time-display {
    font-size: var(--sw-time-h);
    font-family: var(--font-time);
    font-weight: 700;
    color: var(--time-color);
    font-variant-numeric: tabular-nums;
    letter-spacing: 0.04em;
    line-height: 1;
    user-select: none;
    text-align: center;
    white-space: nowrap;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: geometricPrecision;
}

.time-frac {
    font-size: 0.62em;
    font-weight: 700;
    opacity: 0.75;
    margin-left: 0.05em;
}

.lap-split .time-frac,
.lap-total .time-frac {
    font-size: 0.85em;
    opacity: 0.8;
}

.btn-group {
    display: flex;
    gap: 12px;
    width: min(100%, 420px);
}

.btn {
    flex: 1;
    min-height: 48px;
    padding: 12px 0;
    border: none;
    border-radius: 10px;
    font-size: 1rem;
    font-weight: bold;
    cursor: pointer;
    background: transparent;
    color: var(--text-color);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    transition: background-color 0.15s, opacity 0.15s, color 0.15s;
}

.btn:hover {
    background: var(--hover-bg-soft);
}

.btn:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

.btn-label {
    color: var(--text-color);
    font-weight: 700;
    line-height: 1.2;
}

.btn-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    font-size: 0.95rem;
    flex-shrink: 0;
}

.btn-icon svg {
    width: 1.1rem;
    height: 1.1rem;
    fill: none;
    stroke: currentColor;
    stroke-width: 2.1;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.btn-icon--play {
    margin-left: 0.05em;
}

.btn-toggle-run .btn-icon--stop {
    display: none;
}

.btn-toggle-run.running .btn-icon--play {
    display: none;
}

.btn-toggle-run.running .btn-icon--stop {
    display: inline-flex;
    font-size: 0.85rem;
}

.btn-toggle-run .btn-icon { color: var(--start-btn); }
.btn-toggle-run.running .btn-icon { color: var(--stop-btn); }
.btn-lap .btn-icon { color: var(--lap-btn); }
.btn-lap.lap-open {
    box-shadow: inset 0 0 0 2px var(--lap-open-ring);
}
.btn-lap:disabled {
    opacity: 0.35;
    cursor: not-allowed;
}
.btn-reset .btn-icon { color: var(--reset-btn); }

.btn-reset.btn-disabled {
    opacity: 0.45;
    cursor: pointer;
    background-color: transparent !important;
    color: var(--disabled-btn-text) !important;
}

.btn-reset.btn-disabled .btn-icon,
.btn-reset.btn-disabled .btn-label {
    color: var(--disabled-btn-text) !important;
}

.lap-list {
    display: none;
    width: min(100%, 420px);
    max-height: var(--sw-lap-list-max-h);
    overflow-y: auto;
    scrollbar-gutter: stable;
    border-top: 1px solid var(--border-color);
    padding-top: 8px;
    padding-right: 12px;
    box-sizing: border-box;
}

.sw-stage.lap-open .lap-list {
    display: block;
}

.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: 6px 0;
    font-size: 14px;
    color: var(--text-muted);
    border-bottom: 1px solid var(--border-muted);
}

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

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

.lap-split {
    flex: 1;
    text-align: center;
    font-family: var(--font-time);
    font-weight: 700;
    font-size: 16px;
    color: var(--time-color);
    font-variant-numeric: tabular-nums;
}

.lap-total {
    flex: 1;
    text-align: right;
    font-family: var(--font-time);
    font-weight: 700;
    font-size: 15px;
    color: var(--time-color);
    font-variant-numeric: tabular-nums;
}

/* ===================================================
   ガイド＋フッター
====================================================== */
.sw-guide {
    background: var(--surface-elevated);
    border-top: 1px solid var(--border-color);
    flex-shrink: 0;
}

.sw-guide[hidden] {
    display: none;
}

html.sw-guide-hidden .sw-guide {
    display: none;
}

.sw-guide__toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    max-width: 760px;
    margin: 0 auto;
    padding: 14px 20px 0;
}

.sw-guide__toolbar-label {
    margin: 0;
    color: var(--guide-label);
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 0.04em;
}

.sw-guide__dismiss {
    flex-shrink: 0;
    appearance: none;
    border: 1px solid var(--guide-dismiss-border);
    border-radius: 999px;
    background: var(--guide-dismiss-bg);
    color: var(--text-color);
    font-size: 13px;
    font-weight: 700;
    line-height: 1;
    padding: 8px 14px;
    cursor: pointer;
}

.sw-guide__dismiss:hover {
    background: var(--guide-dismiss-hover-bg);
    border-color: var(--guide-dismiss-hover-border);
}

.sw-guide__content {
    max-width: 760px;
    margin: 0 auto;
    padding: 12px 20px 28px;
}

.sw-guide__content h2,
.site-footer__title {
    color: var(--text-color);
    font-size: 1.05rem;
    font-weight: 700;
    line-height: 1.4;
    margin: 0 0 10px;
}

.sw-guide__content h2:not(:first-child) {
    margin-top: 22px;
}

.sw-guide__content p,
.sw-guide__content li,
.sw-guide__content dd,
.site-footer small {
    color: var(--text-soft);
    font-size: 14px;
    line-height: 1.7;
}

.sw-guide__content ul {
    margin: 0 0 12px 1.2em;
}

.sw-guide__content a {
    color: var(--primary);
    text-decoration: none;
}

.sw-guide__content a:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

.sw-guide__faq {
    margin: 0;
}

.sw-guide__faq dt {
    color: var(--text-color);
    font-size: 14px;
    font-weight: 700;
    line-height: 1.6;
}

.sw-guide__faq dd {
    margin: 0 0 14px;
}

.site-footer {
    width: 100%;
    flex-shrink: 0;
    margin-top: auto;
    padding: 20px 20px calc(24px + env(safe-area-inset-bottom));
    background: var(--surface-elevated);
    border-top: 1px solid var(--border-muted);
}

.site-footer__title {
    max-width: 760px;
    margin: 0 auto 8px;
}

.site-footer__links {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 10px 16px;
    max-width: 760px;
    margin: 0 auto 8px;
}

.site-footer small {
    display: block;
    max-width: 760px;
    margin: 0 auto;
}

.site-footer a,
.site-footer__guide-link {
    color: var(--primary);
    font-family: inherit;
    font-size: 14px;
    font-weight: 400;
    line-height: 1.5;
    text-decoration: none;
}

.site-footer__guide-link {
    appearance: none;
    -webkit-appearance: none;
    border: 0;
    background: transparent;
    margin: 0;
    padding: 0;
    cursor: pointer;
    color: var(--primary);
}

.site-footer a:focus-visible,
.site-footer__guide-link:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

@media (min-width: 769px) {
    :root {
        --sw-time-h: clamp(4.75rem, 12.5vw, 10.5rem);
        --sw-btn-row-h: 56px;
        --sw-stage-gap: 32px;
        --sw-stage-pad-bottom: 40px;
        --sw-stage-pad-x: 20px;
        /*
         * 数字の中心が画面高さの約40%（見た目の中心より少し上）。
         * 以前は「中央 − 96px」だったため、短い窓では下限24pxに張り付き、
         * 550px超では余白だけ伸びて位置の割合が変わっていた。
         */
        --sw-stage-pad-top: max(
            32px,
            calc(40vh - var(--pc-heading-h) - (var(--sw-time-h) / 2))
        );
    }

    .sw-stage {
        justify-content: flex-start;
        padding: var(--sw-stage-pad-top) 20px var(--sw-stage-pad-bottom);
    }

    .time-frac {
        font-size: 0.58em;
    }

    .btn-group {
        width: min(100%, 560px);
        gap: 16px;
    }

    .btn {
        min-height: var(--sw-btn-row-h);
        padding: 14px 0;
        font-size: 1.125rem;
    }

    .btn-icon svg {
        width: 1.25rem;
        height: 1.25rem;
    }

    .lap-list {
        width: min(100%, clamp(24rem, 42vw, 30rem));
        padding-right: 16px;
    }

    .lap-item {
        padding: 8px 0;
        font-size: 15px;
        justify-content: space-between;
        gap: clamp(1rem, 1.6vw, 1.75rem);
    }

    .lap-label,
    .lap-split,
    .lap-total {
        flex: 0 1 auto;
    }

    .lap-label {
        min-width: 5.5em;
    }

    .lap-split {
        font-size: 20px;
        text-align: center;
    }

    .lap-total {
        font-size: 18px;
    }
}

@media (max-width: 768px) {
    :root {
        --sw-stage-gap: 22px;
        --sw-btn-row-h: 48px;
        --sw-stage-pad-bottom: 32px;
        --sw-stage-pad-x: 16px;
        --sw-stage-nudge: 48px;
        /*
         * 数字＋ボタン＋（上限まで伸びたラップ一覧）を一塊として中央に置いたときの
         * 上端位置。justify-content:center だとラップ0件時は塊が低く、上限到達後は
         * 数字が上へ逃げる。flex-start + この余白で本体位置を固定する。
         * nudge で、その基準より一段下げる。
         */
        --sw-primary-block-h: calc(
            var(--sw-time-h) + var(--sw-btn-row-h) + (var(--sw-stage-gap) * 2)
        );
        --sw-stage-pad-top: max(
            20px,
            calc(
                (
                        var(--sw-stage-min-h)
                        - var(--sw-primary-block-h)
                        - var(--sw-lap-list-max-h)
                        - var(--sw-stage-pad-bottom)
                    )
                    / 2
                    + var(--sw-stage-nudge)
            )
        );
    }

    .page-heading {
        padding: 0 12px;
    }

    .page-heading-actions {
        top: calc(100% + 6px);
        right: 6px;
        transform: none;
    }

    .help-open-btn,
    .theme-toggle,
    .sw-icon-btn {
        width: 34px;
        height: 34px;
    }

    .sw-stage {
        justify-content: flex-start;
        padding: var(--sw-stage-pad-top) 16px var(--sw-stage-pad-bottom);
    }

    .btn-group {
        gap: 8px;
    }

    .btn {
        min-height: var(--sw-btn-row-h);
        padding: 12px 0;
        font-size: 1rem;
        border-radius: 8px;
    }

    .lap-item {
        justify-content: space-between;
        gap: clamp(0.75rem, 4.5vw, 1.5rem);
        width: min(100%, clamp(18rem, 82vw, 23rem));
        margin-inline: auto;
    }

    .lap-label,
    .lap-split,
    .lap-total {
        flex: 0 1 auto;
    }

    .lap-label {
        min-width: 4.6em;
    }
}
