/*
 * Formularios e teclado do APK.
 * Esta camada atua somente no WebView nativo e nao altera regras funcionais.
 */

html.native-app {
  --app-visible-height: 100dvh;
  --app-viewport-top: 0px;
  --app-viewport-center: 50dvh;
  --app-keyboard-height: 0px;
  --native-control-height: 52px;
  --native-control-radius: 5px;
  --native-form-gap: 14px;
}

/* Mantem uma superficie opaca e sem animacoes concorrentes enquanto o Android
   redimensiona o WebView para o teclado. Isso evita flashes nas bordas curvas. */
html.native-app.keyboard-resizing,
html.native-app.keyboard-resizing body {
  background-color: #f5f6f2;
}

html.native-app.keyboard-resizing *,
html.native-app.keyboard-resizing *::before,
html.native-app.keyboard-resizing *::after {
  transition-duration: 0s !important;
  animation-duration: 0s !important;
}

/* Fix duplicated visual password field in native WebView: render border only on the input, not on the wrapper */
html.native-app .login-form label { position: relative; }
html.native-app .login-form .password-field { border: 0 !important; background: transparent !important; padding: 0 !important; }
html.native-app .login-form .password-field input {
  border: 1px solid #c9dad3 !important;
  border-radius: var(--native-control-radius) !important;
  height: var(--native-control-height) !important;
  padding: 0 14px !important;
  background: #fff !important;
  display: block !important;
  width: 100% !important;
  box-sizing: border-box !important;
}
html.native-app .login-form .password-field .password-toggle { position: absolute !important; right: 6px !important; top: 50% !important; transform: translateY(-50%) !important; z-index: 2 !important; }

html.native-app .login-sheet .form-fields {
  gap: 16px;
}

html.native-app .login-sheet .input-label {
  gap: 8px;
}

html.native-app .login-sheet .input-label > input,
html.native-app .login-sheet .password-field {
  margin-top: 0 !important;
  min-height: 56px !important;
  height: 56px !important;
}

html.native-app .account-sheet {
  gap: 18px;
}

html.native-app .account-sheet .account-content {
  gap: 14px;
  padding: 0;
}

html.native-app .account-sheet .account-identity,
html.native-app .account-sheet .account-permissions {
  border-radius: 18px;
}

html.native-app .account-sheet .account-meta article {
  border-radius: 16px;
}

/* Ensure extra wrapper instances are hidden if present */
html.native-app .login-form .password-field + .password-field { display: none !important; }

/* Topbar padding equal top and bottom (ignore expanded notification area) */
html.native-app .topbar {
  padding-top: calc(12px + env(safe-area-inset-top)) !important;
  padding-bottom: 12px !important;
  height: auto !important;
  box-sizing: border-box !important;
}

/* Keep mobile nav buttons from shifting on press */
html.native-app .mobile-nav button { transform: none !important; }
html.native-app .mobile-nav button:active { transform: none !important; }

html.native-app *,
html.native-app *::before,
html.native-app *::after {
  box-sizing: border-box;
}

html.native-app :is(input, select, textarea, button) {
  max-width: 100%;
}

html.native-app :is(input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]), select, textarea) {
  min-width: 0;
}

html.native-app :is(.form-grid, .record-fields, .password-change-fields, .login-form, .modal-scroll-body) label {
  min-width: 0;
  max-width: 100%;
  color: #40564e;
  font-size: 14px;
  font-weight: 760;
  line-height: 1.35;
  overflow-wrap: anywhere;
}

html.native-app :is(.form-grid, .record-fields, .password-change-fields, .login-form, .modal-scroll-body) :is(input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]), select) {
  width: 100%;
  height: var(--native-control-height);
  min-height: var(--native-control-height);
  margin-top: 7px;
  padding: 0 14px;
  border: 1px solid #c9dad3;
  border-radius: var(--native-control-radius);
  color: #172a26;
  background-color: #fff;
  font-size: 16px;
  line-height: 1.2;
}

html.native-app :is(.form-grid, .record-fields, .password-change-fields, .modal-scroll-body) textarea {
  width: 100%;
  min-height: 112px;
  margin-top: 7px;
  padding: 13px 14px;
  border: 1px solid #c9dad3;
  border-radius: var(--native-control-radius);
  color: #172a26;
  background: #fff;
  font-size: 16px;
  line-height: 1.45;
  resize: vertical;
}

html.native-app :is(.form-grid, .record-fields, .password-change-fields, .login-form, .modal-scroll-body) :is(input, select, textarea):focus {
  border-color: #6da2ba;
  outline: 0;
  box-shadow: 0 0 0 4px rgba(109, 162, 186, .17);
}

html.native-app .password-field {
  width: 100%;
  height: var(--native-control-height);
  min-height: var(--native-control-height);
  margin-top: 7px;
  padding: 0 52px 0 14px;
  border: 1px solid #c9dad3;
  border-radius: var(--native-control-radius);
}

html.native-app .password-field input:not([type="checkbox"]) {
  height: 100%;
  min-height: 0;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  box-shadow: none;
  background: transparent;
}

html.native-app .password-field:focus-within {
  border-color: #6da2ba;
  box-shadow: 0 0 0 4px rgba(109, 162, 186, .17);
}

html.native-app .password-field .password-toggle {
  width: 48px;
  min-width: 48px;
  height: 48px;
  min-height: 48px;
  right: 2px;
}

html.native-app :is(.primary-button, .ghost-button, .secondary-button, .danger-button, .compact-button, .text-button, .manage-actions button, .modal footer button) {
  min-width: 0;
  min-height: 50px;
  height: auto;
  padding: 11px 15px;
  justify-content: center;
  border-radius: 13px;
  font-size: 14px;
  line-height: 1.25;
  text-align: center;
  white-space: normal;
  overflow-wrap: anywhere;
}

html.native-app .modal {
  position: fixed !important;
  top: var(--app-viewport-center) !important;
  right: auto !important;
  bottom: auto !important;
  left: 50% !important;
  width: min(780px, calc(100vw - 20px));
  max-width: calc(100vw - 20px);
  height: auto !important;
  max-height: calc(var(--app-visible-height) - 16px) !important;
  margin: 0 !important;
  overflow: clip !important;
  border-radius: 22px;
  transform: translate(-50%, -50%) !important;
}

html.native-app .modal > :is(form, div),
html.native-app .modal :is(.account-dialog, .sync-status-dialog, .delete-confirm-content, .confirm-dialog) {
  width: 100%;
  min-height: 0;
  height: auto !important;
  max-height: calc(var(--app-visible-height) - 16px) !important;
}

html.native-app .modal :is(form, .account-dialog, .sync-status-dialog, .delete-confirm-content, .confirm-dialog) {
  display: flex;
  flex-direction: column;
  overflow: clip;
}

html.native-app .modal :is(.form-grid, .record-fields, .password-change-fields, .modal-scroll-body, .account-content, .sync-diagnostics-grid) {
  min-height: 0;
  flex: 1 1 auto;
  gap: var(--native-form-gap);
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

html.native-app .modal-scroll-body {
  padding: 0;
}

html.native-app .report-form-body .report-types {
  flex: none;
}

html.native-app .report-form-body .dialog-label {
  display: block;
  padding-bottom: 18px;
}

html.native-app .modal form > header,
html.native-app .modal > div > header {
  min-width: 0;
  flex: 0 0 auto;
}

html.native-app .modal footer {
  min-width: 0;
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 190px));
  justify-content: center;
  gap: 10px;
}

html.native-app .modal footer > button {
  width: 100%;
  min-width: 0;
}

html.native-app #password-change-form > footer {
  grid-template-columns: minmax(0, 260px);
}

/* Os campos de criação da nova senha usam uma grade própria, sem herdar bordas duplicadas. */
html.native-app #password-change-modal .password-change-field {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-auto-rows: max-content;
  gap: 7px;
  margin: 0;
}

html.native-app #password-change-modal .password-change-field .password-field {
  height: 54px;
  min-height: 54px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 50px;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

html.native-app #password-change-modal .password-change-field .password-field input:not([type="checkbox"]) {
  width: 100%;
  height: 100%;
  min-height: 0;
  margin: 0;
  padding: 0 14px;
  border: 0;
  border-radius: 0;
  box-shadow: none;
  background: transparent;
}

html.native-app #password-change-modal .password-change-field .password-field input:focus {
  border: 0;
  box-shadow: none;
}

html.native-app #password-change-modal .password-change-field .password-toggle {
  position: static;
  width: 50px;
  min-width: 50px;
  height: 100%;
  min-height: 0;
  inset: auto;
  transform: none;
}

html.native-app #datasul-link-form .datasul-link-body {
  display: grid;
  grid-auto-rows: max-content;
  gap: 14px;
  overflow-x: hidden;
  overflow-y: auto;
}

html.native-app #datasul-link-form .datasul-link-pickers {
  grid-template-columns: minmax(0, 1fr);
}

html.native-app #datasul-link-form .datasul-link-picker,
html.native-app #datasul-link-form .datasul-link-details {
  padding: 14px;
}

html.native-app #datasul-link-form .datasul-link-details > .form-grid {
  grid-template-columns: minmax(0, 1fr);
  padding: 14px 0 0;
  overflow: visible;
}

html.native-app #datasul-link-form .datasul-link-details > .form-grid .span-3 {
  grid-column: 1;
}

html.native-app #datasul-link-form select[size] {
  height: 132px;
  min-height: 132px;
  margin-top: 0;
  padding: 6px 10px;
}

html.native-app #login-form {
  min-height: 0;
  flex: 1 1 auto;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

html.native-app #login-form > footer {
  width: 100%;
  margin-top: auto;
}

html.native-app #user-modal .user-form-layout {
  min-height: 0;
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-content: start;
  scroll-padding-block: 14px;
}

/* O cadastro/edicao de usuarios abre equilibrado, com respiro igual no topo e na base. */
html.native-app:not(.keyboard-open) #user-modal > form > header {
  padding: 18px 20px;
}

html.native-app:not(.keyboard-open) #user-modal .user-form-layout {
  padding: 16px;
}

html.native-app:not(.keyboard-open) #user-modal > form > footer {
  padding: 16px;
}

html.native-app #user-modal > form,
html.native-app #access-modal > .account-dialog,
html.native-app #password-change-modal > form,
html.native-app #api-settings-modal > form,
html.native-app #report-modal > form,
html.native-app #record-modal > form,
html.native-app #verba-modal > form,
html.native-app #datasul-task-modal > form,
html.native-app #datasul-link-modal > form {
  min-height: 0 !important;
  max-height: calc(var(--app-visible-height) - 16px) !important;
}

html.native-app #user-modal .user-form-section {
  min-width: 0;
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-auto-flow: row;
  grid-auto-rows: max-content;
  height: auto;
  align-content: start;
  overflow: visible;
}

/* Cada campo ocupa sua propria linha e cresce com o texto sem invadir o seguinte. */
html.native-app #user-modal :is(.user-fields-grid, .user-access-switches) {
  display: grid;
  grid-auto-flow: row;
  grid-auto-rows: max-content;
  align-items: start;
}

html.native-app #user-modal :is(.user-fields-grid > label, .user-form-section > label:not(.switch-field)) {
  min-width: 0;
  max-width: 100%;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-auto-flow: row;
  grid-auto-rows: max-content;
  align-content: start;
  gap: 7px;
  margin: 0;
  overflow: visible;
}

html.native-app #user-modal :is(.user-fields-grid > label, .user-form-section > label:not(.switch-field)) > :is(input, select, .password-field) {
  margin-top: 0;
}

html.native-app #user-modal .user-password-help {
  position: static;
  display: block;
  min-width: 0;
  max-width: 100%;
  margin: 0;
  line-height: 1.5;
  overflow-wrap: anywhere;
}

html.native-app #user-modal :is(.user-fields-grid, .user-access-switches) {
  min-width: 0;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

html.native-app #user-modal :is(.switch-field, .permission-customization, .permission-selector, .permission-options, .permission-options label) {
  min-width: 0;
  max-width: 100%;
}

html.native-app #user-modal .switch-field {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-auto-flow: row;
  grid-auto-rows: max-content;
  align-content: start;
  align-items: start;
  gap: 8px;
  margin: 0;
}

html.native-app #user-modal .switch-field > span {
  min-width: 0;
  height: auto;
  min-height: 36px;
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr);
  column-gap: 12px;
  margin-top: 0;
  align-items: center;
}

html.native-app #user-modal .switch-field em {
  grid-column: 2;
  min-width: 0;
  margin: 0;
  line-height: 1.35;
  overflow-wrap: anywhere;
}

html.native-app #user-modal .switch-field i {
  grid-column: 1;
  flex: 0 0 34px;
}

html.native-app #user-modal .permission-options input[type="checkbox"],
html.native-app #user-modal .switch-field input[type="checkbox"] {
  flex: 0 0 auto;
}

html.native-app #user-modal .permission-selector {
  position: static;
  display: block;
  margin: 0;
}

html.native-app #user-modal .permission-options {
  display: grid;
  grid-auto-flow: row;
  grid-auto-rows: max-content;
  align-items: stretch;
}

html.native-app #user-modal .permission-options label {
  position: static;
  min-height: 0;
  height: auto;
  align-self: stretch;
}

html.native-app #user-modal .user-scope-selector {
  min-width: 0;
  max-width: 100%;
}

html.native-app #user-modal .user-scope-options {
  grid-template-columns: minmax(0, 1fr);
  max-height: 210px;
}

html.native-app :is(input, select, textarea):focus {
  scroll-margin-top: 76px;
  scroll-margin-bottom: 76px;
}

@media (max-width: 760px) {
  html.native-app {
    --native-page-left: max(var(--ui-page-x), env(safe-area-inset-left));
    --native-page-right: max(var(--ui-page-x), env(safe-area-inset-right));
  }

  /* O fundo do menu superior ocupa a viewport inteira; apenas o conteudo respeita a margem. */
  html.native-app .topbar {
    width: calc(100% + var(--native-page-left) + var(--native-page-right));
    margin-right: calc(0px - var(--native-page-right));
    margin-left: calc(0px - var(--native-page-left));
    padding-right: var(--native-page-right);
    padding-left: var(--native-page-left);
    background: rgba(247, 250, 249, .96);
    box-shadow: 0 8px 24px rgba(49, 95, 121, .08);
    backdrop-filter: blur(18px);
  }

  html.native-app .modal {
    width: calc(100vw - 16px);
    max-width: calc(100vw - 16px);
  }

  html.native-app .form-grid {
    grid-template-columns: minmax(0, 1fr);
  }

  html.native-app .form-grid > :is(.span-2, .span-3) {
    grid-column: auto;
  }

  html.native-app #user-modal :is(.user-fields-grid, .user-access-switches) {
    grid-template-columns: minmax(0, 1fr);
  }

  html.native-app #user-modal .user-field-wide {
    grid-column: auto;
  }

  html.native-app .modal footer {
    padding-right: 14px;
    padding-left: 14px;
  }
}

/* O teclado usa a altura real do VisualViewport, inclusive em WebViews que nao atualizam dvh. */
html.native-app.keyboard-open {
  scroll-padding-bottom: 20px;
}

html.native-app.keyboard-open body::before,
html.native-app.keyboard-open .topbar,
html.native-app.keyboard-open .mobile-nav {
  display: none !important;
}

html.native-app.keyboard-open .main-content {
  min-height: var(--app-visible-height);
  padding-top: 0;
  padding-bottom: 20px;
}

html.native-app.keyboard-open .view {
  padding-top: 12px;
  padding-bottom: 12px;
}

html.native-app.keyboard-open .modal {
  top: calc(var(--app-viewport-top) + 6px) !important;
  right: auto !important;
  bottom: auto !important;
  left: 50% !important;
  height: calc(var(--app-visible-height) - 12px) !important;
  max-height: calc(var(--app-visible-height) - 12px) !important;
  margin: 0 !important;
  border-radius: 18px;
  transform: translateX(-50%) !important;
}

html.native-app.keyboard-open .modal > :is(form, div),
html.native-app.keyboard-open .modal :is(.account-dialog, .sync-status-dialog, .delete-confirm-content, .confirm-dialog) {
  height: 100% !important;
  max-height: 100% !important;
}

html.native-app.dialog-open.keyboard-open body {
  top: 0 !important;
}

html.native-app.keyboard-open .modal form > header,
html.native-app.keyboard-open .modal > div > header {
  min-height: 52px;
  padding: 8px 12px;
  align-items: center;
}

html.native-app.keyboard-open .modal header .modal-kicker,
html.native-app.keyboard-open .modal header p,
html.native-app.keyboard-open #user-modal .user-form-section-title small {
  display: none;
}

html.native-app.keyboard-open .modal header h2 {
  margin: 0;
  font-size: 18px;
  line-height: 1.2;
}

html.native-app.keyboard-open .modal header .close-modal {
  width: 44px;
  min-width: 44px;
  height: 44px;
  min-height: 44px;
  margin: 0;
}

html.native-app.keyboard-open #datasul-link-form select[size] {
  height: 84px;
  min-height: 84px;
}

html.native-app.keyboard-open .modal footer {
  gap: 8px;
  padding: 7px 10px calc(7px + env(safe-area-inset-bottom));
}

html.native-app.keyboard-open .modal footer > button {
  min-height: 44px;
  padding: 7px 9px;
  border-radius: 11px;
  font-size: 12px;
}

html.native-app.keyboard-open .modal :is(.form-grid, .record-fields, .password-change-fields, .account-content) {
  padding: 11px 12px;
}

html.native-app.keyboard-open #user-modal .user-form-layout {
  gap: 8px;
  padding: 10px;
}

html.native-app.keyboard-open #user-modal .user-form-section {
  gap: 9px;
  padding: 10px;
  border-radius: 12px;
}

html.native-app.keyboard-open #user-modal .user-form-section-title {
  padding-bottom: 7px;
}

html.native-app.keyboard-open #user-modal .user-form-section-title > .icon {
  width: 28px;
  height: 28px;
  padding: 6px;
}

html.native-app.keyboard-open #user-modal .user-form-section-title strong {
  font-size: 12px;
}

html.native-app.keyboard-open :is(input, select, textarea):focus {
  scroll-margin-top: 62px;
  scroll-margin-bottom: 62px;
}

@media (max-width: 360px) {
  html.native-app .modal footer {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  html.native-app .modal footer > button {
    padding-right: 8px;
    padding-left: 8px;
    font-size: 12px;
  }

  html.native-app #password-change-form > footer {
    grid-template-columns: minmax(0, 1fr);
  }
}
