:root {
  --bg: #f6f1ea;
  --surface: #fffdfa;
  --ink: #2a2622;
  --muted: #7a7168;
  --line: #e6ddd2;
  --accent: #2f4a3a;
  --accent-ink: #fff;
  --danger: #b3412e;
  --today: #f0e2cf;
  --radius: 12px;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--ink); font: 15px/1.45 var(--sans); }
[hidden] { display: none !important; }
h1, h2 { font-family: var(--serif); font-weight: 600; margin: 0; }

/* ---------- Buttons & fields ---------- */
.btn {
  font: inherit; border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  border-radius: 999px; padding: .45rem .95rem; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: #cfc3b5; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.1); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); border-color: #e7c3bb; background: transparent; }
.btn.link { border: 0; background: none; color: var(--muted); text-decoration: underline; padding: 0; }
.btn.icon { width: 2.2rem; height: 2.2rem; padding: 0; font-size: 1.3rem; line-height: 1; }
.btn.small { padding: .25rem .7rem; font-size: .85rem; }
.btn:disabled { opacity: .6; cursor: default; }

.stack { display: flex; flex-direction: column; gap: .85rem; }
.row { display: flex; gap: .7rem; flex-wrap: wrap; }
.row .field { flex: 1 1 8rem; }
.field { display: flex; flex-direction: column; gap: .3rem; font-size: .85rem; color: var(--muted); }
.field input, .field textarea {
  font: 15px var(--sans); color: var(--ink); background: #fff; border: 1px solid var(--line);
  border-radius: 8px; padding: .55rem .65rem; width: 100%;
}
.field input:focus, .field textarea:focus { outline: 2px solid #9db8a6; border-color: transparent; }
.check { display: flex; gap: .5rem; align-items: center; }
.muted { color: var(--muted); margin: 0; }
.small { font-size: .82rem; }
.error { color: var(--danger); margin: 0; font-size: .9rem; }
.spacer { flex: 1; }

/* ---------- Login ---------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 1.5rem; }
.login-card {
  width: min(380px, 100%); background: var(--surface); border: 1px solid var(--line);
  border-radius: 18px; padding: 2rem; display: flex; flex-direction: column; gap: 1.2rem;
  box-shadow: 0 10px 30px rgba(60, 40, 20, .06);
}
.brand { font-size: 1.8rem; }
.code-input { font-size: 1.6rem !important; letter-spacing: .5em; text-align: center; font-variant-numeric: tabular-nums; }

/* ---------- App shell ---------- */
.app { display: flex; flex-direction: column; height: 100%; }
.topbar {
  display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap;
  padding: .9rem 1.2rem; border-bottom: 1px solid var(--line);
}
.month-nav, .top-actions { display: flex; align-items: center; gap: .5rem; }
.month-title { font-size: 1.5rem; min-width: 11ch; text-align: center; }
.legend { display: flex; gap: .8rem; margin-right: .4rem; font-size: .85rem; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: .35rem; }
.dot { width: .6rem; height: .6rem; border-radius: 50%; display: inline-block; flex: none; }

.layout { flex: 1; display: grid; grid-template-columns: 1fr 300px; min-height: 0; }
.calendar { display: flex; flex-direction: column; padding: .6rem 1rem 1rem; min-height: 0; }
.weekdays, .grid { display: grid; grid-template-columns: repeat(7, 1fr); }
.weekdays div { font-size: .75rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); padding: .3rem .4rem; }
.grid {
  flex: 1; grid-auto-rows: minmax(92px, 1fr); border-top: 1px solid var(--line); border-left: 1px solid var(--line);
  background: var(--surface); border-radius: 0 0 var(--radius) var(--radius); overflow: hidden;
}
.cell {
  border: 0;
  border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); padding: .3rem .35rem;
  display: flex; flex-direction: column; gap: 2px; cursor: pointer; min-width: 0; text-align: left;
  background: none; font: inherit; color: inherit;
}
.cell:hover { background: #fbf7f1; }
.cell.other { color: #b8aea3; background: #faf7f2; }
.cell.selected { box-shadow: inset 0 0 0 2px var(--accent); }
.cell .num { font-size: .82rem; font-variant-numeric: tabular-nums; width: 1.6rem; height: 1.6rem; display: grid; place-items: center; border-radius: 50%; }
.cell.today .num { background: var(--accent); color: #fff; font-weight: 600; }
.chip {
  font-size: .75rem; padding: 1px 6px; border-radius: 5px; white-space: nowrap; overflow: hidden;
  text-overflow: ellipsis; border-left: 3px solid var(--c); background: color-mix(in srgb, var(--c) 14%, white);
}
.chip.allday { background: color-mix(in srgb, var(--c) 30%, white); }
.more { font-size: .72rem; color: var(--muted); padding-left: 4px; }
.dots { display: none; gap: 3px; padding-left: 3px; }

.day-panel { border-left: 1px solid var(--line); padding: 1rem 1.1rem; overflow-y: auto; }
.day-panel-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: .8rem; }
.day-panel h2 { font-size: 1.15rem; }
.day-events { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: .5rem; }
.day-events li {
  background: var(--surface); border: 1px solid var(--line); border-left: 4px solid var(--c);
  border-radius: 10px; padding: .6rem .75rem; cursor: pointer;
}
.day-events li:hover { border-color: #cfc3b5; border-left-color: var(--c); }
.day-events .t { font-weight: 600; }
.day-events .s { font-size: .82rem; color: var(--muted); }
.day-events .empty { border: 1px dashed var(--line); border-left: 1px dashed var(--line); color: var(--muted); cursor: default; background: none; }

/* ---------- Dialog ---------- */
.dialog {
  border: 0; border-radius: 16px; padding: 1.4rem; width: min(480px, calc(100% - 2rem));
  background: var(--surface); color: var(--ink); box-shadow: 0 20px 60px rgba(40, 25, 10, .25);
}
.dialog::backdrop { background: rgba(42, 38, 34, .35); }
.dialog h2 { font-size: 1.3rem; }
.dialog-actions { display: flex; gap: .5rem; align-items: center; margin-top: .3rem; }
.dialog .allday { display: none; }
.dialog.is-allday .timed { display: none; }
.dialog.is-allday .allday { display: flex; }

.toast {
  position: fixed; bottom: 1.2rem; left: 50%; transform: translateX(-50%); background: var(--ink); color: #fff;
  padding: .6rem 1rem; border-radius: 999px; font-size: .9rem; box-shadow: 0 6px 20px rgba(0,0,0,.2); z-index: 10;
}

/* ---------- Mobile ---------- */
@media (max-width: 760px) {
  .topbar { padding: .7rem; }
  .legend, #logout { display: none; }
  .month-title { font-size: 1.2rem; }
  .layout { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .calendar { padding: .4rem .5rem; }
  .grid { grid-auto-rows: 52px; flex: none; }
  .cell { align-items: center; padding: .25rem 0; }
  .cell .chip, .cell .more { display: none; }
  .cell .dots { display: flex; }
  .day-panel { border-left: 0; border-top: 1px solid var(--line); }
}

/* ---------- Two-timezone bits ---------- */
.clocks { display: flex; gap: .9rem; margin-right: .4rem; font-size: .85rem; color: var(--muted); }
.clocks span { display: inline-flex; align-items: center; gap: .35rem; white-space: nowrap; }
.clocks b { color: var(--ink); font-weight: 600; font-variant-numeric: tabular-nums; }
.chip .alt { opacity: .7; }
.tz, .day-events .tz { display: grid; grid-template-columns: auto 1fr; gap: 0 .5rem; font-size: .82rem; color: var(--muted); margin-top: .15rem; }
.tz b, .day-events .tz b { color: var(--ink); font-weight: 500; font-variant-numeric: tabular-nums; }
.dialog-head { display: flex; justify-content: space-between; align-items: center; gap: .5rem; }
.who { font-size: .82rem; color: var(--muted); display: inline-flex; align-items: center; gap: .35rem; }
.row.center { align-items: center; justify-content: space-between; }
.tz-switch { display: flex; align-items: center; gap: .5rem; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 999px; overflow: hidden; }
.seg button { font: inherit; font-size: .82rem; border: 0; background: transparent; padding: .3rem .7rem; cursor: pointer; color: var(--ink); }
.seg button[aria-checked="true"] { background: color-mix(in srgb, var(--pc) 35%, white); color: color-mix(in srgb, var(--pc) 45%, black); font-weight: 600; }
.tz-preview { background: #f3ede4; border-radius: 10px; padding: .55rem .75rem; font-size: .88rem; display: grid; grid-template-columns: auto 1fr; gap: .15rem .7rem; }
.tz-preview:empty { display: none; }
.tz-preview .lbl { color: var(--muted); }
.tz-preview .val { font-variant-numeric: tabular-nums; }
.dialog.is-allday .tz-preview, .dialog.is-allday .tz-switch { display: none; }
.notify-hint { margin: 0; font-size: .82rem; color: var(--muted); background: #f3ede4; border-radius: 8px; padding: .45rem .7rem; }
@media (max-width: 760px) { .clocks { display: none; } }

/* Each person's times are tinted in their color */
.ptime {
  background: color-mix(in srgb, var(--pc) 30%, white);
  color: color-mix(in srgb, var(--pc) 45%, black);
  border-radius: 5px; padding: 0 .3rem; font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap;
}
.chip .ptime { padding: 0 .2rem; margin-right: 2px; font-size: .7rem; }
.clocks .ptime { padding: .05rem .4rem; }
.tz .ptime { justify-self: start; }

/* ---------- Approvals ---------- */
.btn.approve { background: #2f6b45; color: #fff; border-color: #2f6b45; }
.approvals { margin: .6rem 1rem 0; border: 1px solid #ecd9a8; background: #fdf6e3; border-radius: var(--radius); }
.approvals-head {
  width: 100%; display: flex; justify-content: space-between; align-items: center; gap: .5rem;
  font: inherit; font-weight: 600; color: #6b5310; background: none; border: 0; padding: .7rem .9rem; cursor: pointer; text-align: left;
}
.approvals-head .chev { transition: transform .15s; font-size: 1.2rem; }
.approvals-head[aria-expanded="true"] .chev { transform: rotate(90deg); }
.approvals-list { list-style: none; margin: 0; padding: 0 .9rem .8rem; display: flex; flex-direction: column; gap: .5rem; }
.approvals-list li { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: .6rem .75rem; cursor: pointer; }
.approvals-list .t { font-weight: 600; }
.approve-row { display: flex; align-items: center; gap: .4rem; margin-top: .5rem; flex-wrap: wrap; }
.pending-tag { display: inline-block; font-size: .75rem; font-weight: 600; color: #6b5310; background: #fbedc4; border-radius: 999px; padding: .1rem .55rem; margin-bottom: .3rem; }
.chip.pending { background: repeating-linear-gradient(135deg, #fff 0 5px, color-mix(in srgb, var(--c) 12%, white) 5px 10px); border-left-style: dashed; opacity: .9; }
.day-events li.pending { border-style: dashed; border-left-style: solid; background: #fffcf3; }
.dialog .approve-row { margin-top: 0; background: #fdf6e3; border-radius: 10px; padding: .5rem .6rem; }
.dialog .approve-row .pending-tag { margin: 0; }
.dialog.read-only input, .dialog.read-only textarea { background: #f7f3ed; }
.dialog.read-only .tz-switch { display: none; }

/* ---------- iPhone ---------- */
/* 16px inputs stop iOS Safari from zooming in when a field is tapped. */
.field input, .field textarea, .code-input { font-size: 16px; }
.code-input { font-size: 1.6rem !important; }
input[type="date"], input[type="time"] { -webkit-appearance: none; appearance: none; min-height: 2.6rem; text-align: left; }
input[type="date"]::-webkit-date-and-time-value, input[type="time"]::-webkit-date-and-time-value { text-align: left; }
button, .cell, .chip, li[data-id] { -webkit-tap-highlight-color: transparent; touch-action: manipulation; }
/* Respect the notch / Dynamic Island and the home indicator. */
.topbar { padding-top: max(.9rem, env(safe-area-inset-top)); padding-left: max(1.2rem, env(safe-area-inset-left)); padding-right: max(1.2rem, env(safe-area-inset-right)); }
.login { padding-top: max(1.5rem, env(safe-area-inset-top)); padding-bottom: max(1.5rem, env(safe-area-inset-bottom)); }
.toast { bottom: max(1.2rem, calc(env(safe-area-inset-bottom) + .6rem)); }
.dialog { max-height: calc(100dvh - 2rem); overflow-y: auto; overscroll-behavior: contain; }

@media (max-width: 760px) {
  html, body { height: auto; }
  .app { height: auto; min-height: 100dvh; }
  .layout { display: block; }
  .topbar { display: grid; grid-template-columns: 1fr auto; gap: .5rem; padding-bottom: .6rem; }
  .month-nav { gap: .3rem; }
  .btn.icon { width: 2.75rem; height: 2.75rem; }          /* 44px touch targets */
  .top-actions { grid-column: 1 / -1; display: grid; grid-template-columns: 1fr auto; gap: .5rem; }
  .clocks { grid-column: 1 / -1; display: flex !important; font-size: .82rem; gap: .9rem; flex-wrap: wrap; margin: 0; }
  #new-event { justify-self: stretch; }
  #logout { display: inline-block !important; padding: .45rem .7rem; font-size: .85rem; }
  .approvals { margin: .5rem .5rem 0; }
  .day-panel { padding-bottom: max(1.5rem, env(safe-area-inset-bottom)); }
  /* Event window becomes a bottom sheet. */
  .dialog { width: 100%; max-width: 100%; margin: auto 0 0; border-radius: 18px 18px 0 0;
            padding-bottom: max(1.4rem, env(safe-area-inset-bottom)); max-height: 92dvh; }
  .dialog .row .field { flex: 1 1 40%; }
  .dialog-actions { position: sticky; bottom: calc(-1 * max(1.4rem, env(safe-area-inset-bottom))); background: var(--surface);
                    padding: .6rem 0 max(.2rem, env(safe-area-inset-bottom)); }
  .dialog-actions .btn { padding: .65rem 1.1rem; }
}

/* Pending events show as hollow dots on the phone grid */
.dot.pending { background: transparent !important; box-shadow: inset 0 0 0 2px var(--pc); }
.dialog .approve-row { margin-bottom: .2rem; }
