:root {
    --bg: #0d1a12;
    --panel: rgba(12, 28, 18, 0.92);
    --line: #6bcf8e;
    --text: #e8ffe8;
    --muted: rgba(232, 255, 232, 0.75);
    --accent: #8fd9a8;
    --hud-pad: 8px;
    --play-width: 100%;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
}

* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
}

body {
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    justify-content: center;
    align-items: center;
    background:
        radial-gradient(ellipse at 20% 10%, #1a3324 0%, transparent 50%),
        radial-gradient(ellipse at 80% 90%, #14261c 0%, transparent 45%),
        var(--bg);
    color: var(--text);
    font-family: "Segoe UI", "Noto Sans JP", sans-serif;
    padding:
        max(4px, var(--safe-top))
        max(4px, var(--safe-right))
        max(4px, var(--safe-bottom))
        max(4px, var(--safe-left));
}

#game-shell {
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
}

#game-stage {
    flex: 1 1 auto;
    min-height: 0;
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
}

/* UI 専用帯（マップとは重ならない） */
.hud-chrome {
    flex: 0 0 auto;
    pointer-events: auto;
}

#hud-top,
#hud-bottom {
    width: min(100%, var(--play-cluster-width, 100%));
    align-self: center;
}

#hud-top {
    display: flex;
    flex-direction: column;
    padding: var(--hud-pad) 0 4px;
}

#hud-info {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 10px;
    padding: 6px 8px;
    border: 1px solid rgba(107, 207, 142, 0.45);
    background: rgba(0, 0, 0, 0.45);
}

#hud-stats {
    flex: 1 1 auto;
    min-width: 0;
    display: flex;
    flex-wrap: wrap;
    gap: 6px 12px;
    font-size: 13px;
    line-height: 1.35;
}

#hud-stats span {
    white-space: nowrap;
}

#hud-top-actions {
    flex: 0 0 auto;
    flex-wrap: wrap;
    justify-content: flex-end;
    align-content: center;
    max-width: 46%;
}

#game-mid {
    flex: 1 1 auto;
    min-height: 0;
    min-width: 0;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0 var(--hud-pad);
}

#game-play {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 4px;
    max-width: 100%;
    max-height: 100%;
    flex: 0 1 auto;
    min-width: 0;
    min-height: 0;
}

#hud-aux {
    flex: 0 0 auto;
    width: min(240px, 28vw);
    height: var(--play-height, auto);
    max-height: 100%;
    display: flex;
    flex-direction: column;
    gap: 6px;
    min-height: 0;
    padding: 0;
}

.hud-help {
    flex: 1 1 50%;
    min-height: 0;
    overflow: auto;
    padding: 6px 8px;
    border: 1px solid rgba(107, 207, 142, 0.35);
    background: rgba(0, 0, 0, 0.4);
    font-size: 12px;
    line-height: 1.4;
    color: var(--muted);
    -webkit-overflow-scrolling: touch;
}

.hud-help strong {
    color: var(--text);
    font-weight: 600;
}

.hud-help ul {
    margin: 4px 0 0 0;
    padding-left: 1.2em;
}

.hud-help li {
    margin: 0;
}

#hud-log {
    flex: 1 1 50%;
    min-height: 0;
    overflow: auto;
    font-size: 11px;
    line-height: 1.4;
    opacity: 0.95;
    padding: 6px 8px;
    border: 1px solid rgba(107, 207, 142, 0.3);
    background: rgba(0, 0, 0, 0.4);
    white-space: pre-wrap;
    -webkit-overflow-scrolling: touch;
}

#game-viewport {
    position: relative;
    flex: 0 0 auto;
    min-height: 0;
    min-width: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    line-height: 0;
}

#game-viewport > canvas {
    border: 2px solid var(--line);
    image-rendering: pixelated;
    image-rendering: crisp-edges;
    display: block;
    flex: 0 0 auto;
}

/* スマホ: マップ選択後の確定（マップ上オーバーレイ） */
.map-confirm-btn {
    position: absolute;
    z-index: 5;
    min-width: 72px;
    min-height: 44px;
    padding: 8px 14px;
    border: 2px solid var(--accent);
    background: rgba(12, 28, 18, 0.92);
    color: var(--text);
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    touch-action: manipulation;
    pointer-events: auto;
}

.map-confirm-btn.pos-br {
    right: 6px;
    bottom: 6px;
    top: auto;
    left: auto;
}

.map-confirm-btn.pos-tr {
    right: 6px;
    top: 6px;
    bottom: auto;
    left: auto;
}

.map-confirm-btn:active {
    background: rgba(107, 207, 142, 0.35);
}

#hud-bottom {
    padding: 4px 0 var(--hud-pad);
}

#hud-tools {
    display: grid;
    grid-template-columns: repeat(6, minmax(0, 1fr));
    gap: 6px;
    padding: 6px;
    border: 1px solid rgba(107, 207, 142, 0.4);
    background: rgba(0, 0, 0, 0.45);
}

#hud-tools .tool-btn {
    width: 100%;
    justify-content: center;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
}

.tool-row {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

button.ui-btn {
    min-width: 96px;
    min-height: 40px;
    padding: 8px 10px;
    border: 2px solid var(--line);
    background: rgba(0, 0, 0, 0.55);
    color: var(--text);
    cursor: pointer;
    font-size: 14px;
    touch-action: manipulation;
}

button.ui-btn.small {
    min-width: 44px;
    min-height: 40px;
    font-size: 13px;
}

button.ui-btn:hover:not(:disabled) {
    background: rgba(107, 207, 142, 0.18);
}

button.ui-btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

button.ui-btn.active {
    background: rgba(107, 207, 142, 0.28);
    border-color: var(--accent);
}

button.tool-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    flex: 0 0 auto;
    white-space: nowrap;
}

button.tool-btn img {
    width: 24px;
    height: 24px;
    image-rendering: pixelated;
}

#ui-layer {
    position: fixed;
    inset: 0;
    z-index: 20;
    display: flex;
    justify-content: center;
    align-items: center;
    pointer-events: none;
}

.panel {
    pointer-events: auto;
    min-width: 320px;
    max-width: 520px;
    padding: 22px 24px;
    border: 2px solid var(--line);
    background: var(--panel);
    text-align: center;
}

.panel-help {
    max-width: min(720px, 94vw);
    max-height: min(86vh, 900px);
    display: flex;
    flex-direction: column;
    text-align: left;
    padding: 16px 18px 12px;
}

.help-body {
    overflow: auto;
    flex: 1;
    min-height: 0;
    padding-right: 6px;
    font-size: 13px;
    line-height: 1.5;
    color: var(--muted);
}

.help-body h1 {
    margin: 0 0 8px 0;
    font-size: 24px;
    color: var(--text);
    text-align: center;
}

.help-body h2 {
    margin: 16px 0 6px 0;
    font-size: 15px;
    color: var(--accent);
}

.help-body .sub {
    margin: 0 0 8px 0;
    font-size: 12px;
    text-align: center;
}

.help-body section ul {
    margin: 0;
    padding-left: 1.2em;
}

.help-body li {
    margin: 0.2em 0;
}

.help-body strong {
    color: var(--text);
    font-weight: 600;
}

.panel h1 {
    margin: 0 0 12px 0;
    font-size: 30px;
    letter-spacing: 0.04em;
}

.panel .sub {
    margin: 0 0 10px 0;
    font-size: 14px;
    color: var(--muted);
}

.settings-block {
    margin: 8px 0 4px;
    text-align: left;
}

.settings-label {
    display: block;
    margin-bottom: 8px;
    font-size: 14px;
    color: var(--text);
}

.settings-volume-row {
    display: flex;
    align-items: center;
    gap: 12px;
}

.settings-range {
    flex: 1;
    min-width: 120px;
    height: 28px;
    accent-color: var(--accent);
    cursor: pointer;
    touch-action: manipulation;
}

.settings-volume-value {
    min-width: 3.2em;
    text-align: right;
    font-size: 14px;
    color: var(--muted);
    font-variant-numeric: tabular-nums;
}

.btn-row {
    display: flex;
    gap: 10px;
    justify-content: center;
    flex-wrap: wrap;
    margin-top: 16px;
}

.rank-list {
    text-align: left;
    margin: 12px auto 0 auto;
    padding: 10px 12px;
    border: 1px solid rgba(107, 207, 142, 0.55);
    max-height: 280px;
    overflow: auto;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
    font-size: 14px;
    line-height: 1.6;
    white-space: pre;
    background: rgba(0, 0, 0, 0.35);
}

.name-input {
    width: 92%;
    max-width: 420px;
    padding: 10px 12px;
    border: 2px solid var(--line);
    background: rgba(0, 0, 0, 0.55);
    color: var(--text);
    font-size: 16px;
    outline: none;
}

.name-entry-input-wrap {
    margin-top: 10px;
}

.clear-day-label {
    margin-top: 12px;
}

.hide {
    display: none !important;
}

/* スマホ／狭い幅: フォント優先で HUD を圧縮し、マップを UI ギリギリまで拡大 */
@media (max-width: 720px) {
    :root {
        --hud-pad: 2px;
    }

    body {
        font-size: 11px;
        padding:
            max(2px, var(--safe-top))
            max(2px, var(--safe-right))
            max(2px, var(--safe-bottom))
            max(2px, var(--safe-left));
    }

    #hud-top,
    #hud-bottom {
        width: 100%;
    }

    #hud-top {
        padding: 2px 0 0;
    }

    #hud-info {
        gap: 2px;
        padding: 2px 3px;
    }

    #hud-stats {
        font-size: 7px;
        gap: 1px 3px;
        line-height: 1.15;
    }

    #hud-top-actions {
        gap: 2px;
        max-width: 50%;
    }

    #hud-top-actions button.ui-btn.small {
        font-size: 8px;
        min-height: 20px;
        padding: 0 3px;
        line-height: 1.1;
    }

    /* 左＝詳細／ログ、右＝マップ。aux 高さはゲーム画面（--play-height）に合わせる */
    #game-mid {
        padding: 0;
        align-items: center;
    }

    #game-play {
        flex-direction: row;
        align-items: center;
        width: auto;
        height: auto;
        max-width: 100%;
        max-height: 100%;
        gap: 2px;
        flex: 0 1 auto;
    }

    #hud-aux {
        width: min(112px, 28%);
        height: var(--play-height, auto);
        max-height: 100%;
        flex: 0 0 auto;
        flex-direction: column;
        gap: 2px;
    }

    .hud-help,
    #hud-log {
        flex: 1 1 50%;
        height: auto;
        max-height: none;
        min-height: 0;
        font-size: 7px;
        line-height: 1.2;
        padding: 2px 3px;
    }

    .hud-help ul {
        margin: 2px 0 0 0;
        padding-left: 1em;
    }

    #game-viewport {
        flex: 0 0 auto;
        min-height: 0;
        align-self: center;
    }

    #hud-bottom {
        padding: 0 0 var(--hud-pad);
    }

    #hud-tools {
        gap: 2px;
        padding: 2px;
    }

    .tool-row {
        gap: 2px;
    }

    button.ui-btn {
        min-width: 72px;
        min-height: 32px;
        font-size: 12px;
        padding: 4px 8px;
        border-width: 1px;
    }

    button.ui-btn.small {
        min-width: 0;
        min-height: 22px;
        font-size: 8px;
        padding: 1px 3px;
        border-width: 1px;
    }

    /* 下段建設ツールは特に低く */
    #hud-tools button.tool-btn {
        min-height: 20px;
        font-size: 8px;
        gap: 2px;
        padding: 1px 2px;
        border-width: 1px;
        line-height: 1.1;
    }

    #hud-tools button.tool-btn img {
        width: 12px;
        height: 12px;
    }

    #game-viewport > canvas {
        border-width: 1px;
    }

    .panel {
        min-width: 0;
        width: min(94vw, 420px);
        padding: 14px 14px;
    }

    .panel h1 {
        font-size: 22px;
    }

    .panel .sub {
        font-size: 11px;
    }

    .panel-help {
        padding: 10px 12px 8px;
    }

    .help-body {
        font-size: 11px;
    }

    .help-body h1 {
        font-size: 18px;
    }

    .help-body h2 {
        font-size: 12px;
    }

    .help-body .sub {
        font-size: 10px;
    }

    .settings-label,
    .settings-volume-value {
        font-size: 11px;
    }

    .rank-list {
        font-size: 11px;
        padding: 6px 8px;
        max-height: 220px;
    }

    .name-input {
        font-size: 14px;
        padding: 8px 10px;
    }

    .btn-row {
        gap: 6px;
        margin-top: 10px;
    }
}

@media (max-height: 640px) {
    :root {
        --hud-pad: 2px;
    }

    #hud-top {
        padding: 1px 0 0;
    }

    #hud-info {
        gap: 2px;
        padding: 1px 2px;
    }

    #hud-stats {
        font-size: 7px;
        gap: 1px 3px;
        line-height: 1.15;
        max-height: 32px;
        overflow: auto;
    }

    #hud-top-actions button.ui-btn.small {
        font-size: 8px;
        min-height: 18px;
        padding: 0 3px;
    }

    #hud-bottom {
        padding: 0 0 var(--hud-pad);
    }

    .hud-help,
    #hud-log {
        font-size: 7px;
        line-height: 1.2;
        padding: 1px 2px;
    }

    #hud-tools {
        gap: 2px;
        padding: 2px;
    }

    button.ui-btn.small {
        min-width: 0;
        min-height: 20px;
        font-size: 8px;
        padding: 0 3px;
        border-width: 1px;
    }

    #hud-tools button.tool-btn {
        min-height: 18px;
        font-size: 7px;
        gap: 1px;
        padding: 0 2px;
        border-width: 1px;
        line-height: 1.1;
    }

    #hud-tools button.tool-btn img {
        width: 11px;
        height: 11px;
    }
}
