:root {
  --S: 640px;          /* board side, set by layout() */
  --hw: 48px;          /* hand tile width, set by layout() */
  --gold: #f2c14e;
  --felt-hi: #237a5b;
  --felt-mid: #175a42;
  --felt-lo: #0d3829;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: #071d16;
  color: #e9f1ec;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "Noto Sans SC", sans-serif;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

button, select { font: inherit; }

button {
  cursor: pointer;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, .18);
  background: #1d4a3a;
  color: #fff;
  padding: 6px 14px;
}
button:hover { filter: brightness(1.12); }
button.ghost { background: transparent; }
button.primary {
  background: linear-gradient(#f7cf68, #e0a52c);
  color: #3a2600;
  border-color: #c98f1c;
  font-weight: 700;
}

#app { display: flex; flex-direction: column; height: 100%; }

/* ---------------------------------------------------------------- top bar */
#topbar {
  flex: none;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 0 12px;
  background: rgba(0, 0, 0, .35);
  border-bottom: 1px solid rgba(255, 255, 255, .06);
}
.brand { font-weight: 800; letter-spacing: .08em; white-space: nowrap; }
.brand span { font-weight: 400; font-size: 12px; opacity: .55; margin-left: 6px; letter-spacing: 0; }
.top-actions { display: flex; align-items: center; gap: 12px; font-size: 13px; }
.tgl { display: flex; align-items: center; gap: 4px; cursor: pointer; opacity: .9; white-space: nowrap; }
.tgl input { accent-color: var(--gold); margin: 0; }
#topbar select {
  background: #143628;
  color: #e9f1ec;
  border: 1px solid rgba(255, 255, 255, .15);
  border-radius: 6px;
  padding: 3px 6px;
}
#topbar button { padding: 4px 12px; }

/* ---------------------------------------------------------------- stage */
#stage {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 6px 4px 4px;
}

#board {
  position: relative;
  flex: none;
  width: var(--S);
  height: var(--S);
  border-radius: calc(var(--S) * .025);
  background: radial-gradient(circle at 50% 45%, var(--felt-hi) 0%, var(--felt-mid) 55%, var(--felt-lo) 100%);
  box-shadow: inset 0 0 calc(var(--S) * .08) rgba(0, 0, 0, .45), 0 10px 30px rgba(0, 0, 0, .5);
  overflow: hidden;
}
.felt {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background-image: radial-gradient(rgba(255, 255, 255, .035) 1px, transparent 1px);
  background-size: 4px 4px;
}
#seats { position: absolute; inset: 0; z-index: 1; }
.seat { position: absolute; inset: 0; pointer-events: none; }
.seat.r1 { transform: rotate(-90deg); }
.seat.r2 { transform: rotate(180deg); }
.seat.r3 { transform: rotate(90deg); }

/* ---------------------------------------------------------------- tiles */
.tile {
  position: relative;
  flex: none;
  display: inline-block;
  width: var(--tw);
  height: calc(var(--tw) * 4 / 3);
  background-color: #fbf6ea;
  background-position: center;
  background-size: 100% 100%;
  background-repeat: no-repeat;
  border-radius: calc(var(--tw) * .1);
  box-shadow: 0 calc(var(--tw) * .07) 0 #c7b58c, 0 calc(var(--tw) * .1) calc(var(--tw) * .14) rgba(0, 0, 0, .38);
}
.tile.back {
  background: linear-gradient(160deg, #f4b04c, #d27b27);
  box-shadow: 0 calc(var(--tw) * .07) 0 #efe5cc, 0 calc(var(--tw) * .1) calc(var(--tw) * .14) rgba(0, 0, 0, .38);
}
.tile.back::after {
  content: "";
  position: absolute;
  inset: 14%;
  border-radius: calc(var(--tw) * .08);
  border: 1px solid rgba(255, 255, 255, .25);
}
.tile.called { opacity: .32; }
.tile.tsumogiri { filter: brightness(.86); }
.tile.last { animation: lastpulse 1.4s ease-in-out infinite; }
@keyframes lastpulse {
  0%, 100% { outline: 2px solid rgba(255, 213, 74, .95); outline-offset: 1px; }
  50% { outline: 2px solid rgba(255, 213, 74, .25); outline-offset: 1px; }
}
.tile.wintile { outline: 2px solid #ff6b4f; outline-offset: 1px; }
/* a tile entering the river / my hand */
.fresh { animation: drop .26s ease-out; }
.tile.last.fresh { animation: drop .26s ease-out, lastpulse 1.4s ease-in-out .26s infinite; }
@keyframes drop {
  from { opacity: 0; transform: translateY(-40%) scale(1.15); }
  to { opacity: 1; transform: none; }
}
#myhand .tile.fresh-draw { animation: drawin .22s ease-out; }
@keyframes drawin {
  from { opacity: 0; transform: translateY(-45%); }
  to { opacity: 1; transform: none; }
}
@media (prefers-reduced-motion: reduce) {
  .fresh, .tile.last.fresh, #myhand .tile.fresh-draw { animation: none; }
}

.side { position: relative; flex: none; width: calc(var(--tw) * 4 / 3); height: var(--tw); }
.side > .tile { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%) rotate(-90deg); }
.sidestack { display: flex; flex-direction: column; }
.gap { flex: none; width: calc(var(--tw) * .4); }

/* rivers: 6 per row, the third row keeps going */
.river {
  --tw: calc(var(--S) * .048);
  position: absolute;
  left: calc(var(--S) * .356);
  top: calc(var(--S) * .658);
  display: flex;
  flex-direction: column;
  gap: calc(var(--S) * .006);
}
.rrow { display: flex; align-items: flex-end; height: calc(var(--tw) * 4 / 3); }

/* opponent hands and melds sit along the outer edge of their layer */
.ohand {
  --tw: calc(var(--S) * .036);
  position: absolute;
  left: calc(var(--S) * .15);
  bottom: calc(var(--S) * .014);
  display: flex;
  align-items: flex-end;
}
.omelds {
  --tw: calc(var(--S) * .034);
  position: absolute;
  right: calc(var(--S) * .014);
  bottom: calc(var(--S) * .014);
  display: flex;
  align-items: flex-end;
  gap: calc(var(--S) * .007);
}
.meld { display: flex; align-items: flex-end; }

.rstick {
  position: absolute;
  left: 50%;
  top: calc(var(--S) * .636);
  width: calc(var(--S) * .12);
  height: calc(var(--S) * .01);
  transform: translateX(-50%);
  background: #fff;
  border-radius: 99px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .5);
}
.rstick::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: calc(var(--S) * .008);
  height: calc(var(--S) * .008);
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: #d22;
}

/* ---------------------------------------------------------------- center box */
#center {
  position: absolute;
  left: calc(var(--S) * .35);
  top: calc(var(--S) * .35);
  width: calc(var(--S) * .3);
  height: calc(var(--S) * .3);
  border-radius: calc(var(--S) * .02);
  background: linear-gradient(#163c2f, #0e2b21);
  box-shadow: 0 0 0 2px rgba(0, 0, 0, .25), inset 0 2px 10px rgba(0, 0, 0, .45);
  font-size: calc(var(--S) * .02);
  z-index: 0;
}
/* per-seat score strip along that seat's edge of the center box (lives in the rotated seat layer) */
.ctag {
  position: absolute;
  left: calc(var(--S) * .362);
  width: calc(var(--S) * .276);
  top: calc(var(--S) * .594);
  height: calc(var(--S) * .038);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: .5em;
  font-size: calc(var(--S) * .022);
  line-height: 1;
  border-radius: calc(var(--S) * .008);
  border: 1px solid transparent;
  white-space: nowrap;
}
.seat.r2 .ctag { transform: rotate(180deg); }
.ctag .wind { font-weight: 800; font-size: 1.2em; color: #cfe3d8; }
.ctag.dealer .wind { color: #ff7d6b; }
.ctag .pts { font-weight: 700; font-variant-numeric: tabular-nums; }
.ctag .nm { font-size: .8em; opacity: .6; }
.ctag.active { border-color: var(--gold); box-shadow: 0 0 8px rgba(242, 193, 78, .5); background: rgba(242, 193, 78, .1); }
.ctag.thinking .nm { animation: thinking 1s ease-in-out infinite; }
@keyframes thinking { 50% { opacity: .15; } }

.cinfo {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: .3em;
  text-align: center;
  white-space: nowrap;
}
.cinfo .round { font-size: 1.4em; font-weight: 800; letter-spacing: .06em; }
.cinfo .sub { font-size: .8em; opacity: .7; }
.cinfo .dora { --tw: calc(var(--S) * .024); display: flex; gap: calc(var(--S) * .002); margin: .15em 0 .2em; }
.cinfo .left { font-size: .85em; opacity: .9; }
.cinfo .left b { color: var(--gold); font-size: 1.15em; }

/* ---------------------------------------------------------------- callouts */
#callouts { position: absolute; inset: 0; pointer-events: none; z-index: 2; }
.callout {
  position: absolute;
  transform: translate(-50%, -50%);
  font-family: "Songti SC", "STSong", "Noto Serif CJK SC", "Noto Serif SC", serif;
  font-size: calc(var(--S) * .08);
  font-weight: 900;
  color: #fff6e0;
  -webkit-text-stroke: calc(var(--S) * .003) #b23b12;
  text-shadow: 0 0 14px rgba(255, 140, 40, .7), 0 3px 0 #7a2508;
  white-space: nowrap;
  animation: pop 1.3s ease-out forwards;
}
.callout.r0 { left: 50%; top: 80%; }
.callout.r1 { left: 80%; top: 50%; }
.callout.r2 { left: 50%; top: 20%; }
.callout.r3 { left: 20%; top: 50%; }
.callout.center { left: 50%; top: 50%; }
.callout.banner {
  font-size: calc(var(--S) * .06);
  -webkit-text-stroke: 0;
  color: #fff;
  text-shadow: 0 2px 12px rgba(0, 0, 0, .8);
  background: rgba(0, 0, 0, .55);
  padding: .25em 1.2em;
  border-radius: .3em;
  animation-duration: 1.5s;
}
@keyframes pop {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(1.6); }
  14% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
  78% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -50%) scale(1); }
}

/* ---------------------------------------------------------------- hand bar */
#handbar { flex: none; width: 100%; display: flex; flex-direction: column; align-items: center; gap: 4px; }
#actions { min-height: 42px; display: flex; flex-wrap: wrap; gap: 8px; align-items: center; justify-content: center; }
#actions button {
  --tw: 18px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-width: 72px;
  justify-content: center;
  padding: 7px 16px;
  font-size: 16px;
  font-weight: 700;
  border-radius: 10px;
  box-shadow: 0 3px 0 rgba(0, 0, 0, .35);
}
#actions button.win { background: linear-gradient(#ff8a5b, #d9461f); border-color: #a8300f; }
#actions button.riichi { background: linear-gradient(#ffb347, #e07b12); border-color: #a95a08; }
#actions button.call { background: linear-gradient(#4aa3df, #1f6fb2); border-color: #145285; }
#actions button.kan { background: linear-gradient(#a77bdb, #6d3fb0); border-color: #4f2a85; }
#actions button.ghost { background: rgba(255, 255, 255, .08); }
#actions button.rec, #actions button.rec:hover { outline: 3px solid var(--gold); outline-offset: 2px; }
#actions .tile { --tw: 18px; box-shadow: none; }

#handline { display: flex; align-items: flex-end; gap: calc(var(--hw) * .5); padding-top: calc(var(--hw) * .25); }
#myhand { --tw: var(--hw); display: flex; align-items: flex-end; }
#myhand .tile { transition: transform .08s ease-out; }
#myhand .tile.playable { cursor: pointer; }
#myhand .tile.playable:hover { transform: translateY(-16%); }
#myhand .tile.disabled { filter: brightness(.55) saturate(.6); }
#myhand .tile.rec { outline: 3px solid var(--gold); outline-offset: 1px; }
#myhand.riichi-mode .tile.playable { box-shadow: 0 0 0 2px #ffb347, 0 calc(var(--tw) * .07) 0 #c7b58c; }
#mymelds { --tw: calc(var(--hw) * .78); display: flex; gap: calc(var(--hw) * .18); align-items: flex-end; }

#tip { min-height: 22px; font-size: 13px; line-height: 22px; opacity: .92; text-align: center; padding: 0 8px; }
#tip .w { color: var(--gold); font-weight: 700; }
#tip .bad { color: #ff8a7a; font-weight: 700; }
#tip .ai { color: #9fd8ff; }
#tip .sep { opacity: .4; margin: 0 .6em; }

/* ---------------------------------------------------------------- overlays */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(3, 14, 10, .62);
  backdrop-filter: blur(2px);
}
.overlay.show { display: flex; }
.panel {
  width: 100%;
  max-width: 680px;
  max-height: 100%;
  overflow: auto;
  padding: 20px 22px;
  border-radius: 14px;
  background: #f8f4ea;
  color: #1f2a24;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .5);
  color-scheme: light;
  -webkit-user-select: text;
  user-select: text;
}
.panel button { background: #fff; color: #222; border-color: #cfc6b0; }
.panel button.primary { background: linear-gradient(#f7cf68, #e0a52c); color: #3a2600; border-color: #c98f1c; }

.menu-panel { max-width: 560px; }
.menu-panel h1 { margin: 0 0 4px; font-size: 26px; letter-spacing: .06em; }
.menu-panel h1 small { font-size: 14px; font-weight: 400; color: #6b7b72; letter-spacing: 0; }
.menu-panel .sub { margin: 0 0 16px; color: #56645c; font-size: 14px; }
.field { margin: 14px 0; }
.field .label { font-weight: 700; margin-bottom: 6px; font-size: 14px; }
.seg { display: inline-flex; flex-wrap: wrap; border: 1px solid #c9bfa6; border-radius: 9px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; border-right: 1px solid #e3dccb; padding: 7px 16px; background: #fff; color: #333; }
.seg button:last-child { border-right: 0; }
.seg button.on { background: #1d5a44; color: #fff; }
.cpu-grid { display: flex; flex-wrap: wrap; gap: 14px; margin-top: 10px; font-size: 14px; }
.cpu-grid select { margin-left: 4px; padding: 4px 6px; border-radius: 6px; border: 1px solid #c9bfa6; background: #fff; color: #222; }
.hint { margin-top: 8px; font-size: 13px; color: #56645c; line-height: 1.55; }
.hint b { color: #1d5a44; }
.menu-buttons { display: flex; gap: 10px; margin: 18px 0 8px; }
.menu-buttons button { padding: 10px 22px; font-size: 16px; }
.rules { margin-top: 12px; font-size: 13px; color: #44524a; }
.rules summary { cursor: pointer; font-weight: 700; }
.rules ul { padding-left: 18px; line-height: 1.6; }

/* result dialog */
.res h2 { margin: 0 0 6px; font-size: 20px; display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.res h2 small { font-size: 14px; font-weight: 400; color: #6b5a3a; }
.res .round { font-size: 13px; color: #6b7b72; margin-bottom: 8px; }
.res .win + .win { border-top: 1px dashed #d4c9ad; margin-top: 14px; padding-top: 14px; }
.res .handrow { --tw: 30px; display: flex; flex-wrap: wrap; align-items: flex-end; gap: 0; margin: 10px 0; row-gap: 8px; }
.res .handrow .meld { margin-left: 8px; }
.res .doras { display: flex; flex-wrap: wrap; gap: 16px; font-size: 13px; color: #56645c; margin: 4px 0 10px; }
.res .doras > div { display: flex; align-items: center; gap: 6px; }
.res .doras .tiles { --tw: 22px; display: flex; gap: 2px; }
.res .yaku { display: grid; grid-template-columns: 1fr auto; gap: 3px 24px; max-width: 360px; font-size: 15px; }
.res .yaku .h { text-align: right; font-variant-numeric: tabular-nums; }
.res .total { margin-top: 10px; font-size: 20px; font-weight: 800; color: #b2321f; }
.res .total small { font-size: 14px; color: #6b5a3a; font-weight: 600; margin-left: 8px; }
.res .tenpai { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; align-items: center; margin: 8px 0; font-size: 14px; }
.res .tenpai .handrow { margin: 0; --tw: 24px; }
.res .tenpai .tag { font-weight: 700; }
.res .tenpai .tag.yes { color: #1d7a3a; }
.res .tenpai .tag.no { color: #999; }
table.scores { width: 100%; border-collapse: collapse; margin-top: 16px; font-size: 15px; font-variant-numeric: tabular-nums; }
table.scores th, table.scores td { padding: 6px 8px; border-bottom: 1px solid #e5dcc6; text-align: right; }
table.scores th:first-child, table.scores td:first-child,
table.scores th:nth-child(2), table.scores td:nth-child(2) { text-align: left; }
table.scores th { font-size: 12px; color: #7a6c52; font-weight: 600; }
table.scores tr.me td { background: #fff6d8; font-weight: 700; }
.plus { color: #1d7a3a; }
.minus { color: #c0392b; }
.dlg-buttons { display: flex; justify-content: flex-end; gap: 10px; margin-top: 16px; }
.dlg-buttons button { padding: 9px 26px; font-size: 16px; }
.final .rank1 td { font-weight: 800; }

/* phones held upright: board at the top, hand down where the thumbs are */
@media (orientation: portrait) and (max-width: 640px) {
  #stage { justify-content: space-between; padding-bottom: calc(8px + env(safe-area-inset-bottom)); }
}
@media (max-width: 640px) {
  .brand span { display: none; }
  .top-actions { gap: 8px; font-size: 12px; }
  #topbar { padding: 0 8px; }
  #actions button { min-width: 60px; padding: 6px 12px; font-size: 15px; }
}
@media (max-width: 420px) {
  .brand { display: none; }
  .tgl span { font-size: 12px; }
}
