/* History (Historial) - Final Layout Fix v2 */

#history-section {
  padding-top: 20px;
  padding-bottom: 20px;
  /* margin, width, and ad padding handled by global.css */
}

@media (max-width: 768px) {
  #history-section {
    padding-top: var(--header-height-condensed); /* Clear sticky header */
  }
}

#history-section.hidden {
  display: none !important;
}

.history-page-wrapper {
  width: 100%;
  max-width: 1000px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center; /* Center children like .history-card */
}

.history-page-title {
  font-family: var(--font-header);
  font-size: clamp(1.8rem, 6vw, 2.5rem);
  font-weight: 700;
  color: var(--text-main);
  margin-bottom: 24px;
  text-align: center;
}

.history-card {
  margin: 0 auto; /* Ensure centering as a block */
  background: var(--bg-card);
  border-radius: 16px;
  /* Fluid padding: scales from 12px to 24px */
  padding: clamp(12px, 4vw, 24px);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
  border: 1px solid var(--glass-border);
  display: flex !important;
  flex-direction: column !important;
  align-items: center;
  min-height: auto;
  /* Sync with home panels margins */
  width: 95%;
  max-width: 600px;
  box-sizing: border-box;
}

.history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  max-width: 600px; /* Keep it tight */
  margin-bottom: 8px;
}

.history-header h2 {
  margin: 0;
  /* Fluid title: scales from 1.15rem to 1.5rem */
  font-size: clamp(1.15rem, 5vw, 1.5rem);
  color: var(--text-main);
  text-transform: capitalize;
}

.history-subtitle {
  color: var(--text-muted);
  margin-bottom: 24px;
  font-size: 0.95rem;
}

/* THE GRID - Fluid sizes using clamp */
#history-grid-container {
  /* Fits 7 columns + gaps in ~300px at minimum scaling */
  --cell-size: clamp(34px, 12vw, 60px);
  --gap-size: clamp(4px, 1.5vw, 8px);
  display: grid !important;
  grid-template-columns: repeat(7, var(--cell-size)) !important;
  gap: var(--gap-size);
  justify-content: center;
  margin: 0 auto;
  max-width: 100%; /* Safety */
}

#history-grid-container .calendar-day {
  width: var(--cell-size);
  height: var(--cell-size);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  font-weight: 600;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.05);
  cursor: pointer;
  position: relative;
  transition: all 0.2s ease;
  border: 1px solid transparent;
}

body.dark-mode #history-grid-container .calendar-day {
  background: rgba(255, 255, 255, 0.08); /* Slightly more visible */
}

#history-grid-container .calendar-day:hover {
  transform: translateY(-2px);
  background: rgba(0, 0, 0, 0.1);
  border-color: var(--brand-color);
}

body.dark-mode #history-grid-container .calendar-day:hover {
  background: rgba(255, 255, 255, 0.15);
}

#history-grid-container .calendar-day.header-day {
  background: transparent !important;
  cursor: default;
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  font-weight: 700;
  border: none !important;
  height: 30px; /* Shorter header */
}

#history-grid-container .calendar-day.empty {
  background: transparent !important;
  cursor: default;
  border: none !important;
}

#history-grid-container .calendar-day.win {
  background: #22c55e !important;
  color: #064e3b !important; /* Dark Green Text for Light Mode */
}

#history-grid-container .calendar-day.loss {
  background: #eab308 !important;
  color: #1e293b !important; /* Dark Slate Text for Light Mode */
}

/* Dark Mode Overrides for History Colors */
body.dark-mode #history-grid-container .calendar-day.win {
  background: #22c55e !important;
  color: white !important;
}

body.dark-mode #history-grid-container .calendar-day.loss {
  background: #ca8a04 !important; /* Darker Gold for Dark Mode */
  color: white !important;
}

/* Markers */
#history-grid-container .completed-dot {
  position: absolute;
  bottom: calc(var(--cell-size) * 0.05);
  right: calc(var(--cell-size) * 0.03);
  font-size: calc(var(--cell-size) * 0.3);
  line-height: 1;
  pointer-events: none;
  filter: drop-shadow(0 2px 2px rgba(0, 0, 0, 0.2));
}

/* Nav Buttons */
.icon-btn-medium {
  background: var(--bg-card);
  border: 1px solid var(--glass-border);
  border-radius: 10px;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  cursor: pointer;
  color: var(--text-main);
}

.icon-btn-medium:hover {
  background: var(--brand-color);
  color: white;
  border-color: var(--brand-color);
}

.icon-btn-medium.disabled {
  opacity: 0.4;
  filter: grayscale(1);
  pointer-events: none;
  cursor: default;
}

/* Responsive refinements handled by fluid layouts above */
@media (max-width: 360px) {
  .history-header h2 {
    font-size: 1.1rem; /* Extra safety for ultra small */
  }
}

/* History Legend */
.history-legend-card {
  margin-top: 24px;
  width: 95%;
  max-width: 600px;
  padding: clamp(16px, 4vw, 24px);
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  background: var(--bg-card);
  border-radius: 16px;
  border: 1px solid var(--glass-border);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.history-legend-card h3 {
  margin: 0 0 12px 0;
  font-family: var(--font-header);
  font-size: 1.2rem;
  color: var(--text-main);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.legend-nav-info {
  font-size: 0.9rem;
  color: var(--text-muted);
  margin-bottom: 20px;
  line-height: 1.4;
}

.legend-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 16px;
  width: 100%;
}

.legend-list li {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 0.9rem;
  color: var(--text-main);
  text-align: left;
}

.legend-box {
  width: 24px;
  height: 24px;
  border-radius: 6px;
  flex-shrink: 0;
  border: 1px solid rgba(0, 0, 0, 0.1);
}

.legend-box.gray {
  background: rgba(0, 0, 0, 0.05);
}

body.dark-mode .legend-box.gray {
  background: rgba(255, 255, 255, 0.08);
  border-color: rgba(255, 255, 255, 0.1);
}

.legend-box.yellow {
  background: #eab308;
}

.legend-box.green {
  background: #22c55e;
}

.legend-icon {
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  flex-shrink: 0;
}

@media (max-width: 600px) {
  .legend-list {
    grid-template-columns: 1fr;
  }
  .history-legend-card {
    margin-top: 16px;
  }
}

/* v1.5.62: Floating History Tooltip */
.history-tooltip-container {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 300000;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(4px);
    display: none;
}

.history-tooltip-container.visible {
    display: block !important;
}

.history-tooltip {
    position: fixed;
    z-index: 300001; /* Above container backdrop */
    pointer-events: none;
    background: var(--bg-paper);
    border: 1px solid var(--glass-border);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-radius: 12px;
    padding: 16px;
    box-shadow: var(--paper-shadow);
    color: var(--text-main);
    z-index: 100;
    font-size: 0.85rem;
    min-width: 220px;
    user-select: none;
    -webkit-user-select: none;
    transition: opacity 0.1s ease, transform 0.1s ease;
    opacity: 0;
    transform: translateY(10px) scale(0.95);
    display: block !important;
}

.history-tooltip:not(.hidden) {
    opacity: 1;
    display: block;
}

.history-tooltip.mobile-mode {
    position: fixed !important;
    top: 50% !important;
    left: 50% !important;
    transform: translate(-50%, -50%) !important;
    width: 90% !important;
    max-width: 320px !important;
    margin: 0 !important;
    box-shadow: 0 30px 60px rgba(0,0,0,0.8);
    z-index: 300001 !important;
    display: block !important;
    opacity: 1 !important;
    pointer-events: auto !important;
}

.history-tooltip.mobile-mode .tooltip-title {
    padding-right: 35px; /* Space for the X button */
}

.tooltip-close {
    position: absolute;
    top: 4px;
    right: 10px;
    width: 30px;
    height: 30px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.2rem;
    color: var(--text-muted);
    cursor: pointer;
    transition: background 0.2s;
    z-index: 300002;
}

.tooltip-close:hover {
    background: rgba(255, 255, 255, 0.2);
}

.tooltip-title {
    font-weight: 700;
    color: #4ade80;
    margin-bottom: 8px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--glass-border);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.tooltip-section {
    margin-bottom: 12px;
}

.tooltip-section-title {
    font-size: 0.7rem;
    color: #94a3b8;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 4px;
    display: block;
}

.tooltip-grid {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 4px 12px;
    align-items: center;
}

.tooltip-label {
    color: var(--text-muted);
    font-size: 0.75rem;
}

.tooltip-value {
    font-weight: 600;
    text-align: right;
    font-variant-numeric: tabular-nums;
}

.tooltip-value.highlight {
    color: #4ade80;
}

@media (max-width: 768px) {
    /* Tooltip is enabled in mobile-mode via JavaScript */
}
