/* Componentes de formulário, diálogo e avisos (src/form.js). Usa as variáveis de app.css (temas claro e escuro). */

:root {
  --fld-border: color-mix(in srgb, var(--outline) 45%, var(--muted));
  --scrim: rgba(12, 17, 25, .5);
  --shadow-pop: 0 2px 6px rgba(0, 0, 0, .08), 0 18px 44px rgba(0, 0, 0, .2);
}
@media (prefers-color-scheme: dark) {
  :root {
    --scrim: rgba(0, 0, 0, .62);
    --shadow-pop: 0 0 0 1px var(--outline), 0 18px 44px rgba(0, 0, 0, .5);
  }
}

/* Layout de formulário */
.form { display: grid; gap: 18px; min-width: 0; }
.form-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr)); gap: 18px 16px; align-items: start; min-width: 0; }
.form-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 10px; margin-top: 4px; }
.form-actions .btn, .dlg-actions .btn { min-height: 44px; padding-inline: 20px; }
.form-actions-start { margin-right: auto; }

/* Campo: rótulo, controle, erro e dica */
.fld { display: grid; gap: 6px; min-width: 0; align-content: start; }
.fld-label { font-size: .9rem; font-weight: 600; color: var(--text); }
.fld-req { color: var(--error); }
.fld-input { appearance: none; display: block; width: 100%; min-height: 46px; margin: 0; padding: 10px 14px; border: 1px solid var(--fld-border); border-radius: 12px; background: var(--surface); color: var(--text); font: inherit; font-size: 1rem; line-height: 1.4; }
.fld-input::placeholder { color: var(--muted); opacity: .7; }
.fld-input:hover { border-color: var(--muted); }
.fld-input:focus-visible { outline: 2px solid var(--secondary); outline-offset: 0; border-color: var(--secondary); }
.fld-input:disabled { opacity: .55; cursor: default; }
textarea.fld-input { min-height: 68px; resize: vertical; }
.fld-select { position: relative; display: grid; }
.fld-select select { padding-right: 42px; cursor: pointer; }
.fld-select .icon { position: absolute; right: 13px; top: 50%; translate: 0 -50%; rotate: 90deg; color: var(--muted); pointer-events: none; }
.fld-hint { margin: 0; color: var(--muted); font-size: .85rem; }
.fld-meta { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.fld-count { flex: none; color: var(--muted); font-size: .8rem; font-variant-numeric: tabular-nums; }
.fld-error { display: flex; align-items: flex-start; gap: 6px; margin: 0; color: var(--error); font-size: .85rem; font-weight: 600; }
.fld-error .icon { margin-top: 2px; }
.fld-error[hidden], .form-error[hidden] { display: none; }
.fld-invalid .fld-input, .fld-invalid .seg { border-color: var(--error); }
.fld-invalid .fld-input:focus-visible { outline-color: var(--error); border-color: var(--error); }

/* Caixa de seleção */
.fld-checkbox { gap: 2px; }
.chk { display: flex; align-items: center; gap: 12px; min-height: 44px; padding: 4px 0; cursor: pointer; }
.chk-box { flex: none; width: 22px; height: 22px; margin: 0; accent-color: var(--primary); cursor: pointer; }
.chk-text { min-width: 0; }
.fld-checkbox .fld-hint, .fld-checkbox .fld-error { margin-left: 34px; }
.fld-checkbox .fld-hint { margin-top: -6px; }

/* Escolha única (segmentado) */
/* Largura: a do conteúdo (mínimo de 320 px); em coluna com menos de 440 px ocupa a linha inteira, como os campos. */
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr); gap: 2px; width: fit-content; max-width: 100%; min-width: min(100%, clamp(320px, (440px - 100%) * 999, 100%)); padding: 2px; border: 1px solid var(--fld-border); border-radius: 12px; background: var(--surface-2); }
.seg-opt { appearance: none; min-width: 0; min-height: 40px; margin: 0; padding: 6px 10px; border: 0; border-radius: 9px; background: transparent; color: var(--muted); font: inherit; font-weight: 600; line-height: 1.2; overflow-wrap: anywhere; cursor: pointer; }
/* Quatro opções: linha inteira (até 560 px); em coluna com menos de 440 px viram uma grade 2x2, para rótulos como
   "Aguardando aprovação" caberem. A base de 40% só vale na coluna estreita (mesma conta da largura mínima acima). */
.seg-4 { display: flex; flex-wrap: wrap; width: 100%; max-width: min(100%, 560px); min-width: 0; }
.seg-4 .seg-opt { flex: 1 1 clamp(0px, (440px - 100%) * 999, 40%); }
.seg-opt:hover { background: var(--surface-3); color: var(--text); }
.seg-opt.seg-on { background: var(--primary-container); color: var(--on-primary-container); box-shadow: inset 0 0 0 1.5px var(--primary); }

/* Bloco de erro do formulário e do diálogo */
.form-error { display: flex; align-items: flex-start; gap: 10px; padding: 11px 14px; border-radius: 12px; background: var(--error-container); color: var(--text); font-size: .93rem; overflow-wrap: anywhere; }
.form-error .icon { margin-top: 1px; color: var(--error); }

/* Botão em andamento */
.btn-busy::before { content: ""; display: inline-block; width: .95em; height: .95em; margin-right: 8px; vertical-align: -.14em; border: 2px solid currentColor; border-top-color: transparent; border-radius: 50%; animation: form-spin .8s linear infinite; }
.btn-busy .icon { display: none; }
@keyframes form-spin { to { transform: rotate(360deg); } }

/* Diálogo modal */
/* A caixa visível é .dlg-panel; o <dialog> fica sem transform para os avisos (position: fixed) dentro dele não saírem do lugar. */
.dlg { width: min(560px, calc(100vw - 32px)); max-width: none; max-height: min(760px, calc(100dvh - 32px)); padding: 0; border: 0; background: transparent; color: var(--text); overflow: visible; }
.dlg[open] { display: flex; }
.dlg:focus-visible { outline: none; }
.dlg::backdrop { background: var(--scrim, rgba(12, 17, 25, .5)); animation: dlg-fade .18s ease-out; }
.dlg-confirm { width: min(440px, calc(100vw - 32px)); }
.dlg-panel { display: flex; flex-direction: column; flex: 1; min-width: 0; min-height: 0; border-radius: 22px; background: var(--surface); box-shadow: var(--shadow-pop); overflow: hidden; animation: dlg-in .18s ease-out; }
.dlg-head { display: flex; align-items: flex-start; gap: 12px; padding: 18px 14px 6px 24px; }
.dlg-title { flex: 1; min-width: 0; padding-top: 6px; font-size: 1.22rem; line-height: 1.3; letter-spacing: -.01em; overflow-wrap: anywhere; }
.dlg-x { appearance: none; flex: none; display: grid; place-items: center; width: 40px; height: 40px; margin: 0; padding: 0; border: 0; border-radius: 50%; background: transparent; color: var(--muted); cursor: pointer; }
.dlg-x:hover { background: var(--surface-2); color: var(--text); }
.dlg-x:disabled { opacity: .45; cursor: default; }
.dlg-error { margin: 6px 24px 4px; }
.dlg-body { flex: 1; min-height: 0; padding: 10px 24px 6px; overflow-y: auto; overscroll-behavior: contain; }
.dlg-busy .dlg-body { opacity: .6; }
.dlg-message { margin: 0; color: var(--muted); font-size: 1rem; white-space: pre-wrap; overflow-wrap: anywhere; }
.dlg-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 10px; padding: 16px 24px 20px; }
.dlg-confirm .btn-danger { background: var(--error); color: var(--surface); border-color: transparent; }
.dlg-confirm .btn-danger:hover { background: var(--error); filter: brightness(1.08); }
@keyframes dlg-in { from { opacity: 0; transform: translateY(10px) scale(.985); } }
@keyframes dlg-fade { from { opacity: 0; } }
html.dlg-lock { overflow: hidden; }
html.dlg-lock-gutter { scrollbar-gutter: stable; }

/* Avisos (toast): canto inferior direito; no celular, no alto (embaixo ficam a navegação e as ações do diálogo). */
.toasts { position: fixed; z-index: 60; right: 20px; bottom: 20px; display: flex; flex-direction: column; gap: 10px; width: min(400px, calc(100vw - 32px)); pointer-events: none; }
.toast { display: flex; align-items: flex-start; gap: 10px; padding: 6px 6px 6px 14px; border: 1px solid var(--outline); border-radius: 14px; background: var(--surface); color: var(--text); box-shadow: var(--shadow-pop); font-size: .95rem; pointer-events: auto; animation: toast-in .18s ease-out; }
.toast > .icon { margin-top: 10px; }
.toast-msg { flex: 1; min-width: 0; margin: 0; padding: 9px 0; overflow-wrap: anywhere; }
.toast-x { appearance: none; flex: none; display: grid; place-items: center; width: 40px; height: 40px; margin: 0; padding: 0; border: 0; border-radius: 50%; background: transparent; color: inherit; cursor: pointer; }
.toast-x:hover { background: color-mix(in srgb, var(--text) 10%, transparent); }
.toast-info { background: var(--secondary-container); border-color: color-mix(in srgb, var(--secondary) 30%, transparent); }
.toast-info > .icon { color: var(--secondary); }
.toast-ok { background: var(--ok-container); border-color: color-mix(in srgb, var(--ok) 30%, transparent); }
.toast-ok > .icon { color: var(--ok); }
.toast-bad { background: var(--error-container); border-color: color-mix(in srgb, var(--error) 30%, transparent); }
.toast-bad > .icon { color: var(--error); }
.toast-out { opacity: 0; transform: translateY(6px); transition: opacity .15s ease-in, transform .15s ease-in; }
@keyframes toast-in { from { opacity: 0; transform: translateY(10px); } }

/* Celular e janelas estreitas */
@media (max-width: 760px) {
  .toasts { top: calc(12px + env(safe-area-inset-top)); right: 16px; bottom: auto; left: 16px; width: auto; flex-direction: column-reverse; }
  @keyframes toast-in { from { opacity: 0; transform: translateY(-10px); } }
  .toast-out { transform: translateY(-6px); }
}
@media (max-width: 560px) {
  .dlg, .dlg-confirm { width: 100%; max-width: 100%; max-height: calc(100dvh - 28px); margin: auto 0 0; }
  .dlg-panel { border-radius: 22px 22px 0 0; animation-name: dlg-up; animation-duration: .22s; }
  .dlg-head { padding-left: 20px; }
  .dlg-error { margin-inline: 20px; }
  .dlg-body { padding-inline: 20px; }
  .dlg-actions { padding: 14px 20px calc(16px + env(safe-area-inset-bottom)); }
  .dlg-actions .btn { flex: 1 1 0; min-height: 48px; }
  .form-actions .btn { flex: 1 1 auto; }
  .form-actions-start { margin-right: 0; }
  @keyframes dlg-up { from { transform: translateY(40px); opacity: 0; } }
}

@media (prefers-reduced-motion: reduce) {
  .dlg-panel, .dlg::backdrop, .toast { animation: none; }
  .toast-out { transition: none; }
  .btn-busy::before { animation-duration: 3s; }
}
