:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --border: #e3e6ee;
  --text: #1c2130;
  --muted: #6b7280;
  --accent: #4f6df5;
  --accent-contrast: #ffffff;
  --today-bg: #eef2ff;
  --danger: #d43f6f;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(20, 24, 40, 0.06), 0 4px 12px rgba(20, 24, 40, 0.04);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161f;
    --surface: #1c1f2c;
    --border: #2b2f40;
    --text: #e7e9f2;
    --muted: #9aa0b4;
    --today-bg: #232a4a;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 12px rgba(0, 0, 0, 0.25);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "Segoe UI", system-ui, sans-serif;
}

.app-shell {
  max-width: 1100px;
  margin: 0 auto;
  padding: 1.25rem 1.5rem 3rem;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.25rem;
}

.topbar h1 {
  font-size: 1.4rem;
  margin: 0;
}

#auth-area a, #auth-area button {
  font-size: 0.85rem;
  color: var(--accent);
  background: none;
  border: none;
  cursor: pointer;
  text-decoration: none;
}

#signed-out-message {
  color: var(--muted);
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 0.75rem;
}

.nav-group, .view-group {
  display: flex;
  gap: 0.35rem;
}

.toolbar button {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 8px;
  padding: 0.4rem 0.7rem;
  font-size: 0.85rem;
  cursor: pointer;
}

.toolbar button.icon-btn {
  padding: 0.4rem 0.6rem;
  line-height: 1;
}

.toolbar button:hover {
  border-color: var(--accent);
}

.view-group button.active {
  background: var(--accent);
  color: var(--accent-contrast);
  border-color: var(--accent);
}

#current-range {
  font-size: 1.05rem;
  font-weight: 600;
  margin: 0;
  flex: 1;
  min-width: 160px;
}

button.primary {
  background: var(--accent);
  color: var(--accent-contrast);
  border-color: var(--accent);
  font-weight: 600;
}

button.danger {
  color: var(--danger);
  border-color: var(--danger);
  background: transparent;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0.9rem;
  font-size: 0.78rem;
  color: var(--muted);
  margin-bottom: 1rem;
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}

.dot {
  width: 0.6rem;
  height: 0.6rem;
  border-radius: 50%;
  display: inline-block;
}

.calendar-view {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

/* Month view */
.month-grid {
  display: grid;
  /* minmax(0, 1fr), not bare 1fr: grid tracks default to a min size equal
     to their content's natural width, which pushed this off-screen on
     narrow viewports instead of shrinking to fit. */
  grid-template-columns: repeat(7, minmax(0, 1fr));
}

.month-dow {
  padding: 0.5rem 0.6rem;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
}

.month-cell {
  min-height: 96px;
  min-width: 0;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  padding: 0.35rem;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.month-cell.dim { opacity: 0.4; }
.month-cell.today { background: var(--today-bg); }

.month-daynum {
  font-size: 0.78rem;
  font-weight: 600;
  margin-bottom: 2px;
}

.month-more {
  font-size: 0.68rem;
  color: var(--muted);
  padding-left: 2px;
}

.event-chip {
  color: #fff;
  font-size: 0.7rem;
  padding: 1px 6px;
  border-radius: 5px;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Day / Week grid */
.time-grid-wrap {
  display: flex;
  flex-direction: column;
}

/* Holds the all-day row, day headers, and hour grid together so they stay
   column-aligned and scroll horizontally as one unit once day columns hit
   their minmax() floor (see renderTimeGrid in app.js) — this is what keeps
   Week view usable on a phone instead of squeezing 7 columns illegibly. */
.time-grid-hscroll {
  overflow-x: auto;
}

.allday-row, .day-header-row {
  display: grid;
  border-bottom: 1px solid var(--border);
}

.allday-row { padding: 0.25rem 0; }

.allday-cell {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 0 0.3rem;
  min-height: 1.2rem;
}

.day-header {
  padding: 0.5rem 0.4rem;
  font-size: 0.78rem;
  font-weight: 600;
  text-align: center;
  border-left: 1px solid var(--border);
}

.day-header.today { color: var(--accent); }

.time-scroller {
  max-height: 560px;
  overflow-y: auto;
}

.time-grid {
  position: relative;
  display: grid;
}

.hour-label {
  position: absolute;
  left: 0;
  width: 56px;
  transform: translateY(-0.6em);
  font-size: 0.68rem;
  color: var(--muted);
  text-align: right;
  padding-right: 6px;
}

.day-column {
  position: relative;
  border-left: 1px solid var(--border);
}

.hour-line {
  position: absolute;
  left: 0;
  right: 0;
  border-top: 1px solid var(--border);
}

.event-block {
  position: absolute;
  border-radius: 6px;
  color: #fff;
  font-size: 0.72rem;
  padding: 2px 6px;
  overflow: hidden;
  cursor: pointer;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
}

/* Modal */
.modal-backdrop[hidden] {
  display: none;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 18, 30, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 10;
}

.modal {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem 1.4rem;
  width: 100%;
  max-width: 420px;
}

.modal h3 { margin: 0 0 0.9rem; }

.modal-error {
  color: var(--danger);
  font-size: 0.8rem;
  margin: -0.4rem 0 0.7rem;
}

button.danger.armed {
  background: var(--danger);
  color: #fff;
}

.modal form {
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
}

.modal label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.8rem;
  color: var(--muted);
}

.modal input, .modal textarea {
  font: inherit;
  padding: 0.45rem 0.55rem;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--bg);
  color: var(--text);
}

.modal .row {
  display: flex;
  gap: 0.7rem;
}
.modal .row label { flex: 1; }

.checkbox-row {
  flex-direction: row !important;
  align-items: center;
  gap: 0.4rem !important;
}

.modal-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.4rem;
}

.spacer { flex: 1; }

@media (max-width: 640px) {
  .app-shell { padding: 0.75rem; }
  .toolbar { gap: 0.5rem; }
  #current-range { order: 3; flex-basis: 100%; font-size: 0.95rem; }
  .month-cell { min-height: 64px; padding: 0.25rem; }
  .month-daynum { font-size: 0.72rem; }
  .event-chip { font-size: 0.66rem; }

  /* Comfortable tap targets over compact ones on touch devices. */
  .toolbar button { padding: 0.5rem 0.65rem; min-height: 2.25rem; }
  .view-group, .nav-group { gap: 0.3rem; }

  .modal { padding: 1rem 1.1rem; }
  .modal .row { flex-direction: column; gap: 0.7rem; }
  .modal input, .modal textarea { font-size: 16px; } /* prevents iOS auto-zoom on focus */

  .time-scroller { max-height: 60vh; }
}
