:root {
  --bg: #FBF7F2;
  --surface: #FFFFFF;
  --surface-2: #F4EEE6;
  --line: #ECE4D9;
  --text: #2E2A26;
  --muted: #8C8278;
  --accent: #E07A5F;
  --accent-soft: #F9E3DB;
  --radius: 18px;
  --hour: 60px;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Rounded", "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  -webkit-text-size-adjust: 100%;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  overscroll-behavior: none;
}

body {
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
  touch-action: manipulation;
}

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

.app {
  display: grid;
  grid-template-columns: 92px 1fr;
  height: 100vh;
  height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.rail {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 20px 8px;
}

.rail-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  width: 72px;
  padding: 12px 0 10px;
  border-radius: var(--radius);
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
}

.rail-item svg {
  width: 28px;
  height: 28px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.rail-item.active {
  background: var(--accent-soft);
  color: var(--accent);
}

.rail-item:disabled { opacity: 0.4; cursor: default; }

.rail-people {
  margin-top: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.person {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  font-size: 11px;
  color: var(--muted);
}

.avatar {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--c);
  color: #fff;
  font-weight: 700;
  font-size: 17px;
}

.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  padding: 16px 20px 20px 4px;
}

/* ---------- Header ---------- */

.header {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  padding: 4px 4px 16px;
}

.header-title { margin-right: auto; }

.header h1 {
  margin: 0;
  font-size: 34px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.clock {
  margin: 2px 0 0;
  font-size: 16px;
  color: var(--muted);
}

.demo-pill {
  margin-left: 6px;
  padding: 2px 10px;
  border-radius: 999px;
  background: #FFF3C4;
  font-size: 13px;
  color: #6B5A1E;
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 16px;
}

.nav-buttons {
  display: flex;
  align-items: center;
  gap: 6px;
}

.icon-btn {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--surface);
  font-size: 30px;
  line-height: 1;
  padding-bottom: 4px;
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.06);
}

.pill-btn {
  height: 48px;
  padding: 0 20px;
  border-radius: 999px;
  background: var(--surface);
  font-weight: 600;
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.06);
}

.segmented {
  display: flex;
  padding: 4px;
  border-radius: 999px;
  background: var(--surface-2);
}

.segmented button {
  height: 40px;
  padding: 0 20px;
  border-radius: 999px;
  font-weight: 600;
  color: var(--muted);
}

.segmented button[aria-selected="true"] {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 3px rgb(0 0 0 / 0.1);
}

/* ---------- View container ---------- */

.view {
  flex: 1;
  min-height: 0;
  background: var(--surface);
  border-radius: 24px;
  box-shadow: 0 2px 10px rgb(80 60 40 / 0.06);
  overflow: hidden;
}

/* ---------- Event chips (shared) ---------- */

.chip {
  --c: var(--accent);
  display: flex;
  gap: 4px;
  align-items: baseline;
  width: 100%;
  min-width: 0;
  padding: 2px 6px;
  border-radius: 6px;
  font-size: 13px;
  line-height: 1.35;
  text-align: left;
  white-space: nowrap;
}

.chip::before {
  content: "";
  flex: none;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--c);
  transform: translateY(-1px);
}

.chip-time { color: var(--muted); flex: none; }
.chip-title { overflow: hidden; text-overflow: ellipsis; }

.chip-allday {
  background: color-mix(in srgb, var(--c) 22%, white);
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  display: block;
}
.chip-allday::before { display: none; }

.chip-more {
  padding: 0 6px;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
}

/* ---------- Month ---------- */

.month {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  grid-template-rows: auto repeat(var(--weeks), minmax(0, 1fr));
  height: 100%;
}

.month-weekday {
  padding: 12px 10px 8px;
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  border-bottom: 1px solid var(--line);
}

.month-cell {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-height: 0;
  padding: 6px 4px;
  overflow: hidden;
  text-align: left;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}

.month-cell:nth-child(7n) { border-right: 0; }
.month-cell.other-month { background: #FDFBF8; }
.month-cell.other-month .month-date { color: #C5BBB0; }

.month-date {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  margin: 0 0 2px 2px;
  border-radius: 50%;
  font-size: 16px;
  font-weight: 600;
}

.month-cell.today .month-date {
  background: var(--accent);
  color: #fff;
}

/* ---------- Day / week time grid ---------- */

.tg {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.tg-row {
  display: grid;
  grid-template-columns: 60px repeat(var(--days), minmax(0, 1fr));
}

.tg-head { border-bottom: 1px solid var(--line); }

.tg-day-head {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px 0 8px;
}

.tg-weekday {
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.tg-date {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  font-size: 22px;
  font-weight: 700;
}

.tg-day-head.today .tg-weekday { color: var(--accent); }
.tg-day-head.today .tg-date { background: var(--accent); color: #fff; }

.tg-allday {
  border-bottom: 1px solid var(--line);
  max-height: 110px;
  overflow-y: auto;
}

.tg-allday-label {
  padding: 6px 8px 0 0;
  font-size: 11px;
  text-align: right;
  color: var(--muted);
}

.tg-allday-col {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 4px 3px;
  border-left: 1px solid var(--line);
  min-width: 0;
}

.tg-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.tg-body { position: relative; }

.tg-hour-label {
  height: var(--hour);
  padding-right: 8px;
  font-size: 12px;
  text-align: right;
  color: var(--muted);
  transform: translateY(-0.6em);
}

.tg-col {
  position: relative;
  height: calc(24 * var(--hour));
  border-left: 1px solid var(--line);
  background-image: linear-gradient(var(--line) 1px, transparent 1px);
  background-size: 100% var(--hour);
}

.tg-col.today { background-color: #FFFBF6; }

.tg-event {
  --c: var(--accent);
  position: absolute;
  display: flex;
  flex-direction: column;
  gap: 1px;
  margin: 1px 2px;
  padding: 4px 8px;
  overflow: hidden;
  border-radius: 8px;
  border-left: 4px solid var(--c);
  background: color-mix(in srgb, var(--c) 18%, white);
  text-align: left;
  font-size: 13px;
  line-height: 1.25;
}

.tg-event-title {
  flex: none;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: var(--lines, 1);
  overflow: hidden;
  font-weight: 600;
  overflow-wrap: anywhere;
}
.tg-event-time { flex: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: color-mix(in srgb, var(--text) 65%, transparent); font-size: 12px; }

.tg-now {
  position: absolute;
  left: -1px;
  right: 0;
  height: 2px;
  background: var(--accent);
  pointer-events: none;
  z-index: 2;
}

.tg-now::before {
  content: "";
  position: absolute;
  left: -5px;
  top: -4px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--accent);
}

.view[data-view="day"] { --hour: 72px; }
.view[data-view="day"] .tg-event { font-size: 16px; padding: 6px 12px; }
.view[data-view="day"] .tg-event-time { font-size: 14px; }

/* ---------- Event dialog ---------- */

.event-dialog {
  --c: var(--accent);
  width: min(480px, calc(100vw - 48px));
  padding: 24px 28px;
  border: 0;
  border-top: 10px solid var(--c);
  border-radius: 24px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 60px rgb(0 0 0 / 0.2);
  overflow: hidden;
}

.event-dialog::backdrop { background: rgb(46 42 38 / 0.35); }

.event-dialog-title { margin: 0 0 6px; font-size: 26px; }
.event-dialog-when { margin: 0 0 6px; font-size: 17px; color: var(--muted); }
.event-dialog-where:empty, .event-dialog-notes:empty { display: none; }
.event-dialog-where { margin: 0 0 6px; font-size: 17px; }
.event-dialog-notes {
  margin: 12px 0 0;
  max-height: 40vh;
  overflow-y: auto;
  white-space: pre-wrap;
  font-size: 15px;
  -webkit-user-select: text;
  user-select: text;
}

.event-dialog form { display: flex; justify-content: flex-end; margin-top: 20px; }
.event-dialog .pill-btn { background: var(--surface-2); }

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

body.login {
  display: grid;
  place-items: center;
  min-height: 100vh;
  padding: 16px;
}

.login-card {
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: min(360px, 100%);
  padding: 32px;
  border-radius: 24px;
  background: var(--surface);
  box-shadow: 0 2px 10px rgb(80 60 40 / 0.08);
}

.login-card h1 { margin: 0 0 4px; font-size: 26px; }
.login-error { margin: 0; color: #B3261E; }

.login-card input {
  height: 52px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 14px;
  font: inherit;
  font-size: 18px;
  background: var(--bg);
}

.login-card button {
  height: 52px;
  border-radius: 14px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 17px;
}

/* ---------- Narrow screens (portrait phone) ---------- */

@media (max-width: 700px) {
  .app { grid-template-columns: 1fr; }
  .rail { display: none; }
  .main { padding: 12px; }
  .header h1 { font-size: 26px; }
  .segmented button { padding: 0 14px; }
}
