* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  color: #1a1a1a;
  background: #fff;
  line-height: 1.5;
}

header {
  padding: 1.5rem 2rem 0;
  background: #fff;
  border-bottom: 1px solid #ddd;
}

.header-top {
  margin-bottom: 0.75rem;
}

header h1 {
  font-size: 1.4rem;
  font-weight: 600;
}

.header-note {
  color: #888;
  font-size: 0.8rem;
  margin-top: 0.2rem;
}

.tabs {
  display: flex;
  gap: 0;
}

.tab {
  padding: 0.5rem 1.25rem;
  border: none;
  background: none;
  font-size: 0.85rem;
  font-weight: 500;
  color: #666;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  transition: color 0.15s, border-color 0.15s;
}

.tab:hover {
  color: #333;
}

.tab.active {
  color: #1a1a1a;
  border-bottom-color: #2563eb;
}

main {
  display: grid;
  grid-template-columns: 280px 1fr;
  gap: 1px;
  background: #ddd;
  min-height: calc(100vh - 80px);
  animation: fadeIn 0.6s ease-out;
  transition: opacity 0.3s linear;
}

main.fading-out {
  opacity: 0;
}

main[hidden] {
  display: none;
}

@keyframes fadeIn {
  from { opacity: 0; }
}

.inputs, .results {
  background: #fff;
  padding: 1.5rem;
}

.results > * {
  max-width: 720px;
}

.inputs h2, .results h2 {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 1rem;
}

fieldset {
  border: none;
  margin-bottom: 1.25rem;
}

legend {
  font-weight: 600;
  font-size: 0.85rem;
  color: #444;
  margin-bottom: 0.5rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

label {
  display: block;
  margin-bottom: 0.5rem;
  font-size: 0.85rem;
  color: #555;
}

input[type="number"] {
  display: block;
  width: 100%;
  padding: 0.35rem 0.5rem;
  margin-top: 0.15rem;
  border: 1px solid #ccc;
  border-radius: 3px;
  font-size: 0.9rem;
  font-family: "SF Mono", "Fira Code", monospace;
}

input[type="number"]:focus,
select:focus {
  outline: none;
  border-color: #4a9eff;
  box-shadow: 0 0 0 2px rgba(74, 158, 255, 0.15);
}

select {
  display: block;
  width: 100%;
  padding: 0.35rem 0.5rem;
  margin-top: 0.15rem;
  border: 1px solid #ccc;
  border-radius: 3px;
  font-size: 0.9rem;
  font-family: "SF Mono", "Fira Code", monospace;
  background: #fff;
}

/* Results */

.scalars, .states, .charts, .recup-profile, .scalars-row {
  margin-bottom: 1.5rem;
}

.scalars-row {
  display: flex;
  gap: 4.5rem;
  align-items: flex-start;
}

.scalars-row .scalars {
  margin-bottom: 0;
}

h3 {
  font-size: 0.9rem;
  font-weight: 600;
  margin-bottom: 0.75rem;
  color: #333;
}

table {
  border-collapse: collapse;
  font-size: 0.85rem;
}

#s-state-table, #r-state-table {
  width: max(100%, 600px);
}

th, td {
  text-align: left;
  padding: 0.3rem 0.5rem;
  border-bottom: 1px solid #eee;
  white-space: nowrap;
}

th {
  font-weight: 600;
  color: #444;
  background: #fafafa;
}

#s-scalar-table, .scalar-table {
  width: 50%;
}

#s-scalar-table td:nth-child(2),
.scalar-table td:nth-child(2) {
  font-family: "SF Mono", "Fira Code", monospace;
  text-align: right;
}

#s-scalar-table td:nth-child(3),
.scalar-table td:nth-child(3) {
  color: #888;
  font-size: 0.8rem;
  width: 60px;
}

.sub-header {
  font-weight: 600;
  font-size: 0.8rem;
  color: #444;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding-top: calc(1.25rem + 5px) !important;
}

tr:first-child > .sub-header {
  padding-top: calc(0.5rem - 3px) !important;
}

#s-state-table th,
#r-state-table th {
  text-align: right;
}

#s-state-table th:first-child,
#s-state-table th:nth-child(2),
#r-state-table th:first-child,
#r-state-table th:nth-child(2) {
  text-align: left;
}

#s-state-table td,
#r-state-table td {
  font-family: "SF Mono", "Fira Code", monospace;
  text-align: right;
}

#s-state-table td:first-child,
#s-state-table td:nth-child(2),
#r-state-table td:first-child,
#r-state-table td:nth-child(2) {
  font-family: inherit;
  text-align: left;
}

.error {
  background: #fff0f0;
  border: 1px solid #ffcccc;
  border-radius: 4px;
  padding: 0.75rem 1rem;
  color: #cc0000;
  font-size: 0.85rem;
  margin-bottom: 1rem;
}

/* Charts */

.chart-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1.5rem;
}

.chart-container {
  min-height: 0;
  min-width: 0;
  overflow: hidden;
}

/* Responsive: stack layout on narrow screens */

@media (max-width: 640px) {
  body {
    overflow-x: hidden;
  }

  main {
    display: block;
    background: #fff;
  }

  main[hidden] {
    display: none;
  }

  .scalars-row {
    flex-direction: column;
    gap: 0;
  }

  .inputs {
    border-bottom: 1px solid #ddd;
  }

  .inputs label,
  .inputs input,
  .inputs select {
    max-width: 300px;
  }

  .results > * {
    max-width: none;
  }

  #scalar-table {
    width: 100%;
  }

  .states-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
}

/* Loading overlay */

.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(255, 255, 255, 0.92);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  transition: opacity 0.3s ease;
}

.loading-overlay[hidden] {
  display: none;
}

.loading-content {
  text-align: center;
  color: #555;
  font-size: 0.9rem;
}

.loading-spinner {
  width: 28px;
  height: 28px;
  border: 3px solid #ddd;
  border-top-color: #2563eb;
  border-radius: 50%;
  margin: 0 auto 0.75rem;
  animation: spin 0.8s linear infinite;
}

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

@media (max-width: 480px) {
  header {
    padding: 1rem;
  }

  .inputs, .results {
    padding: 1rem;
  }
}
