/* MAHJONG.EXE — real four-player mahjong against three bots, on a desktop
 * that otherwise pretends to be 1995. Like LOOP, this window stops being
 * beige at the title bar: red lacquer and gold leaf instead of a terminal,
 * because the joke (and the game) only work if the table looks like a
 * table. Tile faces are drawn as plain DOM nodes (pips positioned with
 * percentages) rather than images, so they stay crisp at any size and
 * never depend on a font shipping the Unicode Mahjong Tiles block.
 * Discards collect in a shared pond in the middle (one pile per side),
 * the way Mahjong Soul and most modern clients lay it out, rather than
 * scattered next to each player's own info card.
 */

#win-mahjong {
  --mj-lacquer: #7a1618;
  --mj-lacquer-dark: #4e0d0f;
  --mj-gold: #d4af37;
  --mj-gold-bright: #f4d976;
  --mj-felt: #0f5132;
  --mj-felt-dark: #0a3a24;
  --mj-ivory: #f5ecd7;
  --mj-ivory-dark: #e4d6ae;
  --mj-ink: #1a1310;
  padding: 0;
  overflow: hidden;
}
#win-mahjong .body { padding: 0; }

/* Maximize: same trick as LOOP — pin the whole window to the viewport
   (minus the taskbar) and let .mj-app fill it, tiles and all. Getting
   height:100% to actually reach .mj-app needs every ancestor in between
   to have a *definite* height too, not just an auto one, so .is-max
   turns the window and its .body into a flex column down to that point. */
#win-mahjong.is-max {
  position: fixed; inset: 0 0 var(--taskbar-h); width: 100% !important;
  max-width: none; z-index: 90;
  display: flex; flex-direction: column;
}
#win-mahjong.is-max .body { flex: 1; min-height: 0; display: flex; padding: 0; }

/* .mj-app always fills whatever width the window itself has — the window's
   own --w (and the base .window rule's max-width: calc(100vw - 24px)) is
   the single source of truth for width, so a non-maximized window never
   asks for more room than it's actually been given and gets clipped. */
.mj-app {
  position: relative;
  display: flex; flex-direction: column;
  width: 100%;
  height: 780px; max-height: 88vh;
  background: var(--mj-lacquer);
  color: var(--mj-ivory);
  font: 14px/1.4 var(--font);
}
#win-mahjong.is-max .mj-app { height: 100%; max-height: none; font-size: 15px; }
#win-mahjong.is-max .tile { --tw: 54px; --th: 75px; }
#win-mahjong.is-max .tile.small { --tw: 32px; --th: 44px; }
#win-mahjong.is-max .tile.tiny { --tw: 21px; --th: 29px; }
#win-mahjong.is-max .mj-board { grid-template-columns: 190px 1fr 190px; grid-template-rows: 96px 1fr 104px; }
#win-mahjong.is-max .mj-pond-top, #win-mahjong.is-max .mj-pond-bottom { width: 190px; }
#win-mahjong.is-max .mj-pond-left, #win-mahjong.is-max .mj-pond-right { width: 96px; }

/* --- title bar reskin -------------------------------------------------- */

#win-mahjong .titlebar {
  background: linear-gradient(90deg, var(--mj-lacquer-dark), var(--mj-lacquer) 55%, var(--mj-lacquer-dark));
  border-bottom: 2px solid var(--mj-gold);
}
#win-mahjong .titlebar h1 { font-family: var(--font); letter-spacing: .3px; }
#win-mahjong .tb { background: var(--mj-ivory); color: var(--mj-ink); }
#win-mahjong .tb#mj-maximize { font-size: 13px; }

/* --- toolbar ------------------------------------------------------------ */

.mj-toolbar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 8px 14px;
  background: var(--mj-lacquer-dark);
  border-bottom: 1px solid #000;
  flex: none;
}
.mj-toolbar .btn {
  background: var(--mj-gold); color: var(--mj-ink);
  border: 1px solid #000; box-shadow: none;
  padding: 6px 14px; font-weight: 700; font-size: 13.5px; cursor: pointer; border-radius: 3px;
}
.mj-toolbar .btn:active { transform: translateY(1px); }
.mj-toolbar .btn.ghost { background: transparent; color: var(--mj-gold-bright); border-color: var(--mj-gold); }
.mj-title-flourish { font-weight: 700; letter-spacing: .5px; color: var(--mj-gold-bright); font-size: 14px; }
.mj-spacer { flex: 1; }
.mj-credit-strip { display: flex; gap: 12px; font-size: 13px; }
.mj-credit-chip {
  display: flex; align-items: center; gap: 5px;
  background: rgba(0,0,0,.25); border: 1px solid var(--mj-gold);
  border-radius: 12px; padding: 3px 10px;
  transition: transform .25s;
}
.mj-credit-chip.pulse { transform: scale(1.18); }
.mj-credit-chip img, .mj-credit-chip .mj-avatar-fallback { width: 20px; height: 20px; border-radius: 50%; object-fit: cover; }
.mj-credit-chip b { color: var(--mj-gold-bright); }

/* --- help drawer --------------------------------------------------------- */

.mj-help {
  flex: none; max-height: 0; overflow: hidden;
  background: #200a0a; border-bottom: 0 solid var(--mj-gold);
  transition: max-height .25s ease;
}
.mj-help.open { max-height: 260px; border-bottom-width: 2px; overflow-y: auto; }
.mj-help-inner { padding: 12px 18px; font-size: 13.5px; line-height: 1.6; }
.mj-help-inner h2 { margin: 0 0 5px; font-size: 15px; color: var(--mj-gold-bright); }
.mj-help-inner p { margin: 0 0 7px; }
.mj-help-inner b { color: var(--mj-gold-bright); }

/* --- themed scrollbars (the log and rules drawer are the only things that
   scroll inside the window, so both get the same treatment) ------------- */

.mj-log, .mj-help {
  scrollbar-width: thin; scrollbar-color: var(--mj-gold) rgba(0,0,0,.35);
}
.mj-log::-webkit-scrollbar, .mj-help::-webkit-scrollbar { width: 8px; }
.mj-log::-webkit-scrollbar-track, .mj-help::-webkit-scrollbar-track { background: rgba(0,0,0,.35); border-radius: 4px; }
.mj-log::-webkit-scrollbar-thumb, .mj-help::-webkit-scrollbar-thumb {
  background: var(--mj-gold); border-radius: 4px; border: 1px solid var(--mj-lacquer-dark);
}
.mj-log::-webkit-scrollbar-thumb:hover, .mj-help::-webkit-scrollbar-thumb:hover { background: var(--mj-gold-bright); }

/* --- board --------------------------------------------------------------- */

.mj-board {
  flex: 1; min-height: 0;
  display: grid;
  grid-template-columns: 152px 1fr 152px;
  grid-template-rows: 80px 1fr 88px;
  grid-template-areas:
    "corner1 top    corner2"
    "left    center right"
    "corner3 bottom corner4";
  gap: 6px; padding: 10px;
  background:
    radial-gradient(ellipse at center, rgba(255,255,255,.05), transparent 70%),
    var(--mj-felt);
  background-color: var(--mj-felt);
}
.mj-corner { background: none; }

.mj-seat { display: flex; flex-direction: column; gap: 5px; min-width: 0; min-height: 0; }
.mj-seat-top { grid-area: top; }
.mj-seat-left { grid-area: left; }
.mj-seat-right { grid-area: right; }

.mj-seat-info {
  display: flex; align-items: center; gap: 8px;
  padding: 4px 8px;
  background: rgba(0,0,0,.3);
  border: 1px solid var(--mj-felt-dark);
  border-radius: 14px;
  transition: box-shadow .2s, border-color .2s;
}
.mj-seat.active .mj-seat-info {
  border-color: var(--mj-gold-bright);
  box-shadow: 0 0 10px 2px var(--mj-gold-bright);
}
.mj-avatar {
  width: 32px; height: 32px; border-radius: 50%; object-fit: cover;
  border: 1px solid var(--mj-gold); flex: none; background: var(--mj-ivory);
}
.mj-avatar-fallback {
  width: 32px; height: 32px; border-radius: 50%; flex: none;
  display: grid; place-items: center; background: var(--mj-gold);
  color: var(--mj-ink); font-weight: 700; border: 1px solid var(--mj-gold);
}
.mj-seat-name-wrap { min-width: 0; flex: 1; }
.mj-seat-name { font-weight: 700; font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mj-seat-sub { font-size: 12px; opacity: .75; white-space: nowrap; }
.mj-seat-melds { display: flex; gap: 4px; flex-wrap: wrap; min-height: 0; }
.mj-seat-hand-count { display: flex; gap: 2px; flex-wrap: wrap; }

/* --- the pond: all four discard piles, collected in the middle ---------- */

.mj-pond {
  grid-area: center;
  display: grid;
  grid-template-columns: auto 1fr auto;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    ".     ptop    ."
    "pleft pcenter pright"
    ".     pbottom .";
  gap: 8px 14px;
  place-items: center;
  padding: 8px;
  background: radial-gradient(circle, rgba(0,0,0,.15), transparent 75%);
  border: 1px dashed rgba(212,175,55,.35);
  border-radius: 10px;
  min-width: 0; min-height: 0;
  position: relative;
}
.mj-pond-top { grid-area: ptop; }
.mj-pond-left { grid-area: pleft; }
.mj-pond-right { grid-area: pright; }
.mj-pond-bottom { grid-area: pbottom; }
.mj-pond-center {
  grid-area: pcenter;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 6px; min-width: 0; min-height: 0; max-width: 100%;
}
.mj-pond-pile {
  display: flex; flex-wrap: wrap; gap: 3px; align-content: flex-start;
}
.mj-pond-top, .mj-pond-bottom { width: 148px; justify-content: center; }
.mj-pond-left, .mj-pond-right { width: 78px; justify-content: center; }

.mj-wall-count { font-size: 16px; font-weight: 700; color: var(--mj-gold-bright); text-align: center; }
.mj-wall-count small { display: block; font-weight: 400; opacity: .7; font-size: 11px; }
.mj-turn-arrow { font-size: 24px; color: var(--mj-gold-bright); animation: mj-spin-hint 1.8s ease-in-out infinite; }
@keyframes mj-spin-hint { 0%,100% { opacity: .5; } 50% { opacity: 1; } }
.mj-log {
  width: 220px; max-width: 100%; max-height: 96px; overflow-y: auto;
  font-size: 12.5px; line-height: 1.5; color: #d9cba8;
  text-align: center;
}
.mj-log div:last-child { color: var(--mj-gold-bright); font-weight: 700; }

/* --- shared discard-pile tile container (used for the pond piles) ------ */

.mj-discards { display: flex; flex-wrap: wrap; gap: 3px; align-content: flex-start; }

/* --- bottom (player) strip within the grid ------------------------------ */

.mj-seat-bottom-info {
  grid-area: bottom;
  display: flex; flex-direction: column; gap: 5px; min-width: 0;
}

/* --- tiles --------------------------------------------------------------- */

.tile {
  --tw: 40px; --th: 56px;
  position: relative;
  width: var(--tw); height: var(--th);
  border-radius: 4px;
  background: linear-gradient(160deg, var(--mj-ivory) 0%, var(--mj-ivory-dark) 100%);
  box-shadow: inset 0 0 0 1px #fffef8, inset 0 -3px 0 rgba(0,0,0,.25), 0 2px 3px rgba(0,0,0,.4);
  flex: none;
  display: block;
}
.tile.small { --tw: 24px; --th: 34px; border-radius: 3px; }
.tile.tiny { --tw: 17px; --th: 24px; border-radius: 2px; }
.tile.back {
  background:
    repeating-linear-gradient(45deg, var(--mj-lacquer) 0 3px, var(--mj-lacquer-dark) 3px 6px);
  border: 1px solid var(--mj-gold);
}
.tile.hand-tile {
  cursor: pointer;
  transition: transform .12s ease;
}
.tile.hand-tile:hover { transform: translateY(-8px); }
.tile.hand-tile.just-drawn {
  margin-left: 16px;
  animation: mj-just-drawn-pulse 1.1s ease-in-out infinite;
}
@keyframes mj-just-drawn-pulse {
  0%, 100% { box-shadow: inset 0 0 0 1px #fffef8, 0 0 0 3px var(--mj-gold-bright), 0 2px 3px rgba(0,0,0,.4); }
  50% { box-shadow: inset 0 0 0 1px #fffef8, 0 0 0 5px var(--mj-gold-bright), 0 0 10px 2px rgba(244,217,118,.7); }
}
.tile.latest-discard {
  box-shadow: inset 0 0 0 1px #fffef8, 0 0 0 2px #fff, 0 0 8px 2px rgba(255,255,255,.65);
}
.tile.disabled { cursor: default; filter: grayscale(.3) brightness(.85); }
.tile.disabled:hover { transform: none; }
.tile.ghost-slot { visibility: hidden; }

.tile-num {
  position: absolute; left: 0; right: 0; top: 8%;
  text-align: center; font-size: 20px; font-weight: 700; color: #b5121b;
  font-family: "Songti SC", "STSong", Georgia, serif;
}
.tile.small .tile-num { font-size: 12px; top: 6%; }
.tile.tiny .tile-num { font-size: 9px; }
.tile-wan {
  position: absolute; left: 0; right: 0; bottom: 8%;
  text-align: center; font-size: 18px; font-weight: 700; color: #17324a;
  font-family: "Songti SC", "STSong", Georgia, serif;
}
.tile.small .tile-wan { font-size: 11px; bottom: 5%; }
.tile.tiny .tile-wan { font-size: 8px; }

.tile-honor {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  font-size: 26px; font-weight: 700;
  font-family: "Songti SC", "STSong", Georgia, serif;
}
.tile.small .tile-honor { font-size: 16px; }
.tile.tiny .tile-honor { font-size: 11px; }
.tile-wind { color: #17324a; }
.tile-dragon-red { color: #b5121b; }
.tile-dragon-green { color: #146c2e; }
.tile-blank-frame {
  width: 62%; height: 72%; border: 2px solid #17324a; border-radius: 1px;
  display: grid; place-items: center; font-size: 55%; color: #17324a; opacity: .55; font-weight: 700;
}
.tile.small .tile-blank-frame, .tile.tiny .tile-blank-frame { border-width: 1px; }

.mj-meld {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 3px 4px 2px; margin-right: 6px;
  background: rgba(244, 217, 118, .14);
  border: 1px solid rgba(244, 217, 118, .55);
  border-radius: 5px;
}
.mj-meld-tiles { display: flex; gap: 3px; }
.mj-meld-label {
  font-size: 9.5px; font-weight: 700; letter-spacing: .3px; text-transform: uppercase;
  color: var(--mj-gold-bright); opacity: .9; white-space: nowrap;
}
.tile.called { transform: rotate(8deg) translateY(-4px); }
.mj-app.your-turn .mj-player-area { box-shadow: inset 0 0 0 2px var(--mj-gold-bright); }

.tile-pips { position: absolute; inset: 6%; }
.pip { position: absolute; width: 20%; height: 14%; transform: translate(-50%, -50%); }
.pip-dot { border-radius: 50%; box-shadow: inset 0 0 0 1px rgba(0,0,0,.35); }
.pip-bamboo {
  width: 13%; height: 22%; border-radius: 45% / 20%;
  background: #146c2e !important; box-shadow: inset 0 0 0 1px rgba(0,0,0,.35);
}
.pip-bamboo::before, .pip-bamboo::after {
  content: ""; position: absolute; left: 8%; right: 8%; height: 10%;
  background: rgba(0,0,0,.3);
}
.pip-bamboo::before { top: 28%; }
.pip-bamboo::after { top: 62%; }

/* --- player hand & actions ----------------------------------------------- */

.mj-player-area {
  flex: none;
  background: var(--mj-lacquer-dark);
  border-top: 2px solid var(--mj-gold);
  padding: 8px 14px 10px;
  display: flex; flex-direction: column; gap: 6px;
}
.mj-player-melds { display: flex; gap: 8px; min-height: 0; }
.mj-player-hand-row { display: flex; align-items: flex-end; gap: 4px; min-height: 66px; flex-wrap: wrap; }
.mj-player-actions {
  display: flex; align-items: center; gap: 10px; min-height: 30px;
  font-size: 14px;
}
.mj-player-actions .btn {
  background: var(--mj-gold); color: var(--mj-ink); border: 1px solid #000;
  padding: 5px 14px; font-weight: 700; font-size: 13.5px; border-radius: 4px; cursor: pointer; box-shadow: none;
}
.mj-player-actions .btn.win { background: #c8102e; color: var(--mj-ivory); }
.mj-player-actions .btn:active { transform: translateY(1px); }
.mj-hint { color: var(--mj-gold-bright); opacity: .85; }

/* --- claim overlay --------------------------------------------------------- */

.mj-claim-overlay {
  position: absolute; left: 50%; bottom: 230px; transform: translateX(-50%);
  z-index: 40;
  background: var(--mj-lacquer);
  border: 2px solid var(--mj-gold-bright);
  border-radius: 10px;
  padding: 14px 18px;
  display: flex; flex-direction: column; gap: 10px; align-items: center;
  box-shadow: 0 8px 22px rgba(0,0,0,.5);
  min-width: 320px;
}
.mj-claim-overlay.hidden { display: none; }
.mj-claim-title { font-weight: 700; color: var(--mj-gold-bright); font-size: 15px; text-align: center; }
.mj-claim-options { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.mj-claim-options .btn {
  background: var(--mj-gold); color: var(--mj-ink); border: 1px solid #000;
  padding: 6px 16px; font-weight: 700; font-size: 14px; border-radius: 4px; cursor: pointer; box-shadow: none;
  display: flex; align-items: center; gap: 4px;
}
.mj-claim-options .btn.pass { background: transparent; color: var(--mj-ivory); border-color: var(--mj-gold); }
.mj-claim-options .btn.hu { background: #c8102e; color: var(--mj-ivory); }
.mj-claim-timer { width: 100%; height: 5px; background: rgba(255,255,255,.15); border-radius: 3px; overflow: hidden; }
.mj-claim-timer i { display: block; height: 100%; background: var(--mj-gold-bright); transform-origin: left; }

/* --- result overlay ---------------------------------------------------- */

.mj-result-overlay {
  position: absolute; inset: 0; z-index: 50;
  display: flex; align-items: center; justify-content: center;
  background: rgba(10,3,3,.78);
}
.mj-result-overlay.hidden { display: none; }
.mj-result-card {
  background: var(--mj-lacquer); border: 3px solid var(--mj-gold-bright);
  border-radius: 12px; padding: 26px 34px; text-align: center;
  max-width: 560px; box-shadow: 0 12px 46px rgba(0,0,0,.6);
  animation: mj-pop .25s ease;
}
@keyframes mj-pop { from { transform: scale(.85); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.mj-result-card img.mj-result-avatar { width: 64px; height: 64px; border-radius: 50%; border: 2px solid var(--mj-gold); object-fit: cover; margin-bottom: 8px; }
.mj-result-title { font-size: 26px; font-weight: 700; color: var(--mj-gold-bright); margin: 0 0 6px; }
.mj-result-sub { font-size: 14px; margin: 0 0 12px; color: #ecdfc0; }
.mj-result-hand { display: flex; gap: 4px; justify-content: center; flex-wrap: wrap; margin-bottom: 12px; }
.mj-result-arrow { width: 18px; vertical-align: middle; margin: 0 3px; }
.mj-result-breakdown {
  display: flex; flex-direction: column; gap: 8px;
  margin: 0 0 16px; padding: 10px 14px;
  background: rgba(0,0,0,.25); border-radius: 8px;
  text-align: left;
}
.mj-breakdown-row { display: flex; flex-direction: column; gap: 2px; }
.mj-breakdown-main { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.mj-breakdown-main .mj-avatar, .mj-breakdown-main .mj-avatar-fallback { width: 22px; height: 22px; font-size: 11px; }
.mj-breakdown-name { flex: 1; }
.mj-breakdown-amt { font-weight: 700; font-variant-numeric: tabular-nums; }
.mj-breakdown-row.up .mj-breakdown-amt { color: #6fd67d; }
.mj-breakdown-row.down .mj-breakdown-amt { color: #ef7b7b; }
.mj-breakdown-row.winner .mj-breakdown-main { font-size: 16px; }
.mj-breakdown-row.winner .mj-breakdown-name { font-weight: 700; }
.mj-breakdown-flavor {
  font-size: 11.5px; font-style: italic; opacity: .8; margin-left: 30px;
}
.mj-breakdown-row.up .mj-breakdown-flavor { color: #a9e0af; }
.mj-breakdown-row.down .mj-breakdown-flavor { color: #e3a9a9; }
.mj-result-card .btn {
  background: var(--mj-gold); color: var(--mj-ink); border: 1px solid #000;
  padding: 7px 20px; font-weight: 700; font-size: 14px; border-radius: 4px; cursor: pointer; box-shadow: none;
}

/* --- floating credit popups ------------------------------------------- */

.mj-float {
  position: absolute; z-index: 45; font-weight: 700; font-size: 16px;
  color: var(--mj-gold-bright); pointer-events: none;
  animation: mj-float-up 1.3s ease-out forwards;
  text-shadow: 0 1px 2px #000;
}
@keyframes mj-float-up { from { transform: translateY(0); opacity: 1; } to { transform: translateY(-38px); opacity: 0; } }

/* --- responsive ---------------------------------------------------------- */

@media (max-width: 940px) {
  .mj-app { height: 78vh; }
  .mj-board { grid-template-columns: 130px 1fr 130px; grid-template-rows: 74px 1fr 82px; }
  .tile { --tw: 30px; --th: 42px; }
  .mj-pond-top, .mj-pond-bottom { width: 118px; }
  .mj-pond-left, .mj-pond-right { width: 64px; }
}
@media (max-width: 820px) {
  #win-mahjong { width: auto !important; }
  .mj-app { height: 76vh; max-height: none; }
  .mj-board { grid-template-columns: 80px 1fr 80px; grid-template-rows: 70px 1fr 64px; }
  .tile { --tw: 22px; --th: 31px; }
  .mj-pond-top, .mj-pond-bottom { width: 90px; }
  .mj-pond-left, .mj-pond-right { width: 48px; }
  .mj-credit-strip, .mj-title-flourish { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  .tile.hand-tile, .mj-seat-info, .mj-credit-chip, .mj-result-card, .mj-float, .mj-turn-arrow { animation: none !important; transition: none !important; }
}
