@layer base, components, utilities;

@layer base {
  :root {
    --dashboard-bg: var(--bg);
    --text-primary: var(--text);
    --text-muted: var(--muted);
    --positive: var(--success);
    --negative: var(--danger);
    --warning: var(--warning);
    --accent: var(--accent);
    
    --spacing-xl: 32px;
    
    --radius-sm: 6px;
    --radius-md: 8px;
    --radius-lg: 12px;
    
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.1);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.15);
    --shadow-lg: 0 8px 24px rgba(0,0,0,0.2);
  }
}

@layer components {
  /* Dashboard Layout */
  .home-dashboard {
    padding: var(--spacing-sm);
    background: var(--dashboard-bg);
    min-height: 100%;
    height: 100%;
    box-sizing: border-box; /* include padding in height to prevent overflow */
    padding-bottom: 10px; /* avoid extra bottom space causing scroll */
    overflow-y: auto;
    /* Page-scoped brand overrides */
    --accent: #0964A0;
    --accent-weak: #2B82B6;
    --toggle-bg: rgba(173, 173, 173, 0.2);
    --toggle-border: #ADADAD;
    --toggle-label: #424242;
  }
  
  .dashboard-grid {
    display: grid;
    gap: 6px;
    grid-template-columns: 1fr;
    /* grid-template-rows: auto auto auto auto; */
    grid-template-rows: auto auto 1fr 1fr;
    grid-auto-rows: minmax(0, auto);
    height: 100%;
    max-height: 100%;
    overflow: hidden; /* children manage scrolling if needed */
  }

  /* Ensure sparkline strip spans full width across the grid */
  .sparkline-section {
    grid-column: 1 / -1;
    grid-row: 1;
  }
  
  @media (min-width: 1024px) {
    .dashboard-grid {
      grid-template-columns: 1fr 1fr;
    }
  }
  
  @media (min-width: 1280px) {
    .dashboard-grid {
      grid-template-columns: 1fr 1fr 1fr;
    }
  }

  /* Sparkline Cards */
  .sparkline-container {
    display: flex;
    flex-wrap: nowrap;
    gap: 10px; /* legacy spacing */
    /* margin-bottom: var(--spacing-md); */
    overflow-x: auto;
    overflow-y: hidden;
    white-space: nowrap; /* legacy behavior */
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
    touch-action: pan-x;
    width: 100%;
    padding-bottom: 2px; /* prevent scrollbar overlap */
    scroll-snap-type: x proximity;
    pointer-events: auto;
    z-index: 2;
  }
  
  .sparkline-card {
    background: var(--card-bg);
    border: 1px solid var(--card-border, #ccc); /* legacy border tone */
    border-radius: 8px; /* legacy radius */
    padding: var(--spacing-sm);
    box-shadow: 0 2px 5px rgba(0,0,0,0.1); /* legacy shadow */
    transition: box-shadow 0.2s ease, transform 0.2s ease;
    flex: 0 0 200px; /* legacy width */
    min-width: 150px;
    scroll-snap-align: start;
  }
  
  .sparkline-card:hover {
    box-shadow: var(--shadow-md);
    transform: translateY(-2px);
  }
  
  .card-title {
    font-size: 10px;
    font-weight: 600;
    color: var(--text-muted);
    /* margin-bottom: var(--spacing-sm); */
    text-transform: uppercase;
    letter-spacing: 0.5px;
  }
  
  .card-body {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--spacing-md);
  }
  
  .sparkline-inner {
    flex: 1;
    min-width: 0;
  }
  
  .sparkline-chart {
    height: 40px;
    width: 100%;
  }
  
  .return-value {
    font-size: 12px;
    font-weight: 600;
    min-width: 60px;
    text-align: right;
  }
  
  .return-value.positive {
    color: var(--positive);
  }
  
  .return-value.negative {
    color: var(--negative);
  }

  /* Control Toggles */
  .dashboard-controls {
    display: flex;
    align-items: center;
    gap: var(--spacing-md);
    /* margin-bottom: var(--spacing-md); */
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 2px;
    grid-column: 1 / -1; /* own row below sparklines */
    grid-row: 2;
  }
  
  .data-switch-toggle {
    display: inline-flex;
    align-items: center;
    background: var(--toggle-bg);
    border: 1px solid var(--toggle-border);
    border-radius: 22px;
    padding: 2px;
    gap: 4px;
    height: 28px;
    box-sizing: border-box;
    backdrop-filter: saturate(140%) blur(2px);
  }
  
  .data-switch-toggle input[type="radio"] {
    position: absolute;
    opacity: 0;
    pointer-events: none;
  }
  
  .data-switch-toggle label {
    padding: 5px 8px;
    font-size: 11px;
    font-weight: 600;
    color: var(--toggle-label);
    cursor: pointer;
    border-radius: 20px;
    transition: background-color 0.2s ease, color 0.2s ease;
    white-space: nowrap;
    line-height: 1;
  }
  
  .data-switch-toggle input:checked + label {
    background: var(--accent);
    color: #fff;
  }
  
  .data-switch-toggle label:hover {
    background: rgba(0, 0, 0, 0.05);
  }

  .data-switch-toggle input:focus-visible + label {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
  }
  
  .data-switch-toggle label.disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }
  
  .last-update {
    margin-left: auto;
    font-size: 12px;
    color: var(--text-muted);
    font-style: italic;
  }

  /* Widgets */
  .widget-container {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: var(--radius-lg);
    padding: var(--spacing-md);
    margin-bottom: 2px;
    box-shadow: var(--shadow-sm);
    min-width: 0;
    display: grid;
    grid-template-rows: auto 1fr;
    min-height: 0;
  }
  /* Remove bottom margin from last block to avoid page scroll */
  .dashboard-grid > :last-child { margin-bottom: 0; }

  /* Top row wrapper (contains 3 cards) */
  .dashboard-top-row {
    grid-row: 3;
    grid-column: 1 / -1;
    display: grid;
    gap: var(--spacing-md);
    grid-template-columns: 1fr; /* stacks on small screens */
    min-height: 0;
    align-items: stretch;
    align-content: stretch;
  }
  .dashboard-top-row .widget-container { height: auto; min-height: 0; grid-template-rows: auto auto; }
  .dashboard-top-row .widget-container { margin-bottom: 0; }

  /* Bottom row wrapper (2 across) */
  .dashboard-bottom-row {
    grid-row: 4;
    grid-column: 1 / -1;
    display: grid;
    gap: var(--spacing-md);
    grid-template-columns: 1fr;
    min-height: 0;
    align-items: stretch;
    align-content: stretch;
  }
  .dashboard-bottom-row .grid-container { height: 100%; }
  /* Let bottom row flex to available space within viewport */
  .dashboard-bottom-row .widget-container { height: auto; }
  .dashboard-bottom-row .widget-container { margin-bottom: 0; }
  .dashboard-bottom-row #gainersGrid,
  .dashboard-bottom-row #contentCenterGrid { height: 100%; max-height: none; }
  
  .widget-title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    max-height: 30px;
    margin: 0 0 var(--spacing-sm) 0;
    padding: var(--spacing-xs) 0;
    border-bottom: 1px solid var(--card-border);
  }

  /* Only bottom-row grids should scroll inside available space */
  .dashboard-bottom-row .widget-container > :last-child {
    min-height: 0;
    overflow: auto;
  }
  .dashboard-bottom-row .widget-container > .ag-theme-balham { height: 100%; overflow: auto; }
  
  .title-text {
    font-size: 12px;
    font-weight: 600;
    color: var(--text);
  }
  
  .title-link {
    font-size: 12px;
    color: var(--accent);
    text-decoration: none;
    font-weight: 500;
    transition: color 0.2s ease;
  }
  
  .title-link:hover {
    color: var(--accent-weak);
  }

  /* Returns Widget */
  .returns-widget {
    display: grid;
    gap: var(--spacing-xs);
  }
  
  .grid-row {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr auto;
    gap: var(--spacing-xs);
    align-items: center;
    padding: var(--spacing-xs) 0;
  }
  
  .return-cell {
    text-align: center;
    font-size: 12px;
    font-weight: 600;
    padding: 20px;
    /* padding: var(--spacing-xs) var(--spacing-sm); */
    border-radius: var(--radius-sm);
  }
  
  .return-cell.positive {
    background: rgba(31, 169, 113, 0.1);
    color: var(--positive);
  }
  
  .return-cell.negative {
    background: rgba(224, 91, 91, 0.1);
    color: var(--negative);
  }
  
  .header-cell {
    font-size: 12px;
    font-weight: 500;
    color: var(--text-muted);
    text-align: center;
  }

  /* Bar Chart Widget */
  .bar-chart-widget {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-xs);
  }
  
  .bar {
    display: flex;
    align-items: center;
    gap: var(--spacing-sm);
  }
  
  .bar .label {
    min-width: 120px;
    font-size: 12px;
    color: var(--text-muted);
    text-align: right;
  }
  
  .bar-container {
    flex: 1;
    height: 20px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: var(--radius-sm);
    overflow: hidden;
    position: relative;
  }
  
  .bar-fill {
    height: 100%;
    position: absolute;
    top: 0;
    transition: width 0.3s ease;
  }
  
  .bar-fill.positive-bar {
    background: var(--positive);
    left: 50%;
    transform-origin: left;
  }
  
  .bar-fill.negative-bar {
    background: var(--negative);
    right: 50%;
    transform-origin: right;
  }
  
  .label-text {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    font-size: 10px;
    font-weight: 600;
    color: white;
    text-shadow: 0 1px 2px rgba(0,0,0,0.5);
  }

  /* Map and Grid Containers */
  .map-container {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: var(--radius-lg);
    padding: var(--spacing-sm);
    height: 220px;
    min-height: 220px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  /* Ensure map inside dashboard can grow with content beyond the base size */
  .home-dashboard #map-container { height: auto; min-height: 220px; }
  
  .grid-container {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: var(--radius-lg);
    padding: var(--spacing-md);
  }

  /* Match legacy explicit heights */
  #gainersGrid, #contentCenterGrid {
    width: 100%;
  }
  .dashboard-bottom-row #gainersGrid,
  .dashboard-bottom-row #contentCenterGrid {
    height: 100%;
    max-height: none;
  }
  
  .ag-theme-balham {
    --ag-background-color: var(--card-bg);
    --ag-foreground-color: var(--text);
    --ag-border-color: var(--card-border);
    --ag-header-background-color: var(--bg-elev);
    --ag-odd-row-background-color: rgba(255, 255, 255, 0.02);
    --ag-row-hover-color: rgba(255, 255, 255, 0.05);
  }

  /* Responsive Design */
  @media (max-width: 768px) {
    .home-dashboard {
      padding: var(--spacing-sm);
    }
    
    .sparkline-container {
      grid-template-columns: 1fr;
    }
    
    .dashboard-controls {
      flex-direction: column;
      align-items: stretch;
    }
    
    .data-switch-toggle {
      justify-content: center;
    }
    
    .last-update {
      margin-left: 0;
      text-align: center;
    }
    
    .grid-row {
      grid-template-columns: 1fr;
      gap: var(--spacing-xs);
      text-align: center;
    }
    
    .bar {
      flex-direction: column;
      align-items: stretch;
      gap: var(--spacing-xs);
    }
    
    .bar .label {
      text-align: left;
      min-width: auto;
    }
  }
}

@layer utilities {
  /* Row layout for second row: three cards inline */
  .grid-row-2 {
    grid-column: auto;
  }

  @media (min-width: 1024px) {
    .dashboard-grid {
      grid-template-columns: repeat(3, 1fr);
      grid-auto-flow: row;
    }
    .dashboard-top-row { grid-template-columns: repeat(3, 1fr); }
    .dashboard-bottom-row { grid-template-columns: 1fr 1fr; }
  }
  
  @media (max-width: 1023px) {
    .grid-row-2 { grid-column: 1 / -1; }
  }
  .text-right { text-align: right; }
  .text-left { text-align: left; }
  
  .mb-0 { margin-bottom: 0; }
  .mt-0 { margin-top: 0; }
}
