:root {
  --primary-bg: #ffffff;
  --card-bg: #f7f7f7;
  --text-color: #333333;
  --accent-color: #6b6b6b;
  --transition-speed: 0.3s;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

body {
  background: var(--primary-bg);
  color: var(--text-color);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.container {
  width: 100%;
  margin: 0 auto;
  padding: 0 1rem;
  flex: 1;
  display: flex;
  flex-direction: column;
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  padding: 1rem max(2rem, calc(50% - 600px));
  background: var(--primary-bg);
  position: sticky;
  top: 0;
  z-index: 10;
  gap: 1rem;
}

header h1 {
  text-align: center;
  flex: 1;
}

.header-controls {
  display: flex;
  gap: 1rem;
  align-items: center;
}

.header-btn {
  font-size: 0.9rem;
  padding: 0.5rem 1rem;
  border-radius: 6px;
  opacity: 0.7;
  transition: opacity var(--transition-speed),
    background-color var(--transition-speed);
}

.header-btn:hover {
  opacity: 1;
  background-color: var(--card-bg);
}

h1 {
  font-weight: 500;
  font-size: 1.5rem;
  letter-spacing: -0.02em;
}

.game-info {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0;
  min-width: 160px;
}

.stats {
  display: flex;
  gap: 2rem;
  font-size: 0.9rem;
  color: var(--text-color);
  font-variant-numeric: tabular-nums;
  transition: all var(--transition-speed) ease-in-out;
  opacity: 0;
  transform: translateY(-4px);
}

.stats[style*="opacity: 1"] {
  transform: translateY(0);
}

.stats span:last-child {
  transition: color var(--transition-speed);
}

.stats span:last-child.highlight {
  color: var(--accent-color);
}

.stats span:first-child {
  color: var(--accent-color);
  margin-right: 0.5rem;
}

button {
  background: none;
  border: none;
  color: var(--text-color);
  cursor: pointer;
  font-size: 0.9rem;
  padding: 0.5rem 1rem;
  border-radius: 6px;
  transition: background-color var(--transition-speed);
}

button:hover {
  background-color: var(--card-bg);
}

#game-screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: center;
  width: 100%;
  padding: 2rem 1rem;
  max-width: 800px;
  margin: 0 auto;
}

.cards {
  display: grid;
  gap: 1rem;
  width: 100%;
  margin: 0 auto;
  transition: all var(--transition-speed) ease-in-out;
  grid-template-columns: repeat(4, 1fr);
  max-width: 560px;
}

/* Mobile-optimized grid layout */
@media (max-width: 600px) {
  header {
    display: flex;
    flex-direction: column;
    h1 {
      order: -2;
    }
    .header-controls {
      order: -1;
    }
  }
  .cards {
    gap: 0.75rem;
    padding: 0.5rem;
  }

  .card {
    max-width: 120px;
  }
}

@media (max-width: 480px) {
  .cards {
    gap: 0.5rem;
  }

  .card {
    max-width: 100px;
  }
}

.card {
  aspect-ratio: 3/4;
  position: relative;
  cursor: pointer;
  min-width: 0; /* Prevent grid blowout */
  font-size: clamp(1.25rem, 3vw, 1.75rem);
  perspective: 1000px;
  width: 100%;
  max-width: 140px;
  margin: 0 auto;
}

.card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  text-align: center;
  transition: transform var(--transition-speed) ease-in-out;
  transform-style: preserve-3d;
}

.card.flipped .card-inner {
  transform: rotateY(180deg);
}

.card-front,
.card-back {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  background: var(--card-bg);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.02), 0 1px 2px rgba(0, 0, 0, 0.04);
  transition: box-shadow var(--transition-speed);
}

.card:hover .card-front,
.card:hover .card-back {
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.04), 0 1px 3px rgba(0, 0, 0, 0.08);
}

.card-front {
  background: var(--card-bg);
  -webkit-transform: rotateY(0);
  transform: rotateY(0);
}

.card-back {
  background: var(--card-bg);
  -webkit-transform: rotateY(180deg);
  transform: rotateY(180deg);
}

.hide {
  display: none;
}

.screen {
  text-align: center;
  padding: 2rem;
  transition: opacity var(--transition-speed) ease-in-out,
    transform var(--transition-speed) ease-in-out;
  opacity: 1;
  transform: translateY(0);
}

.screen.hide {
  opacity: 0;
  pointer-events: none;
  transform: translateY(10px);
}

#result-screen {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: var(--primary-bg);
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  z-index: 1000;
  max-width: 400px;
  width: 90%;
  padding: 2rem;
  transition: opacity var(--transition-speed) ease-in-out,
    transform var(--transition-speed) ease-in-out;
}

#result-screen.hide {
  opacity: 0;
  transform: translate(-50%, -45%);
  pointer-events: none;
}

#result-screen h2 {
  margin-bottom: 1rem;
  color: var(--accent-color);
}

#result-screen .result-message {
  margin-bottom: 1.5rem;
  opacity: 0.8;
}

#result-screen .result-info {
  margin-bottom: 2rem;
  font-variant-numeric: tabular-nums;
}

#result-screen .result-info p {
  margin: 0.5rem 0;
  display: flex;
  justify-content: center;
  gap: 0.5rem;
}

#result-screen .result-info span:first-child {
  color: var(--accent-color);
}

#result-screen .controls {
  display: flex;
  gap: 1rem;
  justify-content: center;
}

#result-screen button {
  min-width: 120px;
  padding: 0.75rem 1.5rem;
  background: var(--card-bg);
  transition: all var(--transition-speed);
}

#result-screen button:hover {
  background: var(--accent-color);
  color: var(--primary-bg);
}

.controls {
  margin-top: 2rem;
  display: flex;
  gap: 1rem;
  justify-content: center;
}

.modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(5px);
  z-index: 100;
  opacity: 0;
  transition: opacity var(--transition-speed);
}

.modal.active {
  display: flex;
  opacity: 1;
}

.modal-content {
  margin: auto;
  padding: 2rem;
  max-width: 600px;
  width: 90%;
  position: relative;
}

.close-btn {
  position: absolute;
  top: 1rem;
  right: 1rem;
  cursor: pointer;
  font-size: 1.5rem;
  opacity: 0.5;
  transition: opacity var(--transition-speed);
}

.close-btn:hover {
  opacity: 1;
}

.result-info {
  margin: 2rem 0;
}

.share-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
}

footer {
  text-align: center;
  padding: 2rem !important;
  color: var(--accent-color);
  font-size: 0.9rem;
}

footer a {
  color: inherit;
  text-decoration: none;
}

footer a:hover {
  text-decoration: underline;
}

/* Details Section Typography */
.details-section {
  padding: 4rem max(2rem, calc(50% - 600px));
  background: var(--primary-bg);
  line-height: 1.6;
}

.details-section article {
  max-width: 800px;
  margin: 0 auto 4rem;
}

.details-section article:last-child {
  margin-bottom: 0;
}

.details-section h3 {
  font-size: 1.75rem;
  margin-bottom: 1.5rem;
  color: var(--accent-color);
  letter-spacing: -0.02em;
  font-weight: 500;
}

.details-section h4 {
  font-size: 1.25rem;
  margin: 2rem 0 1rem;
  color: var(--text-color);
  letter-spacing: -0.01em;
  font-weight: 500;
}

.details-section p {
  margin-bottom: 1.25rem;
  color: var(--text-color);
  font-size: 1rem;
  opacity: 0.9;
  max-width: 65ch;
}

.details-section p:last-child {
  margin-bottom: 0;
}

.details-section .how-to-steps {
  list-style: none;
  padding: 0;
  counter-reset: step;
  margin: 2rem 0;
}

.details-section .how-to-steps li {
  margin-bottom: 1.5rem;
  padding-left: 2.5rem;
  position: relative;
}

.details-section .how-to-steps li:last-child {
  margin-bottom: 0;
}

.details-section .how-to-steps li::before {
  counter-increment: step;
  content: counter(step);
  position: absolute;
  left: 0;
  top: 0.25rem;
  width: 1.75rem;
  height: 1.75rem;
  background: var(--accent-color);
  color: var(--primary-bg);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 500;
  font-size: 0.875rem;
}

.details-section .how-to-steps strong {
  display: block;
  margin-bottom: 0.5rem;
  color: var(--text-color);
  font-weight: 500;
}

.details-section .how-to-steps div[itemprop="text"] {
  color: var(--text-color);
  opacity: 0.9;
}

.details-section [itemtype="https://schema.org/Question"]
{
  margin-bottom: 2.5rem;
}

.details-section [itemtype="https://schema.org/Question"]:last-child
{
  margin-bottom: 0;
}

.details-section [itemtype="https://schema.org/Answer"] p
{
  margin: 0;
}

@media (max-width: 768px) {
  .container {
    padding: 0.75rem;
  }

  header {
    padding: 0.75rem;
    margin-bottom: 0.5rem;
  }

  h1 {
    font-size: 1.25rem;
  }

  .cards {
    gap: 0.75rem;
    padding: 0.75rem;
  }

  .card {
    aspect-ratio: 3/4;
    border-radius: 8px;
    min-height: 80px;
    touch-action: manipulation;
  }

  .game-info {
    padding: 0 0.75rem;
    gap: 1.5rem;
  }

  .stats {
    gap: 1rem;
  }

  .stats > div {
    font-size: 0.875rem;
  }

  .grid-size-options,
  .card-type-options {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.5rem;
    margin: 1rem 0;
  }

  .grid-size-options button,
  .card-type-options button {
    padding: 0.75rem 0.5rem;
    font-size: 0.875rem;
    min-width: auto;
    width: 100%;
    min-height: 44px;
    touch-action: manipulation;
  }

  #settings-screen {
    padding: 1rem;
  }

  #settings-screen h2 {
    font-size: 1.25rem;
    margin-bottom: 1rem;
  }

  .details-section {
    padding: 3rem 1.5rem;
  }

  .details-section article {
    margin-bottom: 3rem;
  }

  .details-section h3 {
    font-size: 1.5rem;
    margin-bottom: 1.25rem;
  }

  .details-section h4 {
    font-size: 1.125rem;
    margin: 1.5rem 0 0.875rem;
  }

  .details-section p {
    font-size: 0.9375rem;
    margin-bottom: 1rem;
  }

  .details-section .how-to-steps {
    margin: 1.5rem 0;
  }

  .details-section .how-to-steps li {
    margin-bottom: 1.25rem;
    padding-left: 2.25rem;
  }

  .details-section .how-to-steps li::before {
    width: 1.5rem;
    height: 1.5rem;
    font-size: 0.75rem;
  }
}

/* Animations */
@keyframes cardMatch {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.7;
  }
}

.card.matched .card-back {
  animation: cardMatch 0.5s ease-in-out;
  box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
}

/* Responsive Design */
@media (max-width: 768px) {
  header {
    padding: 1rem;
  }

  h1 {
    font-size: 1.25rem;
  }

  .game-info {
    min-width: 140px;
  }

  .stats {
    gap: 1.5rem;
  }

  .cards {
    width: 100%;
    padding: 0.75rem;
  }

  .card {
    font-size: clamp(1.25rem, 3.5vw, 1.75rem);
  }
}

@media (max-width: 480px) {
  header {
    padding: 0.75rem;
  }

  .header-btn {
    padding: 0.4rem 0.8rem;
    font-size: 0.85rem;
  }

  .stats {
    gap: 1rem;
    font-size: 0.85rem;
  }

  .game-info {
    min-width: 120px;
  }

  h1 {
    font-size: 1.1rem;
  }

  .cards {
    padding: 0.5rem;
    gap: 0.5rem;
  }

  .card {
    font-size: clamp(1rem, 3vw, 1.5rem);
  }

  .settings-section {
    margin-bottom: 1rem;
  }

  .button-group {
    gap: 0.4rem;
  }
}

/* Settings Section */
.settings-divider {
  margin: 2rem 0;
  border: none;
  height: 1px;
  background: var(--border-color);
  opacity: 0.5;
}

.settings {
  max-width: 800px;
  margin: 0 auto;
  padding: 0 1rem;
}

.settings-section {
  margin-bottom: 1.5rem;
  opacity: 1;
  transform: translateY(0);
  transition: all var(--transition-speed) ease-in-out;
}

.settings-section.hide {
  opacity: 0;
  transform: translateY(-0.5rem);
  pointer-events: none;
}

.settings-section h3 {
  margin-bottom: 0.75rem;
  color: var(--text-color);
  font-size: 1rem;
  font-weight: 500;
}

.button-group {
  display: flex;
  justify-content: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.button-group button {
  padding: 0.5rem 1rem;
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius);
  background: var(--secondary-bg);
  color: var(--text-color);
  font-size: 0.9rem;
  cursor: pointer;
  transition: all var(--transition-speed) ease-in-out;
  min-width: 3.5rem;
  text-align: center;
}

.button-group button:hover {
  background: var(--hover-bg);
  transform: translateY(-1px);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.button-group button:active {
  transform: translateY(0);
  box-shadow: none;
}

.button-group button.active {
  background: var(--accent-color);
  color: white;
  border-color: var(--accent-color);
}

.emoji-categories.hide {
  display: none;
}

.button-group.wrap {
  max-width: 600px;
}

@media (max-width: 480px) {
  .settings-divider {
    margin: 1rem 0;
  }

  .cards {
    gap: 0.5rem;
    padding: 0.5rem;
  }

  .card {
    border-radius: 6px;
  }

  .grid-size-options,
  .card-type-options {
    grid-template-columns: repeat(2, 1fr);
  }

  .stats > div {
    font-size: 0.8125rem;
  }

  .grid-size-options button,
  .card-type-options button {
    padding: 0.5rem 0.75rem;
  }

  header .controls {
    gap: 0.5rem;
  }

  header .controls button {
    padding: 0.5rem 1rem;
    font-size: 0.875rem;
    min-height: 36px;
  }

  .settings-section h3 {
    font-size: 0.9rem;
  }

  .button-group button {
    padding: 0.4rem 0.8rem;
    font-size: 0.85rem;
  }
}

/* Accessibility */
@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}
