:root {
  --paper: #f3ecd9;
  --ink: #2b2b2b;
  --panel-bg: rgba(255, 255, 255, 0.92);
  --panel-border: rgba(0, 0, 0, 0.12);
  --accent: #1971c2;
  --danger: #e03131;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.18);
  --radius: 12px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: var(--paper);
  overflow: hidden;
  overscroll-behavior: none;
}
.hidden { display: none !important; }
.muted { color: #777; font-size: 13px; }
.spacer { flex: 1; }

button { font: inherit; cursor: pointer; }
.btn {
  border: 1px solid var(--panel-border); background: #fff; color: var(--ink);
  border-radius: 10px; padding: 9px 14px; font-weight: 600;
}
.btn:hover { background: #f4f4f4; }
.btn.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn.small { padding: 6px 10px; font-size: 13px; }
.link-btn { background: none; border: none; color: var(--accent); text-decoration: underline; padding: 0; }
.icon-btn {
  border: none; background: none; font-size: 20px; width: 40px; height: 40px;
  border-radius: 10px; display: grid; place-items: center;
}
.icon-btn:hover { background: rgba(0, 0, 0, 0.06); }
.icon-btn img { width: 22px; height: 22px; object-fit: contain; pointer-events: none; }

/* --- Connexion ----------------------------------------------------------- */
.auth {
  position: fixed; inset: 0; display: grid; place-items: center; padding: 20px;
  background: var(--paper);
}
.auth-card {
  width: 100%; max-width: 360px; background: var(--panel-bg);
  border: 1px solid var(--panel-border); border-radius: var(--radius);
  padding: 28px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 14px;
}
.auth-card h1 { margin: 0; font-size: 24px; }
.auth-card label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; font-weight: 600; }
.auth-card input {
  padding: 11px 12px; border: 1px solid var(--panel-border); border-radius: 10px;
  font-size: 16px; font-weight: 400;
}
.auth-error { color: var(--danger); font-size: 14px; min-height: 18px; margin: 0; }

/* --- Application --------------------------------------------------------- */
.app { display: flex; height: 100%; }
.sidebar {
  width: 150px; flex-shrink: 0; background: #fff; border-right: 1px solid var(--panel-border);
  display: flex; flex-direction: column; z-index: 30;
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 10px 14px 16px; border-bottom: 1px solid var(--panel-border); }
.sidebar-head #sidebarClose { display: none; }
.sidebar-actions { display: flex; gap: 8px; padding: 12px; }
.img-btn {
  flex: 1; height: 44px; padding: 4px; border: 1px solid var(--panel-border); background: #fff;
  border-radius: 10px; display: grid; place-items: center;
}
.img-btn:hover { background: #f4f4f4; }
.img-btn img { height: 30px; width: auto; max-width: 100%; object-fit: contain; pointer-events: none; }
.board-list { flex: 1; overflow-y: auto; padding: 6px; }
.sidebar-foot { padding: 12px 16px; border-top: 1px solid var(--panel-border); display: flex; flex-direction: column; gap: 4px; }

.board-item { display: flex; align-items: center; gap: 6px; border-radius: 8px; padding: 2px; }
.board-item > .b-label {
  flex: 1; display: flex; align-items: center; gap: 8px; padding: 8px 10px;
  border-radius: 8px; cursor: pointer; font-size: 14px; min-width: 0;
}
.board-item > .b-label span.name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.board-item .b-label:hover { background: rgba(0, 0, 0, 0.05); }
.board-item.active > .b-label { background: rgba(25, 113, 194, 0.14); color: var(--accent); font-weight: 600; }
.b-icon { width: 18px; height: 18px; object-fit: contain; flex-shrink: 0; pointer-events: none; }

/* Cellule de survol (nom complet + actions), déborde hors de la barre latérale */
.row-flyout {
  position: fixed; z-index: 200; background: #fff; border: 1px solid var(--panel-border);
  border-radius: 8px; box-shadow: var(--shadow); padding: 5px 8px; display: flex; align-items: center;
  gap: 8px; white-space: nowrap; font-size: 14px; cursor: pointer;
}
.row-flyout.hidden { display: none; }
.row-flyout .fly-icon { width: 18px; height: 18px; object-fit: contain; flex-shrink: 0; }
.row-flyout .fly-name { font-weight: 600; cursor: pointer; max-width: 280px; overflow: hidden; text-overflow: ellipsis; }
.row-flyout .fly-act { border: none; background: none; padding: 2px; border-radius: 6px; display: grid; place-items: center; cursor: pointer; }
.row-flyout .fly-act:hover { background: rgba(0, 0, 0, 0.08); }
.row-flyout .fly-act img { width: 20px; height: 20px; object-fit: contain; display: block; }
.b-actions button { border: none; background: none; width: 26px; height: 26px; border-radius: 6px; font-size: 13px; }
.b-actions button:hover { background: rgba(0, 0, 0, 0.1); }
.agenda-children { margin-left: 16px; border-left: 2px solid rgba(0,0,0,0.08); padding-left: 4px; }
.agenda-caret { cursor: pointer; width: 18px; display: grid; place-items: center; user-select: none; flex-shrink: 0; }
.agenda-caret img, .row-flyout .fly-caret img {
  width: 11px; height: 11px; object-fit: contain; display: block;
  transform: rotate(-90deg); transition: transform .12s; pointer-events: none;
}
.agenda-caret img.open, .row-flyout .fly-caret img.open { transform: rotate(0deg); }
.row-flyout .fly-caret { border: none; background: none; padding: 2px; border-radius: 6px;
  display: grid; place-items: center; cursor: pointer; flex-shrink: 0; }
.row-flyout .fly-caret:hover { background: rgba(0, 0, 0, 0.08); }

/* --- Zone principale ----------------------------------------------------- */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }
.topbar {
  height: 52px; flex-shrink: 0; display: flex; align-items: center; gap: 8px; padding: 0 12px;
  background: var(--panel-bg); border-bottom: 1px solid var(--panel-border); z-index: 20;
}
.topbar #menuBtn { display: none; }
.save-btn { border: 1px solid var(--panel-border); background: #fff; border-radius: 8px; padding: 4px 7px;
  display: grid; place-items: center; flex-shrink: 0; }
.save-btn img { height: 20px; width: auto; object-fit: contain; display: block; pointer-events: none; }
.save-btn:hover { background: #f4f4f4; }
.save-btn.dirty { border-color: var(--accent); color: var(--accent); }
.save-btn:disabled { opacity: 0.6; cursor: default; }
.save-status { font-size: 11px; color: #888; white-space: nowrap; flex-shrink: 0; }
.save-status.dirty { color: #e8830c; }
.save-status.saving { color: var(--accent); }
.save-status.offline { color: var(--danger); }
.save-status.ok { color: #37b24d; }
#autosaveSel { font: inherit; font-size: 12px; padding: 3px 4px; border-radius: 6px; border: 1px solid var(--panel-border); }
.board-title { font-weight: 600; font-size: 16px; cursor: text; padding: 4px 6px; border-radius: 6px; white-space: nowrap; flex-shrink: 0; }
.board-title:hover { background: rgba(0,0,0,0.05); }
.zoom-info { font-size: 13px; color: #666; min-width: 48px; text-align: right; }
.sync-dot { width: 11px; height: 11px; border-radius: 50%; background: #37b24d; box-shadow: 0 0 0 3px rgba(55,178,77,0.2); flex-shrink: 0; }
.sync-dot.offline { background: #f08c00; box-shadow: 0 0 0 3px rgba(240,140,0,0.2); }

/* Paramètres du crayon, en ligne dans la barre du haut */
.title-spacer { flex-shrink: 0; display: inline-block; }
.inline-tools {
  display: flex; align-items: center; gap: 8px; min-width: 0; flex: 0 1 auto;
  overflow-x: auto; overflow-y: hidden; padding: 4px 0; scrollbar-width: thin;
}
.inline-tools::-webkit-scrollbar { height: 6px; }
.inline-tools .divider { width: 1px; height: 26px; background: var(--panel-border); flex-shrink: 0; }
.inline-tools .grp-label { font-size: 10px; font-weight: 700; color: #999; white-space: nowrap; flex-shrink: 0; }
.tool-sliders { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.t-slider { display: flex; align-items: center; gap: 4px; flex-shrink: 0; }
.t-slider img { width: 16px; height: 16px; object-fit: contain; flex-shrink: 0; pointer-events: none; }
.t-slider input[type=range] { width: 72px; }
.topbar .swatches { display: flex; gap: 3px; flex-shrink: 0; }
.topbar .swatch { width: 20px; height: 20px; aspect-ratio: auto; border-radius: 5px; border-width: 2px; }
.topbar #recentColors .swatch { height: 20px; }
.topbar input[type=color] { width: 32px; height: 28px; flex-shrink: 0; }
.fav-btn { border: none; background: none; padding: 1px; border-radius: 5px; display: grid; place-items: center; flex-shrink: 0; cursor: pointer; }
.fav-btn:hover { background: rgba(0, 0, 0, 0.08); }
.fav-btn img { width: 16px; height: 16px; object-fit: contain; display: block; pointer-events: none; }

.canvas-wrap { flex: 1; position: relative; overflow: hidden; }
.paper {
  position: absolute; inset: 0;
  background-image: url("../../imgs/fondpapier.jpg");
  background-repeat: repeat;
  z-index: 0;
}
#board { position: absolute; inset: 0; width: 100%; height: 100%; z-index: 1; touch-action: none; cursor: none; }

/* --- Barre d'outils ------------------------------------------------------ */
.toolbar {
  position: absolute; left: 12px; top: 50%; transform: translateY(-50%);
  background: var(--panel-bg); border: 1px solid var(--panel-border); border-radius: 5px;
  box-shadow: var(--shadow); padding: 1px; display: flex; flex-direction: column; gap: 1px; z-index: 10;
}
.toolbar .tool {
  width: 40px; height: 40px; border: none; background: none; border-radius: 5px; margin: 5px;
  font-size: 20px; display: grid; place-items: center;
}
.toolbar .tool:hover { background: rgba(0, 0, 0, 0.07); }
.toolbar .tool.active { background: #dadbdb; box-shadow: inset 0 1px 3px 1px #919191; }
.toolbar .tool.danger:hover { background: rgba(224, 49, 49, 0.14); }
.toolbar .tool:disabled { opacity: 0.35; cursor: default; }
.toolbar .tool img { width: 30px; height: 30px; object-fit: contain; pointer-events: none; }
/* Effet « pressé » commun à tous les éléments cliquables */
button:active, a:active, .tool:active, .btn:active, .img-btn:active, .preset:active,
.swatch:active, .icon-btn:active, .link-btn:active, .sel-actions button:active,
.page-tile:active, .board-item .b-label:active, .row-flyout:active {
  background: #dadbdb;
  box-shadow: inset 0 1px 3px 1px #919191;
}
.toolbar .sep { height: 1px; background: var(--panel-border); margin: 3px 4px; }

/* --- Panneau couleurs/réglages ------------------------------------------ */
.panel {
  position: absolute; right: 12px; top: 12px; width: 190px;
  background: var(--panel-bg); border: 1px solid var(--panel-border); border-radius: 14px;
  box-shadow: var(--shadow); padding: 12px; z-index: 10;
  display: flex; flex-direction: column; gap: 12px; max-height: calc(100% - 24px); overflow-y: auto;
}
.panel-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.sliders { flex-direction: column; align-items: stretch; gap: 8px; }
.slider { display: flex; flex-direction: column; font-size: 12px; font-weight: 600; color: #555; gap: 3px; }
.slabel { display: flex; align-items: center; gap: 4px; }
.slider input[type=range] { width: 100%; }

/* Jauges (taille, opacité, pointe, fluidité) : piste fine, curseur rectangle arrondi */
#opacityRange, #sizeRange, #pointeRange, #fluiditeRange {
  -webkit-appearance: none; appearance: none; height: 10px; border: none; border-radius: 2px;
  background: #d9d9d9; outline: none; margin: 4px 0;
}
#opacityRange::-webkit-slider-thumb, #sizeRange::-webkit-slider-thumb,
#pointeRange::-webkit-slider-thumb, #fluiditeRange::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none; width: 10px; height: 22px;
  border-radius: 5px; background: #fff; border: 1px solid #9a9a9a;
  box-shadow: 0 1px 3px rgba(0,0,0,0.3); cursor: pointer;
}
#opacityRange::-moz-range-thumb, #sizeRange::-moz-range-thumb,
#pointeRange::-moz-range-thumb, #fluiditeRange::-moz-range-thumb {
  width: 10px; height: 22px; border-radius: 5px; background: #fff;
  border: 1px solid #9a9a9a; box-shadow: 0 1px 3px rgba(0,0,0,0.3); cursor: pointer;
}
#opacityRange::-moz-range-track, #sizeRange::-moz-range-track,
#pointeRange::-moz-range-track, #fluiditeRange::-moz-range-track { height: 10px; border: none; border-radius: 2px; background: #d9d9d9; }
#opacityRange::-moz-range-progress { height: 10px; border-radius: 2px; background: var(--accent); }
.slider-ico { width: 16px; height: 16px; object-fit: contain; vertical-align: middle; margin-right: 2px; }
.palette-block { display: flex; flex-direction: column; gap: 6px; }
.palette-label { font-size: 12px; font-weight: 700; color: #666; display: flex; justify-content: space-between; }
.palette-label .hint { font-weight: 400; font-size: 10px; color: #aaa; }
.swatches { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.swatch {
  aspect-ratio: 1; border-radius: 8px; border: 2px solid rgba(0,0,0,0.12); cursor: pointer;
  position: relative;
}
.swatch.empty { background: repeating-linear-gradient(45deg,#eee,#eee 4px,#f8f8f8 4px,#f8f8f8 8px); }
.swatch.selected { box-shadow: 0 0 0 2px #fff, 0 0 0 4px var(--accent); }
#recentColors .swatch { aspect-ratio: auto; height: 26px; }
#recentColors.swatches { grid-template-columns: repeat(3, 1fr); }
.presets { display: flex; flex-wrap: wrap; gap: 6px; }
.preset {
  height: 30px; min-width: 30px; padding: 0 6px; border-radius: 8px; border: 1px solid var(--panel-border);
  display: inline-flex; align-items: center; gap: 5px; background: #fff; font-size: 11px;
}
.preset .dot { width: 14px; height: 14px; border-radius: 50%; border: 1px solid rgba(0,0,0,0.2); }
/* Icône du stylo/stabilo teintée à la couleur + opacité enregistrées */
.preset .preset-ico {
  width: 18px; height: 18px; display: inline-block; background-color: var(--c, #000);
  -webkit-mask-size: contain; mask-size: contain;
  -webkit-mask-repeat: no-repeat; mask-repeat: no-repeat;
  -webkit-mask-position: center; mask-position: center;
}
.preset.add { justify-content: center; color: #888; }
.preset.add img { width: 16px; height: 16px; object-fit: contain; pointer-events: none; }

/* Barre d'actions de la sélection */
.sel-actions {
  position: absolute; z-index: 15; display: flex; gap: 2px;
  background: var(--panel-bg); border: 1px solid var(--panel-border);
  border-radius: 10px; box-shadow: var(--shadow); padding: 4px;
  transform: translate(-50%, calc(-100% - 10px)); white-space: nowrap;
}
.sel-actions.below { transform: translate(-50%, 10px); }
.sel-actions button {
  border: none; background: none; padding: 5px 8px; border-radius: 7px;
  font-size: 12px; font-weight: 600; color: var(--ink); white-space: nowrap; cursor: pointer;
}
.sel-actions button:hover { background: rgba(0, 0, 0, 0.07); }
.sel-actions button img { height: 22px; width: auto; display: block; object-fit: contain; pointer-events: none; }
.sel-actions button.active { background: rgba(25, 113, 194, 0.16); color: var(--accent); }
.sel-actions button[data-act="erase"]:hover { background: rgba(224, 49, 49, 0.14); color: var(--danger); }
input[type=color] { width: 42px; height: 34px; border: 1px solid var(--panel-border); border-radius: 8px; background: #fff; padding: 2px; }
.chk { font-size: 12px; display: flex; align-items: center; gap: 6px; color: #555; }

/* --- Agenda -------------------------------------------------------------- */
.agenda-view { position: relative; flex: 1; min-height: 0; background: var(--paper);
  background-image: url("../../imgs/fondagenda.jpg"); overflow-y: auto; padding: 24px 28px 28px; }
.agenda-head { display: flex; align-items: center; justify-content: space-between; gap: 12px;
  max-width: 900px; margin: 0 auto 18px; }
.agenda-head h2 { margin: 0; }
#agendaTitle { font-size: 100px; line-height: 1.05; overflow-wrap: anywhere; }
/* En mode agenda, les réglages du crayon de la barre du haut sont inutiles */
.app.agenda-mode .inline-tools, .app.agenda-mode .tool-sliders, .app.agenda-mode .zoom-info { display: none; }

/* Grille de pages (grandes icônes avec le nom par-dessus) */
.agenda-grid {
  max-width: 900px; margin: 0 auto; display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 20px;
}
.page-tile {
  position: relative; cursor: pointer; display: flex; flex-direction: column; align-items: center;
  padding: 10px; border-radius: 12px; transition: transform .1s ease;
}
.page-tile:hover { transform: translateY(-3px); }
.page-tile-img { width: 100%; max-width: 130px; aspect-ratio: 3 / 4; object-fit: contain;
  display: block; filter: drop-shadow(0 4px 8px rgba(0,0,0,0.18)); pointer-events: none; }
.page-tile-name {
  position: absolute; left: 14px; right: 14px; top: 50%; transform: translateY(-50%);
  text-align: center; font-weight: 700; color: #2b2b2b; background: rgba(255,255,255,0.78);
  border-radius: 6px; padding: 3px 5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.page-tile.add { justify-content: center; }
.page-tile.add .page-tile-img { opacity: 0.9; }
.page-tile.add .page-tile-name {
  position: static; transform: none; background: none; color: #555; margin-top: 6px; white-space: normal;
}
.page-tile-del {
  position: absolute; top: 4px; right: 4px; border: none; background: rgba(255,255,255,0.9);
  border-radius: 50%; width: 30px; height: 30px; display: grid; place-items: center;
  opacity: 0; transition: opacity .1s; box-shadow: 0 1px 4px rgba(0,0,0,0.2);
}
.page-tile:hover .page-tile-del { opacity: 1; }
.page-tile-del img { width: 18px; height: 18px; object-fit: contain; }

/* --- Toast --------------------------------------------------------------- */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: #333; color: #fff; padding: 10px 18px; border-radius: 10px; z-index: 100;
  box-shadow: var(--shadow); font-size: 14px; max-width: 90vw;
}

/* --- Mobile -------------------------------------------------------------- */
@media (max-width: 760px) {
  .sidebar {
    position: fixed; inset: 0 auto 0 0; transform: translateX(-100%);
    transition: transform .2s ease; box-shadow: var(--shadow);
  }
  .app.menu-open .sidebar { transform: translateX(0); }
  .sidebar-head #sidebarClose { display: grid; }
  .topbar #menuBtn { display: grid; }
  .panel { width: 200px; top: 8px; right: 8px; padding: 10px; }
  .toolbar { left: 8px; }
  .save-status { display: none; }
  .save-btn { padding: 5px 8px; }

  /* Barre du haut sur plusieurs lignes : sliders côte à côte sur leur propre ligne */
  .topbar { height: auto; min-height: 48px; flex-wrap: wrap; row-gap: 6px; padding: 6px 8px; }
  .title-spacer { display: none; }
  .topbar .spacer, .zoom-info, .sync-dot { order: 1; }
  .tool-sliders { order: 2; flex-basis: 100%; gap: 10px; }
  .tool-sliders .t-slider { flex: 1; min-width: 0; }
  .tool-sliders .t-slider input[type=range] { width: 100%; }
  .inline-tools { order: 3; flex-basis: 100%; }
}
