/* The table and tile faces keep their intrinsic colors in every theme. */
:root {
  --mj-tile-max: 96px;
}

.mj-topbar {
  gap: 12px 20px;
}

.mj-actions,
.mj-stats {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

.mj-stats {
  gap: 18px;
  margin-left: auto;
}

.mj-stat {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.mj-stat b {
  font-size: 16px;
}

.mj-clock b {
  min-width: 3.5ch;
}

.mj-wrap {
  width: calc(100% - 32px);
  margin: 0 auto;
  padding: 22px 0 28px;
}

.mj-heading {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px 16px;
  margin: 0 4px 16px;
}

.mj-heading h1 {
  margin: 0;
  font-size: clamp(22px, 4vw, 30px);
  font-weight: 650;
  letter-spacing: -0.025em;
}

.mj-layout-choices {
  display: flex;
  flex-shrink: 0;
  gap: 6px;
}

.mj-viewport {
  /* Both desktop layouts share a tile scale, bounded by usable screen space. */
  --mj-tile-size: min(var(--mj-tile-max), calc(var(--mj-board-space, calc(100vw - 34px)) * 56 / 920), calc(var(--mj-board-room, calc(100svh - 200px)) * 56 / 600));
  max-width: 100%;
  width: calc(var(--mj-board-w, 920) * var(--mj-tile-size) / 56 + 2px);
  margin-inline: auto;
  overflow: hidden;
  border: 1px solid #477762;
  border-radius: 12px;
  background: #164c37;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  touch-action: pan-x pan-y;
}

.mj-board {
  position: relative;
  isolation: isolate;
  width: 100%;
  aspect-ratio: var(--mj-board-w, 920) / var(--mj-board-h, 600);
  background: radial-gradient(ellipse at 48% 40%, #327152 0%, #225e43 55%, #164c37 100%);
  touch-action: pan-x pan-y;
}

.mj-tile {
  --mj-edge: clamp(0.375px, calc(var(--mj-tile-size) / 64), 1px);
  position: absolute;
  display: block;
  /* Keep native button hover painting from flashing through stacked tiles. */
  -webkit-appearance: none;
  appearance: none;
  padding: calc(2 * var(--mj-edge));
  border: 1px solid #c5bda3;
  border-top-color: #fffef3;
  border-left-color: #fffef3;
  border-radius: calc(5 * var(--mj-edge));
  background: #eee6cc linear-gradient(135deg, #fffdf0, #eee6cc);
  color: #213e31;
  box-shadow: var(--mj-edge) calc(3 * var(--mj-edge)) 0 #b1a585, calc(2 * var(--mj-edge)) calc(5 * var(--mj-edge)) 0 #617c62, calc(3 * var(--mj-edge)) calc(6 * var(--mj-edge)) calc(3 * var(--mj-edge)) rgba(0, 0, 0, 0.38), inset 0 0 0 var(--mj-edge) rgba(255, 255, 255, 0.6);
  -webkit-tap-highlight-color: transparent;
  touch-action: pan-x pan-y;
}

.mj-tile[hidden] {
  display: none;
}

.mj-tile svg {
  display: block;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

/* Disabled tiles remain opaque: upper layers must hide what is below. */
.mj-tile:disabled,
.mj-tile:disabled:hover {
  opacity: 1;
  cursor: default;
  background: #d9d0b7 linear-gradient(135deg, #e9e5d5, #d9d0b7);
  border-top-color: #eeebdf;
  border-left-color: #eeebdf;
}

.mj-tile:disabled svg {
  opacity: 0.75;
}

.mj-tile.is-free {
  cursor: pointer;
}

@media (hover: hover) {
  .mj-tile.is-free:hover {
    background: #fffef7;
    border-color: #f9d571;
  }
}

.mj-tile.is-selected,
.mj-tile.is-selected:hover {
  background: #e7f4ff;
  outline: calc(3 * var(--mj-edge)) solid #69c5ff;
  outline-offset: calc(-3 * var(--mj-edge));
}

.mj-tile.is-hint,
.mj-tile.is-hint:hover {
  background: #fff5cc;
  outline: calc(3 * var(--mj-edge)) dashed #ba6400;
  outline-offset: calc(-3 * var(--mj-edge));
}

.mj-tile:focus-visible {
  outline: calc(3 * var(--mj-edge)) solid #174fc5;
  outline-offset: calc(-4 * var(--mj-edge));
}

.mj-topbar button:focus-visible,
.mj-topbar a:focus-visible,
#mj-play-again:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 3px;
}

.mj-status {
  min-height: 1.5em;
  margin: 18px 8px 10px;
  text-align: center;
  font-size: 15px;
  font-weight: 600;
  line-height: 1.5;
  color: var(--accent);
}

.mj-instructions {
  max-width: 760px;
  margin: 0 auto;
  padding: 0 8px;
  text-align: center;
  font-size: 13px;
  line-height: 1.6;
}

.mj-instructions p {
  margin: 4px 0;
}

.mj-instructions .mj-size-tip {
  margin-top: 12px;
  font-size: 12px;
  opacity: 0.8;
}

#mj-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(0, 0, 0, 0.7);
}

#mj-overlay.hidden {
  display: none;
}

.mj-overlay-card {
  width: 100%;
  max-width: 420px;
  padding: 32px;
  border: 1px solid var(--tile-border);
  border-radius: 12px;
  background: var(--bg-color);
  color: var(--fg-color);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
  text-align: center;
}

.mj-overlay-card h2 {
  margin: 0 0 12px;
  font-size: 28px;
}

.mj-overlay-card p {
  margin: 0 0 24px;
  line-height: 1.5;
}

@media (max-width: 700px) {
  :root {
    --mj-tile-max: 64px;
  }

  .mj-topbar {
    gap: 8px 10px;
    padding: 8px 12px;
  }

  .mj-actions {
    order: 3;
    flex-basis: 100%;
    justify-content: center;
    gap: 6px;
    flex-wrap: nowrap;
  }

  .mj-actions button {
    padding: 8px 6px;
    font-size: 12px;
    min-height: 36px;
  }

  .mj-stats {
    order: 2;
    flex-basis: auto;
    gap: 10px;
    margin-left: auto;
  }

  .mj-wrap {
    width: calc(100% - 20px);
    padding-top: 16px;
  }

  .mj-heading {
    margin-bottom: 12px;
  }

  .mj-viewport {
    --mj-tile-size: min(var(--mj-tile-max), calc((100vw - 22px) * 56 / var(--mj-board-w, 920)), calc(var(--mj-board-room, calc(100svh - 180px)) * 56 / var(--mj-board-h, 600)));
    border-radius: 8px;
  }
}

/* Upright phones: center the table and status in the height below the heading. */
@media (orientation: portrait) and (max-width: 600px) {
  body {
    display: flex;
    flex-direction: column;
    min-height: 100svh;
  }

  .mj-wrap {
    display: flex;
    flex: 1 0 auto;
    flex-direction: column;
    padding-bottom: max(16px, env(safe-area-inset-bottom));
  }

  .mj-viewport {
    margin-top: auto;
  }

  .mj-status {
    margin-bottom: auto;
  }

  .mj-instructions {
    margin-top: 10px;
  }
}

@media (orientation: portrait) and (max-width: 600px) and (max-height: 700px) {
  .mj-wrap {
    padding-top: 10px;
  }

  .mj-heading {
    margin-bottom: 8px;
  }

  .mj-status {
    margin-top: 10px;
  }
}

/* Put controls beside the table on landscape phones, not above it. */
@media (orientation: landscape) and (max-height: 600px) and (pointer: coarse) {
  body {
    --mj-sidebar-w: 160px;
    --mj-inset-left: max(8px, env(safe-area-inset-left));
    --mj-inset-right: max(8px, env(safe-area-inset-right));
    --mj-inset-top: max(8px, env(safe-area-inset-top));
    --mj-inset-bottom: max(8px, env(safe-area-inset-bottom));
    display: grid;
    grid-template-columns: var(--mj-sidebar-w) minmax(0, 1fr);
    grid-template-rows: auto auto minmax(0, 1fr);
    gap: 8px;
    height: 100svh;
    min-height: 0;
    padding: var(--mj-inset-top) var(--mj-inset-right) var(--mj-inset-bottom) var(--mj-inset-left);
  }

  .mj-wrap {
    display: contents;
  }

  .mj-topbar {
    grid-column: 1;
    grid-row: 1;
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    padding: 8px;
    border-radius: 8px;
  }

  .mj-topbar .back {
    align-self: flex-start;
    padding: 4px;
    font-size: 12px;
  }

  .mj-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    order: 0;
    flex-basis: auto;
    gap: 4px;
  }

  .mj-actions button {
    min-width: 0;
    min-height: 36px;
    padding: 6px 4px;
    font-size: 12px;
    overflow-wrap: anywhere;
  }

  .mj-stats {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    order: 0;
    flex-basis: auto;
    gap: 6px;
    margin: 0;
    padding: 0;
  }

  .mj-stat {
    gap: 4px;
    font-size: 12px;
  }

  .mj-clock {
    grid-column: 1 / -1;
    justify-content: center;
  }

  .mj-heading {
    grid-column: 1;
    grid-row: 2;
    margin: 0;
  }

  .mj-heading h1,
  .mj-instructions {
    display: none;
  }

  .mj-layout-choices {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    width: 100%;
    gap: 4px;
  }

  .mj-layout-choices button {
    min-width: 0;
    min-height: 36px;
    padding: 6px 4px;
    font-size: 12px;
    overflow-wrap: anywhere;
  }

  .mj-status {
    grid-column: 1;
    grid-row: 3;
    min-height: 0;
    margin: 0;
    overflow-y: auto;
    font-size: 12px;
    line-height: 1.4;
  }

  .mj-viewport {
    --mj-tile-size: min(var(--mj-tile-max), calc((100vw - var(--mj-inset-left) - var(--mj-inset-right) - var(--mj-sidebar-w) - 10px) * 56 / var(--mj-board-w, 920)), calc((100svh - var(--mj-inset-top) - var(--mj-inset-bottom) - 2px) * 56 / 600));
    grid-column: 2;
    grid-row: 1 / 4;
    align-self: center;
    justify-self: center;
    margin: 0;
  }
}
