/*
 * スマホの**横持ち・全画面**を既定とする（縦持ちでは向きを変えてもらう）。
 * ページはスクロールさせず、1 画面に収める。
 * フレームワークを使わない方針（設計書 9.1）に合わせ、ここも素の CSS で書く。
 */

:root {
    --bg: #12131a;
    --card: #1c1e28;
    --line: #2c2f3d;
    --text: #e8e9ef;
    --dim: #9096a8;
    --accent: #6ee7a8;
    --warn: #f0a868;
    color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", sans-serif;
    /* iOS Safari のダブルタップ拡大を止める。 */
    touch-action: manipulation;
}

/*
 * 画面全体のグリッド。**描いていないときは状況だけを大きく出す。**
 * 参加者・退出・診断はメニュー（`.side`）にしまってある。
 * 高さは画面にぴったり合わせ、はみ出す枠だけが自分の中でスクロールする。
 */
main {
    height: 100dvh;
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr);
    grid-template-areas: "status";
    gap: 0.5rem;
    padding: max(0.5rem, env(safe-area-inset-top)) max(0.5rem, env(safe-area-inset-right))
        max(0.5rem, env(safe-area-inset-bottom)) max(0.5rem, env(safe-area-inset-left));
}

.status {
    grid-area: status;
    display: flex;
    flex-direction: column;
    justify-content: center;
    overflow-y: auto;
}

/* メニュー。**右から被せる。** 下の画面は組み替えない（描きかけの面が縮まない）。 */
.side {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    z-index: 30;
    width: min(20rem, 85vw);
    display: none;
    flex-direction: column;
    gap: 0.5rem;
    padding: max(0.5rem, env(safe-area-inset-top)) max(0.5rem, env(safe-area-inset-right))
        max(0.5rem, env(safe-area-inset-bottom)) 0.5rem;
    background: var(--bg);
    border-left: 1px solid var(--line);
    overflow-y: auto;
}

.menu-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 29;
    background: rgba(0, 0, 0, 0.5);
}

body.menu-open .side {
    display: flex;
}

body.menu-open .menu-backdrop {
    display: block;
}

/* ☰ と ✕。小さくても指で押せる大きさは取る（設計書 16.4）。 */
.menu-button {
    min-width: 2.75rem;
    min-height: 2.75rem;
    margin-left: auto;
    background: var(--bg);
    border: 1px solid var(--line);
    color: var(--text);
    font-size: 1.1rem;
}

/*
 * 表示名は参加前にしか意味が無い（参加するときに送る）。**描く画面などには出さない。**
 * 後ろにある `.field` の `display: flex` に負けないよう、`body` から絞って消す。
 */
body:not(.matching) .name-field {
    display: none;
}

body.matching .name-field {
    width: min(20rem, 100%);
    margin-top: 1rem;
}

/*
 * マッチング中（接続 ID を出してから参加が成立するまで）。**番号だけを画面いっぱいに出す。**
 * 部屋の向こうから読めることが、この画面の仕事の全部である。
 */
body.matching .side,
body.matching .menu-backdrop,
body.matching .phase-row,
body.matching .message {
    display: none;
}

body.matching .status {
    align-items: center;
    background: none;
    border: none;
}

body.matching .connection-id {
    margin: 0;
    font-size: min(20vw, 45dvh);
}

/*
 * 描いている最中。**真ん中に絵、左に描く道具、右上に説明、右下に決定。スクロールさせない。**
 * 絵は画面の高さいっぱいに取る（横持ちでは高さのほうが足りない）。
 * 左右の列は同じ幅にする。**絵が画面の真ん中に来る。**
 *
 * 説明とタブの段は `minmax(0, max-content)` にしてある。背の低い画面では**両者が高さを分け合い**、
 * それぞれの枠の中でだけ流れる。決定ボタンを画面の外へ押し出さない。
 */
body.drawing main {
    grid-template-columns: minmax(9.5rem, 12rem) minmax(0, 1fr) minmax(9.5rem, 12rem);
    grid-template-rows: minmax(0, max-content) minmax(0, max-content) minmax(0, 1fr) auto;
    grid-template-areas:
        "tools board status"
        "tools board tabs"
        "tools board ."
        "tools board complete";
}

/*
 * 配置（仕様書 4.3a）。**左に見取り図、右に状況と大きさのボタン。** 見取り図は全員の部位を映すので、
 * 描く面と同じく画面のいちばん広いところを使う。
 */
body.placing main {
    grid-template-columns: minmax(0, 1fr) minmax(9.5rem, 13rem);
    grid-template-rows: minmax(0, max-content) minmax(0, 1fr);
    grid-template-areas:
        "place status"
        "place place-side";
}

body.drawing .status,
body.placing .status {
    justify-content: flex-start;
    padding: 0.5rem 0.75rem;
}

/* 列が細いので、残り時間と ☰ を上の段に、見出しをその下に置く。 */
body.drawing .phase-row,
body.placing .phase-row {
    flex-wrap: wrap;
    row-gap: 0.25rem;
}

body.drawing .phase,
body.placing .phase {
    order: 1;
    flex-basis: 100%;
    font-size: 1.05rem;
    line-height: 1.4;
}

body.drawing .status .message,
body.placing .status .message {
    margin-top: 0.25rem;
    font-size: 0.8rem;
    line-height: 1.5;
}

header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

h1 {
    font-size: 0.9rem;
    font-weight: 600;
    letter-spacing: 0.08em;
    color: var(--dim);
    margin: 0;
}

.badge {
    font-size: 0.7rem;
    padding: 0.15rem 0.45rem;
    background: var(--warn);
    color: #1a1205;
    font-weight: 700;
}

.card {
    background: var(--card);
    border: 1px solid var(--line);
    padding: 1rem;
}

.phase-row {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.phase {
    margin: 0;
    font-size: 1rem;
    font-weight: 600;
    flex: 1;
}

/*
 * 残り時間。**描いている最中に、目を上げずに読める大きさ**であることだけを狙う。
 * 数字が動くと幅が揺れるので、等幅の数字に固定する。
 */
.timer {
    margin: 0;
    font-size: 1.6rem;
    font-weight: 700;
    line-height: 1;
    font-variant-numeric: tabular-nums;
    font-family: ui-monospace, Menlo, Consolas, monospace;
    color: var(--accent);
}

.timer:empty { display: none; }

.card-title {
    margin: 0;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--dim);
}

/*
 * 接続 ID は人が読み上げて人が打ち込む（仕様書 3.2 手順 5）。
 * 部屋の向こうから読める大きさであることが、この画面の仕事のほぼ全部である。
 */
.connection-id {
    margin: 0.5rem 0 0;
    font-size: clamp(3rem, min(12vw, 30dvh), 7rem);
    font-weight: 800;
    line-height: 1.05;
    letter-spacing: 0.12em;
    text-align: center;
    font-variant-numeric: tabular-nums;
    color: var(--accent);
    /* 紛らわしい字は ID 側で除いてあるが、字形でも稼いでおく。 */
    font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

.connection-id:empty { display: none; }

.message {
    margin: 0.5rem 0 0;
    color: var(--dim);
    font-size: 0.85rem;
    line-height: 1.6;
}

.message:empty { display: none; }

.field {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-size: 0.85rem;
    color: var(--dim);
}

.field input {
    flex: 1;
    background: var(--bg);
    border: 1px solid var(--line);
    color: var(--text);
    /* 16px 未満だと iOS Safari が勝手に拡大する。 */
    font-size: 1rem;
    padding: 0.5rem 0.6rem;
}

summary {
    cursor: pointer;
    color: var(--dim);
    font-size: 0.85rem;
}

.diagnostics {
    margin-top: 0.75rem;
    font-size: 0.8rem;
}

.diag-row {
    display: flex;
    align-items: baseline;
    gap: 0.5rem;
    padding: 0.2rem 0;
    border-bottom: 1px solid var(--line);
}

.diag-key { color: var(--dim); min-width: 5.5rem; }
.diag-value { font-family: ui-monospace, Menlo, Consolas, monospace; word-break: break-all; }

.diag-badge {
    margin-left: auto;
    font-size: 0.7rem;
    padding: 0.1rem 0.4rem;
    background: var(--accent);
    color: #06120c;
    font-weight: 700;
    white-space: nowrap;
}

.log {
    margin-top: 0.75rem;
    max-height: 14rem;
    overflow-y: auto;
    font-family: ui-monospace, Menlo, Consolas, monospace;
    font-size: 0.7rem;
    line-height: 1.7;
    color: var(--dim);
}

/*
 * 描く面（ロードマップ S3）。**主役は絵である**ため、周りは足さない（設計書 16.1）。
 * 角丸を書かないのは 16.4 の指定である。
 */
.draw {
    /* 中身（絵と道具）を main のグリッドに直接置く。 */
    display: contents;
}

/* **`display: contents` は `hidden` 属性より強い。** */
.draw[hidden] {
    display: none;
}

.board-area {
    grid-area: board;
    display: flex;
    flex-direction: column;
    min-height: 0;
    padding: 0.5rem;
}

/* 絵を置く枠。**幅と高さの両方に収まる最大の大きさ**を、コンテナ単位で求める。 */
.board-frame {
    position: relative;
    flex: 1;
    min-height: 0;
    container-type: size;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* ほかの端末で描いているあいだ、面に被せる（PenState）。 */
.pen-away {
    position: absolute;
    inset: 0;
    z-index: 4;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    background: rgb(20 20 24 / 0.72);
    color: #fff;
    font-size: 1.2rem;
    text-align: center;
}

.pen-away[hidden] {
    display: none;
}

.board {
    display: block;
    /*
     * 比は既定で正方形だが、**スロットの描画領域に合わせて JS が書き換える**
     * （`SlotLayout.drawArea` → `aspect-ratio` と `--board-ratio`）。
     * ホスト側の面と比が違うと絵が歪む（game.md 5.2）。
     */
    width: min(100cqw, 100cqh * var(--board-ratio, 1));
    height: auto;
    aspect-ratio: 1 / 1;
    background: #f5f5f0;
    border: 1px solid var(--line);
    /* 指で描く。ブラウザのスクロールと拡大に取られない。 */
    touch-action: none;
}

/* 配置（仕様書 4.3a）。中身は main のグリッドに直接並ぶ（描く面と同じ）。 */
.place {
    display: contents;
}

.place[hidden] {
    display: none;
}

.place-area {
    grid-area: place;
    display: flex;
    flex-direction: column;
    min-height: 0;
    padding: 0.5rem;
}

/* 見取り図は正方形（骨格空間を正方形に写す。ホストの見取り図と同じ）。 */
.place-board {
    display: block;
    width: min(100cqw, 100cqh);
    height: auto;
    aspect-ratio: 1 / 1;
    /* 指で動かす。ブラウザのスクロールと拡大に取られない。 */
    touch-action: none;
}

.place-side {
    grid-area: place-side;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    min-height: 0;
    overflow-y: auto;
    padding: 0.5rem;
}

.place-info {
    margin: 0;
    font-size: 0.8rem;
    line-height: 1.5;
    color: var(--dim);
}

.place-size {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.4rem;
    margin-top: auto;
}

/* インクの残量（仕様書 5.4）。**常時表示する。** 列が細いので、文字の下に棒を置く。 */
.ink {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.ink-bar {
    order: 1;
    height: 0.5rem;
    background: var(--bg);
    border: 1px solid var(--line);
}

.ink-fill {
    height: 100%;
    width: 0;
    background: var(--accent);
}

/* 超過。**押せない理由が色で分かるようにする。** */
.ink-fill.over {
    background: #c83434;
}

.ink-label {
    font-size: 0.75rem;
    color: var(--dim);
}

/*
 * 描く道具（左の列）。**左に太さ、右にボタンを縦に積む。**
 * 枠は `overflow` を持たない。色の吹き出しが枠の外（絵の上）へはみ出して出るためである。
 * 代わりに、背の低い画面ではボタンが縮んで収まる。
 */
.tools {
    grid-area: tools;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    min-height: 0;
    padding: 0.5rem;
}

.tools-body {
    flex: 1;
    min-height: 0;
    display: flex;
    gap: 0.4rem;
}

/* 太さは縦の棒。**上が太い。** */
.width-field {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.25rem;
    width: 2.75rem;
}

.width-label {
    font-size: 0.8rem;
    color: var(--dim);
}

/* 指で掴める太さにする（設計書 16.4）。`direction: rtl` で最小を下にする。 */
.width-range {
    flex: 1;
    min-height: 0;
    width: 100%;
    margin: 0;
    writing-mode: vertical-lr;
    direction: rtl;
}

.tool-column {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

/* 高さが余れば伸び、足りなければ縮む。**伸びすぎて間延びしないよう上限を置く。** */
.tool-column > .action,
.palette {
    flex: 1 1 0;
    min-height: 2.25rem;
    max-height: 3.25rem;
}

.tool-column .action {
    font-size: 0.85rem;
}

.palette {
    position: relative;
    display: flex;
}

.color-button {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
}

/* 今の色。**色の名前ではなく色そのものを見せる。** */
.color-current {
    width: 1.25rem;
    height: 1.25rem;
    border: 1px solid var(--line);
}

/*
 * 色の吹き出し。**道具の枠のすぐ外（絵の上）に出す。** 枠の中に出すと、隣のボタンを隠してしまう。
 * 外を触ったら閉じる（`.palette-backdrop`）。
 */
.palette-pop {
    position: absolute;
    top: 0;
    left: calc(100% + 0.75rem);
    z-index: 6;
    display: grid;
    grid-template-columns: repeat(3, 3rem);
    gap: 0.4rem;
    padding: 0.5rem;
    background: var(--card);
    border: 1px solid var(--accent);
    box-shadow: 0 0.5rem 1.5rem rgba(0, 0, 0, 0.5);
}

/* **`display: grid` は `hidden` 属性より強い。** */
.palette-pop[hidden] {
    display: none;
}

.palette-pop .choice {
    width: 3rem;
    height: 3rem;
}

.palette-backdrop {
    position: fixed;
    inset: 0;
    z-index: 5;
}

/* 担当スロットのタブ。**右の列に縦に積む。** 担当が 1 枚なら出さない（見出しで分かる）。 */
.slot-tabs {
    grid-area: tabs;
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    min-height: 0;
    overflow-y: auto;
}

.slot-tabs[hidden] {
    display: none;
}

.slot-tabs .choice {
    flex-shrink: 0;
    padding: 0.25rem 0.5rem;
}

/* 決定。**右下に大きく置く。** 押すと確認が出る。 */
.action.complete {
    grid-area: complete;
    min-height: 3.5rem;
    font-size: 1.2rem;
}

/* 指で押せる大きさを確保する。簡素であることと押しにくいことは別（設計書 16.4）。 */
.choice {
    min-width: 2.75rem;
    min-height: 2.75rem;
    padding: 0;
    background: var(--bg);
    border: 1px solid var(--line);
    color: var(--text);
    font-size: 0.95rem;
}

.choice.chosen {
    border-color: var(--accent);
    border-width: 2px;
}

.action {
    flex: 1;
    min-height: 2.75rem;
    background: var(--bg);
    border: 1px solid var(--line);
    color: var(--text);
    font-size: 0.9rem;
}

.action:active {
    background: var(--card);
}

.action:disabled {
    color: var(--dim);
    border-style: dashed;
}

/* 「決定」。**押すと描く側が止まる**ので、他のボタンと見分けがつくようにする。 */
.action.primary {
    border-color: var(--accent);
    color: var(--accent);
    font-weight: 700;
}

.action.primary:disabled {
    border-color: var(--line);
    color: var(--dim);
    font-weight: 400;
}

/* 決定の確認。**画面の真ん中に出す**（右下の決定ボタンと重ならないので、2 度押しで通らない）。 */
.confirm {
    width: min(22rem, 90vw);
    padding: 0;
    background: var(--card);
    color: var(--text);
    border: 1px solid var(--accent);
}

.confirm::backdrop {
    background: rgba(0, 0, 0, 0.6);
}

.confirm-body {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    padding: 1.25rem;
}

.confirm-title {
    margin: 0;
    font-size: 1.1rem;
    font-weight: 700;
}

.confirm-message {
    margin: 0;
    color: var(--dim);
    font-size: 0.85rem;
    line-height: 1.6;
}

.confirm-message:empty { display: none; }

.confirm-actions {
    display: flex;
    gap: 0.75rem;
    margin-top: 0.25rem;
}

.confirm-actions .action {
    min-height: 3rem;
    font-size: 1rem;
}

/*
 * 戦闘の操作面（仕様書 7.1、版数 2.1）。**面そのものが操作系である。**
 *
 * **戦闘中は画面全体に固定する**（横持ちの 1 画面）。スクロールが起きると、指を滑らせた拍子に
 * 面がずれて角度が狂う。1 人 1 部位なので、面は 1 枚を大きく取れる。
 */
.battle {
    position: fixed;
    inset: 0;
    z-index: 10;
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    margin: 0;
    padding: max(0.4rem, env(safe-area-inset-top)) max(0.6rem, env(safe-area-inset-right))
        max(0.4rem, env(safe-area-inset-bottom)) max(0.6rem, env(safe-area-inset-left));
    height: 100dvh;
    border-radius: 0;
    overflow: hidden;
}

/* **`display: flex` は `hidden` 属性より強い。** 明示しないと、戦闘の外でも画面全体を覆う。 */
.battle[hidden] {
    display: none;
}

/* ページ自体はスクロールさせない（横持ちの 1 画面）。 */
body {
    overflow: hidden;
}

.control {
    display: block;
    flex: 1;
    min-height: 0;
    width: 100%;
    background: #f5f5f0;
    border: 1px solid var(--line);
    touch-action: none;
}

/* **`display: block` は `hidden` 属性より強い**（`.battle[hidden]` と同じ）。作戦モードでは指の面を消す。 */
.control[hidden] {
    display: none;
}

/* 作戦モードの操作面。**縦持ちでも使える**（回す面ではないので、横に広い必要が無い）。 */
.tactics {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
}

.tactics[hidden] {
    display: none;
}

.tactics-turn {
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
    font-weight: bold;
}

.gauge-fill.turn {
    background: var(--accent);
    transition: width 0.25s linear;
}

.tactics-parts {
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
}

.tactics-part {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    padding: 0.6rem;
    border: 1px solid var(--line);
    border-radius: 8px;
}

.tactics-title {
    display: flex;
    justify-content: space-between;
    gap: 0.6rem;
}

.tactics-last {
    font-size: 0.8rem;
    color: var(--dim);
}

/* このターンの頭に指示が発動した部位。**ターンのあいだずっと、何が発動したかを目立たせておく。** */
.tactics-part.fired .tactics-last {
    color: var(--accent);
    font-weight: bold;
}

/* 発動した瞬間に 1 回だけ光らせる。 */
.tactics-part.flash {
    animation: tactics-part-flash 1.2s ease-out;
}

@keyframes tactics-part-flash {
    from {
        border-color: var(--accent);
        background: rgba(110, 231, 168, 0.25);
    }
    to {
        border-color: var(--line);
        background: transparent;
    }
}

/*
 * 発動の知らせ。**画面の真ん中に大きく出して、すぐ消す。**
 * 指を通す（`pointer-events: none`）ので、出ているあいだも下のボタンを押せる。
 */
.tactics-flash {
    position: fixed;
    inset: 0;
    z-index: 25;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
}

.tactics-flash-box {
    padding: 0.8rem 2rem;
    border: 2px solid var(--accent);
    border-radius: 12px;
    background: var(--card);
    box-shadow: 0 0.5rem 2rem rgba(0, 0, 0, 0.5);
    text-align: center;
    animation: tactics-flash 1.4s ease-out forwards;
}

.tactics-flash-part {
    font-size: 0.9rem;
    color: var(--dim);
}

.tactics-flash-command {
    font-size: 2.4rem;
    font-weight: bold;
    color: var(--accent);
}

@keyframes tactics-flash {
    0% {
        opacity: 0;
        transform: scale(0.6);
    }
    15% {
        opacity: 1;
        transform: scale(1.1);
    }
    25% {
        transform: scale(1);
    }
    75% {
        opacity: 1;
        transform: translateY(0);
    }
    100% {
        opacity: 0;
        transform: translateY(-1rem);
    }
}

/* **動きを減らす設定の端末では、拡大と移動をやめて出し入れだけにする。** */
@media (prefers-reduced-motion: reduce) {
    .tactics-flash-box {
        animation-name: tactics-flash-fade;
    }
}

@keyframes tactics-flash-fade {
    0%,
    75% {
        opacity: 1;
    }
    100% {
        opacity: 0;
    }
}

/* 指で押しやすい大きさにする。**4 つを 2×2 か 1 列 4 つに並べる。** */
.tactics-buttons {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(7rem, 1fr));
    gap: 0.4rem;
}

.tactics-buttons .action {
    min-height: 3rem;
    font-size: 1rem;
}

.battle .message {
    margin: 0;
    font-size: 0.8rem;
}

/* ゲージは横に並べて細く取る。**操作面の高さを食わない。** */
.gauges {
    display: flex;
    flex-direction: row;
    gap: 1rem;
}

.gauges .gauge {
    flex: 1;
}

.rotate-hint {
    display: none;
}

/*
 * 縦持ちのスマホ（指で触る端末だけ）。PC の縦長ウインドウでは出さない。
 * **マッチング中は出さない**（番号は縦でも読める）。作戦モードもボタンだけなので縦で使える。
 */
@media (orientation: portrait) and (pointer: coarse) {
    body:not(.matching):not(.tactics-mode) .rotate-hint {
        position: fixed;
        inset: 0;
        z-index: 20;
        display: flex;
        align-items: center;
        justify-content: center;
        background: var(--bg);
        color: var(--text);
        font-size: 1.2rem;
        text-align: center;
    }
}

.gauge {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.gauge-label {
    min-width: 2.5rem;
    font-size: 0.8rem;
    color: var(--dim);
}

.gauge-bar {
    flex: 1;
    height: 0.5rem;
    background: var(--bg);
    border: 1px solid var(--line);
}

.gauge-fill {
    height: 100%;
    width: 100%;
}

.gauge-fill.ally {
    background: var(--accent);
}

.gauge-fill.enemy {
    background: #7a7a7a;
}
