:root {
  --primary: #4f46e5;
  --primary-light: #e0e7ff;
  --bg: #f3f4f6;
  --card-bg: #ffffff;
  --text-main: #1f2937;
  --text-muted: #6b7280;
  --border: #e5e7eb;
  --weekend-bg: #f0f9ff;
  --weekend-text: #1e40af;
  --today-border: #4f46e5;
  --danger: #ef4444;
}

body.dark {
  --primary: #818cf8;
  --primary-light: #312e81;
  --bg: #111827;
  --card-bg: #1f2937;
  --text-main: #f3f4f6;
  --text-muted: #9ca3af;
  --border: #374151;
  --weekend-bg: #1e293b;
  --weekend-text: #60a5fa;
  --today-border: #818cf8;
}

* { box-sizing: border-box; }
body { margin: 0; font-family: 'Inter', sans-serif; background: var(--bg); color: var(--text-main); }

/* --- FIT TO SCREEN LAYOUT --- */
main { 
  width: 98%;        /* Use almost full width */
  max-width: 100%;   /* Remove pixel limit to prevent cutting off on wide screens */
  margin: 0 auto;    
  padding: 15px 0;   
}

/* HEADER */
header {
  display: flex; justify-content: space-between; align-items: center;
  background: var(--card-bg); padding: 10px 20px; border-radius: 12px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1); margin-bottom: 15px;
}
.controls { display: flex; gap: 8px; align-items: center; }

select, button { padding: 8px 12px; border: 1px solid var(--border); border-radius: 6px; background: var(--card-bg); color: var(--text-main); cursor: pointer; }
button.primary { background: var(--primary); color: white; border: none; }

/* DAILY RING */
.daily-ring-container { display: flex; align-items: center; gap: 10px; margin-right: 15px; }
.ring-svg { width: 32px; height: 32px; transform: rotate(-90deg); }
.ring-bg { stroke: var(--border); fill: none; stroke-width: 4; }
.ring-progress { stroke: var(--primary); fill: none; stroke-width: 4; stroke-dasharray: 100; stroke-dashoffset: 100; transition: stroke-dashoffset 0.5s ease; stroke-linecap: round; }

/* --- CHART ALIGNMENT (THE MAGIC FIX) --- */
.chart-alignment-wrapper {
  display: flex;
  background: var(--card-bg);
  border-top-left-radius: 12px;
  border-top-right-radius: 12px;
  padding: 20px 20px 0 20px; /* Padding matches grid padding */
  margin-bottom: 0; /* Connected to grid */
  border-bottom: 1px solid var(--border);
}

.chart-spacer {
  /* Must match Width of Name(180) + Streak(50) + Progress(80) */
  width: 310px; 
  flex-shrink: 0;
  display: flex;
  align-items: flex-end;
  padding-bottom: 10px;
}

.chart-container {
  flex-grow: 1;
  height: 150px; /* Slimmer chart */
  position: relative;
}

/* --- HABIT GRID (AUTO-FIT) --- */
.grid-container { 
  background: var(--card-bg); 
  border-bottom-left-radius: 12px; 
  border-bottom-right-radius: 12px; 
  overflow: hidden; 
  box-shadow: 0 4px 6px rgba(0,0,0,0.05); 
  padding: 0 20px 20px 20px;
}

.habit-table {
  display: grid;
  /* OLD: grid-template-columns: 180px 50px 80px repeat(31, 1fr); */
  
  /* NEW: Dynamic columns */
  grid-template-columns: 180px 50px 80px repeat(var(--month-days, 31), 1fr);
  
  gap: 1px;
  align-items: center;
}

/* CELLS */
.h-cell { text-align: center; font-size: 0.8rem; color: var(--text-muted); padding: 8px 0; }
.h-cell.weekend { background-color: var(--weekend-bg); color: var(--weekend-text); font-weight: bold; border-radius: 4px; }
.h-cell.today-col { border: 2px solid var(--today-border); border-radius: 4px; position: relative; z-index: 5; }
.row-name { font-weight: 500; display: flex; align-items: center; justify-content: space-between; padding-right: 10px; white-space: nowrap; overflow: hidden; }

/* PROGRESS BAR */
.row-progress { width: 100%; height: 6px; background: var(--border); border-radius: 3px; overflow: hidden; }
.row-fill { height: 100%; background: var(--primary); transition: width 0.3s; }
.progress-text { font-size: 0.75rem; color: var(--text-muted); margin-left: 8px; }

/* CHECKBOXES */
.day-chk { display: flex; justify-content: center; align-items: center; height: 36px; width: 100%; }
.custom-checkbox { 
  width: 20px; height: 20px; /* Slightly smaller to fit width */
  border: 2px solid var(--border); border-radius: 5px; 
  cursor: pointer; display: grid; place-items: center; transition: all 0.2s; 
}
.custom-checkbox.checked { background: var(--primary); border-color: var(--primary); }
.custom-checkbox.disabled { opacity: 0.3; cursor: not-allowed; background: var(--border); }
.custom-checkbox svg { width: 12px; height: 12px; color: white; transform: scale(0); transition: transform 0.2s; }
.custom-checkbox.checked svg { transform: scale(1); }
.streak-flame { font-size: 0.85rem; font-weight: bold; color: #f59e0b; display: flex; align-items: center; justify-content: center; gap: 2px; }
.gray-flame { filter: grayscale(100%); opacity: 0.4; }

/* --- FOOTER STATS --- */
.footer-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  margin-top: 20px;
}
.widget-card { background: var(--card-bg); padding: 15px; border-radius: 12px; box-shadow: 0 1px 3px rgba(0,0,0,0.05); }
.widget-title { margin: 0 0 15px 0; font-size: 0.85rem; color: var(--text-muted); text-transform: uppercase; font-weight: 700; }

/* Leaderboard */
.habit-list-item { display: flex; flex-direction: column; margin-bottom: 8px; padding-bottom: 8px; border-bottom: 1px solid var(--border); }
.habit-list-item:last-child { border-bottom: none; }
.list-header { display: flex; justify-content: space-between; font-size: 0.85rem; font-weight: 600; margin-bottom: 3px; }
.list-meta { font-size: 0.7rem; color: var(--text-muted); }
.mini-progress { width: 100%; height: 4px; background: var(--bg); border-radius: 2px; overflow: hidden; margin-top: 3px; }
.mini-fill { height: 100%; background: var(--primary); }

/* MODAL */
.modal-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); display: none; justify-content: center; align-items: center; z-index: 1000; backdrop-filter: blur(2px); }
.modal { background: var(--card-bg); padding: 24px; border-radius: 12px; width: 400px; box-shadow: 0 10px 25px rgba(0,0,0,0.2); }
.modal h2 { margin-top: 0; }
.modal label { font-size: 0.8rem; color: var(--text-muted); display: block; margin-bottom: 4px; }
.modal input { width: 100%; padding: 10px; margin-bottom: 15px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); color: var(--text-main); }
.modal-btns { display: flex; justify-content: flex-end; gap: 10px; }

/* UTILITY */
.icon-btn { font-size: 1rem; padding: 6px 10px; }


/* ... Keep :root and body styles as they are ... */

/* --- ANIMATED CARDS --- */
.widget-card, .chart-alignment-wrapper, .grid-container {
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

/* Hover Effect: Lifts the card slightly */
.widget-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 10px 20px -5px rgba(0, 0, 0, 0.1);
}

/* --- BUTTONS & INPUTS --- */
select, button, .icon-btn {
  padding: 8px 12px; 
  border: 1px solid var(--border); 
  border-radius: 6px; 
  background: var(--card-bg); 
  color: var(--text-main); 
  cursor: pointer;
  transition: all 0.2s ease; /* Smooth hover transition */
}

button.primary { 
  background: var(--primary); 
  color: white; 
  border: none; 
  box-shadow: 0 2px 5px rgba(79, 70, 229, 0.3);
}

button:hover { 
  opacity: 0.95; 
  transform: translateY(-1px);
}

button:active {
  transform: translateY(0);
}

/* --- CHECKBOX ANIMATION --- */
.custom-checkbox { 
  width: 20px; height: 20px; 
  border: 2px solid var(--border); border-radius: 6px; 
  cursor: pointer; display: grid; place-items: center; 
  transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1); /* Bouncy effect */
}

.custom-checkbox:hover {
  border-color: var(--primary);
  background: var(--primary-light);
}

.custom-checkbox.checked { 
  background: var(--primary); 
  border-color: var(--primary); 
  transform: scale(1.1); /* Pop effect when checked */
}

.custom-checkbox svg { 
  width: 12px; height: 12px; 
  color: white; 
  transform: scale(0) rotate(-45deg); 
  transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.custom-checkbox.checked svg { 
  transform: scale(1) rotate(0deg); 
}

/* --- PROGRESS BAR ANIMATION --- */
.row-fill { 
  height: 100%; 
  background: var(--primary); 
  transition: width 0.6s cubic-bezier(0.4, 0, 0.2, 1); /* Smoother fill */
}

/* ... Keep the rest of your layout/grid styles ... */

/* =========================================================
   MOBILE RESPONSIVENESS (Fixes Squished Layouts)
   ========================================================= */

@media screen and (max-width: 768px) {

  /* 1. RESET CONTAINER WIDTHS */
  main {
    width: 100%;
    padding: 10px;
    margin: 0;
  }

  /* 2. HEADER: Stack controls vertically */
  header {
    flex-direction: column;
    align-items: stretch;
    gap: 15px;
    padding: 15px;
  }

  .controls {
    flex-wrap: wrap; /* Allow buttons to wrap to next line */
    justify-content: center;
    gap: 8px;
  }
  
  .controls select, .controls button, .controls a {
    flex: 1 1 auto; /* Grow to fill space */
    font-size: 0.85rem;
    padding: 8px 4px; /* Save space */
  }

  /* 3. CHART & GRID ALIGNMENT (Stack them) */
  .chart-alignment-wrapper {
    flex-direction: column;
    padding: 15px;
    height: auto;
  }

  .chart-spacer {
    width: 100%;
    height: auto;
    padding-bottom: 5px;
    margin-bottom: 10px;
    border-bottom: 1px solid var(--border);
  }

  .chart-container {
    height: 200px; /* Give chart fixed height on mobile */
    width: 100%;
  }

  /* 4. HABIT GRID: Make it horizontally scrollable */
  .grid-container {
    padding: 0; /* Remove padding to maximize space */
    border-radius: 12px; /* Reset radii */
    border: 1px solid var(--border);
    overflow-x: auto; /* ENABLE HORIZONTAL SCROLL */
    -webkit-overflow-scrolling: touch; /* Smooth scroll on iOS */
    margin-top: 15px;
  }

  .habit-table {
    /* Keep the grid layout but let it be wider than screen */
    min-width: 800px; 
    padding: 10px;
  }

  /* Sticky columns for Name on Mobile */
  .sticky-col {
    position: sticky;
    left: 0;
    background: var(--card-bg);
    z-index: 10;
    box-shadow: 2px 0 5px rgba(0,0,0,0.05);
  }

  /* 5. FOOTER STATS: Stack vertically */
  .footer-stats {
    grid-template-columns: 1fr; /* Single column */
    gap: 15px;
  }

  /* 6. MASTER PAGE: Stack Charts */
  .charts-row, .dashboard-layout {
    grid-template-columns: 1fr !important; /* Force single column */
    display: flex !important;
    flex-direction: column;
    height: auto !important;
  }

  .widget-card {
    min-height: auto; /* Let content dictate height */
    padding: 15px;
  }
  
  /* Fix Radar Chart Height on Mobile */
  .widget-card canvas {
    max-height: 300px; 
  }

  /* 7. MODAL: Full width on mobile */
  .modal {
    width: 90%;
    padding: 20px;
  }
}