/* --- CSS Variables & Theme Setup --- */
:root {
  /* Light Mode (Default) */
  --bg-color: #eeeeff;
  --container-bg: #ffffff;
  --primary-color: #8c9eff;  /* Soft Periwinkle */
  --primary-hover: #7289da;
  --text-color: #5a5961;     /* Dark Slate Grey */
  --text-light: #888899;
  --input-bg: #f7f7fc;
  --row-bg: #ffffff;
  --card-bg: #ffffff;
  --shadow-soft: 0 8px 30px rgba(140, 158, 255, 0.15);
  --shadow-card: 0 4px 12px rgba(0, 0, 0, 0.03);
  --shadow-focus: 0 0 0 3px rgba(140, 158, 255, 0.3);
  
  /* Status Colors */
  --success-bg: #e8f5e9;
  --success-text: #2e7d32;
  --error-bg: #ffebee;
  --error-text: #c62828;
  --info-bg: #e3f2fd;
  --info-text: #1565c0;

  /* Info Box Specific (Periwinkle Tint) */
  --info-box-bg: rgba(140, 158, 255, 0.12); 
}

[data-theme="dark"] {
  /* Dark Mode */
  --bg-color: #2b2b3b;
  --container-bg: #353545;
  --primary-color: #8c9eff;
  --primary-hover: #aebfff;
  --text-color: #e0e0e0;
  --text-light: #aaaabb;
  --input-bg: #454555;
  --row-bg: #3a3a4a;
  --card-bg: #404050;
  --shadow-soft: 0 10px 40px rgba(0, 0, 0, 0.3);
  --shadow-card: 0 4px 12px rgba(0, 0, 0, 0.2);
  --shadow-focus: 0 0 0 3px rgba(140, 158, 255, 0.5);
  
  --success-bg: #1b3320;
  --success-text: #a5d6a7;
  --error-bg: #4a2c2c;
  --error-text: #ef9a9a;
  --info-bg: #1a2836;
  --info-text: #90caf9;

  /* Dark Mode Tint */
  --info-box-bg: rgba(140, 158, 255, 0.15); 
}

/* --- Base Reset & Typography --- */
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  background-color: var(--bg-color);
  color: var(--text-color);
  margin: 0;
  padding: 20px;
  min-height: 100vh;
  box-sizing: border-box;
  transition: background-color 0.3s ease, color 0.3s ease;
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

.container {
  width: 100%;
  max-width: 900px;
  background: var(--container-bg);
  border-radius: 24px;
  padding: 40px;
  box-shadow: var(--shadow-soft);
  position: relative;
  margin-top: 20px;
  margin-bottom: 40px;
  transition: background-color 0.3s ease;
}

h1 {
  text-align: center;
  color: var(--primary-color);
  font-size: 2rem;
  font-weight: 800;
  margin-bottom: 10px;
  letter-spacing: -0.5px;
}

h2 {
  font-size: 1.4rem;
  margin-top: 30px;
  margin-bottom: 15px;
  color: var(--text-color);
}

p {
  line-height: 1.6;
  opacity: 0.9;
}

a {
  text-decoration: none;
  color: inherit;
  transition: opacity 0.2s;
}

a:hover {
  opacity: 0.8;
}

/* --- Inputs & Rows --- */
.input-row, .target-row {
  display: flex;
  gap: 12px;
  margin-bottom: 12px;
  padding: 12px;
  background: var(--input-bg);
  border-radius: 12px;
  align-items: center;
  transition: transform 0.2s, box-shadow 0.2s;
}

.input-row:focus-within {
  background: var(--container-bg);
  box-shadow: var(--shadow-card), var(--shadow-focus);
}

.input-group {
  flex: 1;
  display: flex;
  flex-direction: column;
}

label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-light);
  margin-bottom: 4px;
  margin-left: 4px;
}

input {
  width: 100%;
  padding: 12px 16px;
  border: none;
  border-radius: 8px;
  background: var(--row-bg);
  color: var(--text-color);
  font-size: 1rem;
  box-sizing: border-box;
  box-shadow: var(--shadow-card);
  transition: all 0.2s;
}

input:focus {
  outline: none;
  box-shadow: var(--shadow-focus);
}

/* Hide Number Input Spinner Arrows */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
input[type=number] {
  -moz-appearance: textfield;
}

/* --- Buttons --- */
button {
  cursor: pointer;
  border: none;
  font-family: inherit;
  font-weight: 600;
  border-radius: 12px;
  padding: 14px 24px;
  font-size: 1rem;
  transition: transform 0.1s, box-shadow 0.2s, background-color 0.2s;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

button:active {
  transform: scale(0.98);
}

.btn-primary {
  background-color: var(--primary-color);
  color: white;
  width: 100%;
  margin-top: 20px;
  font-size: 1.1rem;
  box-shadow: 0 4px 15px rgba(140, 158, 255, 0.4);
}

.btn-primary:hover {
  background-color: var(--primary-hover);
  transform: translateY(-2px);
}

.btn-add {
  background-color: var(--row-bg);
  color: var(--text-light);
  width: 100%;
  margin-top: 10px;
  border: 1px dashed var(--primary-color);
  box-shadow: none;
}

.btn-add:hover {
  background-color: var(--bg-color);
  color: var(--primary-color);
}

.btn-remove {
  background: transparent;
  color: var(--error-text);
  padding: 8px 12px;
  font-size: 0.9rem;
}

.btn-remove:hover {
  background-color: var(--error-bg);
  border-radius: 8px;
}

/* --- Sections & Cards --- */
/* Unified Info Box Style */
.info-box {
  background: var(--info-box-bg);
  color: var(--text-color);
  padding: 20px;
  border-radius: 16px;
  font-size: 0.95rem;
  margin-bottom: 30px;
  display: flex;
  align-items: flex-start;
  gap: 15px;
  box-shadow: var(--shadow-card);
  line-height: 1.6;
}

.info-box strong {
  color: var(--primary-color);
  font-weight: 700;
}

.info-box span {
  font-size: 1.4rem;
  line-height: 1;
  margin-top: 2px;
}

.section-title {
  display: flex;
  align-items: center;
  color: var(--primary-color);
  font-weight: 700;
  margin: 30px 0 15px;
  font-size: 1.1rem;
}

.section-title::before {
  content: '';
  display: inline-block;
  width: 6px;
  height: 24px;
  background-color: var(--primary-color);
  margin-right: 10px;
  border-radius: 3px;
}

/* --- Results --- */
.results-area {
  margin-top: 40px;
  padding-top: 20px;
  border-top: 2px dashed var(--input-bg);
  display: none;
  animation: fadeIn 0.5s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.summary-card {
  background: var(--info-bg);
  color: var(--info-text);
  padding: 20px;
  border-radius: 16px;
  text-align: center;
  margin-bottom: 20px;
  font-weight: 500;
}

.table-responsive {
  overflow-x: auto;
  border-radius: 16px;
  box-shadow: var(--shadow-card);
  max-height: 70vh;
  overflow-y: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  background: var(--row-bg);
  /* Removed min-width so it doesn't force scroll on mobile unless necessary */
  font-size: 0.9rem; /* Slightly smaller text for compactness */
}

th {
  background: var(--primary-color);
  color: white;
  padding: 8px 4px; /* Much smaller padding */
  text-align: center;
  font-weight: 600;
  position: sticky;
  top: 0;
  z-index: 10;
  /* Allow headers to wrap to save width */
  white-space: normal; 
  line-height: 1.2;
}

td {
  padding: 8px 4px; /* Much smaller padding */
  text-align: center;
  border-bottom: 1px solid var(--input-bg);
  color: var(--text-color);
}

tr:last-child td {
  border-bottom: none;
}

/* --- Night Mode Toggle & Home Button --- */
.theme-toggle, .btn-home {
  position: absolute;
  top: 25px;
  background: var(--input-bg);
  color: var(--text-color);
  width: 40px;
  height: 40px;
  border-radius: 50%;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  box-shadow: var(--shadow-card);
  border: none;
  transition: all 0.2s;
}

.theme-toggle:hover, .btn-home:hover {
  background: var(--row-bg);
  transform: translateY(-2px);
  box-shadow: var(--shadow-soft);
  color: var(--primary-color);
}

.theme-toggle {
  right: 25px;
}

.btn-home {
  left: 25px;
  text-decoration: none;
}

/* --- Mobile Responsiveness --- */
@media (max-width: 600px) {
  .container {
    padding: 25px 15px; /* Reduced side padding for more screen space */
    border-radius: 20px;
  }
  
  /* Default behavior for rows (like the settings row) */
  .input-row, .target-row {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }

  /* --- SPECIAL GRID LAYOUT FOR ITEMS --- */
  /* This specifically targets the Item rows to make them compact */
  .item-row {
    display: grid !important; /* Override flex */
    /* Column 1 takes 60%, Column 2 takes remaining space */
    grid-template-columns: 1.5fr 1fr; 
    gap: 10px;
    align-items: end; /* Align bottoms so inputs line up even with labels */
  }

  /* 1. Name Input (Top Left) */
  .item-row .input-group:nth-child(1) {
    grid-column: 1 / 2;
  }

  /* 2. Price Input (Top Right) */
  .item-row .input-group:nth-child(2) {
    grid-column: 2 / 3;
  }

  /* 3. Quantity Input (Bottom Left) */
  .item-row .input-group:nth-child(3) {
    grid-column: 1 / 2;
  }

  /* 4. Delete Button (Bottom Right) */
  .item-row .btn-remove {
    grid-column: 2 / 3;
    width: 100%;
    margin-top: 0;
    height: 46px; /* Match height of inputs roughly */
    background-color: var(--error-bg); /* Make it more visible on mobile */
    border-radius: 8px;
  }
  
  h1 { font-size: 1.6rem; }
  
  .theme-toggle { top: 15px; right: 15px; }
  .btn-home { top: 15px; left: 15px; }
}
/* --- Warning Message --- */
.limit-warning {
  background-color: #fff3cd;
  color: #856404;
  border: 1px solid #ffeeba;
  padding: 15px;
  border-radius: 12px;
  margin-bottom: 20px;
  text-align: center;
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

/* --- Mobile Results (Card View) --- */
.mobile-results-container {
  display: none; /* Hidden by default (Desktop) */
}

.result-card {
  background: var(--row-bg);
  border: 1px solid var(--input-bg);
  border-radius: 12px;
  padding: 15px;
  margin-bottom: 15px;
  box-shadow: var(--shadow-card);
  position: relative;
}

.result-card-header {
  display: flex;
  justify-content: space-between;
  border-bottom: 1px solid var(--input-bg);
  padding-bottom: 8px;
  margin-bottom: 8px;
  font-weight: 700;
  color: var(--primary-color);
}

.result-item-line {
  display: flex;
  justify-content: space-between;
  font-size: 0.9rem;
  margin-bottom: 4px;
  color: var(--text-color);
}

.result-item-name {
  color: var(--text-light);
}

/* --- Responsive Switch --- */
@media (max-width: 768px) {
  /* Hide the table on mobile */
  .table-responsive {
    display: none;
  }
  /* Show the cards on mobile */
  .mobile-results-container {
    display: block;
  }
}

/* --- Floating Scroll Button (Mobile Only) --- */
.btn-scroll-top {
  /* Desktop Default: A normal block button at the bottom */
  width: 100%;
  margin-top: 30px;
  background: var(--input-bg);
  color: var(--text-color);
}

@media (max-width: 768px) {
  .btn-scroll-top {
    /* Mobile Override: Floating Circle */
    position: fixed;
    bottom: 30px;
    right: 25px;
    width: 50px;       /* Fixed width for circle */
    height: 50px;      /* Fixed height for circle */
    border-radius: 50%;
    margin: 0;         /* Remove desktop margin */
    padding: 0;        /* Remove desktop padding */
    
    /* Center the arrow */
    display: flex;
    align-items: center;
    justify-content: center;
    
    /* Visuals */
    background: var(--primary-color);
    color: white;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
    z-index: 1000;     /* Ensure it sits on top of cards */
    font-size: 1.5rem; /* Larger arrow */
    opacity: 0.9;
  }
  
  .btn-scroll-top:active {
    transform: scale(0.9);
  }
}

/* --- Result Stats Dashboard --- */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
  gap: 10px;
  margin-bottom: 25px;
}

.stat-card {
  background: var(--info-bg); /* Light Blue background */
  padding: 12px 8px;
  border-radius: 12px;
  text-align: center;
  border: 1px solid rgba(140, 158, 255, 0.2);
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.stat-val {
  font-size: 1.3rem;
  font-weight: 800;
  color: var(--primary-color);
  line-height: 1.2;
}

.stat-label {
  font-size: 0.75rem;
  color: var(--text-color);
  opacity: 0.8;
  margin-top: 4px;
}

/* Make the computation count look slightly different (less important) */
.stat-card.sub-stat {
  background: var(--input-bg);
  border-color: transparent;
}
.stat-card.sub-stat .stat-val {
  color: var(--text-light);
  font-size: 1.1rem;
}

/* --- Mobile Summary List (For Split Calculator) --- */
.mobile-summary-container {
  display: none; /* Hidden on Desktop */
}

.mobile-summary-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 15px;
  border-bottom: 1px solid var(--input-bg);
  background: var(--row-bg);
  color: var(--text-color);
}

.mobile-summary-row:first-child {
  border-top-left-radius: 12px;
  border-top-right-radius: 12px;
}

.mobile-summary-row:last-child {
  border-bottom: none;
  border-bottom-left-radius: 12px;
  border-bottom-right-radius: 12px;
}

.mobile-summary-name {
  font-weight: 500;
}

.mobile-summary-price {
  font-size: 0.85rem;
  color: var(--text-light);
  margin-left: 6px;
}

@media (max-width: 768px) {
  /* On mobile, show this list */
  .mobile-summary-container {
    display: block;
    margin-bottom: 25px;
    box-shadow: var(--shadow-card);
    border-radius: 12px;
    border: 1px solid var(--input-bg);
  }
}

/* --- Gamma Calculator Specifics (Scheme Cards) --- */
.scheme-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 15px;
  margin-bottom: 30px;
}

.scheme-card {
  background: var(--row-bg);
  padding: 20px;
  border-radius: 16px;
  cursor: pointer;
  box-shadow: var(--shadow-card);
  border: 2px solid transparent; /* Invisible border to prevent layout shift on hover */
  transition: all 0.2s;
}

.scheme-card:hover {
  transform: translateY(-4px);
  box-shadow: var(--shadow-soft);
}

.scheme-card.active {
  border-color: var(--primary-color);
  background: var(--info-bg);
}

.scheme-name {
  font-weight: 700;
  color: var(--primary-color);
  margin-bottom: 8px;
}

.scheme-stat {
  font-size: 0.9rem;
  color: var(--text-color);
  margin-bottom: 4px;
}

/* Result Tags (Small pills showing "2 x Item") */
.res-tag { 
  display: inline-block;
  background: var(--input-bg); 
  padding: 4px 10px; 
  border-radius: 8px; 
  font-size: 0.9rem; 
  color: var(--text-color);
  border: 1px solid rgba(140, 158, 255, 0.2);
  margin-right: 5px;
  margin-bottom: 5px;
}