:root {
  --bg: #f4f6f8;
  --card: #ffffff;
  --ink: #1f2933;
  --muted: #6b7280;
  --line: #e5e7eb;
  --teal: #0f766e;
  --teal-soft: #ccfbf1;
  --meal: #f59e0b;
  --insulin: #3b82f6;
  --trilostane: #8b5cf6;
  --weight: #10b981;
  --danger: #ef4444;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
}

* { box-sizing: border-box; }

/* Ensure the `hidden` attribute always wins, even on flex/grid elements. */
[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  padding-bottom: 84px;
  -webkit-tap-highlight-color: transparent;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: env(safe-area-inset-top) 16px 14px;
  padding-top: max(env(safe-area-inset-top), 14px);
  background: var(--teal);
  color: #fff;
}
.brand { display: flex; align-items: center; gap: 12px; }
.paw { font-size: 30px; }
.avatar { width: 46px; height: 46px; border-radius: 50%; object-fit: cover; border: 2px solid rgba(255,255,255,.7); box-shadow: 0 1px 4px rgba(0,0,0,.2); }
.app-header h1 { margin: 0; font-size: 22px; font-weight: 700; }
.subtitle { margin: 2px 0 0; font-size: 12px; opacity: .85; }

.header-actions { display: flex; gap: 8px; }
.ghost-btn {
  background: rgba(255,255,255,.15);
  border: none; color: #fff; font-size: 18px;
  width: 40px; height: 40px; border-radius: 50%; cursor: pointer;
}

/* Login page */
.login-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 20px; }
.login-card {
  background: var(--card); border-radius: 20px; box-shadow: var(--shadow);
  padding: 32px 24px; width: 100%; max-width: 360px; text-align: center;
}
.login-avatar { width: 88px; height: 88px; border-radius: 50%; object-fit: cover; border: 3px solid var(--teal-soft); }
.login-card h1 { font-size: 20px; margin: 14px 0 2px; }
.login-sub { color: var(--muted); font-size: 14px; margin: 0 0 22px; }
.login-card form { display: flex; flex-direction: column; gap: 14px; text-align: left; }
.login-card label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); font-weight: 600; }
.login-card input { border: 1px solid var(--line); border-radius: 10px; padding: 12px; font-size: 16px; }
.login-error { color: var(--danger); font-size: 13px; margin: 0; font-weight: 600; }

main { padding: 16px; max-width: 640px; margin: 0 auto; }

.tab { display: none; }
.tab.active { display: block; }

/* Quick actions */
.quick-grid {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px;
}
.quick-card {
  border: none; border-radius: 16px; background: var(--card); box-shadow: var(--shadow);
  padding: 14px 4px; font-size: 26px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  transition: transform .08s ease;
}
.quick-card:active { transform: scale(.95); }
.quick-card span { font-size: 12px; font-weight: 600; color: var(--ink); }
.quick-card.meal { border-bottom: 3px solid var(--meal); }
.quick-card.insulin { border-bottom: 3px solid var(--insulin); }
.quick-card.trilostane { border-bottom: 3px solid var(--trilostane); }
.quick-card.weight { border-bottom: 3px solid var(--weight); }

/* Summary cards */
.summary {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin: 16px 0;
}
.sum-card {
  background: var(--card); border-radius: 14px; box-shadow: var(--shadow); padding: 12px 14px;
}
.sum-card .label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.sum-card .big { font-size: 17px; font-weight: 700; margin-top: 4px; }
.sum-card .time { font-size: 13px; color: var(--ink); margin-top: 3px; font-weight: 600; }
.sum-card .ago { font-size: 12px; color: var(--muted); margin-top: 1px; }

.section-title { font-size: 15px; color: var(--muted); margin: 20px 0 10px; font-weight: 600; }

/* Timeline */
.timeline { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.event {
  background: var(--card); border-radius: 12px; box-shadow: var(--shadow);
  padding: 12px 14px; display: flex; align-items: center; gap: 12px;
}
.event .icon { font-size: 22px; width: 30px; text-align: center; }
.event .body { flex: 1; min-width: 0; }
.event .title { font-weight: 600; font-size: 15px; }
.event .meta { font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.event .del { background: none; border: none; color: var(--muted); font-size: 18px; cursor: pointer; padding: 6px; }
.event .del:hover { color: var(--danger); }
.event .bar { width: 4px; align-self: stretch; border-radius: 4px; }
.bar.meal { background: var(--meal); }
.bar.insulin { background: var(--insulin); }
.bar.trilostane { background: var(--trilostane); }
.bar.weight { background: var(--weight); }
.empty { text-align: center; color: var(--muted); padding: 32px 0; font-size: 14px; }

/* Banner */
.banner {
  margin: 12px 16px 0; background: #fef3c7; border: 1px solid #fcd34d; color: #92400e;
  border-radius: 12px; padding: 12px 14px; display: flex; align-items: center; justify-content: space-between; gap: 10px;
  max-width: 640px; margin-left: auto; margin-right: auto;
}
.banner-btn { background: #92400e; color: #fff; border: none; border-radius: 8px; padding: 8px 12px; font-weight: 600; cursor: pointer; white-space: nowrap; }

/* Filter chips */
.filter-row { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 8px; margin-bottom: 6px; }
.chip {
  border: 1px solid var(--line); background: var(--card); color: var(--muted);
  border-radius: 999px; padding: 7px 14px; font-size: 13px; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.chip.active { background: var(--teal); color: #fff; border-color: var(--teal); }

/* Settings */
.settings-form { display: flex; flex-direction: column; gap: 14px; }
.settings-form label, .modal label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); font-weight: 600; }
.settings-form input, .settings-form select, .modal input, .modal select {
  border: 1px solid var(--line); border-radius: 10px; padding: 11px 12px; font-size: 16px; color: var(--ink); background: #fff;
}

/* Modal food chips */
.modal .field { display: flex; flex-direction: column; gap: 8px; }
.modal .field-label { font-size: 13px; color: var(--muted); font-weight: 600; }
.chip-row { display: flex; flex-wrap: wrap; gap: 8px; }
.chip-row .chip {
  border: 1px solid var(--line); background: #fff; color: var(--ink);
  border-radius: 999px; padding: 9px 14px; font-size: 14px; font-weight: 600; cursor: pointer;
}
.chip-row .chip.active { background: var(--meal); color: #fff; border-color: var(--meal); }

/* Export buttons */
.export-row { display: flex; flex-direction: column; gap: 10px; }
.export-btn {
  display: block; text-align: center; text-decoration: none;
  background: var(--card); border: 1px solid var(--line); color: var(--ink);
  border-radius: 10px; padding: 13px; font-size: 15px; font-weight: 600; box-shadow: var(--shadow);
}
.export-btn:active { transform: scale(.98); }
.settings-form hr { border: none; border-top: 1px solid var(--line); margin: 4px 0; }
.primary-btn { background: var(--teal); color: #fff; border: none; border-radius: 10px; padding: 13px; font-size: 15px; font-weight: 700; cursor: pointer; }
.saved-msg { color: var(--weight); font-weight: 600; text-align: center; }
.disclaimer { font-size: 12px; color: var(--muted); margin-top: 20px; line-height: 1.5; }

/* Tabbar */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; background: var(--card);
  border-top: 1px solid var(--line); display: flex; justify-content: space-around;
  padding: 8px 0 max(env(safe-area-inset-bottom), 8px);
}
.tabbtn {
  background: none; border: none; color: var(--muted); font-size: 22px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 2px; flex: 1;
}
.tabbtn span { font-size: 11px; font-weight: 600; }
.tabbtn.active { color: var(--teal); }

/* Modal */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.45); display: flex; align-items: flex-end; justify-content: center; z-index: 50;
}
.modal {
  background: var(--card); width: 100%; max-width: 480px; border-radius: 20px 20px 0 0; padding: 22px 18px max(env(safe-area-inset-bottom), 18px);
  animation: slideup .22s ease;
}
@media (min-width: 520px) { .modal-backdrop { align-items: center; } .modal { border-radius: 20px; } }
@keyframes slideup { from { transform: translateY(100%); } to { transform: translateY(0); } }
.modal h3 { margin: 0 0 16px; font-size: 19px; }
.modal form { display: flex; flex-direction: column; gap: 14px; }
.modal-actions { display: flex; gap: 10px; margin-top: 6px; }
.modal-actions .ghost-btn { flex: 1; border-radius: 10px; width: auto; height: auto; padding: 13px; background: var(--line); color: var(--ink); font-weight: 600; }
.modal-actions .primary-btn { flex: 1; }
