:root {
  --bg: #0f1115;
  --surface: #171a21;
  --surface-2: #1f232c;
  --surface-3: #2a2f3a;
  --border: #2c313c;
  --text: #eef1f6;
  --text-dim: #9aa3b2;
  --accent: #ffb020;
  --accent-press: #d9930f;
  --plus: #2e7d5b;
  --plus-press: #256b4d;
  --minus: #3a3f4b;
  --danger: #d9534f;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Kaku Gothic ProN",
    "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", Meiryo, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

button { font-family: inherit; cursor: pointer; border: none; }

.app {
  max-width: 560px;
  margin: 0 auto;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: calc(10px + var(--safe-top)) 12px calc(12px + var(--safe-bottom));
  gap: 12px;
}

/* ---------- Header ---------- */
.app-header { display: flex; flex-direction: column; gap: 8px; }
.title-row { display: flex; align-items: center; justify-content: space-between; }
.app-title { font-size: 1.15rem; font-weight: 700; margin: 0; letter-spacing: 0.02em; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 10px;
  background: var(--surface-2); color: var(--text);
  font-size: 1.3rem; line-height: 1; display: grid; place-items: center;
}
.icon-btn:active { background: var(--surface-3); }
.session-name {
  width: 100%; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px; font-size: 0.95rem;
}
.session-name::placeholder { color: var(--text-dim); }

/* ---------- Tabs (ページ) ---------- */
.tabs { display: flex; align-items: center; gap: 8px; }
.tab-list {
  display: flex; gap: 6px; overflow-x: auto; flex: 1;
  scrollbar-width: none; -ms-overflow-style: none;
  padding-bottom: 2px;
}
.tab-list::-webkit-scrollbar { display: none; }
.tab {
  flex: 0 0 auto; max-width: 160px;
  padding: 9px 14px; border-radius: 999px;
  background: var(--surface-2); color: var(--text-dim);
  border: 1px solid var(--border); font-size: 0.9rem; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tab.active { background: var(--accent); color: #10131a; border-color: var(--accent); }
.tab:active { filter: brightness(0.92); }
.tab-add {
  flex: 0 0 auto; width: 40px; height: 38px; border-radius: 10px;
  background: var(--surface-2); color: var(--text); border: 1px solid var(--border);
  font-size: 1.2rem; line-height: 1; display: grid; place-items: center;
}
.tab-add:active { background: var(--surface-3); }

/* ---------- Games card ---------- */
.games-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px;
  display: flex; flex-direction: column; align-items: center; gap: 10px;
}
.games-label { color: var(--text-dim); font-size: 0.85rem; }
.games-input {
  width: 100%; text-align: center;
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 12px;
  padding: 12px; font-size: 2.6rem; font-weight: 800; line-height: 1.1;
  font-variant-numeric: tabular-nums; letter-spacing: 0.02em;
  -moz-appearance: textfield;
}
.games-input::-webkit-outer-spin-button,
.games-input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.games-input::placeholder { color: var(--text-dim); opacity: 0.5; }
.games-input:focus { outline: none; border-color: var(--accent); }

/* 打ち出しゲーム数（控えめなサブ入力） */
.start-games {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  color: var(--text-dim); font-size: 0.85rem;
}
.start-games-label { white-space: nowrap; }
.start-games-input {
  width: 6.5em; text-align: right;
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 6px 8px; font-size: 0.95rem; font-weight: 700;
  font-variant-numeric: tabular-nums;
  -moz-appearance: textfield;
}
.start-games-input::-webkit-outer-spin-button,
.start-games-input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.start-games-input::placeholder { color: var(--text-dim); opacity: 0.5; }
.start-games-input:focus { outline: none; border-color: var(--accent); }
.start-games-unit { color: var(--text-dim); }

.user-games {
  font-size: 0.9rem; color: var(--text); text-align: center;
  font-variant-numeric: tabular-nums;
}
.user-games b { color: var(--accent); font-weight: 800; font-size: 1.05rem; }
.user-games-note { color: var(--text-dim); font-size: 0.78rem; }

/* ---------- Counter list ---------- */
.list-head {
  display: flex; justify-content: space-between;
  align-items: center; gap: 10px;
  padding: 0 6px; color: var(--text-dim); font-size: 0.75rem;
}
.list-head-count { text-align: right; }
.counter-list { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }

.counter-item {
  display: grid; grid-template-columns: 1fr auto auto auto;
  align-items: center; gap: 8px;
  background: var(--surface); border: 1px solid var(--border);
  border-left: 5px solid var(--dot, var(--accent));
  border-radius: var(--radius); padding: 10px 10px 10px 12px;
}
/* カウント時のフィードバック（発光＋数字ポップ） */
.counter-item.flash { animation: ciFlash .45s ease-out; }
@keyframes ciFlash {
  0%   { box-shadow: inset 0 0 0 2px var(--dot, var(--accent)), 0 0 18px -2px var(--dot, var(--accent)); }
  60%  { box-shadow: inset 0 0 0 2px var(--dot, var(--accent)), 0 0 14px -3px var(--dot, var(--accent)); }
  100% { box-shadow: inset 0 0 0 0 transparent, 0 0 0 0 transparent; }
}
.counter-item.flash .ci-name { color: var(--dot, var(--accent)); text-shadow: 0 0 10px var(--dot, var(--accent)); }
.counter-item.flash .ci-count { animation: ciCountPop .3s ease-out; }
@keyframes ciCountPop {
  0%   { transform: scale(1); }
  35%  { transform: scale(1.28); color: var(--dot, var(--accent)); }
  100% { transform: scale(1); }
}

.ci-main { min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.ci-name {
  font-size: 1.05rem; font-weight: 700; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
}
.ci-prob { color: var(--text-dim); font-size: 0.8rem; font-variant-numeric: tabular-nums; }
.ci-prob b { color: var(--text); font-weight: 700; }

.ci-count {
  min-width: 46px; text-align: right; font-size: 1.5rem; font-weight: 800;
  font-variant-numeric: tabular-nums;
  background: none; color: inherit; padding: 4px 6px; border-radius: 8px;
  transition: background .1s ease;
}
.ci-count:active { background: var(--surface-3); }
.ci-minus {
  width: 46px; height: 58px; border-radius: 12px;
  background: var(--surface-3); color: var(--text);
  font-size: 1.5rem; font-weight: 800; line-height: 1;
  display: grid; place-items: center;
  transition: transform .04s ease, filter .04s ease;
}
.ci-minus:disabled { opacity: 0.32; }
.ci-minus:active:not(:disabled) { transform: scale(0.93); filter: brightness(1.25); }
.ci-plus {
  width: 58px; height: 58px; border-radius: 14px;
  background: var(--dot, var(--accent)); color: #10131a;
  font-size: 1.6rem; font-weight: 800; display: grid; place-items: center;
  transition: transform .04s ease, filter .04s ease;
}
.ci-plus:active { transform: scale(0.93); filter: brightness(0.9); }

/* 並び替えモードの移動ボタン */
.ci-move {
  width: 46px; height: 58px; border-radius: 12px;
  background: var(--surface-3); color: var(--text);
  font-size: 1.1rem; font-weight: 800; line-height: 1;
  display: grid; place-items: center;
  transition: transform .04s ease, filter .04s ease;
}
.ci-move:disabled { opacity: 0.3; }
.ci-move:active:not(:disabled) { transform: scale(0.93); filter: brightness(1.25); }

/* 並び替えバー */
.reorder-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  background: var(--surface-2); border: 1px solid var(--accent);
  border-radius: 12px; padding: 10px 10px 10px 14px; margin-bottom: 10px;
}
.reorder-bar[hidden] { display: none; }
.reorder-bar-text { color: var(--text); font-size: 0.9rem; font-weight: 600; }
.reorder-done {
  padding: 8px 18px; border-radius: 10px; font-size: 0.9rem; font-weight: 700;
  background: var(--accent); color: #10131a;
}
.reorder-done:active { background: var(--accent-press); }

.ci-edit {
  position: absolute; opacity: 0; pointer-events: none;
}
/* long-press / edit handled via dedicated button */
.ci-name-btn {
  background: none; padding: 0; text-align: left; color: inherit;
  display: flex; flex-direction: column; gap: 3px; width: 100%;
}
.ci-name-btn:active .ci-name { color: var(--accent); }

.empty-note {
  text-align: center; color: var(--text-dim); font-size: 0.9rem;
  padding: 24px 8px; line-height: 1.6;
}

.add-counter-btn {
  margin-top: 10px; width: 100%; padding: 14px;
  background: var(--surface-2); color: var(--text);
  border: 1px dashed var(--border); border-radius: var(--radius);
  font-size: 1rem; font-weight: 600;
}
.add-counter-btn:active { background: var(--surface-3); }

/* ---------- Footer ---------- */
.app-footer { margin-top: auto; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; padding-top: 6px; }
.foot-btn {
  padding: 13px; border-radius: 12px; font-size: 0.95rem; font-weight: 600;
  background: var(--surface-2); color: var(--text); border: 1px solid var(--border);
}
.foot-btn:disabled { opacity: 0.4; }
.foot-btn:active:not(:disabled) { background: var(--surface-3); }

.app-legal { text-align: center; padding: 12px 0 2px; }
.app-legal a { color: var(--text-dim); font-size: 0.78rem; text-decoration: none; }
.app-legal a:active { color: var(--accent); }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(76px + var(--safe-bottom));
  transform: translateX(-50%) translateY(20px);
  background: var(--surface-3); color: var(--text);
  padding: 10px 18px; border-radius: 999px; font-size: 0.9rem;
  border: 1px solid var(--border); box-shadow: 0 6px 20px rgba(0,0,0,.4);
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 50;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- Dialog ---------- */
.dialog {
  border: 1px solid var(--border); background: var(--surface);
  color: var(--text); border-radius: 16px; padding: 0;
  width: min(92vw, 420px); box-shadow: 0 20px 60px rgba(0,0,0,.5);
}
.dialog::backdrop { background: rgba(0,0,0,.6); }
.dialog-form { padding: 18px; display: flex; flex-direction: column; gap: 14px; }
.dialog-title { margin: 0; font-size: 1.1rem; font-weight: 700; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field-label { font-size: 0.8rem; color: var(--text-dim); }
.field-input {
  background: var(--surface-2); color: var(--text); border: 1px solid var(--border);
  border-radius: 10px; padding: 12px; font-size: 1rem;
}
.color-picker { display: flex; flex-wrap: wrap; gap: 10px; }
.color-dot {
  width: 34px; height: 34px; border-radius: 50%;
  border: 3px solid transparent; transition: transform .05s;
}
.color-dot[aria-pressed="true"] { border-color: var(--text); transform: scale(1.1); }

.dialog-actions { display: flex; align-items: center; gap: 8px; margin-top: 4px; }
.spacer { flex: 1; }
.dlg-btn { padding: 11px 16px; border-radius: 10px; font-size: 0.95rem; font-weight: 600; }
.dlg-primary { background: var(--accent); color: #10131a; }
.dlg-primary:active { background: var(--accent-press); }
.dlg-ghost { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.dlg-danger { background: transparent; color: var(--danger); border: 1px solid var(--danger); }

.menu-item {
  width: 100%; text-align: left; padding: 14px; border-radius: 10px;
  background: var(--surface-2); color: var(--text); border: 1px solid var(--border);
  font-size: 0.95rem;
}
.menu-item:active { background: var(--surface-3); }
.menu-item:disabled { opacity: 0.4; }
.menu-danger { color: var(--danger); border-color: var(--danger); }
.menu-note { color: var(--text-dim); font-size: 0.78rem; margin: 4px 2px 0; line-height: 1.5; }

.confirm-message { margin: 0; font-size: 1rem; line-height: 1.6; }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
  .counter-item.flash .ci-count { animation: none; }
}
