/* Reset & Global Styles */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --bg-dark: #070716;
  --bg-stars: #0b0b26;
  --text-main: #f3f3fb;
  --text-muted: #a0a0c0;
  --neon-cyan: #00f3ff;
  --neon-pink: #ff007f;
  --neon-purple: #9d4edf;
  --neon-gold: #ffd700;
  --glass-bg: rgba(13, 13, 35, 0.6);
  --glass-border: rgba(255, 255, 255, 0.08);
  --glass-border-focus: rgba(0, 243, 255, 0.4);
  --card-back-bg: radial-gradient(circle, #1e1e4f 0%, #0c0c28 100%);
  --transition-smooth: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
}

body {
  font-family: 'Outfit', 'Noto Sans KR', sans-serif;
  background-color: var(--bg-dark);
  color: var(--text-main);
  min-height: 100vh;
  overflow-x: hidden;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 2rem 1rem;
}

/* Cosmic Animated Background */
.stars-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: var(--bg-dark) url('https://i.imgur.com/8Q5FqWv.png') repeat;
  z-index: -2;
  opacity: 0.4;
}

.twinkling-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: transparent url('https://i.imgur.com/2U5K4L7.png') repeat;
  z-index: -1;
  opacity: 0.3;
  animation: move-twink-back 200s linear infinite;
}

@keyframes move-twink-back {
  from { background-position: 0 0; }
  to { background-position: -10000px 5000px; }
}

/* App Layout Container */
.app-container {
  width: 100%;
  max-width: 1100px;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  z-index: 1;
}

/* Header styling */
.app-header {
  text-align: center;
  margin-bottom: 0.5rem;
}

.neon-title {
  font-size: 2.8rem;
  font-weight: 800;
  letter-spacing: 3px;
  margin-bottom: 0.5rem;
  text-shadow: 0 0 10px rgba(157, 78, 223, 0.5),
               0 0 20px rgba(0, 243, 255, 0.3);
}

.title-cosmic {
  color: var(--text-main);
}

.title-memory {
  color: var(--neon-cyan);
  background: linear-gradient(45deg, var(--neon-cyan), var(--neon-pink));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  animation: glow-pulse 3s infinite alternate;
}

@keyframes glow-pulse {
  from { filter: drop-shadow(0 0 2px var(--neon-cyan)); }
  to { filter: drop-shadow(0 0 8px var(--neon-pink)); }
}

.subtitle {
  color: var(--text-muted);
  font-size: 1rem;
  font-weight: 300;
}

/* Main Grid Layout */
.main-content {
  display: grid;
  grid-template-columns: 1.6fr 1fr;
  gap: 1.5rem;
}

/* Glassmorphism Cards */
.card-glass {
  background: var(--glass-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--glass-border);
  border-radius: 16px;
  box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.37);
  transition: var(--transition-smooth);
}

.card-glass:hover {
  border-color: rgba(255, 255, 255, 0.12);
}

/* Left Section: Game Play */
.game-section {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

/* Dashboard Statistics */
.dashboard {
  display: flex;
  justify-content: space-around;
  padding: 1.2rem;
  text-align: center;
}

.stat-box {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.stat-label {
  font-size: 0.85rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 1px;
}

.stat-value {
  font-size: 1.8rem;
  font-weight: 800;
  font-family: 'Outfit', sans-serif;
  text-shadow: 0 0 5px currentColor;
}

#score-val { color: var(--neon-gold); }
#moves-val { color: var(--neon-cyan); }
#timer-val { color: var(--neon-pink); }

.timer-box {
  min-width: 90px;
}

/* Progress bar for timer */
.progress-bar-container {
  width: 100%;
  height: 5px;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 3px;
  overflow: hidden;
  display: none; /* Only show in timer mode */
  opacity: 0;
  transition: opacity 0.3s;
}

.progress-bar-container.active {
  display: block;
  opacity: 1;
}

.progress-bar {
  height: 100%;
  width: 100%;
  background: linear-gradient(90deg, var(--neon-cyan), var(--neon-pink));
  box-shadow: 0 0 10px var(--neon-pink);
  transition: width 0.1s linear;
}

/* Control Panel */
.control-panel {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem;
  gap: 1rem;
}

/* Buttons */
.btn {
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  padding: 0.75rem 1.5rem;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  transition: var(--transition-smooth);
}

.btn-primary {
  background: linear-gradient(135deg, var(--neon-purple) 0%, #6f2dbd 100%);
  color: var(--text-main);
  box-shadow: 0 4px 15px rgba(157, 78, 223, 0.4);
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(157, 78, 223, 0.6);
  filter: brightness(1.1);
}

.btn-primary:active {
  transform: translateY(1px);
}

.btn-secondary {
  background: rgba(255, 255, 255, 0.07);
  color: var(--text-muted);
  border: 1px solid var(--glass-border);
}

.btn-secondary:hover {
  background: rgba(255, 255, 255, 0.15);
  color: var(--text-main);
}

.btn-icon {
  background: rgba(255, 255, 255, 0.05);
  color: var(--text-muted);
  border: 1px solid var(--glass-border);
  padding: 0.75rem;
  border-radius: 8px;
  aspect-ratio: 1;
  position: relative;
}

.btn-icon:hover {
  color: var(--neon-cyan);
  border-color: var(--neon-cyan);
  box-shadow: 0 0 10px rgba(0, 243, 255, 0.3);
  background: rgba(0, 243, 255, 0.05);
}

.status-dot {
  position: absolute;
  top: -3px;
  right: -3px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background-color: #ff3838; /* Red for disconnected */
  border: 2px solid var(--bg-dark);
  box-shadow: 0 0 5px #ff3838;
}

.status-dot.connected {
  background-color: #2ed573; /* Green for connected */
  box-shadow: 0 0 5px #2ed573;
}

/* Switch Toggle Styling */
.switch-container {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
  user-select: none;
}

.switch-container input {
  display: none;
}

.switch-slider {
  position: relative;
  width: 44px;
  height: 22px;
  background-color: rgba(255, 255, 255, 0.1);
  border: 1px solid var(--glass-border);
  border-radius: 11px;
  transition: var(--transition-smooth);
}

.switch-slider::before {
  content: "";
  position: absolute;
  width: 14px;
  height: 14px;
  left: 3px;
  bottom: 3px;
  background-color: var(--text-muted);
  border-radius: 50%;
  transition: var(--transition-smooth);
}

.switch-container input:checked + .switch-slider {
  background-color: rgba(255, 0, 127, 0.2);
  border-color: var(--neon-pink);
}

.switch-container input:checked + .switch-slider::before {
  transform: translateX(22px);
  background-color: var(--neon-pink);
  box-shadow: 0 0 5px var(--neon-pink);
}

.switch-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
}

.switch-container input:checked ~ .switch-label {
  color: var(--text-main);
}

/* Game Board Wrapper & Overlays */
.game-board-wrapper {
  position: relative;
  width: 100%;
}

.board-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(7, 7, 22, 0.85);
  backdrop-filter: blur(8px);
  border-radius: 16px;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 10;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.4s ease;
  border: 1px solid var(--glass-border);
}

.board-overlay.active {
  opacity: 1;
  pointer-events: auto;
}

#overlay-text {
  font-size: 1.3rem;
  font-weight: 600;
  letter-spacing: 1px;
  color: var(--neon-cyan);
  text-shadow: 0 0 8px rgba(0, 243, 255, 0.4);
  animation: pulse-text 1.5s infinite alternate;
}

@keyframes pulse-text {
  from { opacity: 0.6; }
  to { opacity: 1; }
}

/* Game Card Grid */
.game-board {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 1rem;
  perspective: 1000px;
}

/* 3D Flipping Cards */
.card-wrapper {
  aspect-ratio: 3 / 4;
  cursor: pointer;
}

.card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  text-align: center;
  transition: transform 0.6s cubic-bezier(0.175, 0.885, 0.32, 1.275), box-shadow 0.3s;
  transform-style: preserve-3d;
  border-radius: 12px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
}

/* Neon Glow on Hover */
.card-wrapper:hover .card-inner {
  transform: scale(1.03);
  box-shadow: 0 0 15px rgba(0, 243, 255, 0.4);
}

.card-wrapper:active .card-inner {
  transform: scale(0.98);
}

/* Flip state */
.card-wrapper.flipped .card-inner {
  transform: rotateY(180deg);
}

/* Keep card matched and open */
.card-wrapper.matched {
  cursor: default;
  pointer-events: none;
}

.card-wrapper.matched .card-inner {
  transform: rotateY(180deg);
  box-shadow: 0 0 20px rgba(157, 78, 223, 0.5);
  animation: matched-glow 1s ease forwards;
}

@keyframes matched-glow {
  0% { transform: rotateY(180deg) scale(1); }
  50% { transform: rotateY(180deg) scale(1.1); filter: brightness(1.2); }
  100% { transform: rotateY(180deg) scale(1); opacity: 0.85; }
}

/* Card Front & Back Sides */
.card-front, .card-back {
  position: absolute;
  width: 100%;
  height: 100%;
  -webkit-backface-visibility: hidden; /* For Safari */
  backface-visibility: hidden;
  border-radius: 12px;
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px solid var(--glass-border);
}

/* Back of card (facing user initially) */
.card-back {
  background: var(--card-back-bg);
  color: var(--neon-cyan);
  font-size: 2rem;
  overflow: hidden;
}

/* Background grid patterns on card back */
.card-back::after {
  content: "?";
  font-family: 'Outfit', sans-serif;
  font-weight: 800;
  opacity: 0.15;
  font-size: 3.5rem;
  text-shadow: 0 0 10px var(--neon-cyan);
  transition: var(--transition-smooth);
}

.card-wrapper:hover .card-back::after {
  opacity: 0.4;
  transform: scale(1.15);
}

/* Front of card (shown when flipped) */
.card-front {
  background: rgba(13, 13, 38, 0.95);
  color: var(--text-main);
  font-size: 2.8rem;
  transform: rotateY(180deg);
  border: 1.5px solid var(--neon-cyan);
  box-shadow: inset 0 0 15px rgba(0, 243, 255, 0.15);
}

/* Special Border for Matched Cards */
.card-wrapper.matched .card-front {
  border-color: var(--neon-purple);
  box-shadow: inset 0 0 15px rgba(157, 78, 223, 0.2);
}

/* Error Flip Shake effect */
.card-wrapper.wrong .card-front {
  border-color: var(--neon-pink);
  animation: shake 0.5s ease;
  box-shadow: inset 0 0 15px rgba(255, 0, 127, 0.3);
}

@keyframes shake {
  0%, 100% { transform: rotateY(180deg) translateX(0); }
  20%, 60% { transform: rotateY(180deg) translateX(-6px); }
  40%, 80% { transform: rotateY(180deg) translateX(6px); }
}


/* Right Section: Rules & Leaderboard */
.side-section {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.info-card {
  padding: 1.2rem;
}

.info-card h3, .leaderboard-card h3 {
  font-size: 1.1rem;
  font-weight: 600;
  margin-bottom: 0.8rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.info-card ul {
  list-style: none;
  font-size: 0.85rem;
  color: var(--text-muted);
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  line-height: 1.4;
}

.info-card strong {
  color: var(--text-main);
}

/* Leaderboard */
.leaderboard-card {
  padding: 1.2rem;
  flex-grow: 1;
  display: flex;
  flex-direction: column;
}

.leaderboard-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.8rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  padding-bottom: 0.5rem;
}

.leaderboard-header h3 {
  margin-bottom: 0;
}

.db-badge {
  font-size: 0.7rem;
  padding: 0.25rem 0.5rem;
  border-radius: 4px;
  font-weight: bold;
  background-color: rgba(255, 255, 255, 0.05);
  color: var(--text-muted);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.db-badge.connected {
  background-color: rgba(46, 213, 115, 0.15);
  color: #2ed573;
  border-color: #2ed573;
  box-shadow: 0 0 5px rgba(46, 213, 115, 0.1);
}

.leaderboard-list-wrapper {
  flex-grow: 1;
  overflow-y: auto;
  max-height: 250px;
}

.leaderboard-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
  text-align: left;
}

.leaderboard-table th, .leaderboard-table td {
  padding: 0.6rem 0.4rem;
}

.leaderboard-table th {
  color: var(--text-muted);
  font-weight: 600;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.leaderboard-table tbody tr {
  border-bottom: 1px solid rgba(255, 255, 255, 0.03);
  transition: var(--transition-smooth);
}

.leaderboard-table tbody tr:hover {
  background: rgba(255, 255, 255, 0.03);
}

.leaderboard-table td {
  color: var(--text-main);
  font-family: 'Outfit', 'Noto Sans KR', sans-serif;
}

.leaderboard-table td.empty-table {
  text-align: center;
  color: var(--text-muted);
  padding: 2rem 0;
  font-style: italic;
}

/* Rank medal colors */
.leaderboard-table tr:nth-child(1) td:first-child { color: var(--neon-gold); font-weight: bold; }
.leaderboard-table tr:nth-child(2) td:first-child { color: #e0e0e0; font-weight: bold; }
.leaderboard-table tr:nth-child(3) td:first-child { color: #cd7f32; font-weight: bold; }


/* Modal Base Styling */
.modal-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(4, 4, 12, 0.7);
  backdrop-filter: blur(8px);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 100;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

.modal-backdrop.active {
  opacity: 1;
  pointer-events: auto;
}

.modal {
  width: 90%;
  max-width: 480px;
  padding: 1.8rem;
  border-color: rgba(255, 255, 255, 0.15);
  transform: translateY(-20px);
  transition: transform 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.modal-backdrop.active .modal {
  transform: translateY(0);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.2rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  padding-bottom: 0.6rem;
}

.modal-header h2 {
  font-size: 1.3rem;
  font-weight: 600;
}

.btn-close-modal {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.8rem;
  cursor: pointer;
  line-height: 1;
  transition: var(--transition-smooth);
}

.btn-close-modal:hover {
  color: var(--neon-pink);
}

.modal-desc {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 1.2rem;
  line-height: 1.5;
}

/* Forms */
.form-group {
  margin-bottom: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.form-group label {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-weight: 600;
  text-transform: uppercase;
}

.form-group input {
  font-family: inherit;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--glass-border);
  border-radius: 6px;
  padding: 0.6rem;
  color: var(--text-main);
  font-size: 0.9rem;
  outline: none;
  transition: var(--transition-smooth);
}

.form-group input:focus {
  border-color: var(--neon-cyan);
  background: rgba(0, 243, 255, 0.03);
  box-shadow: 0 0 8px rgba(0, 243, 255, 0.2);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.8rem;
  margin-top: 1.5rem;
}

.justify-center {
  justify-content: center;
}

/* Game Results Overlay Specifics */
.text-center {
  text-align: center;
}

.result-icon {
  font-size: 4rem;
  margin: 1rem 0;
  filter: drop-shadow(0 0 10px rgba(255, 215, 0, 0.4));
}

.result-msg-text {
  font-size: 1rem;
  line-height: 1.6;
  margin-bottom: 1.5rem;
}

.ranking-reg-box {
  background: rgba(255, 255, 255, 0.03);
  border: 1px dashed var(--glass-border);
  border-radius: 8px;
  padding: 1rem;
  margin-bottom: 1.5rem;
}

.reg-desc {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 0.6rem;
}

.input-action-group {
  display: flex;
  gap: 0.5rem;
  justify-content: center;
}

.input-action-group input {
  font-family: inherit;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--glass-border);
  border-radius: 6px;
  padding: 0.5rem;
  color: var(--text-main);
  outline: none;
  font-size: 0.9rem;
  width: 160px;
}

.input-action-group input:focus {
  border-color: var(--neon-cyan);
}

.status-msg {
  font-size: 0.75rem;
  margin-top: 0.5rem;
}

.status-msg.success { color: #2ed573; }
.status-msg.error { color: #ff4757; }
.status-msg.info { color: var(--neon-cyan); }

.hidden {
  display: none !important;
}

/* Custom Animation Utils */
.animate-bounce {
  animation: bounce 2s infinite alternate;
}

@keyframes bounce {
  0% { transform: translateY(0) scale(1); }
  100% { transform: translateY(-10px) scale(1.05); }
}

/* Colors styling utilities */
.text-gold { color: var(--neon-gold); }
.text-cyan { color: var(--neon-cyan); }
.text-pink { color: var(--neon-pink); }


/* Responsive Design */
@media (max-width: 900px) {
  .main-content {
    grid-template-columns: 1fr;
  }
  
  .app-container {
    max-width: 600px;
  }
}

@media (max-width: 480px) {
  body {
    padding: 1rem 0.5rem;
  }
  
  .neon-title {
    font-size: 2.2rem;
  }
  
  .stat-value {
    font-size: 1.4rem;
  }
  
  .game-board {
    gap: 0.6rem;
  }
  
  .card-front {
    font-size: 2.2rem;
  }
  
  .card-back {
    font-size: 1.5rem;
  }
  
  .card-back::after {
    font-size: 2.5rem;
  }
  
  .btn {
    padding: 0.6rem 1rem;
    font-size: 0.85rem;
  }
}
