:root {
  --bg: #f2f5f4;
  --card: #ffffff;
  --text: #1b2421;
  --muted: #6b7a75;
  --line: #e1e7e5;
  --primary: #1f6f5c;
  --primary-2: #2a8c75;
  --danger: #c93b3b;
  --a: #e5484d;
  --b: #3e63dd;
  --a-soft: #fde8e8;
  --b-soft: #e6ecfd;
  --win: #1f8a4c;
  --radius: 14px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Hiragino Sans", "Yu Gothic UI", "Meiryo", sans-serif;
  font-size: 16px;
  line-height: 1.5;
  min-height: 100dvh;
}
body.scoring { background: #0f1513; overflow: hidden; }
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
input, select { font: inherit; }

.muted { color: var(--muted); }
.small { font-size: 0.82rem; }

/* ----- ボタン ----- */
.btn {
  border: 0; border-radius: 10px; padding: 10px 16px;
  background: var(--line); font-weight: 600;
}
.btn:disabled { opacity: .4; cursor: default; }
.btn.primary { background: var(--primary); color: #fff; }
.btn.primary:active:not(:disabled) { background: var(--primary-2); }
.btn.big { display: block; width: 100%; padding: 15px; font-size: 1.1rem; margin-top: 14px; }
.btn.small { padding: 6px 12px; font-size: .9rem; }
.btn.ghost { background: transparent; border: 1px solid var(--line); }
.btn.danger { color: var(--danger); }
.btn.light { background: rgba(255,255,255,.1); color: #fff; border: 1px solid rgba(255,255,255,.18); }

/* ----- ログイン ----- */
.login { max-width: 420px; margin: 0 auto; padding: 48px 16px; text-align: center; }
.logo { width: 88px; height: 88px; border-radius: 22px; }
.login h1 { margin: 4px 0; }
.login form { text-align: left; margin-top: 20px; }
.login label { display: block; font-weight: 600; margin-bottom: 12px; }
.login input, .inline input, select {
  display: block; width: 100%; margin-top: 6px; padding: 12px;
  border: 1px solid var(--line); border-radius: 10px; background: #fff;
}
.install {
  display: flex; align-items: center; gap: 8px; justify-content: space-between;
  background: #e8f4f0; border: 1px solid #b9dccf; border-radius: 12px;
  padding: 10px 12px; margin: 12px 0; font-size: .88rem; text-align: left;
}
.install-x { background: none; border: 0; color: var(--muted); padding: 4px 6px; flex: none; }
.manual-link { color: var(--primary); font-weight: 600; font-size: .9rem; }
.link-admin { margin-top: 18px; background: none; border: 0; color: var(--muted); font-size: .85rem; text-decoration: underline; }
.note { font-size: .85rem; color: var(--muted); background: #fff8e6; border-radius: 10px; padding: 10px 12px; text-align: left; }

/* ----- ホーム ----- */
.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 12px 16px; padding-top: max(12px, env(safe-area-inset-top));
  background: var(--primary); color: #fff;
}
.top .btn.ghost { color: #fff; border-color: rgba(255,255,255,.4); }
.room-name { font-weight: 700; font-size: 1.1rem; }
.mode { font-size: .75rem; opacity: .85; }
.tabs { display: flex; background: #fff; border-bottom: 1px solid var(--line); position: sticky; top: 56px; z-index: 4; }
.tab { flex: 1; border: 0; background: none; padding: 12px 2px; font-weight: 600; font-size: .86rem; color: var(--muted); border-bottom: 3px solid transparent; white-space: nowrap; }

.sync-btn {
  display: inline-flex; align-items: center; gap: 6px; margin-top: 2px;
  background: rgba(255,255,255,.14); border: 0; border-radius: 999px; padding: 2px 4px 2px 10px; color: #fff;
}
.sync-btn.warn { background: #f6c453; color: #3d2b00; }
.sync-label { background: rgba(255,255,255,.22); border-radius: 999px; padding: 1px 8px; font-weight: 700; }
.sync-btn.warn .sync-label { background: rgba(0,0,0,.12); }
.unsent { font-size: .75rem; color: #f6c453; font-weight: 700; }
.top-btns { display: flex; gap: 6px; flex: none; }
.top-btns .btn { padding: 6px 9px; font-size: .8rem; }
a.btn { text-decoration: none; display: inline-block; }
.room-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* 招待 */
.share { text-align: center; }
.qr { width: min(260px, 70vw); margin: 12px auto; aspect-ratio: 1; display: grid; place-items: center; color: var(--muted); font-size: .85rem; }
.qr svg, .poster-qr svg { width: 100%; height: auto; display: block; }
.share .inline input { font-size: .8rem; color: var(--muted); }
.share .note { margin-top: 14px; }
.poster { display: none; }

@media print {
  @page { size: A4 portrait; margin: 15mm; }
  body { background: #fff !important; }
  #app > *:not(.poster) { display: none !important; }
  .toast { display: none !important; }
  .poster { display: block; text-align: center; color: #111; }
  .poster-logo { width: 34mm; height: 34mm; border-radius: 8mm; }
  .poster-title { font-size: 44pt; font-weight: 800; margin-top: 4mm; }
  .poster-lead { font-size: 20pt; margin-bottom: 8mm; }
  .poster-qr { width: 110mm; margin: 0 auto; }
  .poster-room { font-size: 18pt; font-weight: 700; margin: 6mm 0; }
  .poster-steps { display: inline-block; text-align: left; font-size: 15pt; line-height: 1.9; margin: 0; }
}

/* ダッシュボード */
.tops { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.top-card { background: #f3f8f6; border-radius: 12px; padding: 10px 12px; min-width: 0; }
.top-card.empty { opacity: .55; }
.top-label { font-size: .78rem; color: var(--muted); font-weight: 600; }
.top-name { font-size: 1.1rem; font-weight: 800; overflow-wrap: anywhere; }
.top-value { font-size: .85rem; color: var(--primary); font-weight: 600; }
.state { font-size: .75rem; font-weight: 700; }
.state.playing { color: var(--a); }
.state.paused { color: #b7791f; }
.tag { font-size: .75rem; color: var(--muted); border: 1px solid var(--line); border-radius: 999px; padding: 2px 10px; }
.btn.locked { opacity: .5; }
.void-end { margin-top: 14px; }
.score-top .void { justify-self: end; }
.tab.on { color: var(--primary); border-bottom-color: var(--primary); }
.content { max-width: 640px; margin: 0 auto; padding: 12px 16px 40px; }

.card { background: var(--card); border-radius: var(--radius); padding: 16px; margin-bottom: 12px; box-shadow: 0 1px 2px rgba(0,0,0,.05); }
.card h2 { font-size: 1.05rem; margin: 0 0 10px; }
.card h3 { font-size: .9rem; margin: 16px 0 8px; color: var(--muted); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
.card-head h2 { margin: 0; }
.card-head select { width: auto; margin: 0; padding: 6px 10px; }

.formats { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.fmt {
  text-align: left; border: 2px solid var(--line); background: #fff; border-radius: 12px; padding: 10px 12px;
  display: flex; flex-direction: column;
}
.fmt b { font-size: .95rem; }
.fmt small { color: var(--muted); }
.fmt.on { border-color: var(--primary); background: #e8f4f0; }
.check { display: flex; align-items: center; gap: 8px; margin-top: 12px; }
.check input { width: 20px; height: 20px; accent-color: var(--primary); }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { border: 2px solid var(--line); background: #fff; border-radius: 999px; padding: 8px 16px; font-weight: 600; }
.chip.on.side-a { border-color: var(--a); background: var(--a-soft); }
.chip.on.side-b { border-color: var(--b); background: var(--b-soft); }
.chip.add { border-style: dashed; color: var(--muted); }

.row { display: flex; align-items: center; gap: 8px; padding: 10px 0; border-top: 1px solid var(--line); }
.row:first-of-type { border-top: 0; }
.grow { flex: 1; min-width: 0; }
.link-btn { display: flex; align-items: center; gap: 10px; background: none; border: 0; text-align: left; padding: 4px 0; }
.link-btn .chev { margin-left: auto; color: var(--muted); font-size: 1.3rem; }
.inline { display: flex; gap: 8px; }
.inline input { margin: 0; }
.inline .btn { flex: none; }

.hist .win { font-weight: 700; }
.hist .lose { color: var(--muted); }
.score-mini { margin: 0 6px; }
.badge { flex: none; width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; font-size: .85rem; color: #fff; }
.badge.win { background: var(--win); }
.badge.lose { background: #9aa6a2; }

table.rank { width: 100%; border-collapse: collapse; font-size: .92rem; }
.rank th { text-align: left; font-size: .75rem; color: var(--muted); font-weight: 600; padding: 4px; }
.rank td { padding: 8px 4px; border-top: 1px solid var(--line); white-space: nowrap; }
.rank tr.link { cursor: pointer; }
.rank tr.link:active { background: #f3f7f6; }
.rk { color: var(--muted); width: 1.5em; }
.bar { display: inline-block; width: 40px; height: 6px; background: var(--line); border-radius: 3px; margin-right: 6px; vertical-align: middle; overflow: hidden; }
.bar span { display: block; height: 100%; background: var(--primary); }

/* 個人成績 */
.big-stats { display: grid; grid-template-columns: repeat(3, 1fr); text-align: center; margin-bottom: 8px; }
.num { font-size: 1.8rem; font-weight: 800; line-height: 1.2; }
.num small { font-size: .8rem; font-weight: 600; margin: 0 2px; }
.kv { display: flex; justify-content: space-between; align-items: center; padding: 8px 0; border-top: 1px solid var(--line); }
.recent { letter-spacing: 2px; }
.dot { font-style: normal; }
.dot.win { color: var(--win); }
.dot.lose { color: #9aa6a2; }

/* ----- モーダル ----- */
.modal-bg, .overlay {
  position: fixed; inset: 0; z-index: 20; background: rgba(0,0,0,.55);
  display: grid; place-items: center; padding: 16px;
}
.modal {
  background: #fff; color: var(--text); border-radius: 18px; padding: 22px 20px;
  width: 100%; max-width: 380px; text-align: center;
  animation: pop .18s ease-out;
}
.modal h2 { margin: 0 0 10px; }
.modal p { margin: 6px 0; }
.modal-btns { display: flex; gap: 8px; justify-content: center; margin-top: 10px; flex-wrap: wrap; }
@keyframes pop { from { transform: scale(.92); opacity: 0; } to { transform: none; opacity: 1; } }

.draw { display: flex; align-items: center; justify-content: center; gap: 10px; margin: 16px 0; }
.draw-name {
  flex: 1; border: 3px solid var(--line); background: #fff; border-radius: 14px; padding: 18px 8px;
  font-size: 1.15rem; font-weight: 700; transition: transform .08s, background .08s;
  overflow-wrap: anywhere;
}
.draw-name.side-a.hi { border-color: var(--a); background: var(--a-soft); transform: scale(1.06); }
.draw-name.side-b.hi { border-color: var(--b); background: var(--b-soft); transform: scale(1.06); }
.draw-msg { font-size: 1.1rem; min-height: 1.6em; }
.trophy { font-size: 56px; }
.big-count { font-size: 2.6rem; font-weight: 800; margin: 4px 0 !important; }
.set-list { margin: 8px 0; }

.toast {
  position: fixed; left: 50%; top: 18%; transform: translateX(-50%); z-index: 30;
  background: rgba(0,0,0,.85); color: #fff; padding: 14px 24px; border-radius: 999px;
  font-size: 1.2rem; font-weight: 700; transition: opacity .4s;
}
.toast.out { opacity: 0; }

/* ----- 審判画面 ----- */
.score-wrap {
  height: 100dvh; display: grid; grid-template-rows: auto 1fr auto; color: #fff;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  user-select: none; -webkit-user-select: none;
}
.score-top { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; padding: 8px 10px; gap: 8px; }
.score-info { text-align: center; font-size: .9rem; opacity: .9; }
.set-chips { display: flex; gap: 6px; justify-content: center; margin-top: 2px; }
.set-chips span { padding: 1px 8px; border-radius: 6px; font-weight: 700; font-size: .8rem; }
.set-chips .side-a { background: var(--a); }
.set-chips .side-b { background: var(--b); }

.panels { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; padding: 0 8px; min-height: 0; }
.panel {
  border: 0; border-radius: 18px; color: #fff; min-height: 0; overflow: hidden;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  padding: 10px; position: relative;
  transition: filter .08s, transform .08s;
}
.panel:active { filter: brightness(1.25); transform: scale(.985); }
.panel.side-a { background: linear-gradient(160deg, #b8323a, #7d1d24); }
.panel.side-b { background: linear-gradient(160deg, #3355c4, #1d3282); }
.p-name { font-size: clamp(1.2rem, 5vmin, 2.2rem); font-weight: 700; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.p-sets { font-size: 1.1rem; letter-spacing: 4px; opacity: .9; }
.p-score { font-size: min(24vw, 46vh); font-weight: 800; line-height: 1; font-variant-numeric: tabular-nums; }
.p-serve { min-height: 2em; }
.serve { display: inline-block; background: #fff; color: #222; border-radius: 999px; padding: 3px 14px; font-weight: 700; font-size: clamp(.9rem, 3vmin, 1.3rem); }
.p-flag { min-height: 1.4em; font-weight: 700; color: #ffe27a; font-size: clamp(.85rem, 3vmin, 1.2rem); }

.score-bottom { display: flex; gap: 8px; justify-content: center; padding: 10px; }
.score-bottom .btn { flex: 1; max-width: 220px; padding: 12px; }

/* 縦向きのスマホは上下に並べる */
@media (orientation: portrait) {
  .panels { grid-template-columns: 1fr; grid-template-rows: 1fr 1fr; }
  .p-score { font-size: min(34vw, 20vh); }
}
