:root {
  --sky: #76d9ff;
  --sky-deep: #27a9f2;
  --grass: #62c96b;
  --leaf: #2eab6d;
  --sun: #ffd765;
  --pink: #ff86bd;
  --coral: #ff8c6b;
  --ink: #25405f;
  --muted: #64748b;
  --panel: rgba(255, 255, 255, 0.9);
  --line: rgba(37, 64, 95, 0.13);
  --shadow: 0 18px 40px rgba(34, 80, 130, 0.16);
  --radius: 22px;
  --card-width: 76px;
  --cell-gap: 7px;
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  color: var(--ink);
  font-family: "Microsoft JhengHei", "PingFang TC", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: linear-gradient(180deg, #79dcff 0%, #c7f3ff 48%, #dff9dc 49%, #f7fff5 100%);
  overflow-x: hidden;
}

button,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

img {
  display: block;
  max-width: 100%;
  -webkit-user-drag: none;
  user-select: none;
}

.sky-scene {
  position: fixed;
  inset: 0;
  z-index: -1;
  overflow: hidden;
  pointer-events: none;
}

.cloud {
  position: absolute;
  width: 124px;
  height: 42px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.86);
  box-shadow:
    32px -14px 0 10px rgba(255, 255, 255, 0.82),
    70px 0 0 2px rgba(255, 255, 255, 0.78);
}

.cloud-one {
  top: 90px;
  left: 8vw;
}

.cloud-two {
  top: 150px;
  right: 12vw;
  transform: scale(0.78);
}

.hill {
  position: absolute;
  bottom: -130px;
  width: 520px;
  height: 260px;
  border-radius: 50% 50% 0 0;
  background: linear-gradient(145deg, #64c75f, #2fb174);
}

.hill-one {
  left: -90px;
}

.hill-two {
  right: -110px;
  bottom: -150px;
  background: linear-gradient(145deg, #ffe071, #6fd067 45%, #38b978);
}

.town {
  position: absolute;
  bottom: 0;
  width: 170px;
  height: 74px;
  border-radius: 18px 18px 0 0;
  background:
    linear-gradient(90deg, transparent 16px, rgba(255, 255, 255, 0.5) 16px 32px, transparent 32px 54px, rgba(255, 255, 255, 0.5) 54px 70px, transparent 70px),
    linear-gradient(180deg, #ffb45f 0 42%, #5ab0df 42%);
  box-shadow: inset 0 9px 0 rgba(255, 255, 255, 0.45);
}

.town::before {
  content: "";
  position: absolute;
  left: 0;
  top: -24px;
  width: 100%;
  height: 35px;
  clip-path: polygon(0 80%, 18% 24%, 36% 80%, 54% 24%, 72% 80%, 90% 24%, 100% 80%);
  background: #ff7f75;
}

.town-one {
  left: 9vw;
}

.town-two {
  right: 10vw;
  width: 138px;
  transform: scale(0.9);
}

.app-shell {
  width: min(1120px, calc(100% - 28px));
  min-height: 100vh;
  margin: 0 auto;
  padding: 18px 0 36px;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px;
  border: 1px solid rgba(255, 255, 255, 0.74);
  border-radius: 26px;
  background: rgba(255, 255, 255, 0.72);
  box-shadow: var(--shadow);
  backdrop-filter: blur(14px);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 56px;
  height: 56px;
  flex: none;
  border: 3px solid #fff;
  border-radius: 20px;
  color: #fff;
  background: linear-gradient(135deg, var(--sky-deep), #33c8b4);
  box-shadow: 0 8px 0 rgba(43, 111, 154, 0.14);
  font-size: 28px;
  font-weight: 900;
}

h1,
h2,
p {
  margin: 0;
  overflow-wrap: anywhere;
}

h1 {
  font-size: clamp(1.55rem, 4vw, 2.45rem);
  line-height: 1.08;
  letter-spacing: 0;
}

.brand p {
  margin-top: 4px;
  color: #1579ad;
  font-size: 0.88rem;
  font-weight: 900;
}

h2 {
  color: var(--ink);
  font-size: 1.08rem;
  line-height: 1.15;
}

.language-switch {
  display: inline-flex;
  gap: 6px;
  padding: 6px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.54);
}

.language-button {
  min-width: 60px;
  min-height: 44px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: #40617f;
  font-weight: 900;
  transition: transform 160ms ease, box-shadow 160ms ease, background 160ms ease;
}

.language-button.is-active {
  color: #fff;
  background: linear-gradient(135deg, var(--sky-deep), #33c8b4);
  box-shadow: 0 8px 18px rgba(39, 169, 242, 0.24);
}

.workspace {
  display: grid;
  gap: 14px;
  padding: 22px 0 0;
}

.quick-link-band {
  margin-top: 14px;
  padding: 14px;
  border: 1px solid rgba(255, 255, 255, 0.72);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
  backdrop-filter: blur(14px);
}

.quick-links {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
}

.quick-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 42px;
  padding: 0 16px;
  border: 1px solid rgba(37, 64, 95, 0.08);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.95);
  box-shadow: 0 4px 6px rgba(34, 80, 130, 0.1);
  color: #25405f;
  font-size: 0.9rem;
  font-weight: 900;
  line-height: 1.2;
  text-align: center;
  text-decoration: none;
  transition: transform 180ms ease, box-shadow 180ms ease, background 180ms ease;
}

.quick-link:hover,
.quick-link:focus-visible {
  background: #fff;
  box-shadow: 0 7px 12px rgba(34, 80, 130, 0.16);
  transform: translateY(-2px);
}

.quick-link:focus-visible {
  outline: 3px solid rgba(39, 169, 242, 0.35);
  outline-offset: 2px;
}

.app-footer {
  margin-top: 22px;
  padding: 18px;
  border: 1px solid rgba(255, 255, 255, 0.72);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.76);
  box-shadow: var(--shadow);
  backdrop-filter: blur(14px);
  color: #40617f;
  text-align: center;
}

.footer-brand {
  color: var(--ink);
  font-size: 1rem;
  font-weight: 900;
  letter-spacing: 0.08em;
}

.app-footer p {
  margin-top: 6px;
  color: #64748b;
  font-size: 0.88rem;
  font-weight: 800;
}

.control-band,
.card-panel,
.board-panel {
  border: 1px solid rgba(255, 255, 255, 0.72);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
  backdrop-filter: blur(14px);
}

.control-band {
  display: grid;
  grid-template-columns: minmax(120px, 1fr) minmax(120px, 1fr) minmax(150px, 0.9fr) minmax(130px, 0.7fr);
  gap: 12px;
  align-items: end;
  padding: 18px;
}

.field {
  display: grid;
  gap: 8px;
  color: #315878;
  font-size: 0.85rem;
  font-weight: 900;
}

select {
  width: 100%;
  min-height: 50px;
  padding: 0 15px;
  border: 2px solid rgba(62, 129, 178, 0.16);
  border-radius: 18px;
  color: var(--ink);
  background: #fff;
  outline: 0;
  font-weight: 900;
}

select:focus,
button:focus-visible,
.board-cell:focus-visible {
  outline: 0;
  border-color: var(--sky-deep);
  box-shadow: 0 0 0 4px rgba(39, 169, 242, 0.14);
}

.primary-action,
.secondary-action,
.history-button {
  border: 0;
  border-radius: 999px;
  font-weight: 900;
  transition: transform 160ms ease, box-shadow 160ms ease, background 160ms ease;
}

.primary-action {
  min-height: 50px;
  padding: 0 20px;
  color: #fff;
  background: linear-gradient(135deg, #ff8e5e, #ff69ad);
  box-shadow: 0 10px 0 rgba(206, 88, 98, 0.2);
}

.secondary-action {
  min-height: 50px;
  padding: 0 18px;
  color: #275b80;
  background: #fff;
  box-shadow: 0 10px 0 rgba(58, 135, 186, 0.14);
}

.primary-action:hover,
.secondary-action:hover,
.history-button:hover,
.card-thumb:hover {
  transform: translateY(-2px);
}

.notice {
  color: #d64a79;
  font-size: 0.95rem;
  font-weight: 900;
}

.notice:empty {
  display: none;
}

.notice[data-tone="success"] {
  color: #1579ad;
}

.tool-surface {
  display: grid;
  grid-template-columns: minmax(190px, 260px) minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}

.card-panel,
.board-panel {
  min-width: 0;
  padding: 18px;
}

.card-panel {
  position: sticky;
  top: 16px;
}

.panel-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.panel-heading span,
#boardStats {
  color: var(--muted);
  font-size: 0.92rem;
  font-weight: 900;
}

.panel-note {
  margin-top: 6px;
  color: #51708a;
  font-size: 0.82rem;
  font-weight: 800;
  line-height: 1.35;
}

.board-heading {
  align-items: start;
}

.history-actions {
  display: inline-flex;
  gap: 8px;
}

.history-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 92px;
  height: 42px;
  padding: 0 14px;
  color: #275b80;
  background: #fff;
  box-shadow: 0 8px 0 rgba(58, 135, 186, 0.12);
  line-height: 1;
  white-space: nowrap;
}

.history-icon {
  font-size: 21px;
}

.button-label {
  font-size: 0.86rem;
  font-weight: 900;
}

.card-gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(66px, 1fr));
  gap: 10px;
}

.card-thumb {
  position: relative;
  display: grid;
  place-items: center;
  min-height: 0;
  padding: 0;
  border: 3px solid #fff;
  border-radius: 20px;
  background: #fff;
  box-shadow: 0 8px 0 rgba(50, 106, 154, 0.1);
  aspect-ratio: 113 / 128;
  touch-action: none;
  transition: transform 160ms ease, box-shadow 160ms ease;
}

.card-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 16px;
}

.card-thumb.is-selected {
  border-color: var(--sun);
  box-shadow:
    0 8px 0 rgba(255, 181, 71, 0.2),
    0 0 0 5px rgba(255, 215, 101, 0.35);
}

.card-thumb.is-selected::after {
  content: "";
  position: absolute;
  inset: -7px;
  border: 3px solid rgba(255, 134, 189, 0.45);
  border-radius: 24px;
  pointer-events: none;
  animation: selected-card-glow 1.4s ease-in-out infinite;
}

.board-scroll {
  width: 100%;
  min-height: 390px;
  max-height: calc(100vh - 260px);
  overflow: auto;
  padding: 8px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.56);
}

.board-empty {
  display: grid;
  place-items: center;
  min-height: 360px;
  border: 2px dashed rgba(48, 108, 152, 0.24);
  border-radius: 18px;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.55);
  font-weight: 900;
  text-align: center;
}

.board-empty[hidden] {
  display: none;
}

.board-shell {
  display: grid;
  grid-template-columns: max-content 18px;
  grid-template-rows: 18px max-content;
  gap: 4px 6px;
  width: max-content;
  margin: 0 auto;
}

.axis {
  display: grid;
  color: #51708a;
  font-size: 0.78rem;
  font-weight: 900;
}

.axis-top {
  grid-column: 1;
  grid-row: 1;
  grid-template-columns: repeat(var(--cols), var(--card-width));
  gap: var(--cell-gap);
}

.axis-side {
  grid-column: 2;
  grid-row: 2;
  grid-template-rows: repeat(var(--rows), calc(var(--card-width) * 128 / 113));
  gap: var(--cell-gap);
}

.axis span {
  display: grid;
  place-items: center;
}

.board-grid {
  grid-column: 1;
  grid-row: 2;
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--card-width));
  grid-auto-rows: calc(var(--card-width) * 128 / 113);
  gap: var(--cell-gap);
}

.board-cell {
  position: relative;
  display: grid;
  place-items: center;
  width: var(--card-width);
  aspect-ratio: 113 / 128;
  border: 2px solid rgba(62, 129, 178, 0.16);
  border-radius: 18px;
  background: #fff;
  box-shadow: 0 7px 0 rgba(50, 106, 154, 0.08);
  cursor: pointer;
  overflow: hidden;
  transition: transform 160ms ease, border-color 160ms ease, box-shadow 160ms ease, background 160ms ease;
}

.board-cell::before {
  content: "";
  position: absolute;
  inset: 10px;
  border: 2px dashed rgba(48, 108, 152, 0.18);
  border-radius: 13px;
}

.board-cell img {
  position: absolute;
  inset: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.board-cell.has-card {
  border-color: #fff;
}

.board-cell.has-card::before {
  display: none;
}

.board-cell.is-selected {
  transform: translateY(-2px);
  box-shadow:
    0 7px 0 rgba(50, 106, 154, 0.08),
    0 0 0 5px rgba(255, 215, 101, 0.35);
}

.board-cell.is-matched {
  animation: match-pop 460ms ease forwards;
  box-shadow: 0 0 0 5px rgba(39, 169, 242, 0.2);
}

.board-cell.is-cleared {
  background: linear-gradient(145deg, rgba(255, 255, 255, 0.86), rgba(239, 255, 244, 0.82));
  border-color: rgba(48, 108, 152, 0.18);
}

.board-cell.is-cleared::before {
  content: "✓";
  display: grid;
  place-items: center;
  inset: 0;
  border: 0;
  color: #38b978;
  font-size: 1.45rem;
  font-weight: 900;
}

.board-cell.drag-over {
  border-color: var(--sky-deep);
  box-shadow: 0 0 0 5px rgba(39, 169, 242, 0.18);
}

.remove-cell {
  position: absolute;
  top: 4px;
  right: 4px;
  z-index: 2;
  display: grid;
  place-items: center;
  width: 23px;
  height: 23px;
  padding: 0;
  border: 2px solid #fff;
  border-radius: 50%;
  color: #fff;
  background: linear-gradient(135deg, var(--pink), var(--coral));
  font-size: 15px;
  font-weight: 900;
  line-height: 1;
}

.card-flight-clone {
  position: fixed;
  z-index: 999;
  pointer-events: none;
  border: 3px solid #fff;
  border-radius: 20px;
  box-shadow: 0 18px 34px rgba(37, 64, 95, 0.22);
  object-fit: cover;
  will-change: transform, opacity;
}

.touch-drag-clone {
  position: fixed;
  z-index: 1000;
  pointer-events: none;
  border: 3px solid #fff;
  border-radius: 20px;
  box-shadow: 0 18px 34px rgba(37, 64, 95, 0.24);
  opacity: 0.9;
  object-fit: cover;
  transform: translate(-50%, -50%) scale(1.05);
}

body.is-touch-dragging,
body.is-touch-dragging * {
  cursor: grabbing;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(37, 64, 95, 0.34);
  backdrop-filter: blur(6px);
}

.modal[hidden] {
  display: none;
}

.modal-dialog {
  display: grid;
  gap: 14px;
  width: min(420px, 100%);
  padding: 22px;
  border: 1px solid rgba(255, 255, 255, 0.72);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
}

.modal-dialog h2 {
  font-size: 1.35rem;
}

.modal-dialog p {
  color: var(--muted);
  line-height: 1.7;
}

.modal-actions {
  display: flex;
  justify-content: end;
  gap: 10px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@keyframes match-pop {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  42% {
    transform: scale(1.06);
    opacity: 1;
  }
  100% {
    transform: scale(0.8);
    opacity: 0;
  }
}

@keyframes selected-card-glow {
  0%,
  100% {
    opacity: 0.32;
    transform: scale(0.98);
  }
  50% {
    opacity: 0.85;
    transform: scale(1.03);
  }
}

@media (max-width: 860px) {
  :root {
    --card-width: 68px;
  }

  .tool-surface {
    grid-template-columns: 1fr;
  }

  .tool-surface.is-play-mode-shell {
    min-height: var(--play-shell-height, 100svh);
  }

  .card-panel {
    position: sticky;
    top: 8px;
    z-index: 12;
  }

  .card-panel.is-stuck {
    position: fixed;
    top: 8px;
    left: 9px;
    right: 9px;
    max-width: 1120px;
    margin: 0 auto;
    padding-top: 10px;
    padding-bottom: 10px;
    border-radius: 0 0 22px 22px;
  }

  .card-panel.is-stuck .panel-heading {
    display: none;
  }

  .board-panel.is-play-mode {
    position: fixed;
    top: var(--fixed-board-top, 150px);
    right: 9px;
    bottom: 0;
    left: 9px;
    z-index: 11;
    display: flex;
    width: auto;
    min-height: 0;
    height: auto;
    margin-top: 0;
    overflow: hidden;
    flex-direction: column;
  }

  .board-panel.is-play-mode .board-scroll {
    flex: 1;
    min-height: 0;
    max-height: none;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .board-panel.is-play-mode .board-shell {
    --card-width: var(--fit-card-width, 68px);
  }
}

@media (max-width: 640px) {
  :root {
    --card-width: 58px;
    --cell-gap: 5px;
  }

  .app-shell {
    width: min(100% - 18px, 1120px);
    padding-top: 9px;
  }

  .app-header {
    align-items: stretch;
    flex-direction: column;
  }

  .quick-link-band {
    margin-top: 10px;
    padding: 12px;
    border-radius: 18px;
  }

  .quick-links {
    gap: 7px;
  }

  .quick-link {
    flex: 1 1 100%;
    min-height: 40px;
    padding: 0 12px;
  }

  .app-footer {
    margin-top: 16px;
    padding: 15px;
    border-radius: 18px;
  }

  .brand-mark {
    width: 50px;
    height: 50px;
    border-radius: 18px;
  }

  .language-switch {
    justify-content: stretch;
  }

  .language-button {
    flex: 1;
  }

  .control-band {
    grid-template-columns: 1fr 1fr;
    padding: 14px;
    border-radius: 18px;
  }

  .primary-action,
  .secondary-action {
    width: 100%;
  }

  .card-panel,
  .board-panel {
    padding: 14px;
    border-radius: 18px;
  }

  .card-panel {
    padding: 12px;
  }

  .card-panel .panel-heading {
    margin-bottom: 8px;
  }

  .panel-note {
    margin-top: 4px;
    font-size: 0.76rem;
  }

  .card-gallery {
    grid-template-columns: repeat(5, minmax(0, 52px));
    justify-content: center;
    gap: 8px;
  }

  .card-thumb {
    border-width: 2px;
    border-radius: 15px;
    box-shadow: 0 6px 0 rgba(50, 106, 154, 0.08);
  }

  .card-thumb img {
    border-radius: 12px;
  }

  .board-scroll {
    min-height: 350px;
    max-height: 64vh;
  }

  .board-empty {
    min-height: 320px;
  }

  .history-button {
    min-width: 78px;
    padding: 0 10px;
  }
}

@media (max-width: 420px) {
  :root {
    --card-width: 52px;
  }

  h1 {
    font-size: 1.55rem;
  }

  .brand {
    gap: 9px;
  }

  .history-actions {
    gap: 6px;
  }

  .button-label {
    font-size: 0.78rem;
  }

  .card-gallery {
    grid-template-columns: repeat(5, minmax(0, 48px));
    gap: 7px;
  }
}
