/* ============================================================
   Matchstick Math Puzzles
   ============================================================ */

/* --- Level select, matched to Parking Jam's level picker --- */
#level-select-view {
  width: 100%;
}

.rh-intro {
  color: var(--ink-soft);
  font-weight: 600;
  margin: 0 0 22px;
  text-align: center;
}

.level-grid {
  display: grid;
  gap: 10px;
  grid-template-columns: repeat(auto-fill, minmax(58px, 1fr));
  margin: 0 auto;
  max-width: 620px;
}

.level-btn {
  align-items: center;
  aspect-ratio: 1;
  background: var(--surface);
  border: 2px solid var(--ink);
  border-radius: var(--radius-sm);
  box-shadow: 0 4px 0 rgba(34, 49, 74, 0.12);
  color: var(--ink);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  font-family: var(--font-display);
  font-size: 1.05rem;
  font-weight: 700;
  gap: 2px;
  justify-content: center;
  padding: 0;
  position: relative;
  transition: transform 0.1s ease;
}
.level-btn:hover:not(:disabled) {
  transform: translateY(-2px);
}
.level-btn:active:not(:disabled) {
  box-shadow: 0 1px 0 rgba(34, 49, 74, 0.12);
  transform: translateY(2px);
}
.level-btn.active {
  background: var(--yellow);
  border-color: var(--ink);
}
.level-btn.completed {
  background: var(--simon-green);
  border-color: var(--ink);
  color: #fff;
}
.level-btn .best {
  font-family: var(--font-body);
  font-size: 0.55rem;
  font-weight: 800;
  opacity: 0.9;
}
.level-btn .diff-dot {
  border-radius: 50%;
  height: 7px;
  position: absolute;
  right: 6px;
  top: 5px;
  width: 7px;
}
.diff-easy { background: var(--simon-green); }
.diff-medium { background: var(--yellow-deep); }
.diff-hard { background: var(--pink-deep); }
.diff-expert { background: var(--purple-deep); }

.rh-legend {
  color: var(--ink-soft);
  display: flex;
  flex-wrap: wrap;
  font-size: 0.8rem;
  font-weight: 700;
  gap: 18px;
  justify-content: center;
  margin: 20px 0 6px;
}
.rh-legend span {
  align-items: center;
  display: inline-flex;
  gap: 6px;
}
.rh-legend .dot {
  border-radius: 50%;
  display: inline-block;
  height: 9px;
  width: 9px;
}

#game-play-view:not([hidden]) {
  align-items: center;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  max-width: 100%;
  min-width: 0;
  position: relative;
  width: 100%;
}

/* --- Gameplay header --- */
.rh-play-header {
  align-items: center;
  box-sizing: border-box;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: space-between;
  margin-bottom: 6px;
  max-width: 100%;
  min-width: 0;
  width: 100%;
}
.rh-play-header .pill-btn {
  flex: 0 1 auto;
}
.rh-level-title {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  font-family: var(--font-display);
  font-size: 1.1rem;
  font-weight: 700;
  gap: 8px;
  justify-content: center;
  max-width: 100%;
  min-width: 0;
  overflow-wrap: anywhere;
  text-align: center;
}
.difficulty-chip {
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--ink-soft);
  font-family: var(--font-body);
  font-size: 0.7rem;
  font-weight: 800;
  letter-spacing: 0.4px;
  max-width: 100%;
  overflow-wrap: anywhere;
  padding: 3px 10px;
  text-transform: uppercase;
}

.mp-status {
  box-sizing: border-box;
  color: var(--ink-soft);
  display: block;
  font-family: var(--font-display);
  font-size: 1.08rem;
  font-weight: 700;
  margin: 14px 0 0;
  min-height: 1.4em;
  max-width: 100%;
  min-width: 0;
  overflow-wrap: anywhere;
  text-align: center;
  width: 100%;
}

#check-btn {
  background: var(--yellow);
  border-color: var(--ink);
  box-shadow: 0 5px 0 rgba(34, 49, 74, 0.2);
  color: var(--ink);
  font-size: 0.95rem;
  padding: 9px 18px;
}
#check-btn:hover {
  filter: brightness(1.04);
}

#game-play-view .scoreboard {
  align-items: stretch;
  width: 100%;
}

@media (min-width: 561px) {
  #game-play-view .scoreboard {
    flex-wrap: nowrap;
  }
  #game-play-view .score-chip,
  #game-play-view .mp-removed-tray {
    flex: 0 0 auto;
  }
}

.mp-equation-wrap {
  box-sizing: border-box;
  max-width: 100%;
  margin-top: 8px;
  min-width: 0;
  overflow-x: clip;
  overflow-y: visible;
  position: relative;
  width: 100%;
}

.mp-equation {
  align-items: center;
  display: flex;
  gap: clamp(10px, 2.2vw, 22px);
  justify-content: center;
  max-width: 100%;
  overflow-x: auto;
  padding: 4px 4px 84px;
}
.mp-equation-wrap.is-celebrating .mp-equation {
  animation: mp-equation-win 1.2s ease both;
}

.mp-celebration {
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  position: absolute;
  z-index: 6;
}
.mp-celebration::before {
  animation: mp-win-ring 1.2s ease-out both;
  border: 5px solid rgba(53, 196, 184, 0.72);
  border-radius: 50%;
  content: '';
  height: 42px;
  left: 50%;
  position: absolute;
  top: 50%;
  transform: translate(-50%, -50%) scale(0.2);
  width: 42px;
}
.mp-spark {
  --spark-color: var(--yellow);
  animation: mp-spark-pop 1.15s ease-out both;
  background: var(--spark-color);
  border: 2px solid rgba(34, 49, 74, 0.22);
  border-radius: 50%;
  box-shadow: 0 2px 0 rgba(34, 49, 74, 0.14);
  height: 12px;
  left: var(--spark-x);
  position: absolute;
  top: var(--spark-y);
  transform: translate(-50%, -50%) scale(0);
  width: 12px;
}

@keyframes mp-equation-win {
  0% { transform: scale(1); }
  28% { transform: scale(1.05); }
  54% { transform: scale(0.99); }
  78% { transform: scale(1.02); }
  100% { transform: scale(1); }
}

@keyframes mp-win-ring {
  0% {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.2);
  }
  18% { opacity: 0.95; }
  100% {
    opacity: 0;
    transform: translate(-50%, -50%) scale(8);
  }
}

@keyframes mp-spark-pop {
  0% {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0);
  }
  18% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1.1);
  }
  100% {
    opacity: 0;
    transform: translate(calc(-50% + var(--spark-dx)), calc(-50% + var(--spark-dy))) scale(0.55);
  }
}

.mp-symbol {
  --match-on: #c9822f;
  --match-off: rgba(34, 49, 74, 0.08);
  --match-tip: #a92e27;
  flex: 0 0 auto;
  height: 140px;
  position: relative;
  width: 84px;
}
.mp-symbol.is-sign {
  width: 72px;
}
.mp-symbol.is-selected-sign {
  filter: drop-shadow(0 0 8px rgba(255, 201, 61, 0.55));
}

@media (min-width: 561px) {
  .mp-equation[data-symbol-count="8"] {
    gap: 14px;
    overflow-x: hidden;
  }
  .mp-equation[data-symbol-count="9"] {
    gap: 12px;
    overflow-x: hidden;
  }
  .mp-equation[data-symbol-count="10"] {
    gap: 10px;
    overflow-x: hidden;
  }
  .mp-equation[data-symbol-count="9"] .mp-symbol,
  .mp-equation[data-symbol-count="10"] .mp-symbol {
    transform: scale(var(--mp-desktop-symbol-scale));
    transform-origin: top left;
  }
  .mp-equation[data-symbol-count="9"] .mp-symbol {
    --mp-desktop-symbol-scale: 0.96;
    height: calc(140px * var(--mp-desktop-symbol-scale));
    width: calc(84px * var(--mp-desktop-symbol-scale));
  }
  .mp-equation[data-symbol-count="10"] .mp-symbol {
    --mp-desktop-symbol-scale: 0.9;
    height: calc(140px * var(--mp-desktop-symbol-scale));
    width: calc(84px * var(--mp-desktop-symbol-scale));
  }
  .mp-equation[data-symbol-count="9"] .mp-symbol.is-sign {
    width: calc(72px * var(--mp-desktop-symbol-scale));
  }
  .mp-equation[data-symbol-count="10"] .mp-symbol.is-sign {
    width: calc(72px * var(--mp-desktop-symbol-scale));
  }
}

.mp-match {
  appearance: none;
  background: var(--match-off);
  border: 2px dashed rgba(34, 49, 74, 0.72);
  border-radius: 999px;
  cursor: pointer;
  padding: 0;
  position: absolute;
  touch-action: none;
  transition: background 0.1s ease, border-color 0.1s ease, box-shadow 0.1s ease, transform 0.08s ease, opacity 0.08s ease;
  z-index: 0;
}
.mp-match:hover:not(:disabled) {
  transform: scale(1.04);
}
.mp-match.is-on {
  background:
    linear-gradient(90deg, rgba(255, 244, 204, 0.62), transparent 28%, transparent 72%, rgba(72, 41, 18, 0.2)),
    var(--match-on);
  border-color: var(--ink);
  border-style: solid;
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.16), 0 4px 0 rgba(34, 49, 74, 0.15);
  z-index: 2;
}
.mp-match.is-on::after {
  background:
    radial-gradient(circle at 35% 35%, #e86f5f 0 18%, transparent 19%),
    var(--match-tip);
  border: 1px solid rgba(34, 49, 74, 0.42);
  border-radius: 999px;
  content: '';
  position: absolute;
}
.mp-match.is-horizontal.is-on::after {
  height: 20px;
  right: -9px;
  top: 50%;
  transform: translateY(-50%);
  width: 20px;
}
.mp-match.is-vertical.is-on::after {
  height: 20px;
  left: 50%;
  top: -9px;
  transform: translateX(-50%);
  width: 20px;
}
.mp-match.is-added {
  outline: 3px solid rgba(47, 191, 90, 0.28);
  outline-offset: 1px;
}
.mp-match.is-dragging {
  cursor: grabbing;
  opacity: 0.72;
  z-index: 8;
}
.mp-match.is-removed {
  opacity: 0.42;
  z-index: 0;
}
.mp-match:disabled {
  cursor: default;
}
.mp-sign .mp-match:not(.is-on) {
  opacity: 0;
  pointer-events: none;
}
.mp-sign-picker {
  align-items: center;
  background: var(--surface);
  border: 2px solid var(--ink);
  border-radius: 10px;
  box-shadow: 0 5px 0 rgba(34, 49, 74, 0.14);
  display: grid;
  gap: 5px;
  grid-template-columns: repeat(2, 1fr);
  left: 50%;
  padding: 5px;
  position: absolute;
  top: calc(100% - 12px);
  transform: translateX(-50%);
  z-index: 12;
}
.mp-sign-picker-btn {
  align-items: center;
  background: transparent;
  border: 0;
  border-radius: 7px;
  color: var(--ink-soft);
  cursor: pointer;
  display: inline-flex;
  font-family: var(--font-display);
  font-size: 1rem;
  font-weight: 800;
  height: 36px;
  justify-content: center;
  min-width: 40px;
  padding: 0 10px;
}
.mp-sign-picker-btn.active {
  background: var(--yellow);
  color: var(--ink);
}

.mp-digit .seg-a,
.mp-digit .seg-g,
.mp-digit .seg-d {
  height: 17px;
  left: 19px;
  width: 46px;
}
.mp-digit .seg-a { top: 4px; }
.mp-digit .seg-g { top: 61px; }
.mp-digit .seg-d { top: 119px; }
.mp-digit .seg-b,
.mp-digit .seg-c,
.mp-digit .seg-e,
.mp-digit .seg-f {
  height: 50px;
  width: 17px;
}
.mp-digit .seg-b { left: 63px; top: 15px; }
.mp-digit .seg-c { left: 63px; top: 73px; }
.mp-digit .seg-e { left: 4px; top: 73px; }
.mp-digit .seg-f { left: 4px; top: 15px; }

.mp-sign .seg-h,
.mp-sign .seg-top,
.mp-sign .seg-bottom {
  height: 17px;
  left: 13px;
  width: 46px;
}
.mp-sign .seg-h { top: 61px; }
.mp-sign .seg-v {
  height: 50px;
  left: 28px;
  top: 45px;
  width: 17px;
}
.mp-sign .seg-x,
.mp-sign .seg-y {
  height: 17px;
  left: 13px;
  top: 61px;
  transform-origin: center;
  width: 46px;
}
.mp-sign .seg-x { transform: rotate(42deg); }
.mp-sign .seg-y { transform: rotate(-42deg); }
.mp-sign .seg-x:hover:not(:disabled) { transform: rotate(42deg) scale(1.04); }
.mp-sign .seg-y:hover:not(:disabled) { transform: rotate(-42deg) scale(1.04); }
.mp-sign .seg-x.is-on::after,
.mp-sign .seg-y.is-on::after {
  height: 20px;
  left: auto;
  right: -9px;
  top: 50%;
  transform: translateY(-50%);
  width: 20px;
}
.mp-sign .seg-top { top: 50px; }
.mp-sign .seg-bottom { top: 75px; }

.mp-removed-tray {
  align-items: center;
  background: rgba(255, 255, 255, 0.92);
  border: 2px solid var(--ink);
  border-radius: 10px;
  box-shadow: 0 5px 0 rgba(34, 49, 74, 0.14);
  display: inline-flex;
  gap: 10px;
  justify-content: center;
  margin: 0;
  max-width: 100%;
  min-height: 62px;
  min-width: 130px;
  padding: 8px 12px;
}
.mp-removed-tray[hidden] {
  display: none;
}
.mp-removed-label {
  color: var(--ink-soft);
  font-family: var(--font-display);
  font-size: 0.78rem;
  font-weight: 800;
  text-transform: uppercase;
}
.mp-removed-list {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}
.mp-tray-match {
  background:
    linear-gradient(180deg, rgba(255, 244, 204, 0.62), transparent 28%, transparent 72%, rgba(72, 41, 18, 0.2)),
    #c9822f;
  border: 2px solid var(--ink);
  border-radius: 999px;
  box-shadow: inset -2px 0 0 rgba(0, 0, 0, 0.14), 0 2px 0 rgba(34, 49, 74, 0.12);
  display: inline-block;
  height: 34px;
  position: relative;
  width: 13px;
}
.mp-tray-match::after {
  background:
    radial-gradient(circle at 35% 35%, #e86f5f 0 18%, transparent 19%),
    #a92e27;
  border: 1px solid rgba(34, 49, 74, 0.42);
  border-radius: 999px;
  content: '';
  height: 16px;
  left: 50%;
  position: absolute;
  top: -8px;
  transform: translateX(-50%);
  width: 16px;
}
.mp-tray-match.is-empty {
  background: rgba(34, 49, 74, 0.05);
  border: 2px dashed rgba(34, 49, 74, 0.42);
  box-shadow: none;
}
.mp-tray-match.is-empty::after {
  background: rgba(34, 49, 74, 0.08);
  border-color: rgba(34, 49, 74, 0.32);
}

.mp-symbol-key {
  display: grid;
  gap: 10px;
  grid-template-columns: repeat(auto-fit, minmax(66px, 1fr));
  margin-top: 12px;
  max-width: 680px;
}
.mp-key-item {
  align-items: center;
  background: var(--surface);
  border: 2px solid var(--ink);
  border-radius: 8px;
  box-shadow: 0 4px 0 rgba(34, 49, 74, 0.12);
  color: var(--ink);
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-family: var(--font-display);
  font-size: 0.78rem;
  font-weight: 800;
  justify-content: center;
  min-height: 92px;
  padding: 8px;
}
.mp-key-symbol {
  height: 58px;
  position: relative;
  width: 36px;
}
.mp-key-symbol.is-sign {
  width: 34px;
}
.mp-key-match {
  background:
    linear-gradient(90deg, rgba(255, 244, 204, 0.62), transparent 28%, transparent 72%, rgba(72, 41, 18, 0.2)),
    #c9822f;
  border: 1px solid var(--ink);
  border-radius: 999px;
  box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.14), 0 1px 0 rgba(34, 49, 74, 0.12);
  position: absolute;
}
.mp-key-match::after {
  background:
    radial-gradient(circle at 35% 35%, #e86f5f 0 18%, transparent 19%),
    #a92e27;
  border: 1px solid rgba(34, 49, 74, 0.42);
  border-radius: 999px;
  content: '';
  height: 8px;
  position: absolute;
  width: 8px;
}
.mp-key-match.seg-a,
.mp-key-match.seg-g,
.mp-key-match.seg-d,
.mp-key-match.seg-h,
.mp-key-match.seg-top,
.mp-key-match.seg-bottom,
.mp-key-match.seg-x,
.mp-key-match.seg-y {
  height: 7px;
  width: 22px;
}
.mp-key-match.seg-a { left: 7px; top: 2px; }
.mp-key-match.seg-g { left: 7px; top: 25px; }
.mp-key-match.seg-d { left: 7px; top: 49px; }
.mp-key-match.seg-b,
.mp-key-match.seg-c,
.mp-key-match.seg-e,
.mp-key-match.seg-f,
.mp-key-match.seg-v {
  height: 22px;
  width: 7px;
}
.mp-key-match.seg-b { left: 27px; top: 7px; }
.mp-key-match.seg-c { left: 27px; top: 31px; }
.mp-key-match.seg-e { left: 2px; top: 31px; }
.mp-key-match.seg-f { left: 2px; top: 7px; }
.mp-key-match.seg-h { left: 6px; top: 25px; }
.mp-key-match.seg-v { left: 14px; top: 18px; }
.mp-key-match.seg-top { left: 6px; top: 20px; }
.mp-key-match.seg-bottom { left: 6px; top: 32px; }
.mp-key-match.seg-x,
.mp-key-match.seg-y {
  left: 6px;
  top: 25px;
  transform-origin: center;
}
.mp-key-match.seg-x { transform: rotate(42deg); }
.mp-key-match.seg-y { transform: rotate(-42deg); }
.mp-key-match.seg-a::after,
.mp-key-match.seg-g::after,
.mp-key-match.seg-d::after,
.mp-key-match.seg-h::after,
.mp-key-match.seg-top::after,
.mp-key-match.seg-bottom::after,
.mp-key-match.seg-x::after,
.mp-key-match.seg-y::after {
  right: -4px;
  top: 50%;
  transform: translateY(-50%);
}
.mp-key-match.seg-b::after,
.mp-key-match.seg-c::after,
.mp-key-match.seg-e::after,
.mp-key-match.seg-f::after,
.mp-key-match.seg-v::after {
  left: 50%;
  top: -4px;
  transform: translateX(-50%);
}
.mp-key-caption {
  color: var(--ink-soft);
  line-height: 1;
}

#game-stage:fullscreen,
#game-stage.is-ios-fullscreen {
  border: none;
  border-radius: 0;
  box-sizing: border-box;
  height: 100vh;
  height: 100dvh;
  height: calc(var(--obg-vh, 1vh) * 100);
  justify-content: flex-start;
  max-width: 100vw;
  overflow-y: auto;
  width: 100vw;
}

@media (max-width: 560px) {
  #game-stage {
    box-sizing: border-box;
    max-width: 100%;
    min-height: calc((var(--obg-vh, 1vh) * 100) + 96px);
    overflow-x: hidden;
    padding-bottom: 96px;
    padding-left: 8px;
    padding-right: 8px;
  }
  .rh-play-header {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    row-gap: 8px;
  }
  .rh-level-title {
    font-size: 1rem;
    grid-column: 1 / -1;
    grid-row: 1;
    justify-content: center;
    line-height: 1.2;
    text-align: center;
  }
  .difficulty-chip {
    font-size: 0.66rem;
    letter-spacing: 0.2px;
    padding: 3px 8px;
  }
  .rh-play-header .pill-btn {
    box-sizing: border-box;
    font-size: 0.82rem;
    justify-self: center;
    justify-content: center;
    max-width: min(100%, 220px);
    min-width: 0;
    padding: 8px 10px;
    white-space: normal;
    width: 100%;
  }
  #back-to-levels {
    grid-column: 1;
    grid-row: 2;
  }
  #reset-btn {
    grid-column: 1;
    grid-row: 3;
  }
  .mp-status,
  .instructions {
    align-self: stretch;
    box-sizing: border-box;
    display: block;
    line-height: 1.25;
    max-width: 100%;
    min-width: 0;
    overflow-wrap: anywhere;
    padding-left: 4px;
    padding-right: 4px;
    text-align: center;
    white-space: normal;
    width: 100%;
  }
  .instructions {
    font-size: clamp(0.84rem, 3.8vw, 0.95rem);
    max-inline-size: calc(100vw - 34px);
  }
  .mp-status {
    font-size: clamp(0.9rem, 4vw, 1rem);
    max-inline-size: calc(100vw - 34px);
  }
  .game-controls {
    box-sizing: border-box;
    max-inline-size: calc(100vw - 34px);
    width: 100%;
  }
  .mp-removed-tray {
    margin: 0;
    min-height: 58px;
    min-width: 110px;
    padding: 6px 8px;
  }
  .mp-tray-match {
    height: 30px;
    width: 11px;
  }
  .mp-tray-match::after {
    height: 14px;
    top: -7px;
    width: 14px;
  }
  .mp-equation {
    --mp-symbol-scale: 0.72;
    gap: 4px;
    justify-content: center;
    max-width: 100%;
    overflow: visible;
    padding-bottom: 104px;
    padding-left: 0;
    padding-right: 0;
  }
  .mp-equation .mp-symbol {
    height: calc(140px * var(--mp-symbol-scale));
    transform: scale(var(--mp-symbol-scale));
    transform-origin: top left;
    width: calc(84px * var(--mp-symbol-scale));
  }
  .mp-equation .mp-symbol.is-operator,
  .mp-equation .mp-symbol.is-equals,
  .mp-equation .mp-symbol.is-sign {
    width: calc(72px * var(--mp-symbol-scale));
  }
  .mp-equation[data-symbol-count="6"] {
    --mp-symbol-scale: 0.62;
  }
  .mp-equation[data-symbol-count="7"] {
    --mp-symbol-scale: 0.54;
  }
  .mp-equation[data-symbol-count="8"] {
    --mp-symbol-scale: 0.46;
  }
  .mp-equation[data-symbol-count="9"] {
    --mp-symbol-scale: 0.4;
  }
  .mp-equation[data-symbol-count="10"] {
    --mp-symbol-scale: 0.36;
  }
  .mp-equation-wrap {
    overflow-x: clip;
    overflow-y: visible;
  }
  .mp-sign-picker {
    gap: 6px;
    padding: 6px;
  }
  .mp-sign-picker-btn {
    font-size: 1.08rem;
    height: 42px;
    min-width: 44px;
  }
  .level-grid {
    gap: 8px;
    grid-template-columns: repeat(auto-fill, minmax(46px, 1fr));
  }
  .level-btn {
    font-size: 0.9rem;
  }
}
