:root {
  color-scheme: light dark;
  --bg: #f6f2ea;
  --surface: #fffdf9;
  --ink: #26221c;
  --muted: #6f675b;
  --line: #e2dacb;
  --accent: #b4532f;
  --accent-ink: #fff;
  --danger: #a8321f;
  --today: #fbe9df;
  --outside: #efe9dd;
  --terracotta: #b4532f;
  --ocre: #a87510;
  --sapin: #2f7a5b;
  --bleu: #2f6a9e;
  --prune: #84457f;
  --ardoise: #5c6670;
  --hour: 48px;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1916;
    --surface: #24211d;
    --ink: #eee7da;
    --muted: #a69c8c;
    --line: #3a352e;
    --accent: #e08a63;
    --accent-ink: #26130a;
    --danger: #ef8a78;
    --today: #3b2a20;
    --outside: #1f1d19;
    --terracotta: #e08a63;
    --ocre: #d9ad4f;
    --sapin: #63bd97;
    --bleu: #72afe2;
    --prune: #c78cc2;
    --ardoise: #a3aeb9;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.4 var(--sans);
}

[data-color="terracotta"] { --c: var(--terracotta); }
[data-color="ocre"] { --c: var(--ocre); }
[data-color="sapin"] { --c: var(--sapin); }
[data-color="bleu"] { --c: var(--bleu); }
[data-color="prune"] { --c: var(--prune); }
[data-color="ardoise"] { --c: var(--ardoise); }

/* Commandes */
button, .button {
  display: inline-block;
  font: inherit;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 6px 12px;
  cursor: pointer;
  text-decoration: none;
}
button:hover, .button:hover { border-color: var(--muted); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
a { color: var(--accent); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
button.danger { color: var(--danger); }
button.icon { font-size: 19px; line-height: 1; padding: 5px 11px 7px; }

input, select, textarea {
  width: 100%;
  font: inherit;
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 7px 9px;
}
input[type="checkbox"], input[type="radio"] { width: auto; }
textarea { resize: vertical; }

/* Barre du haut */
#app { display: flex; flex-direction: column; height: 100dvh; }

.bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 16px;
  padding: 12px 20px;
}
.nav, .views, .actions { display: flex; gap: 6px; }
.bar h1 {
  flex: 1;
  min-width: 190px;
  margin: 0;
  font: 500 27px/1.2 var(--serif);
}
.bar h1::first-letter { text-transform: uppercase; }
.views { gap: 0; }
.views button { border-radius: 0; margin-left: -1px; }
.views button:first-child { border-radius: 7px 0 0 7px; margin-left: 0; }
.views button:last-child { border-radius: 0 7px 7px 0; }
.views button[aria-pressed="true"] { background: var(--ink); border-color: var(--ink); color: var(--bg); }

main { flex: 1; min-height: 0; padding: 0 20px 20px; display: flex; flex-direction: column; }

/* Pastille d'événement */
.chip {
  display: block;
  width: 100%;
  padding: 1px 5px;
  border: 0;
  border-left: 3px solid var(--c);
  border-radius: 3px;
  background: color-mix(in srgb, var(--c) 17%, var(--surface));
  color: var(--ink);
  font-size: 12.5px;
  line-height: 1.45;
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chip:hover { background: color-mix(in srgb, var(--c) 30%, var(--surface)); }
.chip time { color: var(--muted); margin-right: 3px; font-variant-numeric: tabular-nums; }

/* Mois */
.month-head, .month-grid { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); }
.month-head div {
  padding: 4px 8px;
  font-size: 12px;
  color: var(--muted);
  text-transform: capitalize;
}
.month-grid {
  flex: 1;
  min-height: 0;
  grid-auto-rows: minmax(0, 1fr);
  background: var(--line);
  gap: 1px;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}
.day {
  min-width: 0;
  min-height: 0;
  padding: 3px 4px;
  background: var(--surface);
  display: flex;
  flex-direction: column;
  gap: 2px;
  overflow: hidden;
  cursor: pointer;
}
.day.outside { background: var(--outside); }
.day.outside .num { color: var(--muted); }
.day.today { background: var(--today); }
.day.today .num { color: var(--accent); font-weight: 700; }
.num { font-size: 13px; padding: 1px 3px; font-variant-numeric: tabular-nums; }
.more {
  border: 0;
  background: none;
  padding: 0 4px;
  color: var(--muted);
  font-size: 12px;
  text-align: left;
}

/* Semaine */
.week {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: auto;
}
.week-inner { min-width: 620px; flex: 1; display: flex; flex-direction: column; }
.week-head, .week-allday, .week-grid { display: grid; grid-template-columns: 52px repeat(7, minmax(0, 1fr)); }
.week-top { position: sticky; top: 0; z-index: 3; background: var(--surface); border-bottom: 1px solid var(--line); }
.week-head > div { padding: 5px 7px; font-size: 12.5px; color: var(--muted); text-transform: capitalize; }
.week-head > div.today { color: var(--accent); font-weight: 700; }
.week-head strong { font: 500 19px/1.1 var(--serif); color: var(--ink); margin-left: 2px; }
.week-head .today strong { color: var(--accent); }
.week-allday > div { min-height: 8px; padding: 1px 2px 3px; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.hours div { height: var(--hour); padding-right: 6px; font-size: 11px; color: var(--muted); text-align: right; transform: translateY(-7px); }
.hours div:first-child { visibility: hidden; }
.col {
  position: relative;
  height: calc(var(--hour) * 24);
  border-left: 1px solid var(--line);
  background: repeating-linear-gradient(to bottom, var(--line) 0 1px, transparent 1px var(--hour));
}
.col.today { background-color: color-mix(in srgb, var(--today) 45%, transparent); }
.block {
  position: absolute;
  overflow: hidden;
  padding: 2px 5px;
  border: 0;
  border-left: 3px solid var(--c);
  border-radius: 3px;
  background: color-mix(in srgb, var(--c) 22%, var(--surface));
  color: var(--ink);
  font-size: 12px;
  line-height: 1.3;
  text-align: left;
  display: block;
}
.block:hover { z-index: 2; background: color-mix(in srgb, var(--c) 35%, var(--surface)); }
.block strong { font-weight: 600; display: block; }
.block time { color: var(--muted); }
.now { position: absolute; left: 0; right: 0; height: 2px; background: var(--accent); z-index: 2; pointer-events: none; }

/* Liste */
.list { flex: 1; min-height: 0; overflow: auto; max-width: 720px; width: 100%; }
.list section { display: grid; grid-template-columns: 112px minmax(0, 1fr); gap: 12px; padding: 12px 0; border-top: 1px solid var(--line); }
.list section:first-child { border-top: 0; }
.list h2 { margin: 0; font: 500 16px/1.3 var(--serif); }
.list h2::first-letter { text-transform: uppercase; }
.list h2.today { color: var(--accent); }
.entry {
  display: block;
  width: 100%;
  padding: 5px 9px;
  margin-bottom: 4px;
  border: 0;
  border-left: 3px solid var(--c);
  border-radius: 4px;
  background: transparent;
  text-align: left;
}
.entry:hover { background: color-mix(in srgb, var(--c) 12%, transparent); }
.entry small { display: block; color: var(--muted); font-size: 13px; }
.empty { padding: 40px 0; color: var(--muted); }

/* Dialogues */
dialog {
  width: min(520px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  padding: 20px;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 12px 40px rgb(0 0 0 / 0.25);
}
dialog::backdrop { background: rgb(30 25 18 / 0.4); }
dialog h2 { margin: 0 0 14px; font: 500 22px/1.2 var(--serif); }
dialog h3 { margin: 20px 0 6px; font-size: 14px; }
dialog p, dialog ul { margin: 0 0 10px; color: var(--muted); font-size: 14px; }
dialog ul { padding-left: 18px; }
dialog li { margin-bottom: 4px; }
dialog strong { color: var(--ink); }
#event-form label, .stack label { display: block; margin-bottom: 10px; font-size: 13px; color: var(--muted); }
#event-form label.check { display: flex; align-items: center; gap: 7px; color: var(--ink); font-size: 14px; }
.row { display: flex; gap: 10px; }
.row label { flex: 1; min-width: 0; }
.row label.time { flex: 0 0 120px; }
.colors { border: 0; padding: 0; margin: 0 0 10px; }
.colors legend { padding: 0; font-size: 13px; color: var(--muted); margin-bottom: 5px; }
#color-choices { display: flex; gap: 8px; }
#color-choices label { margin: 0; cursor: pointer; }
#color-choices input { position: absolute; opacity: 0; }
#color-choices span { display: block; width: 26px; height: 26px; border-radius: 50%; background: var(--c); }
#color-choices input:checked + span { box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--c); }
#color-choices input:focus-visible + span { outline: 2px solid var(--ink); outline-offset: 4px; }
.dialog-actions { display: flex; gap: 8px; margin-top: 14px; }
.spacer { flex: 1; }
.form-error { color: var(--danger); margin: 0 0 8px; font-size: 14px; }
.form-error:empty { display: none; }
.copy { display: flex; gap: 8px; margin-bottom: 10px; }
.copy input { font: 13px ui-monospace, Menlo, Consolas, monospace; }
.note { color: var(--danger) !important; }
.file-actions { display: flex; flex-wrap: wrap; gap: 8px; }
#import-result { margin-top: 8px; font-size: 14px; }
#import-result ul { margin-top: 4px; max-height: 110px; overflow: auto; font-size: 13px; }

.banner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 12px;
  margin: 0 20px 12px;
  padding: 8px 12px;
  border-left: 3px solid var(--danger);
  border-radius: 4px;
  background: var(--today);
  font-size: 14px;
}
.users { list-style: none; padding: 0; color: var(--ink); }
.users li { display: flex; align-items: center; gap: 8px; padding: 6px 0; border-top: 1px solid var(--line); margin: 0; }
.users span { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.users small { display: block; color: var(--muted); }
.users button { padding: 3px 8px; font-size: 13px; }

/* Connexion */
.login {
  width: min(340px, calc(100vw - 32px));
  margin: 18vh auto 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.login h1 { margin: 0; font: 500 32px/1.2 var(--serif); }
.login label { display: block; font-size: 13px; color: var(--muted); }

#toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  padding: 8px 14px;
  border-radius: 8px;
  background: var(--ink);
  color: var(--bg);
  z-index: 10;
}

@media (max-width: 720px) {
  .bar {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas: "title actions" "nav views";
    gap: 8px;
    padding: 10px 16px;
  }
  .bar h1 { grid-area: title; min-width: 0; font-size: 23px; }
  .actions { grid-area: actions; justify-self: end; }
  .nav { grid-area: nav; }
  .views { grid-area: views; justify-self: end; }
  .views button, .actions button, .nav button { padding: 5px 9px; }
  #new-event { font-size: 0; padding: 2px 12px 4px; }
  #new-event::before { content: "+"; font-size: 20px; }
  main { padding: 0 16px 16px; }
  .banner { margin: 0 16px 10px; }
  .day { padding: 2px; }
  .chip { font-size: 11px; padding: 0 2px; border-left-width: 2px; }
  .chip time { display: none; }
  .list section { grid-template-columns: 1fr; gap: 4px; }
  .row { flex-wrap: wrap; }
  .row label { flex: 1 1 130px; }
  .row label.time { flex: 1 1 100px; }
}

@media (max-width: 400px) {
  .nav { gap: 4px; }
  .nav button.icon { padding: 3px 8px 5px; }
  #today, .views button, #open-ical, #open-account { font-size: 12px; padding: 5px; }
}
