:root {
  --bg: #f3f1ea;
  --board: #faf8f2;
  --grid: #e2ded2;
  --card: rgba(255, 255, 255, 0.9);
  --card-solid: #ffffff;
  --ink: #1c1c1e;
  --muted: #6b6b70;
  --line: rgba(0, 0, 0, 0.09);
  --x: #2463eb;
  --o: #e5483d;
  --good: #1f9d55;
  --last: rgba(245, 190, 30, 0.28);
  --win: rgba(31, 157, 85, 0.2);
  --win-line: #1f9d55;
  --shadow: 0 6px 24px rgba(20, 20, 30, 0.12), 0 1px 3px rgba(20, 20, 30, 0.08);
  --radius: 18px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121316;
    --board: #16171b;
    --grid: #272930;
    --card: rgba(34, 36, 42, 0.9);
    --card-solid: #22242a;
    --ink: #f2f2f5;
    --muted: #9b9ba3;
    --line: rgba(255, 255, 255, 0.1);
    --x: #6c9dff;
    --o: #ff7b70;
    --good: #3ecf7c;
    --last: rgba(245, 190, 30, 0.17);
    --win: rgba(62, 207, 124, 0.18);
    --win-line: #3ecf7c;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

body { overflow: hidden; }
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
.screen { position: fixed; inset: 0; }

.pc { width: 1em; height: 1em; fill: none; stroke-width: 3; stroke-linecap: round; flex: none; }
.pc-x { stroke: var(--x); }
.pc-o { stroke: var(--o); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 48px;
  padding: 0 18px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: transparent;
  font-weight: 600;
  font-size: 16px;
  transition: transform 0.1s, opacity 0.2s;
}
.btn.primary { background: var(--ink); color: var(--bg); border-color: transparent; }
.btn.big { min-height: 56px; font-size: 17px; margin-top: 6px; }
.btn.wide { width: 100%; margin-top: 4px; }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn:active:not(:disabled) { transform: scale(0.98); }

/* Home */

.home {
  overflow-y: auto;
  padding: calc(env(safe-area-inset-top) + 28px) 20px calc(env(safe-area-inset-bottom) + 28px);
}
.home-inner {
  max-width: 420px;
  min-height: 100%;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 14px;
}
.home-top { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.logo { display: flex; gap: 2px; font-size: 48px; margin-left: -6px; }

.lang-switch { display: inline-flex; flex: none; padding: 3px; border-radius: 10px; background: var(--line); }
.lang-switch button {
  min-width: 40px;
  padding: 6px 8px;
  border: 0;
  border-radius: 8px;
  background: none;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.04em;
}
.lang-switch button[aria-pressed="true"] { background: var(--card-solid); color: var(--ink); box-shadow: var(--shadow); }
h1 { margin: 0; font-size: 30px; letter-spacing: -0.02em; }
.lead { margin: 0; color: var(--muted); }
.label {
  margin-top: 10px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.pick { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.pick-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 18px 10px 16px;
  border-radius: var(--radius);
  border: 2px solid var(--line);
  background: var(--card-solid);
  box-shadow: var(--shadow);
  transition: border-color 0.15s, transform 0.1s;
}
.pick-btn:active { transform: scale(0.98); }
.pick-btn[aria-checked="true"] { border-color: var(--ink); }
.pick-icon { font-size: 56px; }
.pick-title { font-weight: 650; font-size: 17px; }
.pick-sub { font-size: 13px; color: var(--muted); }
.hint { margin: 0; font-size: 14px; color: var(--muted); }

.field {
  width: 100%;
  min-height: 48px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--card-solid);
  color: var(--ink);
  font: inherit;
  font-size: 16px;
  user-select: text;
  -webkit-user-select: text;
  touch-action: manipulation;
}
.field:focus { outline: 2px solid var(--x); outline-offset: -1px; }
.error { margin: 0; font-size: 14px; color: var(--o); }

.recent-list { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.recent-list a {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--card-solid);
  color: inherit;
  text-decoration: none;
}
.recent-list .pc { font-size: 20px; }
.recent-list .when { margin-left: auto; font-size: 14px; color: var(--muted); }

/* Game */

.game {
  background: var(--board);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
#board { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }

.topbar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: calc(env(safe-area-inset-top) + 8px) calc(env(safe-area-inset-right) + 8px) 0 calc(env(safe-area-inset-left) + 8px);
  pointer-events: none;
}
.bar {
  width: 100%;
  max-width: 480px;
  padding: 8px 8px 10px;
  border-radius: var(--radius);
  background: var(--card);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  box-shadow: var(--shadow);
  pointer-events: auto;
}
.players { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 6px; }
.player {
  --turn: transparent;
  display: flex;
  align-items: center;
  gap: 7px;
  min-width: 0;
  padding: 6px 10px;
  border-radius: 12px;
  font-weight: 600;
  font-size: 15px;
  transition: background 0.2s;
}
.player.right { flex-direction: row-reverse; }
.player .pc { font-size: 20px; }
.player .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.player.waiting .name { color: var(--muted); font-weight: 500; }
.player.turn-x { background: color-mix(in srgb, var(--x) 14%, transparent); }
.player.turn-o { background: color-mix(in srgb, var(--o) 14%, transparent); }
.dot { width: 8px; height: 8px; flex: none; border-radius: 50%; background: var(--muted); opacity: 0.4; }
.dot.on { background: var(--good); opacity: 1; }
.score { font-size: 17px; font-weight: 700; font-variant-numeric: tabular-nums; color: var(--muted); }
.score.vs { font-size: 13px; font-weight: 600; }

.status {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 22px;
  margin-top: 4px;
  padding: 0 8px;
  text-align: center;
  font-size: 15px;
  font-weight: 600;
}
.status .pc { font-size: 17px; }
.status.hot-x { color: var(--x); }
.status.hot-o { color: var(--o); }
.status.muted { color: var(--muted); font-weight: 500; }
.pulse { width: 8px; height: 8px; border-radius: 50%; background: currentColor; animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.2; } }

.toast {
  max-width: calc(100% - 32px);
  padding: 9px 14px;
  border-radius: 999px;
  background: var(--ink);
  color: var(--bg);
  box-shadow: var(--shadow);
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  animation: fade 0.2s;
}

.fabs {
  position: absolute;
  right: calc(env(safe-area-inset-right) + 12px);
  bottom: calc(env(safe-area-inset-bottom) + 12px + var(--sheet-h, 0px));
  display: flex;
  flex-direction: column;
  gap: 10px;
  transition: bottom 0.25s;
}
.fab {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border: none;
  border-radius: 50%;
  background: var(--card);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  box-shadow: var(--shadow);
}
.fab:active { transform: scale(0.94); }
.fab svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }

.sheet {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  max-width: 496px;
  margin: 0 auto;
  padding: 0 calc(env(safe-area-inset-right) + 8px) calc(env(safe-area-inset-bottom) + 8px) calc(env(safe-area-inset-left) + 8px);
  pointer-events: none;
}
.sheet-card {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 18px;
  border-radius: 22px;
  background: var(--card);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  box-shadow: var(--shadow);
  pointer-events: auto;
  animation: rise 0.25s ease-out;
}
.sheet-title { display: flex; align-items: center; gap: 8px; font-size: 20px; font-weight: 700; }
.sheet-title .pc { font-size: 22px; }
.sheet-text { display: flex; align-items: center; flex-wrap: wrap; gap: 5px; margin: 0; font-size: 15px; color: var(--muted); }
.sheet-text .pc { font-size: 15px; }
.sheet-text.accent { color: var(--ink); font-weight: 600; }
.link {
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--bg);
  font: 15px ui-monospace, SFMono-Regular, Menlo, monospace;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  user-select: all;
  -webkit-user-select: all;
}
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.actions.single { grid-template-columns: 1fr; }

.menu { position: absolute; inset: 0; z-index: 10; }
.menu-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.35); animation: fade 0.2s; }
.menu-panel {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  max-width: 480px;
  max-height: 85%;
  margin: 0 auto;
  padding: 6px 14px calc(env(safe-area-inset-bottom) + 14px);
  overflow-y: auto;
  border-radius: 22px 22px 0 0;
  background: var(--card-solid);
  touch-action: pan-y;
  animation: rise 0.25s ease-out;
}
.menu-panel::before {
  content: "";
  display: block;
  width: 36px;
  height: 5px;
  margin: 4px auto 6px;
  border-radius: 3px;
  background: var(--line);
}
.menu-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  min-height: 54px;
  padding: 0 4px;
  border: none;
  border-bottom: 1px solid var(--line);
  background: none;
  text-align: left;
  font-size: 16px;
  cursor: pointer;
}
.menu-item.danger { color: var(--o); }
.menu-value { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rules { margin: 14px 4px; font-size: 14px; color: var(--muted); }

.switch {
  appearance: none;
  -webkit-appearance: none;
  position: relative;
  flex: none;
  width: 50px;
  height: 30px;
  margin: 0;
  border-radius: 15px;
  background: var(--line);
  cursor: pointer;
  transition: background 0.2s;
}
.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  transition: transform 0.2s;
}
.switch:checked { background: var(--good); }
.switch:checked::after { transform: translateX(20px); }

@keyframes rise { from { transform: translateY(16px); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
