:root {
  --bg: #f6f4f0;
  --fg: #2b2b28;
  --muted: #8f8b82;
  --line: #e2ddd3;
  --accent: #45624f;
  --accent-fg: #ffffff;
  --danger: #a8453a;
  --card-bg: #ffffff;
  --radius-lg: 16px;
  --radius-sm: 10px;
  --shadow: 0 2px 10px rgba(0, 0, 0, 0.06);
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, "SF Pro Text", system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
}

/* ---------- Topbar ---------- */

#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 18px;
  padding-top: max(14px, env(safe-area-inset-top));
  background: var(--card-bg);
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}

.app-title {
  font-size: 1.4rem;
  margin: 0;
}

.topbar-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.btn {
  border: none;
  border-radius: 999px;
  padding: 10px 16px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  background: #eee9e1;
  color: var(--fg);
}

.btn.primary { background: var(--accent); color: var(--accent-fg); }
.btn.secondary { background: #e4e0d6; color: var(--fg); }
.btn.ghost { background: transparent; border: 1px solid var(--line); }
.btn.danger { background: var(--danger); color: #fff; }

/* ---------- Grid ---------- */

.grid-scroll {
  flex: 1;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.grid {
  display: grid;
  min-width: max-content;
  gap: 7px;
  background: var(--bg);
  padding: 7px;
}

.cell {
  padding: 10px 8px;
  min-height: 74px;
  border-radius: 12px;
}

.corner {
  background: var(--bg);
  z-index: 5;
}

.sticky-top {
  position: sticky;
  top: 0;
  z-index: 3;
}

.sticky-left {
  position: sticky;
  left: 0;
  z-index: 2;
  background: var(--bg);
}

.corner.sticky-top.sticky-left { z-index: 6; }

.member-head {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 10px 6px;
  min-height: 92px;
  justify-content: center;
  background: var(--tint);
  border: 2px solid var(--tint-border);
}

.member-head.editable { cursor: pointer; }

.member-name {
  font-weight: 700;
  font-size: 1.05rem;
  text-align: center;
}

.avatar {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  background-size: cover;
  background-position: center;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  color: #fff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
  border: 2px solid rgba(255, 255, 255, 0.8);
}

.avatar-lg {
  width: 96px;
  height: 96px;
  font-size: 1.8rem;
  margin: 0 auto 8px;
}

.day-label {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  color: var(--muted);
  text-align: center;
  line-height: 1.3;
}

.day-label .day-weekday { font-weight: 600; text-transform: capitalize; color: var(--fg); }
.day-label .day-num { font-size: 1.4rem; font-weight: 700; color: var(--fg); }
.day-label.today .day-num, .day-label.today .day-weekday { color: var(--accent); }
.day-label.today { border: 2px solid var(--accent); }

.day-cell {
  display: flex;
  flex-direction: column;
  gap: 6px;
  position: relative;
  padding-bottom: 34px;
  background: var(--tint);
  border: 2px solid var(--tint-border);
}

.day-cell.today {
  border: 3px solid var(--accent);
}

.past.cell:not(.corner) { opacity: 0.5; filter: grayscale(0.45); }

.chip {
  display: flex;
  align-items: baseline;
  gap: 6px;
  padding: 3px 2px;
  font-size: 0.95rem;
  line-height: 1.25;
}

.chip-emoji { font-size: 1.15rem; }
.chip-text { flex: 1; word-break: break-word; }
.chip-time { font-size: 0.75rem; color: var(--muted); white-space: nowrap; }
.chip-recurring { border-left: 3px solid var(--tint-border); padding-left: 6px; }
.chip.editable { cursor: pointer; }
.chip.editable:active { opacity: 0.65; }

.add-btn {
  position: absolute;
  bottom: 6px;
  right: 8px;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 2px solid #fff;
  background: var(--accent);
  color: #fff;
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25);
}

.parent-mode .add-btn { display: flex; }

/* ---------- Kids mode readability ---------- */

body:not(.parent-mode) .chip { font-size: 1.05rem; padding: 8px 10px; }
body:not(.parent-mode) .chip-emoji { font-size: 1.35rem; }
body:not(.parent-mode) .member-name { font-size: 1.15rem; }

/* ---------- Loading ---------- */

.loading-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  transition: opacity 0.3s;
}
.loading-overlay.hidden { opacity: 0; pointer-events: none; }
.loading-box { font-size: 1.1rem; color: var(--muted); text-align: center; padding: 0 24px; }
.loading-box.error { color: var(--danger); }

/* ---------- Modals ---------- */

.modal-root { position: relative; }
.modal-root:not(.open) { display: none; }

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(20, 18, 14, 0.42);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 100;
  padding: 0;
}

@media (min-width: 700px) {
  .modal-backdrop { align-items: center; }
}

.modal-panel {
  background: var(--card-bg);
  width: 100%;
  max-width: 480px;
  border-radius: 20px 20px 0 0;
  padding: 20px 20px calc(20px + env(safe-area-inset-bottom));
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 -4px 24px rgba(0,0,0,0.2);
}

@media (min-width: 700px) {
  .modal-panel { border-radius: 20px; margin-bottom: 40px; }
}

.modal-panel.small { max-width: 360px; }

.modal-panel h3 { margin: 0 0 4px; font-size: 1.2rem; }

.text-input, .select-input {
  width: 100%;
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
  font-size: 1rem;
  background: #fbfaf7;
}

input[type="time"], input[type="date"] {
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
  font-size: 1rem;
  background: #fbfaf7;
}

.pin-input { text-align: center; letter-spacing: 6px; font-size: 1.4rem; }

.field-label {
  font-size: 0.85rem;
  color: var(--muted);
  font-weight: 600;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field-label.hidden { display: none; }

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.95rem;
}

.color-swatches {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.swatch {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
}
.swatch.selected { border-color: var(--fg); }

.file-input { font-size: 0.9rem; }

.member-chip-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.member-chip {
  border: 2px solid var(--line);
  border-radius: 999px;
  padding: 8px 14px;
  background: #fff;
  font-weight: 600;
  cursor: pointer;
}
.member-chip.selected { color: #fff; }

.weekday-chip-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.weekday-chip {
  border: 2px solid var(--line);
  border-radius: 999px;
  width: 44px;
  height: 44px;
  background: #fff;
  font-weight: 600;
  text-transform: capitalize;
  cursor: pointer;
}
.weekday-chip.selected {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-fg);
}

.emoji-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.emoji-display {
  font-size: 2.2rem;
  width: 56px;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fbfaf7;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
}

.emoji-overlay-backdrop { z-index: 200; }

.emoji-overlay-panel {
  width: min(94vw, 420px);
  max-width: 420px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.emoji-overlay-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
  flex-shrink: 0;
}
.emoji-overlay-header h3 { margin: 0; font-size: 1.1rem; }

.emoji-overlay-panel emoji-picker {
  width: 100%;
  height: 420px;
}

.suggestion-chip {
  align-self: flex-start;
  background: #eef3ee;
  border: 1px solid var(--accent);
  color: var(--accent);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 0.85rem;
  cursor: pointer;
}
.suggestion-chip.hidden { display: none; }

.error-msg { color: var(--danger); font-size: 0.9rem; min-height: 1.1em; }

.modal-buttons {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  flex-wrap: wrap;
  margin-top: 4px;
}

.scope-buttons {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.scope-buttons .btn { width: 100%; }

.overview-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
}

.overview-row {
  text-align: left;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.overview-row:active { background: #f5f3ee; }

.overview-row-main {
  display: flex;
  align-items: baseline;
  gap: 8px;
}
.overview-emoji { font-size: 1.2rem; }
.overview-text { flex: 1; font-weight: 600; }
.overview-time { font-size: 0.8rem; color: var(--muted); white-space: nowrap; }

.overview-meta {
  font-size: 0.8rem;
  color: var(--muted);
}

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

.hidden { display: none !important; }

@media (max-width: 600px) {
  .app-title { font-size: 1.1rem; }
  .btn { padding: 8px 12px; font-size: 0.85rem; }
  .cell { padding: 8px 6px; min-height: 64px; }
}
