:root {
  --navy: #1b3570;
  --navy-d: #142a5c;
  --yellow: #f5c518;
  --yellow-d: #e0a800;
  --blue: #2b5fb8;
  --slot-bg: #ffffff;
  --slot-line: #c7ccd6;
  --text: #1a1f2b;
  --page-bg: #e8ebf0;
  --page-text: #1a1f2b;
  --btn-bg: #ffffff;
  --btn-line: #c3c9d4;
}
@media (prefers-color-scheme: dark) {
  :root {
    --page-bg: #15171c;
    --page-text: #e6e8ec;
    --btn-bg: #252a33;
    --btn-line: #3a4150;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--page-bg);
  color: var(--page-text);
  font-family: -apple-system, "PingFang TC", "Microsoft JhengHei", "Noto Sans TC", sans-serif;
  padding: 16px;
}

/* ---------- toolbar ---------- */
.toolbar { max-width: 820px; margin: 0 auto 12px; }
.toolbar h1 { font-size: 22px; margin: 0 0 8px; }
.tools { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.tools button {
  font: inherit; font-size: 14px;
  padding: 6px 12px; border-radius: 8px;
  border: 1px solid var(--btn-line); background: var(--btn-bg); color: var(--page-text);
  cursor: pointer;
}
.tools button:hover { filter: brightness(0.95); }
.tools button.danger { color: #d33; }
.hint { font-size: 12px; opacity: .7; margin: 8px 0 0; }

/* ---------- board frame ---------- */
.frame {
  max-width: 820px; margin: 0 auto;
  background: #1c1c1e;
  border-radius: 14px;
  padding: 14px;
  box-shadow: 0 8px 24px rgba(0,0,0,.35), inset 0 0 0 2px #333;
}
.board {
  position: relative;
  border-radius: 4px;
  padding: 16px 28px 18px;
  background:
    repeating-conic-gradient(from 0deg at 50% 42%, #1e3b7a 0deg 7deg, #173168 7deg 14deg);
  color: var(--text);
  overflow: hidden;
}
/* 左右黃色警示條 */
.board::before, .board::after {
  content: ""; position: absolute; top: 40px; bottom: 360px; width: 8px;
  background: repeating-linear-gradient(180deg, var(--yellow) 0 8px, transparent 8px 14px);
  border-radius: 2px;
}
.board::before { left: 10px; }
.board::after { right: 10px; }

/* ---------- scoreboard ---------- */
.scoreboard {
  position: relative;
  background: var(--blue);
  border: 4px solid #6fa0ec;
  border-radius: 10px;
  padding: 6px;
  margin: 0 6px;
}
.star {
  position: absolute; top: -14px; color: var(--navy-d);
  background: var(--yellow); width: 28px; height: 28px; display: grid; place-items: center;
  font-size: 16px; clip-path: polygon(0 0, 100% 0, 100% 100%);
}
.star.l { left: -18px; clip-path: polygon(0 0, 100% 0, 0 100%); }
.star.r { right: -18px; }
.score-wrap { overflow-x: auto; background: #fff; border-radius: 4px; }
#scoreTable { border-collapse: collapse; width: 100%; min-width: 560px; table-layout: fixed; }
#scoreTable th, #scoreTable td { border: 1px solid #9aa3b2; padding: 0; height: 34px; text-align: center; }
#scoreTable th { font-size: 20px; font-weight: 800; font-family: "Arial Black", Arial, sans-serif; }
#scoreTable th.diag {
  width: 92px; position: relative; font-size: 12px; font-weight: 700;
  background: linear-gradient(to top right, transparent calc(50% - 1px), #333 50%, transparent calc(50% + 1px));
}
#scoreTable th.diag .a { position: absolute; top: 2px; right: 6px; }
#scoreTable th.diag .b { position: absolute; bottom: 2px; left: 6px; }
#scoreTable th.rhe { background: #eef2fa; }
#scoreTable input {
  width: 100%; height: 100%; border: 0; background: transparent; text-align: center;
  font: inherit; font-size: 16px; font-weight: 700; color: var(--text); padding: 0;
}
#scoreTable input:focus { outline: 2px solid var(--yellow-d); outline-offset: -2px; background: #fffbe6; }
#scoreTable input.team { font-size: 14px; text-align: left; padding: 0 6px; }
#scoreTable td.runs { font-size: 18px; font-weight: 800; color: #c0262d; background: #eef2fa; }

/* ---------- field ---------- */
.field { position: relative; margin: 12px auto 0; }
.field-svg { display: block; width: 100%; height: auto; }
#positions { position: absolute; inset: 0; }
.pos {
  position: absolute; transform: translate(-50%, -50%);
  width: 20%; min-width: 80px;
  background: #fff; border: 2px solid #3b4457; border-radius: 6px;
  box-shadow: 0 2px 0 rgba(0,0,0,.35);
}
.pos .lbl {
  display: block; font-size: 11px; font-weight: 700; padding: 1px 4px; line-height: 1.2;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: #1a1f2b;
}
.pos .lbl .code { color: #c0262d; margin-right: 3px; }
.pos .lbl small { font-size: 8px; font-weight: 400; color: #556; margin-left: 2px; }
.pos .slot { border: 0; border-top: 1px solid var(--slot-line); border-radius: 0 0 5px 5px; min-height: 46px; font-size: 18px; }

/* ---------- slot (可輸入 / 可拖曳) ---------- */
.slot {
  display: flex; align-items: center; justify-content: center; gap: 3px;
  min-height: 30px; padding: 2px 6px;
  background: var(--slot-bg); border: 1px solid var(--slot-line); border-radius: 4px;
  font-size: 15px; font-weight: 700; color: var(--text);
  cursor: text; user-select: none; -webkit-user-select: none;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  touch-action: pan-y;
}
.slot .snum { font-size: .85em; font-weight: 800; color: #c0262d; font-variant-numeric: tabular-nums; flex: none; }
.slot .sname { overflow: hidden; text-overflow: ellipsis; }
.slot:empty::before { content: attr(data-ph); color: #c3c8d2; font-weight: 400; font-size: 12px; }
.slot.filled { cursor: grab; }
.slot[contenteditable="true"] {
  cursor: text; user-select: text; -webkit-user-select: text;
  outline: 2px solid var(--yellow-d); background: #fffbe6; text-overflow: clip;
}
.slot.drop-target { outline: 3px dashed var(--yellow-d); outline-offset: -1px; background: #fff6cc; }
.slot.drag-src { opacity: .35; }

.ghost {
  position: fixed; left: 0; top: 0; z-index: 9999; pointer-events: none;
  padding: 6px 12px; border-radius: 6px;
  background: var(--yellow); color: #1a1f2b; font-weight: 800; font-size: 15px;
  box-shadow: 0 6px 16px rgba(0,0,0,.35);
  transform: translate(-50%, -120%);
}
body.dragging, body.dragging * { cursor: grabbing !important; }

/* ---------- bottom ---------- */
.bottom {
  display: grid; grid-template-columns: 1.25fr 1fr; gap: 14px; align-items: end;
  margin-top: 6px;
}

.bench {
  position: relative;
  background: rgba(200, 208, 222, .85);
  border-radius: 6px; padding: 10px 10px 10px 34px;
  min-height: 124px;
}
.bench-title {
  position: absolute; left: 7px; top: 12px;
  writing-mode: vertical-rl; letter-spacing: 6px;
  font-size: 17px; font-weight: 800; color: #1b3570;
}
.bench-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.bench .slot { font-size: 14px; min-height: 28px; }

/* 退場球員：在預備選手與打序表下方 */
.exited { grid-column: 1 / -1; background: rgba(170, 176, 190, .85); }
.exited .bench-title { color: #7a1f1f; }
.exited .slot { background: #eef0f4; color: #4b5263; }
.log { margin: 10px 0 0; padding: 8px 0 0; border-top: 1px dashed rgba(26,31,43,.3); font-size: 13px; color: #1a1f2b; }
.log:empty { display: none; }
.log div { padding: 2px 0; }
.log time { font-weight: 800; color: #1b3570; margin-right: 6px; font-variant-numeric: tabular-nums; }

.lineup {
  background: linear-gradient(#ffd84d, var(--yellow));
  border: 4px solid #1a1f2b; border-radius: 12px;
  padding: 8px 10px 10px;
  box-shadow: 0 0 0 3px var(--yellow);
}
.lineup h2 {
  margin: 0 0 6px; text-align: center; font-size: 18px; font-weight: 900;
  color: #fff; background: #1b3570; border-radius: 6px; padding: 4px;
  letter-spacing: 2px;
}
.lineup h2 span { color: var(--yellow); }
.lineup-head, .lu-row {
  display: grid; grid-template-columns: 36px minmax(104px, 1fr) 1fr 46px; gap: 6px; align-items: center;
}
.lineup-head { font-weight: 800; font-size: 14px; text-align: center; margin-bottom: 4px; color: #1a1f2b; }
.lu-row { margin-bottom: 5px; }
.lu-no {
  height: 30px; display: grid; place-items: center;
  background: #fff; border: 2px solid #1a1f2b; border-radius: 6px;
  font-family: "Arial Black", Arial, sans-serif; font-size: 16px; color: #1a1f2b;
}
.lu-num {
  width: 100%; height: 30px; font: inherit; font-family: "Arial Black", Arial, sans-serif;
  font-size: 15px; text-align: center; padding: 0;
  border: 1px solid var(--slot-line); border-radius: 4px; background: #fff; color: #c0262d;
}
.lu-num::placeholder { color: #c3c8d2; font-family: inherit; }
.lu-num:disabled { background: #f3f4f7; }
.lu-num:focus { outline: 2px solid var(--yellow-d); outline-offset: -2px; background: #fffbe6; }
.lu-pos {
  width: 100%; height: 30px; font: inherit; font-size: 14px; font-weight: 700;
  border: 1px solid var(--slot-line); border-radius: 4px; background: #fff; color: var(--text);
  padding: 0 2px; text-align: center; text-align-last: center;
}
/* 已經被其他棒次選走的守位；重複的守位 */
.lu-pos option.taken { color: #b45309; background: #fff4d6; }
.lu-pos.dup { border: 2px solid #e11d48; background: #ffe4ea; color: #9f1239; }

/* ---------- 比賽開始、異動確認 ---------- */
.tools .start { background: #16a34a; border-color: #16a34a; color: #fff; font-weight: 800; }
.tools .end { color: #b45309; }
.live-badge {
  display: inline-flex; align-items: center; gap: 6px; padding: 5px 12px; border-radius: 99px;
  background: #dcfce7; color: #15803d; font-size: 14px; font-weight: 800;
}
.live-badge::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: #16a34a; animation: blink 1.4s infinite; }
@keyframes blink { 50% { opacity: .25; } }
.confirm-fab {
  position: fixed; left: 50%; bottom: 18px; transform: translateX(-50%); z-index: 50;
  font: inherit; font-size: 16px; font-weight: 900; padding: 12px 22px; border: 0; border-radius: 99px;
  background: #e11d48; color: #fff; box-shadow: 0 8px 22px rgba(225,29,72,.45); cursor: pointer;
}
dialog.changes {
  width: min(440px, calc(100vw - 32px)); border: 0; border-radius: 16px; padding: 0;
  box-shadow: 0 20px 50px rgba(0,0,0,.4); color: #1a1f2b;
}
dialog.changes::backdrop { background: rgba(10,15,30,.55); }
.dlg-head { padding: 14px 18px; background: #1b3570; color: #fff; font-weight: 900; font-size: 17px; }
.dlg-body { padding: 16px 18px; }
.notation { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.notation span {
  font-family: "Arial Black", Arial, sans-serif; font-size: 26px; letter-spacing: 1px;
  padding: 4px 14px; border-radius: 10px; background: #fff4d6; color: #10203f;
}
.dlg-lines { margin: 0; padding-left: 20px; font-size: 14px; line-height: 1.7; }
.dlg-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px 16px; }
.dlg-foot button {
  font: inherit; font-size: 15px; font-weight: 800; padding: 9px 18px; border-radius: 10px; cursor: pointer;
  border: 1px solid #c3c9d4; background: #fff; color: #1a1f2b;
}
.dlg-foot button.ok { background: #1b3570; border-color: #1b3570; color: #fff; }

/* ---------- responsive ---------- */
@media (max-width: 680px) {
  body { padding: 12px 16px; }
  .frame { padding: 8px; border-radius: 10px; }
  .board { padding: 12px 14px 14px; }
  .board::before, .board::after { display: none; }
  .star { display: none; }
  .bottom { grid-template-columns: 1fr; }
  .lineup { order: -1; }
  /* 手機：球場貼齊左右邊，守備名字放大 */
  body { padding: 10px 8px; }
  .frame { padding: 4px; }
  .board { padding: 10px 8px 12px; }
  .field { margin: 8px -6px 0; }
  .pos { min-width: 0; width: 23.5%; border-width: 1.5px; }
  .pos .lbl { font-size: 10px; padding: 0 3px; }
  .pos .lbl small { display: none; }
  .pos .slot { font-size: 15px; min-height: 30px; padding: 1px 2px; gap: 2px; }
  .pos .slot .snum { font-size: 12px; }
  .pos .slot .sname { max-width: 100%; }
}
@media (max-width: 680px) {
  /* 手機：預備球員、退場球員的標題改成橫排放在上方 */
  .bench { padding: 10px; min-height: 0; }
  .bench-title {
    position: static; writing-mode: horizontal-tb; letter-spacing: 2px;
    font-size: 16px; margin: 0 0 8px 2px;
  }
}
@media (max-width: 420px) {
  .bench-grid { grid-template-columns: repeat(3, 1fr); }
}

/* ---------- print ---------- */
@media print {
  body { background: #fff; padding: 0; }
  .toolbar, .confirm-fab { display: none; }
  .frame { box-shadow: none; }
  .board, .lineup, .bench { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

/* ---------- 登入列 ---------- */
.authbar {
  max-width: 820px; margin: -16px auto 14px;
  background: #1b3570; color: #fff;
  border-radius: 0 0 12px 12px; border-bottom: 3px solid var(--yellow);
  padding: 8px 12px;
}
.authbar-in { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.ab-brand { font-weight: 800; font-size: 17px; margin-right: auto; letter-spacing: 1px; }
.ab-me { font-size: 14px; font-weight: 700; }
.ab-role { font-size: 11px; padding: 1px 8px; border-radius: 99px; margin-left: 4px; font-weight: 700; vertical-align: 1px; }
.ab-role.admin { background: var(--yellow); color: #1a1f2b; }
.ab-role.coach { background: #cfe0ff; color: #1b3570; }
.ab-login { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.ab-login input {
  width: 130px; height: 32px; padding: 0 10px; font: inherit; font-size: 14px;
  border: 1px solid rgba(255,255,255,.35); border-radius: 8px; background: rgba(255,255,255,.12); color: #fff;
}
.ab-login input::placeholder { color: rgba(255,255,255,.65); }
.ab-login input:focus { outline: 2px solid var(--yellow); outline-offset: -1px; background: rgba(255,255,255,.2); }
.ab-btn {
  display: inline-flex; align-items: center; height: 32px; padding: 0 14px; border-radius: 8px;
  font: inherit; font-size: 14px; font-weight: 700; text-decoration: none; cursor: pointer;
  color: #fff; background: transparent; border: 1px solid rgba(255,255,255,.4);
}
.ab-btn.primary { background: var(--yellow); color: #1a1f2b; border-color: var(--yellow); }
.ab-btn:hover { filter: brightness(1.08); }
.ab-flash { margin-top: 8px; font-size: 13px; padding: 6px 10px; border-radius: 8px; }
.ab-flash.ok { background: #e7f8ee; color: #15803d; }
.ab-flash.err { background: #fdecec; color: #c62828; }
@media (max-width: 680px) {
  .authbar { margin: -10px -8px 12px; border-radius: 0; }
  .ab-brand { width: 100%; }
  .ab-login { width: 100%; }
  .ab-login input { flex: 1 1 0; min-width: 0; width: auto; }
}
@media print { .authbar { display: none; } }

/* ---------- 球賽資訊列 ---------- */
.gamebar {
  max-width: 820px; margin: 0 auto 12px;
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 10px;
  background: #fff7d1; color: #1a1f2b; border: 2px solid var(--yellow); border-radius: 12px; padding: 10px 12px;
}
.gamebar .ab-btn { color: #1b3570; border-color: #1b3570; background: #fff; }
.gb-main { margin-right: auto; min-width: 0; }
.gb-title { font-size: 18px; font-weight: 900; }
.gb-meta { font-size: 13px; opacity: .75; }
.gb-status { font-size: 13px; font-weight: 700; padding: 3px 10px; border-radius: 99px; background: #e7f8ee; color: #15803d; }
.gb-status.pending { background: #eef2fa; color: #3b4457; }
.gb-status.err { background: #fdecec; color: #c62828; }
.gb-status.view { background: #e3ecff; color: #1b3570; }
body.readonly .slot, body.readonly .slot.filled { cursor: default; }
@media (max-width: 680px) { .gb-main { width: 100%; } }
@media print { .gamebar { display: none; } }

/* ---------- 首頁賽事列表 ---------- */
.games-home {
  max-width: 820px; margin: 0 auto 14px; padding: 12px 14px;
  background: var(--btn-bg); border: 1px solid var(--btn-line); border-radius: 12px;
}
.gh-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 12px; margin-bottom: 10px; }
.gh-head h2 { margin: 0; font-size: 17px; }
.gh-date { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; font-size: 13px; }
.gh-date input {
  font: inherit; font-size: 14px; height: 32px; padding: 0 8px; border-radius: 8px;
  border: 1px solid var(--btn-line); background: var(--page-bg); color: var(--page-text);
}
.gh-back { color: inherit; font-weight: 700; }
.gh-list { display: grid; gap: 6px; }
.gh-row {
  display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; padding: 9px 12px;
  border: 1px solid var(--btn-line); border-radius: 10px; color: inherit; text-decoration: none;
}
.gh-row:hover { border-color: #2b5fb8; }
.gh-status { font-size: 12px; font-weight: 800; padding: 2px 10px; border-radius: 99px; background: #e5e7eb; color: #4b5563; }
.gh-status.live { background: #dcfce7; color: #15803d; }
.gh-status.done { background: #f3f4f6; color: #9ca3af; }
.gh-date-label { font-weight: 700; font-variant-numeric: tabular-nums; }
.gh-title { font-weight: 800; }
.gh-title small { font-weight: 400; opacity: .6; }
.gh-group { font-size: 12px; font-weight: 700; padding: 1px 8px; border-radius: 99px; background: #e3ecff; color: #1b3570; }
.gh-go { margin-left: auto; font-size: 13px; font-weight: 700; color: #2b5fb8; }
.gh-empty { margin: 4px 0; font-size: 14px; opacity: .8; }
.gh-empty a { color: inherit; font-weight: 700; }
.gh-past { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 10px; font-size: 13px; opacity: .9; }
.gh-past a { padding: 2px 10px; border-radius: 99px; border: 1px solid var(--btn-line); color: inherit; text-decoration: none; font-variant-numeric: tabular-nums; }
.gh-past a.on { background: #1b3570; border-color: #1b3570; color: #fff; }
@media print { .games-home { display: none; } }
