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

:root {
  --accent: #4361ee;
  --accent-dark: #3348c4;
  --accent-soft: #edf0ff;
  --text: #1a1a2e;
  --text-muted: #6b7086;
  --bg: #f7f8fc;
  --card-bg: #ffffff;
  --border: #e3e5f0;
  --danger: #c0392b;
  --shadow: rgba(20, 20, 35, 0.06);
  --shell-max: 640px;
}

/* Dark palette applies when the OS prefers dark AND the person hasn't
   explicitly picked "Light" in the account menu, or when they've explicitly
   picked "Dark" regardless of the OS setting. Keep both blocks in sync. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --accent: #5b76f7;
    --accent-dark: #7891ff;
    --accent-soft: rgba(91, 118, 247, 0.22);
    --text: #e8e9f3;
    --text-muted: #9a9fbd;
    --bg: #121319;
    --card-bg: #1c1e2b;
    --border: #333650;
    --danger: #ff7a7a;
    --shadow: rgba(0, 0, 0, 0.4);
  }
}

:root[data-theme="dark"] {
  --accent: #5b76f7;
  --accent-dark: #7891ff;
  --accent-soft: rgba(91, 118, 247, 0.22);
  --text: #e8e9f3;
  --text-muted: #9a9fbd;
  --bg: #121319;
  --card-bg: #1c1e2b;
  --border: #333650;
  --danger: #ff7a7a;
  --shadow: rgba(0, 0, 0, 0.4);
}

@media (min-width: 900px) {
  :root { --shell-max: 760px; }
}

@media (min-width: 1300px) {
  :root { --shell-max: 880px; }
}

/* A phone rotated to landscape is wide but short - unlike an actual desktop
   window, so it should use most of that width instead of sitting in a
   phone-width column with wasted grey margins either side. */
@media (orientation: landscape) and (max-height: 500px) {
  :root { --shell-max: min(96vw, 1100px); }
}

html, body {
  height: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-tap-highlight-color: transparent;
}

button {
  font: inherit;
  cursor: pointer;
  color: inherit;
}

/* ---------- Avatars ---------- */

.avatar {
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  border: none;
  color: #fff;
  font-weight: 700;
  background: var(--accent);
}

.avatar-sm { width: 36px; height: 36px; font-size: 0.9rem; }
.avatar-md { width: 56px; height: 56px; font-size: 1.2rem; }
.avatar-lg { width: 72px; height: 72px; font-size: 1.6rem; margin: 0 auto 8px; }

/* ---------- Login screen ---------- */

.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.login-screen[hidden] { display: none; }

.login-card {
  width: 100%;
  max-width: 420px;
  text-align: center;
}

.login-card h1 {
  font-size: 1.6rem;
  margin-bottom: 6px;
}

.login-card p {
  color: var(--text-muted);
  margin-bottom: 28px;
}

#loginErrorBox { margin-top: 12px; }

.people-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
  justify-items: center;
}

.person-pick {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  background: none;
  border: none;
  color: var(--text);
}

.person-pick span { font-weight: 600; font-size: 0.9rem; }

/* ---------- Overlays / cards / sheets ---------- */

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 20, 35, 0.55);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 50;
}

@media (min-width: 560px) {
  .overlay { align-items: center; }
}

.overlay[hidden] { display: none; }

.pin-card, .menu-card, .sheet {
  background: var(--card-bg);
  width: 100%;
  max-width: 400px;
  border-radius: 20px 20px 0 0;
  padding: 24px;
  position: relative;
}

@media (min-width: 560px) {
  .pin-card, .menu-card, .sheet { border-radius: 20px; }
}

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

.pin-back {
  position: absolute;
  top: 16px;
  left: 16px;
  background: none;
  border: none;
  font-size: 1.4rem;
  color: var(--text-muted);
}

.pin-name {
  font-weight: 600;
  margin-bottom: 16px;
}

.pin-dots {
  display: flex;
  justify-content: center;
  gap: 12px;
  margin-bottom: 8px;
  min-height: 16px;
}

.pin-dots span {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid var(--accent);
  display: inline-block;
}

.pin-dots span.filled { background: var(--accent); }

.pin-error {
  color: var(--danger);
  font-size: 0.85rem;
  min-height: 20px;
  margin-bottom: 8px;
}

.keypad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  max-width: 280px;
  margin: 0 auto;
}

.keypad button {
  aspect-ratio: 1;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--card-bg);
  font-size: 1.3rem;
  font-weight: 600;
}

.keypad button:active { background: var(--bg); }

.keypad button.keypad-empty { visibility: hidden; }

/* ---------- Person menu ---------- */

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

.menu-name {
  font-weight: 700;
  font-size: 1.1rem;
  margin-bottom: 16px;
}

.menu-card button { display: block; width: 100%; margin-top: 10px; }

.theme-toggle {
  display: flex;
  gap: 4px;
  background: var(--bg);
  border-radius: 10px;
  padding: 4px;
  margin-bottom: 14px;
}

.theme-toggle button {
  flex: 1;
  border: none;
  background: none;
  padding: 8px 0;
  border-radius: 8px;
  font-weight: 600;
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-top: 0;
}

.theme-toggle button.active {
  background: var(--card-bg);
  color: var(--accent);
  box-shadow: 0 1px 3px var(--shadow);
}

/* ---------- Buttons ---------- */

.btn-primary, .btn-secondary, .btn-text {
  padding: 12px 20px;
  border-radius: 10px;
  border: none;
  font-weight: 600;
}

.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:active { background: var(--accent-dark); }

.btn-secondary { background: var(--bg); color: var(--text); border: 1px solid var(--border); }
.btn-secondary.btn-danger { color: var(--danger); border-color: var(--danger); }

button:disabled { opacity: 0.6; cursor: default; }

.btn-text { background: none; color: var(--text-muted); }

/* ---------- App shell ---------- */

.app {
  min-height: 100vh;
  max-width: var(--shell-max);
  margin: 0 auto;
  padding-bottom: 90px;
  position: relative;
}

@media (min-width: 700px) {
  .app {
    background: var(--card-bg);
    box-shadow: 0 0 40px var(--shadow);
  }
}

.app-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.today-btn {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  font-weight: 600;
  font-size: 0.85rem;
  white-space: nowrap;
}

.nav-arrows {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 0;
}

.icon-btn {
  background: none;
  border: none;
  font-size: 1.4rem;
  color: var(--text-muted);
  padding: 4px 6px;
  flex-shrink: 0;
}

.view-title {
  font-weight: 700;
  font-size: 1rem;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.view-content {
  padding: 16px 12px 24px;
}

/* ---------- Bottom nav + FAB ---------- */

.bottom-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  max-width: var(--shell-max);
  margin: 0 auto;
  z-index: 20;
}

.bottom-nav {
  display: flex;
  background: var(--card-bg);
  border-top: 1px solid var(--border);
  padding: 6px env(safe-area-inset-right) calc(6px + env(safe-area-inset-bottom)) env(safe-area-inset-left);
}

.nav-btn {
  flex: 1;
  background: none;
  border: none;
  padding: 8px 4px;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  border-radius: 10px;
}

.nav-btn.active { color: var(--accent); background: var(--accent-soft); }

.fab {
  position: absolute;
  right: 18px;
  bottom: 100%;
  margin-bottom: 14px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 1.8rem;
  border: none;
  box-shadow: 0 4px 14px rgba(67, 97, 238, 0.4);
}

/* ---------- Event cards (Today / Week) ---------- */

.day-heading {
  font-weight: 700;
  font-size: 1.15rem;
  margin: 18px 0 10px;
}

.day-heading:first-child { margin-top: 0; }

.event-card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  text-align: left;
  color: inherit;
}

.event-card:active { background: var(--bg); }

.event-main { flex: 1; min-width: 0; }

.event-title { font-weight: 600; }

.event-time {
  display: inline-block;
  color: var(--text-muted);
  font-size: 0.8rem;
  margin-top: 2px;
}

.event-chips {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}

.chip {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  color: #fff;
  font-size: 0.7rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}

.chip.family { background: var(--text-muted); }

.empty-note {
  color: var(--text-muted);
  text-align: center;
  padding: 30px 0;
}

/* ---------- Week view ---------- */

.week-day-card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  margin-bottom: 12px;
  overflow: hidden;
}

.week-day-card.is-today { border-color: var(--accent); }

.week-day-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  background: var(--bg);
  border: none;
  width: 100%;
  text-align: left;
  font-weight: 700;
}

.week-day-body { padding: 10px 14px; }

.week-day-body .event-card { margin-bottom: 8px; }
.week-day-body .event-card:last-child { margin-bottom: 0; }

/* ---------- Month view ---------- */

.month-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
}

.month-weekday {
  text-align: center;
  font-size: 0.75rem;
  color: var(--text-muted);
  font-weight: 600;
  padding-bottom: 4px;
}

.month-cell {
  min-height: 68px;
  min-width: 0;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--card-bg);
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 2px;
  padding: 4px;
  position: relative;
  text-align: left;
}

.month-cell.outside { opacity: 0.35; }
.month-cell.is-today { border: 2px solid var(--accent); }

.month-cell .day-num { font-weight: 600; font-size: 0.8rem; text-align: center; }

.month-events {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
  width: 100%;
}

.month-event {
  display: flex;
  align-items: center;
  gap: 3px;
  font-size: 0.6rem;
  line-height: 1.3;
  color: var(--text);
  min-width: 0;
}

.month-event-dot {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  flex-shrink: 0;
}

.month-event-title {
  min-width: 0;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.month-event-more {
  color: var(--text-muted);
  font-weight: 600;
}

/* ---------- Photo view ---------- */

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

.photo-nav {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-bottom: 14px;
  font-weight: 700;
}

.photo-view img {
  max-width: 100%;
  border-radius: 12px;
  border: 1px solid var(--border);
  cursor: zoom-in;
}

.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 12px;
}

.lightbox[hidden] { display: none; }

.lightbox img {
  max-width: 100%;
  max-height: 100%;
  border-radius: 6px;
}

/* ---------- Add entry form ---------- */

.sheet form label {
  display: block;
  font-weight: 600;
  font-size: 0.9rem;
  margin-top: 14px;
  margin-bottom: 6px;
}

.sheet form label:first-of-type { margin-top: 0; }

.sheet input[type="text"],
.sheet input[type="date"] {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
  background: var(--card-bg);
  color: var(--text);
}

.field-label {
  font-weight: 600;
  font-size: 0.9rem;
  margin-top: 14px;
  margin-bottom: 6px;
}

.people-toggle {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.people-toggle button {
  border: 2px solid var(--border);
  background: var(--card-bg);
  border-radius: 20px;
  padding: 6px 14px;
  font-weight: 600;
  font-size: 0.85rem;
}

.people-toggle button.selected {
  border-color: currentColor;
  color: #fff;
}

.add-error {
  color: var(--danger);
  font-size: 0.85rem;
  min-height: 20px;
  margin-top: 8px;
}

.sheet-actions {
  display: flex;
  gap: 10px;
  margin-top: 18px;
}

.sheet-actions button { flex: 1; }
