:root {
  --primary:       #f5c400;
  --primary-dark:  #d4a900;
  --primary-light: #fffbea;
  --dark:          #1a1a1a;
  --bg:            #f0f2f5;
  --gray-bg:       #f5f5f7;
  --card:          #ffffff;
  --border:        #e2e8f0;
  --text:          #1a1a1a;
  --muted:         #64748b;
  --red-light:     #fff0f0;
  --red-border:    #fcc;
  --red:           #cc0000;
  --radius-lg:     12px;
  --radius:        8px;
  --radius-sm:     6px;
  --shadow:        0 1px 3px rgba(0,0,0,0.07), 0 1px 2px rgba(0,0,0,0.04);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: 'Inter', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  padding: 24px 16px 110px;
}

.container { max-width: 860px; margin: 0 auto; }

/* ── Header ────────────────────────────────────────── */

.form-header {
  background: var(--dark);
  border-radius: var(--radius-lg);
  padding: 20px 24px;
  display: flex;
  align-items: center;
  gap: 18px;
  margin-bottom: 20px;
}

.btn-historico {
  margin-left: auto;
  background: rgba(245, 196, 0, 0.15);
  border: 1.5px solid rgba(245, 196, 0, 0.3);
  color: var(--primary);
  padding: 10px 18px;
  border-radius: var(--radius);
  text-decoration: none;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  transition: all 0.2s;
}

.btn-historico:hover {
  background: rgba(245, 196, 0, 0.25);
  border-color: var(--primary);
  transform: translateY(-1px);
}

.btn-historico i {
  font-size: 1rem;
}

.fh-logo {
  width: 52px;
  height: 52px;
  background: var(--primary);
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 0.85rem;
  color: var(--dark);
  flex-shrink: 0;
  letter-spacing: -0.5px;
}

.fh-brand { display: flex; flex-direction: column; gap: 6px; }

.fh-brand h1 {
  font-size: 1.3rem;
  font-weight: 700;
  color: #fff;
  line-height: 1.2;
}

.fh-badge {
  display: inline-block;
  background: rgba(245,196,0,0.18);
  color: var(--primary);
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 1.8px;
  padding: 3px 10px;
  border-radius: 20px;
  text-transform: uppercase;
  border: 1px solid rgba(245,196,0,0.3);
  align-self: flex-start;
}

/* ── Section Cards ─────────────────────────────────── */

.section-card {
  background: var(--card);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 22px 24px;
  margin-bottom: 14px;
  border: 1.5px solid transparent;
  transition: border-color 0.25s;
}

.section-card:focus-within {
  border-color: rgba(245,196,0,0.35);
}

.section-header {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 18px;
}

.section-icon {
  width: 34px;
  height: 34px;
  background: var(--primary-light);
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  margin-top: 1px;
}

.section-icon svg {
  width: 16px;
  height: 16px;
  stroke: var(--dark);
  stroke-width: 2;
}

.section-title {
  font-size: 0.93rem;
  font-weight: 700;
  color: var(--text);
  line-height: 1.3;
}

.section-subtitle {
  font-size: 0.77rem;
  color: var(--muted);
  margin-top: 2px;
}

/* ── Field Layout ──────────────────────────────────── */

.field-row {
  display: grid;
  grid-template-columns: 1fr;
  gap: 14px;
  margin-bottom: 14px;
}

.field-row:last-child { margin-bottom: 0; }

.field-row.cols-2 { grid-template-columns: 1fr 1fr; }
.field-row.cols-3 { grid-template-columns: 1fr 1fr 1fr; }
.field-row.cols-4 { grid-template-columns: 1fr 1fr 1fr 1fr; }

.field { display: flex; flex-direction: column; gap: 6px; }

label {
  font-size: 0.73rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.label-opt {
  font-weight: 400;
  text-transform: none;
  letter-spacing: 0;
  font-size: 0.73rem;
  color: #b0b8c4;
}

.req {
  color: #cc0000;
  font-weight: 800;
}

input[type="text"],
input[type="date"],
input[type="number"],
textarea {
  width: 100%;
  padding: 10px 14px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 0.9rem;
  color: var(--text);
  background: #fafafa;
  transition: border-color 0.2s, box-shadow 0.2s, background 0.2s;
  outline: none;
  resize: vertical;
}

input:focus, textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(245,196,0,0.18);
  background: #fff;
}

input::placeholder,
textarea::placeholder { color: #b8c0cc; }

input[type="number"] {
  appearance: textfield;
  -moz-appearance: textfield;
}

input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

input[readonly] {
  background: #f5f5f7;
  color: var(--muted);
  cursor: default;
}

/* ── Rich Text Editor ──────────────────────────────── */

.editor-wrap {
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.editor-wrap:focus-within {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(245,196,0,0.18);
}

.rich-toolbar {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 6px 8px;
  background: #f8f9fa;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.rt-btn {
  background: none;
  border: 1px solid transparent;
  border-radius: 4px;
  cursor: pointer;
  color: var(--muted);
  padding: 4px 7px;
  font-size: 0.82rem;
  height: 28px;
  min-width: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
  line-height: 1;
  font-family: inherit;
}

.rt-btn:hover {
  background: #e9ecef;
  color: var(--text);
}

.rt-btn.active {
  background: var(--primary);
  border-color: var(--primary-dark);
  color: var(--dark);
}

.rt-btn svg {
  width: 13px;
  height: 13px;
  stroke: currentColor;
}

.rt-sep {
  width: 1px;
  height: 18px;
  background: var(--border);
  margin: 0 4px;
  flex-shrink: 0;
}

.rich-editor {
  min-height: 110px;
  padding: 12px 14px;
  font-family: inherit;
  font-size: 0.9rem;
  color: var(--text);
  background: #fff;
  outline: none;
  line-height: 1.65;
}

.rich-editor:empty::before {
  content: attr(data-placeholder);
  color: #b8c0cc;
  pointer-events: none;
  display: block;
}

/* ── Dynamic Table Rows ────────────────────────────── */

.table-header-row {
  display: grid;
  grid-template-columns: 1fr 120px 80px 120px 36px;
  gap: 6px;
  margin-bottom: 4px;
  padding: 0 2px;
}

.table-header-row span {
  font-size: 0.68rem;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.table-row {
  display: grid;
  grid-template-columns: 1fr 120px 80px 120px 36px;
  gap: 6px;
  margin-bottom: 6px;
  align-items: center;
}

/* btn-del vem primeiro no HTML mas deve aparecer por último no grid */
.table-row > .btn-del {
  order: 10;
}

/* measure-toggle usa as mesmas colunas — btn-measure fica dentro de .desc-row */
.with-measure-toggle .table-header-row,
.with-measure-toggle .table-row {
  grid-template-columns: 1fr 120px 80px 120px 36px;
}

.table-row input { margin: 0; }

/* .desc-row é flex: agrupa col-desc + btn (note/measure) numa célula só */
.desc-row {
  display: flex;
  gap: 6px;
  align-items: center;
  min-width: 0;
}

.desc-row .col-desc {
  flex: 1;
  min-width: 0;
}

.input-row {
  display: contents;
}

.input-wrapper {
  display: contents;
}

.input-label {
  display: none;
}

.row-actions {
  display: flex;
  gap: 4px;
}

.btn-note {
  background: #eef3ff;
  border: 1.5px solid #c9d7ff;
  color: #2e4ea8;
  border-radius: var(--radius-sm);
  cursor: pointer;
  height: 38px;
  width: 36px;
  font-size: 0.66rem;
  font-weight: 700;
}

.btn-note.active {
  background: #dbe6ff;
  border-color: #a9c0ff;
}

.row-detail {
  grid-column: 1 / -1;
  display: none;
  margin-top: 2px;
}

.row-detail.open {
  display: block;
}

.col-detail {
  width: 100%;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 12px;
  font-family: inherit;
  font-size: 0.83rem;
  color: var(--text);
  background: #fff;
  line-height: 1.45;
  resize: vertical;
}

.measure-toggle {
  display: flex;
  align-items: center;
}

.btn-measure {
  width: auto;
  min-width: 44px;
  padding: 0 10px;
  height: 38px;
  flex-shrink: 0;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  background: #f8fafc;
  color: var(--text);
  font-family: inherit;
  font-size: 0.76rem;
  font-weight: 700;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
}

.btn-measure:hover {
  border-color: var(--primary);
  background: var(--primary-light);
}

.btn-del {
  background: var(--red-light);
  border: 1.5px solid var(--red-border);
  color: var(--red);
  border-radius: var(--radius-sm);
  cursor: pointer;
  height: 38px;
  width: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
  flex-shrink: 0;
  font-size: 0.85rem;
}

.btn-del:hover { background: var(--red-border); }

.btn-add-row {
  margin-top: 8px;
  background: none;
  border: 2px dashed var(--border);
  color: var(--muted);
  border-radius: var(--radius-sm);
  padding: 10px 16px;
  font-size: 0.83rem;
  font-weight: 500;
  cursor: pointer;
  width: 100%;
  transition: all 0.2s;
  font-family: inherit;
}

.btn-add-row:hover {
  border-color: var(--primary);
  color: var(--text);
  background: var(--primary-light);
}

.section-subtotal {
  text-align: right;
  font-size: 0.82rem;
  color: var(--muted);
  padding: 10px 4px 2px;
  border-top: 1px solid var(--border);
  margin-top: 10px;
}

.section-subtotal strong {
  color: var(--text);
  font-size: 0.92rem;
}

/* ── Total Geral Bar ───────────────────────────────── */

.total-geral-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--dark);
  padding: 20px 24px;
  border-radius: var(--radius-lg);
  margin-bottom: 14px;
}

.tg-label {
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.8px;
  text-transform: uppercase;
  color: rgba(255,255,255,0.55);
}

.tg-value {
  font-size: 1.9rem;
  font-weight: 800;
  color: var(--primary);
  letter-spacing: -0.5px;
}

/* ── Payment Options ───────────────────────────────── */

.payment-options {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.pay-opt {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  padding: 12px 14px;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  transition: all 0.2s;
  user-select: none;
  min-width: 82px;
  position: relative;
}

.pay-opt input[type="checkbox"] {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.pay-opt:has(input:checked) {
  border-color: var(--primary);
  background: var(--primary-light);
}

.pay-icon {
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f4f4f7;
  border-radius: var(--radius-sm);
  color: var(--muted);
  transition: all 0.2s;
}

.pay-opt:has(input:checked) .pay-icon {
  background: var(--primary);
  color: var(--dark);
}

.pay-icon i {
  font-size: 0.96rem;
  line-height: 1;
}

.pay-label {
  font-size: 0.66rem;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.4px;
  transition: color 0.2s;
}

.pay-opt:has(input:checked) .pay-label { color: var(--text); }

/* ── Sticky Actions ────────────────────────────────── */

.actions-wrap {
  position: sticky;
  bottom: 16px;
  z-index: 100;
}

.actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  background: rgba(255,255,255,0.9);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border: 1px solid rgba(226,232,240,0.8);
  border-radius: var(--radius-lg);
  padding: 14px 20px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.12);
}

.btn-preview {
  padding: 11px 22px;
  background: #fff;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 0.87rem;
  font-weight: 600;
  color: var(--text);
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  gap: 7px;
}

.btn-preview:hover { background: #f4f4f7; }

.btn-preview svg, .btn-export svg {
  width: 15px;
  height: 15px;
  stroke: currentColor;
  flex-shrink: 0;
}

.btn-export {
  padding: 11px 26px;
  background: var(--primary);
  border: none;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 0.87rem;
  font-weight: 700;
  color: var(--dark);
  cursor: pointer;
  transition: all 0.15s;
  display: flex;
  align-items: center;
  gap: 7px;
  box-shadow: 0 4px 0 var(--primary-dark);
}

.btn-export:hover  { background: #ffd200; }
.btn-export:active {
  transform: translateY(2px);
  box-shadow: 0 2px 0 var(--primary-dark);
}

/* ── Responsive ────────────────────────────────────── */

@media (max-width: 640px) {
  body { padding: 14px 12px 110px; }

  .form-header { padding: 16px; gap: 14px; }
  .fh-logo { width: 56px; height: 56px; font-size: 0.85rem; }
  .fh-brand h1 { font-size: 1.1rem; }

  .section-card { padding: 18px 16px; }

  .field-row.cols-2,
  .field-row.cols-3,
  .field-row.cols-4 { grid-template-columns: 1fr; }

  /* ── Card Layout Limpo para Mobile ── */
  .table-header-row {
    display: none;
  }

  .table-row {
    position: relative;
    display: block;
    background: #fff;
    border: 2px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 16px;
    padding-top: 56px;
    margin-bottom: 16px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
  }

  .table-row > * {
    margin-bottom: 14px;
  }

  .table-row > *:last-child {
    margin-bottom: 0;
  }

  .table-row input,
  .table-row textarea {
    font-size: 0.95rem;
    padding: 11px 12px;
    border: 1.5px solid var(--border);
    border-radius: var(--radius-sm);
    background: #fff;
    width: 100%;
    margin: 0;
    font-family: inherit;
  }

  .table-row input:focus,
  .table-row textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(245, 196, 0, 0.12);
  }

  /* Labels com melhor legibilidade */
  .table-row [data-label]::before {
    content: attr(data-label);
    display: block;
    font-size: 0.72rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.6px;
    color: var(--muted);
    margin-bottom: 6px;
  }

  /* Linha de descrição + botão */
  .table-row .desc-row {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 8px;
    align-items: flex-end;
    margin-bottom: 0;
  }

  .table-row .desc-row > * {
    margin: 0;
  }

  /* Descrição - destaque suave */
  .table-row .col-desc {
    font-weight: 600;
    font-size: 0.98rem;
    color: var(--text);
    padding: 12px;
    border: 2px solid var(--primary);
    background: var(--primary-light);
    min-width: 0;
  }

  .table-row .col-desc::before {
    color: var(--dark);
    font-weight: 800;
  }

  /* Botões compactos ao lado da descrição */
  .table-row .btn-measure,
  .table-row .btn-note {
    height: auto;
    min-height: 48px;
    min-width: 48px;
    padding: 8px 12px;
    font-size: 0.8rem;
    font-weight: 700;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
    flex-shrink: 0;
  }

  .table-row .btn-measure {
    background: var(--primary);
    border: 2px solid var(--primary-dark);
    color: var(--dark);
  }

  .table-row .btn-measure:active {
    transform: scale(0.96);
  }

  .table-row .btn-note {
    background: #eef3ff;
    border: 2px solid #c9d7ff;
    color: #2e4ea8;
  }

  .table-row .btn-note:active {
    transform: scale(0.96);
  }

  .table-row .btn-note.active {
    background: #2e4ea8;
    color: #fff;
    border-color: #1a2f5f;
  }

  /* Grid 2 colunas para Valor + Quantidade */
  .table-row .input-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-top: 14px;
    margin-bottom: 10px;
  }

  .table-row .input-wrapper {
    display: flex;
    flex-direction: column;
    gap: 6px;
  }

  .table-row .input-label {
    font-size: 0.72rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.6px;
    color: var(--muted);
    display: block;
  }

  .table-row .input-wrapper input {
    margin: 0;
  }

  /* Subtotal destacado */
  .table-row .col-vtot {
    font-weight: 800;
    font-size: 1.05rem;
    color: var(--dark);
    background: var(--gray-bg);
    border: 2px solid var(--border);
    text-align: right;
  }

  .table-row .col-vtot::before {
    font-weight: 800;
    color: var(--dark);
  }

  /* Botão delete no topo à direita */
  .btn-del {
    position: absolute;
    top: 12px;
    right: 12px;
    height: 38px;
    width: 38px;
    padding: 0;
    background: #fff;
    border: 2px solid var(--red-border);
    color: var(--red);
    border-radius: var(--radius);
    cursor: pointer;
    font-weight: 700;
    font-size: 0.95rem;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10;
  }

  .btn-del:active {
    transform: scale(0.96);
  }

  .btn-del:hover {
    background: var(--red-light);
    border-color: var(--red);
  }

  /* Detail box - logo abaixo da descrição */
  .row-detail {
    display: none;
    padding-top: 10px;
    margin-top: 8px;
    margin-bottom: 0;
  }

  .row-detail.open {
    display: block;
  }

  .row-detail textarea {
    font-family: 'Inter', sans-serif;
    font-size: 0.9rem;
    line-height: 1.5;
    min-height: 90px;
    resize: vertical;
  }

  .payment-options { gap: 8px; }
  .pay-opt { min-width: 72px; padding: 12px 10px; }

  .actions { flex-direction: column; }
  .btn-preview, .btn-export { justify-content: center; }
}
