:root {
  --navy: #0f2238;
  --navy-2: #17324f;
  --accent: #1f6fd1;
  --bg: #f2f4f7;
  --card: #fff;
  --ink: #17212c;
  --muted: #64707f;
  --line: #e1e6ec;
  --red: #c8303d;
  --red-bg: #fdebed;
  --amber: #a76200;
  --amber-bg: #fff3d9;
  --green: #1c7a45;
  --green-bg: #e3f5ea;
  --grey: #5d6b7c;
  --grey-bg: #edf0f4;
  --radius: 12px;
  --shadow: 0 1px 2px rgb(16 34 56 / 6%), 0 1px 1px rgb(16 34 56 / 4%);
  --tabbar-h: 64px;
  color-scheme: light;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: transparent; }
body {
  margin: 0;
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.5rem; margin: 0; line-height: 1.25; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }
h3 { font-size: 1rem; margin: 0 0 .5rem; }
p { margin: .4rem 0; }
small, .muted { color: var(--muted); }
.icon { width: 20px; height: 20px; flex: none; }

/* ---------- Shell (desktop) ---------- */
.shell { display: grid; grid-template-columns: 236px 1fr; min-height: 100vh; }
.topbar, .tabbar, .menu-backdrop { display: none; }
.menu-toggle { position: absolute; opacity: 0; pointer-events: none; width: 0; height: 0; }
.sidebar {
  background: var(--navy);
  color: #c9d6e6;
  padding: 1.25rem .9rem;
  position: sticky; top: 0; height: 100vh;
  display: flex; flex-direction: column; gap: 1.25rem;
}
.brand { color: #fff; font-weight: 700; font-size: 1.05rem; letter-spacing: .02em; padding: 0 .5rem; }
.brand small { display: block; color: #8fa6c1; font-weight: 500; font-size: .8rem; margin-top: .15rem; }
.nav { display: flex; flex-direction: column; gap: .15rem; }
.nav a { color: #c9d6e6; padding: .55rem .7rem; border-radius: 8px; display: flex; gap: .6rem; align-items: center; position: relative; }
.nav a:hover { background: var(--navy-2); text-decoration: none; color: #fff; }
.nav a.active { background: var(--accent); color: #fff; }
.nav .count { margin-left: auto; background: var(--red); color: #fff; border-radius: 99px; font-size: .72rem; padding: 0 .45rem; font-weight: 700; line-height: 1.5; }
.sidebar .logout { margin-top: auto; }
.sidebar .link-btn { color: #8fa6c1; display: flex; gap: .5rem; align-items: center; padding: .5rem .7rem; }

.main { padding: 1.5rem 2rem 3rem; min-width: 0; max-width: 1400px; }
.page-head { display: flex; flex-wrap: wrap; gap: .75rem; align-items: center; justify-content: space-between; margin-bottom: 1.25rem; }
.page-head .sub { color: var(--muted); margin-top: .2rem; }
.page-head h1 .badge { vertical-align: middle; font-size: .8rem; }
.actions { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }

/* ---------- Cards & grid ---------- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.1rem 1.2rem; box-shadow: var(--shadow); }
.card + .card, .stack > * + * { margin-top: 1rem; }
.grid > .card { margin-top: 0; }
.grid { display: grid; gap: 1rem; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.kpis { display: grid; gap: .75rem; grid-template-columns: repeat(4, 1fr); margin-bottom: 1.25rem; }
.kpi { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: .9rem 1rem; display: block; color: inherit; border-top: 4px solid var(--grey); box-shadow: var(--shadow); }
.kpi:hover { text-decoration: none; border-color: var(--accent); }
.kpi .n { font-size: 1.9rem; font-weight: 700; line-height: 1.1; }
.kpi .l { color: var(--muted); font-size: .85rem; }
.kpi.overdue { border-top-color: var(--red); } .kpi.overdue .n { color: var(--red); }
.kpi.soon { border-top-color: var(--amber); } .kpi.soon .n { color: var(--amber); }
.kpi.ok { border-top-color: var(--green); } .kpi.ok .n { color: var(--green); }

dl.props { display: grid; grid-template-columns: max-content 1fr; gap: .45rem 1rem; margin: 0; }
dl.props dt { color: var(--muted); }
dl.props dd { margin: 0; min-width: 0; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
table { border-collapse: collapse; width: 100%; }
th, td { text-align: left; padding: .65rem .75rem; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: .75rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); background: #f8fafc; white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
tr.inactive td { color: var(--muted); }
td.num, th.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
tr.group td { background: #eef3f9; font-weight: 600; color: var(--navy-2); }
.cell-title { font-weight: 600; }
.cell-sub { color: var(--muted); font-size: .85rem; }
.card table td, .card table th { padding-left: .25rem; padding-right: .25rem; }

/* ---------- Badges & progress ---------- */
.badge { display: inline-block; padding: .12rem .55rem; border-radius: 99px; font-size: .76rem; font-weight: 600; white-space: nowrap; }
.badge.overdue, .badge.issue { background: var(--red-bg); color: var(--red); }
.badge.soon { background: var(--amber-bg); color: var(--amber); }
.badge.ok, .badge.done { background: var(--green-bg); color: var(--green); }
.badge.unknown, .badge.decommissioned, .badge.neutral { background: var(--grey-bg); color: var(--grey); }
.progress { height: 5px; background: var(--grey-bg); border-radius: 99px; overflow: hidden; margin-top: .4rem; min-width: 90px; max-width: 220px; }
.progress > span { display: block; height: 100%; background: var(--green); }
.progress.soon > span { background: var(--amber); }
.progress.overdue > span { background: var(--red); }
.due { font-size: .88rem; white-space: nowrap; }
.due.overdue { color: var(--red); font-weight: 600; }

/* ---------- Forms ---------- */
form.inline { display: inline; }
label { display: block; font-weight: 600; font-size: .88rem; margin-bottom: .3rem; }
label .hint { font-weight: 400; color: var(--muted); }
input, select, textarea {
  width: 100%; font: inherit; color: inherit; padding: .55rem .7rem; border: 1px solid #c9d2dd; border-radius: 9px; background: #fff; min-height: 40px;
}
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 30%, transparent); border-color: var(--accent); }
textarea { min-height: 84px; resize: vertical; }
input[type="checkbox"], input[type="radio"] { width: auto; min-height: 0; accent-color: var(--accent); }
.check { display: flex; gap: .55rem; align-items: center; font-weight: 400; padding: .2rem 0; }
.fields { display: grid; gap: .9rem 1rem; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.fields .full { grid-column: 1 / -1; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: .8rem 1rem 1rem; margin: 0; min-width: 0; }
legend { font-weight: 700; padding: 0 .35rem; }
.form-foot { display: flex; gap: .5rem; justify-content: flex-end; margin-top: 1.1rem; flex-wrap: wrap; }
.toolbar { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; margin-bottom: 1rem; }
.toolbar > * { width: auto; }
.toolbar input[type="search"] { min-width: 220px; flex: 1; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .35rem; font: inherit; font-weight: 600; font-size: .9rem;
  padding: .5rem .95rem; min-height: 40px; border-radius: 9px; border: 1px solid #c9d2dd; background: #fff; color: var(--ink); cursor: pointer; white-space: nowrap;
}
.btn:hover { text-decoration: none; border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.success { background: var(--green); border-color: var(--green); color: #fff; }
.btn.danger { color: var(--red); border-color: #efc2c6; }
.btn.danger:hover { background: var(--red-bg); }
.btn.sm { padding: .25rem .7rem; min-height: 32px; font-size: .82rem; }
.link-btn { background: none; border: 0; padding: 0; font: inherit; color: var(--accent); cursor: pointer; }
.icon-btn { background: none; border: 0; color: inherit; padding: .5rem; display: grid; place-items: center; cursor: pointer; border-radius: 8px; }

.flash { padding: .7rem 1rem; border-radius: 10px; margin-bottom: 1rem; background: var(--green-bg); color: var(--green); border: 1px solid #b9e3c9; }
.flash.error { background: var(--red-bg); color: var(--red); border-color: #efc2c6; }
.empty { text-align: center; color: var(--muted); padding: 2rem 1rem; }

details.panel { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); }
details.panel > summary { cursor: pointer; padding: .8rem 1.1rem; font-weight: 600; list-style: none; }
details.panel > summary::-webkit-details-marker { display: none; }
details.panel > summary::before { content: "▸ "; color: var(--muted); }
details.panel[open] > summary::before { content: "▾ "; }
details.panel > .body { padding: 0 1.1rem 1.1rem; }

/* ---------- Auth ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; background: linear-gradient(160deg, var(--navy), #0b4a7d); padding: 1rem; }
.auth .card { width: min(100%, 380px); }
.auth h1 { margin-bottom: .25rem; }
.auth form > * + * { margin-top: .9rem; }
.auth .btn { width: 100%; }

/* ======================================================================
   Mobile
   ====================================================================== */
@media (max-width: 760px) {
  body { font-size: 15px; }
  input, select, textarea { font-size: 16px; } /* évite le zoom auto des navigateurs mobiles */

  .shell { display: block; }

  /* Barre du haut : bouton menu + nom du bateau */
  .topbar {
    display: flex; align-items: center; gap: .35rem;
    position: sticky; top: 0; z-index: 20;
    background: var(--navy); color: #fff;
    padding: .45rem 1rem .45rem .35rem; padding-top: calc(.45rem + env(safe-area-inset-top));
  }
  .topbar-brand { color: #fff; font-weight: 700; font-size: 1.02rem; min-width: 0; display: flex; align-items: baseline; gap: .45rem; white-space: nowrap; }
  .topbar-brand span { font-weight: 500; font-size: .85rem; color: #9fb3cb; overflow: hidden; text-overflow: ellipsis; }
  .topbar-brand:hover { text-decoration: none; }
  .menu-btn { color: #fff; width: 44px; height: 44px; margin: 0; }
  .menu-btn .icon { width: 24px; height: 24px; }

  /* Menu latéral : tiroir qui glisse depuis la gauche */
  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; z-index: 40;
    width: min(82vw, 300px); height: 100%;
    padding: calc(1.25rem + env(safe-area-inset-top)) .9rem calc(1rem + env(safe-area-inset-bottom));
    transform: translateX(-105%); transition: transform .22s ease;
    box-shadow: 4px 0 24px rgb(0 0 0 / 25%); overflow-y: auto;
  }
  .nav a { padding: .8rem .8rem; font-size: 1rem; }
  .menu-backdrop {
    display: block; position: fixed; inset: 0; z-index: 30; background: rgb(8 18 30 / 45%);
    opacity: 0; pointer-events: none; transition: opacity .22s ease; margin: 0;
  }
  .menu-toggle:checked ~ .shell .sidebar { transform: none; }
  .menu-toggle:checked ~ .shell .menu-backdrop { opacity: 1; pointer-events: auto; }
  body:has(.menu-toggle:checked) { overflow: hidden; }

  /* Barre d'onglets en bas (raccourcis) */
  .tabbar {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
    padding-bottom: env(safe-area-inset-bottom); background: #fff; border-top: 1px solid var(--line);
    box-shadow: 0 -2px 12px rgb(16 34 56 / 8%);
  }
  .tabbar a {
    flex: 1 1 0; min-width: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: .2rem;
    height: var(--tabbar-h); padding: .5rem .1rem .45rem; position: relative;
    color: var(--muted); font-size: .66rem; font-weight: 600;
  }
  .tabbar a:hover { text-decoration: none; color: var(--ink); }
  .tabbar a.active { color: var(--accent); }
  .tabbar a.active .icon { stroke-width: 2.4; }
  .tabbar .icon { width: 23px; height: 23px; }
  .tabbar span:not(.count) { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .tabbar .count {
    position: absolute; top: 5px; left: calc(50% + 6px); background: var(--red); color: #fff;
    border-radius: 99px; font-size: .64rem; font-weight: 700; padding: 0 .35rem; line-height: 1.5;
  }

  .main { padding: 1rem .8rem calc(var(--tabbar-h) + 1.5rem + env(safe-area-inset-bottom)); }

  .page-head { margin-bottom: 1rem; gap: .7rem; }
  .page-head > div:first-child { width: 100%; }
  h1 { font-size: 1.3rem; }
  h2 { font-size: 1.05rem; }
  .actions { width: 100%; }
  .actions > .btn, .actions > form { flex: 1 1 auto; }
  .actions > form .btn { width: 100%; }

  .kpis { grid-template-columns: repeat(4, 1fr); gap: .45rem; margin-bottom: 1rem; }
  .kpi { padding: .55rem .5rem .6rem; text-align: center; border-radius: 10px; }
  .kpi .n { font-size: 1.45rem; }
  .kpi .l { font-size: .68rem; line-height: 1.2; }

  .card { padding: .95rem 1rem; }
  .grid-2 { grid-template-columns: 1fr; }
  .form-foot .btn { flex: 1 1 0; }

  .toolbar input[type="search"] { min-width: 0; flex: 1 1 100%; }
  .filters { display: grid; grid-template-columns: 1fr 1fr; }
  .filters > * { width: 100%; }
  .filters select[name="etat"], .filters input[type="search"] { grid-column: 1 / -1; }
  .filters input[type="search"] { order: -1; }
  .hours-form { display: grid; grid-template-columns: 1fr auto; }
  .hours-form > * { max-width: none !important; }
  .hours-form input[name="note"] { grid-column: 1 / -1; order: 1; }
  .hours-form label.check { order: 2; }
  .hours-form .btn { order: 3; }

  /* Fiche tâche : le formulaire de validation passe en premier */
  #valider { order: -1; }
  .validate-fields { grid-template-columns: 1fr 1fr; gap: .75rem; }
  .validate-fields > div:nth-child(n + 3) { grid-column: 1 / -1; }

  /* --- Tables → cartes --- */
  .table-wrap { overflow: visible; background: none; border: 0; box-shadow: none; border-radius: 0; }
  .table-wrap table, .table-wrap tbody { display: block; }
  .table-wrap thead { display: none; }
  .table-wrap tr {
    display: grid; grid-template-columns: 1fr 1fr; gap: .55rem .9rem;
    background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
    padding: .85rem .95rem; margin-bottom: .6rem; box-shadow: var(--shadow);
  }
  .table-wrap tr[hidden] { display: none; }
  .table-wrap td { display: block; border: 0; padding: 0; min-width: 0; }
  .table-wrap td.num { text-align: left; }
  .table-wrap td.full { grid-column: 1 / -1; }
  .table-wrap td:empty, .table-wrap .hide-m { display: none; }
  .table-wrap td[data-label]::before {
    content: attr(data-label); display: block; font-size: .68rem; font-weight: 600; text-transform: uppercase;
    letter-spacing: .04em; color: var(--muted); margin-bottom: .1rem;
  }
  .table-wrap tr.group {
    display: block; background: none; border: 0; box-shadow: none; padding: .9rem .2rem .35rem; margin: 0;
  }
  .table-wrap tr.group td { background: none; padding: 0; font-size: .78rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
  .table-wrap tr.group:first-child { padding-top: 0; }
  .table-wrap .empty { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); }
  .table-wrap input { max-width: none !important; }

  /* Carte de tâche */
  .task-list tr {
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "title state"
      "eq    eq"
      "due   act"
      "meta  meta";
    border-left: 4px solid var(--line);
    gap: .4rem .75rem;
  }
  .task-list tr.st-overdue { border-left-color: var(--red); }
  .task-list tr.st-soon { border-left-color: var(--amber); }
  .task-list tr.st-ok { border-left-color: var(--green); }
  .task-list .c-title { grid-area: title; }
  .task-list .c-title .cell-title { font-size: 1rem; line-height: 1.3; }
  .task-list .c-title .cell-sub { display: none; }
  .task-list .c-state { grid-area: state; }
  .task-list .c-eq { grid-area: eq; font-size: .88rem; margin-top: -.25rem; }
  .task-list .c-eq .cell-sub { display: inline; }
  .task-list .c-eq .cell-sub::before { content: " · "; }
  .task-list .c-due { grid-area: due; align-self: center; }
  .task-list .c-due .due { white-space: normal; font-size: .9rem; }
  .task-list .c-due .progress { max-width: none; }
  .task-list .c-act { grid-area: act; align-self: center; }
  .task-list .c-act .btn { min-height: 38px; padding: .35rem .85rem; }
  .task-list .c-interval, .task-list .c-last {
    grid-area: meta; font-size: .8rem; color: var(--muted); border-top: 1px solid var(--line); padding-top: .45rem;
  }
  .task-list .c-interval::before, .task-list .c-last::before { display: inline !important; text-transform: none !important; letter-spacing: 0 !important; font-size: .8rem !important; font-weight: 400 !important; }
  .task-list .c-interval::before { content: "↻ " !important; }
  .task-list .c-last { justify-self: end; text-align: right; }
  .task-list .c-last::before { content: "Fait le " !important; }
  .task-list .c-last .cell-sub { display: inline; font-size: .8rem; }
  .task-list .c-last .cell-sub::before { content: " · "; }

  /* Carte d'intervention */
  .log-list tr { grid-template-columns: 1fr 1fr auto; }
  .log-list .c-date { grid-column: 1 / 3; font-size: .8rem; font-weight: 600; color: var(--muted); align-self: center; }
  .log-list .c-outcome { grid-column: 3; grid-row: 1; }
  .log-list td.full { grid-column: 1 / -1; }

  /* Relevé groupé : valeurs et saisie sur une seule ligne */
  .counters-list tr { grid-template-columns: 5.2rem 7.6rem 1fr; gap: .45rem .7rem; align-items: end; }
  .counters-list td.full:last-child { grid-column: auto; }
  .counters-list td:last-child::before { content: "Nouveau (h)" !important; }
  .counters-list input { min-height: 38px; padding: .35rem .6rem; }
  .counters-form .form-foot {
    position: sticky; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom)); margin: 0 -.8rem; padding: .6rem .8rem;
    background: linear-gradient(to bottom, rgb(242 244 247 / 0), var(--bg) 35%);
  }

  .systems-list form.toolbar { display: grid !important; grid-template-columns: 5rem 1fr; }
  .systems-list form.toolbar input[name="name"] { grid-column: 1 / -1; }
  .systems-list form.toolbar > * { min-width: 0; max-width: none !important; }
  .systems-list tr { align-items: end; }
}

@media print {
  .sidebar, .topbar, .tabbar, .actions, .toolbar, .btn { display: none !important; }
  .shell { display: block; }
}
