:root {
  color-scheme: light;
  --surface-max: 1560px;
  --touch-target: 44px;
  --focus-ring: 0 0 0 3px rgba(217, 0, 0, 0.2);
}

html {
  text-size-adjust: 100%;
  -webkit-text-size-adjust: 100%;
}

body[data-surface] {
  min-width: 280px;
  background:
    radial-gradient(circle at 15% -10%, rgba(217, 0, 0, 0.05), transparent 30rem),
    #f3f4f6;
}

body[data-surface] .topnav,
body[data-surface] .diag {
  display: none !important;
}

body[data-surface] .header {
  min-height: 76px;
  padding: 12px max(18px, env(safe-area-inset-right)) 12px max(18px, env(safe-area-inset-left));
  box-shadow: 0 1px 0 rgba(31, 36, 43, 0.06);
}

body[data-surface] .brand img {
  width: 150px;
  height: 44px;
  object-fit: contain;
  object-position: center;
  border: 0;
  padding: 0;
  border-radius: 0;
}

body[data-surface] .login-brand img {
  object-fit: contain;
  object-position: center;
}

body[data-surface] .brand h1 {
  font-size: clamp(16px, 1.7vw, 22px);
  letter-spacing: -0.02em;
}

body[data-surface] .brand p {
  margin-top: 4px;
  font-size: 11px;
}

body[data-surface] .badges {
  align-items: center;
}

body[data-surface] .badges .badge:first-child,
body[data-surface] .badges .badge:nth-child(2) {
  display: none;
}

body[data-surface="erp"] #sessionBadge {
  display: none;
}

body[data-surface] .view {
  width: 100%;
  max-width: var(--surface-max);
  margin: 0 auto;
  padding: 18px;
}

body[data-surface] .integrated,
body[data-surface] .integrated.erp-only,
body[data-surface] .integrated.app-only {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  min-height: calc(100dvh - 112px);
}

body[data-surface="erp"] .app-pane,
body[data-surface="worker"] .erp-pane {
  display: none !important;
}

body[data-surface] .pane {
  border-radius: 18px;
  border-color: #e1e4e8;
  box-shadow: 0 14px 45px rgba(31, 36, 43, 0.06);
}

body[data-surface] .pane-head {
  min-height: 52px;
  padding: 12px 16px;
  background: linear-gradient(180deg, #fff, #fbfbfc);
}

body[data-surface] .erp-pane .pane-body,
body[data-surface] .app-pane .pane-body {
  height: auto;
  min-height: calc(100dvh - 190px);
  overflow: visible;
  padding: 14px;
}

body[data-surface="worker"] .integrated {
  max-width: 920px;
  margin: 0 auto;
}

body[data-surface="worker"] .app-shell {
  width: 100%;
  max-width: 820px;
}

.login-card {
  width: min(440px, 100%);
  margin: clamp(20px, 7vh, 78px) auto;
  padding: clamp(20px, 4vw, 30px);
  border: 0;
  box-shadow: 0 22px 60px rgba(31, 36, 43, 0.11);
}

.login-brand {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 18px;
}

.login-brand img {
  width: 150px;
  height: 44px;
  object-fit: contain;
  object-position: center;
}

.login-brand span,
.session-eyebrow {
  display: block;
  color: #8a9099;
  font-size: 10px;
  font-weight: 900;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.login-brand h2 {
  margin: 2px 0 0;
  font-size: 24px;
}

.login-message {
  min-height: 20px;
  margin-top: 10px;
  color: #a00000;
  font-size: 12px;
  font-weight: 700;
}

.login-help {
  margin-top: 8px;
  padding-top: 10px;
  border-top: 1px solid #eceef1;
}

.session-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
  padding: 8px 10px;
  border: 1px solid #e4e7eb;
  border-radius: 10px;
  background: #fafbfc;
}

button,
.btn,
input,
select,
textarea {
  max-width: 100%;
}

button:focus-visible,
.btn:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
a:focus-visible {
  outline: 2px solid #d90000;
  outline-offset: 2px;
  box-shadow: var(--focus-ring);
}

.btn,
.erp-nav button,
.app-nav button {
  min-height: var(--touch-target);
  touch-action: manipulation;
}

.field input,
.field select,
.field textarea {
  min-height: var(--touch-target);
}

.scroll {
  max-width: 100%;
  overscroll-behavior-x: contain;
  -webkit-overflow-scrolling: touch;
}

.scroll table {
  min-width: 720px;
}

.erp-nav,
.app-nav,
.doc-sidebar {
  scrollbar-width: thin;
  -webkit-overflow-scrolling: touch;
}

.erp-nav button,
.app-nav button,
.doc-folder-btn {
  scroll-snap-align: start;
}

.sign-placement,
.signature-pad {
  touch-action: none;
}

.modal-box {
  max-height: calc(100dvh - 24px);
}

.modal-body {
  overscroll-behavior: contain;
}

@media (max-width: 900px) {
  body[data-surface] .view {
    padding: 12px;
  }

  body[data-surface] .erp-nav,
  body[data-surface] .app-nav {
    scroll-snap-type: x proximity;
  }

  .doc-workspace {
    display: block !important;
  }

  .doc-sidebar {
    display: flex !important;
    gap: 6px;
    width: 100% !important;
    margin-bottom: 10px;
    overflow-x: auto;
    border-right: 0 !important;
    padding: 4px 0 9px !important;
  }

  .doc-sidebar-title {
    display: none;
  }

  .doc-folder-btn {
    flex: 0 0 auto;
    min-height: var(--touch-target);
    white-space: nowrap;
  }

  .doc-main {
    min-width: 0;
  }

  .doc-toolbar,
  .filterbar {
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    align-items: end;
  }

  .doc-toolbar .field,
  .filterbar .field,
  .filterbar .field.searchwide {
    min-width: 0 !important;
  }

  .worker-doc-control .scroll table {
    min-width: 1180px;
  }

  .google-map-shell,
  .no-key-map,
  .fast-map-shell {
    height: min(420px, 58dvh) !important;
    min-height: 300px;
  }
}

@media (max-width: 640px) {
  :root {
    --touch-target: 46px;
  }

  body[data-surface] .header {
    position: sticky;
    top: 0;
    min-height: 66px;
    padding-top: max(9px, env(safe-area-inset-top));
    z-index: 1000;
  }

  body[data-surface] .brand {
    gap: 8px;
  }

  body[data-surface] .brand img {
    width: 122px;
    height: 35px;
  }

  body[data-surface] .brand h1 {
    font-size: 14px;
  }

  body[data-surface] .brand p,
  body[data-surface] .badges {
    display: none;
  }

  body[data-surface] .view {
    padding: 0;
  }

  body[data-surface] .pane {
    border: 0;
    border-radius: 0;
    box-shadow: none;
  }

  body[data-surface] .pane-head {
    min-height: 46px;
    padding: 9px 12px;
  }

  body[data-surface] .erp-pane .pane-body,
  body[data-surface] .app-pane .pane-body {
    min-height: calc(100dvh - 112px);
    padding: 10px;
  }

  .login-card {
    margin: 18px auto;
    padding: 20px;
  }

  .login-brand img {
    width: 132px;
    height: 38px;
  }

  .login-brand h2 {
    font-size: 21px;
  }

  input,
  select,
  textarea,
  .field input,
  .field select,
  .field textarea {
    font-size: 16px;
  }

  .erp-nav,
  .app-nav {
    position: sticky;
    top: 65px;
    z-index: 45;
    margin: -2px -10px 10px;
    padding: 8px 10px;
    overflow-x: auto;
    background: rgba(255, 255, 255, 0.97);
    border-bottom: 1px solid #e4e7eb;
    backdrop-filter: blur(12px);
  }

  .erp-nav button,
  .app-nav button {
    flex: 0 0 auto;
    padding: 9px 12px;
  }

  .card {
    padding: 11px;
    border-radius: 11px;
  }

  .grid2,
  .grid3,
  .grid4,
  .size-grid,
  .evidence-grid-pro,
  .sign-manager-grid,
  .ref-kpis,
  .ref-main-grid,
  .ref-lower,
  .folder-grid,
  .folder-history {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  .portal-grid {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  .portal-card {
    min-height: 0;
    gap: 12px;
  }

  .doc-toolbar,
  .filterbar {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  .doc-row-card {
    grid-template-columns: minmax(0, 1fr) !important;
    gap: 8px;
  }

  .doc-row-actions,
  .doc-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    width: 100%;
  }

  .doc-row-actions .btn,
  .doc-actions .btn {
    width: 100%;
  }

  .att-cal-top {
    padding: 10px;
  }

  .att-cal-title {
    font-size: 17px !important;
  }

  .att-cal-week div {
    padding: 7px 1px;
    font-size: 9px;
  }

  .att-cal-day {
    min-height: 66px !important;
    padding: 3px !important;
  }

  .att-day-num {
    font-size: 12px !important;
  }

  .att-hours {
    margin-top: 4px !important;
    font-size: 8px !important;
  }

  .att-entry-exit,
  .att-more {
    display: none;
  }

  .sign-placement {
    min-height: 210px;
  }

  .modal {
    align-items: end;
    padding: 0;
  }

  .modal-box {
    width: 100% !important;
    max-height: calc(100dvh - env(safe-area-inset-top));
    border-radius: 18px 18px 0 0;
  }

  .modal-head {
    flex-wrap: wrap;
    gap: 8px;
  }

  .doc-frame {
    height: 68dvh;
  }

  .toast {
    right: 10px;
    bottom: max(10px, env(safe-area-inset-bottom));
    left: 10px;
    max-width: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

@media print {
  .header,
  .pane-head,
  .erp-nav,
  .app-nav,
  .session-bar,
  button,
  .btn {
    display: none !important;
  }

  body,
  body[data-surface],
  .pane,
  .card {
    background: #fff !important;
    box-shadow: none !important;
  }
}
