:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --text: #1c1f2a;
  --muted: #667085;
  --border: #e3e6ee;
  --accent: #4f46e5;
  --accent-soft: #eef0ff;
  --accent-text: #ffffff;
  --ok: #12805c;
  --ok-soft: #e6f6ef;
  --danger: #c0362c;
  --best: #f5a524;
  /* Results colors: fill + edge for each availability band */
  --lvl-all-bg: #d3f5e3;  --lvl-all: #1a8a55;
  --lvl-most-bg: #fff1bf; --lvl-most: #b08600;
  --lvl-some-bg: #ffe0c2; --lvl-some: #d56500;
  --lvl-few-bg: #ffd9d6;  --lvl-few: #d0342c;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1117;
    --surface: #181b24;
    --text: #e6e8ef;
    --muted: #9aa1b2;
    --border: #2a2f3c;
    --accent: #8b85ff;
    --accent-soft: #24264a;
    --accent-text: #0f1117;
    --ok: #4cc79a;
    --ok-soft: #173329;
    --danger: #ff7b72;
    --lvl-all-bg: #123a24;  --lvl-all: #45d18a;
    --lvl-most-bg: #3a3210; --lvl-most: #f2c94c;
    --lvl-some-bg: #42260f; --lvl-some: #ff9a4a;
    --lvl-few-bg: #451a18;  --lvl-few: #ff6e64;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

a { color: var(--accent); }
h1 { font-size: 1.6rem; margin: 0; line-height: 1.25; }
h2 { font-size: 1.1rem; margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.strong { font-weight: 600; }

/* ---------- layout ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.brand { font-weight: 700; text-decoration: none; color: var(--text); white-space: nowrap; }
.user { display: flex; align-items: center; gap: 10px; min-width: 0; }
.user-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 30vw; }

main { max-width: 1040px; margin: 0 auto; padding: 24px 16px 64px; }
.view > * + * { margin-top: 16px; }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow);
}
.card > * + * { margin-top: 12px; }

.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.section-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 10px; flex-wrap: wrap; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.form-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 16px; }

/* ---------- controls ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 500;
  cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--danger); }
.btn.subtle { border-color: transparent; background: transparent; }
.btn.link { border: none; background: none; color: var(--accent); padding: 4px 6px; }
.btn.icon { width: 34px; height: 34px; padding: 0; font-size: 1.2rem; }
.btn.pulse { box-shadow: 0 0 0 4px var(--accent-soft); }

input[type="text"], input[type="time"], textarea {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; width: 100%;
}
input:focus, textarea:focus, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
textarea { resize: vertical; }

.field { display: block; }
.field > span { display: block; font-weight: 600; margin-bottom: 4px; }
.check { display: flex; align-items: center; gap: 8px; cursor: pointer; font-size: 0.9rem; }
.check input { width: 16px; height: 16px; accent-color: var(--accent); }

/* ---------- home ---------- */
.event-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.event-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; text-decoration: none; color: inherit; }
.event-row:hover { border-color: var(--accent); }
.event-name { font-weight: 600; font-size: 1.05rem; }
.badges { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.badge { font-size: 0.75rem; padding: 2px 8px; border-radius: 999px; background: var(--bg); border: 1px solid var(--border); white-space: nowrap; }
.badge.host { background: var(--accent-soft); border-color: transparent; color: var(--accent); }
.badge.ok { background: var(--ok-soft); border-color: transparent; color: var(--ok); }
.empty { text-align: center; padding: 32px 18px; }
.hero { text-align: center; padding: 48px 20px; max-width: 560px; margin: 40px auto 0; }
.gsi-button { display: flex; justify-content: center; min-height: 44px; }

/* ---------- editor ---------- */
.editor-grid { display: grid; grid-template-columns: minmax(300px, 380px) minmax(0, 1fr); gap: 16px; align-items: start; margin-top: 16px; }
.editor .fields { margin-top: 16px; }
.calendar { position: sticky; top: 72px; }
.cal-head { display: flex; align-items: center; justify-content: space-between; }
/* minmax(0, 1fr) keeps the 7 columns inside the card; plain 1fr lets cells grow past it. */
.cal-grid { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 4px; }
.cal-dow { text-align: center; font-size: 0.75rem; color: var(--muted); padding: 4px 0; }
.cal-cell {
  height: 40px; min-width: 0; padding: 0; border-radius: 8px; border: 1px solid transparent;
  background: transparent; color: var(--text); font: inherit; cursor: pointer;
}
.cal-cell.empty { cursor: default; }
.cal-cell:hover:not(:disabled):not(.empty) { border-color: var(--accent); }
.cal-cell.today { font-weight: 700; border-color: var(--border); }
.cal-cell.selected { background: var(--accent); color: var(--accent-text); }
.cal-cell.past { color: var(--muted); opacity: 0.45; }
.cal-cell.past.selected { opacity: 0.7; }
.cal-cell:disabled { cursor: default; }

.weekday-fill { border-top: 1px solid var(--border); padding-top: 12px; }
.chips { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 4px; margin-top: 8px; }
.chip {
  padding: 6px 0; min-width: 0; border-radius: 999px; border: 1px solid var(--border); background: var(--surface);
  color: var(--text); font: inherit; font-size: 0.8rem; cursor: pointer;
}
.chip.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }

.day-list { display: grid; gap: 10px; }
.day-card { padding: 14px; }
.day-card > * + * { margin-top: 8px; }
.day-card.linked { background: color-mix(in srgb, var(--surface) 70%, var(--bg)); }
.day-head { display: flex; align-items: center; justify-content: space-between; }
.slots { display: grid; gap: 6px; }
.slot-row { display: grid; grid-template-columns: 1fr auto 1fr auto; gap: 8px; align-items: center; }
.slot-row.invalid input { border-color: var(--danger); }
.slots .btn.link { justify-self: start; }
.inherit { margin: 0; }

/* ---------- event page ---------- */
.event-head .host { display: flex; align-items: center; gap: 6px; margin-top: 6px; }
.description { white-space: pre-wrap; }
.share { display: flex; gap: 8px; }
.share input { flex: 1; min-width: 0; }

.options { display: grid; gap: 14px; }
.option-date { font-weight: 600; margin-bottom: 6px; }
.option {
  display: grid; grid-template-columns: auto minmax(150px, auto) 1fr auto minmax(0, 200px);
  align-items: center; gap: 12px; padding: 8px 12px; border: 1px solid var(--border);
  border-radius: 10px; cursor: pointer; margin-top: 6px;
}
.option:hover { border-color: var(--accent); }
.option.picked { background: var(--accent-soft); border-color: var(--accent); }
.option.best .option-count { color: var(--best); }
.option input { width: 18px; height: 18px; accent-color: var(--accent); }
.option-time { font-variant-numeric: tabular-nums; }
.option-bar { height: 6px; border-radius: 3px; background: var(--border); overflow: hidden; }
.option-bar > span { display: block; height: 100%; background: var(--ok); transition: width 0.2s; }
.option-count { font-weight: 700; min-width: 2.5ch; text-align: right; font-variant-numeric: tabular-nums; }
.option-people { display: flex; align-items: center; gap: 2px; overflow: hidden; }

/* List / Calendar toggle */
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 999px; padding: 2px; }
.seg button {
  border: none; background: none; color: var(--muted); font: inherit; font-size: 0.85rem;
  padding: 4px 12px; border-radius: 999px; cursor: pointer;
}
.seg button.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

/* Calendar view of availability */
.free-months { display: grid; gap: 18px; }
.free-cal { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 4px; }
.free-cell {
  min-width: 0; min-height: 64px; padding: 6px; border-radius: 8px; border: 1px solid transparent;
  display: flex; flex-direction: column; align-items: flex-start; gap: 2px;
  font: inherit; color: var(--text); background: transparent; text-align: left;
}
.free-cell.off { color: var(--muted); opacity: 0.45; }
button.free-cell { border-color: var(--border); background: var(--bg); cursor: pointer; }
button.free-cell:hover { border-color: var(--accent); }
button.free-cell.picked { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
/* Some but not all of the day's times picked: half-filled. */
button.free-cell.partial {
  border-color: var(--accent);
  background: linear-gradient(135deg, var(--accent-soft) 50%, var(--bg) 50%);
}
.free-num { font-weight: 600; }
.free-times { font-size: 0.72rem; opacity: 0.85; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.free-count { font-size: 0.75rem; font-weight: 700; margin-top: auto; align-self: flex-end; }
button.free-cell.best:not(.picked) .free-count { color: var(--best); }

/* Results (closed poll) */
.badge.closed { background: var(--lvl-few-bg); border-color: transparent; color: var(--lvl-few); }
.lvl-all  { --lvl-bg: var(--lvl-all-bg);  --lvl: var(--lvl-all); }
.lvl-most { --lvl-bg: var(--lvl-most-bg); --lvl: var(--lvl-most); }
.lvl-some { --lvl-bg: var(--lvl-some-bg); --lvl: var(--lvl-some); }
.lvl-few  { --lvl-bg: var(--lvl-few-bg);  --lvl: var(--lvl-few); }
.lvl-none { --lvl-bg: var(--bg); --lvl: var(--border); }
button.free-cell.result { background: var(--lvl-bg); border: 1px solid var(--lvl); }
button.free-cell.result .free-count { color: var(--lvl); }
button.free-cell.result.selected { box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--lvl); }

.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 0.85rem; }
.legend > span { display: inline-flex; align-items: center; gap: 6px; }
.swatch { width: 14px; height: 14px; border-radius: 4px; background: var(--lvl-bg); border: 1px solid var(--lvl); }

.results-grid { display: grid; grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); gap: 20px; align-items: start; }
.day-detail { border: 1px solid var(--border); border-radius: 10px; padding: 14px; }
.day-detail h3 { margin: 0 0 10px; font-size: 1rem; }
.slot-result { border-left: 4px solid var(--lvl); padding-left: 12px; }
.slot-result + .slot-result { margin-top: 16px; }
.slot-result-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.two-col h4 { margin: 0 0 8px; font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.04em; }
.col-yes { color: var(--lvl-all); }
.col-no { color: var(--lvl-few); }
.two-col .people { gap: 6px; }

.people { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.people li { display: flex; align-items: center; gap: 10px; }
.people li .small { margin-left: auto; }

.avatar { border-radius: 50%; object-fit: cover; flex: none; vertical-align: middle; }
.avatar-initials {
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--accent-soft); color: var(--accent); font-size: 0.7rem; font-weight: 700;
}

/* ---------- feedback ---------- */
.loading { display: flex; align-items: center; gap: 10px; color: var(--muted); padding: 24px 0; }
.spinner {
  width: 18px; height: 18px; border-radius: 50%;
  border: 2px solid var(--border); border-top-color: var(--accent); animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.error-box { border-color: var(--danger); color: var(--danger); }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px;
  opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s;
  max-width: calc(100vw - 32px); z-index: 100;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); color: #fff; }
.toast.ok { background: var(--ok); color: #fff; }

/* ---------- small screens ---------- */
@media (max-width: 760px) {
  .editor-grid { grid-template-columns: 1fr; }
  .calendar { position: static; }
  .option { grid-template-columns: auto 1fr auto; }
  .option-bar { display: none; }
  .option-people { grid-column: 2 / -1; }
  .user-name { display: none; }
  .free-cell { padding: 4px; }
  .results-grid { grid-template-columns: 1fr; }
  .free-times { white-space: normal; line-height: 1.2; }
}
