/* Only the 24 Chinese characters the tiles use (Noto Serif TC, weight 800, SIL OFL; see fonts/OFL.txt). */
@font-face {
  font-family: "Mahjong Han";
  src: url("fonts/mahjong-han.woff2") format("woff2");
  font-weight: 400;
  font-display: block;
}

:root {
  /* Jade (the default): green baize. Tile backs are coral, drawn by render.js. */
  --felt: #165c42;
  --felt-light: #1f7a56;
  --felt-dark: #0c3a29;
  --bar: #0e3c2b;
  --panel: #0f3a2a;
  --card: #14503a;
  --card-active: #1b6247;
  --line: #1f5640;
  --line-strong: #2f6f55;
  --pressed: #3a7f5f;
  --text: #ebf0e6;
  --muted: #a9c4b5;
  --accent: #ffcd28;
  --button: #1d6b4d;
  --button-hover: #25805d;
  --button-border: #2f8f69;
  /* Fine noise that gives the table a woven, felt-like surface. */
  --texture: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='180' height='180'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0  0 0 0 0 0  0 0 0 0 0  0 0 0 0.11 0'/></filter><rect width='100%25' height='100%25' filter='url(%23n)'/></svg>");
  color-scheme: dark;
}

/* Midnight: deep blue cloth; tile backs are jade. */
:root[data-theme="midnight"] {
  --felt: #1a2a58;
  --felt-light: #27407e;
  --felt-dark: #0c1431;
  --bar: #0d1736;
  --panel: #111d40;
  --card: #1b2b5c;
  --card-active: #243a78;
  --line: #24346a;
  --line-strong: #3a5096;
  --pressed: #34509a;
  --muted: #aab6da;
  --button: #23376f;
  --button-hover: #2d4588;
  --button-border: #3d58a0;
}

/* Rosewood: a dark polished table; tile backs are blue. */
:root[data-theme="rosewood"] {
  --felt: #5a2c1a;
  --felt-light: #7a4127;
  --felt-dark: #2e140a;
  --bar: #2a1209;
  --panel: #33170c;
  --card: #4a2414;
  --card-active: #5f311c;
  --line: #4d2a19;
  --line-strong: #7a4a30;
  --pressed: #7d4a2e;
  --muted: #d9bfae;
  --button: #5e301c;
  --button-hover: #743c24;
  --button-border: #8a5435;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  display: grid;
  grid-template: "hud" auto "stage" minmax(0, 1fr) "caption" auto / minmax(0, 1fr);
  background: var(--felt-dark);
  color: var(--text);
  font: 15px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overflow: hidden;
  /* No double-tap zoom or text selection while tapping tiles. */
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

/* --- top bar ------------------------------------------------------------- */

.hud {
  grid-area: hud;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 6px 16px;
  padding: 8px max(16px, env(safe-area-inset-right)) 8px max(16px, env(safe-area-inset-left));
  background: var(--bar);
}

.stats {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 4px 18px;
  font-size: 17px;
}

.stat .label { color: var(--muted); }
.stat b { font-variant-numeric: tabular-nums; }


.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

button {
  font: inherit;
  font-size: 14px;
  color: var(--text);
  background: var(--button);
  border: 1px solid var(--button-border);
  border-radius: 8px;
  padding: 6px 11px;
  min-height: 34px;
  cursor: pointer;
}

button:hover { background: var(--button-hover); }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button[aria-pressed="true"] { background: var(--pressed); border-color: var(--accent); }
button.primary { background: #c99a10; border-color: var(--accent); color: #1b1300; font-weight: 600; }
button.primary:hover { background: #e0ad16; }

kbd {
  font: inherit;
  font-size: 11px;
  color: var(--muted);
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  padding: 0 4px;
  margin-left: 3px;
}

body.demo .play-only,
body:not(.demo) .demo-only { display: none; }

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

.stage {
  grid-area: stage;
  position: relative;
  min-height: 0;
  /* Lit from above: brighter in the middle, darker toward the edges, with texture. */
  background:
    var(--texture),
    radial-gradient(ellipse 85% 75% at 50% 42%, var(--felt-light), var(--felt) 58%, var(--felt-dark));
}

#board {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  cursor: pointer;
}

body.demo #board { cursor: default; }

.rotate-hint {
  display: none;
  position: absolute;
  top: 6px;
  left: 0;
  right: 0;
  margin: 0;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
  pointer-events: none;
}

/* A short-lived notice over the top of the board. It floats, so showing or
   hiding it never changes the layout or resizes the board. */
.toast {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  margin: 0;
  padding: 6px 14px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.72);
  color: var(--accent);
  font-size: 15px;
  text-align: center;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
}

.toast.show { opacity: 1; }

/* --- end-of-game banner -------------------------------------------------- */

.banner {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  pointer-events: none;
}

.banner[hidden] { display: none; }
.banner.bottom { align-items: flex-end; }
.banner.bottom .banner-box { padding: 12px 18px; background: rgba(0, 0, 0, 0.82); }
.banner.bottom h2 { font-size: 26px; margin-bottom: 2px; }
.banner.bottom p { margin-bottom: 10px; font-size: 15px; }

.banner-box {
  pointer-events: auto;
  max-width: 520px;
  width: 100%;
  padding: 20px 24px;
  border-radius: 14px;
  background: rgba(0, 0, 0, 0.78);
  text-align: center;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.4);
}

.banner-box h2 {
  margin: 0 0 6px;
  font-size: 34px;
  color: var(--accent);
}

.banner-box p { margin: 0 0 16px; font-size: 17px; }

.banner-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

.banner-actions button { font-size: 16px; padding: 8px 16px; }

/* --- captions ------------------------------------------------------------ */

.caption {
  grid-area: caption;
  padding: 8px max(16px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  background: var(--bar);
  min-height: 58px;
}

.caption p { margin: 0; }
#caption-1 { font-size: 17px; font-weight: 600; }
#caption-2 { font-size: 15px; color: var(--accent); min-height: 1.35em; }

/* --- small screens ------------------------------------------------------- */

/* The key hints make the toolbar too wide for one row on laptop screens; the button
   tooltips and the How to play keys table still show them. */
@media (max-width: 1360px) {
  .toolbar kbd { display: none; }
}

@media (max-width: 900px) {
  kbd { display: none; }
  .stats { font-size: 15px; gap: 2px 12px; }
  button { padding: 5px 9px; font-size: 13px; min-height: 32px; }
}

/* Phones in landscape: the board is limited by height, so the controls move to a
   side column where there is spare width, and the captions shrink. */
@media (max-height: 520px) and (orientation: landscape) {
  .mode-chip { align-self: flex-start; margin-top: 2px; }
  body {
    grid-template: "stage hud" minmax(0, 1fr) "caption hud" auto / minmax(0, 1fr) auto;
  }
  .hud {
    flex-direction: column;
    flex-wrap: nowrap;
    align-items: stretch;
    justify-content: flex-start;
    gap: 6px;
    width: 148px;
    padding: max(6px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) 6px 8px;
    overflow-y: auto;
  }
  .stats { flex-direction: column; gap: 0; font-size: 13px; line-height: 1.3; }
  .toolbar { display: grid; grid-template-columns: 1fr 1fr; gap: 5px; }
  .toolbar button:not(.icon) { grid-column: span 2; }
  .toolbar button { min-height: 27px; padding: 2px 8px; }
  .caption {
    min-height: 0;
    padding: 3px 8px max(3px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  }
  #caption-1 { font-size: 13px; }
  #caption-2 { font-size: 12px; }
  .banner-box { padding: 12px 16px; }
  .banner-box h2 { font-size: 26px; }
  .banner-box p { margin-bottom: 10px; font-size: 14px; }
}

@media (orientation: portrait) and (max-width: 600px) {
  .rotate-hint { display: block; }
}

/* --- icon buttons, pause screen -------------------------------------------- */

button.icon {
  min-width: 34px;
  padding: 4px 8px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

button.subtle {
  background: transparent;
  border-color: var(--line-strong);
  color: var(--muted);
}

button.subtle.armed {
  border-color: #e8705f;
  color: #ffb3a8;
}

.paused-screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  background: var(--felt);
  text-align: center;
  padding: 16px;
}

.paused-screen[hidden] { display: none; }
.paused-screen h2 { margin: 0; font-size: 40px; color: var(--accent); }
.paused-screen p { margin: 0 0 14px; color: var(--muted); }
.paused-screen button { font-size: 17px; padding: 10px 28px; }

.banner-stats {
  color: var(--muted);
  font-size: 15px;
}

.banner-stats:empty { display: none; }
.banner-actions button[hidden] { display: none; }
.new-best { color: var(--accent); font-weight: 700; }

/* --- dialogs --------------------------------------------------------------- */

dialog {
  width: min(460px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  padding: 0;
  border: 1px solid var(--button-border);
  border-radius: 14px;
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  -webkit-user-select: text;
  user-select: text;
}

dialog[open] { display: flex; flex-direction: column; }
dialog.wide { width: min(680px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(0, 0, 0, 0.55); }

dialog > h2,
dialog > p { margin: 0; padding: 18px 20px 0; }
dialog > h2 { font-size: 22px; }
dialog > p { color: var(--muted); }

.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px 10px 20px;
  border-bottom: 1px solid var(--line);
}

.dialog-head h2 { margin: 0; font-size: 22px; }

.dialog-body {
  overflow-y: auto;
  padding: 6px 20px 14px;
}

.dialog-body h3 {
  margin: 18px 0 6px;
  font-size: 15px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--accent);
}

.dialog-body p, .dialog-body li { line-height: 1.45; }
.dialog-body ul { padding-left: 20px; margin: 6px 0; }

.dialog-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
  padding: 16px 20px 18px;
}

.dialog-actions button { font-size: 15px; padding: 8px 14px; }

.fine { color: var(--muted); font-size: 13px; }

.tile-guide { display: grid; gap: 10px; }
.tile-guide p { margin: 2px 0 0; font-size: 14px; }
.tile-row { display: flex; gap: 4px; }
/* Nine tiles always fit on one line: they shrink on narrow screens. */
.tile-row canvas { width: calc((100% - 32px) / 9); max-width: 34px; height: auto; aspect-ratio: 34 / 45; }

table.keys { border-collapse: collapse; font-size: 14px; }
table.keys td { padding: 3px 10px 3px 0; vertical-align: top; }
table.keys kbd { font-size: 12px; color: var(--text); }

label.switch {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 0;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
}

label.switch input { width: 18px; height: 18px; accent-color: #e0ad16; }

.stat-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin: 8px 0;
}

.stat-grid div {
  background: var(--card);
  border-radius: 10px;
  padding: 8px 10px;
}

.stat-grid dt { font-size: 12px; color: var(--muted); }
.stat-grid dd { margin: 2px 0 0; font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }

@media (max-width: 480px) {
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
  table.keys td { display: block; padding: 0; }
  table.keys td:nth-child(odd) { margin-top: 6px; }
}

@media (prefers-reduced-motion: reduce) {
  .toast { transition: none; }
}

/* --- modes, layouts, daily ------------------------------------------------- */

.mode-chip {
  font-size: 13px;
  color: var(--muted);
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  padding: 1px 10px;
  white-space: nowrap;
}

.mode-chip.daily { color: var(--accent); border-color: #8a7420; }
.stat[hidden] { display: none; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

#board:focus { outline: none; }
#board:focus-visible { outline: 2px solid rgba(255, 205, 40, 0.5); outline-offset: -2px; }

.warn {
  margin: 8px 0 4px;
  padding: 8px 12px;
  border-radius: 8px;
  background: rgba(232, 112, 95, 0.15);
  color: #ffd2cb;
}

.warn[hidden] { display: none; }

.daily-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 10px;
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--card);
  border: 1px solid var(--line-strong);
}

.daily-card h3 { margin: 0 0 2px; }
.daily-card p { margin: 0; }
.daily-card button { white-space: nowrap; font-size: 15px; padding: 8px 14px; }

.layout-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(118px, 1fr));
  gap: 8px;
}

.layout-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 8px 6px;
  border-radius: 10px;
  background: var(--card);
  border: 2px solid transparent;
  min-height: 0;
}

.layout-card canvas { width: 100%; max-width: 120px; aspect-ratio: 120 / 70; }
.layout-card .name { font-weight: 600; }
.layout-card .best { font-size: 12px; color: var(--muted); }
.layout-card[aria-checked="true"] { border-color: var(--accent); background: var(--card-active); }

table.best-table { width: 100%; border-collapse: collapse; font-size: 14px; margin: 4px 0 8px; }
table.best-table th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 500; padding: 4px 0; }
table.best-table td { padding: 4px 0; border-top: 1px solid var(--line); font-variant-numeric: tabular-nums; }

@media (max-width: 480px) {
  .daily-card { flex-direction: column; align-items: stretch; }
}

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

button { display: inline-flex; align-items: center; justify-content: center; gap: 6px; }

.ico {
  width: 16px;
  height: 16px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* Buttons that change meaning carry two icons and show the one for their state. */
button[data-on="true"] .ico-off,
button:not([data-on="true"]) .ico-on { display: none; }

/* On laptop widths the toolbar keeps icons plus labels but drops the key hints
   (see above); below that, labels go too and the tooltips carry the names. */
@media (max-width: 1100px) and (min-height: 521px) {
  .toolbar .lbl { display: none; }
  .toolbar button { padding: 6px 9px; }
}

/* --- table theme picker (Settings) ----------------------------------------- */

.theme-row { display: flex; gap: 8px; margin: 6px 0 4px; }

.theme-swatch {
  flex: 1;
  flex-direction: column;
  gap: 6px;
  padding: 8px;
  border-radius: 10px;
  border: 2px solid transparent;
  background: var(--card);
}

.theme-swatch[aria-checked="true"] { border-color: var(--accent); }

.theme-swatch .chip {
  width: 100%;
  height: 34px;
  border-radius: 6px;
  position: relative;
}

.theme-swatch .chip::after {
  /* A little tile: ivory face on its coloured back. */
  content: "";
  position: absolute;
  left: calc(50% - 9px);
  top: 6px;
  width: 16px;
  height: 21px;
  border-radius: 3px;
  background: #fbf6e7;
  box-shadow: 3px 3px 0 var(--tile-back);
}

.chip-jade { background: radial-gradient(circle at 50% 40%, #1f7a56, #0c3a29); --tile-back: #c66a2d; }
.chip-midnight { background: radial-gradient(circle at 50% 40%, #27407e, #0c1431); --tile-back: #238a62; }
.chip-rosewood { background: radial-gradient(circle at 50% 40%, #7a4127, #2e140a); --tile-back: #2f55a3; }

table.best-table caption { caption-side: bottom; text-align: left; font-size: 12px; color: var(--muted); padding-top: 4px; }

/* An odd tile left alone on the last row of the statistics grid spans the row. */
.stat-grid div:last-child:nth-child(3n + 1) { grid-column: 1 / -1; }
@media (max-width: 480px) {
  .stat-grid div:last-child:nth-child(3n + 1) { grid-column: auto; }
  .stat-grid div:last-child:nth-child(2n + 1) { grid-column: 1 / -1; }
}
