/* --- Global Resets & Theme Variables --- */
:root {
  --primary-color: #2e6930;       /* Forest green from reference */
  --primary-hover: #224d23;
  --bg-color: #f3f4f6;            /* Soft light gray mobile app background */
  --card-bg: #ffffff;
  --text-main: #111827;
  --text-muted: #6b7280;
  --border-color: #e5e7eb;
  
  --green-loss: #def7ec;          /* Weight loss = Green/Cool success */
  --green-loss-text: #03543f;
  --red-gain: #fde8e8;            /* Weight gain = Red/Warm warning */
  --red-gain-text: #9b1c1c;
  --neutral-gray: #f9fafb;
  
  --font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --app-radius: 12px;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: var(--font-family);
  background-color: var(--bg-color);
  color: var(--text-main);
  line-height: 1.5;
  font-size: 16px;
  padding-bottom: 0;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.hidden {
  display: none !important;
}

.text-center {
  text-align: center;
}

/* --- Lucide SVG Icon Styles --- */
.lucide {
  stroke-width: 2.2px;
  display: inline-block;
  vertical-align: middle;
}

.nav-icon .lucide {
  width: 22px;
  height: 22px;
}

/* --- App Layout & Header --- */
.screen-container {
  width: 100%;
}

.app-header {
  background-color: var(--card-bg);
  border-bottom: 1px solid var(--border-color);
  position: sticky;
  top: 0;
  z-index: 10;
  padding: 0.75rem 1rem;
}

.header-container {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
}

.app-title {
  font-size: 1.15rem;
  font-weight: 800;
  color: var(--primary-color);
  letter-spacing: -0.03em;
}

.user-info-area {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.active-user-name {
  font-weight: 700;
  font-size: 0.9rem;
  color: var(--text-main);
}

.main-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 1rem;
}

/* --- Card Bases (App Style: Rounded & Shadowed) --- */
.metrics-card, .entry-card, .charts-card, .history-card {
  background-color: var(--card-bg);
  border-radius: var(--app-radius);
  padding: 1.25rem;
  margin-bottom: 1rem;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05), 0 2px 4px -1px rgba(0, 0, 0, 0.03);
  border: 1px solid rgba(0,0,0,0.02);
}

.card-title {
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--text-main);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin-bottom: 0.75rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* --- Profile Selection Screen --- */
.login-box {
  max-width: 400px;
  margin: 5rem auto;
  padding: 2.5rem 1.5rem;
  background-color: var(--card-bg);
  border-radius: var(--app-radius);
  text-align: center;
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
}

.login-title {
  font-size: 1.85rem;
  font-weight: 900;
  color: var(--primary-color);
  letter-spacing: -0.03em;
  margin-bottom: 0.25rem;
}

.login-subtitle {
  font-size: 0.9rem;
  color: var(--text-muted);
  margin-bottom: 1.75rem;
}

.profile-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.75rem;
}

.profile-card {
  background-color: var(--neutral-gray);
  border: 1px solid var(--border-color);
  border-radius: var(--app-radius);
  padding: 1.25rem 0.5rem;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 120px;
}

.profile-card:active {
  background-color: #e5e7eb;
}

.profile-avatar {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  color: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 1.35rem;
  margin-bottom: 0.5rem;
}

.profile-name {
  font-weight: 700;
  font-size: 0.95rem;
  color: var(--text-main);
}

/* --- Hero Weight Logging Area & Numpad --- */
.hero-entry-section {
  max-width: 480px;
  margin: 0 auto;
}

.hero-entry-card {
  border: 1.5px solid var(--primary-color) !important;
}

/* Digital scale display - perfectly centered numbers */
.scale-display {
  position: relative; /* container for absolute background graph */
  overflow: hidden; /* crop background graph inside rounded corners */
  display: flex;
  justify-content: center;
  align-items: center; /* perfectly centers numbers vertically */
  background-color: var(--neutral-gray);
  border-radius: 12px;
  height: 80px; /* fixed height to prevent cropping */
  margin: 0.75rem 0;
  border: none;
  text-align: center;
}

/* Stylized background graph for scale-display */
.scale-bg-graph {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none; /* click-through */
  z-index: 1;
}

.scale-bg-graph .grid-line {
  stroke: var(--border-color);
  stroke-width: 0.75;
  opacity: 0.35;
}

.scale-bg-graph .graph-line {
  stroke: var(--primary-color);
  stroke-width: 3;
  stroke-linecap: round;
  fill: none;
  opacity: 0.3;
  transition: stroke 0.3s ease;
}

.scale-bg-graph .graph-area {
  fill: var(--primary-color);
  opacity: 0.05;
  transition: fill 0.3s ease;
}

#scale-value {
  position: relative;
  z-index: 2;
  font-size: 3.5rem;
  font-weight: 800;
  color: var(--text-main);
  letter-spacing: -0.03em;
  text-align: center;
  line-height: 1; /* removes excessive vertical leading */
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 140px; /* locks centering when values change */
}

.scale-unit {
  position: relative;
  z-index: 2;
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--text-muted);
  margin-left: 0.25rem;
  line-height: 1;
  align-self: flex-end; /* align unit to the bottom of flex container */
  margin-bottom: 0.75rem; /* align unit perfectly with numbers baseline */
}

/* Numpad layout - Squared keys filling the container width */
.numpad-container {
  max-width: 100%;
  margin: 0.5rem auto;
}

.numpad-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.6rem;
}

.num-btn {
  background-color: var(--neutral-gray);
  border: 1px solid var(--border-color);
  font-family: var(--font-family);
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--text-main);
  border-radius: 10px; /* squared rounded corners */
  width: 100%;
  height: 56px; /* comfortable, tap-friendly height */
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  outline: none;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  text-align: center;
}

.num-btn:active {
  background-color: #cbd5e1;
}

.btn-action-backspace {
  font-size: 1.25rem;
  background-color: #fee2e2;
  color: #c53030;
  border-color: #fca5a5;
}
.btn-action-backspace:active {
  background-color: #fca5a5;
}

/* --- Flat Buttons --- */
.btn {
  font-family: var(--font-family);
  font-size: 0.9rem;
  font-weight: 700;
  padding: 0.5rem 1rem;
  border: 1px solid transparent;
  border-radius: var(--app-radius);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
}

.btn-primary {
  background-color: var(--primary-color);
  color: white;
}
.btn-primary:active {
  background-color: var(--primary-hover);
}

.btn-secondary {
  background-color: var(--neutral-gray);
  color: var(--text-main);
  border-color: var(--border-color);
}
.btn-secondary:active {
  background-color: #e5e7eb;
}

.btn-success {
  background-color: #10b981;
  color: white;
}
.btn-success:active {
  background-color: #059669;
}

.btn-danger {
  background-color: #ef4444;
  color: white;
}
.btn-danger:active {
  background-color: #dc2626;
}

.btn-block {
  display: flex;
  width: 100%;
}

.btn-lg {
  font-size: 1rem;
  min-height: 48px;
  border-radius: 8px;
}

.btn-small {
  padding: 0.25rem 0.5rem;
  font-size: 0.75rem;
  min-height: 28px;
  border-radius: 6px;
  font-weight: 600;
}

.btn-icon-small {
  width: 28px;
  height: 28px;
  padding: 0;
  font-size: 0.75rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background-color: var(--neutral-gray);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  cursor: pointer;
}
.btn-icon-small:active {
  background-color: #e5e7eb;
}

/* --- Flat Form Elements --- */
.flat-form .form-group {
  margin-bottom: 0.75rem;
}

.flat-form .form-row {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.flat-form label {
  display: block;
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  margin-bottom: 0.25rem;
}

.flat-form input, .flat-form textarea {
  font-family: var(--font-family);
  font-size: 0.95rem;
  width: 100%;
  padding: 0.75rem 1rem;
  border: none;
  border-radius: 8px;
  background-color: var(--neutral-gray);
  color: var(--text-main);
}

.flat-form textarea {
  resize: none;
}

.flat-form input:focus, .flat-form textarea:focus {
  outline: none;
  background-color: #e5e7eb;
}

/* --- Metrics Grid (App Cards) --- */
.metrics-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.5rem;
}

.metric-box {
  background-color: var(--neutral-gray);
  border: 1px solid var(--border-color);
  padding: 0.75rem 0.5rem;
  border-radius: var(--app-radius);
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.metric-label {
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 0.25rem;
}

.metric-val {
  font-size: 1.35rem;
  font-weight: 800;
  color: var(--text-main);
  letter-spacing: -0.02em;
  text-align: center;
}

.metric-unit {
  font-size: 0.7rem;
  color: var(--text-muted);
}

.bmi-indicator {
  margin-top: 0.75rem;
  text-align: center;
  font-size: 0.85rem;
  padding: 0.5rem;
  background-color: var(--neutral-gray);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  font-weight: 600;
}

/* --- Segmented Controls (iOS/Android App Style Tabs) --- */
.tab-header {
  background-color: #e5e7eb;
  border-radius: 8px;
  padding: 0.25rem;
  display: flex;
  margin-bottom: 1rem;
}

.tab-btn {
  font-family: var(--font-family);
  font-size: 0.85rem;
  font-weight: 700;
  background: none;
  border: none;
  padding: 0.4rem;
  cursor: pointer;
  color: var(--text-muted);
  border-radius: 6px;
  flex: 1;
  text-align: center;
  border-bottom: none;
}

.tab-btn.active {
  background-color: var(--card-bg);
  color: var(--primary-color);
  box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06);
}

.chart-wrapper {
  position: relative;
  width: 100%;
  height: 250px;
}

/* --- App-Style History List --- */
.app-history-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.history-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: var(--neutral-gray);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 0.75rem;
}

.item-left {
  display: flex;
  flex-direction: column;
}

.item-date {
  font-weight: 700;
  font-size: 0.9rem;
  color: var(--text-main);
}

.item-notes {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 0.1rem;
  word-break: break-word;
  max-width: 200px;
}

.item-right {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.item-weight {
  font-weight: 800;
  font-size: 0.95rem;
  color: var(--text-main);
  text-align: center;
}

/* Change pills */
.change-pill {
  font-size: 0.75rem;
  font-weight: 700;
  padding: 0.2rem 0.4rem;
  border-radius: 6px;
  min-width: 56px;
  text-align: center;
}

.change-pill.change-neg {
  background-color: var(--green-loss);
  color: var(--green-loss-text);
}

.change-pill.change-pos {
  background-color: var(--red-gain);
  color: var(--red-gain-text);
}

.change-pill.change-zero {
  background-color: #f3f4f6;
  color: var(--text-muted);
}

.btn-delete-item {
  background: none;
  border: none;
  color: #ef4444;
  cursor: pointer;
  padding: 0.25rem;
  font-size: 0.9rem;
  font-weight: 800;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

/* --- iOS/Android Bottom Navigation Bar --- */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 60px;
  background-color: var(--card-bg);
  border-top: 1px solid var(--border-color);
  display: flex;
  justify-content: space-around;
  align-items: center;
  z-index: 50;
  padding-bottom: env(safe-area-inset-bottom, 0px);
  box-shadow: 0 -2px 10px rgba(0,0,0,0.05);
}

.nav-tab {
  background: none;
  border: none;
  font-family: var(--font-family);
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  height: 100%;
  cursor: pointer;
  outline: none;
}

.nav-tab.active {
  color: var(--primary-color);
}

.nav-icon {
  margin-bottom: 0.15rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* --- Responsive Adjustments & Viewport Navigation --- */
@media (max-width: 767px) {
  html, body {
    height: 100dvh;
    overflow: hidden;
  }

  #screen-login {
    height: 100dvh;
    overflow-y: auto;
  }

  #screen-dashboard {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    width: 100vw !important;
    max-width: 100vw !important;
    margin: 0 auto !important;
    overflow: hidden;
    background-color: var(--card-bg); /* ensure white background extends to edge */
  }

  .main-content {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    overflow: hidden;
    padding: 0;
    width: 100% !important;
    max-width: 100% !important;
    margin: 0 !important;
    background-color: var(--card-bg); /* ensure white background extends to edge */
  }

  .dashboard-grid {
    display: contents; /* splits children to scroll independently inside main-content on mobile */
  }

  .bottom-nav {
    position: relative;
    height: 60px;
    padding-bottom: env(safe-area-inset-bottom, 0px);
    flex-shrink: 0;
  }

  /* Sub-screen behavior: hide inactive tabs, lock height, enable independent scrolling */
  .sub-screen {
    display: none;
    height: 100%;
    width: 100% !important; /* ensures full bleed */
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    box-sizing: border-box;
    overscroll-behavior-y: contain; /* prevent page bounce/chaining */
  }
  .sub-screen.active {
    display: block;
  }

  #subscreen-log {
    width: 100% !important;
  }

  .hero-entry-section {
    width: 100% !important;
    max-width: 100% !important;
    margin: 0 !important;
    height: 100%;
  }

  .hero-entry-card {
    height: 100%; /* fills main-content exactly, preventing viewport scroll */
    border-radius: 0 !important;
    border: none !important;
    box-shadow: none !important;
    margin: 0 !important; /* completely removes margins */
    width: 100% !important; /* ensures full bleed */
    display: flex;
    flex-direction: column;
    justify-content: center; /* group components in the center of viewport */
    gap: 0.5rem; /* tighter unified gap */
    padding: 0.75rem 0.5rem calc(0.75rem + env(safe-area-inset-bottom, 16px)) 0.5rem !important; /* reduced side padding to 0.5rem for full width content */
    box-sizing: border-box;
    overflow: hidden;
    background-color: var(--card-bg); /* ensure white background covers entire view */
  }
  
  .hero-entry-card .card-title {
    display: none; /* hide redundant title to save screen space on mobile */
  }
  
  .scale-display {
    width: 100%;
    height: 72px; /* slightly more compact height on mobile */
    margin: 0; /* spacing managed by parent flex gap */
    padding: 0;
    box-sizing: border-box;
  }
  
  #scale-value {
    font-size: 3rem; /* slightly more compact display digits on mobile */
  }
  
  .numpad-container {
    margin: 0 auto; /* spacing managed by parent flex gap */
    width: 100%;
    max-width: 100%; /* allow it to fill full screen width */
    padding: 0 0.25rem; /* very small edge padding for buttons */
    box-sizing: border-box;
  }

  .numpad-grid {
    gap: 0.5rem; /* slightly tighter key separation */
  }

  .num-btn {
    height: 48px; /* compact but highly touch-responsive keys */
    font-size: 1.35rem;
    border-radius: 8px;
  }

  .flat-form {
    width: 100%;
    padding: 0 0.25rem; /* align form with numpad width */
    box-sizing: border-box;
  }

  .flat-form .form-group {
    margin-bottom: 0.4rem; /* tighter spacing between inputs */
  }

  .flat-form input, .flat-form textarea {
    padding: 0.5rem 0.75rem;
    font-size: 0.9rem;
  }

  .flat-form textarea {
    height: 52px; /* compact fixed height to prevent overflow */
  }

  .btn-lg {
    min-height: 42px; /* compact, comfortable action button */
    font-size: 0.95rem;
  }

  /* Secondary cards on mobile get margins so they look clean when scrolled */
  .metrics-card, .charts-card, .history-card {
    margin: 1rem 1rem calc(1rem + env(safe-area-inset-bottom, 16px)) 1rem;
  }
}

/* Desktop Grid Layout: displays all three panels side-by-side */
@media (min-width: 768px) {
  .bottom-nav {
    display: none !important; /* Hide bottom nav on desktop */
  }

  .sub-screen {
    display: block !important; /* Force all sub-screens visible side-by-side */
  }

  .main-content {
    display: grid;
    grid-template-columns: 320px 1.2fr 300px;
    gap: 1rem;
    align-items: start;
  }

  .dashboard-grid {
    display: contents; /* splits children (subscreen-progress and subscreen-history) into main-content grid */
  }

  #subscreen-log {
    grid-column: 1;
  }

  #subscreen-progress {
    grid-column: 2;
  }

  #subscreen-history {
    grid-column: 3;
  }

  .chart-wrapper {
    height: 320px;
  }
}

/* --- Flat Modals --- */
.modal-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.4);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 100;
  opacity: 0;
  pointer-events: none;
}

.modal-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}

.modal-content {
  background-color: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: var(--app-radius);
  width: 100%;
  max-width: 420px;
  padding: 1.25rem;
  margin: 1rem;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--border-color);
  padding-bottom: 0.5rem;
  margin-bottom: 0.75rem;
}

.modal-header h3 {
  font-size: 1.05rem;
  font-weight: 800;
  color: var(--text-main);
}

.btn-close {
  background: none;
  border: none;
  font-size: 1.35rem;
  cursor: pointer;
  color: var(--text-muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.color-picker-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 0.5rem;
  margin-top: 0.25rem;
}

.color-option {
  height: 38px;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid rgba(0,0,0,0.05);
}

.color-option input {
  position: absolute;
  opacity: 0;
  cursor: pointer;
}

.color-check {
  display: none;
  color: white;
  font-weight: bold;
}

.color-option input:checked ~ .color-check {
  display: block;
}
.color-option input:checked ~ .color-check::after {
  content: '✓';
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 1.25rem;
  border-top: 1px solid var(--border-color);
  padding-top: 0.75rem;
}
