/* ===== CSS Variables & Reset ===== */
:root {
  --bg: #121212;
  --surface: #1e1e1e;
  --surface2: #2c2c2c;
  --primary: #1976d2;
  --primary-light: #42a5f5;
  --accent: #ff9800;
  --text: #e0e0e0;
  --text-secondary: #9e9e9e;
  --success: #4caf50;
  --danger: #f44336;
  --warning: #ff9800;
  --radius: 12px;
  --shadow: 0 2px 8px rgba(0,0,0,.4);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%; width: 100%;
  height: 100dvh; /* iOS dynamic viewport height */
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  overflow: hidden;
  /* Prevent iOS overscroll bounce */
  overscroll-behavior: none;
  position: fixed;
  inset: 0;
}

/* ===== Utility ===== */
.hidden { display: none !important; }

/* ===== Auth Screen ===== */
#auth-screen {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  height: 100vh; height: 100dvh; padding: 24px;
  padding-top: calc(24px + env(safe-area-inset-top, 0px));
  padding-bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  padding-left: calc(24px + env(safe-area-inset-left, 0px));
  padding-right: calc(24px + env(safe-area-inset-right, 0px));
}
#auth-screen .logo {
  width: 80px; height: 80px; border-radius: 20px;
  background: var(--primary); display: flex; align-items: center; justify-content: center;
  font-size: 36px; font-weight: 700; color: #fff; margin-bottom: 16px;
}
#auth-screen h1 { font-size: 24px; margin-bottom: 4px; }
#auth-screen .subtitle { color: var(--text-secondary); margin-bottom: 32px; font-size: 14px; }

.auth-form { width: 100%; max-width: 360px; }
.auth-form input {
  width: 100%; padding: 14px 16px; margin-bottom: 12px;
  background: var(--surface); border: 1px solid var(--surface2);
  border-radius: var(--radius); color: var(--text); font-size: 16px;
  outline: none; transition: border-color .2s;
}
.auth-form input:focus { border-color: var(--primary); }

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 14px 24px; border: none; border-radius: var(--radius);
  font-size: 16px; font-weight: 600; cursor: pointer; transition: opacity .2s;
  width: 100%;
}
.btn:active { opacity: .7; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-outline { background: transparent; color: var(--primary); border: 1px solid var(--primary); margin-top: 8px; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-small { padding: 8px 16px; font-size: 14px; width: auto; }

.auth-error {
  background: rgba(244,67,54,.15); color: var(--danger);
  padding: 10px 14px; border-radius: 8px; font-size: 13px;
  margin-bottom: 12px; display: none;
}

/* ===== App Shell ===== */
#app-screen {
  display: flex; flex-direction: column;
  height: 100vh; height: 100dvh;
}

/* Top bar */
.top-bar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px;
  padding-top: calc(12px + env(safe-area-inset-top, 0px));
  padding-left: calc(16px + env(safe-area-inset-left, 0px));
  padding-right: calc(16px + env(safe-area-inset-right, 0px));
  background: var(--surface);
  border-bottom: 1px solid var(--surface2);
  flex-shrink: 0;
}
.top-bar h2 { font-size: 18px; font-weight: 600; }
.sync-indicator {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--success); transition: background .3s;
}
.sync-indicator.syncing { background: var(--warning); animation: pulse .8s infinite; }
.sync-indicator.error { background: var(--danger); }

@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .4; } }

/* Tab content area */
.tab-content {
  flex: 1; overflow-y: auto;
  padding: 16px;
  padding-left: calc(16px + env(safe-area-inset-left, 0px));
  padding-right: calc(16px + env(safe-area-inset-right, 0px));
  -webkit-overflow-scrolling: touch;
  min-height: 0; /* flex child scroll fix */
}
.tab-pane { display: none; }
.tab-pane.active { display: block; }

/* Bottom navigation */
.bottom-nav {
  display: flex; background: var(--surface);
  border-top: 1px solid var(--surface2);
  padding: 4px 0;
  padding-bottom: max(4px, env(safe-area-inset-bottom, 4px));
  padding-left: env(safe-area-inset-left, 0px);
  padding-right: env(safe-area-inset-right, 0px);
  flex-shrink: 0;
}
.nav-item {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  padding: 8px 0; font-size: 11px; color: var(--text-secondary);
  cursor: pointer; transition: color .2s; border: none; background: none;
}
.nav-item.active { color: var(--primary-light); }
.nav-item.nav-item-primary {
  color: var(--accent); position: relative;
}
.nav-item.nav-item-primary .nav-icon {
  background: var(--accent); color: #fff; width: 38px; height: 38px;
  border-radius: 50%; font-size: 20px; margin-top: -14px;
  box-shadow: 0 2px 10px rgba(255,152,0,.4);
}
.nav-item.nav-item-primary.active { color: var(--accent); }
.nav-item.nav-item-primary.active .nav-icon {
  background: var(--primary); box-shadow: 0 2px 10px rgba(25,118,210,.5);
}
.nav-item .nav-icon { font-size: 22px; margin-bottom: 2px; display: flex; align-items: center; justify-content: center; }

/* ===== Cards ===== */
.card {
  background: var(--surface); border-radius: var(--radius);
  padding: 16px; margin-bottom: 12px; box-shadow: var(--shadow);
  overflow: hidden;
}
.card-title { font-size: 14px; color: var(--text-secondary); margin-bottom: 8px; text-transform: uppercase; letter-spacing: .5px; }
.card-value { font-size: 28px; font-weight: 700; }
.card-value.small { font-size: 20px; }

/* ===== Dashboard ===== */
.stats-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 16px;
}
.suggested-dose-card {
  grid-column: 1 / -1;
  background: linear-gradient(135deg, var(--surface), var(--surface2));
  border: 1px solid var(--primary);
}
.suggested-dose-card .card-value {
  color: var(--primary-light); font-size: 32px;
}
.card-hint {
  font-size: 12px; margin-top: 4px;
}
.level-bar-container {
  width: 100%; height: 4px; background: var(--surface2); border-radius: 2px; margin-top: 8px; overflow: hidden;
}
.level-bar {
  height: 100%; width: 0%; border-radius: 2px; transition: width .4s ease, background .4s ease;
  background: var(--primary);
}
.next-injection-card {
  background: linear-gradient(135deg, var(--primary), #1565c0);
  border-radius: var(--radius); padding: 20px; text-align: center;
  margin-bottom: 16px; box-shadow: var(--shadow);
}
.next-injection-card .label { font-size: 13px; opacity: .8; margin-bottom: 4px; }
.next-injection-card .date { font-size: 22px; font-weight: 700; }
.next-injection-card .countdown { font-size: 14px; opacity: .7; margin-top: 4px; }

.progress-bar-container {
  background: var(--surface2); border-radius: 8px; height: 8px; margin-top: 12px; overflow: hidden;
}
.progress-bar {
  height: 100%; border-radius: 8px; background: var(--primary-light);
  transition: width .5s ease;
}

/* ===== History ===== */
.history-item {
  display: flex; align-items: center; padding: 14px 0;
  border-bottom: 1px solid var(--surface2);
}
.history-item:last-child { border-bottom: none; }
.history-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--primary); margin-right: 14px; flex-shrink: 0;
}
.history-info { flex: 1; }
.history-date { font-size: 15px; font-weight: 500; }
.history-detail { font-size: 13px; color: var(--text-secondary); margin-top: 2px; }
.history-actions { display: flex; gap: 8px; }
.history-actions button {
  background: none; border: none; color: var(--text-secondary); cursor: pointer;
  font-size: 18px; padding: 4px; display: flex; align-items: center;
}
.history-actions button:hover { color: var(--danger); }
.history-actions button[data-edit]:hover { color: var(--primary-light); }
.history-empty { text-align: center; color: var(--text-secondary); padding: 40px 0; }

/* ===== Log Form ===== */
.form-group { margin-bottom: 16px; }
.form-group label {
  display: block; font-size: 13px; color: var(--text-secondary);
  margin-bottom: 6px; text-transform: uppercase; letter-spacing: .5px;
}
.form-group input, .form-group select, .form-group textarea {
  width: 100%; padding: 12px 14px; background: var(--surface);
  border: 1px solid var(--surface2); border-radius: var(--radius);
  color: var(--text); font-size: 16px; outline: none;
  min-width: 0; max-width: 100%;
}
.form-group input:focus, .form-group select:focus { border-color: var(--primary); }
.form-group select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' fill='%239e9e9e'%3E%3Cpath d='M6 8L0 0h12z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 14px center;
  padding-right: 36px;
}
.form-row { display: flex; gap: 12px; }
.form-row .form-group { flex: 1; min-width: 0; }

/* ===== Settings ===== */
.setting-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 0; border-bottom: 1px solid var(--surface2);
}
.setting-item:last-child { border-bottom: none; }
.setting-label { font-size: 15px; }
.setting-desc { font-size: 12px; color: var(--text-secondary); margin-top: 2px; }

/* Toggle switch */
.toggle {
  position: relative; width: 48px; height: 26px; cursor: pointer;
}
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle .slider {
  position: absolute; inset: 0; background: var(--surface2);
  border-radius: 26px; transition: .3s;
}
.toggle .slider::before {
  content: ''; position: absolute; width: 20px; height: 20px;
  left: 3px; bottom: 3px; background: #fff; border-radius: 50%;
  transition: .3s;
}
.toggle input:checked + .slider { background: var(--primary); }
.toggle input:checked + .slider::before { transform: translateX(22px); }

/* ===== Summary ===== */
.summary-stat {
  display: flex; justify-content: space-between; padding: 12px 0;
  border-bottom: 1px solid var(--surface2);
}
.summary-stat:last-child { border-bottom: none; }
.summary-label { color: var(--text-secondary); }
.summary-value { font-weight: 600; }

/* ===== Calendar ===== */
.cal-sticky-header {
  position: sticky; top: 0; z-index: 10;
  background: var(--bg); padding-bottom: 4px;
}
.cal-weekday-row {
  display: grid; grid-template-columns: repeat(7, 1fr);
  text-align: center; font-size: 12px; color: var(--text-secondary);
  text-transform: uppercase; letter-spacing: .5px;
  padding: 8px 12px; background: var(--surface);
  border-radius: var(--radius) var(--radius) 0 0;
}
.cal-legend {
  display: flex; gap: 12px; justify-content: center;
  font-size: 11px; color: var(--text-secondary);
  padding: 6px 0 4px; background: var(--surface);
  border-radius: 0 0 var(--radius) var(--radius);
  border-top: 1px solid var(--surface2);
}
.cal-legend-item { display: flex; align-items: center; gap: 5px; }
.cal-dot {
  width: 9px; height: 9px; border-radius: 50%; display: inline-block;
}
.cal-dot-done { background: var(--primary); }
.cal-dot-upcoming { background: transparent; box-shadow: 0 0 0 2px var(--accent); }
.cal-dot-today { background: var(--surface2); box-shadow: 0 0 0 2px var(--primary-light); }

.cal-scroll {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 0; scroll-behavior: auto;
  min-height: 0; /* allow flex shrink */
}
#tab-calendar.active {
  display: flex; flex-direction: column;
  overflow: hidden;
  /* Use dvh with fallback; account for topbar + safe-area-top, bottomnav + safe-area-bottom, padding */
  height: calc(100vh - 56px - 52px - 32px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
  height: calc(100dvh - 56px - 52px - 32px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
}

.cal-month-block {
  padding: 0 2px; margin-bottom: 8px;
}
.cal-month-title {
  font-size: 15px; font-weight: 600; color: var(--text);
  padding: 14px 10px 8px; position: relative;
}
.cal-month-title .cal-year {
  font-weight: 400; color: var(--text-secondary); font-size: 13px; margin-left: 6px;
}
.cal-grid {
  display: grid; grid-template-columns: repeat(7, 1fr);
  gap: 3px; padding: 0 4px;
}
.cal-day {
  aspect-ratio: 1; display: flex; align-items: center; justify-content: center;
  border-radius: 50%; font-size: 13px; font-weight: 500;
  color: var(--text); cursor: default; position: relative;
  transition: background .15s, box-shadow .15s;
}
.cal-day.empty { visibility: hidden; }
.cal-day.cal-day-inactive {
  color: var(--surface2); opacity: .35; pointer-events: none;
}
.cal-day.today {
  background: rgba(66,165,245,.2); box-shadow: 0 0 0 2.5px var(--primary-light);
  color: var(--primary-light); font-weight: 700;
}
.cal-day.injection-done {
  background: var(--primary); color: #fff; font-weight: 700;
}
.cal-day.injection-upcoming {
  background: transparent; box-shadow: 0 0 0 2px var(--accent);
  color: var(--accent); font-weight: 600;
}
.cal-day.today.injection-done {
  background: var(--primary); box-shadow: 0 0 0 2px var(--primary-light); color: #fff;
}
.cal-day.today.injection-upcoming {
  background: var(--accent); box-shadow: 0 0 0 2px var(--accent); color: #fff;
}
.cal-day:hover:not(.empty) {
  background: var(--surface2); cursor: pointer;
}
.cal-day.injection-done:hover { background: var(--primary-light); }

.cal-day-detail {
  position: sticky; bottom: 0;
  background: var(--surface); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow);
  font-size: 14px; margin-top: 4px;
}
.cal-day-detail .cal-detail-title {
  font-weight: 600; margin-bottom: 6px; font-size: 15px;
}
.cal-day-detail .cal-detail-row {
  color: var(--text-secondary); padding: 4px 0;
  display: flex; align-items: center; gap: 6px;
}
.cal-day-detail .cal-detail-row i { font-size: 16px; flex-shrink: 0; }
.cal-edit-btn {
  background: none; border: none; color: var(--text-secondary);
  font-size: 18px; padding: 4px 6px; cursor: pointer;
  display: flex; align-items: center; border-radius: 6px;
  transition: color .2s, background .2s; flex-shrink: 0;
}
.cal-edit-btn:hover, .cal-edit-btn:active {
  color: var(--primary-light); background: rgba(66,165,245,.12);
}

/* ===== Banner ===== */
.notification-banner {
  position: fixed; top: 0; left: 0; right: 0;
  background: var(--warning); color: #000;
  padding: 10px 16px;
  padding-top: calc(10px + env(safe-area-inset-top, 0px));
  font-size: 13px; text-align: center; z-index: 1000;
  transform: translateY(-100%); transition: transform .3s;
}
.notification-banner.show { transform: translateY(0); }
.notification-banner .close-banner {
  position: absolute; right: 12px; top: 50%; transform: translateY(-50%);
  background: none; border: none; font-size: 18px; cursor: pointer; color: #000;
}

/* ===== Scrollbar ===== */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--surface2); border-radius: 4px; }

/* ===== Responsive ===== */
@media (min-width: 600px) {
  .tab-content { max-width: 540px; margin: 0 auto; }
}

/* ===== iOS standalone (Add to Home Screen) tweaks ===== */
@media screen and (display-mode: standalone) {
  html, body { position: fixed; inset: 0; }
}

/* ===== iOS landscape safety ===== */
@media (orientation: landscape) {
  .bottom-nav {
    padding-left: env(safe-area-inset-left, 0px);
    padding-right: env(safe-area-inset-right, 0px);
  }
}

/* ===== Edit mode indicator ===== */
#tab-log.editing .card {
  border-left: 3px solid var(--primary-light);
}
#tab-log.editing .card-title {
  color: var(--primary-light);
}
#btn-log-cancel {
  margin-top: 8px;
}
