:root {
  --bg: #0b0b14;
  --panel: #14141f;
  --border: #262638;
  --accent: #7c5cff;
  --accent2: #22e6c8;
  --text: #e8e8f2;
  --muted: #8a8aa0;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  background: var(--bg);
  color: #e8e8f2;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Yu Gothic UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  -webkit-text-size-adjust: 100%;
}
.mono { font-family: ui-monospace, "SFMono-Regular", Consolas, "Courier New", monospace; }

/* ============ HUD ============ */
#hud {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 1px;
  background: var(--border);
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.hud-cell {
  background: var(--panel);
  padding: 8px 12px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}
.hud-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 6px;
}
.hud-value { font-size: 13px; color: #e8e8f2; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hud-value.big { font-size: 20px; font-weight: 700; }
.hud-sub { font-size: 11px; color: var(--muted); }
.hud-body {
  font-size: 10px;
  color: var(--accent2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tag {
  font-size: 9px;
  background: var(--accent);
  color: #fff;
  padding: 1px 5px;
  border-radius: 4px;
  font-family: ui-monospace, monospace;
}
.hud-badge {
  font-size: 10px;
  padding: 1px 6px;
  border-radius: 4px;
  background: #333;
  color: #fff;
  align-self: flex-start;
}
.hud-badge.ok { background: #1f9d55; }
.hud-badge.bad { background: #c0392b; }
.hud-cell.score .hud-value { color: var(--accent2); }

/* ============ ゲーム ============ */
/* ゲーム領域 + ログパネルのラッパ。横向き=右、縦向き=下にログを配置 */
#main {
  flex: 1 1 auto;
  display: flex;
  min-height: 0;
  min-width: 0;
}
#stage {
  flex: 1 1 auto;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  min-width: 0;
}

/* ============ パケットログ ============ */
#log {
  display: flex;
  flex-direction: column;
  background: #07070d;
  min-height: 0;
  min-width: 0;
  flex: 0 0 auto;
}
.log-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  font-size: 11px;
}
.log-title { font-weight: 700; color: #fff; letter-spacing: 0.04em; }
.log-legend { color: var(--muted); }
.log-legend .up { color: var(--accent2); }
.log-legend .down { color: #6aa3ff; }
.log-spacer { flex: 1 1 auto; }
.log-btn {
  font-size: 10px; padding: 2px 8px; border-radius: 5px;
  border: 1px solid var(--border); background: #1b1b2a; color: #cfcfe0; cursor: pointer;
}
.log-btn:hover { background: #24243a; }
#log-list {
  flex: 1 1 auto;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 4px 8px;
  font-family: ui-monospace, "SFMono-Regular", Consolas, monospace;
  font-size: 11px;
  line-height: 1.5;
  overscroll-behavior: contain;
}
.log-line { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.log-line .t { color: #555; margin-right: 6px; }
.log-line .ar { display: inline-block; width: 12px; }
.log-line .ev { font-weight: 700; margin-right: 6px; }
.log-line .by { color: var(--muted); margin-right: 8px; }
.log-line .v { color: #b9b9cc; }
.log-line.up .ar, .log-line.up .ev { color: var(--accent2); }
.log-line.down .ar, .log-line.down .ev { color: #6aa3ff; }
.log-line.hot { background: rgba(124, 92, 255, 0.18); }
.log-line.hot .ev { color: var(--accent-light, #a98bff); }

/* 横向き（PC / スマホ横持ち）: ログは右 */
@media (orientation: landscape) {
  #main { flex-direction: row; }
  #log { width: clamp(240px, 30vw, 380px); border-left: 1px solid var(--border); }
}
/* 縦向き（スマホ縦持ち）: ログは下 */
@media (orientation: portrait) {
  #main { flex-direction: column; }
  #log { height: clamp(150px, 34vh, 340px); border-top: 1px solid var(--border); }
}
#game {
  width: 100%;
  height: 100%;
  display: block;
  background: var(--bg); /* JS 描画前の白フラッシュ防止 */
  touch-action: none; /* タッチでスクロールさせない */
  cursor: grab; /* ドラッグ操作 */
}
#game.dragging { cursor: grabbing; }
.hint {
  position: absolute;
  bottom: 8px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 11px;
  color: var(--muted);
  pointer-events: none;
}

/* ============ 検証環境スイッチ ============ */
#controls {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--panel);
  border-top: 1px solid var(--border);
  flex-wrap: wrap;
}
.ctrl-label { font-size: 11px; color: var(--muted); }
.env-btn {
  font-size: 12px;
  padding: 6px 12px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: #1b1b2a;
  color: #e8e8f2;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.env-btn:hover { background: #24243a; }
.env-btn.active { border-color: var(--accent); background: var(--accent); color: #fff; }
.env-btn.primary { border-color: var(--accent2); color: var(--accent2); }
.env-btn.primary:hover { background: rgba(34,230,200,0.12); }
.ctrl-divider { width: 1px; height: 20px; background: var(--border); margin: 0 4px; }
#room-label { color: var(--accent2); font-family: ui-monospace, monospace; }

/* 対戦相手ステータス */
#hud-opp.opp-on { color: var(--accent2); }
#hud-opp.opp-off { color: var(--muted); }

/* トースト */
.toast {
  position: fixed;
  left: 50%;
  bottom: 64px;
  transform: translateX(-50%) translateY(10px);
  background: var(--accent);
  color: #fff;
  font-size: 13px;
  padding: 10px 18px;
  border-radius: 8px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 50;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ============ コントロール追加分 ============ */
.ctrl-spacer { flex: 1 1 auto; }
.env-btn.sm { font-size: 11px; padding: 3px 8px; }
.env-btn.help { border-color: var(--accent); color: var(--accent-light, #a98bff); }

/* AWS オリジン制御 */
.aws-control { display: inline-flex; align-items: center; gap: 8px; }
.aws-control[hidden] { display: none; } /* class の display が UA の [hidden] を上書きするため明示 */
.aws-badge {
  font-size: 11px; font-family: ui-monospace, monospace;
  padding: 3px 8px; border-radius: 5px; background: #333; color: #fff; white-space: nowrap;
}
.aws-badge.ok { background: #1f9d55; }
.aws-badge.warn { background: #b7791f; }
.aws-badge.bad { background: #c0392b; }

/* ============ 解説モーダル ============ */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex; align-items: center; justify-content: center;
  z-index: 100; padding: 24px;
}
.modal-overlay[hidden] { display: none; }
.modal {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  max-width: 720px; width: 100%;
  max-height: 86vh; overflow-y: auto;
  padding: 28px 32px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}
.modal h2 { font-size: 20px; margin-bottom: 16px; color: #fff; }
.modal h3 {
  font-size: 14px; margin: 18px 0 8px; color: var(--accent2);
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
}
.modal section { border-top: 1px solid var(--border); padding-top: 4px; }
.modal section:first-of-type { border-top: 0; }
.modal p { font-size: 13px; line-height: 1.7; color: #cfcfe0; }
.modal ul { font-size: 13px; line-height: 1.8; color: #cfcfe0; padding-left: 20px; }
.modal b { color: #fff; }
.modal code { font-family: ui-monospace, monospace; font-size: 12px; background: #000; padding: 1px 5px; border-radius: 4px; color: var(--accent2); }
.modal-close {
  position: absolute; top: 14px; right: 16px;
  width: 32px; height: 32px; border-radius: 8px;
  background: #24243a; border: 1px solid var(--border); color: #fff;
  font-size: 20px; line-height: 1; cursor: pointer;
}
.modal-close:hover { background: #303050; }

/* トラフィック凡例 */
.legend div { font-size: 13px; line-height: 1.9; color: #cfcfe0; display: flex; align-items: center; gap: 8px; }
.dot { width: 11px; height: 11px; border-radius: 3px; flex: 0 0 auto; }
.dot.c1 { background: #4f8cff; }
.dot.c2 { background: #f5c542; }
.dot.c3 { background: #22e6c8; }
.badge-dwa { font-size: 10px; background: #f5c542; color: #1a1a1a; padding: 2px 7px; border-radius: 5px; font-family: ui-monospace, monospace; }

/* 名前入力 */
.namebox { font-size: 13px; color: #cfcfe0; margin: 6px 0 12px; display: flex; align-items: center; gap: 8px; }
.namebox input {
  background: #0b0b14; border: 1px solid var(--border); color: #fff;
  border-radius: 6px; padding: 5px 10px; font-size: 13px; width: 160px;
}

/* 戦績 */
.stats-body { font-size: 13px; }
.stats-meta { font-size: 11px; color: var(--muted); margin-bottom: 8px; }
table.lb { width: 100%; border-collapse: collapse; }
table.lb th, table.lb td { text-align: left; padding: 5px 8px; border-bottom: 1px solid var(--border); }
table.lb th { font-size: 11px; color: var(--muted); text-transform: uppercase; }
table.lb td:first-child, table.lb td:nth-child(3) { width: 48px; }
.recent-title { font-size: 12px; color: var(--muted); margin: 14px 0 6px; }
ul.recent { list-style: none; padding: 0; font-size: 12px; line-height: 1.9; color: #cfcfe0; }
.muted { color: var(--muted); }

/* ============ レスポンシブ ============ */
@media (max-width: 720px) {
  #hud { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .hud-cell.score { grid-column: span 2; }
}
