/* ===== Reset & Base ===== */
*, *::before, *::after {
  margin: 0; padding: 0; box-sizing: border-box;
}

:root {
  --bg: #0a0a1a;
  --bg-light: #12122a;
  --gold: #c9a84c;
  --gold-dim: #8a7333;
  --gold-glow: rgba(201, 168, 76, 0.3);
  --text: #e0d5c1;
  --text-dim: #8a8070;
  --card-w: 70px;
  --card-h: 108px;
  --card-radius: 6px;
  --drawer-h: 190px;
  --topbar-h: 44px;
  --transition: 0.3s ease;
  --sand: #c3a471;
  --sand-light: #f6dfb4;
  --sand-dark: #8d6f49;
}

html, body {
  height: 100%; width: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', sans-serif;
  background: var(--bg);
  color: var(--text);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

/* ===== Top Bar ===== */
#top-bar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  background: rgba(10, 10, 26, 0.92);
  border-bottom: 1px solid rgba(201, 168, 76, 0.15);
  z-index: 100;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

/* Mode Tabs */
#mode-tabs {
  display: flex;
  gap: 2px;
  background: rgba(255,255,255,0.06);
  border-radius: 10px;
  padding: 2px;
}

.mode-tab {
  padding: 4px 12px;
  border-radius: 8px;
  border: none;
  background: transparent;
  color: var(--text-dim);
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s;
  touch-action: manipulation;
}

.mode-tab.active {
  background: rgba(201, 168, 76, 0.2);
  color: var(--gold);
  font-weight: 600;
}

/* Top Actions */
#top-actions {
  display: flex;
  gap: 8px;
}

#top-actions button {
  padding: 4px 12px;
  border-radius: 14px;
  border: 1px solid var(--gold-dim);
  background: rgba(201, 168, 76, 0.1);
  color: var(--gold);
  font-size: 11px;
  cursor: pointer;
  touch-action: manipulation;
  transition: all var(--transition);
}

#top-actions button:active {
  transform: scale(0.95);
  background: rgba(201, 168, 76, 0.25);
}

/* ===== Energy Panel ===== */
#energy-panel {
  position: fixed;
  top: var(--topbar-h);
  left: 0; right: 0;
  bottom: 0;
  z-index: 80;
  background: var(--bg);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

#energy-panel.hidden { display: none; }

#energy-inner {
  max-width: 400px;
  margin: 0 auto;
  padding: 32px 20px 40px;
}

.energy-header {
  text-align: center;
  margin-bottom: 28px;
}

.energy-header .energy-icon {
  display: block;
  font-size: 32px;
  color: var(--gold);
  margin-bottom: 12px;
}

.energy-header h3 {
  font-size: 20px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 6px;
}

.energy-header p {
  font-size: 13px;
  color: var(--text-dim);
}

.energy-item {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
  padding: 10px 14px;
  border-radius: 16px;
  border: 1px solid rgba(255,255,255,0.1);
  background: rgba(255,255,255,0.05);
  backdrop-filter: blur(4px);
}

.energy-item input {
  flex: 1;
  min-width: 0;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text);
  font-size: 14px;
}

.energy-item input::placeholder {
  color: rgba(224, 213, 193, 0.35);
}

.energy-remove {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 1px solid rgba(255,255,255,0.1);
  background: rgba(255,255,255,0.05);
  color: var(--gold);
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  touch-action: manipulation;
}

#add-energy {
  width: 100%;
  padding: 12px;
  border-radius: 16px;
  border: 1px dashed rgba(201, 168, 76, 0.4);
  background: transparent;
  color: var(--gold);
  font-size: 13px;
  cursor: pointer;
  margin-top: 4px;
  margin-bottom: 20px;
  transition: all 0.2s;
  touch-action: manipulation;
}

#add-energy:active {
  background: rgba(201, 168, 76, 0.1);
}

#start-sandbox {
  width: 100%;
  padding: 14px;
  border-radius: 20px;
  border: none;
  background: linear-gradient(135deg, var(--gold), #d4b65a);
  color: var(--bg);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  touch-action: manipulation;
}

#start-sandbox:active {
  transform: scale(0.98);
}

#start-sandbox:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ===== Arena ===== */
#arena {
  position: fixed;
  top: var(--topbar-h); left: 0; right: 0;
  bottom: var(--drawer-h);
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  background:
    radial-gradient(ellipse at 50% 40%, rgba(30, 20, 60, 0.8) 0%, transparent 70%),
    var(--bg);
  transition: background 0.5s;
}

/* Sandbox mode arena */
#arena.sandbox-board {
  background:
    radial-gradient(circle at top, var(--sand-light) 0%, #ead09a 25%, var(--sand) 60%, var(--sand-dark) 100%);
}

#arena-content {
  position: relative;
  width: 1200px;
  height: 1200px;
  transform-origin: 0 0;
  transition: transform 0.2s ease;
}

#arena-hint {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  color: var(--text-dim);
  font-size: 14px;
  line-height: 1.8;
  pointer-events: none;
  transition: opacity var(--transition);
}

#arena-hint .hint-icon {
  font-size: 40px;
  margin-bottom: 12px;
  animation: float 3s ease-in-out infinite;
}

#arena-hint.hidden { opacity: 0; }

/* Sandbox hint color override */
#arena.sandbox-board #arena-hint {
  color: rgba(93, 71, 45, 0.6);
}

@keyframes float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-8px); }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  75% { transform: translateX(6px); }
}

/* ===== Arena Cards ===== */
.arena-card {
  position: absolute;
  width: var(--card-w);
  height: var(--card-h);
  cursor: grab;
  perspective: 600px;
  transition: box-shadow 0.2s;
  z-index: 1;
  animation: cardAppear 0.4s ease-out;
}

.arena-card.dragging {
  cursor: grabbing;
  z-index: 9999;
  filter: brightness(1.1);
}

.arena-card .card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.6s ease;
}

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

.arena-card .card-face {
  position: absolute;
  width: 100%;
  height: 100%;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  border-radius: var(--card-radius);
  overflow: hidden;
}

.arena-card .card-back {
  z-index: 2;
}

.arena-card .card-front {
  transform: rotateY(180deg);
}

.arena-card.revealed {
  cursor: pointer;
}

@keyframes cardAppear {
  from { opacity: 0; transform: scale(0.5); }
  to { opacity: 1; transform: scale(1); }
}

/* ===== Energy Label on Cards ===== */
.energy-label {
  position: absolute;
  bottom: -28px;
  left: 50%;
  transform: translateX(-50%);
  white-space: nowrap;
  font-size: 11px;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 10px;
  background: rgba(10, 10, 26, 0.85);
  color: var(--gold);
  border: 1px solid rgba(201, 168, 76, 0.3);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  opacity: 0;
  transition: opacity 0.4s ease;
  pointer-events: none;
}

.sandbox-board .energy-label {
  background: rgba(93, 71, 45, 0.85);
  color: var(--sand-light);
  border-color: rgba(246, 223, 180, 0.3);
}

.arena-card.revealed .energy-label {
  opacity: 1;
}

/* ===== Card Back Design ===== */
.card-back-design {
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #1a0a2e 0%, #2d1b4e 50%, #1a0a2e 100%);
  border: 2px solid var(--gold);
  border-radius: var(--card-radius);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}

.card-back-design::before {
  content: '';
  position: absolute;
  inset: 6px;
  border: 1px solid var(--gold-dim);
  border-radius: 3px;
}

.card-back-design::after {
  content: '✦';
  font-size: 24px;
  color: var(--gold);
  text-shadow: 0 0 10px var(--gold-glow);
}

/* ===== Card Front Design ===== */
.card-front-design {
  width: 100%;
  height: 100%;
  background: linear-gradient(to bottom, #f5e6d3, #e8d5b7);
  border: 2px solid var(--gold);
  border-radius: var(--card-radius);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 4px;
  position: relative;
}

.card-front-design .card-number {
  font-size: 10px;
  color: var(--gold-dim);
  position: absolute;
  top: 4px;
  left: 6px;
}

.card-front-design .card-symbol {
  font-size: 28px;
  margin-bottom: 4px;
}

.card-front-design .card-label {
  font-size: 9px;
  color: #4a3a2a;
  text-align: center;
  line-height: 1.2;
  font-weight: 600;
}

.card-front-design img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 4px;
  position: absolute;
  inset: 0;
}

/* ===== Floating Controls ===== */
#controls {
  position: fixed;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 100;
}

#controls button {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1.5px solid var(--gold-dim);
  background: rgba(10, 10, 26, 0.85);
  color: var(--gold);
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  transition: all var(--transition);
  touch-action: manipulation;
}

#controls button:active {
  transform: scale(0.9);
  background: rgba(201, 168, 76, 0.2);
}

/* ===== Drawer ===== */
#drawer {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  height: var(--drawer-h);
  background: var(--bg-light);
  border-top: 1px solid rgba(201, 168, 76, 0.2);
  z-index: 50;
  transition: transform var(--transition);
  display: flex;
  flex-direction: column;
}

#drawer.collapsed {
  transform: translateY(calc(var(--drawer-h) - 40px));
}

#drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  cursor: pointer;
  flex-shrink: 0;
  min-height: 40px;
  touch-action: manipulation;
}

.drawer-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--gold);
}

#drawer-count {
  font-size: 12px;
  color: var(--text-dim);
}

#drawer-toggle {
  color: var(--gold-dim);
  font-size: 16px;
  transition: transform var(--transition);
}

#drawer.collapsed #drawer-toggle {
  transform: rotate(180deg);
}

#drawer-body {
  flex: 1;
  overflow: hidden;
  padding: 0 12px 12px;
}

#drawer-cards {
  display: grid;
  grid-template-rows: repeat(2, 1fr);
  grid-auto-flow: column;
  grid-auto-columns: min-content;
  gap: 6px;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 4px 0;
  height: 100%;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

#drawer-cards::-webkit-scrollbar {
  height: 3px;
}

#drawer-cards::-webkit-scrollbar-track {
  background: transparent;
}

#drawer-cards::-webkit-scrollbar-thumb {
  background: var(--gold-dim);
  border-radius: 2px;
}

/* ===== Drawer Cards ===== */
.deck-card {
  flex-shrink: 0;
  width: 46px;
  height: 71px;
  cursor: pointer;
  border-radius: 4px;
  transition: all 0.2s ease;
  touch-action: manipulation;
}

.deck-card:active {
  transform: scale(0.92);
}

.deck-card .card-back-design {
  border-radius: 4px;
}

.deck-card .card-back-design::after {
  font-size: 16px;
}

.deck-card .card-back-design::before {
  inset: 4px;
}

.deck-card.selected {
  opacity: 0.3;
  pointer-events: none;
  transform: scale(0.9);
}

/* ===== Sandbox energy remaining hint ===== */
#sandbox-hint {
  position: fixed;
  top: calc(var(--topbar-h) + 8px);
  left: 50%;
  transform: translateX(-50%);
  font-size: 12px;
  color: rgba(93, 71, 45, 0.7);
  background: rgba(246, 223, 180, 0.8);
  padding: 4px 14px;
  border-radius: 12px;
  z-index: 90;
  pointer-events: none;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  transition: opacity 0.3s;
}

#sandbox-hint.hidden { opacity: 0; pointer-events: none; }

/* ===== Modal ===== */
#modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}

#modal.hidden {
  display: none;
}

#modal-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

#modal-content {
  position: relative;
  z-index: 1;
  text-align: center;
  animation: modalIn 0.3s ease;
  max-width: 90vw;
}

#modal-card-wrap {
  width: 260px;
  max-height: 70vh;
  border-radius: 10px;
  overflow: hidden;
  border: 2px solid var(--gold);
  box-shadow: 0 0 40px var(--gold-glow);
  margin: 0 auto;
}

#modal-img {
  width: 100%;
  display: block;
}

#modal-placeholder {
  width: 260px;
  height: 400px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: linear-gradient(to bottom, #f5e6d3, #e8d5b7);
}

#modal-placeholder.hidden { display: none; }

#modal-placeholder .card-symbol { font-size: 60px; margin-bottom: 12px; }
#modal-placeholder .card-label { font-size: 18px; color: #4a3a2a; font-weight: 600; }
#modal-placeholder .card-number { font-size: 14px; color: var(--gold-dim); margin-top: 8px; }

#modal-name {
  margin-top: 16px;
  font-size: 18px;
  color: var(--gold);
  font-weight: 600;
}

#modal-energy {
  margin-top: 10px;
  font-size: 14px;
  color: var(--sand-light);
  padding: 6px 16px;
  border-radius: 12px;
  background: rgba(141, 111, 73, 0.6);
  display: inline-block;
}

#modal-energy.hidden { display: none; }

#modal-close {
  position: absolute;
  top: -40px;
  right: 0;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid var(--gold-dim);
  background: rgba(10, 10, 26, 0.8);
  color: var(--text);
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

@keyframes modalIn {
  from { opacity: 0; transform: scale(0.85); }
  to { opacity: 1; transform: scale(1); }
}

/* ===== Confirm Dialog ===== */
#confirm-dialog {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
}

#confirm-dialog.hidden { display: none; }

#confirm-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
}

#confirm-box {
  position: relative;
  z-index: 1;
  background: var(--bg-light);
  border: 1px solid var(--gold-dim);
  border-radius: 12px;
  padding: 24px;
  text-align: center;
  max-width: 280px;
  animation: modalIn 0.2s ease;
}

#confirm-box p {
  font-size: 14px;
  line-height: 1.6;
  margin-bottom: 20px;
  color: var(--text);
}

.confirm-buttons {
  display: flex;
  gap: 12px;
  justify-content: center;
}

.confirm-buttons button {
  padding: 8px 24px;
  border-radius: 8px;
  border: 1px solid var(--gold-dim);
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s;
}

#confirm-yes {
  background: var(--gold);
  color: var(--bg);
  border-color: var(--gold);
  font-weight: 600;
}

#confirm-no {
  background: transparent;
  color: var(--text);
}

/* ===== Responsive ===== */
@media (max-width: 400px) {
  :root {
    --card-w: 60px;
    --card-h: 92px;
  }
  .deck-card {
    width: 42px;
    height: 65px;
  }
}

@media (min-width: 768px) {
  :root {
    --card-w: 90px;
    --card-h: 138px;
    --drawer-h: 230px;
  }
  .deck-card {
    width: 56px;
    height: 86px;
  }
  #modal-card-wrap {
    width: 320px;
  }
}
