:root {
  --bg: #0b1020;
  --panel: #121933;
  --panel-2: #182142;
  --text: #edf2ff;
  --muted: #a9b4d0;
  --accent: #6ea8fe;
  --accent-2: #8ef0c7;
  --danger: #ff8c8c;
  --warning: #ffd36e;
  --border: rgba(255,255,255,0.08);
  --shadow: 0 10px 30px rgba(0,0,0,0.28);
  --radius: 18px;
  --coin-size: 38px;
  --touch-min: 40px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { touch-action: manipulation; }
body {
  margin: 0;
  font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: radial-gradient(circle at top, #16204a 0%, var(--bg) 52%);
  color: var(--text);
  line-height: 1.5;
  min-height: 100dvh;
  padding-bottom: env(safe-area-inset-bottom, 0px);
  overflow-x: hidden;
}

.top-bar {
  position: sticky;
  top: 0;
  z-index: 100;
  background: rgba(11,16,32,0.85);
  backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border);
  padding: 8px 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.top-bar h1 { font-size: 15px; margin: 0; letter-spacing: -0.02em; }
.score-pill { display: flex; gap: 6px; font-size: 11px; color: var(--muted); flex-wrap: wrap; }
.score-pill span { font-weight: 700; }
.score-pill .wins { color: var(--accent-2); }
.score-pill .losses { color: var(--danger); }
.score-pill .online { color: var(--warning); }

.wrap { width: 100%; max-width: 430px; margin: 0 auto; padding: 10px; }

.turn-bar {
  text-align: center;
  padding: 10px 12px;
  border-radius: 14px;
  margin-bottom: 10px;
  font-weight: 700;
  font-size: 14px;
  transition: all 0.25s ease;
}

.turn-bar.human {
  background: linear-gradient(135deg, rgba(110,168,254,0.15), rgba(110,168,254,0.05));
  border: 1px solid rgba(110,168,254,0.25);
  color: #cfe0ff;
}

.turn-bar.computer {
  background: linear-gradient(135deg, rgba(255,140,140,0.15), rgba(255,140,140,0.05));
  border: 1px solid rgba(255,140,140,0.25);
  color: #ffc4c4;
}

.turn-bar .sub { font-weight: 400; font-size: 11px; color: var(--muted); display: block; margin-top: 2px; }

.mode-card, .history, .pile-card, .lobby-card, .player-card {
  background: linear-gradient(180deg, rgba(255,255,255,0.04), rgba(255,255,255,0.015));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.mode-card, .lobby-card, .player-card { padding: 10px; margin-bottom: 10px; }
.mode-card.hidden-online,
.status-msg.hidden-online,
.lobby-card.hidden-online,
.player-card.hidden-online,
.explain-toggle.hidden-online,
.explain-body.hidden-online {
  display: none !important;
}
.mode-title { font-size: 11px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; margin-bottom: 8px; }
.mode-actions, .player-list { display: flex; gap: 8px; flex-wrap: wrap; }

.mode-actions button, .action-bar button, .modal button {
  min-height: var(--touch-min);
  border-radius: 14px;
  border: 0;
  font-weight: 700;
  font-size: 13px;
  cursor: pointer;
  transition: transform 0.12s ease, opacity 0.12s ease;
}

.mode-actions button:active, .action-bar button:active, .modal button:active { transform: scale(0.96); }
.mode-actions button { flex: 1; min-width: 88px; padding: 10px 10px; }

.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 10px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 10px; color: var(--muted); font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em; }
.field input {
  width: 100%;
  min-height: 40px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: rgba(255,255,255,0.06);
  color: var(--text);
  padding: 0 10px;
  outline: none;
  font-size: 14px;
}

.field input::placeholder { color: #7d88a9; }
.lobby-meta { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.lobby-compact {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.lobby-compact__info,
.lobby-compact__actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.lobby-compact__actions button {
  min-height: 40px;
  padding: 8px 12px;
}
.room-bar-list,
#roomBarList {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.meta-pill, .player-pill {
  padding: 6px 8px;
  border-radius: 999px;
  background: rgba(255,255,255,0.06);
  border: 1px solid rgba(255,255,255,0.05);
  color: var(--muted);
  font-size: 11px;
}

.player-pill strong { color: var(--text); }
.player-pill.online { border-color: rgba(142,240,199,0.2); color: #d8fff2; }
.player-pill.offline { border-color: rgba(255,140,140,0.2); color: #ffd3d3; }

.pile-card {
  padding: 10px;
  margin-bottom: 10px;
  transition: border-color 0.2s, box-shadow 0.2s, opacity 0.2s;
}

.pile-card.selected { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(110,168,254,0.25); }
.pile-card.disabled { opacity: 0.5; pointer-events: none; }
.pile-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.pile-label { font-weight: 700; font-size: 14px; }
.pile-badge { font-size: 10px; padding: 4px 8px; border-radius: 999px; background: rgba(255,255,255,0.06); color: var(--muted); font-weight: 600; }
.coin-row { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; min-height: var(--coin-size); padding: 4px 0; }

.coin {
  width: var(--coin-size);
  height: var(--coin-size);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: linear-gradient(180deg, #ffe89c, #d5a941);
  color: #523800;
  font-size: 18px;
  box-shadow: inset 0 2px 0 rgba(255,255,255,0.45), 0 4px 12px rgba(0,0,0,0.25);
  cursor: pointer;
  transition: transform 0.15s ease, opacity 0.25s ease, box-shadow 0.15s ease;
  user-select: none;
  position: relative;
}

.coin:active { transform: scale(0.92); }
.coin.marked { background: linear-gradient(180deg, #ff9a9a, #e05555); color: #fff; box-shadow: inset 0 2px 0 rgba(255,255,255,0.2), 0 4px 12px rgba(200,50,50,0.3); }
.coin.marked::after { content: '✕'; position: absolute; font-size: 20px; font-weight: 900; }
.coin.removing { animation: coinRemove 0.35s ease forwards; pointer-events: none; }

@keyframes coinRemove {
  0% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.18) rotate(10deg); opacity: 0.6; }
  100% { transform: scale(0) rotate(24deg); opacity: 0; }
}

.selection-info { text-align: center; padding: 6px; font-size: 12px; color: var(--muted); min-height: 24px; }
.selection-info strong { color: var(--accent); }
.status-msg { text-align: center; padding: 9px 10px; border-radius: 12px; margin-bottom: 10px; font-size: 12px; transition: all 0.25s ease; }
.status-msg.info { background: rgba(110,168,254,0.08); border: 1px solid rgba(110,168,254,0.18); color: #cfe0ff; }
.status-msg.success { background: rgba(142,240,199,0.08); border: 1px solid rgba(142,240,199,0.18); color: #dbfff1; }
.status-msg.error { background: rgba(255,140,140,0.08); border: 1px solid rgba(255,140,140,0.18); color: #ffc4c4; }
.status-msg.warning { background: rgba(255,211,110,0.08); border: 1px solid rgba(255,211,110,0.18); color: #fff4cf; }

.history { margin-top: 14px; overflow: hidden; }
.history-title { padding: 8px 10px; font-size: 11px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; border-bottom: 1px solid var(--border); }
.history-list { max-height: 180px; overflow-y: auto; }
.history-item { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-bottom: 1px solid rgba(255,255,255,0.04); font-size: 12px; }
.history-item .num { width: 24px; height: 24px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 11px; font-weight: 700; flex-shrink: 0; }
.history-item .num.h, .history-item .num.p1, .history-item .num.o1 { background: rgba(110,168,254,0.2); color: var(--accent); }
.history-item .num.c, .history-item .num.o2 { background: rgba(255,140,140,0.2); color: var(--danger); }
.history-item .num.p2 { background: rgba(142,240,199,0.2); color: var(--accent-2); }
.history-item .detail { flex: 1; color: var(--muted); }
.history-item .detail strong { color: var(--text); }

.overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(0,0,0,0.7);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

.overlay.show { opacity: 1; pointer-events: auto; }
.modal { background: var(--panel); border: 1px solid var(--border); border-radius: 24px; padding: 32px 24px; text-align: center; max-width: 360px; width: 100%; box-shadow: var(--shadow); transform: scale(0.9); transition: transform 0.3s ease; }
.overlay.show .modal { transform: scale(1); }
.modal-icon { font-size: 56px; margin-bottom: 12px; }
.modal h2 { margin: 0 0 8px; font-size: 24px; }
.modal p { color: var(--muted); margin: 0 0 20px; }
.modal button { padding: 14px 32px; background: var(--accent); color: #071226; }

.explain-toggle { display: flex; align-items: center; justify-content: space-between; width: 100%; appearance: none; border: 0; background: none; color: var(--accent); font-size: 15px; font-weight: 700; padding: 14px 0; cursor: pointer; }
.explain-toggle .arrow { transition: transform 0.3s ease; font-size: 12px; }
.explain-toggle.open .arrow { transform: rotate(180deg); }
.explain-body { max-height: 0; overflow: hidden; transition: max-height 0.4s ease; }
.explain-body.open { max-height: 2000px; }
.explain-content { padding: 0 0 16px; color: var(--muted); font-size: 14px; line-height: 1.7; }
.explain-content table { width: 100%; border-collapse: collapse; margin: 10px 0; font-size: 13px; }
.explain-content th, .explain-content td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--border); }
.explain-content th { color: #d9e4ff; font-weight: 600; }
.action-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 100;
  background: rgba(11,16,32,0.92);
  backdrop-filter: blur(16px);
  border-top: 1px solid var(--border);
  padding: 8px 10px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  display: flex;
  gap: 10px;
  justify-content: center;
}

.action-bar button { flex: 1; max-width: 140px; padding: 10px 8px; display: flex; align-items: center; justify-content: center; gap: 4px; font-size: 12px; }
.action-bar button:disabled { opacity: 0.35; cursor: not-allowed; transform: none; }
.btn-confirm { background: var(--accent); color: #071226; }
.btn-reset { background: rgba(255,255,255,0.06); color: var(--muted); border: 1px solid var(--border); }
.hidden { display: none !important; }
.bottom-spacer { height: 72px; }

.legal-footer {
  display: flex;
  justify-content: center;
  gap: 14px;
  padding: 0 16px 110px;
}

.legal-link {
  background: transparent;
  border: 0;
  color: var(--muted);
  text-decoration: underline;
  cursor: pointer;
}

.cookie-banner {
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: 86px;
  z-index: 160;
  border-radius: 18px;
  padding: 14px;
  background: rgba(18, 25, 51, 0.96);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}

.cookie-banner__text {
  color: var(--muted);
  font-size: 13px;
  margin-bottom: 10px;
}

.cookie-banner__actions {
  display: flex;
  gap: 10px;
}

.cookie-banner__actions button {
  flex: 1;
  min-height: 42px;
}

.legal-modal p {
  text-align: left;
  white-space: pre-line;
}

.legal-copy {
  text-align: left;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.6;
  max-height: min(62dvh, 520px);
  overflow-y: auto;
  margin: 0 0 20px;
  padding-right: 4px;
}

.legal-copy p {
  margin: 0 0 12px;
}

.legal-copy strong {
  color: var(--text);
}

.legal-copy ul {
  margin: 0 0 12px 18px;
  padding: 0;
}

.legal-copy li {
  margin: 0 0 8px;
}

.legal-copy a {
  color: var(--accent);
}

#confirmOverlay .mode-actions {
  margin-top: 16px;
}

@media (max-width: 680px) {
  .field-grid { grid-template-columns: 1fr; }
  .lobby-card.is-joined #onlineLobbyForm {
    display: none;
  }
  .lobby-card.is-joined .lobby-compact {
    display: flex !important;
  }
  .lobby-compact {
    flex-direction: column;
    align-items: stretch;
  }
  .lobby-compact__info,
  .lobby-compact__actions {
    width: 100%;
  }
  .lobby-compact__actions button {
    flex: 1;
  }
  .player-card {
    padding: 10px 12px;
  }
  .player-list {
    gap: 6px;
  }
  .player-pill {
    font-size: 11px;
    padding: 6px 8px;
  }
  .cookie-banner__actions {
    flex-direction: column;
  }
}
