:root {
  --bg: #f2f3f5;
  --card: #ffffff;
  --text: #1c2128;
  --muted: #656d76;
  --border: #d8dde3;
  --primary: #1f6feb;
  --primary-press: #1a5fcc;
  --danger: #cf222e;
  --free: #dafbe1;
  --free-text: #116329;
  --busy: #fff1e5;
  --busy-text: #953800;
  --warn-bg: #fff8c5;
  --warn-text: #7d4e00;
  --tap: 48px; /* minimum touch target */
  --radius: 14px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1117;
    --card: #161b22;
    --text: #e6edf3;
    --muted: #8d96a0;
    --border: #30363d;
    --free: #12261e;
    --free-text: #56d364;
    --busy: #2d1b0e;
    --busy-text: #f0883e;
    --warn-bg: #2e2607;
    --warn-text: #e3b341;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left);
  overscroll-behavior-y: none;
  -webkit-tap-highlight-color: transparent;
}

/* No 300ms delay / double-tap zoom on interactive elements. */
button, a, input, textarea, label, summary, .fc { touch-action: manipulation; }

[hidden] { display: none !important; }

h1 { font-size: 1.2rem; margin: 0; }
h2 { font-size: 1.2rem; margin: 0 0 0.75rem; }
h2:focus { outline: none; }

/* ---------- Buttons ---------- */

button, .button {
  font: inherit;
  font-weight: 500;
  min-height: 44px;
  border-radius: 12px;
  padding: 0.55rem 1rem;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  user-select: none;
  -webkit-user-select: none;
  transition: transform 0.08s ease, background-color 0.15s ease;
}
button:active, .button:active { transform: scale(0.97); }
button:disabled { opacity: 0.55; }
button.primary, .button.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
button.primary:active, .button.primary:active { background: var(--primary-press); }
button.danger { color: var(--danger); border-color: transparent; background: transparent; }
button.ghost { background: transparent; }
button.big, .button.big { min-height: var(--tap); width: 100%; font-size: 1.05rem; }

.icon-button {
  width: 44px;
  padding: 0;
  font-size: 1.3rem;
  background: transparent;
  border-color: transparent;
}

.refresh { position: relative; font-size: 1.5rem; }
.refresh span { display: inline-block; }
.refresh.spinning span { animation: spin 0.6s linear infinite; }
/* Green dot = live connection active. */
.refresh.live::after {
  content: "";
  position: absolute;
  top: 9px;
  right: 8px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #2da44e;
  box-shadow: 0 0 0 2px var(--card);
}
@keyframes spin { to { transform: rotate(360deg); } }

.avatar {
  width: 44px;
  height: 44px;
  padding: 0;
  border-radius: 50%;
  border: none;
  color: #fff;
  font-weight: 700;
  font-size: 1.1rem;
}

/* ---------- Forms ---------- */

label { display: flex; flex-direction: column; gap: 0.3rem; margin-bottom: 0.9rem; font-weight: 500; font-size: 0.95rem; }
/* 16px+ font prevents iOS Safari from zooming into focused inputs. */
input, textarea {
  font: inherit;
  font-size: 16px;
  min-height: var(--tap);
  padding: 0.6rem 0.75rem;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  width: 100%;
  -webkit-appearance: none;
  appearance: none;
}
input[type="datetime-local"] { display: block; min-width: 0; }
input:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: 0; border-color: transparent; }
input:disabled, textarea:disabled { opacity: 0.85; }
textarea { resize: vertical; }

.error { color: var(--danger); min-height: 1.2em; margin: 0.25rem 0; }
.error:empty { min-height: 0; margin: 0; }
.warning { background: var(--warn-bg); color: var(--warn-text); padding: 0.6rem 0.8rem; border-radius: 10px; margin: 0 0 0.9rem; font-size: 0.95rem; }
.muted { color: var(--muted); margin: -0.4rem 0 0.9rem; }
.hint { color: var(--muted); font-size: 0.9rem; text-align: center; margin: 0.75rem 0 0; }

.card { background: var(--card); border: 1px solid var(--border); border-radius: 18px; padding: 1.5rem; }

/* ---------- Login ---------- */

.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login form { width: min(380px, 100%); display: flex; flex-direction: column; }
.login label { margin-bottom: 0.3rem; }
.login input { margin-bottom: 0.9rem; }
.login-icon { display: block; margin: 0 auto 0.5rem; border-radius: 14px; }
.login h1 { text-align: center; margin-bottom: 1.25rem; font-size: 1.4rem; }

/* ---------- Top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  min-height: 60px;
  padding: 0.4rem 12px 0.4rem 16px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar h1 { display: flex; align-items: center; }
.home-link { display: flex; align-items: center; gap: 0.5rem; min-height: 44px; padding-right: 0.5rem; color: inherit; text-decoration: none; }
.home-link:active { opacity: 0.7; }
.topbar h1 img { border-radius: 7px; }
.topbar-actions { display: flex; gap: 0.25rem; align-items: center; }

.install-hint {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0.75rem 12px 0;
  padding: 0.5rem 0.25rem 0.5rem 0.9rem;
  border-radius: var(--radius);
  border: 1px solid var(--primary);
  background: rgb(31 111 235 / 0.08);
  font-size: 0.95rem;
}
.install-hint > span { flex: 1; }

.status {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  margin: 0.75rem 12px 0;
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
}
.status:empty { display: none; }
.status strong { font-size: 1.05rem; }
.status span { font-size: 0.95rem; }
.status.free { background: var(--free); color: var(--free-text); }
.status.busy { background: var(--busy); color: var(--busy-text); }

/* Segmented control for switching views. */
.view-switch {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  margin: 0.75rem 12px 0;
  padding: 4px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.view-switch button { border: none; background: transparent; min-height: 40px; border-radius: 10px; color: var(--muted); }
.view-switch button[aria-selected="true"] { background: var(--primary); color: #fff; }

#calendar {
  margin: 0.75rem 12px 0;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.6rem;
}

.legend { display: flex; gap: 1rem; flex-wrap: wrap; justify-content: center; margin: 0.75rem 12px; color: var(--muted); font-size: 0.9rem; }
.legend i { display: inline-block; width: 0.75rem; height: 0.75rem; border-radius: 50%; margin-right: 0.35rem; vertical-align: -1px; }

/* Floating action button – within thumb reach, above the home indicator. */
.fab {
  position: fixed;
  right: 16px;
  bottom: calc(16px + var(--safe-bottom));
  z-index: 20;
  min-height: 56px;
  padding: 0 1.4rem 0 1.1rem;
  border-radius: 28px;
  border: none;
  background: var(--primary);
  color: #fff;
  font-size: 1.1rem;
  font-weight: 600;
  box-shadow: 0 6px 20px rgb(0 0 0 / 0.25);
}
.fab span { font-size: 1.6rem; line-height: 1; font-weight: 400; }

/* ---------- Dialogs as bottom sheets ---------- */

dialog {
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--card);
  color: var(--text);
  width: min(480px, calc(100vw - 32px));
  max-height: 90dvh;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 1.25rem;
  transition: transform 0.15s ease;
}
dialog::backdrop { background: rgb(0 0 0 / 0.45); }
.sheet-handle { display: none; }
.sheet-actions { display: flex; flex-direction: column; gap: 0.5rem; margin-top: 0.75rem; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 0.75rem; }

.chips { display: flex; flex-wrap: wrap; gap: 0.5rem; margin: -0.2rem 0 0.9rem; }
.chips button { flex: 1 0 auto; min-height: 44px; border-radius: 22px; padding: 0 0.9rem; font-size: 0.95rem; }

.subscribe p { margin: 0 0 0.9rem; }
.subscribe .button { margin-bottom: 1rem; }
.copy-row { display: flex; gap: 0.4rem; }
.copy-row input { font-size: 14px; }
details { border-top: 1px solid var(--border); }
details summary { cursor: pointer; font-weight: 500; min-height: var(--tap); display: flex; align-items: center; }
details p { margin: 0 0 0.9rem; color: var(--muted); }

/* ---------- FullCalendar ---------- */

.fc {
  --fc-border-color: var(--border);
  --fc-page-bg-color: var(--card);
  --fc-neutral-bg-color: var(--bg);
  --fc-list-event-hover-bg-color: var(--bg);
  --fc-today-bg-color: rgb(31 111 235 / 0.08);
  --fc-button-bg-color: var(--bg);
  --fc-button-border-color: var(--border);
  --fc-button-text-color: var(--text);
  --fc-button-hover-bg-color: var(--border);
  --fc-button-hover-border-color: var(--border);
  --fc-button-active-bg-color: var(--primary);
  --fc-button-active-border-color: var(--primary);
  --fc-now-indicator-color: var(--danger);
}
.fc .fc-toolbar.fc-header-toolbar { margin-bottom: 0.6rem; gap: 0.5rem; }
.fc .fc-toolbar-title { font-size: 1.1rem; text-align: center; }
.fc .fc-button { min-height: 44px; min-width: 44px; padding: 0 0.8rem; border-radius: 12px !important; font-size: 1rem; box-shadow: none !important; }
.fc .fc-button-group { gap: 0.35rem; }
.fc .fc-today-button { text-transform: none; }
.fc .fc-button-primary:disabled { opacity: 0.5; }
.fc-event { cursor: pointer; border-radius: 8px; }
.fc .fc-timegrid-event .fc-event-main { padding: 3px 5px; font-size: 0.9rem; line-height: 1.25; }
.fc .fc-daygrid-event { padding: 3px 4px; font-size: 0.8rem; min-height: 24px; }
.fc .fc-daygrid-block-event .fc-event-title { font-weight: 600; }
.fc .fc-list-event td { padding: 14px 12px; font-size: 1rem; }
.fc .fc-list-day-cushion { padding: 10px 12px; }
.fc .fc-list-empty { min-height: 160px; font-size: 1.05rem; }
.fc .fc-daygrid-day-frame { min-height: 64px; }

/* ---------- Layout switches ---------- */

.mobile-only { display: none; }

@media (max-width: 700px) {
  .desktop-only { display: none !important; }
  .mobile-only { display: inline-flex; }
  .topbar h1 { font-size: 1.1rem; }
  #calendar { padding: 0.4rem; margin-left: 8px; margin-right: 8px; }
  /* Room so the FAB never covers the last rows. */
  .legend { margin-bottom: calc(96px + var(--safe-bottom)); }

  /* Title (month + year) gets its own full-width row above the navigation buttons. */
  .fc .fc-toolbar.fc-header-toolbar { display: grid; grid-template-columns: 1fr auto; row-gap: 0.5rem; }
  .fc .fc-header-toolbar .fc-toolbar-chunk:nth-child(2) { grid-column: 1 / -1; order: -1; text-align: center; }
  .fc .fc-toolbar-title { font-size: 1.2rem; }
  .fc .fc-daygrid-day.fc-day-disabled { background: var(--bg); }

  /* Bigger rows = easier tapping on empty slots. */
  .fc .fc-timegrid-slot { height: 3.25rem; }
  .fc .fc-col-header-cell-cushion { padding: 8px 2px; font-size: 0.85rem; }
  .fc .fc-timegrid-axis-cushion, .fc .fc-timegrid-slot-label-cushion { font-size: 0.8rem; }

  /* Dialogs become bottom sheets. */
  dialog {
    width: 100vw;
    max-width: 100vw;
    margin: auto 0 0 0;
    border-radius: 20px 20px 0 0;
    border-bottom: none;
    padding: 0.5rem 16px calc(16px + var(--safe-bottom));
    animation: sheet-in 0.2s ease-out;
  }
  .sheet-handle { display: block; width: 40px; height: 5px; border-radius: 3px; background: var(--border); margin: 0.25rem auto 0.9rem; }
  .field-row { grid-template-columns: 1fr; gap: 0; }
}

@media (min-width: 701px) {
  .fc .fc-timegrid-slot { height: 2rem; }
  .sheet-actions { flex-direction: row-reverse; }
  .sheet-actions .big { width: auto; flex: 1; }
  .sheet-actions .danger { flex: 0 0 auto; margin-right: auto; }
}

@keyframes sheet-in {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

@media (prefers-reduced-motion: reduce) {
  dialog { animation: none; transition: none; }
  button:active { transform: none; }
}
