:root {
  --bg: #0f1720;
  --surface: #1b2733;
  --surface-2: #223140;
  --border: #31465a;
  --text: #e7eef5;
  --muted: #93a4b5;
  --accent: #1f9d63;
  --accent-hover: #24b473;
  --danger: #e2565a;
  --radius: 14px;
  --nav-h: 62px;
}

/* ライトテーマ(端末/ユーザー選択で切替)。既定は上のダーク。 */
:root[data-theme="light"] {
  --bg: #f3f5f8;
  --surface: #ffffff;
  --surface-2: #eceff4;
  --border: #d5dce4;
  --text: #1a2430;
  --muted: #5c6b7a;
  --accent: #178a55;
  --accent-hover: #1f9d63;
  --danger: #d23c40;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: -apple-system, "Hiragino Sans", "Noto Sans JP", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

/* ---- トップバー ---- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.65rem 1rem;
  padding-top: calc(0.65rem + env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand { font-weight: 700; font-size: 1.05rem; }
.topbar-right { display: flex; align-items: center; gap: 0.75rem; }

.user-box { display: flex; align-items: center; gap: 0.6rem; font-size: 0.85rem; }
.icon-btn { background: none; border: none; font-size: 1.15rem; line-height: 1; padding: 0.25rem 0.35rem; min-height: auto; cursor: pointer; }
.icon-btn:hover { border: none; }
.topbar-link { color: var(--muted); font-size: 0.78rem; text-decoration: none; padding: 0.3rem 0.4rem; white-space: nowrap; }
.topbar-link:hover { color: var(--accent); }

/* ---- レイアウト ---- */
main {
  max-width: 640px;
  margin: 0 auto;
  padding: 1rem 0.9rem;
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 1.5rem);
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem 1.1rem;
  margin-bottom: 1rem;
}
h2 { margin: 0 0 0.6rem; font-size: 1.2rem; }
h3 { margin: 0 0 0.85rem; font-size: 1.02rem; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 0.4rem; }
.card-head h3 { margin: 0; }

/* ---- フォーム ---- */
label { display: flex; flex-direction: column; gap: 0.3rem; font-size: 0.82rem; color: var(--muted); }
input, select {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.65rem 0.7rem;
  color: var(--text);
  font-size: 16px; /* iOS の自動ズーム防止 */
  min-height: 44px;
  width: 100%;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.stack, .auth-form { display: flex; flex-direction: column; gap: 0.85rem; }
.rule-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.75rem; }
.col-2 { grid-column: 1 / -1; }

button {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.7rem 1rem;
  font-size: 0.95rem;
  min-height: 44px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
button:active { transform: scale(0.99); }
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button.primary:hover { background: var(--accent-hover); }
button.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
button.danger:hover { background: rgba(226, 86, 90, 0.12); border-color: var(--danger); }
button.subtle { background: transparent; color: var(--muted); }
button.subtle:hover { border-color: var(--muted); color: var(--text); }
.room-actions { display: flex; gap: 0.6rem; margin-top: 1rem; flex-wrap: wrap; }
.room-actions button { flex: 1 1 auto; }
.link-btn { background: none; border: none; color: var(--accent); padding: 0.3rem 0; min-height: auto; }
.link-btn:hover { text-decoration: underline; border: none; }

.tabs { display: flex; gap: 0.5rem; margin-bottom: 1.1rem; }
.tab { flex: 1; }
.tab.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.auth-card { max-width: 420px; margin: 2rem auto 0; }
.create-summary { cursor: pointer; font-weight: 600; color: var(--accent); }

/* ---- サマリー(ヒーロー) ---- */
.hero { text-align: center; }
.hero-label { color: var(--muted); font-size: 0.85rem; }
.hero-label small { font-size: 0.75rem; }
.hero-value { font-size: 2.4rem; font-weight: 800; letter-spacing: -0.02em; margin: 0.2rem 0; }
.hero-value.small { font-size: 1.7rem; }
.hero-sub { color: var(--muted); font-size: 0.85rem; }
.hero-today { margin-top: 0.6rem; padding-top: 0.6rem; border-top: 1px solid var(--border); color: var(--muted); font-size: 0.9rem; }
.hero-today span { font-weight: 700; font-size: 1.05rem; }

.lb-filters { display: flex; gap: 0.75rem; flex-wrap: wrap; }
.lb-filters label { flex: 1 1 140px; }

.summary-rooms { list-style: none; padding: 0; margin: 1rem 0 0; display: flex; flex-direction: column; gap: 0.55rem; }
.summary-room {
  display: flex; align-items: center; gap: 0.7rem;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 10px; padding: 0.6rem 0.8rem;
}
.summary-room input[type="checkbox"] { width: 22px; height: 22px; min-height: 22px; accent-color: var(--accent); flex: 0 0 auto; }
.sr-main { flex: 1; cursor: pointer; min-width: 0; }
.sr-name { font-weight: 600; margin-bottom: 0.15rem; }
.sr-vals { display: flex; gap: 1rem; font-size: 0.82rem; color: var(--muted); flex-wrap: wrap; }
.sr-vals b { font-weight: 700; }

/* ---- 部屋一覧 ---- */
.room-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 0.5rem; }
.room-list li {
  display: flex; justify-content: space-between; align-items: center; gap: 0.6rem;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 10px; padding: 0.7rem 0.9rem;
}
.room-list .room-name { font-weight: 600; }
.room-list small { color: var(--muted); }
.room-list button { flex: 0 0 auto; }

/* ---- 部屋詳細 ---- */
.room-header h2 { margin-top: 0.4rem; }
.room-id-row { font-size: 0.85rem; color: var(--muted); margin: 0.3rem 0; display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }
.share-box { background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 0.8rem 0.9rem; margin: 0.6rem 0; }
.share-url { font-size: 0.78rem; color: var(--muted); word-break: break-all; margin: 0.35rem 0 0.7rem; }
.share-actions { display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: center; }
.share-actions button { flex: 0 0 auto; }
.share-actions .primary { min-width: 96px; }
code { background: var(--surface-2); padding: 0.2rem 0.5rem; border-radius: 6px; word-break: break-all; }
.rules-summary { font-size: 0.8rem; color: var(--muted); margin: 0.5rem 0; }
.rules-summary span { display: inline-block; margin-right: 0.8rem; white-space: nowrap; }
.members { display: flex; flex-wrap: wrap; gap: 0.4rem; margin-top: 0.6rem; align-items: center; }
.members-head { width: 100%; color: var(--muted); font-size: 0.78rem; }
.member { background: var(--surface-2); border-radius: 8px; padding: 0.3rem 0.7rem; font-size: 0.85rem; }

.score-form { display: grid; grid-template-columns: 1fr 1fr; gap: 0.75rem; }

/* チップの ± ステッパー */
.stepper { display: flex; align-items: stretch; }
.stepper .step {
  flex: 0 0 46px;
  min-width: 46px;
  padding: 0;
  font-size: 1.35rem;
  font-weight: 700;
  border-radius: 0;
  color: var(--accent);
}
.stepper .step:first-child { border-radius: 10px 0 0 10px; }
.stepper .step:last-child { border-radius: 0 10px 10px 0; }
.stepper input {
  border-radius: 0;
  border-left: none;
  border-right: none;
  text-align: center;
  min-width: 0;
}

/* 点棒の符号反転（±） */
.sign-input { display: flex; align-items: stretch; }
.sign-input input {
  border-radius: 10px 0 0 10px;
  border-right: none;
  min-width: 0;
  flex: 1;
}
.sign-input .sign-toggle {
  flex: 0 0 46px;
  min-width: 46px;
  padding: 0;
  font-size: 1.2rem;
  font-weight: 700;
  border-radius: 0 10px 10px 0;
  color: var(--accent);
}
.hint { color: var(--muted); font-size: 0.8rem; margin: 0.8rem 0 0; }

/* ---- テーブル ---- */
table { border-collapse: collapse; width: 100%; font-size: 0.88rem; }
th, td { border: 1px solid var(--border); padding: 0.45rem 0.55rem; text-align: right; white-space: nowrap; }
th:first-child, td:first-child { text-align: left; position: sticky; left: 0; background: var(--surface); }
thead th { background: var(--surface-2); }
thead th:first-child { background: var(--surface-2); }
.scroll-x { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.pos { color: var(--accent-hover); }
.neg { color: var(--danger); }
.rank-1 { font-weight: 700; }
.badge-invalid { color: var(--danger); font-size: 0.72rem; }
.badge-ok { color: var(--accent); font-size: 0.72rem; }
.chip-mini { color: var(--muted); font-size: 0.68rem; }
.chip-mini.pos { color: var(--accent-hover); }
.chip-mini.neg { color: var(--danger); }
/* 順位表の総計行(突合用) */
tfoot .lb-total td { background: var(--surface-2); font-weight: 700; border-top: 2px solid var(--border); }
tfoot .lb-total td:first-child { background: var(--surface-2); }
.lb-warn { color: var(--danger); font-size: 0.8rem; margin: 0.6rem 0 0; line-height: 1.5; }
.badge-left { color: var(--muted); font-size: 0.68rem; border: 1px solid var(--border); border-radius: 999px; padding: 0.05rem 0.45rem; vertical-align: middle; }
.left-note { margin-top: 0.9rem; padding: 0.6rem 0.8rem; background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; color: var(--muted); font-size: 0.85rem; }
.row-del {
  padding: 0.25rem 0.5rem;
  min-height: 34px;
  font-size: 0.95rem;
  background: transparent;
  border-color: var(--border);
}
.row-del:hover { border-color: var(--danger); }

/* ---- 個人成績(スタッツ) ---- */
.stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.55rem; }
.stat {
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 10px; padding: 0.55rem 0.4rem; text-align: center; min-width: 0;
}
.stat-label { color: var(--muted); font-size: 0.7rem; white-space: nowrap; }
.stat-val { font-size: 1.2rem; font-weight: 700; margin-top: 0.15rem; letter-spacing: -0.01em; }
.stat-val small { font-size: 0.62rem; font-weight: 500; color: var(--muted); margin-left: 1px; }

.rankbars { display: flex; flex-direction: column; gap: 0.5rem; }
.rankbar-row { display: grid; grid-template-columns: 2.4rem 1fr 4.4rem; align-items: center; gap: 0.55rem; font-size: 0.85rem; }
.rankbar-track { background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px; height: 16px; overflow: hidden; }
.rankbar-fill { display: block; height: 100%; background: var(--accent); border-radius: 999px; min-width: 2px; transition: width 0.3s; }
.rankbar-fill.rf-top { background: var(--accent-hover); }
.rankbar-fill.rf-last { background: var(--danger); }
.rankbar-num { text-align: right; color: var(--muted); }
.rankbar-num small { font-size: 0.72rem; }

.chart-wrap { width: 100%; }
.chart-grid { display: flex; align-items: stretch; gap: 6px; }
.chart-yaxis {
  display: flex; flex-direction: column; justify-content: space-between; align-items: flex-end;
  min-width: 2.8rem; font-size: 0.66rem; color: var(--muted); padding: 1px 0;
}
.chart-yunit { opacity: 0.7; }
.chart-plot { flex: 1; min-width: 0; }
.chart-plot svg { width: 100%; height: auto; display: block; }
.chart-xaxis { display: flex; justify-content: space-between; font-size: 0.66rem; color: var(--muted); margin-top: 0.15rem; }
.chart-xmid { flex: 1; text-align: center; }
.chart-axtext { fill: var(--muted); font-size: 9px; }
.chart-line { stroke: var(--accent); fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart-area { fill: var(--accent); opacity: 0.12; stroke: none; }
.chart-zero { stroke: var(--border); stroke-width: 1; stroke-dasharray: 3 3; }
.chart-dot { fill: var(--accent); }
.chart-dot.cd-neg { fill: var(--danger); }
.chart-cap { display: flex; justify-content: space-between; color: var(--muted); font-size: 0.78rem; margin-top: 0.4rem; }
.chart-cap .pos { color: var(--accent-hover); }
.chart-cap .neg { color: var(--danger); }

/* ---- つかいかた ---- */
.guide-intro h2 { margin-bottom: 0.4rem; }
.guide-intro p { color: var(--muted); font-size: 0.9rem; margin: 0; }
.guide-steps { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 1.1rem; }
.guide-steps li { display: flex; gap: 0.85rem; position: relative; }
/* ステップ番号をつなぐ縦線(最後の項目以外) */
.guide-steps li:not(:last-child)::before {
  content: ""; position: absolute; left: 15px; margin-left: -1px;
  top: 32px; bottom: -1.1rem; width: 2px; background: var(--border);
}
.gs-num {
  flex: 0 0 30px; width: 30px; height: 30px;
  display: flex; align-items: center; justify-content: center;
  background: var(--accent); color: #fff; font-weight: 700;
  border-radius: 50%; font-size: 0.95rem; position: relative; z-index: 1;
}
.gs-body { flex: 1; min-width: 0; }
.gs-body h3 { margin: 0.25rem 0 0.3rem; font-size: 0.98rem; }
.gs-body p { margin: 0; color: var(--muted); font-size: 0.86rem; }
.gs-body b { color: var(--text); font-weight: 600; }
.guide-tips { margin: 0; padding-left: 1.1rem; color: var(--muted); font-size: 0.86rem; }
.guide-tips li { margin: 0.35rem 0; }
.guide-tips b { color: var(--text); font-weight: 600; }
.guide-links { text-align: center; }
.guide-link { color: var(--accent); text-decoration: none; font-weight: 600; font-size: 0.92rem; }
.guide-link:hover { text-decoration: underline; }

/* ---- 下部ナビ ---- */
.bottomnav {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  z-index: 20;
}
.navbtn {
  flex: 1;
  min-width: 0;
  background: none;
  border: none;
  border-radius: 0;
  padding: 0.4rem 0.2rem;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.15rem;
  font-size: 0.68rem;
  white-space: nowrap;
  min-height: var(--nav-h);
}
.navbtn span { font-size: 1.3rem; line-height: 1; }
.navbtn:hover { border: none; }
.navbtn.active { color: var(--accent); font-weight: 700; }

/* ---- トースト ---- */
.toast {
  position: fixed;
  bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 1rem);
  left: 50%; transform: translateX(-50%);
  background: var(--surface-2); border: 1px solid var(--border);
  padding: 0.7rem 1.2rem; border-radius: 10px; font-size: 0.9rem;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45); z-index: 100;
  max-width: 90vw; text-align: center;
}
.toast.error { border-color: var(--danger); color: #ffd7d8; }
.toast.success { border-color: var(--accent); }

/* ---- デスクトップ ---- */
@media (min-width: 720px) {
  .bottomnav { max-width: 640px; margin: 0 auto; border-radius: 14px 14px 0 0; border-left: 1px solid var(--border); border-right: 1px solid var(--border); }
}
