:root {
  --bg: #0f172a;
  --panel: #111827;
  --muted: #94a3b8;
  --text: #e5e7eb;
  --primary: #3b82f6;
  --primary-600: #2563eb;
  --border: #1f2937;
  --success: #10b981;
  --danger: #ef4444;
  --warning: #f59e0b;
  --shadow: 0 4px 16px rgba(0,0,0,0.3);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Inter, Arial, sans-serif;
  background: linear-gradient(180deg, #0b1023, #0f172a 45%, #0b0f1d);
  color: var(--text);
  line-height: 1.45;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: grid;
  gap: 12px;
  padding: 20px clamp(16px, 3vw, 28px);
  background: rgba(17, 24, 39, 0.7);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.app-header h1 {
  margin: 0;
  font-size: clamp(20px, 2.2vw, 28px);
  font-weight: 700;
  letter-spacing: 0.3px;
}
.tabs {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.tab-btn {
  appearance: none;
  border: 1px solid var(--border);
  background: #0b1226;
  color: var(--text);
  padding: 10px 14px;
  border-radius: 10px;
  cursor: pointer;
  font-weight: 600;
  transition: transform 0.08s ease, background 0.2s ease, border 0.2s ease;
}
.tab-btn:hover { transform: translateY(-1px); background: #0d1630; }
.tab-btn.active {
  border-color: rgba(59, 130, 246, 0.35);
  background: radial-gradient(1200px 120px at top, rgba(31, 106, 250, 0.15), transparent 30%);
  box-shadow: 0 0 0 1px rgba(59,130,246,0.25) inset;
}

main {
  padding: 20px clamp(16px, 3vw, 28px) 80px;
  display: grid;
  gap: 20px;
}

.view {
  display: block;
  background: radial-gradient(1200px 120px at top, rgba(31, 106, 250, 0.12), transparent 30%),
              linear-gradient(180deg, rgba(59,130,246,0.08), transparent);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: clamp(14px, 2.2vw, 24px);
  box-shadow: var(--shadow);
}
[hidden] { display: none !important; }

h2 {
  margin: 0 0 12px 0;
  font-size: clamp(18px, 2vw, 22px);
}

/* Form */
form {
  display: grid;
  gap: 14px;
  max-width: 720px;
}
.form-row {
  display: grid;
  gap: 6px;
}
label {
  color: var(--muted);
  font-size: 14px;
}
input[type="date"], input[type="text"], textarea, select {
  width: 100%;
  background: #0b1226;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 15px;
  outline: none;
  transition: border 0.15s ease, box-shadow 0.15s ease, background 0.2s ease;
}
input[type="date"]:focus, input[type="text"]:focus, textarea:focus, select:focus {
  border-color: rgba(59, 130, 246, 0.4);
  box-shadow: 0 0 0 3px rgba(59,130,246,0.2);
  background: #0d1630;
}
textarea { resize: vertical; }
/* Inline calendar button with date input */
#date-input { width: auto; flex: 1 1 auto; min-width: 0; }
#open-date-picker { flex: 0 0 auto; }
.type-row { display: flex; gap: 8px; align-items: center; }
.inline-add-type { display: flex; gap: 8px; align-items: center; margin-top: 8px; }

/* Buttons */
button, .secondary, .ghost, .primary {
  font: inherit;
  color: var(--text);
  cursor: pointer;
  user-select: none;
}
.primary {
  background: linear-gradient(180deg, var(--primary), var(--primary-600));
  border: 0;
  padding: 10px 14px;
  border-radius: 10px;
  font-weight: 700;
  letter-spacing: 0.2px;
  box-shadow: 0 6px 18px rgba(37, 99, 235, 0.35);
}
.primary:hover { filter: brightness(1.05); }
.secondary {
  background: #0b1226;
  border: 1px solid var(--border);
  padding: 9px 12px;
  border-radius: 10px;
  font-weight: 600;
}
.secondary:hover { background: #0d1630; }
.ghost {
  background: transparent;
  border: 1px solid var(--border);
  padding: 8px 10px;
  border-radius: 10px;
  color: var(--muted);
}
.ghost:hover { border-color: rgba(148,163,184,0.4); color: var(--text); }
.form-actions { display: flex; gap: 10px; }

/* Calendar */
.calendar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}
.cal-controls { display: flex; gap: 8px; }
.month-label { margin: 0; }

.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
}
.calendar-dow {
  color: var(--muted);
  font-weight: 700;
  margin-top: 6px;
}
.dow { text-align: center; padding: 6px 0; }

.calendar-days {
  margin-top: 6px;
}
.day {
  min-height: 92px;
  background: #0b1226;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px;
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 6px;
  position: relative;
  transition: border 0.15s ease, background 0.2s ease, transform 0.08s ease;
}
.day:hover { background: #0d1630; transform: translateY(-1px); }

/* Week highlights: current week (yellow), past good (green), past low (red) */
.day.week-current {
  background:
    linear-gradient(180deg, rgba(250, 204, 21, 0.14), transparent),
    #0b1226;
  border-color: rgba(250, 204, 21, 0.35);
}
.day.week-good {
  background:
    linear-gradient(180deg, rgba(16, 185, 129, 0.14), transparent),
    #0b1226;
  border-color: rgba(16, 185, 129, 0.35);
}
.day.week-bad {
  background:
    linear-gradient(180deg, rgba(239, 68, 68, 0.14), transparent),
    #0b1226;
  border-color: rgba(239, 68, 68, 0.35);
}
.day.week-current:hover,
.day.week-good:hover,
.day.week-bad:hover {
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.06), transparent),
    #0d1630;
}
.day.outside { opacity: 0.4; }
.day .date {
  font-weight: 700;
  color: var(--muted);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.day .entries {
  display: flex;
  flex-direction: column;
  gap: 4px;
  overflow: hidden;
}
.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 100%;
  font-size: 12px;
  color: #dbeafe;
  background: rgba(59,130,246,0.18);
  border: 1px solid rgba(59,130,246,0.25);
  border-radius: 999px;
  padding: 4px 8px;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
.badge.more { background: rgba(148,163,184,0.15); border-color: rgba(148,163,184,0.25); color: var(--muted); }

/* Table */
.table-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}
.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}
.io {
  display: flex;
  gap: 8px;
  align-items: center;
}
.import span.secondary { display: inline-flex; align-items: center; }

/* Horizontal scroll container for History table */
.table-scroll {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
/* Optional nicer scrollbar on WebKit */
.table-scroll::-webkit-scrollbar { height: 10px; }
.table-scroll::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 8px;
}

table {
  width: 100%;
  border-collapse: collapse;
  overflow: hidden;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: #0b1226;
}
thead th {
  text-align: left;
  font-size: 14px;
  color: var(--muted);
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  background: #0d1630;
  position: sticky;
  top: 0;
  z-index: 1;
}
thead th[data-sort] { cursor: pointer; }
tbody td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
tbody tr:hover { background: #0d1630; }
.table-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(2,6,23,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 1000; /* ensure modal overlays sticky headers/toolbars */
  overflow: auto; /* allow full-overlay scrolling as a fallback */
}
.modal-content {
  width: min(720px, 92vw);
  max-height: calc(100vh - 32px); /* fallback for older browsers */
  max-height: calc(100dvh - 32px); /* use dynamic viewport height when supported */
  background: #0b1226;
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  overflow: hidden; /* keep header fixed; body scrolls */
  position: relative;
  z-index: 1001; /* above overlay just in case */
  display: grid;
  grid-template-rows: auto 1fr; /* header + scrollable body */
  overscroll-behavior: contain; /* prevent scroll chaining */
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  background: #0d1630;
}
#modal-body { padding: 14px; display: grid; gap: 10px; overflow: auto; min-height: 0; -webkit-overflow-scrolling: touch; }

/* Toast */
.toast {
  position: fixed;
  bottom: 16px;
  right: 16px;
  background: #0b1226;
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 14px;
  box-shadow: var(--shadow);
  z-index: 100;
}

/* Footer */
.app-footer {
  padding: 18px clamp(16px, 3vw, 28px) 40px;
  color: var(--muted);
  text-align: center;
}

/* Utilities */
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.hidden { display: none !important; }

/* Stats */
.stats-topbar { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 8px; }
.stats-topbar-right { display: flex; align-items: center; gap: 8px; }
select.compact {
  font-size: 12px;
  padding: 4px 8px;
  background: #0b1226;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
}
#cal-weekly-focus {
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 8px;
  line-height: 1.2; /* closer match to native select */
}
select.compact:focus {
  border-color: rgba(59, 130, 246, 0.4);
  box-shadow: 0 0 0 3px rgba(59,130,246,0.2);
  background: #0d1630;
}
/* Visually-hidden label for accessibility */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 12px;
  margin-top: 8px;
}
.stat-card {
  background: #0b1226;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  box-shadow: var(--shadow);
}
.stat-label {
  color: var(--muted);
  font-size: 14px;
  margin-bottom: 6px;
}
.stat-value {
  font-size: clamp(28px, 4vw, 40px);
  font-weight: 800;
  color: #dbeafe; /* blue-100 */
  letter-spacing: 0.3px;
}
.stat-sub {
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}

/* Day-of-Week Chart */
.stats-section { margin-top: 16px; display: grid; gap: 10px; }
.dow-section {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 12px;
  align-items: stretch; /* let items stretch to same row height */
}
.bar-chart {
  display: flex;
  align-items: stretch; /* stretch columns to full height; bar-wrap handles bar area */
  gap: 10px;
  height: auto;         /* height will be set dynamically to match the table */
  min-height: 160px;    /* reasonable minimum */
  padding: 12px;
  background: #0b1226;
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
}
.bar-col {
  flex: 1 1 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between; /* value (top), bar-wrap (middle, flex), label (bottom) */
  gap: 6px;
  height: 100%;
}
.bar-wrap {
  flex: 1;
  display: flex;
  align-items: flex-end;
  width: clamp(14px, 2.5vw, 28px);
}
.bar {
  width: 100%;
  background: rgba(59,130,246,0.6);
  border: 1px solid rgba(59,130,246,0.35);
  border-radius: 6px 6px 0 0;
  box-shadow: inset 0 -2px 6px rgba(0,0,0,0.25);
  transition: filter 0.2s ease;
}
.bar-col:hover .bar { filter: brightness(1.1); }
.bar-label { color: var(--muted); font-size: 12px; }
.bar-value { color: #dbeafe; font-weight: 700; font-size: 12px; }
.dow-table { width: 100%; }

@media (max-width: 900px) {
  .dow-section { grid-template-columns: 1fr; }
  /* Ensure table remains readable on small screens; wrapper enables horizontal scroll */
  .table-scroll table { min-width: 720px; }
}

/* Responsive adjustments */
@media (max-width: 720px) {
  .calendar-grid { gap: 4px; }
  .day { min-height: 84px; padding: 6px; }
}

/* Mobile-first enhancements: keep desktop unchanged by scoping to small widths */
@media (max-width: 600px) {
  /* Header compaction */
  .app-header { padding: 12px 16px; gap: 8px; }
  #auth-controls #user-email { display: none; }

  /* Tabs: horizontal scroll + snap */
  .tabs {
    overflow-x: auto;
    flex-wrap: nowrap;
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
  }
  .tabs::-webkit-scrollbar { height: 8px; }
  .tabs::-webkit-scrollbar-thumb {
    background: transparent; /* subtle on mobile */
  }
  .tab-btn {
    scroll-snap-align: start;
    white-space: nowrap;
  }

  /* Calendar compaction + affordances */
  .calendar-grid { gap: 4px; }
  .day { min-height: 72px; padding: 6px; }
  /* Today/selected markers (mobile only to avoid desktop change) */
  .day.today {
    outline: 2px solid rgba(59,130,246,0.6);
    outline-offset: -2px;
  }
  .day.selected {
    box-shadow: 0 0 0 2px rgba(148,163,184,0.35) inset;
  }
  /* Mobile legend for week highlighting */
  .calendar-legend {
    margin-top: 8px;
    display: flex;
    gap: 10px;
    align-items: center;
    color: var(--muted);
    font-size: 12px;
    flex-wrap: wrap;
  }
  .calendar-legend .swatch {
    width: 10px;
    height: 10px;
    border-radius: 2px;
    display: inline-block;
    margin-right: 6px;
  }
  .legend-current .swatch { background: rgba(250, 204, 21, 0.5); border: 1px solid rgba(250, 204, 21, 0.7); }
  .legend-good .swatch { background: rgba(16, 185, 129, 0.5); border: 1px solid rgba(16, 185, 129, 0.7); }
  .legend-bad .swatch { background: rgba(239, 68, 68, 0.5); border: 1px solid rgba(239, 68, 68, 0.7); }

  /* Stats: clamp bar-chart height for small screens */
  .bar-chart {
    max-height: 360px;
    overflow: hidden;
  }
}

@media (max-width: 480px) {
  /* Tighter calendar cells and badges */
  .day { min-height: 64px; padding: 6px; }
  .badge { font-size: 11px; padding: 3px 6px; }
}

/* Mobile-only: History card view and quick ranges, notes search visibility, form errors */
.mobile-cards { display: none; } /* hidden by default; enabled on narrow screens */
@media (max-width: 560px) {
  .table-scroll { display: none; }          /* replace wide table with cards on phones */
  .mobile-cards { display: grid; gap: 8px; }
}
.entry-card {
  background: #0b1226;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px;
  box-shadow: var(--shadow);
  display: grid;
  gap: 6px;
}
.entry-card .row { justify-content: space-between; }
.entry-meta {
  color: var(--muted);
  font-size: 12px;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.entry-actions { display: flex; gap: 6px; flex-wrap: wrap; }

/* Quick date range chips (mobile toolbar enhancement) */
.quick-ranges { display: none; }
@media (max-width: 600px) {
  .quick-ranges {
    display: flex;
    gap: 6px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .chip {
    background: #0b1226;
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 6px 10px;
    font-size: 12px;
    white-space: nowrap;
  }
  .chip:focus, .chip:hover { border-color: rgba(59,130,246,0.35); }
}

/* Notes search input only visible on mobile */
.notes-search { display: none; }
@media (max-width: 600px) {
  .notes-search { display: block; width: 100%; }
}

/* Inline form errors visible on mobile only */
.form-error { display: none; color: var(--danger); font-size: 12px; }
@media (max-width: 600px) {
  .form-error.show { display: block; }
}
