:root{
  /* Light theme (default) – inspired by HDJ Planner */
  --bg:#f4f7fb;
  --surface:#ffffff;
  --surface2:#f8fafc;
  --text:#0f172a;
  --muted:#64748b;
  --line:rgba(15,23,42,.10);
  --line2:rgba(15,23,42,.14);
  --shadow:0 12px 30px rgba(15,23,42,.10);
  --radius:18px;

  --primary:#0b63ce;
  --primary2:#0ea5e9;
  --good:#16a34a;
  --bad:#dc2626;

  --sans: ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,Arial;
  --mono: ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;
}

/* Optional dark mode (set <html data-theme="dark">) */
html[data-theme="dark"]{
  --bg:#0b1220;
  --surface:rgba(255,255,255,.06);
  --surface2:rgba(255,255,255,.08);
  --text:#e5e7eb;
  --muted:#9ca3af;
  --line:rgba(255,255,255,.12);
  --line2:rgba(255,255,255,.16);
  --shadow:0 18px 45px rgba(0,0,0,.35);
  --primary:#1f6feb;
  --primary2:#0ea5e9;
  --good:#22c55e;
  --bad:#ef4444;
}

*{box-sizing:border-box}
html,body{height:100%}
body{
  margin:0;
  font-family:var(--sans);
  background: var(--bg);
  color:var(--text);
}
a{color:inherit}
.mono{font-family:var(--mono)}
.t-right{text-align:right}
.t-center{text-align:center}

/* Layout */
.app{
  display:grid;
  grid-template-columns: 340px 1fr;
  gap:16px;
  padding:16px;
  height:100%;
}
@media (max-width: 1100px){
  .app{grid-template-columns: 1fr; height:auto}
  .two{grid-template-columns: 1fr}
}

/* Sidebar */
.sidebar{
  background: var(--surface);
  border:1px solid var(--line);
  border-radius:var(--radius);
  box-shadow:var(--shadow);
  overflow:hidden;
  display:flex;
  flex-direction:column;
  min-width: 280px;
}
.side-top{
  padding:14px 14px 10px;
  border-bottom:1px solid var(--line);
  background: linear-gradient(180deg, rgba(14,165,233,.10), transparent 55%);
}
.brand{display:flex; align-items:center; gap:12px;}
.logo{
  width:48px; height:48px;
  border-radius:14px;
  background: rgba(11,99,206,.12);
  border:1px solid rgba(11,99,206,.22);
  display:grid; place-items:center;
  overflow:hidden;
}
html[data-theme="dark"] .logo{
  background: rgba(78,161,255,.18);
  border:1px solid rgba(78,161,255,.35);
}
.logo img{width:100%; height:100%; object-fit:cover}
.logo-fallback{font-weight:900;color: rgba(11,99,206,.85)}
html[data-theme="dark"] .logo-fallback{color: rgba(78,161,255,.8)}
.brand h1{font-size:14px; margin:0; line-height:1.2}
.brand p{margin:2px 0 0; font-size:12px; color:var(--muted)}

.side-actions{display:flex; gap:8px; margin-top:12px; flex-wrap:wrap}
.search{
  margin-top:12px;
  display:flex;
  gap:8px;
  flex-wrap:wrap;
}
.search input{
  flex:1;
  min-width: 160px;
  padding:10px 12px;
  border-radius:12px;
  border:1px solid var(--line);
  background: var(--surface2);
  color:var(--text);
  outline:none;
}
html[data-theme="dark"] .search input{background: rgba(0,0,0,.18)}
.search input::placeholder{color: rgba(100,116,139,.85)}
html[data-theme="dark"] .search input::placeholder{color: rgba(138,160,191,.7)}

.hint{
  color: var(--muted);
  font-size:12px;
  margin-top:10px;
  line-height:1.35;
}

/* Doc list cards */
.doc-list{padding:10px; overflow:auto;}
.doc-card{
  padding:12px;
  border:1px solid var(--line);
  border-radius:14px;
  background: var(--surface2);
  margin-bottom:10px;
  cursor:pointer;
  transition: border-color .15s ease, transform .06s ease, box-shadow .15s ease;
}
html[data-theme="dark"] .doc-card{background: rgba(0,0,0,.18)}
.doc-card:hover{border-color: rgba(11,99,206,.25); box-shadow:0 8px 18px rgba(15,23,42,.08)}
.doc-card:active{transform: translateY(1px)}
.doc-card.active{border-color: rgba(22,163,74,.35); box-shadow: 0 0 0 1px rgba(22,163,74,.12) inset}
.doc-topline{display:flex; align-items:center; justify-content:space-between; gap:10px;}
.chip{
  font-size:11px;
  padding:4px 8px;
  border-radius:999px;
  border:1px solid var(--line);
  color: var(--muted);
  background: rgba(2,6,23,.03);
  white-space:nowrap;
}
html[data-theme="dark"] .chip{background: rgba(255,255,255,.04)}
.doc-title{font-weight:800;font-size:13px;}
.doc-sub{
  margin-top:6px;
  display:flex; align-items:center; justify-content:space-between;
  color:var(--muted);
  font-size:12px;
  gap:10px;
}

/* Main */
.main{
  background: var(--surface);
  border:1px solid var(--line);
  border-radius:var(--radius);
  box-shadow:var(--shadow);
  overflow:hidden;
  display:flex;
  flex-direction:column;
  min-height: 0;
}

/* Main header (HDJ-like top bar) */
.main-header{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:12px;
  padding:14px 16px;
  background: linear-gradient(90deg, rgba(11,99,206,.14), rgba(14,165,233,.08));
  border-bottom:1px solid var(--line);
}
html[data-theme="dark"] .main-header{
  background: linear-gradient(90deg, rgba(78,161,255,.18), rgba(52,211,153,.10));
  border-bottom:1px solid rgba(255,255,255,.10);
}
.mh-title{display:flex; flex-direction:column; gap:2px}
.mh-app{font-weight:900; letter-spacing:.2px}
.mh-sub{font-size:12px; color: var(--muted)}
.mh-right{display:flex; gap:8px; flex-wrap:wrap}

/* Tabs (sections) */
.top-tabs{
  display:flex;
  gap:8px;
  padding:10px 14px;
  position:sticky;
  top:0;
  z-index:5;
  background: var(--surface);
  border-bottom:1px solid var(--line);
}
html[data-theme="dark"] .top-tabs{
  background:linear-gradient(to bottom, rgba(11,18,32,.95), rgba(11,18,32,.75));
  border-bottom:1px solid rgba(255,255,255,.08);
  backdrop-filter: blur(10px);
}
.tab-btn{
  border:1px solid var(--line2);
  background: rgba(2,6,23,.03);
  color:var(--text);
  padding:8px 12px;
  border-radius:999px;
  cursor:pointer;
  font-weight:800;
}
html[data-theme="dark"] .tab-btn{
  border:1px solid rgba(255,255,255,.12);
  background:rgba(255,255,255,.06);
}
.tab-btn.active{
  background: rgba(11,99,206,.14);
  border-color: rgba(11,99,206,.28);
}
html[data-theme="dark"] .tab-btn.active{
  background:rgba(124,92,255,.22);
  border-color:rgba(124,92,255,.45);
}
.page[hidden]{ display:none !important; }

/* Document meta + actions */
.main-top{
  padding:14px;
  border-bottom:1px solid var(--line);
  display:flex;
  align-items:flex-end;
  justify-content:space-between;
  gap:12px;
  flex-wrap:wrap;
  background: linear-gradient(180deg, rgba(2,6,23,.02), transparent);
}
html[data-theme="dark"] .main-top{
  background: transparent;
  border-bottom:1px solid rgba(255,255,255,.10);
}
.main-top .left{display:flex; gap:10px; align-items:center; flex-wrap:wrap;}
.actions{display:flex; gap:8px; flex-wrap:wrap}

/* Segmented control */
.seg{
  display:flex;
  border:1px solid var(--line);
  background: rgba(2,6,23,.03);
  border-radius:12px;
  overflow:hidden;
}
html[data-theme="dark"] .seg{background: rgba(0,0,0,.18); border:1px solid rgba(255,255,255,.12)}
.seg button{
  padding:10px 12px;
  border:0;
  background: transparent;
  color: var(--muted);
  font-weight:900;
  cursor:pointer;
}
.seg button.active{
  color: var(--text);
  background: rgba(11,99,206,.14);
}
html[data-theme="dark"] .seg button.active{background: rgba(78,161,255,.16)}

/* Fields */
.meta{display:flex; gap:10px; flex-wrap:wrap;}
.field{display:flex; flex-direction:column; gap:6px; min-width: 160px;}
.field label{font-size:12px; color: var(--muted); font-weight:700;}
.field input, .field textarea, .select{
  padding:10px 12px;
  border-radius:12px;
  border:1px solid var(--line);
  background: var(--surface2);
  color: var(--text);
  outline:none;
}
html[data-theme="dark"] .field input,
html[data-theme="dark"] .field textarea,
html[data-theme="dark"] .select{
  background: rgba(0,0,0,.18);
  border:1px solid rgba(255,255,255,.12);
}
.field textarea{min-height:92px; resize:vertical}
.subfield{margin-top:10px}
.select{width:100%}

/* Content grid */
.grid{padding:14px; overflow:auto; min-height:0;}
.two{display:grid;grid-template-columns: 1fr 1fr;gap:12px;}

/* Cards */
.card{
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding:12px;
  box-shadow: 0 8px 20px rgba(15,23,42,.06);
}
html[data-theme="dark"] .card{
  background: rgba(0,0,0,.18);
  border: 1px solid rgba(255,255,255,.12);
  box-shadow:none;
}
.card h2{margin:0 0 10px 0;font-size:14px;letter-spacing:.2px;}
.card-row{display:flex; align-items:center; justify-content:space-between; gap:10px; flex-wrap:wrap}
.row-buttons{display:flex; gap:8px; flex-wrap:wrap}
.logo-row{display:flex; gap:8px; align-items:center; flex-wrap:wrap}

/* Table */
table{
  width:100%;
  border-collapse:separate;
  border-spacing:0;
  overflow:hidden;
  border-radius:16px;
  border:1px solid var(--line);
  background: var(--surface);
}
html[data-theme="dark"] table{background: rgba(0,0,0,.14); border:1px solid rgba(255,255,255,.12)}
thead th{
  text-align:left;
  padding:10px 10px;
  font-size:12px;
  color: var(--muted);
  border-bottom:1px solid var(--line);
  background: rgba(2,6,23,.03);
}
html[data-theme="dark"] thead th{background: rgba(255,255,255,.04); border-bottom:1px solid rgba(255,255,255,.12)}
tbody td{
  padding:8px 10px;
  border-bottom:1px solid rgba(15,23,42,.06);
  vertical-align:top;
}
html[data-theme="dark"] tbody td{border-bottom:1px solid rgba(255,255,255,.06)}
tbody tr:last-child td{border-bottom:0}
.t-input{
  width:100%;
  padding:10px 10px;
  border-radius:12px;
  border:1px solid var(--line);
  background: var(--surface2);
  color: var(--text);
  outline:none;
}
html[data-theme="dark"] .t-input{
  border:1px solid rgba(255,255,255,.10);
  background: rgba(0,0,0,.18);
}

/* Totals */
.totals{
  display:grid;
  grid-template-columns: 1fr 260px;
  gap:12px;
  align-items:start;
}
@media (max-width: 900px){
  .totals{grid-template-columns: 1fr}
}
.sum{
  border:1px solid var(--line);
  border-radius:16px;
  padding:12px;
  background: var(--surface);
}
html[data-theme="dark"] .sum{background: rgba(0,0,0,.18); border:1px solid rgba(255,255,255,.12)}
.sum .line{
  display:flex;
  justify-content:space-between;
  align-items:center;
  padding:8px 0;
  border-bottom:1px dashed rgba(15,23,42,.14);
  gap:12px;
}
html[data-theme="dark"] .sum .line{border-bottom:1px dashed rgba(255,255,255,.10)}
.sum .line:last-child{border-bottom:0}
.sum .k{color:var(--muted); font-size:12px; font-weight:700}
.sum .v{font-weight:900}
.sum .big .v{font-size:18px; color: var(--primary2)}

/* Toolbar */
.toolbar{
  display:flex; gap:8px; flex-wrap:wrap;
  justify-content:flex-end;
  padding:14px;
  border-top:1px solid var(--line);
  background: rgba(2,6,23,.02);
}
html[data-theme="dark"] .toolbar{background: rgba(0,0,0,.12); border-top:1px solid rgba(255,255,255,.10)}

.file-input{display:none}

/* Buttons */
.btn{
  appearance:none;
  border:1px solid var(--line2);
  background: rgba(2,6,23,.03);
  color:var(--text);
  padding:10px 12px;
  border-radius:999px;
  font-weight:800;
  cursor:pointer;
  transition: transform .06s ease, background .15s ease, border-color .15s ease, box-shadow .15s ease;
  display:inline-flex;
  align-items:center;
  justify-content:center;
  gap:8px;
  user-select:none;
}
html[data-theme="dark"] .btn{
  border:1px solid rgba(255,255,255,.12);
  background: rgba(255,255,255,.04);
}
.btn:hover{background: rgba(11,99,206,.10); border-color: rgba(11,99,206,.20); box-shadow:0 8px 18px rgba(15,23,42,.08)}
html[data-theme="dark"] .btn:hover{background: rgba(255,255,255,.06); border-color: rgba(78,161,255,.35); box-shadow:none}
.btn:active{transform: translateY(1px)}
.btn.primary{background: rgba(11,99,206,.14); border-color: rgba(11,99,206,.24)}
.btn.good{background: rgba(22,163,74,.12); border-color: rgba(22,163,74,.22)}
.btn.danger{background: rgba(220,38,38,.10); border-color: rgba(220,38,38,.20)}
.btn.small{padding:8px 12px; font-size:12px}

/* Toasts */
#toasts{position:fixed;right:16px;bottom:16px;display:flex;flex-direction:column;gap:10px;z-index:9999}
.toast{background:#111827;color:#fff;padding:10px 12px;border-radius:12px;box-shadow:0 10px 30px rgba(0,0,0,.25);max-width:320px;font-size:13px;display:flex;gap:10px;align-items:flex-start}
.toast.ok{background:#0f766e}
.toast.warn{background:#b45309}
.toast.err{background:#b91c1c}
.toast .t-title{font-weight:700;margin-bottom:2px}
.toast .t-msg{opacity:.95}

/* Prestations */
.t-textarea{resize:vertical; overflow:hidden; line-height:1.25;}
.t-select{padding-right:26px;}
.row-actions{display:flex; gap:6px; justify-content:flex-end;}

/* PRINT */
@media print{
  body{background:#fff; color:#111}
  .app{display:block; padding:0}
  .sidebar,.main-header,.main-top,.toolbar,.hint,.no-print{display:none !important}
  .main{border:0; box-shadow:none}
  .grid{padding:0}
  .card{border:0; background:#fff; box-shadow:none}
  table{border:1px solid #ccc; background:#fff}
  thead th{background:#f5f5f5; color:#222}
  .sum{border:1px solid #ccc; background:#fff}
  .t-input{border:0; padding:0; background:transparent; color:#111}
  .logo{border:1px solid #ddd; background:#fff}
}


/* =========================================================
   DEFINITIVE SCROLL FIX (validated)
   Structure:
   .app (grid, full viewport)
     .main (flex column, fixed header)
       .page (flex column)
         .grid (SCROLL CONTAINER)
   ========================================================= */

html, body {
  height: 100%;
}

.app{
  height: 100vh;
}

/* main container */
.main{
  min-height: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* each tab page */
.main > .page{
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* scrollable content */
.page > .grid{
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
}

/* optional: nicer scrollbar (webkit) */
.page > .grid::-webkit-scrollbar{
  width: 10px;
}
.page > .grid::-webkit-scrollbar-track{
  background: transparent;
}
.page > .grid::-webkit-scrollbar-thumb{
  background: rgba(15,23,42,.25);
  border-radius: 999px;
}



/* =========================================================
   UI FIX – prevent "letter-by-letter" wrapping in narrow cells
   (ex: "Accès au logiciel..." shown vertically)
   ========================================================= */

/* Never break words into individual letters */
.t-input, .t-textarea, textarea, input, select, button, .chip, .doc-title, .doc-sub, .btn{
  word-break: normal;
  overflow-wrap: break-word;
  hyphens: auto;
}

/* Give form controls a sensible minimum width so columns don't collapse */
.t-input, .t-textarea, select, input[type="text"], input[type="number"], input[type="date"]{
  min-width: 120px;
}

/* In prestation tables, keep the "Détails" cell wide enough */
table td:nth-child(1), table th:nth-child(1){
  min-width: 220px;
}
/* Quantity / small numeric columns can stay compact */
table td:nth-child(2), table th:nth-child(2){
  min-width: 70px;
}



/* =========================================================
   SIDEBAR → DRAWER (optional)
   Left panel is often not needed; keep it accessible via Menu
   ========================================================= */

.app{
  /* keep the main layout wide: only main by default */
  grid-template-columns: 1fr;
  height: 100vh;
}

/* Make sidebar a drawer */
.sidebar{
  position: fixed;
  top: 16px;
  left: 16px;
  bottom: 16px;
  width: min(360px, calc(100vw - 32px));
  z-index: 50;
  transform: translateX(calc(-100% - 24px));
  transition: transform .18s ease;
}

/* Hide the drawer toggle checkbox */
.nav-toggle{
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

/* Overlay to close */
.nav-overlay{
  position: fixed;
  inset: 0;
  background: rgba(15,23,42,.35);
  backdrop-filter: blur(2px);
  z-index: 40;
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s ease;
}

/* Open state */
.nav-toggle:checked ~ .sidebar{ transform: translateX(0); }
.nav-toggle:checked ~ .nav-overlay{ opacity: 1; pointer-events: auto; }

/* Button style */
.btn.ghost{
  background: rgba(255,255,255,.55);
  border-color: rgba(15,23,42,.12);
}
html[data-theme="dark"] .btn.ghost{
  background: rgba(255,255,255,.10);
  border-color: rgba(255,255,255,.16);
}

/* On large screens, allow an option to keep sidebar visible by adding class .with-sidebar on body */
body.with-sidebar .app{ grid-template-columns: 340px 1fr; }
body.with-sidebar .sidebar{
  position: relative;
  top: auto; left: auto; bottom: auto;
  transform: none;
  width: auto;
  z-index: auto;
}
body.with-sidebar .nav-overlay{ display:none; }
body.with-sidebar .btn.ghost{ display:none; }



/* =========================================================
   MEDILINK TECH – Logo-inspired polish
   ========================================================= */

body{
  background:
    radial-gradient(1200px 520px at 78% 0%, rgba(14,165,233,.16), transparent 60%),
    radial-gradient(900px 420px at 6% 12%, rgba(11,99,206,.14), transparent 60%),
    var(--bg);
}

/* Global top banner */
.global-bar{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:16px;
  padding:14px 18px;
  border-bottom:1px solid var(--line);
  background:
    radial-gradient(520px 220px at 15% 30%, rgba(14,165,233,.18), transparent 60%),
    linear-gradient(90deg, rgba(11,99,206,.14), rgba(14,165,233,.06));
  position:sticky;
  top:0;
  z-index:10;
}
html[data-theme="dark"] .global-bar{
  border-bottom:1px solid rgba(255,255,255,.10);
  background:
    radial-gradient(520px 220px at 15% 30%, rgba(78,161,255,.18), transparent 60%),
    linear-gradient(90deg, rgba(78,161,255,.16), rgba(52,211,153,.07));
  backdrop-filter: blur(10px);
}

.gb-left{display:flex; align-items:center; gap:12px; min-width: 280px}
.gb-title{display:flex; flex-direction:column; gap:2px}
.gb-name{font-weight:950; letter-spacing:.6px}
.gb-sub{font-size:12px; color: var(--muted); max-width: 60vw; white-space:nowrap; overflow:hidden; text-overflow:ellipsis}
.gb-right{display:flex; gap:8px; flex-wrap:wrap; justify-content:flex-end}

/* Logo block blended into banner */
.gb-logo{
  width:44px; height:44px;
  border-radius:14px;
  position:relative;
  overflow:hidden;
  border:1px solid rgba(15,23,42,.10);
  background: rgba(255,255,255,.55);
}
html[data-theme="dark"] .gb-logo{
  border:1px solid rgba(255,255,255,.14);
  background: rgba(255,255,255,.08);
}
.gb-logo img{
  width:100%; height:100%;
  object-fit:cover;
  opacity:.72;
  filter: saturate(1.05) contrast(1.05);
  mix-blend-mode:multiply;
}
html[data-theme="dark"] .gb-logo img{
  opacity:.85;
  mix-blend-mode:screen;
}
.gb-logo::after{
  content:"";
  position:absolute; inset:-40%;
  background: radial-gradient(circle at 30% 30%, rgba(255,255,255,.8), transparent 45%);
  opacity:.55;
  transform: rotate(25deg);
}

/* Make tabs sit under the global bar */
.top-tabs{
  top: 72px; /* height of global bar */
}

/* Drawer overlay should cover under the global bar too */
.nav-overlay{ top:0; }

/* Table: keep the Détails column comfortable (it's the 2nd column) */
table td:nth-child(2), table th:nth-child(2){
  min-width: 260px;
}
/* Designation can be moderately wide */
table td:nth-child(1), table th:nth-child(1){
  min-width: 220px;
}
/* Avoid ultra-narrow inputs in the table */
#itemsBody .t-input, #itemsBody .t-textarea, #itemsBody select{
  min-width: 120px;
}

/* Slightly nicer "primary" / "good" emphasis to match logo */
.btn.good{
  background: linear-gradient(90deg, rgba(22,163,74,.18), rgba(14,165,233,.10));
  border-color: rgba(22,163,74,.22);
}
.btn.primary{
  background: linear-gradient(90deg, rgba(11,99,206,.16), rgba(14,165,233,.10));
  border-color: rgba(11,99,206,.24);
}

/* Keep main content full width by default (drawer mode) */
.app{
  grid-template-columns: 1fr;
}

.gb-badges{display:flex;gap:8px;margin-left:12px;align-items:center;flex-wrap:wrap}
.badge{display:inline-flex;align-items:center;padding:6px 10px;border-radius:999px;font-size:12px;font-weight:800;border:1px solid rgba(15,23,42,.12);background:rgba(255,255,255,.55)}
.badge.ok{border-color:rgba(22,163,74,.22);background:rgba(22,163,74,.10)}
.badge.warn{border-color:rgba(220,38,38,.22);background:rgba(220,38,38,.08)}


/* Logo saved hint */
.logo-hint{
  margin-top:8px;
  font-size:12px;
  font-weight:800;
  color: var(--muted);
}
.logo-hint.ok{ color: var(--good); }
.logo-hint.warn{ color: var(--bad); }


/* Separators & simple checkbox rows */
.sep{
  border: 0;
  border-top: 1px solid rgba(15,23,42,.10);
  margin: 14px 0;
}
.checkbox{
  display:flex;
  gap:10px;
  align-items:center;
  font-weight:800;
}
.checkbox input{
  width: 18px;
  height: 18px;
}


/* Theme smoothness */
html, body{ transition: background-color .2s ease, color .2s ease; }
