:root {
  --ink: #14110d;
  --ink-2: #1d1914;
  --panel: #1d1a15;
  --panel-edge: #2d2820;
  --paper: #e9e2d4;
  --muted: #9a8f7c;
  --accent: #d4a24e;
  --accent-soft: rgba(212, 162, 78, 0.16);
  --danger: #e25d4a;
  --ok: #7fb069;
  --wood-1: #c99e63;
  --wood-2: #b3854b;
  --line: rgba(56, 38, 18, 0.85);
  --serif: "Instrument Serif", Georgia, "Times New Roman", serif;
  --mono: "Fragment Mono", "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; }

body {
  font-family: var(--mono);
  color: var(--paper);
  background:
    radial-gradient(1100px 700px at 50% -12%, #252018 0%, var(--ink) 62%),
    var(--ink);
  -webkit-font-smoothing: antialiased;
}

/* paper grain over everything */
body::after {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 50;
  opacity: 0.05;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2'/%3E%3C/filter%3E%3Crect width='160' height='160' filter='url(%23n)'/%3E%3C/svg%3E");
}

main {
  max-width: 620px;
  margin: 0 auto;
  min-height: 100dvh;
  padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 12px;
}

main[hidden] { display: none; }

button { font: inherit; color: inherit; cursor: pointer; }
input { font: inherit; color: inherit; }

/* ---------- home ---------- */

.home-head { text-align: center; margin-bottom: 14px; }

.kanji {
  color: var(--accent);
  letter-spacing: 0.6em;
  margin-left: 0.6em; /* recenter the tracked text */
  font-size: 0.85rem;
  margin-bottom: 10px;
  animation: rise 0.6s ease both;
}

h1 {
  font-family: var(--serif);
  font-weight: 400;
  font-size: clamp(3.2rem, 11vw, 4.6rem);
  line-height: 1;
  letter-spacing: -0.01em;
  animation: rise 0.6s 0.08s ease both;
}

.tagline {
  font-family: var(--serif);
  font-style: italic;
  color: var(--muted);
  font-size: 1.15rem;
  margin-top: 10px;
  animation: rise 0.6s 0.16s ease both;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: none; }
}

.setup { animation: rise 0.6s 0.24s ease both; }

.setup-label, .custom-label {
  color: var(--muted);
  font-size: 0.7rem;
  letter-spacing: 0.22em;
  text-transform: uppercase;
}

.setup-label { text-align: center; margin-bottom: 12px; }

.presets {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 9px;
}

.preset {
  background: var(--panel);
  border: 1px solid var(--panel-edge);
  border-radius: 12px;
  padding: 13px 4px 11px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  transition: border-color 0.15s, color 0.15s, background 0.15s, transform 0.1s;
}

.preset:hover { border-color: var(--muted); transform: translateY(-1px); }

.preset .tc { font-size: 1.25rem; }

.preset .cat {
  font-size: 0.62rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--muted);
}

.preset.selected,
.custom.selected {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.preset.selected .tc { color: var(--accent); }
.preset.selected .cat { color: var(--accent); }

.custom {
  margin-top: 9px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: var(--panel);
  border: 1px solid var(--panel-edge);
  border-radius: 12px;
  padding: 12px;
  color: var(--muted);
  transition: border-color 0.15s, background 0.15s;
}

.custom label { display: flex; align-items: center; gap: 7px; }

.custom input {
  width: 4.2ch;
  background: var(--ink);
  border: 1px solid var(--panel-edge);
  border-radius: 8px;
  padding: 6px 4px;
  text-align: center;
  color: var(--paper);
}

.custom input:focus { outline: none; border-color: var(--accent); }

.custom.selected, .custom.selected .plus { color: var(--accent); }

.btn-primary {
  background: var(--accent);
  color: #211705;
  border: none;
  border-radius: 12px;
  padding: 15px 24px;
  font-size: 0.92rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  transition: filter 0.15s, transform 0.1s;
  animation: rise 0.6s 0.32s ease both;
}

.btn-primary:hover { filter: brightness(1.08); transform: translateY(-1px); }
.btn-primary:disabled { filter: saturate(0.4) brightness(0.75); cursor: default; transform: none; }

.home-foot {
  text-align: center;
  color: var(--muted);
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  margin-top: 6px;
  animation: rise 0.6s 0.4s ease both;
}

.error { color: var(--danger); text-align: center; font-size: 0.85rem; }

/* ---------- game ---------- */

#game { gap: 10px; }

.game-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
}

.brand {
  font-family: var(--serif);
  font-size: 1.25rem;
  color: var(--paper);
  text-decoration: none;
}

.brand-mark { color: var(--accent); margin-right: 2px; }

.tc-chip { color: var(--muted); font-size: 0.85rem; }

.share {
  border: 1px dashed var(--panel-edge);
  border-radius: 14px;
  padding: 14px;
  text-align: center;
  background: rgba(29, 26, 21, 0.6);
}

.share-title {
  font-family: var(--serif);
  font-style: italic;
  font-size: 1.1rem;
  margin-bottom: 10px;
}

.share-row { display: flex; gap: 8px; }

#share-url {
  flex: 1;
  min-width: 0;
  background: var(--ink);
  border: 1px solid var(--panel-edge);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 16px; /* <16px makes iOS zoom the page when the input gets focus */
  color: var(--muted);
}

.btn-copy {
  background: var(--panel);
  border: 1px solid var(--accent);
  color: var(--accent);
  border-radius: 10px;
  padding: 10px 16px;
  font-size: 0.8rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  transition: background 0.15s;
}

.btn-copy:hover { background: var(--accent-soft); }

.waiting-line {
  margin-top: 11px;
  color: var(--muted);
  font-size: 0.78rem;
  letter-spacing: 0.06em;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.pulse {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 1.6s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.35; transform: scale(0.7); }
}

.player-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.who { display: flex; align-items: center; gap: 9px; font-size: 0.9rem; }

.mini-stone {
  width: 17px;
  height: 17px;
  border-radius: 50%;
  flex: none;
}

.mini-stone.black {
  background: radial-gradient(circle at 35% 30%, #5a5a58, #121110 65%);
  box-shadow: inset 0 1px 1px rgba(255, 255, 255, 0.25), 0 1px 2px rgba(0, 0, 0, 0.6);
}

.mini-stone.white {
  background: radial-gradient(circle at 35% 30%, #fffdf4, #d6cfbd 70%);
  box-shadow: inset 0 -1px 2px rgba(80, 70, 50, 0.4), 0 1px 2px rgba(0, 0, 0, 0.6);
}

.conn {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #464036;
  transition: background 0.3s;
}

.conn.on { background: var(--ok); }

.clock {
  font-size: clamp(1.5rem, 6.5vw, 2.1rem);
  font-variant-numeric: tabular-nums;
  background: var(--panel);
  border: 1px solid var(--panel-edge);
  border-radius: 12px;
  padding: 4px 16px;
  min-width: 4.6ch;
  text-align: center;
  transition: border-color 0.2s, color 0.2s, box-shadow 0.2s, opacity 0.2s;
  opacity: 0.75;
}

.clock.active {
  opacity: 1;
  color: var(--accent);
  border-color: rgba(212, 162, 78, 0.6);
  box-shadow: 0 0 22px rgba(212, 162, 78, 0.12);
}

.clock.low { color: var(--danger); border-color: rgba(226, 93, 74, 0.6); }

.status {
  font-family: var(--serif);
  font-style: italic;
  font-size: 1.12rem;
  text-align: center;
  color: var(--muted);
  min-height: 1.5em;
}

.status.mine { color: var(--accent); }

/* ---------- goban ---------- */

.board {
  width: min(100%, 58dvh);
  aspect-ratio: 1;
  align-self: center;
  display: grid;
  grid-template-columns: repeat(15, 1fr);
  grid-template-rows: repeat(15, 1fr);
  padding: 1.8%;
  border-radius: 10px;
  background:
    radial-gradient(140% 110% at 30% 12%, rgba(255, 240, 205, 0.22), transparent 55%),
    linear-gradient(155deg, var(--wood-1), var(--wood-2) 70%, #a3753f);
  box-shadow:
    0 18px 45px rgba(0, 0, 0, 0.55),
    inset 0 1px 0 rgba(255, 235, 200, 0.35),
    inset 0 -2px 6px rgba(60, 35, 10, 0.4);
}

.cell {
  position: relative;
  border: none;
  background-color: transparent;
  background-image:
    linear-gradient(var(--line), var(--line)),
    linear-gradient(var(--line), var(--line));
  background-size: 100% 1px, 1px 100%;
  background-position: center center, center center;
  background-repeat: no-repeat;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

/* trim grid lines at the four edges so it reads as a true goban */
.cell:nth-child(15n + 1) { background-size: 50% 1px, 1px 100%; background-position: right center, center center; }
.cell:nth-child(15n)     { background-size: 50% 1px, 1px 100%; background-position: left center, center center; }
.cell:nth-child(-n + 15) { background-size: 100% 1px, 1px 50%; background-position: center center, center bottom; }
.cell:nth-child(n + 211) { background-size: 100% 1px, 1px 50%; background-position: center center, center top; }
.cell:nth-child(1)   { background-size: 50% 1px, 1px 50%; background-position: right center, center bottom; }
.cell:nth-child(15)  { background-size: 50% 1px, 1px 50%; background-position: left center, center bottom; }
.cell:nth-child(211) { background-size: 50% 1px, 1px 50%; background-position: right center, center top; }
.cell:nth-child(225) { background-size: 50% 1px, 1px 50%; background-position: left center, center top; }

/* star points */
.cell.hoshi::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 16%;
  height: 16%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: var(--line);
}

.stone {
  position: absolute;
  inset: 8%;
  border-radius: 50%;
  z-index: 1;
}

.stone.pop { animation: place 0.16s ease-out; }

@keyframes place {
  from { transform: scale(0.55); opacity: 0.3; }
  to { transform: scale(1); opacity: 1; }
}

.stone.black {
  background: radial-gradient(circle at 36% 30%, #57544f, #181614 55%, #060505 90%);
  box-shadow: 0 2px 3px rgba(20, 10, 0, 0.55), inset 0 1px 2px rgba(255, 255, 255, 0.22);
}

.stone.white {
  background: radial-gradient(circle at 36% 30%, #fffef6, #e7dfcc 60%, #c0b69d 95%);
  box-shadow: 0 2px 3px rgba(20, 10, 0, 0.45), inset 0 -1px 2px rgba(90, 75, 50, 0.35);
}

.cell.last .stone::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 26%;
  height: 26%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: var(--accent);
}

.cell.last .stone.black::after { background: var(--accent); }
.cell.last .stone.white::after { background: #a3753f; }

.cell.win .stone {
  box-shadow: 0 0 0 2px var(--accent), 0 0 14px rgba(212, 162, 78, 0.65);
  animation: glow 1.2s ease-in-out infinite alternate;
}

@keyframes glow {
  from { box-shadow: 0 0 0 2px var(--accent), 0 0 8px rgba(212, 162, 78, 0.4); }
  to { box-shadow: 0 0 0 2px var(--accent), 0 0 18px rgba(212, 162, 78, 0.85); }
}

/* ghost stone on hover when it's your move */
.board .cell { cursor: default; }
.board.playable .cell { cursor: pointer; }

.board.playable .cell:not(.taken):hover::after {
  content: "";
  position: absolute;
  inset: 8%;
  border-radius: 50%;
  opacity: 0.45;
}

.board.playable.ghost-black .cell:not(.taken):hover::after {
  background: radial-gradient(circle at 36% 30%, #57544f, #181614 60%);
}

.board.playable.ghost-white .cell:not(.taken):hover::after {
  background: radial-gradient(circle at 36% 30%, #fffef6, #d9d1bd 60%);
}

/* ---------- game over ---------- */

.over {
  text-align: center;
  background: var(--panel);
  border: 1px solid var(--panel-edge);
  border-radius: 16px;
  padding: 18px 16px;
  animation: rise 0.4s ease both;
}

.over-title {
  font-family: var(--serif);
  font-size: 1.7rem;
  line-height: 1.15;
}

.over-title.won { color: var(--accent); }
.over-title.lost { color: var(--danger); }

.score { color: var(--muted); font-size: 0.9rem; margin-top: 6px; }

.over-actions {
  margin-top: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
}

.over-actions .btn-primary { animation: none; }

#rematch.offered { animation: pulse-btn 1.4s ease-in-out infinite; }

@keyframes pulse-btn {
  0%, 100% { box-shadow: 0 0 0 0 rgba(212, 162, 78, 0.5); }
  50% { box-shadow: 0 0 0 9px rgba(212, 162, 78, 0); }
}

.ghost-link {
  color: var(--muted);
  font-size: 0.8rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.ghost-link:hover { color: var(--paper); }

/* ---------- toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(26px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--panel);
  border: 1px solid var(--panel-edge);
  color: var(--paper);
  border-radius: 10px;
  padding: 10px 18px;
  font-size: 0.82rem;
  z-index: 60;
  animation: rise 0.25s ease both;
}

@media (max-width: 430px) {
  .presets { grid-template-columns: repeat(4, 1fr); gap: 7px; }
  .preset .tc { font-size: 1.05rem; }
  .clock { padding: 3px 12px; }
}
