:root {
  --bg: #f4f1ec;
  --card: #ffffff;
  --ink: #1d1b18;
  --muted: #6b665e;
  --line: #ddd7cd;
  --primary: #0f5c55;
  --primary-ink: #ffffff;
  --primary-soft: #e3efed;
  --busy: #fbe3a6;
  --busy-line: #c98a06;
  --draft: #e8f0ff;
  --draft-line: #3b6fd8;
  --done: #237a3b;
  --cancel: #6b665e;
  --loss: #b42318;
  --radius: 14px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overscroll-behavior: none; }
body { background: var(--bg); }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; -webkit-tap-highlight-color: transparent; touch-action: manipulation; }

#app { height: 100dvh; display: flex; flex-direction: column; }
.loading { margin: auto; color: var(--muted); }

/* --- Barre du haut ------------------------------------------------------- */
.topbar {
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top, 0px)) 14px 10px;
  background: var(--primary); color: var(--primary-ink);
  flex: none;
}
.topbar h1 { font-size: 19px; margin: 0; flex: 1; line-height: 1.15; }
.topbar h1 small { display: block; font-size: 12.5px; font-weight: 400; opacity: .85; }
.topbar .icon-btn { background: rgba(255,255,255,.14); border: 0; color: inherit; border-radius: 10px; min-width: 44px; height: 40px; padding: 0 12px; font-size: 15px; }
.topbar .who { font-size: 13px; opacity: .9; }

.tabs { display: flex; gap: 4px; padding: 8px 10px; background: var(--card); border-bottom: 1px solid var(--line); overflow-x: auto; flex: none; }
.tabs a { padding: 8px 12px; border-radius: 10px; color: var(--muted); text-decoration: none; font-weight: 600; font-size: 15px; white-space: nowrap; }
.tabs a.active { background: var(--primary-soft); color: var(--primary); }

.main { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 14px 14px calc(24px + var(--safe-bottom)); }
.main.with-ticket { padding-bottom: 16px; }

h2 { font-size: 15px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 22px 2px 10px; }
h2:first-child { margin-top: 4px; }

/* --- Boutons et formulaires ---------------------------------------------- */
.btn { border: 1px solid var(--line); background: var(--card); border-radius: 12px; min-height: 46px; padding: 0 16px; font-weight: 600; font-size: 16px; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.done { background: var(--done); border-color: var(--done); color: #fff; }
.btn.danger { color: var(--loss); border-color: #e9b8b3; }
.btn.danger.solid { background: var(--loss); color: #fff; border-color: var(--loss); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; }
.btn.small { min-height: 36px; padding: 0 12px; font-size: 14px; border-radius: 10px; }
.link-btn { background: none; border: 0; color: var(--primary); font-weight: 600; padding: 6px; }

.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-size: 13.5px; font-weight: 600; color: var(--muted); margin-bottom: 6px; }
.input, select.input { width: 100%; height: 48px; border: 1px solid var(--line); border-radius: 12px; padding: 0 14px; background: #fff; font-size: 16px; }
.input:focus { outline: 2px solid var(--primary); outline-offset: 0; border-color: transparent; }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; min-width: 0; }
.check { display: flex; align-items: center; gap: 10px; font-weight: 600; margin-bottom: 14px; min-height: 44px; }
.check input { width: 22px; height: 22px; accent-color: var(--primary); }
.seg { display: flex; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: #fff; }
.seg button { flex: 1; border: 0; background: none; min-height: 46px; font-weight: 600; }
.seg button.on { background: var(--primary); color: #fff; }

/* --- Connexion ------------------------------------------------------------ */
.login { margin: auto; width: 100%; max-width: 380px; padding: 24px; }
.login .logo { width: 72px; height: 72px; display: block; margin: 0 auto 14px; }
.login h1 { text-align: center; font-size: 24px; margin: 0 0 4px; }
.login p { text-align: center; color: var(--muted); margin: 0 0 24px; }
.error-text { color: var(--loss); font-weight: 600; margin: -4px 0 12px; min-height: 1.2em; }

/* --- Plan de salle -------------------------------------------------------- */
.plan {
  position: relative; width: 100%; aspect-ratio: 100 / 140; margin: 0 auto;
  background: #fbfaf7;
  background-image: linear-gradient(var(--line) 1px, transparent 1px), linear-gradient(90deg, var(--line) 1px, transparent 1px);
  background-position: -1px -1px;
  border: 1px solid var(--line); border-radius: var(--radius);
  container-type: inline-size; overflow: hidden; user-select: none; -webkit-user-select: none;
}
.plan.service { background-image: none; }
.spot {
  position: absolute; display: flex; flex-direction: column; align-items: center; justify-content: center;
  border: 2px solid #b7afa2; background: #fff; border-radius: 16%; padding: 0; text-align: center; line-height: 1.05;
  -webkit-tap-highlight-color: transparent; overflow: hidden;
}
.spot.round { border-radius: 50%; }
.spot.rect { border-radius: calc(2.4cqw * 100 / var(--plan-w, 100)); }
/* Tailles exprimées en unités du plan : même rendu en vertical (100 de large) et en horizontal (140). */
.spot .num { font-size: calc(6.4cqw * 100 / var(--plan-w, 100)); font-weight: 700; }
.spot .name { font-size: max(10px, calc(3.3cqw * 100 / var(--plan-w, 100))); font-weight: 700; }
.spot .meta { font-size: max(9px, calc(2.8cqw * 100 / var(--plan-w, 100))); font-weight: 600; margin-top: .4cqw; }
.spot .age { font-size: max(8.5px, calc(2.5cqw * 100 / var(--plan-w, 100))); color: var(--muted); }
.spot.table.busy { background: var(--busy); border-color: var(--busy-line); }
.spot.marker { border-style: dashed; background: transparent; color: var(--muted); border-color: #c8c0b3; }
.spot.marker .name { font-weight: 600; }
.spot.tall .name { writing-mode: vertical-rl; }
button.spot:active { transform: scale(.97); }
.plan.edit .spot { touch-action: none; cursor: grab; }
.plan.edit .spot.selected { outline: 3px solid var(--draft-line); outline-offset: 2px; z-index: 2; }
.plan.edit .spot.dragging { cursor: grabbing; opacity: .85; z-index: 3; }
.legend { display: flex; gap: 16px; font-size: 13px; color: var(--muted); margin: 10px 4px 0; }
.help-text { font-size: 13px; color: var(--muted); margin: 10px 4px 0; }
.legend i { display: inline-block; width: 12px; height: 12px; border-radius: 50%; border: 2px solid #b7afa2; vertical-align: -1px; margin-right: 6px; background: #fff; }
.legend i.busy { background: var(--busy); border-color: var(--busy-line); }

/* --- Listes / cartes ---------------------------------------------------- */
.card-list { display: flex; flex-direction: column; gap: 8px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; text-align: left; width: 100%; display: block; }
button.card:active { background: #faf8f4; }
.card .line1 { display: flex; justify-content: space-between; gap: 10px; font-weight: 700; font-size: 16.5px; }
.card .line1 > span:last-child { white-space: nowrap; }
.card .line2 { color: var(--muted); font-size: 14px; margin-top: 3px; }
.light { font-weight: 500; }
.card.inactive { opacity: .55; }
.empty { color: var(--muted); text-align: center; padding: 18px 8px; }
.badge { display: inline-block; font-size: 12px; font-weight: 700; padding: 2px 8px; border-radius: 999px; background: var(--primary-soft); color: var(--primary); vertical-align: 1px; }
.badge.done { background: #e3f3e7; color: var(--done); }
.badge.cancelled { background: #ece9e4; color: var(--cancel); }
.badge.loss { background: #fbe4e1; color: var(--loss); }
.badge.open { background: var(--busy); color: #7a5200; }

/* --- Écran de commande ------------------------------------------------- */
.comment-input { margin-bottom: 4px; }
.cat-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 8px; }
.prod {
  position: relative; min-height: 72px; border-radius: 14px; border: 1px solid var(--line); background: var(--card);
  font-weight: 700; font-size: 15.5px; padding: 8px 6px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; line-height: 1.15;
}
.prod small { font-weight: 500; color: var(--muted); font-size: 12.5px; }
.prod:active { background: var(--draft); border-color: var(--draft-line); }
.prod .count { position: absolute; top: -7px; right: -5px; background: var(--draft-line); color: #fff; font-size: 13px; min-width: 24px; height: 24px; border-radius: 12px; display: grid; place-items: center; padding: 0 6px; }

.ticket {
  flex: none; background: var(--card); border-top: 1px solid var(--line);
  box-shadow: 0 -6px 18px rgba(30, 25, 15, .07);
  display: flex; flex-direction: column; max-height: 50dvh;
  padding-bottom: var(--safe-bottom);
}
.ticket-lines { overflow-y: auto; padding: 8px 14px 0; }
.ticket-lines .section-label { font-size: 12.5px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin: 6px 0 2px; }
.tline { display: flex; align-items: center; gap: 10px; padding: 6px 0; border-bottom: 1px solid #f0ece5; min-height: 44px; }
.tline:last-child { border-bottom: 0; }
.tline .qty { font-weight: 800; min-width: 30px; }
.tline .label { flex: 1; min-width: 0; }
.tline .label small { display: block; color: var(--muted); font-size: 12.5px; }
.tline .price { font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }
.tline.draft { background: var(--draft); margin: 0 -14px; padding-left: 14px; padding-right: 14px; border-bottom-color: #d6e2fb; }
.tline.removed { color: var(--muted); }
.tline.removed .label span { text-decoration: line-through; }
.stepper { display: flex; gap: 6px; }
.stepper button { width: 36px; height: 36px; border-radius: 10px; border: 1px solid var(--line); background: #fff; font-size: 20px; font-weight: 700; line-height: 1; padding: 0; }
.ticket-total { display: flex; justify-content: space-between; align-items: baseline; padding: 10px 14px 4px; font-weight: 800; font-size: 18px; border-top: 1px solid var(--line); }
.ticket-total small { font-weight: 500; color: var(--muted); font-size: 13px; }
.ticket-actions { display: flex; gap: 8px; padding: 8px 14px 12px; }
.hint { color: var(--muted); text-align: center; padding: 14px; font-size: 15px; }

/* --- Fenêtres modales ---------------------------------------------------- */
.overlay { position: fixed; inset: 0; background: rgba(20, 16, 10, .45); display: flex; align-items: flex-end; justify-content: center; z-index: 50; animation: fade .15s ease-out; }
.sheet { background: var(--bg); width: 100%; max-width: 560px; max-height: 92dvh; overflow-y: auto; border-radius: 20px 20px 0 0; padding: 18px 16px calc(18px + var(--safe-bottom)); animation: up .18s ease-out; }
.sheet h3 { margin: 0 0 6px; font-size: 20px; }
.sheet p { margin: 0 0 16px; color: var(--muted); }
.sheet .actions { display: flex; gap: 10px; margin-top: 8px; }
.sheet .actions > * { flex: 1; }
.menu-list { display: flex; flex-direction: column; gap: 8px; }
@media (min-width: 600px) { .overlay { align-items: center; } .sheet { border-radius: 20px; } }
@keyframes fade { from { opacity: 0; } }
@keyframes up { from { transform: translateY(30px); opacity: .6; } }

#toast {
  position: fixed; left: 50%; top: calc(14px + env(safe-area-inset-top, 0px)); transform: translate(-50%, -150%);
  background: #1d1b18; color: #fff; padding: 12px 18px; border-radius: 12px; font-weight: 600; z-index: 100;
  transition: transform .2s ease-out; max-width: calc(100% - 28px); text-align: center; pointer-events: none;
}
#toast.show { transform: translate(-50%, 0); }
#toast.error { background: var(--loss); }

/* --- Admin ---------------------------------------------------------------- */
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.editor-panel { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-top: 12px; }
.size-row { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.size-row > span { flex: 1; font-weight: 600; color: var(--muted); font-size: 14px; }
.size-row b { min-width: 34px; text-align: center; font-variant-numeric: tabular-nums; }
.filters { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 12px 0; margin-bottom: 12px; }
.filters .field { margin-bottom: 10px; }
.filters .input { height: 44px; }
.timeline { list-style: none; padding: 0; margin: 0 0 12px; }
.timeline li { display: flex; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.timeline li time { font-variant-numeric: tabular-nums; color: var(--muted); min-width: 44px; }
.timeline li.removed span.what { text-decoration: line-through; color: var(--muted); }
.timeline li .by { color: var(--muted); font-size: 13px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin-bottom: 14px; font-size: 15px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-weight: 600; }
.panel-kind { color: var(--muted); margin-bottom: 10px; font-size: 14px; }
.form-title { margin-bottom: 14px; }
.form-hint { font-size: 13px; margin: -6px 0 14px; }
.filter-buttons { margin-bottom: 12px; }
.grow { flex: 1; }

/* --- Grands écrans et écran tourné ------------------------------------------ */
/* Sur grand écran, les autres écrans gardent une colonne centrée de 900 px ; la salle prend tout l'écran. */
.main:not(.room) { padding-left: max(14px, calc((100% - 900px) / 2)); padding-right: max(14px, calc((100% - 900px) / 2)); }
.ticket { padding-left: max(0px, calc((100% - 928px) / 2)); padding-right: max(0px, calc((100% - 928px) / 2)); }

@media (orientation: landscape) and (min-width: 640px) {
  /* Plan à gauche, commandes ouvertes à droite (chaque colonne défile seule). */
  .main.room { display: grid; grid-template-columns: minmax(0, 1fr) minmax(240px, 320px); gap: 16px; overflow: hidden; }
  .room-plan { min-width: 0; }
  .room-orders { overflow-y: auto; min-height: 0; }
  .room-orders h2:first-child { margin-top: 4px; }
}

@media (orientation: landscape) and (max-height: 500px) {
  /* Téléphone tourné : barres réduites pour laisser toute la hauteur au plan. */
  .topbar { padding-top: max(6px, env(safe-area-inset-top, 0px)); padding-bottom: 6px; padding-left: max(14px, env(safe-area-inset-left, 0px)); padding-right: max(14px, env(safe-area-inset-right, 0px)); }
  .topbar h1 { font-size: 17px; }
  .topbar h1 small { display: none; }
  .topbar .icon-btn { height: 34px; }
  .tabs { padding: 4px 10px; }
  .main { padding: 8px max(14px, env(safe-area-inset-right, 0px)) 8px max(14px, env(safe-area-inset-left, 0px)); }
  .room-plan .legend { display: none; }
}
