/* === Reset & Base === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }
body {
  font-family: 'Segoe UI', 'Noto Sans SC', system-ui, sans-serif;
  background: #2c1810;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 16px;
}

#app {
  max-width: 720px;
  width: 100%;
  background: #f5e6d3;
  border-radius: 32px;
  padding: 24px 28px;
  box-shadow: 0 8px 40px rgba(0,0,0,0.35);
  border: 1px solid rgba(139,69,19,0.15);
}

/* === Title === */
#title-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  gap: 12px;
}
#title-bar h1 { font-size: 1.5rem; color: #4a2c1a; }

/* Title bar dice display */
#title-dice {
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(255,255,255,0.5);
  padding: 4px 12px;
  border-radius: 12px;
  font-weight: bold;
  color: #5a3a2a;
}
.dice-mini {
  font-size: 1.4rem;
  line-height: 1;
  display: inline-block;
  transition: transform 0.3s;
}
.dice-mini.rolling {
  animation: diceRoll 0.6s ease-out;
}
#title-dice #dice-result {
  font-size: 1rem;
  min-width: 20px;
  text-align: center;
}

/* Title bar history button — subtle, icon-only */
.btn-history {
  background: rgba(255,255,255,0.4);
  border: 1px solid rgba(139,69,19,0.2);
  font-size: 1rem;
  color: #5a3a2a;
  cursor: pointer;
  padding: 4px 10px;
  border-radius: 10px;
  transition: background 0.2s, color 0.2s;
  line-height: 1;
  white-space: nowrap;
}
.btn-history:hover { background: rgba(255,255,255,0.7); color: #4a2c1a; }

/* === Game Area (board only, no panels) === */
#game-area {
  display: flex;
  justify-content: center;
  position: relative;
}

/* === Player Panels (spacious standalone sections) === */
.player-panel {
  width: 100%;
  max-width: 560px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 18px;
  border-radius: 14px;
  background: rgba(255,255,255,0.65);
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
  margin: 0 auto;
}
#panel-cow { margin-bottom: 10px; }
#panel-farmer { margin-top: 10px; margin-bottom: 0; }
.panel-avatar {
  width: 40px; height: 40px;
  border-radius: 50%;
  background: #e8ddd0;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.2rem;
  flex-shrink: 0;
  box-shadow: 0 2px 6px rgba(0,0,0,0.1);
}
.panel-info {
  display: flex;
  align-items: center;
  gap: 14px;
  font-size: 0.95rem;
  color: #5a3a2a;
  flex: 1;
}
.panel-header { font-weight: bold; color: #4a2c1a; white-space: nowrap; font-size: 1rem; }
.info-position { white-space: nowrap; font-size: 0.9rem; }
.info-status { font-size: 0.85rem; color: #8a6a5a; white-space: nowrap; }

/* Per-player roll button */
.btn-roll {
  padding: 6px 16px;
  border: none;
  border-radius: 10px;
  background: #8B4513;
  color: white;
  font-size: 0.85rem;
  cursor: pointer;
  transition: background 0.2s, opacity 0.2s;
  white-space: nowrap;
  flex-shrink: 0;
}
.btn-roll:hover:not(:disabled) { background: #a0522d; }
.btn-roll:active:not(:disabled) { transform: scale(0.97); }
.btn-roll:disabled {
  background: #d0c8bc;
  color: #a09080;
  cursor: not-allowed;
  opacity: 0.6;
}

/* === Board === */
#board-container {
  display: flex;
  justify-content: center;
  background: rgba(139,69,19,0.08);
  border-radius: 16px;
  padding: 16px;
  width: 100%;
  box-shadow:
    inset 0 2px 4px rgba(0,0,0,0.05),
    0 4px 12px rgba(0,0,0,0.1);
}
#board-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 5px;
  max-width: 560px;
  width: 100%;
}
.board-cell {
  aspect-ratio: 1;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  text-align: center;
  padding: 2px;
  position: relative;
  cursor: default;
  transition: transform 0.2s, box-shadow 0.2s;
}
.board-cell.path-cell { box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
.board-cell.deco-cell { font-size: 1.5rem; background: transparent; }
.board-cell .piece-marker {
  position: absolute;
  width: 20px; height: 20px;
  border-radius: 50%;
  border: 2px solid white;
  box-shadow: 0 2px 4px rgba(0,0,0,0.3);
  font-size: 0.7rem;
  display: flex; align-items: center; justify-content: center;
}
.piece-farmer { background: #8B4513; top: -6px; left: -6px; }
.piece-cow { background: #a0522d; bottom: -6px; right: -6px; }

/* === Dice Area (compact => moved to title bar) === */
@keyframes diceRoll {
  0% { transform: rotateX(0deg) rotateY(0deg); }
  50% { transform: rotateX(360deg) rotateY(360deg); }
  100% { transform: rotateX(720deg) rotateY(720deg); }
}

.btn-primary {
  padding: 10px 24px;
  border: none;
  border-radius: 12px;
  background: #8B4513;
  color: white;
  font-size: 1rem;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
}
.btn-primary:hover { background: #a0522d; }
.btn-primary:active { transform: scale(0.97); }
.btn-primary:disabled { background: #ccc; cursor: not-allowed; }

.dice-result { font-size: 1.2rem; font-weight: bold; min-width: 40px; }

/* === Message Bar === */
#message-bar {
  text-align: center;
  margin-top: 16px;
  padding: 12px 16px;
  color: #5a3a2a;
  font-size: 0.95rem;
  background: rgba(255,255,255,0.35);
  border-radius: 12px;
}

/* === Popup Overlay === */
#popup-overlay, #gameover-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
#popup-card, #gameover-card {
  background: white;
  border-radius: 20px;
  padding: 32px;
  max-width: 360px;
  width: 90%;
  text-align: center;
  box-shadow: 0 8px 32px rgba(0,0,0,0.3);
}
#popup-card { position: relative; }
.popup-close {
  position: absolute; top: 8px; right: 12px;
  background: none; border: none;
  font-size: 1.5rem; color: #8a6a5a; cursor: pointer;
  line-height: 1; padding: 4px 8px; border-radius: 8px;
}
.popup-close:hover { background: rgba(0,0,0,0.06); color: #4a2c1a; }
#popup-timer {
  font-size: 2.5rem; font-weight: bold; color: #8B4513;
  margin-bottom: 16px;
}
#popup-timer .timer-label { font-size: 1rem; font-weight: normal; color: #8a6a5a; }
#popup-type-icon { font-size: 2.5rem; margin-bottom: 8px; }
#popup-title { font-size: 1.3rem; margin-bottom: 12px; color: #4a2c1a; }
#popup-description { font-size: 1rem; color: #5a3a2a; margin-bottom: 20px; line-height: 1.5; }

.hidden { display: none !important; }

/* === Dice Roll Modal === */
#roll-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}
#roll-card {
  background: rgba(255,255,255,0.95);
  border-radius: 28px;
  padding: 48px 56px;
  text-align: center;
  box-shadow: 0 8px 40px rgba(0,0,0,0.3);
  animation: rollIn 0.2s ease-out;
}
@keyframes rollIn {
  0% { transform: scale(0.8); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}
#roll-dice-display {
  font-size: 5rem;
  line-height: 1;
  margin-bottom: 16px;
}
#roll-dice-display.rolling {
  animation: diceRoll 0.5s ease-out;
}
#roll-result-text {
  font-size: 2.5rem;
  font-weight: bold;
  color: #4a2c1a;
}

/* Cell number badge */
.cell-number {
  position: absolute;
  top: 2px;
  right: 4px;
  font-size: 0.55rem;
  opacity: 0.4;
}

/* === Responsive — narrow screens (mobile) === */
@media (max-width: 700px) {
  html { min-height: 100dvh; }
  html, body {
    overflow-y: auto;
    overflow-x: hidden;
  }
  body {
    align-items: flex-start;
    padding: 0;
    min-height: 100dvh;
  }
  #app {
    padding: 16px;
    border-radius: 20px;
    min-height: 100dvh;
  }
  #app::before { border-radius: 20px; }
  .player-panel { padding: 6px 10px; gap: 8px; }
  .panel-avatar { width: 30px; height: 30px; font-size: 1rem; }
  .panel-info { font-size: 0.8rem; gap: 8px; }
  .panel-header { font-size: 0.85rem; }
  #panel-cow { margin-bottom: 6px; }
  #panel-farmer { margin-top: 6px; }
  #board-grid { gap: 3px; }
  #board-container { padding: 8px; }
  #title-bar { margin-bottom: 8px; }
  #title-bar h1 { font-size: 1.15rem; }
  #message-bar { margin-top: 6px; padding: 6px 10px; font-size: 0.8rem; }
}

/* === Responsive — short screens (compact layout) === */
@media (max-height: 850px) {
  #app { padding: 16px; }
  .player-panel { padding: 8px 14px; gap: 10px; }
  .panel-avatar { width: 34px; height: 34px; font-size: 1rem; }
  .panel-info { font-size: 0.8rem; gap: 8px; }
  .panel-header { font-size: 0.85rem; }
  #panel-cow { margin-bottom: 6px; }
  #panel-farmer { margin-top: 6px; }
  #title-bar { margin-bottom: 8px; }
  #title-bar h1 { font-size: 1.1rem; }
  #message-bar { margin-top: 6px; padding: 6px 10px; font-size: 0.8rem; }
  .btn-roll { padding: 4px 12px; font-size: 0.8rem; }
  #board-container { padding: 8px; }
}

/* === Responsive — very short screens === */
@media (max-height: 960px) {
  html {
    min-height: 100%;
    height: auto;
    overflow-y: auto;
  }
  body {
    align-items: flex-start;
    padding-top: 0;
    padding-bottom: 0;
    overflow-y: auto;
  }
  #app { padding: 10px; }
  .player-panel { padding: 4px 10px; }
  #message-bar { padding: 4px 8px; }
}

/* Small screens: 8x8 cells get tiny */
@media (max-width: 500px) {
  #board-grid { gap: 2px; }
  .board-cell.path-cell .cell-label { font-size: 0.5rem; line-height: 1.1; }
  .board-cell.deco-cell { font-size: 1rem; }
  .cell-number { display: none; }
}

/* === Indoor Cozy Room Theme === */
#app {
  background:
    radial-gradient(ellipse at 50% 100%, rgba(139,69,19,0.3), transparent 70%),
    linear-gradient(180deg, #f5e6d3, #e8d5be);
  border: 2px solid #8B4513;
  position: relative;
  overflow: hidden;
}

/* Floor planks */
#app::before {
  content: '';
  position: absolute;
  inset: 0;
  background: repeating-linear-gradient(
    90deg,
    transparent,
    transparent 60px,
    rgba(139,69,19,0.06) 60px,
    rgba(139,69,19,0.06) 61px
  );
  pointer-events: none;
  border-radius: 32px;
}

/* Warm lighting gradient overlay */
#game-area::after {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: radial-gradient(ellipse at 30% 20%, rgba(255,200,100,0.15), transparent 60%);
  pointer-events: none;
  z-index: 0;
}

/* Keep content above overlay */
#board-container, #message-bar, #title-bar, .player-panel {
  position: relative;
  z-index: 1;
}


/* Board cell enhancements */
.board-cell.path-cell {
  transition: transform 0.2s, box-shadow 0.2s;
  cursor: default;
}
.board-cell.path-cell:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0,0,0,0.15);
}
.board-cell.path-cell .cell-label {
  font-size: 0.65rem;
  line-height: 1.15;
  max-width: 100%;
}
.board-cell.deco-cell {
  background: transparent;
  font-size: 1.8rem;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0.7;
}

/* Piece markers — dual player pieces */
.piece-marker {
  position: absolute;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  z-index: 10;
  box-shadow: 0 2px 8px rgba(0,0,0,0.4);
  pointer-events: none;
}
.piece-farmer {
  background: #B8860B;
  border: 2px solid #DAA520;
  top: -12px;
  left: -12px;
}
.piece-cow {
  background: #FFD700;
  border: 2px solid #B8860B;
  bottom: -12px;
  right: -12px;
}

/* Active cell — farmer gold / cow blue */
.board-cell.active-farmer, .board-cell.moving-farmer {
  animation: farmerGlow 1.5s ease-in-out infinite;
  z-index: 5;
}
@keyframes farmerGlow {
  0%, 100% { box-shadow: 0 0 8px 4px rgba(255,215,0,0.3), inset 0 0 6px rgba(255,215,0,0.1); }
  50% { box-shadow: 0 0 20px 12px rgba(255,200,50,0.6), 0 0 40px 20px rgba(255,200,50,0.2), inset 0 0 12px rgba(255,200,50,0.2); transform: scale(1.08); }
}
.board-cell.active-cow, .board-cell.moving-cow {
  animation: cowGlow 1.5s ease-in-out infinite;
  z-index: 5;
}
@keyframes cowGlow {
  0%, 100% { box-shadow: 0 0 8px 4px rgba(100,180,255,0.3), inset 0 0 6px rgba(100,180,255,0.1); }
  50% { box-shadow: 0 0 20px 12px rgba(70,150,255,0.6), 0 0 40px 20px rgba(70,150,255,0.2), inset 0 0 12px rgba(70,150,255,0.2); transform: scale(1.08); }
}

/* Scrollable cell labels on small screens */
@media (max-width: 700px) {
  .board-cell.path-cell .cell-label { font-size: 0.5rem; }
  .board-cell { font-size: 0.6rem; }
  .board-cell.deco-cell { font-size: 1.2rem; }
  .piece-marker { width: 24px; height: 24px; font-size: 0.7rem; }
  .piece-farmer { top: -8px; left: -8px; }
  .piece-cow { bottom: -8px; right: -8px; }
}

/* Game over special effects */
#gameover-card {
  animation: celebrateIn 0.5s ease-out;
}
@keyframes celebrateIn {
  0% { transform: scale(0.5) rotate(-5deg); opacity: 0; }
  100% { transform: scale(1) rotate(0deg); opacity: 1; }
}

/* === Avatar blink (roll phase indicator) === */
@keyframes avatarBlink {
  0%, 100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(139,69,19,0); }
  50% { transform: scale(1.1); box-shadow: 0 0 16px 8px rgba(139,69,19,0.35), 0 0 30px 12px rgba(139,69,19,0.15); }
}
.avatar-blink {
  animation: avatarBlink 0.8s ease-in-out infinite;
}

/* Button variants */
.btn-secondary {
  padding: 10px 24px;
  border: 2px solid #8B4513;
  border-radius: 12px;
  background: transparent;
  color: #8B4513;
  font-size: 1rem;
  cursor: pointer;
  margin-left: 8px;
}
.btn-secondary:hover { background: rgba(139,69,19,0.1); }

/* === History dialog === */
#history-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
}
#history-card {
  background: white;
  border-radius: 20px;
  padding: 24px;
  max-width: 480px;
  width: 90%;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 8px 32px rgba(0,0,0,0.3);
}
#history-card h2 {
  text-align: center;
  font-size: 1.2rem;
  color: #4a2c1a;
  margin-bottom: 16px;
}
#history-list {
  flex: 1;
  overflow-y: auto;
  margin-bottom: 16px;
}
.history-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}
.history-table th {
  text-align: left;
  padding: 6px 8px;
  background: #f5e6d3;
  color: #5a3a2a;
  position: sticky;
  top: 0;
}
.history-table td {
  padding: 6px 8px;
  border-bottom: 1px solid #e8ddd0;
  color: #4a2c1a;
}
.history-table tr:last-child td { border-bottom: none; }
#history-close-btn {
  align-self: center;
  min-width: 100px;
}

@media (max-width: 500px) {
  #history-card { padding: 16px; max-width: 100%; }
  .history-table { font-size: 0.75rem; }
  .history-table th, .history-table td { padding: 4px 6px; }
}
