/* ============ 海战棋 · 多人对战 ============ */

:root {
  --bg: #0a1220;
  --panel: #101c30;
  --panel2: #16263f;
  --line: #22375a;
  --text: #dce7f7;
  --muted: #8fa3c0;
  --accent: #4cc9f0;
  --danger: #ff5964;
  --ok: #37d67a;
  --warn: #ffd166;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "PingFang SC", "Noto Sans CJK SC", "Microsoft YaHei", system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

button { font-family: inherit; }
input { font-family: inherit; }

.hidden { display: none !important; }
.muted { color: var(--muted); }
.pad { padding: 10px; }

/* ---------------- 顶栏 ---------------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  padding-top: calc(8px + env(safe-area-inset-top));
  background: rgba(10, 18, 32, .92);
  backdrop-filter: blur(6px);
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 800; font-size: 17px; white-space: nowrap; }
.room-info { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room-info b { color: var(--text); letter-spacing: 1px; }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.conn { font-size: 12px; color: var(--ok); white-space: nowrap; }
.conn.bad { color: var(--warn); }

/* ---------------- 登录 ---------------- */

.screen { min-height: 100vh; }
.login-screen { display: flex; align-items: flex-start; justify-content: center; padding: 24px 14px 40px; }
.login-card {
  width: 100%;
  max-width: 420px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 22px 18px;
}
.login-card h1 { margin: 0 0 4px; font-size: 22px; }
.login-card .sub { margin: 0 0 18px; color: var(--muted); font-size: 13px; }
.field-label { display: block; font-size: 13px; color: var(--muted); margin: 10px 0 6px; }
.input {
  width: 100%;
  height: 46px;
  padding: 0 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel2);
  color: var(--text);
  font-size: 16px;
  outline: none;
}
.input:focus { border-color: var(--accent); }
.row { display: flex; gap: 8px; margin-top: 10px; }
.row .input { flex: 1; }
.sep { text-align: center; color: var(--muted); font-size: 12px; margin: 16px 0 4px; position: relative; }
.rooms-title { display: flex; align-items: center; justify-content: space-between; margin-top: 20px; font-size: 13px; color: var(--muted); }
.admin-entry { margin-top: 14px; text-align: center; }
.admin-close-btn { flex: none; }
.room-item .admin-close-btn { margin-left: 8px; }

.room-list { margin-top: 8px; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; max-height: 260px; overflow-y: auto; }
.room-item {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
  font-size: 14px;
  min-height: 44px;
}
.room-item:last-child { border-bottom: none; }
.room-item:active { background: var(--panel2); }
.room-item .code { font-weight: 800; letter-spacing: 2px; color: var(--accent); }
.room-item .meta { margin-left: auto; color: var(--muted); font-size: 12px; }

/* ---------------- 按钮 ---------------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 16px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel2);
  color: var(--text);
  font-size: 15px;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: #2563eb; border-color: #2563eb; color: #fff; font-weight: 700; }
.btn.danger { background: #4a1520; border-color: #7f2434; color: #ffc9cf; }
.btn.ok { background: #14432c; border-color: #1e6b46; color: #c6f6dd; }
.btn.ghost { background: transparent; }
.btn.block { width: 100%; margin-top: 12px; }
.btn.small { min-height: 36px; font-size: 13px; padding: 0 10px; }
.btn.tiny { min-height: 30px; font-size: 12px; padding: 0 8px; border-radius: 8px; }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn.active { outline: 2px solid #fff; outline-offset: 1px; }

/* ---------------- 房间布局 ---------------- */

.layout { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 14px; padding: 12px; align-items: start; }
.stage { min-width: 0; }
.sidebar {
  position: sticky;
  top: 62px;
  max-height: calc(100vh - 76px);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
}
.card h3 { margin: 0 0 8px; font-size: 14px; color: var(--muted); font-weight: 600; }

/* ---------------- 玩家条 ---------------- */

.playersbar {
  display: flex;
  gap: 8px;
  padding: 10px 12px 2px;
  overflow-x: auto;
  scrollbar-width: thin;
}
.pchip {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  min-height: 46px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--panel);
  cursor: pointer;
  white-space: nowrap;
  touch-action: manipulation;
  user-select: none;
}
.pchip.active { outline: 2px solid var(--accent); }
.pchip.dead { opacity: .55; }
.pchip .dot { width: 12px; height: 12px; border-radius: 50%; flex: none; }
.pchip .nm { font-weight: 700; font-size: 14px; }
.pchip .badges { display: flex; gap: 4px; flex-wrap: wrap; }

.badge {
  font-size: 11px;
  line-height: 18px;
  padding: 0 7px;
  border-radius: 999px;
  background: #1c2b45;
  color: var(--muted);
  white-space: nowrap;
}
.badge.you { background: #16324f; color: var(--accent); }
.badge.host { background: #4a3a10; color: var(--warn); }
.badge.ready { background: #14432c; color: #7ce8b0; }
.badge.dead { background: #40141b; color: #ff9aa3; }
.badge.off { background: #333; color: #bbb; }
.badge.spect { background: #2b1d45; color: #c9a8ff; }
.badge.warn { background: #4a3a10; color: var(--warn); }

.bombs-row { display: flex; gap: 4px; flex-wrap: wrap; }
.bombchip {
  font-size: 11px;
  padding: 1px 7px;
  border-radius: 999px;
  border: 1px solid var(--muted);
  background: #20202e;
  color: #ffd166;
  white-space: nowrap;
}
button.bombchip { cursor: pointer; min-height: 26px; touch-action: manipulation; }
button.bombchip:active { background: #3a3a55; }

/* ---------------- 棋盘 ---------------- */

.boards { display: flex; flex-wrap: wrap; gap: 14px; align-items: flex-start; }
.board-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px;
  overflow-x: auto;
}
.board-card.me { border-color: #2f5688; }
.board-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.board-head .dot { width: 12px; height: 12px; border-radius: 50%; flex: none; }
.board-head .nm { font-weight: 800; }
.board-head .stat { font-size: 12px; color: var(--muted); }
.board-head .spacer { flex: 1; }

.board-grid {
  display: grid;
  --cell: 32px;
  --head: 20px;
  grid-template-columns: var(--head) repeat(10, var(--cell));
  grid-template-rows: var(--head) repeat(10, var(--cell));
  gap: 2px;
  user-select: none;
  -webkit-user-select: none;
}
.board-grid .head {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  color: var(--muted);
}
.cell {
  position: relative;
  border-radius: 5px;
  background: #0e1b30;
  border: 1px solid #1b2f50;
  cursor: default;
  touch-action: manipulation;
  overflow: hidden;
}
.cell.clickable { cursor: pointer; }
.cell.clickable:active { background: #16263f; }
.cell.targetable { border-color: #2c5488; }
.cell.targetable:hover { background: #17304f; }
.cell.own-ship { background: #24486f; border-color: #3b6ea8; }
.cell.own-ship.own-sunk { background: #3a2430; border-color: #6b3a4a; }
.cell.sunk { background: #232a36; border-color: #4a3b3b; }
.cell.sunk::after {
  content: "";
  position: absolute;
  inset: 0;
  background: repeating-linear-gradient(45deg, rgba(255, 255, 255, .07) 0 4px, transparent 4px 8px);
  pointer-events: none;
}
/* 每人最新一回合的攻击高亮（放在选中/瞄准之前，优先级更低） */
.cell.latest { box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .55); }
.cell.sel { box-shadow: inset 0 0 0 3px #fff; }
.cell.aim-ok { box-shadow: inset 0 0 0 2px var(--ok); background: rgba(55, 214, 122, .22); }
.cell.aim-bad { box-shadow: inset 0 0 0 2px var(--danger); background: rgba(255, 89, 100, .18); }

/* 每人最新一回合的攻击高亮（攻击标记的呼吸描边） */
.chip.latest { animation: latestPulse 1.8s ease-in-out infinite; }
@keyframes latestPulse {
  0%, 100% { box-shadow: inset 0 0 0 2px rgba(255, 255, 255, .95); }
  50% { box-shadow: inset 0 0 0 2px rgba(255, 255, 255, .3); }
}

/* 舰船轮廓描边（相邻舰船可区分） */
.cell.own-ship { --oc: #8fd3ff; }
.cell.own-ship.own-sunk { --oc: #ff9daa; }
.cell.sunk { --oc: #e0b4b4; }
.cell.se-t { border-top: 2px solid var(--oc, #9fd8ff) !important; }
.cell.se-b { border-bottom: 2px solid var(--oc, #9fd8ff) !important; }
.cell.se-l { border-left: 2px solid var(--oc, #9fd8ff) !important; }
.cell.se-r { border-right: 2px solid var(--oc, #9fd8ff) !important; }

/* 炸弹一览 */
.bomblist { display: flex; flex-direction: column; gap: 8px; }
.bombrow { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.bombrow.dead { opacity: .55; }
.bombrow .bname { display: flex; align-items: center; gap: 6px; min-width: 96px; }
.bombrow .bname .dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.bombrow .bname .nm { font-weight: 700; font-size: 13px; }
button.bombchip:disabled { opacity: .4; cursor: not-allowed; }

.chips {
  position: absolute;
  inset: 1px;
  display: flex;
  flex-wrap: wrap;
  align-content: center;
  justify-content: center;
  gap: 1px;
  pointer-events: none;
  z-index: 1;
}
.chip {
  width: calc(50% - 2px);
  height: calc(50% - 2px);
  min-width: 10px;
  min-height: 10px;
  border-radius: 3px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 9px;
  font-weight: 900;
  color: #08101d;
  line-height: 1;
}
.chips.one .chip { width: 82%; height: 82%; border-radius: 6px; font-size: 15px; }
.chip.more { background: #333 !important; color: #aaa; font-size: 9px; }

/* 布阵编辑器 */
.placing-wrap { display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.dock { display: flex; flex-wrap: wrap; gap: 8px; }
.shipbtn {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  min-height: 52px;
  padding: 6px 10px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel2);
  color: var(--text);
  cursor: pointer;
  touch-action: manipulation;
  font-size: 12px;
}
.shipbtn.sel { outline: 2px solid var(--accent); background: #1d3453; }
.shipbtn .shape { display: flex; gap: 1px; }
.shipbtn .sq { width: 8px; height: 8px; background: #4cc9f0; border-radius: 2px; }
.shipbtn.done { opacity: .45; }
.ghost-ship { background: rgba(76, 201, 240, .3) !important; border-color: var(--accent) !important; }
.bad-ship { background: rgba(255, 89, 100, .3) !important; border-color: var(--danger) !important; }

/* ---------------- 侧栏内容 ---------------- */

.turn-line { font-size: 15px; font-weight: 700; }
.turn-line .who { font-weight: 900; }
.shots { color: var(--muted); font-size: 13px; margin-top: 4px; }

.detail-line { font-size: 13px; line-height: 1.7; white-space: pre-line; }
.detail-line .k { color: var(--muted); }

.feed { display: flex; flex-direction: column; max-height: 320px; overflow-y: auto; margin: -4px -4px; }
.feed-item { padding: 6px 4px; border-bottom: 1px solid #14233a; font-size: 13px; line-height: 1.5; word-break: break-word; }
.feed-item:last-child { border-bottom: none; }
.feed-item .pname { font-weight: 700; }
.feed-item .hit { color: #ff9aa3; font-weight: 700; }
.feed-item .miss { color: #7fb2e5; }
.feed-item .unk { color: var(--muted); }
.feed-item .coord { font-family: ui-monospace, monospace; color: #ffd166; }

/* ---------------- 行动栏 ---------------- */

.actionbar {
  position: sticky;
  bottom: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  padding: 10px 12px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  background: rgba(10, 18, 32, .95);
  backdrop-filter: blur(6px);
  border-top: 1px solid var(--line);
  min-height: 62px;
}
.actionbar .grow { flex: 1; }
.action-hint { font-size: 13px; color: var(--muted); }
/* 「结束回合」固定在行动栏最右侧，避免与开火按钮位置互换导致误触 */
.end-turn-btn { margin-left: auto; flex: none; }
.end-turn-btn.attention { box-shadow: 0 0 0 2px rgba(255, 209, 102, .65); }

details.card summary { cursor: pointer; font-weight: 700; list-style: none; }
details.card summary::-webkit-details-marker { display: none; }
details.card[open] summary { margin-bottom: 8px; }
details.card .detail-line { white-space: pre-line; }
.placement-progress { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.admin-entry .btn { margin: 0 4px; }

/* 规则弹窗 */
.modal {
  position: fixed;
  inset: 0;
  z-index: 110;
  background: rgba(4, 8, 16, .72);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal-card {
  width: 100%;
  max-width: 560px;
  max-height: 84vh;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 12px 48px rgba(0, 0, 0, .6);
}
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
}
.modal-head h2 { margin: 0; font-size: 17px; }
.modal-body {
  padding: 14px 16px;
  overflow-y: auto;
  font-size: 14px;
  line-height: 1.8;
  white-space: pre-line;
  color: var(--text);
  -webkit-overflow-scrolling: touch;
}

/* ---------------- 提示 ---------------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(84px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 90;
  background: #2a1b1b;
  border: 1px solid #7f2434;
  color: #ffd7db;
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 14px;
  max-width: 90vw;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .5);
}
.banner { cursor: pointer; }

.banner {
  position: fixed;
  top: 18%;
  left: 50%;
  transform: translateX(-50%);
  z-index: 80;
  background: #12263f;
  border: 2px solid var(--warn);
  border-radius: 16px;
  padding: 20px 28px;
  text-align: center;
  box-shadow: 0 10px 40px rgba(0, 0, 0, .6);
  font-size: 18px;
  max-width: 92vw;
}
.banner .big { font-size: 26px; font-weight: 900; color: var(--warn); }

/* ---------------- 窄屏（触屏）适配 ---------------- */

.tabs { display: none; }

@media (max-width: 999px) {
  .layout { display: block; padding: 8px; }
  .sidebar { position: static; max-height: none; margin-top: 12px; overflow: visible; }
  .boards { gap: 10px; }
  .board-card { width: 100%; }

  /* 一次只显示一张棋盘 */
  .boards.single .board-card { display: none; }
  .boards.single .board-card.active { display: block; }

  .board-grid { --cell: min(8.6vw, 40px); --head: 18px; margin: 0 auto; }

  .tabs {
    display: flex;
    gap: 6px;
    overflow-x: auto;
    padding: 4px 0 10px;
  }
  .tabbtn {
    min-height: 40px;
    padding: 0 14px;
    border-radius: 10px;
    border: 1px solid var(--line);
    background: var(--panel2);
    color: var(--text);
    font-size: 14px;
    white-space: nowrap;
    touch-action: manipulation;
  }
  .tabbtn.active { background: #1d3453; outline: 2px solid var(--accent); }
  .feed { max-height: 240px; }
}
