:root {
  color-scheme: light;
  font-family: 'Segoe UI', 'Microsoft JhengHei UI', 'PingFang TC', 'Noto Sans TC', sans-serif;
  line-height: 1.5;
  font-weight: 500;
  color: #4b3b2f;
  background: #f7efe3;
  --board-size: min(84vw, 430px);
  --gap: clamp(10px, 2vw, 16px);
  --cell-size: calc((var(--board-size) - (var(--gap) * 5)) / 4);
}

* {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(circle at top, rgba(255, 255, 255, 0.95), rgba(255, 255, 255, 0) 45%),
    linear-gradient(180deg, #fbf8f3 0%, #f4e8d8 100%);
}

button {
  border: 0;
  cursor: pointer;
  font: inherit;
}

button:focus-visible {
  outline: 3px solid rgba(237, 194, 46, 0.45);
  outline-offset: 3px;
}

.app-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 28px 16px 36px;
}

.app-card {
  width: min(100%, 1080px);
  display: grid;
  grid-template-columns: minmax(300px, 1fr) auto;
  gap: 28px;
  align-items: center;
}

.info-panel {
  display: grid;
  gap: 20px;
}

.title-block {
  display: grid;
  gap: 12px;
}

.eyebrow {
  display: inline-flex;
  width: fit-content;
  padding: 6px 12px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.8);
  color: #9c7c47;
  font-size: 0.92rem;
  font-weight: 700;
  letter-spacing: 0.04em;
}

.title-block h1 {
  margin: 0;
  font-size: clamp(3.8rem, 10vw, 5.6rem);
  line-height: 0.88;
  letter-spacing: -0.05em;
  color: #6f5846;
}

.title-block p,
.status-card p,
.controls-card p,
.overlay-card p {
  margin: 0;
  color: #6a5849;
  font-size: 1.02rem;
}

.score-row {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
}

.score-card,
.status-card,
.controls-card {
  border-radius: 24px;
  background: rgba(255, 255, 255, 0.78);
  backdrop-filter: blur(10px);
  box-shadow: 0 18px 40px rgba(125, 95, 61, 0.12);
}

.score-card {
  padding: 16px 14px;
  display: grid;
  gap: 6px;
}

.score-card span {
  font-size: 0.84rem;
  color: #9d8163;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.score-card strong {
  font-size: clamp(1.45rem, 3.5vw, 2rem);
  color: #5a4739;
}

.score-card--accent {
  background: linear-gradient(145deg, #edc850, #edc22e);
}

.score-card--accent span,
.score-card--accent strong {
  color: #fff9f0;
}

.status-card,
.controls-card {
  padding: 20px;
  display: grid;
  gap: 16px;
}

.status-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  width: fit-content;
  min-height: 38px;
  padding: 0 14px;
  border-radius: 999px;
  background: rgba(111, 88, 70, 0.12);
  color: #6f5846;
  font-weight: 700;
}

.status-pill--success {
  background: rgba(112, 185, 77, 0.16);
  color: #497d2a;
}

.status-pill--danger {
  background: rgba(232, 107, 85, 0.16);
  color: #b4503c;
}

.primary-button,
.secondary-button {
  min-height: 46px;
  padding: 0 18px;
  border-radius: 14px;
  font-weight: 700;
  transition: transform 140ms ease, box-shadow 140ms ease, background-color 140ms ease;
}

.primary-button {
  background: linear-gradient(145deg, #8f6f51, #6f5846);
  color: #fffaf4;
  box-shadow: 0 10px 24px rgba(111, 88, 70, 0.2);
}

.primary-button:hover,
.secondary-button:hover {
  transform: translateY(-1px);
}

.secondary-button {
  background: rgba(111, 88, 70, 0.12);
  color: #6f5846;
}

.controls-card h2,
.overlay-card h2 {
  margin: 0;
  font-size: 1.3rem;
  color: #584538;
}

.keycaps {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.keycaps span {
  min-width: 48px;
  min-height: 48px;
  display: grid;
  place-items: center;
  border-radius: 14px;
  background: #fff7ee;
  color: #765c49;
  font-weight: 800;
  box-shadow: inset 0 0 0 1px rgba(145, 111, 78, 0.08);
}

.board-panel-wrapper {
  position: relative;
}

.board-panel {
  position: relative;
  width: var(--board-size);
  height: var(--board-size);
  padding: var(--gap);
  border-radius: 30px;
  background: linear-gradient(180deg, rgba(188, 172, 155, 0.96), rgba(171, 156, 140, 0.96));
  box-shadow: 0 30px 60px rgba(120, 92, 61, 0.24);
  touch-action: none;
  isolation: isolate;
  overflow: hidden;
}

.board-panel:focus-visible {
  outline: 3px solid rgba(237, 194, 46, 0.45);
  outline-offset: 6px;
}

.board-grid {
  position: relative;
  z-index: 0;
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--gap);
}

.board-cell {
  border-radius: 20px;
  background: rgba(238, 228, 218, 0.32);
}

.tile-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 1;
}

.tile {
  position: absolute;
  width: var(--cell-size);
  height: var(--cell-size);
  border-radius: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  letter-spacing: -0.04em;
  box-shadow: 0 12px 25px rgba(114, 86, 57, 0.18);
  transition:
    top 170ms cubic-bezier(0.22, 1, 0.36, 1),
    left 170ms cubic-bezier(0.22, 1, 0.36, 1),
    transform 150ms ease,
    opacity 150ms ease;
  will-change: top, left, transform;
  user-select: none;
}

.tile--new,
.tile--merged {
  animation: tile-pop 150ms cubic-bezier(0.34, 1.56, 0.64, 1);
}

.board-overlay {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(245, 238, 229, 0.72);
  backdrop-filter: blur(8px);
}

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

.overlay-card {
  width: min(100%, 280px);
  border-radius: 24px;
  background: rgba(255, 255, 255, 0.9);
  padding: 22px;
  display: grid;
  gap: 14px;
  text-align: center;
  position: relative;
  z-index: 1;
  box-shadow: 0 18px 36px rgba(118, 92, 71, 0.18);
}

.overlay-actions {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 10px;
}

@keyframes tile-pop {
  0% {
    transform: scale(0.45);
    opacity: 0.4;
  }

  70% {
    transform: scale(1.08);
    opacity: 1;
  }

  100% {
    transform: scale(1);
  }
}

@media (max-width: 900px) {
  .app-card {
    grid-template-columns: 1fr;
    justify-items: center;
  }

  .info-panel {
    width: min(100%, 640px);
  }
}

@media (max-width: 560px) {
  :root {
    --board-size: min(92vw, 420px);
    --gap: 10px;
  }

  .app-shell {
    padding-top: 20px;
  }

  .score-row {
    grid-template-columns: 1fr;
  }

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

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

/* Settings Row */
.settings-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.lang-selector select {
  padding: 8px 12px;
  border-radius: 12px;
  border: 1px solid rgba(111, 88, 70, 0.2);
  background: rgba(255, 255, 255, 0.8);
  color: #5a4739;
  font: inherit;
  font-size: 0.9rem;
  cursor: pointer;
  outline: none;
}

.lang-selector select:focus-visible {
  outline: 3px solid rgba(237, 194, 46, 0.45);
  outline-offset: 2px;
}

.theme-toggle {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.8);
  border: 1px solid rgba(111, 88, 70, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.3rem;
  cursor: pointer;
  transition: transform 140ms ease, background-color 140ms ease;
}

.theme-toggle:hover {
  transform: scale(1.05);
}

.theme-toggle .theme-icon-dark {
  display: none;
}

.theme-toggle .theme-icon-light {
  display: block;
}

/* Dark Mode */
[data-theme="dark"] {
  color-scheme: dark;
  color: #e8ddd0;
  background: #3d3530;
}

[data-theme="dark"] body {
  background:
    radial-gradient(circle at top, rgba(120, 100, 80, 0.4), rgba(120, 100, 80, 0) 45%),
    linear-gradient(180deg, #4a4038 0%, #3a3230 100%);
}

[data-theme="dark"] .title-block h1 {
  color: #f5e6d3;
}

[data-theme="dark"] .title-block p,
[data-theme="dark"] .status-card p,
[data-theme="dark"] .controls-card p,
[data-theme="dark"] .overlay-card p {
  color: #d4c8b8;
}

[data-theme="dark"] .score-card,
[data-theme="dark"] .status-card,
[data-theme="dark"] .controls-card {
  background: rgba(80, 70, 60, 0.7);
  box-shadow: 0 18px 40px rgba(30, 25, 20, 0.25);
}

[data-theme="dark"] .score-card span {
  color: #c4b8a8;
}

[data-theme="dark"] .score-card strong {
  color: #f5e6d3;
}

[data-theme="dark"] .status-pill {
  background: rgba(220, 200, 180, 0.15);
  color: #e0d4c4;
}

[data-theme="dark"] .status-pill--success {
  background: rgba(112, 185, 77, 0.25);
  color: #a0e070;
}

[data-theme="dark"] .status-pill--danger {
  background: rgba(232, 107, 85, 0.25);
  color: #ff9980;
}

[data-theme="dark"] .primary-button {
  background: linear-gradient(145deg, #8a6a52, #6a5242);
  color: #fff5e8;
  box-shadow: 0 10px 24px rgba(40, 30, 25, 0.3);
}

[data-theme="dark"] .secondary-button {
  background: rgba(220, 200, 180, 0.2);
  color: #e0d4c4;
}

[data-theme="dark"] .controls-card h2,
[data-theme="dark"] .overlay-card h2 {
  color: #e8ddd0;
}

[data-theme="dark"] .keycaps span {
  background: #5a5048;
  color: #e0d4c4;
  box-shadow: inset 0 0 0 1px rgba(220, 200, 180, 0.2);
}

[data-theme="dark"] .board-panel {
  background: linear-gradient(180deg, rgba(100, 90, 80, 0.95), rgba(80, 72, 65, 0.95));
  box-shadow: 0 30px 60px rgba(30, 25, 20, 0.35);
}

[data-theme="dark"] .board-cell {
  background: rgba(140, 130, 120, 0.3);
}

[data-theme="dark"] .tile {
  box-shadow: 0 12px 25px rgba(30, 25, 20, 0.3);
}

[data-theme="dark"] .board-overlay {
  background: rgba(50, 45, 40, 0.8);
}

[data-theme="dark"] .overlay-card {
  background: rgba(70, 62, 55, 0.95);
  box-shadow: 0 18px 36px rgba(30, 25, 20, 0.35);
}

[data-theme="dark"] .lang-selector select {
  background: rgba(80, 70, 60, 0.8);
  border-color: rgba(220, 200, 180, 0.25);
  color: #e0d4c4;
}

[data-theme="dark"] .theme-toggle {
  background: rgba(80, 70, 60, 0.8);
  border-color: rgba(220, 200, 180, 0.25);
}

[data-theme="dark"] .theme-toggle .theme-icon-light {
  display: none;
}

[data-theme="dark"] .theme-toggle .theme-icon-dark {
  display: block;
}

[data-theme="dark"] .theme-toggle[data-theme="dark"] {
  background: rgba(60, 55, 50, 0.9);
}

@media (prefers-reduced-motion: reduce) {
  .tile,
  .primary-button,
  .secondary-button,
  .theme-toggle {
    transition: none;
  }

  .tile--new,
  .tile--merged {
    animation: none;
  }
}
