:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #1d2330;
  --muted: #6b7280;
  --line: #e5e7eb;
  --primary: #2563eb;
  --primary-soft: #dbeafe;
  --green: #16a34a;
  --green-soft: #dcfce7;
  --amber: #b45309;
  --amber-soft: #fef3c7;
  --red: #dc2626;
  --red-soft: #fee2e2;
  --purple: #7c3aed;
  --purple-soft: #ede9fe;
  --side: #111827;
  --side-text: #cbd5e1;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 1px 3px rgba(0,0,0,.06);
}
* { box-sizing: border-box; }
html, body { margin: 0; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.45;
}
a { color: var(--primary); text-decoration: none; }
h1 { font-size: 22px; margin: 0; }
h2 { font-size: 17px; margin: 0 0 12px; }
h3 { font-size: 15px; margin: 0 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }
.mono { font-variant-numeric: tabular-nums; }

/* ---------- Layout ---------- */
.layout { display: flex; min-height: 100vh; min-height: 100dvh; }
.side {
  width: 232px; background: var(--side); color: var(--side-text); flex-shrink: 0;
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; height: 100dvh;
}
.brand { padding: 18px 18px 6px; font-weight: 800; font-size: 19px; color: #fff; letter-spacing: .3px; }
.brand small { display: block; font-size: 11px; font-weight: 500; color: #94a3b8; letter-spacing: 0; }
.side nav { flex: 1; overflow-y: auto; padding: 8px; }
.side nav .grp { font-size: 10px; text-transform: uppercase; letter-spacing: 1px; color: #64748b; padding: 14px 10px 4px; }
.side nav a {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 7px;
  color: var(--side-text); font-weight: 500;
}
.side nav a:hover { background: #1f2937; color: #fff; }
.side nav a.ativo { background: var(--primary); color: #fff; }
.side nav a .cnt { margin-left: auto; background: #ef4444; color: #fff; border-radius: 10px; font-size: 11px; padding: 0 7px; }
.side .user { padding: 12px 16px; border-top: 1px solid #1f2937; font-size: 12px; }
.side .user b { color: #fff; display: block; font-size: 13px; }
.side .user a { color: #94a3b8; margin-right: 10px; }
main { flex: 1; min-width: 0; padding: 22px 26px 60px; }
.topbar { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.topbar .sp { flex: 1; }

/* ---------- Componentes ---------- */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 16px; }
.card.flush { padding: 0; overflow: hidden; }
.grid { display: grid; gap: 14px; }
.g2 { grid-template-columns: repeat(2, minmax(0,1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0,1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0,1fr)); }

.stat { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 16px; display: block; color: inherit; }
.stat .v { font-size: 26px; font-weight: 700; }
.stat .l { color: var(--muted); font-size: 12px; }
a.stat:hover { outline: 2px solid var(--primary-soft); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--line); background: #fff; color: var(--text); border-radius: 8px;
  padding: 7px 13px; font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: #f9fafb; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.pri { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.pri:hover { background: #1d4ed8; }
.btn.ok { background: var(--green); border-color: var(--green); color: #fff; }
.btn.ok:hover { background: #15803d; }
.btn.wa { background: #25d366; border-color: #25d366; color: #fff; }
.btn.danger { color: var(--red); }
.btn.danger.solid { background: var(--red); color: #fff; border-color: var(--red); }
.btn.sm { padding: 4px 9px; font-size: 12px; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: #f3f4f6; }
.btns { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 20px; font-size: 11px; font-weight: 600; background: #f3f4f6; color: #374151; white-space: nowrap; }
.b-green { background: var(--green-soft); color: #166534; }
.b-amber { background: var(--amber-soft); color: var(--amber); }
.b-red { background: var(--red-soft); color: #991b1b; }
.b-blue { background: var(--primary-soft); color: #1e40af; }
.b-purple { background: var(--purple-soft); color: #5b21b6; }

table.t { width: 100%; border-collapse: collapse; }
.t th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); font-weight: 600; padding: 10px 12px; border-bottom: 1px solid var(--line); background: #fafafa; }
.t td { padding: 10px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.t tr:last-child td { border-bottom: 0; }
.t tr.click { cursor: pointer; }
.t tr.click:hover td { background: #f9fafb; }
.tw { overflow-x: auto; }

label.f { display: block; margin-bottom: 10px; }
label.f > span { display: block; font-size: 12px; font-weight: 600; color: #374151; margin-bottom: 4px; }
input, select, textarea {
  font: inherit; width: 100%; padding: 7px 10px; border: 1px solid #d1d5db; border-radius: 7px; background: #fff; color: var(--text);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }
input[type=checkbox] { width: auto; }
textarea { min-height: 70px; resize: vertical; }
.inline { display: flex; gap: 8px; align-items: center; }
.inline > * { flex: 1; }
.input-sm { padding: 4px 8px; width: 110px; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 14px; overflow-x: auto; }
.tabs a { padding: 8px 12px; color: var(--muted); font-weight: 600; border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs a.ativo { color: var(--primary); border-color: var(--primary); }

.empty { text-align: center; padding: 34px; color: var(--muted); }

/* ---------- Itens de orçamento ---------- */
.item { border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; margin-bottom: 10px; background: #fff; }
.item.ok { border-left: 4px solid var(--green); }
.item.pend { border-left: 4px solid var(--amber); }
.item .hd { display: flex; gap: 10px; align-items: flex-start; flex-wrap: wrap; }
.item .hd .nm { flex: 1; min-width: 180px; }
.item .nums { display: flex; gap: 18px; flex-wrap: wrap; align-items: flex-end; margin-top: 10px; }
.item .nums .k { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .4px; }
.item .nums .v { font-weight: 700; font-size: 15px; }
.lucro-pos { color: var(--green); }
.lucro-neg { color: var(--red); }
.anexos { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.anexo { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); border-radius: 6px; padding: 3px 8px; font-size: 12px; background: #fafafa; }
.anexo img { width: 34px; height: 34px; object-fit: cover; border-radius: 4px; }
.anexo .x { color: var(--red); cursor: pointer; font-weight: 700; }

.resumo { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; }
.resumo div { background: #f9fafb; border-radius: 8px; padding: 10px 12px; }
.resumo .k { font-size: 11px; color: var(--muted); text-transform: uppercase; }
.resumo .v { font-size: 19px; font-weight: 700; }

/* ---------- Custo breakdown ---------- */
.breakdown { width: 100%; font-size: 13px; }
.breakdown td { padding: 4px 0; }
.breakdown td:last-child { text-align: right; font-variant-numeric: tabular-nums; }
.breakdown tr.tot td { border-top: 2px solid var(--text); font-weight: 800; font-size: 17px; padding-top: 8px; }
.custo-box { background: #0f172a; color: #e2e8f0; border-radius: var(--radius); padding: 16px; position: sticky; top: 16px; }
.custo-box .breakdown tr.tot td { border-color: #475569; color: #4ade80; }
.custo-box h3 { color: #fff; }

/* ---------- Kanban ---------- */
.kanban { display: grid; grid-template-columns: repeat(4, minmax(240px, 1fr)); gap: 12px; overflow-x: auto; align-items: start; }
.col { background: #eceef2; border-radius: var(--radius); padding: 10px; min-height: 120px; }
.col > h3 { display: flex; justify-content: space-between; font-size: 13px; text-transform: uppercase; letter-spacing: .5px; color: #4b5563; margin: 2px 4px 10px; }
.job { background: #fff; border-radius: 8px; padding: 10px 12px; margin-bottom: 8px; box-shadow: var(--shadow); font-size: 13px; }
.job .tt { font-weight: 700; }
.job select { padding: 3px 6px; font-size: 12px; margin-top: 4px; }
.job .act { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.prio { border-left: 3px solid var(--red); }

/* ---------- Carretel ---------- */
.spool-bar { height: 8px; background: #e5e7eb; border-radius: 4px; overflow: hidden; min-width: 80px; }
.spool-bar > div { height: 100%; background: var(--green); }
.spool-bar.low > div { background: var(--red); }
.swatch { display: inline-block; width: 12px; height: 12px; border-radius: 50%; border: 1px solid rgba(0,0,0,.2); vertical-align: middle; margin-right: 4px; }

/* ---------- Modal ---------- */
.ov { position: fixed; inset: 0; background: rgba(15,23,42,.5); display: flex; align-items: flex-start; justify-content: center; z-index: 100; padding: 40px 12px; overflow-y: auto; }
.modal { background: #fff; border-radius: 12px; width: 100%; max-width: 560px; box-shadow: 0 20px 50px rgba(0,0,0,.3); }
.modal.lg { max-width: 820px; }
.modal .mh { padding: 14px 18px; border-bottom: 1px solid var(--line); display: flex; align-items: center; }
.modal .mh h2 { margin: 0; flex: 1; }
.modal .mb { padding: 16px 18px; }
.modal .mf { padding: 12px 18px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 8px; }
.modal pre { white-space: pre-wrap; background: #f0fdf4; border: 1px solid #bbf7d0; border-radius: 8px; padding: 12px; font-family: inherit; max-height: 50vh; overflow-y: auto; }

#toasts { position: fixed; bottom: 18px; right: 18px; z-index: 200; display: flex; flex-direction: column; gap: 8px; }
.toast { background: #111827; color: #fff; padding: 10px 14px; border-radius: 8px; box-shadow: 0 6px 20px rgba(0,0,0,.25); max-width: 380px; animation: tin .2s; }
.toast.err { background: var(--red); }
.toast.ok { background: var(--green); }
@keyframes tin { from { transform: translateY(10px); opacity: 0; } }

/* ---------- Login ---------- */
.login-wrap { min-height: 100vh; min-height: 100dvh; display: flex; align-items: center; justify-content: center; padding: 16px; background: linear-gradient(135deg, #0f172a, #1e3a8a); }
.login { background: #fff; border-radius: 14px; padding: 28px; width: 100%; max-width: 360px; box-shadow: 0 20px 60px rgba(0,0,0,.35); }
.login h1 { margin-bottom: 4px; }

.alert { padding: 10px 12px; border-radius: 8px; margin-bottom: 12px; font-size: 13px; }
.alert.amber { background: var(--amber-soft); color: #78350f; }
.alert.red { background: var(--red-soft); color: #7f1d1d; }
.alert.green { background: var(--green-soft); color: #14532d; }
.alert.blue { background: var(--primary-soft); color: #1e3a8a; }

.steps { display: flex; gap: 4px; flex-wrap: wrap; }
.steps span { font-size: 11px; padding: 3px 8px; border-radius: 4px; background: #f3f4f6; color: #9ca3af; font-weight: 600; }
.steps span.on { background: var(--primary); color: #fff; }
.steps span.done { background: var(--green-soft); color: #166534; }

/* =====================================================================
   Celular / iPhone
   ===================================================================== */
body { -webkit-tap-highlight-color: transparent; }
a, button, .btn, select, label, summary { touch-action: manipulation; }
/* input de arquivo invisível mas acionável pelo <label> (iOS não aciona display:none em versões antigas) */
.vh { position: absolute; width: 1px; height: 1px; opacity: 0; overflow: hidden; clip: rect(0 0 0 0); }
.fat-grid { display: grid; grid-template-columns: minmax(0,1fr) 320px; gap: 14px; align-items: start; }
.mob-top, .bottom-nav, .mob-bar, .side-bg { display: none; }
input:disabled { background: #f3f4f6; color: var(--muted); }

@media (max-width: 860px) {
  body { font-size: 15px; background: var(--bg); }
  /* 16px evita o zoom automático do Safari ao tocar no campo */
  input, select, textarea { font-size: 16px; padding: 10px 12px; border-radius: 9px; }
  .btn { min-height: 44px; padding: 10px 14px; border-radius: 10px; }
  .btn.sm { min-height: 38px; padding: 6px 11px; font-size: 13px; }
  .input-sm { width: 100%; }

  .layout { display: block; }
  main {
    padding: 14px max(12px, env(safe-area-inset-right)) calc(86px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  }
  main:has(.mob-bar) { padding-bottom: calc(150px + env(safe-area-inset-bottom)); }

  /* Cabeçalho fixo respeitando o notch */
  .mob-top {
    display: flex; align-items: center; position: sticky; top: 0; z-index: 40;
    background: var(--side); color: #fff; font-weight: 800; font-size: 17px;
    padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  }

  /* Menu lateral completo (aberto pelo botão "Menu") */
  .side {
    position: fixed; top: 0; left: -290px; width: 280px; z-index: 60; transition: left .22s ease;
    padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom);
  }
  .side.aberto { left: 0; box-shadow: 0 0 40px rgba(0,0,0,.4); }
  .side.aberto ~ .side-bg { display: block; position: fixed; inset: 0; background: rgba(15,23,42,.5); z-index: 55; }
  .side nav a { padding: 12px 10px; font-size: 15px; }
  .side .user { font-size: 14px; }
  .side .user a { display: inline-block; padding: 8px 12px 8px 0; }
  body.travado { overflow: hidden; }

  /* Barra de navegação inferior */
  .bottom-nav {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 45;
    background: rgba(255,255,255,.96); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
    border-top: 1px solid var(--line);
    padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  }
  .bottom-nav a, .bottom-nav button {
    flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
    min-height: 56px; padding: 6px 2px; font: 600 11px/1.1 inherit; font-family: inherit;
    color: var(--muted); background: none; border: 0; cursor: pointer;
  }
  .bottom-nav .ic { font-size: 21px; line-height: 1; position: relative; }
  .bottom-nav .cnt { position: absolute; top: -5px; right: -14px; background: #ef4444; color: #fff; border-radius: 10px; font-size: 10px; padding: 1px 5px; font-style: normal; }
  .bottom-nav a.ativo { color: var(--primary); }

  /* Topo das páginas */
  .topbar { gap: 10px; margin-bottom: 14px; }
  .topbar h1 { font-size: 20px; flex: 1 1 100%; line-height: 1.3; }
  .topbar .sp { display: none; }
  .topbar .btns { width: 100%; }
  .topbar .btns > * { flex: 1 1 auto; }
  .topbar .btns:has(> .voltar) { order: -1; width: auto; }
  .btn.voltar { border: 0; background: none; padding: 0; min-height: 32px; color: var(--primary); font-weight: 600; }

  /* Grades: uma ou duas colunas; ignora "span" que criaria coluna fantasma */
  .g2, .g3 { grid-template-columns: minmax(0,1fr); }
  .g4 { grid-template-columns: repeat(2, minmax(0,1fr)); }
  .grid > * { grid-column: auto !important; }
  .stat { padding: 12px 14px; }
  .stat .v { font-size: 22px; }
  .card { padding: 14px; border-radius: 12px; }
  h2 { font-size: 16px; }

  /* Tabelas viram cartões */
  .tw { overflow: visible; }
  .card.flush { background: transparent; box-shadow: none; overflow: visible; }
  table.t, .t tbody, .t tr, .t td { display: block; width: 100%; }
  .t thead { display: none; }
  .t tr { background: #fff; border-radius: 12px; box-shadow: var(--shadow); margin-bottom: 10px; padding: 8px 0; }
  .card:not(.flush) .t tr { box-shadow: none; border: 1px solid var(--line); }
  /* rótulo à esquerda (float) e conteúdo à direita, mantendo o conteúdo junto */
  .t td { border: 0; padding: 6px 14px; text-align: right; min-height: 32px; overflow-wrap: anywhere; }
  .t td[data-label]::before { content: attr(data-label); float: left; margin-right: 12px; color: var(--muted); font-size: 12px; font-weight: 600; line-height: 22px; }
  .t td.td-tit { text-align: left; font-size: 15px; padding-top: 8px; }
  .t td.td-tit::before { display: none; }
  .t td.td-aux { display: block; float: right; width: auto; padding-top: 9px; color: var(--muted); font-size: 13px; }
  .t td.td-aux::before { display: none; }
  .t td.td-aux:not([data-label]):not(:has(img)) { display: none; }
  .t td:not([data-label]) .btn { margin: 3px 0 3px 4px; }
  .t td .spool-bar { display: inline-block; width: 110px; vertical-align: middle; }
  .t td.nowrap { white-space: normal; }
  .t td:empty { display: none; }
  .t tr.click:active { background: #eef2ff; }
  .t.sem-cab td { text-align: left; }

  /* Itens do orçamento */
  .item { padding: 12px; border-radius: 12px; }
  .item .hd .nm { flex-basis: 100%; min-width: 0; }
  .item .nums { display: grid; grid-template-columns: repeat(3, minmax(0,1fr)); gap: 12px 10px; align-items: end; }
  .item .nums .k { font-size: 10px; }
  .item .nums > div { margin-left: 0 !important; text-align: left; }
  .item .nums .v { font-size: 14px; }
  #acoes .btn { flex: 1 1 100%; }
  .resumo { grid-template-columns: repeat(2, minmax(0,1fr)); }
  .resumo .v { font-size: 17px; }
  .anexo { font-size: 13px; padding: 6px 10px; max-width: 100%; }
  .anexo a { overflow-wrap: anywhere; }
  .anexo .x { padding: 0 6px; font-size: 20px; line-height: 1; }

  /* Modais como "folha" que sobe de baixo */
  .ov { padding: 0; align-items: flex-end; overscroll-behavior: contain; }
  .modal, .modal.lg { max-width: 100%; border-radius: 16px 16px 0 0; max-height: 92vh; max-height: 92dvh; display: flex; flex-direction: column; }
  .modal .mb { overflow-y: auto; -webkit-overflow-scrolling: touch; flex: 1; }
  .modal .mf { flex-wrap: wrap; padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
  .modal .mf .btn { flex: 1 1 40%; }
  .modal pre { max-height: none; }

  /* Fila: colunas deslizam lado a lado */
  .kanban { grid-template-columns: repeat(4, 86vw); scroll-snap-type: x mandatory; margin: 0 -12px; padding: 0 12px 8px; -webkit-overflow-scrolling: touch; }
  .col { scroll-snap-align: center; }
  .job select { font-size: 16px; padding: 8px 10px; }
  .job .act .btn { flex: 1 1 auto; }

  /* Fatiamento: custo sempre visível numa barra fixa */
  .fat-grid { grid-template-columns: minmax(0,1fr); }
  .custo-box { position: static; }
  .mob-bar {
    display: flex; align-items: center; gap: 12px; position: fixed; left: 0; right: 0; z-index: 44;
    bottom: calc(56px + env(safe-area-inset-bottom)); background: #0f172a; color: #fff;
    padding: 10px max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
    box-shadow: 0 -4px 16px rgba(0,0,0,.15);
  }
  .mob-bar > div { flex: 1; }
  .mob-bar b { font-size: 19px; color: #4ade80; }
  .mob-bar .muted { color: #94a3b8; }

  .tabs { margin-left: -12px; margin-right: -12px; padding: 0 12px; }
  .tabs a { padding: 10px 12px; }
  #toasts { left: 12px; right: 12px; bottom: calc(72px + env(safe-area-inset-bottom)); }
  .toast { max-width: none; }
  .login-wrap { padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom)); }
  .login { padding: 22px; }
}
@media (max-width: 380px) {
  .item .nums { grid-template-columns: repeat(2, minmax(0,1fr)); }
  .g4 { grid-template-columns: minmax(0,1fr); }
}
@media (hover: none) {
  .t tr.click:hover td { background: transparent; }
}

/* Seletor segmentado (Sem nota | Com nota) */
.nota-linha { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-top: 12px; }
.seg { display: inline-flex; background: #f3f4f6; border-radius: 10px; padding: 3px; }
.seg label { position: relative; cursor: pointer; }
.seg input { position: absolute; left: 0; top: 0; width: 1px !important; height: 1px; padding: 0 !important; margin: 0; opacity: 0; pointer-events: none; }
.seg span { display: block; padding: 7px 14px; border-radius: 8px; font-weight: 600; color: var(--muted); }
.seg input:checked + span { background: #fff; color: var(--text); box-shadow: var(--shadow); }
.seg input:disabled + span { cursor: default; }
.seg input:focus-visible + span { outline: 2px solid var(--primary); }
@media (max-width: 860px) {
  .seg { display: flex; width: 100%; }
  .seg label { flex: 1; text-align: center; }
  .seg span { padding: 10px 8px; }
}
