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

:root {
  --bg: #FAF7F2;
  --sidebar-bg: #2C1A0E;
  --topbar-bg: #3D2410;
  --text: #1A0F00;
  --accent: #C8860A;
  --text-light: #FFF;
  --border: #E0D5C7;
  --surface: #FFF;
  --text-dim: #8B7355;
  --today-bg: #FFF8EE;
  --today-border: #C8860A;
  --color-success: #00b894;
  --color-error: #e17055;
  --color-syncing: #fdcb6e;
  --color-google: #4285F4;
  --color-microsoft: #0078D4;
}

body {
  font-family: system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  display: grid;
  grid-template-columns: 11rem 1fr;
  grid-template-rows: 2.75rem 1fr;
  overflow: hidden;
}

/* ---------- Top bar ---------- */
.topbar {
  grid-column: 1 / -1;
  background: var(--topbar-bg);
  color: var(--text-light);
  display: flex;
  align-items: center;
  padding: 0 0.75rem;
  gap: 0.5rem;
  font-size: 0.8rem;
}

.topbar h1 {
  font-size: 0.9rem;
  font-weight: 700;
  margin-right: 0.75rem;
}

.hamburger {
  display: none;
  background: none;
  border: none;
  color: var(--text-light);
  font-size: 1.1rem;
  cursor: pointer;
  padding: 0.15rem 0.35rem;
}

.nav-btn {
  background: none;
  border: none;
  color: var(--text-light);
  cursor: pointer;
  font-size: 0.9rem;
  padding: 0.15rem 0.35rem;
  opacity: 0.7;
}

.nav-btn:hover {
  opacity: 1;
}

.month-lbl {
  font-weight: 600;
  min-width: 10rem;
  text-align: center;
  font-size: 0.85rem;
}

.spacer {
  flex: 1;
}

/* ---------- User pill ---------- */
.user-pill {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  background: color-mix(in srgb, var(--text-light) 5%, transparent);
  border: 1px solid color-mix(in srgb, var(--text-light) 20%, transparent);
  border-radius: 1rem;
  padding: 0.15rem 0.6rem 0.15rem 0.15rem;
}

.user-avatar {
  width: 1.5rem;
  height: 1.5rem;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.65rem;
  font-weight: 700;
  color: var(--text-light);
  text-transform: uppercase;
  flex-shrink: 0;
}

.user-name {
  font-size: 0.75rem;
  color: color-mix(in srgb, var(--text-light) 75%, transparent);
  max-width: 10rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.logout-btn {
  background: none;
  border: 1px solid color-mix(in srgb, var(--text-light) 20%, transparent);
  color: color-mix(in srgb, var(--text-light) 75%, transparent);
  font-size: 0.75rem;
  padding: 0.2rem 0.6rem;
  border-radius: 0.25rem;
  cursor: pointer;
}

.logout-btn:hover {
  border-color: color-mix(in srgb, var(--text-light) 50%, transparent);
  color: var(--text-light);
}

/* ---------- Sidebar ---------- */
.sidebar {
  background: var(--sidebar-bg);
  color: var(--text-light);
  padding: 0.75rem 0.6rem;
  font-size: 0.75rem;
  overflow-y: auto;
}

.sidebar .heading {
  font-size: 0.6rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: color-mix(in srgb, var(--text-light) 35%, transparent);
  margin-bottom: 0.5rem;
  font-weight: 600;
}

.cal-row {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.3rem 0.25rem;
  border-radius: 0.25rem;
  cursor: pointer;
}

.cal-row:hover {
  background: color-mix(in srgb, var(--text-light) 6%, transparent);
}

.cal-row .dot {
  width: 0.5rem;
  height: 0.5rem;
  border-radius: 50%;
  flex-shrink: 0;
}

.cal-row .name {
  flex: 1;
  font-size: 0.75rem;
}

.cal-row input[type="checkbox"] {
  accent-color: var(--accent);
}

/* Source badge + sync dot in sidebar */
.cal-meta {
  display: flex;
  align-items: center;
  gap: 0.2rem;
}

.sync-dot {
  width: 0.375rem;
  height: 0.375rem;
  border-radius: 50%;
  flex-shrink: 0;
}

.sync-dot.idle { background: var(--color-success); }
.sync-dot.error { background: var(--color-error); }
.sync-dot.syncing {
  background: var(--color-syncing);
  animation: pulse 1.5s infinite;
}
.sync-dot.hidden-dot { display: none; }

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

.sync-now-btn {
  background: none;
  border: none;
  color: color-mix(in srgb, var(--text-light) 40%, transparent);
  cursor: pointer;
  font-size: 0.6rem;
  padding: 0;
  line-height: 1;
  margin-left: 0.1rem;
}

.sync-now-btn:hover {
  color: var(--accent);
}

/* ---------- + New Calendar area ---------- */
.add-wrapper {
  position: relative;
  margin-top: 0.75rem;
}

.new-cal {
  width: 100%;
  background: none;
  border: 1px dashed color-mix(in srgb, var(--text-light) 20%, transparent);
  color: color-mix(in srgb, var(--text-light) 50%, transparent);
  padding: 0.3rem;
  border-radius: 0.25rem;
  cursor: pointer;
  font-size: 0.7rem;
  text-align: left;
}

.new-cal:hover {
  color: var(--accent);
  border-color: var(--accent);
}

/* ---------- Source dropdown ---------- */
.source-dropdown {
  position: absolute;
  top: calc(100% + 0.25rem);
  left: 0;
  right: 0;
  background: var(--sidebar-bg);
  border: 1px solid color-mix(in srgb, var(--text-light) 15%, transparent);
  border-radius: 0.375rem;
  overflow: hidden;
  box-shadow: 0 0.25rem 1rem color-mix(in srgb, #000 40%, transparent);
  z-index: 20;
}

.dropdown-item {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.4rem 0.6rem;
  cursor: pointer;
  font-size: 0.7rem;
  transition: background 0.12s;
}

.dropdown-item:hover,
.dropdown-item:focus {
  background: color-mix(in srgb, var(--text-light) 8%, transparent);
  outline: none;
}

.di-icon {
  width: 1.1rem;
  text-align: center;
  font-size: 0.75rem;
  flex-shrink: 0;
}

.di-icon-google {
  color: var(--color-google);
  font-weight: 700;
}

.di-icon-ms {
  color: var(--color-microsoft);
}

.di-label {
  flex: 1;
}

.di-hint {
  font-size: 0.6rem;
  color: color-mix(in srgb, var(--text-light) 35%, transparent);
}

/* ---------- Inline form ---------- */
.inline-form {
  margin-top: 0.5rem;
  background: color-mix(in srgb, var(--text-light) 5%, transparent);
  border: 1px solid color-mix(in srgb, var(--text-light) 10%, transparent);
  border-radius: 0.375rem;
  padding: 0.6rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.form-hdr {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.1rem;
}

.form-hdr h4 {
  font-size: 0.65rem;
  color: var(--accent);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.form-hdr button {
  background: none;
  border: none;
  color: color-mix(in srgb, var(--text-light) 40%, transparent);
  cursor: pointer;
  font-size: 0.8rem;
  line-height: 1;
  padding: 0;
}

.form-hdr button:hover {
  color: var(--text-light);
}

.fld {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}

.fld label {
  font-size: 0.6rem;
  color: color-mix(in srgb, var(--text-light) 45%, transparent);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.fld input[type="text"],
.fld input[type="url"] {
  background: color-mix(in srgb, #000 30%, transparent);
  border: 1px solid color-mix(in srgb, var(--text-light) 12%, transparent);
  color: var(--text-light);
  padding: 0.25rem 0.4rem;
  border-radius: 0.2rem;
  font-size: 0.7rem;
  font-family: inherit;
  width: 100%;
}

.fld input:focus {
  outline: none;
  border-color: var(--accent);
}

.fld .url-hint {
  font-size: 0.55rem;
  color: color-mix(in srgb, var(--text-light) 30%, transparent);
  font-style: italic;
}

/* Color swatches in inline form */
.swatches {
  display: flex;
  gap: 0.3rem;
  margin-top: 0.1rem;
}

.sw {
  width: 1.1rem;
  height: 1.1rem;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
}

.sw.sel {
  border-color: var(--text-light);
}

/* Form action buttons */
.btn-row {
  display: flex;
  justify-content: flex-end;
  gap: 0.25rem;
  margin-top: 0.15rem;
}

.btn-cancel {
  background: color-mix(in srgb, var(--text-light) 8%, transparent);
  border: none;
  color: color-mix(in srgb, var(--text-light) 55%, transparent);
  padding: 0.25rem 0.5rem;
  border-radius: 0.2rem;
  font-size: 0.65rem;
  cursor: pointer;
  font-family: inherit;
}

.btn-save {
  background: var(--accent);
  border: none;
  color: var(--text-light);
  padding: 0.25rem 0.5rem;
  border-radius: 0.2rem;
  font-size: 0.65rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
}

.oauth-btn {
  width: 100%;
  background: var(--accent);
  border: none;
  color: var(--text-light);
  padding: 0.3rem 0.5rem;
  border-radius: 0.2rem;
  font-size: 0.7rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
}

.form-error {
  font-size: 0.65rem;
  color: var(--color-error);
}

/* ---------- Main / grid ---------- */
.main {
  padding: 0.5rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.dow {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
}

.dow span {
  text-align: center;
  font-size: 0.6rem;
  font-weight: 700;
  color: var(--text-dim);
  text-transform: uppercase;
  padding: 0.2rem 0;
}

.grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  flex: 1;
  border-top: 1px solid var(--border);
  border-left: 1px solid var(--border);
}

.day {
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  padding: 0.2rem;
  min-height: 4.5rem;
  background: var(--surface);
  display: flex;
  flex-direction: column;
}

.day-hdr {
  font-size: 0.7rem;
  font-weight: 500;
  margin-bottom: 0.15rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.day.out .day-hdr {
  color: var(--border);
}

.day.today .day-hdr .dn {
  background: var(--accent);
  color: white;
  border-radius: 50%;
  width: 1.3rem;
  height: 1.3rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.65rem;
}

.evt {
  font-size: 0.6rem;
  padding: 0.05rem 0;
  display: flex;
  align-items: center;
  gap: 0.2rem;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
}

.evt .bar {
  width: 0.2rem;
  height: 0.7rem;
  border-radius: 0.1rem;
  flex-shrink: 0;
}

.evt span {
  display: inline-block;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
}

.evt:hover {
  background: color-mix(in srgb, var(--accent) 6%, transparent);
  border-radius: 0.15rem;
}

.more-link {
  font-size: 0.55rem;
  color: var(--accent);
  cursor: pointer;
  margin-top: auto;
}

/* ---------- FAB ---------- */
.fab {
  position: fixed;
  bottom: 1rem;
  right: 1rem;
  width: 2.75rem;
  height: 2.75rem;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  border: none;
  font-size: 1.2rem;
  cursor: pointer;
  box-shadow: 0 0.15rem 0.5rem color-mix(in srgb, var(--accent) 30%, transparent);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: color-mix(in srgb, #000 35%, transparent);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal {
  background: var(--surface);
  border-radius: 0.5rem;
  width: 90%;
  max-width: 24rem;
  box-shadow: 0 0.5rem 2rem color-mix(in srgb, #000 15%, transparent);
}

.modal-head {
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-head h2 {
  font-size: 0.9rem;
  font-weight: 700;
}

.modal-head .close {
  background: none;
  border: none;
  font-size: 1.1rem;
  cursor: pointer;
  color: var(--text-dim);
}

.modal-body {
  padding: 1rem;
}

.fg {
  margin-bottom: 0.6rem;
}

.fg label {
  display: block;
  font-size: 0.65rem;
  font-weight: 600;
  color: var(--text-dim);
  margin-bottom: 0.15rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.fg input,
.fg select,
.fg textarea {
  width: 100%;
  padding: 0.35rem 0.4rem;
  border: 1px solid var(--border);
  border-radius: 0.25rem;
  font-size: 0.8rem;
  font-family: inherit;
  background: var(--bg);
}

.fg input:focus,
.fg select:focus,
.fg textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.fg textarea {
  resize: vertical;
  min-height: 2rem;
}

.frow {
  display: flex;
  gap: 0.5rem;
}

.frow .fg {
  flex: 1;
}

.allday {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  margin-bottom: 0.6rem;
  font-size: 0.8rem;
}

.allday input {
  accent-color: var(--accent);
}

.modal-foot {
  padding: 0.6rem 1rem;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 0.4rem;
  justify-content: flex-end;
}

.modal-foot .del {
  margin-right: auto;
  background: none;
  border: 1px solid var(--color-error);
  color: var(--color-error);
  padding: 0.3rem 0.6rem;
  border-radius: 0.25rem;
  cursor: pointer;
  font-size: 0.75rem;
}

.modal-foot .can {
  background: none;
  border: 1px solid var(--border);
  padding: 0.3rem 0.6rem;
  border-radius: 0.25rem;
  cursor: pointer;
  font-size: 0.75rem;
  color: var(--text-dim);
}

.modal-foot .sav {
  background: var(--accent);
  color: white;
  border: none;
  padding: 0.3rem 0.8rem;
  border-radius: 0.25rem;
  cursor: pointer;
  font-size: 0.75rem;
  font-weight: 600;
}

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

.sidebar-overlay {
  display: none;
}

/* ---------- View toggle ---------- */
.view-toggle {
  display: flex;
  gap: 2px;
  background: color-mix(in srgb, var(--text-light) 12%, transparent);
  border-radius: 0.25rem;
  padding: 2px;
}

.view-toggle button {
  background: none;
  border: none;
  color: color-mix(in srgb, var(--text-light) 60%, transparent);
  padding: 0.2rem 0.5rem;
  border-radius: 0.2rem;
  cursor: pointer;
  font-size: 0.7rem;
  font-family: inherit;
}

.view-toggle button.active {
  background: var(--accent);
  color: var(--text-light);
}

/* ---------- Weeks view (swimlane) ---------- */
.weeks-view {
  display: flex;
  flex-direction: column;
  flex: 1;
  overflow: hidden;
}

body.weeks-mode .main {
  overflow: hidden;
}

/* Outer wrapper — fills weeks-view */
.swim-wrap {
  display: flex;
  flex-direction: column;
  flex: 1;
  overflow: hidden;
}

/* Sticky header row: gutter + 7 day columns (1 week per band) */
.swim-header {
  display: grid;
  grid-template-columns: 3rem repeat(7, 1fr);
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 5;
}

.swim-header-gutter {
  padding: 0.3rem;
  position: sticky;
  left: 0;
  z-index: 15;
  background: var(--surface);
}

.swim-col-hdr {
  text-align: center;
  padding: 0.4rem 0.2rem;
  border-left: 1px solid var(--border);
}

.swim-day-name {
  font-size: 0.55rem;
  font-weight: 600;
  text-transform: uppercase;
  color: var(--text-dim);
}

.swim-day-num {
  font-size: 0.9rem;
  font-weight: 700;
  margin-top: 0.1rem;
}

.swim-today-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--accent);
  color: #FFF;
  border-radius: 50%;
  width: 1.5rem;
  height: 1.5rem;
  font-size: 0.7rem;
}

/* All-day row */
.swim-allday {
  display: grid;
  grid-template-columns: 3rem repeat(7, 1fr);
  border-bottom: 1px solid var(--border);
  min-height: 1.5rem;
  background: var(--surface);
  flex-shrink: 0;
}

.swim-allday-gutter {
  position: sticky;
  left: 0;
  z-index: 10;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.45rem;
  color: var(--text-dim);
  text-align: center;
  padding: 0.15rem;
}

.swim-allday-cell {
  border-left: 1px solid var(--border);
  padding: 0.15rem 0.1rem;
}

.swim-allday-evt {
  font-size: 0.55rem;
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  border-left: 3px solid var(--accent);
  padding: 0.1rem 0.25rem;
  border-radius: 0.15rem;
  margin-bottom: 0.1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}

/* Scrollable time body */
.swim-body {
  flex: 1;
  overflow-y: auto;
  overflow-x: auto;
}

/* Inner grid: gutter + 7 day columns (1 week per band) */
.swim-grid {
  display: grid;
  grid-template-columns: 3rem repeat(7, 1fr);
  position: relative;
}

/* Time gutter */
.swim-gutter {
  position: sticky;
  left: 0;
  z-index: 10;
  background: var(--bg);
}

.swim-hour-label {
  display: flex;
  align-items: flex-start;
  justify-content: flex-end;
  padding-right: 0.3rem;
  padding-top: 0;
  font-size: 0.55rem;
  color: var(--text-dim);
  transform: translateY(-0.4rem);
}

/* Day columns */
.swim-col {
  position: relative;
  border-left: 1px solid var(--border);
}

.swim-col-today {
  background: var(--today-bg);
}

/* Hour gridlines */
.swim-hour-line {
  border-bottom: 1px solid var(--border);
}

.swim-hour-line.work-hours {
  background: color-mix(in srgb, var(--text) 4%, var(--surface));
}

.swim-col-today .swim-hour-line.work-hours {
  background: color-mix(in srgb, var(--text) 4%, var(--today-bg));
}

/* Week band wrapper — one per week, stacked vertically */
.week-band {
  flex-shrink: 0;
}

/* Label bar between the two week bands */
.week-band-label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.15rem 0;
  border-top: 2px solid var(--accent);
}

.week-band-label-text {
  font-size: 0.55rem;
  font-weight: 700;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  white-space: nowrap;
  padding: 0 0.35rem;
  background: var(--bg);
}

/* Positioned event blocks */
.swim-evt {
  position: absolute;
  border-radius: 0.2rem;
  padding: 0.15rem 0.25rem;
  font-size: 0.55rem;
  overflow: visible;
  cursor: pointer;
  border-left: 3px solid;
  z-index: 2;
}

.swim-evt:hover {
  filter: brightness(0.93);
}

/* Editable event drag affordances */
.swim-evt.editable {
  cursor: grab;
}

.swim-evt.editable.dragging {
  cursor: grabbing;
  opacity: 0.75;
  z-index: 10;
  outline: 2px dashed var(--accent, #C8860A);
  outline-offset: 1px;
}

.grip-top {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 6px;
  cursor: ns-resize;
  opacity: 0;
  background: linear-gradient(to bottom, color-mix(in srgb, var(--text, #1A0F00) 10%, transparent), transparent);
}

.swim-evt.editable:hover .grip-top {
  opacity: 1;
}

.grip-bottom {
  position: absolute;
  bottom: -1px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 2px;
  padding: 2px 4px;
  cursor: ns-resize;
  border-radius: 0 0 0.15rem 0.15rem;
}

.grip-bottom span {
  width: 3px;
  height: 3px;
  background: color-mix(in srgb, var(--text, #1A0F00) 25%, transparent);
  border-radius: 50%;
  display: block;
}

.swim-evt.editable:hover .grip-bottom span {
  background: color-mix(in srgb, var(--text, #1A0F00) 50%, transparent);
}

.swim-evt-time {
  font-size: 0.5rem;
  color: var(--text-dim);
  line-height: 1.2;
}

.swim-evt-title {
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.3;
}

/* Current-time indicator */
.swim-now {
  position: absolute;
  left: 0;
  right: 0;
  height: 2px;
  background: var(--color-error);
  z-index: 5;
  pointer-events: none;
}

.swim-now::before {
  content: '';
  position: absolute;
  left: -3px;
  top: -3px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--color-error);
}

/* ---------- Responsive (mobile < 48rem) ---------- */
@media (max-width: 48rem) {
  body {
    grid-template-columns: 1fr;
  }

  .hamburger {
    display: block;
  }

  .sidebar {
    display: none;
    position: fixed;
    top: 2.75rem;
    left: 0;
    bottom: 0;
    width: 14rem;
    z-index: 90;
  }

  body.sidebar-open .sidebar {
    display: block;
  }

  body.sidebar-open {
    overflow: hidden;
  }

  body.sidebar-open .sidebar-overlay {
    display: block;
    position: fixed;
    inset: 0;
    top: 2.75rem;
    z-index: 80;
    background: color-mix(in srgb, #000 30%, transparent);
  }

  .day {
    min-height: 3.5rem;
  }

  .modal {
    max-width: 95%;
  }

  .swim-grid {
    grid-template-columns: 3rem repeat(7, minmax(2.5rem, 1fr));
  }

  .swim-body {
    overflow-x: auto;
  }
}
