:root {
    --home-color: #1e3e7d;
    --away-color: #7c0002;

    --slot-size: 48px;
    --icon-width: 44px;
    --num-size: 30px;
    --num-font: 14px;
    --name-font: 11px;
    
    --arc-size: 60px;
    --arc-thick: 7px;
    --arc-x: -24px;
    --tri-h: 11px;
    --tri-w: 16px;
    --tri-x: -5px;
}

body.icon-large {
    --slot-size: 64px;
    --icon-width: 60px;
    --num-size: 40px;
    --num-font: 18px;
    --name-font: 13px;
    
    --arc-size: 76px;
    --arc-thick: 9px;
    --arc-x: -32px;
    --tri-h: 14px;
    --tri-w: 20px;
    --tri-x: -6px;
}

body.icon-small {
    --slot-size: 36px;
    --icon-width: 36px;
    --num-size: 24px;
    --num-font: 11px;
    --name-font: 9px;
    
    --arc-size: 46px;
    --arc-thick: 5px;
    --arc-x: -18px;
    --tri-h: 8px;
    --tri-w: 12px;
    --tri-x: -4px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: sans-serif; background-color: #1a1a1a; color: #ffffff; display: flex; justify-content: center; }

.app-container { width: 100%; max-width: 1100px; padding: 10px; display: flex; flex-direction: column; gap: 15px; }

header { display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid #333; padding-bottom: 10px; }

.header-title { display: flex; align-items: center; gap: 10px; }
.title-wrapper { display: flex; flex-direction: column; }
.version-text { font-size: 11px; color: #ff6b6b; margin-top: -2px; font-weight: bold; }
.header-tools { display: flex; align-items: center; gap: 15px; flex-wrap: wrap; }
.obj-tools, .draw-tools { display: flex; gap: 5px; background: #333; padding: 4px; border-radius: 6px; }
.tool-btn { background: transparent; border: none; font-size: 16px; cursor: pointer; padding: 6px; border-radius: 4px; color: white; display: flex; align-items: center; justify-content: center; }
.tool-btn.text-btn { font-size: 12px; font-weight: bold; }
.tool-btn:hover { background: #444; }
.tool-btn.active { background: #007bff; }

.icon-btn { background-color: #555; color: white; border: none; border-radius: 50%; width: 26px; height: 26px; font-weight: bold; cursor: pointer; display: flex; justify-content: center; align-items: center; font-size: 14px; }
.icon-btn:hover { background-color: #777; }
.btn { padding: 8px 16px; border: none; border-radius: 4px; cursor: pointer; background-color: #444; color: white; font-weight: bold; }
.btn.primary { background-color: #007bff; }
.btn.danger { background-color: #dc3545; }
.btn.danger:hover { background-color: #c82333; }
.btn.reset-btn { background-color: #555; padding: 6px 12px; font-size: 13px; }

.pitch-header { display: flex; justify-content: space-between; align-items: flex-end; margin-bottom: -5px; padding: 0 10px; position: relative; }
.team-info { display: flex; align-items: center; gap: 10px; }
.team-name-input { background: transparent; border: none; border-bottom: 1px solid #555; color: white; font-size: 18px; font-weight: bold; padding: 4px; width: 250px; }
.team-name-input:focus { outline: none; border-bottom-color: #007bff; }

.match-info { position: absolute; left: 50%; transform: translateX(-50%); bottom: 4px; }
.match-name-input { background: transparent; border: none; border-bottom: 1px solid #555; color: #ddd; font-size: 14px; text-align: center; padding: 2px; width: 220px; transition: border-color 0.2s; }
.match-name-input:focus { outline: none; border-bottom-color: #007bff; color: white; }

.color-picker-wrap { position: relative; width: 28px; height: 28px; flex-shrink: 0; }
.color-indicator { position: absolute; inset: 0; border-radius: 50%; border: 2px solid #555; pointer-events: none; }
.color-picker { position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%; cursor: pointer; }

.pitch {
    --pitch-bg: #2a2d34;
    --line-color: #555;
    --slot-border: rgba(255, 255, 255, 0.4);
    --slot-text: rgba(255, 255, 255, 0.3);
    
    position: relative; width: 100%; aspect-ratio: 16 / 9; 
    background: var(--pitch-bg); border: 2px solid var(--line-color); 
    overflow: hidden; touch-action: none;
    transition: background 0.3s, border-color 0.3s, transform 0.4s ease;
}

.pitch[data-theme="grass"] {
    --pitch-bg: #4c9a3f; 
    --line-color: rgba(255, 255, 255, 0.7);
    --slot-border: rgba(255, 255, 255, 0.6);
    --slot-text: rgba(255, 255, 255, 0.5);
}
.pitch[data-theme="whiteboard"] {
    --pitch-bg: #f4f6f9;
    --line-color: #2c3e50;
    --slot-border: rgba(44, 62, 80, 0.4);
    --slot-text: rgba(44, 62, 80, 0.4);
}

#bg-canvas { position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: 0; pointer-events: none; }
#draw-canvas { position: absolute; top: 0; left: 0; z-index: 4; pointer-events: none; }
.pitch.draw-mode #draw-canvas { z-index: 200; pointer-events: auto; cursor: crosshair; }

.line { position: absolute; border: 2px solid var(--line-color); transition: border-color 0.3s; }
.halfway-line { top: 0; bottom: 0; left: 50%; transform: translateX(-50%); border-width: 0 2px 0 0; z-index: 1; }
.center-circle { top: 50%; left: 50%; width: 120px; height: 120px; border-radius: 50%; transform: translate(-50%, -50%); z-index: 1; transition: width 0.3s, height 0.3s; }
.penalty-area { top: 20%; bottom: 20%; width: 15%; z-index: 2; background: transparent; transition: all 0.3s; }
.penalty-area.left { left: 0; border-width: 2px 2px 2px 0; }
.penalty-area.right { right: 0; border-width: 2px 0 2px 2px; }
.goal-area { top: 35%; bottom: 35%; width: 5%; z-index: 3; background: transparent; }
.goal-area.left { left: 0; border-width: 2px 2px 2px 0; }
.goal-area.right { right: 0; border-width: 2px 0 2px 2px; }
.penalty-arc { top: 50%; width: 45px; height: 90px; z-index: 1; transform: translateY(-50%); transition: opacity 0.3s; }
.penalty-arc.left { left: 15%; border-radius: 0 90px 90px 0; border-width: 2px 2px 2px 0; }
.penalty-arc.right { right: 15%; border-radius: 90px 0 0 90px; border-width: 2px 0 2px 2px; }
.corner-arc { width: 40px; height: 40px; border-radius: 50%; z-index: 1; }
.corner-arc.top-left { top: -20px; left: -20px; }
.corner-arc.bottom-left { bottom: -20px; left: -20px; }
.corner-arc.top-right { top: -20px; right: -20px; }
.corner-arc.bottom-right { bottom: -20px; right: -20px; }

/* 8人制・フットサル用スタイル */
.pitch[data-mode="8v8"] .penalty-arc { opacity: 0; display: none; }
.pitch[data-mode="8v8"] .center-circle { width: 90px; height: 90px; }
.pitch[data-mode="8v8"] .penalty-area { width: 25%; }
.pitch[data-mode="futsal"] { aspect-ratio: 2 / 1; }
.pitch[data-mode="futsal"] .goal-area, .pitch[data-mode="futsal"] .penalty-arc { display: none; }
.pitch[data-mode="futsal"] .center-circle { width: 80px; height: 80px; }
.pitch[data-mode="futsal"] .penalty-area { width: 15%; top: 25%; bottom: 25%; }
.pitch[data-mode="futsal"] .penalty-area.left { border-radius: 0 100px 100px 0; border-width: 2px 2px 2px 0; }
.pitch[data-mode="futsal"] .penalty-area.right { border-radius: 100px 0 0 100px; border-width: 2px 0 2px 2px; }

.pitch-slot { 
    position: absolute; 
    width: var(--slot-size); height: var(--slot-size); 
    border: 2px dashed var(--slot-border); border-radius: 50%; 
    transform: translate(-50%, -50%); display: flex; justify-content: center; align-items: center; z-index: 5; 
    transition: border-color 0.3s, width 0.3s, height 0.3s;
}
.pitch-slot::after { content: attr(data-pos); position: absolute; color: var(--slot-text); font-weight: bold; z-index: 0; transition: color 0.3s; }

.player-icon { display: flex; flex-direction: column; align-items: center; cursor: grab; user-select: none; touch-action: none; width: var(--icon-width); z-index: 10; transition: width 0.3s; }
.player-icon:active { cursor: grabbing; }

.player-number { width: var(--num-size); height: var(--num-size); font-size: var(--num-font); border-radius: 50%; display: flex; justify-content: center; align-items: center; font-weight: bold; margin-bottom: 4px; position: relative; z-index: 2; transition: background-color 0.2s, width 0.3s, height 0.3s, font-size 0.3s; color: white; }
.home .player-number { background-color: var(--home-color); }
.away .player-number { background-color: var(--away-color); }

.body-shape { display: none; position: absolute; inset: 0; pointer-events: none; z-index: -1; transition: transform 0.1s ease-out; }

body.show-bodyshape .player-number {
    background-color: transparent !important;
    text-shadow: -1px -1px 0 #fff, 1px -1px 0 #fff, -1px 1px 0 #fff, 1px 1px 0 #fff, 0 2px 2px rgba(0,0,0,0.6); font-weight: 900;
}
body.show-bodyshape .home .player-number { color: var(--home-color); }
body.show-bodyshape .away .player-number { color: var(--away-color); }

.shape-arc { position: absolute; top: 50%; left: 100%; width: var(--arc-size); height: var(--arc-size); border: var(--arc-thick) solid transparent; border-radius: 50%; transform: translate(var(--arc-x), -50%); z-index: 1; }
.shape-arc { border-left-color: #ffffff; filter: drop-shadow(0 0 1.5px #111111); }
.pitch[data-theme="grass"] .shape-arc { border-left-color: #111111; filter: drop-shadow(0 0 1.5px #ffffff); }
.pitch[data-theme="whiteboard"] .shape-arc { border-left-color: #111111; filter: none; }

.shape-pointer { position: absolute; top: 50%; left: 100%; width: var(--tri-w); height: calc(var(--tri-h) * 2); transform: translate(var(--tri-x), -50%); z-index: 2; overflow: visible; display: block; }
.shape-pointer polygon { stroke-width: 14px; stroke-linejoin: round; }
.shape-pointer polygon { stroke: #111111; }
.home .shape-pointer polygon { fill: #00e5ff; }
.away .shape-pointer polygon { fill: #ffea00; }
.pitch[data-theme="grass"] .shape-pointer polygon { stroke: #111111; }
.pitch[data-theme="grass"] .home .shape-pointer polygon { fill: #00e5ff; }
.pitch[data-theme="grass"] .away .shape-pointer polygon { fill: #ffea00; }
.pitch[data-theme="whiteboard"] .shape-pointer polygon { stroke: #ffffff; }
.pitch[data-theme="whiteboard"] .home .shape-pointer polygon { fill: #0077ff; }
.pitch[data-theme="whiteboard"] .away .shape-pointer polygon { fill: #ff5500; }

body.show-bodyshape .body-shape { display: block; }

.player-name { font-size: var(--name-font); white-space: nowrap; position: relative; z-index: 2; text-shadow: 1px 1px 2px #000; color: white; transition: font-size 0.3s; }
.hide-names .player-name { display: none; }

#pitch > .player-icon { position: absolute; transform: translate(-50%, -50%); }
.pitch-slot .player-icon { position: static; transform: none; }

.ball-obj { position: absolute; width: 24px; height: 24px; background-color: transparent; border-radius: 50%; cursor: grab; user-select: none; touch-action: none; transform: translate(-50%, -50%); z-index: 100; box-shadow: 1px 2px 4px rgba(0,0,0,0.6); }
.ball-obj svg { width: 100%; height: 100%; display: block; }
.ball-obj:active { cursor: grabbing; }

.sub-area-wrapper { display: flex; gap: 10px; }
.team-sub, .team-bench { flex: 1; display: flex; flex-wrap: wrap; gap: 10px; padding: 10px; background-color: #222; border-radius: 8px; min-height: 70px; }
.team-sub { border: 2px dashed #555; position: relative; }
.team-sub::before { content: attr(data-label); position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); color: #333; font-size: 24px; font-weight: bold; pointer-events: none; z-index: 0; white-space: nowrap; }

.formation-controls-container { display: flex; flex-direction: column; gap: 12px; background-color: #222; padding: 12px 15px; border-radius: 8px; margin-bottom: 15px; }
.options-row { display: flex; justify-content: center; align-items: center; flex-wrap: wrap; gap: 20px; padding-bottom: 12px; border-bottom: 1px dashed #444; }
.toggle-group { display: flex; align-items: center; gap: 8px; }
.toggle-group select { padding: 4px; background-color: #444; color: white; border: 1px solid #666; border-radius: 4px; }
.formation-row { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 10px; }
.team-control { display: flex; align-items: center; gap: 10px; }
.formation-row select { padding: 6px; background-color: #444; color: white; border: 1px solid #666; border-radius: 4px; }

.control-panel { display: flex; gap: 15px; margin-bottom: 10px; align-items: stretch; }
.input-group { flex: 1; display: flex; flex-direction: column; gap: 5px; }
.input-header { display: flex; justify-content: space-between; align-items: center; }
.team-select { background-color: #444; color: white; border: 1px solid #666; border-radius: 4px; padding: 4px; font-size: 13px; max-width: 140px; }

.action-group { display: flex; flex-direction: column; justify-content: center; align-items: center; gap: 8px; width: 160px; flex-shrink: 0; }
.btn-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; width: 100%; }
.action-btn { width: 100%; padding: 10px 0; font-size: 12px; font-weight: bold; }
.save-all-btn { background-color: #d35400; color: #fff; margin-top: 2px; }
.save-all-btn:hover { background-color: #e67e22; }

.sort-toggle-label { font-size: 13px; cursor: pointer; display: flex; align-items: center; gap: 5px; user-select: none; }
.bench-header { display: flex; justify-content: center; align-items: center; margin-bottom: 8px; padding-top: 5px; border-top: 1px dashed #444; }

textarea { background-color: #333; color: #fff; border: 1px solid #555; border-radius: 4px; padding: 8px; resize: vertical; height: 100%; }
textarea.dragover { background-color: #444; border: 2px dashed #007bff; }

.modal { display: none; position: fixed; z-index: 2000; left: 0; top: 0; width: 100%; height: 100%; background-color: rgba(0,0,0,0.6); justify-content: center; align-items: center; }
.modal-content { background-color: #2a2a2a; color: #fff; padding: 25px; border-radius: 8px; width: 90%; max-width: 600px; position: relative; max-height: 80vh; overflow-y: auto; border: 1px solid #444; }
.close { position: absolute; top: 10px; right: 20px; color: #aaa; font-size: 28px; font-weight: bold; cursor: pointer; }
.close:hover { color: #fff; }
#help-content { white-space: pre-wrap; font-family: inherit; margin-top: 15px; line-height: 1.6; font-size: 14px; color: #ddd; }

/* スタートアップモーダル用 */
.mode-card { background: #333; padding: 20px; border-radius: 8px; cursor: pointer; flex: 1; min-width: 200px; border: 2px solid transparent; transition: all 0.2s; }
.mode-card:hover { border-color: #007bff; background: #444; }

.capture-area { display: flex; flex-direction: column; background-color: #1a1a1a; padding: 15px; margin: -15px; border-radius: 8px; }
.capture-area > header { margin-bottom: 15px; }
.capture-area > .sub-area-wrapper { margin-top: 15px; }

/* =========================================
   UIモード切替設定（タブレット準備 / タブレット戦術 / PC）
   ========================================= */

/* タブレット準備モード */
body.mode-tablet-prep #btn-enter-tactical { display: inline-block !important; }

/* タブレット戦術モード */
body.mode-tablet-tactical .pitch-header,
body.mode-tablet-tactical .formation-controls-container,
body.mode-tablet-tactical footer { display: none !important; }

body.mode-tablet-tactical #btn-exit-tactical,
body.mode-tablet-tactical #zoom-controls { display: flex !important; }

body.mode-tablet-tactical #btn-initialize,
body.mode-tablet-tactical #btn-enter-tactical,
body.mode-tablet-tactical #btn-help,
body.mode-tablet-tactical #btn-change-mode,
body.mode-tablet-tactical .header-title { display: none !important; }

/* 戦術モード：キャンバスのフルスクリーン化とヘッダーのフローティング */
body.mode-tablet-tactical .capture-area {
    position: fixed; inset: 0; z-index: 500; background: #1a1a1a; 
    padding: 60px 10px 90px 10px; margin: 0; border-radius: 0; 
    display: flex; flex-direction: column; justify-content: center;
}
body.mode-tablet-tactical header {
    position: absolute; top: 0; left: 0; right: 0; padding: 10px; 
    background: rgba(0,0,0,0.8); border-bottom: none; z-index: 1000; justify-content: center;
}
body.mode-tablet-tactical #pitch { margin: 0 auto; } /* 中央配置 */

/* 戦術モード：コンパクト控えリスト */
body.mode-tablet-tactical .sub-area-wrapper {
    position: absolute; bottom: 0; left: 0; right: 0; height: 85px; 
    background: rgba(34,34,34,0.95); flex-direction: row; overflow-x: auto; 
    overflow-y: hidden; z-index: 1000; padding: 10px; gap: 20px;
}
body.mode-tablet-tactical .team-sub {
    flex-wrap: nowrap; min-width: max-content; padding: 0; border: none; 
    background: transparent; height: 100%; align-items: center;
}
body.mode-tablet-tactical .team-sub::before { display: none; }


@media screen and (max-width: 768px) {
    :root {
        --slot-size: 36px; --icon-width: 36px; --num-size: 24px; --num-font: 11px; --name-font: 9px;
        --arc-size: 46px; --arc-thick: 5px; --arc-x: -18px; --tri-h: 8px; --tri-w: 12px; --tri-x: -4px;
    }
    body.icon-large {
        --slot-size: 48px; --icon-width: 44px; --num-size: 30px; --num-font: 14px; --name-font: 11px;
        --arc-size: 60px; --arc-thick: 7px; --arc-x: -24px; --tri-h: 11px; --tri-w: 16px; --tri-x: -5px;
    }
    body.icon-small {
        --slot-size: 28px; --icon-width: 28px; --num-size: 18px; --num-font: 9px; --name-font: 8px;
        --arc-size: 36px; --arc-thick: 4px; --arc-x: -14px; --tri-h: 6px; --tri-w: 9px; --tri-x: -3px;
    }

    header { flex-direction: column; align-items: flex-start; gap: 10px; }
    .header-tools { width: 100%; justify-content: space-between; }
    
    .pitch-header { flex-direction: column; align-items: stretch; gap: 8px; padding: 0 5px; }
    .team-info { width: 100%; justify-content: flex-start !important; }
    .match-info { position: static; transform: none; display: flex; justify-content: center; width: 100%; order: -1; }
    .match-name-input { width: 90%; font-size: 13px; }
    
    .team-name-input { width: calc(100% - 40px); font-size: 14px; }
    .pitch { aspect-ratio: 4 / 3; }
    .sub-area-wrapper { flex-direction: column; }
    .team-sub::before { font-size: 18px; }
    
    .options-row { gap: 10px; justify-content: flex-start; }
    .formation-row { flex-direction: column; gap: 12px; }
    .team-control { justify-content: space-between; width: 100%; }
    
    .control-panel { flex-direction: column; }
    .action-group { width: 100%; }
    .btn-grid { gap: 8px; }
    .action-btn { padding: 12px 0; }
    .bench-header { margin-top: 10px; }
    .modal-content { width: 95%; padding: 15px; }
}