* {
  box-sizing: border-box;
}

:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --border: #e0e2e6;
  --text: #1c1e21;
  --text-muted: #6b7280;
  --accent: #3b6ff0;
  --accent-contrast: #ffffff;
  --danger: #d64545;
  --today-ring: #3b6ff0;
  --radius: 14px;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15161a;
    --surface: #1f2024;
    --border: #303237;
    --text: #f0f1f3;
    --text-muted: #9a9ea6;
    --accent: #5b86ff;
    --accent-contrast: #0b0b0d;
    --shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  }
}

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

#app {
  max-width: 1100px;
  margin: 0 auto;
  padding: env(safe-area-inset-top) 16px 24px;
  display: flex;
  flex-direction: column;
  height: 100%;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  padding: 16px 0;
}

.nav-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.nav-group h1 {
  font-size: 1.4rem;
  font-weight: 600;
  margin: 0;
  min-width: 200px;
  text-align: center;
  text-transform: capitalize;
}

.actions {
  display: flex;
  gap: 8px;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: var(--surface);
  color: var(--text);
  font-size: 1.2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}
.icon-btn:active {
  transform: scale(0.94);
}

.btn {
  border: none;
  background: var(--surface);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 0.95rem;
  font-weight: 500;
  cursor: pointer;
  box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}
.btn:active {
  transform: scale(0.97);
}

.btn-primary {
  background: var(--accent);
  color: var(--accent-contrast);
}

.btn-danger {
  background: transparent;
  color: var(--danger);
  box-shadow: none;
  border: 1px solid var(--danger);
}

.btn-sm {
  padding: 6px 12px;
  font-size: 0.82rem;
}

.today-panel {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

.today-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 4px;
}

.today-panel-header h2 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 600;
}

.today-panel .event-list {
  margin-top: 10px;
}

.weekday-row {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 8px;
  padding: 4px 0;
  color: var(--text-muted);
  font-size: 0.8rem;
  font-weight: 600;
  text-align: center;
  text-transform: uppercase;
}

.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  grid-auto-rows: 1fr;
  gap: 8px;
  flex: 1;
  min-height: 480px;
}

.day-cell {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 8px;
  display: flex;
  flex-direction: column;
  cursor: pointer;
  border: 1px solid transparent;
  min-height: 84px;
  overflow: hidden;
  transition: background 0.15s ease;
}
.day-cell:active {
  background: color-mix(in srgb, var(--surface) 85%, var(--accent) 15%);
}

.day-cell.outside {
  opacity: 0.35;
}

.day-cell.today {
  border-color: var(--today-ring);
}

.day-num {
  font-size: 0.95rem;
  font-weight: 600;
  align-self: flex-start;
  width: 26px;
  height: 26px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
}

.day-cell.today .day-num {
  background: var(--accent);
  color: var(--accent-contrast);
}

.day-events {
  margin-top: 4px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  overflow: hidden;
  flex: 1;
}

.event-chip {
  font-size: 0.72rem;
  background: color-mix(in srgb, var(--accent) 16%, transparent);
  color: var(--accent);
  border-radius: 6px;
  padding: 2px 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.event-more {
  font-size: 0.7rem;
  color: var(--text-muted);
}

/* Modals */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 50;
}
.modal-backdrop.open {
  display: flex;
}

.modal {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  width: 100%;
  max-width: 480px;
  max-height: 90vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-header h2 {
  margin: 0;
  font-size: 1.1rem;
}

.modal-body {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

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

.field input {
  font-size: 1rem;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}

.field-row {
  display: flex;
  gap: 12px;
}

.modal-footer {
  justify-content: flex-end;
  margin-top: 4px;
}

.event-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.event-item {
  background: var(--bg);
  border-radius: 10px;
  padding: 10px 12px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.event-item .event-time {
  font-size: 0.8rem;
  color: var(--accent);
  font-weight: 600;
}

.event-item .event-meta {
  font-size: 0.78rem;
  color: var(--text-muted);
}

.empty-hint {
  color: var(--text-muted);
  font-size: 0.9rem;
  text-align: center;
  padding: 12px 0;
}

textarea {
  width: 100%;
  min-height: 90px;
  font-size: 0.85rem;
  padding: 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  resize: vertical;
  font-family: monospace;
}

.hint {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin: 0;
}

.qr-wrap {
  width: 200px;
  aspect-ratio: 1 / 1;
  margin: 5px auto;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 3px;
  background: #ffffff;
  border-radius: 16px;
  border: 1px solid var(--border);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
}
.qr-wrap img, .qr-wrap table {
  width: 100%;
  height: 100%;
  object-fit: contain;
  image-rendering: pixelated;
}

.qr-scan-wrap {
  width: 100%;
  aspect-ratio: 1 / 1;
  background: #000;
  border-radius: 10px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}
.qr-scan-wrap video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 10px;
  font-size: 0.9rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 100;
}
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* Mobile tweaks */
@media (max-width: 640px) {
  .nav-group h1 {
    font-size: 1.1rem;
    min-width: 140px;
  }
  .actions {
    width: 100%;
    justify-content: space-between;
  }
  .btn {
    padding: 8px 10px;
    font-size: 0.85rem;
  }
  .calendar-grid {
    gap: 5px;
  }
  .day-cell {
    padding: 5px;
    min-height: 64px;
    border-radius: 10px;
  }
  .day-num {
    font-size: 0.8rem;
    width: 22px;
    height: 22px;
  }
  .event-chip {
    font-size: 0.62rem;
  }
  .field-row {
    flex-direction: column;
  }
}

/* iPad landscape/portrait */
@media (min-width: 700px) {
  .day-cell {
    min-height: 110px;
    padding: 10px;
  }
  .day-num {
    font-size: 1.05rem;
  }
  .event-chip {
    font-size: 0.78rem;
  }
}
