:root {
  --bg: #f6f8fb;
  --surface: #ffffff;
  --surface-soft: #f8fafc;
  --ink: #17202a;
  --muted: #5b6777;
  --line: #d8e0ea;
  --line-strong: #b8c3d1;
  --primary: #0f766e;
  --primary-strong: #0b5f59;
  --nav: #0f172a;
  --nav-hover: #1f2a44;
  --accent: #b45309;
  --focus: #f59e0b;
  --danger-bg: #fee2e2;
  --danger-ink: #991b1b;
  --ok-bg: #d1fae5;
  --ok-ink: #065f46;
  --shadow: 0 20px 45px rgba(15, 23, 42, 0.16);
}

* { box-sizing: border-box; }

html {
  min-width: 320px;
  background: var(--bg);
}

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
  color: var(--ink);
  background: var(--bg);
  overflow-x: hidden;
}

button,
input,
select,
textarea {
  font: inherit;
}

button,
input,
select,
textarea,
a {
  outline-offset: 3px;
}

button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
a:focus-visible,
.table-wrap:focus-visible,
.content:focus-visible {
  outline: 3px solid var(--focus);
}

button {
  min-height: 42px;
  border: 1px solid transparent;
  background: var(--primary);
  color: white;
  padding: 9px 14px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 700;
}

button:hover {
  background: var(--primary-strong);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.6;
}

button.muted,
.nav.muted {
  background: transparent;
  color: #d4dce8;
}

input,
select,
textarea {
  min-width: 0;
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  padding: 9px 10px;
  background: white;
  color: var(--ink);
}

textarea {
  resize: vertical;
  font-family: Consolas, "Courier New", monospace;
  font-size: 13px;
  line-height: 1.45;
}

table {
  width: 100%;
  min-width: max-content;
  border-collapse: collapse;
  font-size: 13px;
  background: white;
}

th,
td {
  border-bottom: 1px solid #e2e8f0;
  padding: 10px;
  text-align: left;
  white-space: nowrap;
}

th {
  position: sticky;
  top: 0;
  background: var(--surface-soft);
  z-index: 1;
  font-weight: 700;
}

pre {
  max-width: 100%;
  min-height: 120px;
  overflow: auto;
  border-radius: 6px;
  background: #101827;
  color: #e5eefb;
  padding: 12px;
  white-space: pre-wrap;
  word-break: break-word;
}

details {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
  padding: 10px 12px;
}

summary {
  cursor: pointer;
  font-weight: 700;
}

.hidden {
  display: none !important;
}

.skip-link {
  position: fixed;
  top: 10px;
  left: 10px;
  z-index: 100;
  transform: translateY(-160%);
  border-radius: 6px;
  background: #111827;
  color: white;
  padding: 10px 12px;
  text-decoration: none;
}

.skip-link:focus {
  transform: translateY(0);
}

.login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
}

.login-panel {
  width: min(420px, 100%);
  display: grid;
  gap: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  padding: 26px;
  box-shadow: 0 10px 30px rgba(15, 23, 42, 0.08);
}

.login-panel h1 {
  margin: 0 0 8px;
  font-size: 28px;
  line-height: 1.15;
  letter-spacing: 0;
}

.login-panel label,
.grid-form label,
.panel label {
  display: grid;
  gap: 6px;
  color: #475569;
}

.login-panel label,
.panel label {
  margin-bottom: 10px;
}

.error {
  min-height: 20px;
  margin: 0;
  color: #b91c1c;
}

.app {
  min-height: 100vh;
  width: 100%;
  display: grid;
  grid-template-columns: 232px minmax(0, 1fr);
  overflow-x: hidden;
}

.mobile-shellbar,
.nav-backdrop {
  display: none;
}

.sidebar {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--nav);
  color: white;
  padding: 18px 12px;
}

.brand {
  padding: 8px 10px 18px;
  font-size: 18px;
  font-weight: 700;
}

.nav-list {
  display: grid;
  gap: 8px;
}

.nav {
  width: 100%;
  min-height: 42px;
  text-align: left;
  background: transparent;
  color: #cbd5e1;
}

.nav.active,
.nav[aria-current="page"],
.nav:hover {
  background: var(--nav-hover);
  color: white;
}

#logout {
  margin-top: auto;
}

.content {
  min-width: 0;
  width: 100%;
  max-width: 100vw;
  padding: 18px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 16px;
}

.topbar h2 {
  margin: 0 0 6px;
  font-size: 24px;
  line-height: 1.2;
  letter-spacing: 0;
}

.pill,
.status-badge {
  display: inline-flex;
  align-items: center;
  max-width: 100%;
  min-height: 24px;
  border-radius: 999px;
  padding: 3px 9px;
  background: #eef2ff;
  color: #3730a3;
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}

.pill.is-ok {
  background: var(--ok-bg);
  color: var(--ok-ink);
}

.pill.is-error {
  background: var(--danger-bg);
  color: var(--danger-ink);
}

.loading-state,
.app-alert {
  width: 100%;
  margin: 0 0 12px;
  border-radius: 8px;
  padding: 10px 12px;
}

.loading-state {
  display: flex;
  align-items: center;
  gap: 10px;
  border: 1px solid #bfdbfe;
  background: #eff6ff;
  color: #1e3a8a;
  font-weight: 700;
}

.spinner {
  width: 18px;
  height: 18px;
  border: 3px solid #93c5fd;
  border-top-color: #1d4ed8;
  border-radius: 999px;
  animation: spin 0.8s linear infinite;
}

.app-alert {
  border: 1px solid #fecaca;
  background: #fff1f2;
  color: #9f1239;
}

.dashboard-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 14px;
  margin-bottom: 16px;
}

.module-card {
  min-height: 112px;
  display: grid;
  align-content: space-between;
  gap: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  color: var(--ink);
  padding: 16px;
  text-align: left;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.06);
}

.module-card:hover {
  border-color: #95b8c2;
  background: #f8fbfb;
  color: var(--ink);
}

.module-title {
  font-size: 18px;
  font-weight: 700;
}

.module-meta {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.shell-state-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  margin-bottom: 12px;
}

.toolbar > input,
.toolbar > select {
  flex: 1 1 190px;
}

.toolbar > button {
  flex: 0 1 auto;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  min-width: 190px;
}

.inline-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.inline-actions > button {
  flex: 0 1 auto;
}

.table-wrap {
  max-width: 100%;
  max-height: calc(100vh - 190px);
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
}

.table-wrap.compact {
  max-height: 520px;
}

.table-state {
  min-height: 74px;
  padding: 22px;
  color: var(--muted);
  text-align: center;
}

.split {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(320px, 430px);
  gap: 16px;
  align-items: start;
}

.panel {
  max-width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  padding: 14px;
}

.panel h3,
.panel h4 {
  margin: 0 0 12px;
  line-height: 1.2;
  letter-spacing: 0;
}

.panel.wide {
  max-width: 900px;
}

.record-detail {
  display: grid;
  gap: 12px;
  margin-top: 14px;
  border-top: 1px solid var(--line);
  padding-top: 14px;
}

.summary-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}

.summary-tile {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
  padding: 12px;
}

.summary-tile span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.summary-tile strong {
  display: block;
  margin-top: 4px;
  overflow-wrap: anywhere;
}

.analytics-layout {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
  align-items: start;
}

.analytics-layout > .wide {
  grid-column: 1 / -1;
  max-width: none;
}

.analytics-kpis {
  grid-template-columns: repeat(5, minmax(0, 1fr));
}

.analytics-table {
  margin-top: 12px;
}

.inline-form {
  display: grid;
  grid-template-columns: minmax(120px, 160px) minmax(160px, 1fr) auto auto;
  gap: 8px;
  align-items: center;
  margin: 12px 0;
}

.view-message {
  max-width: 760px;
  border: 1px solid #bfdbfe;
  border-radius: 8px;
  background: #eff6ff;
  color: #1e3a8a;
  padding: 12px;
  font-weight: 700;
}

.stack {
  display: grid;
  gap: 10px;
  margin-top: 14px;
}

.split > .stack {
  margin-top: 0;
}

.grid-form {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 12px;
}

.detail-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  margin: 12px 0;
}

.state-panel p {
  margin: 0;
  color: var(--muted);
  font-weight: 700;
}

select[multiple] {
  min-height: 118px;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

@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 (min-width: 1440px) {
  .app {
    grid-template-columns: 248px minmax(0, 1fr);
  }

  .content {
    padding: 24px;
  }

  .dashboard-grid {
    grid-template-columns: repeat(4, minmax(220px, 1fr));
  }

  .analytics-kpis {
    grid-template-columns: repeat(5, minmax(180px, 1fr));
  }
}

@media (max-width: 1180px) {
  .dashboard-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .split {
    grid-template-columns: 1fr;
  }

  .panel.wide {
    max-width: none;
  }

  .analytics-layout {
    grid-template-columns: 1fr;
  }

  .analytics-kpis {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

@media (max-width: 960px) {
  body.nav-locked {
    overflow: hidden;
  }

  .app {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr);
  }

  .mobile-shellbar {
    position: sticky;
    top: 0;
    z-index: 40;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    min-width: 0;
    border-bottom: 1px solid var(--line);
    background: white;
    padding: 10px 12px;
  }

  .mobile-brand {
    min-width: 0;
    overflow: hidden;
    font-weight: 700;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .mobile-status {
    width: 42px;
  }

  .icon-button {
    width: 42px;
    min-width: 42px;
    padding: 8px;
    display: inline-grid;
    place-items: center;
    background: #111827;
  }

  .menu-lines {
    width: 18px;
    display: grid;
    gap: 4px;
  }

  .menu-lines span {
    display: block;
    height: 2px;
    border-radius: 999px;
    background: white;
  }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 60;
    width: min(286px, calc(100vw - 32px));
    transform: translateX(-105%);
    overflow-y: auto;
    box-shadow: var(--shadow);
    transition: transform 0.18s ease;
  }

  .app[data-nav-open="true"] .sidebar {
    transform: translateX(0);
  }

  .app[data-nav-open="true"] .nav-backdrop {
    position: fixed;
    inset: 0;
    z-index: 50;
    display: block;
    background: rgba(15, 23, 42, 0.45);
  }

  .nav-backdrop[hidden] {
    display: none !important;
  }

  .content {
    max-width: 100%;
    padding: 14px 12px 20px;
  }

  .topbar {
    align-items: stretch;
    flex-wrap: wrap;
  }

  .topbar > div {
    min-width: 0;
    flex: 1 1 180px;
  }

  .topbar > button {
    flex: 0 0 auto;
  }

  .table-wrap {
    max-height: calc(100svh - 220px);
  }
}

@media (max-width: 640px) {
  .login-view {
    padding: 12px;
  }

  .login-panel {
    padding: 20px;
  }

  .dashboard-grid,
  .shell-state-grid,
  .grid-form,
  .detail-grid {
    grid-template-columns: 1fr;
  }

  .module-card {
    min-height: 94px;
  }

  .toolbar {
    align-items: stretch;
    flex-direction: column;
  }

  .toolbar > input,
  .toolbar > select,
  .toolbar > button {
    width: 100%;
    flex-basis: auto;
  }

  .actions {
    min-width: 0;
  }

  .actions > button {
    flex: 1 1 112px;
  }

  .panel,
  .loading-state,
  .app-alert {
    border-radius: 8px;
  }

  .summary-grid {
    grid-template-columns: 1fr;
  }

  .analytics-kpis,
  .inline-form {
    grid-template-columns: 1fr;
  }

  .mobile-workflow .toolbar,
  .mobile-workflow .inline-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .mobile-workflow button,
  .mobile-workflow input,
  .mobile-workflow select,
  .mobile-workflow textarea {
    width: 100%;
  }
}
