:root {
  --bg: #f5f7f8;
  --surface: #ffffff;
  --line: #d9e0e4;
  --text: #172126;
  --muted: #63717a;
  --nav: #172126;
  --nav-soft: #223039;
  --teal: #0d9488;
  --blue: #2563eb;
  --amber: #b7791f;
  --red: #b42318;
  --green: #15803d;
  --shadow: 0 16px 38px rgba(30, 42, 52, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  color: var(--text);
  background: var(--bg);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  letter-spacing: 0;
}

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

button {
  cursor: pointer;
}

[hidden] {
  display: none !important;
}

.app-shell {
  display: grid;
  min-height: 100vh;
  grid-template-columns: 260px 1fr;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  padding: 20px 16px;
  color: #e6edf0;
  background: var(--nav);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 8px 24px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
}

.brand-mark {
  display: grid;
  width: 38px;
  height: 38px;
  place-items: center;
  border-radius: 8px;
  background: #e6edf0;
  color: var(--nav);
  font-weight: 800;
}

.brand strong,
.brand span {
  display: block;
}

.brand span {
  margin-top: 2px;
  color: #a9b7bf;
  font-size: 13px;
}

.nav {
  display: grid;
  gap: 6px;
  margin-top: 20px;
}

.nav-item {
  width: 100%;
  padding: 11px 12px;
  color: #cbd5da;
  text-align: left;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
}

.nav-item:hover,
.nav-item.active {
  color: #fff;
  background: var(--nav-soft);
  border-color: rgba(255, 255, 255, 0.1);
}

.role-box {
  position: absolute;
  right: 16px;
  bottom: 20px;
  left: 16px;
  display: grid;
  gap: 8px;
  padding: 12px;
  border-radius: 8px;
  background: var(--nav-soft);
}

.session-box {
  position: absolute;
  right: 16px;
  bottom: 20px;
  left: 16px;
  display: grid;
  gap: 8px;
  padding: 12px;
  border-radius: 8px;
  background: var(--nav-soft);
}

.session-box span { color: #a9b7bf; font-size: 13px; }

.login-page { display: grid; min-height: 100vh; place-items: center; padding: 24px; background: #eef3f4; }

.login-panel {
  display: grid;
  width: min(100%, 400px);
  gap: 16px;
  padding: 28px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.login-brand { padding: 0 0 16px; color: var(--text); border-bottom: 1px solid var(--line); }
.login-brand span { color: var(--muted); }
.form-error { min-height: 1.2em; margin: 0; color: var(--red); font-size: 13px; }
.form-error.success-message { color: var(--green); }
.form-import-actions { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.form-import-actions small { color: var(--muted); font-size: 13px; }
.workbench-list { display: grid; gap: 10px; }

.role-box label {
  color: #a9b7bf;
  font-size: 12px;
}

.role-box select {
  width: 100%;
  color: #fff;
  background: #0f171c;
  border: 1px solid rgba(255, 255, 255, 0.16);
}

.main {
  min-width: 0;
  padding: 26px;
}

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

.eyebrow {
  margin: 0 0 4px;
  color: var(--teal);
  font-size: 13px;
  font-weight: 700;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: 28px;
  line-height: 1.2;
}

h2 {
  margin-bottom: 0;
  font-size: 18px;
  line-height: 1.25;
}

.top-actions,
.button-row,
.inline-form {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.company-switcher {
  min-width: 250px;
  color: var(--muted);
  font-size: 12px;
}

.company-switcher select,
.company-switcher input {
  margin-top: 5px;
  font-weight: 700;
}

.company-controls {
  display: grid;
  grid-template-columns: minmax(280px, 1fr);
  gap: 10px;
  align-items: end;
}

.company-result-count {
  color: var(--muted);
  font-size: 12px;
}

.current-company-indicator {
  color: var(--ink);
  font-size: 12px;
  font-weight: 700;
}

.company-search-results {
  display: grid;
  gap: 6px;
  max-height: 310px;
  overflow: auto;
}

.company-search-result {
  display: grid;
  gap: 3px;
  width: 100%;
  padding: 10px 12px;
  text-align: left;
  color: var(--ink);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 6px;
  cursor: pointer;
}

.company-search-result:hover,
.company-search-result:focus-visible {
  border-color: var(--teal);
  background: #f1fbf9;
  outline: none;
}

.company-search-result small {
  color: var(--muted);
  font-weight: 400;
}

.no-company-selected > :not(.panel-head) {
  display: none;
}

.section {
  display: none;
}

.section.active {
  display: grid;
  gap: 20px;
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(150px, 1fr));
  gap: 14px;
}

.kpi {
  min-height: 112px;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.kpi span {
  color: var(--muted);
  font-size: 13px;
}

.kpi strong {
  display: block;
  margin-top: 10px;
  font-size: 28px;
}

.kpi small {
  display: block;
  margin-top: 6px;
  color: var(--muted);
}

.content-grid {
  display: grid;
  gap: 20px;
}

.two-col {
  grid-template-columns: minmax(0, 1.05fr) minmax(360px, 0.95fr);
}

.panel {
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 16px;
}

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: 4px 10px;
  border-radius: 999px;
  color: #fff;
  background: var(--blue);
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}

.badge.neutral {
  color: var(--text);
  background: #e7ecef;
}

.badge.teal {
  background: var(--teal);
}

.badge.warn {
  background: var(--amber);
}

.badge.success {
  background: var(--green);
}

.badge.danger {
  background: var(--red);
}

.primary-btn,
.ghost-btn,
.danger-btn,
.icon-btn {
  min-height: 38px;
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid transparent;
  font-weight: 700;
}

.primary-btn {
  color: #fff;
  background: var(--teal);
}

.ghost-btn {
  color: var(--text);
  background: #eef3f5;
  border-color: #d8e1e5;
}

.danger-btn {
  color: #fff;
  background: var(--red);
}

.icon-btn {
  width: 38px;
  padding: 0;
  color: var(--text);
  background: #eef3f5;
  border-color: #d8e1e5;
}

.primary-btn:disabled,
.ghost-btn:disabled,
.danger-btn:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.summary-list,
.task-list,
.document-list,
.audit-log,
.company-portfolio {
  display: grid;
  gap: 10px;
}

.summary-row,
.task-row,
.document-row,
.audit-row,
.company-row {
  display: grid;
  gap: 4px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfc;
}

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

.summary-row span:first-child,
.task-row small,
.document-row small,
.audit-row small {
  color: var(--muted);
}

.task-row {
  grid-template-columns: 1fr auto;
  align-items: center;
}

.company-row {
  grid-template-columns: minmax(220px, 1fr) auto auto;
  align-items: center;
}

.company-row.is-active {
  border-color: var(--teal);
  background: #f1fbf9;
}

.company-metric {
  display: grid;
  justify-items: end;
  gap: 5px;
}

.company-metric small {
  color: var(--muted);
}

.task-row strong,
.document-row strong,
.audit-row strong {
  font-size: 14px;
}

.material-bars {
  display: grid;
  gap: 14px;
}

.material-bar {
  display: grid;
  grid-template-columns: 130px 1fr 86px;
  gap: 12px;
  align-items: center;
}

.bar-track {
  height: 12px;
  overflow: hidden;
  border-radius: 999px;
  background: #e6ecef;
}

.bar-fill {
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--teal), var(--blue));
}

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

.form-grid .full {
  grid-column: 1 / -1;
}

label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

input,
select,
textarea {
  width: 100%;
  min-height: 40px;
  padding: 9px 10px;
  color: var(--text);
  border: 1px solid #cdd7dc;
  border-radius: 8px;
  background: #fff;
}

textarea {
  resize: vertical;
}

fieldset {
  margin: 0;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.quantity-group {
  display: grid;
  gap: 12px;
}

legend {
  padding: 0 6px;
  color: var(--muted);
  font-weight: 700;
}

.checkbox-group {
  display: flex;
  gap: 18px;
  flex-wrap: wrap;
}

.checkbox-group label {
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--text);
}

.checkbox-group input {
  width: 16px;
  min-height: 16px;
}

.material-inputs {
  display: grid;
  grid-template-columns: repeat(4, minmax(120px, 1fr));
  gap: 12px;
}

.material-inputs.two-materials {
  grid-template-columns: repeat(2, minmax(120px, 1fr));
}

.inline-form {
  justify-content: flex-end;
}

.inline-form select {
  width: 170px;
}

.inline-form input {
  width: min(280px, 44vw);
}

.operation-buttons {
  display: grid;
  grid-template-columns: repeat(2, minmax(130px, 1fr));
  gap: 10px;
  margin-bottom: 18px;
}

.note-form {
  display: grid;
  gap: 12px;
}

dialog {
  width: min(860px, calc(100vw - 32px));
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.2);
}

dialog::backdrop {
  background: rgba(10, 18, 24, 0.4);
}

.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

#jsonOutput {
  min-height: 460px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
}

.hidden-for-role {
  opacity: 0.42;
  pointer-events: none;
}

@media (max-width: 980px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: static;
    height: auto;
  }

  .role-box {
    position: static;
    margin-top: 20px;
  }

  .kpi-grid,
  .two-col,
  .form-grid,
  .material-inputs {
    grid-template-columns: 1fr;
  }

  .summary-row,
  .task-row,
  .material-bar,
  .company-row {
    grid-template-columns: 1fr;
  }

  .company-metric {
    justify-items: start;
  }
}

@media (max-width: 620px) {
  .main {
    padding: 18px;
  }

  .topbar,
  .panel-head {
    align-items: flex-start;
    flex-direction: column;
  }

  .inline-form,
  .top-actions,
  .button-row {
    width: 100%;
  }

  .inline-form input,
  .inline-form select,
  .company-switcher,
  .primary-btn,
  .ghost-btn,
  .danger-btn {
    width: 100%;
  }

  .company-controls {
    width: 100%;
    grid-template-columns: 1fr;
  }
}
