* { box-sizing: border-box; }

:root {
  --bg: #ffffff;
  --surface: #f7f7f9;
  --border: #e1e1e6;
  --text: #1d1d1f;
  --muted: #6b6b70;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --danger: #dc2626;
  --today-bg: #eef4ff;
  --other-month: #fafafb;
  --event-bg: #2563eb;
  --event-text: #ffffff;
  --completed-bg: #9ca3af;
}

html, body {
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100%;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  gap: 16px;
}

.nav {
  display: flex;
  align-items: center;
  gap: 8px;
}

.month-label {
  font-size: 18px;
  font-weight: 600;
  border: none;
  background: none;
  padding: 6px 10px;
  margin-left: 6px;
  cursor: pointer;
  border-radius: 6px;
}

.month-label:hover { background: var(--surface); }

button {
  font: inherit;
  padding: 6px 12px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

button:hover { background: var(--surface); }

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

button.primary:hover {
  background: var(--accent-hover);
}

button.danger {
  background: var(--bg);
  border-color: var(--danger);
  color: var(--danger);
}

button.danger:hover { background: #fdecec; }

.weekday-row {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.weekday-row > div {
  padding: 8px;
  text-align: center;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.calendar-grid {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  grid-auto-rows: 1fr;
  min-height: 0;
}

.day-cell {
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  padding: 6px;
  min-height: 110px;
  position: relative;
  background: var(--bg);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.day-cell:nth-child(7n) { border-right: none; }
.day-cell.other-month { background: var(--other-month); color: var(--muted); }
.day-cell.today { background: var(--today-bg); }

.day-cell.drag-over {
  outline: 2px dashed var(--accent);
  outline-offset: -4px;
}

.day-number {
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 4px;
  align-self: flex-start;
}

.day-cell.today .day-number {
  background: var(--accent);
  color: #fff;
  border-radius: 50%;
  width: 22px;
  height: 22px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.event {
  background: var(--event-bg);
  color: var(--event-text);
  font-size: 12px;
  padding: 2px 6px;
  border-radius: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
  user-select: none;
}

.event:hover { filter: brightness(1.1); }

.event.recurring::before {
  content: "\21BB  ";
  opacity: 0.8;
}

.event.completed {
  background: var(--completed-bg) !important;
  text-decoration: line-through;
  opacity: 0.75;
}

.event.dragging { opacity: 0.4; }

/* ---------- Popover base ---------- */
.popover {
  position: fixed;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.15);
  padding: 6px;
  z-index: 300;
}

.popover.hidden { display: none !important; }

#context-menu { min-width: 170px; }

#context-menu button {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  background: none;
  padding: 8px 12px;
  border-radius: 4px;
}

#context-menu button:hover { background: var(--surface); }

/* ---------- Month / year picker ---------- */
#mypicker { padding: 12px; min-width: 260px; }

.mypicker-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.mypicker-header span {
  font-weight: 600;
  font-size: 16px;
}

.mypicker-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.mypicker-grid button {
  padding: 10px 6px;
  font-size: 13px;
}

.mypicker-grid button.current {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

/* ---------- Event details popover ---------- */
#details-popover {
  padding: 14px;
  min-width: 260px;
  max-width: 320px;
}

.details-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}

.details-color-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  flex-shrink: 0;
}

.details-title {
  font-weight: 600;
  font-size: 15px;
  overflow-wrap: anywhere;
}

.details-meta {
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 8px;
}

.details-desc {
  font-size: 13px;
  margin-bottom: 12px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.details-desc:empty { display: none; }

.details-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.details-actions button {
  flex: 1;
  min-width: 90px;
  font-size: 13px;
}

/* ---------- Time picker ---------- */
#time-picker { padding: 10px; width: 200px; }

.time-picker-cols {
  display: flex;
  gap: 6px;
  height: 180px;
}

.time-col {
  flex: 1;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px;
}

.time-col button {
  display: block;
  width: 100%;
  padding: 6px;
  border: none;
  background: none;
  border-radius: 4px;
  font-size: 13px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.time-col button:hover { background: var(--surface); }

.time-col button.selected {
  background: var(--accent);
  color: #fff;
}

.time-picker-actions {
  display: flex;
  gap: 6px;
  align-items: center;
  margin-top: 10px;
}

.time-picker-actions .spacer { flex: 1; }

.time-btn {
  text-align: left;
  font-variant-numeric: tabular-nums;
}

/* ---------- Color palette (modal) ---------- */
.color-palette {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  padding: 4px 0;
}

.color-swatch {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid transparent;
  padding: 0;
}

.color-swatch.selected {
  border-color: var(--text);
}

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 16px;
}

.modal {
  background: var(--bg);
  border-radius: 10px;
  padding: 24px;
  width: 100%;
  max-width: 440px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
}

.modal h2 { margin: 0 0 16px; font-size: 18px; }

.modal label {
  display: flex;
  flex-direction: column;
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
  margin-bottom: 12px;
  gap: 4px;
}

.modal input,
.modal textarea,
.modal select {
  font: inherit;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  background: var(--bg);
}

.modal input:focus,
.modal textarea:focus,
.modal select:focus {
  outline: none;
  border-color: var(--accent);
}

.modal .row { display: flex; gap: 12px; }
.modal .row label { flex: 1; }

.modal-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 16px;
}

.modal-actions .spacer { flex: 1; }

.hidden { display: none !important; }

/* ---------- Header actions (sync button) ---------- */
.header-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.sync-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.sync-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #9ca3af;
  flex-shrink: 0;
}

.sync-dot[data-status="idle"] { background: #9ca3af; }
.sync-dot[data-status="connected"] { background: #16a34a; }
.sync-dot[data-status="syncing"] {
  background: #f59e0b;
  animation: sync-pulse 1s ease-in-out infinite;
}
.sync-dot[data-status="error"] { background: var(--danger); }

@keyframes sync-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.35; }
}

/* ---------- Confirm modal ---------- */
.confirm-backdrop { z-index: 400; }
.confirm-modal { max-width: 360px; }
.confirm-message {
  margin: 0 0 16px;
  font-size: 14px;
  color: var(--text);
  line-height: 1.5;
}

/* ---------- Sync modal ---------- */
.sync-modal { max-width: 480px; }

.sync-section { margin-bottom: 16px; }

.sync-section > p {
  font-size: 13px;
  line-height: 1.5;
  margin: 0 0 8px;
  color: var(--text);
}

.sync-section .muted {
  color: var(--muted);
  font-size: 12px;
}

.sync-steps {
  margin: 8px 0;
  padding-left: 20px;
  font-size: 13px;
  line-height: 1.6;
  color: var(--text);
}

.sync-steps code {
  background: var(--surface);
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.sync-buttons {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 12px;
}

.sync-status-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: var(--surface);
  border-radius: 6px;
  margin-bottom: 12px;
  font-size: 13px;
}

.sync-warning {
  background: #fef3c7;
  border: 1px solid #fbbf24;
  color: #78350f;
  padding: 10px 12px;
  border-radius: 6px;
  font-size: 12px;
  line-height: 1.5;
  margin-bottom: 12px;
}

.sync-error {
  background: #fee2e2;
  border: 1px solid #fca5a5;
  color: #7f1d1d;
  padding: 10px 12px;
  border-radius: 6px;
  font-size: 12px;
  line-height: 1.5;
  margin-bottom: 12px;
  overflow-wrap: anywhere;
}

.sync-modal label {
  display: flex;
  flex-direction: column;
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
  gap: 4px;
  margin-bottom: 12px;
}

.sync-modal input[type="text"] {
  font: inherit;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  background: var(--bg);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
}

.sync-modal input[type="text"]:focus {
  outline: none;
  border-color: var(--accent);
}

/* ============== Mobile / weekly view ============== */
@media (max-width: 768px) {
  .app-header { padding: 10px 12px; }
  .nav { gap: 4px; flex-wrap: wrap; }
  .month-label { font-size: 15px; margin-left: 2px; }
  .weekday-row { display: none; }

  .calendar-grid {
    display: flex;
    flex-direction: column;
  }

  .day-cell {
    min-height: auto;
    border-right: none;
    padding: 10px 14px;
    gap: 4px;
  }

  .day-cell.other-month { background: var(--bg); color: var(--text); }

  .day-cell .day-label {
    font-size: 12px;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 4px;
  }

  .day-cell .day-label .day-number {
    font-size: 16px;
    color: var(--text);
    text-transform: none;
    margin: 0;
  }

  .day-cell.today .day-label .day-number {
    background: var(--accent);
    color: #fff;
    width: auto;
    height: auto;
    border-radius: 999px;
    padding: 2px 10px;
    display: inline-flex;
  }

  .event {
    font-size: 14px;
    padding: 8px 12px;
  }
}
