:root {
  color-scheme: light;
  --bg: #f4f6f4;
  --panel: #ffffff;
  --panel-strong: #f9faf7;
  --ink: #1f2623;
  --muted: #67716d;
  --line: #d9dfda;
  --teal: #0f766e;
  --teal-dark: #0b5954;
  --blue: #2454a6;
  --amber: #b7791f;
  --red: #b42318;
  --green: #247a45;
  --shadow: 0 18px 48px rgba(31, 38, 35, 0.1);
  font-family:
    "Yu Gothic UI", "Meiryo", "Noto Sans JP", system-ui, -apple-system,
    BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  color: var(--ink);
  background:
    linear-gradient(180deg, #eef4ef 0, rgba(244, 246, 244, 0) 260px),
    var(--bg);
}

button,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  width: min(1800px, 100%);
  margin: 0 auto;
  padding: 18px;
}

.topbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 18px;
  padding: 4px 2px 18px;
}

.app-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 14px;
}

.view-tab {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 14px;
  background: var(--panel-strong);
  color: var(--muted);
  font-weight: 900;
}

.view-tab.active {
  border-color: rgba(15, 118, 110, 0.42);
  background: #e8f2f1;
  color: var(--teal-dark);
}

.app-view[hidden],
.hidden {
  display: none !important;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--teal-dark);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

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

h1 {
  margin-bottom: 0;
  font-size: 28px;
  line-height: 1.15;
  letter-spacing: 0;
}

h2 {
  margin-bottom: 0;
  font-size: 15px;
  letter-spacing: 0;
}

h3 {
  margin-bottom: 10px;
  font-size: 13px;
  letter-spacing: 0;
}

.top-actions,
.panel-title-row,
.chart-tabs,
.chart-tools {
  display: flex;
  align-items: center;
  gap: 8px;
}

.top-actions {
  flex-wrap: wrap;
  justify-content: flex-end;
}

.status-chip,
.risk-badge,
.small-chip,
.zoom-chip {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  padding: 5px 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}

.risk-badge.ok {
  color: var(--green);
  border-color: rgba(36, 122, 69, 0.35);
  background: rgba(36, 122, 69, 0.08);
}

.risk-badge.warn {
  color: var(--amber);
  border-color: rgba(183, 121, 31, 0.35);
  background: rgba(183, 121, 31, 0.09);
}

.risk-badge.danger {
  color: var(--red);
  border-color: rgba(180, 35, 24, 0.35);
  background: rgba(180, 35, 24, 0.08);
}

.primary-button,
.ghost-button,
.tab-button {
  border: 1px solid transparent;
  border-radius: 7px;
  min-height: 36px;
  padding: 8px 12px;
  font-weight: 800;
  letter-spacing: 0;
}

.primary-button {
  background: var(--teal);
  color: #fff;
  box-shadow: 0 10px 22px rgba(15, 118, 110, 0.22);
}

.primary-button:hover {
  background: var(--teal-dark);
}

.ghost-button,
.tab-button {
  background: var(--panel-strong);
  border-color: var(--line);
  color: var(--ink);
}

.tab-button {
  min-height: 30px;
  padding: 5px 9px;
  color: var(--muted);
  font-size: 12px;
}

.tab-button.active {
  background: #e8f2f1;
  border-color: rgba(15, 118, 110, 0.35);
  color: var(--teal-dark);
}

.compact-button {
  min-height: 30px;
  padding: 5px 9px;
  font-size: 12px;
}

.compact-button:disabled {
  cursor: default;
  opacity: 0.48;
}

.chart-tools {
  flex-wrap: wrap;
  justify-content: flex-end;
  margin-left: auto;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(300px, 360px) minmax(520px, 1fr) minmax(300px, 380px);
  gap: 14px;
  align-items: start;
}

.compat-layout {
  display: grid;
  grid-template-columns: minmax(300px, 380px) minmax(520px, 1fr);
  gap: 14px;
  align-items: start;
}

.panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.94);
  box-shadow: var(--shadow);
}

.control-panel,
.compare-panel,
.compat-control-panel {
  position: sticky;
  top: 12px;
  max-height: calc(100vh - 24px);
  overflow: auto;
  padding: 14px;
}

.panel-title-row {
  justify-content: space-between;
  margin-bottom: 12px;
}

.control-section {
  padding: 12px 0;
  border-top: 1px solid var(--line);
}

.control-section:first-child {
  border-top: 0;
  padding-top: 0;
}

.control-section h3 {
  display: flex;
  justify-content: space-between;
  margin: 0 0 10px;
  color: #33413c;
}

.field-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.field {
  min-width: 0;
}

.field.wide {
  grid-column: 1 / -1;
}

.field label {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 5px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.unit {
  color: #84908b;
  font-weight: 700;
}

input,
select {
  width: 100%;
  min-height: 36px;
  border: 1px solid #cbd5cf;
  border-radius: 7px;
  padding: 7px 9px;
  background: #fff;
  color: var(--ink);
}

input:focus,
select:focus,
button:focus-visible {
  outline: 3px solid rgba(15, 118, 110, 0.18);
  border-color: var(--teal);
}

.sim-column {
  display: grid;
  gap: 14px;
}

.compat-results {
  display: grid;
  gap: 14px;
}

.compat-summary-panel {
  display: grid;
  grid-template-columns: 132px minmax(0, 1fr);
  gap: 16px;
  align-items: center;
  padding: 16px;
}

.compat-score-ring {
  width: 118px;
  height: 118px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: conic-gradient(var(--ring-color, var(--green)) var(--ring-score, 0%), #e8ece8 0);
  position: relative;
}

.compat-score-ring::before {
  content: "";
  position: absolute;
  inset: 12px;
  border-radius: inherit;
  background: #fff;
  border: 1px solid var(--line);
}

.compat-score-ring span {
  position: relative;
  font-size: 34px;
  font-weight: 950;
}

.compat-summary-panel p {
  margin: 8px 0 0;
  color: var(--muted);
  line-height: 1.55;
}

.compat-check-panel {
  padding: 14px;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(31, 38, 35, 0.46);
}

.modal-panel {
  width: min(1060px, 100%);
  min-width: 0;
  max-height: min(760px, calc(100vh - 36px));
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 28px 80px rgba(0, 0, 0, 0.24);
  padding: 16px;
}

.model-doc {
  display: grid;
  gap: 14px;
  min-width: 0;
  color: #33413c;
  line-height: 1.7;
}

.model-doc h3 {
  margin: 0;
  color: #1f342f;
  font-size: 15px;
  line-height: 1.35;
}

.model-doc p {
  margin-bottom: 0;
}

.model-doc pre {
  overflow: auto;
  max-width: 100%;
  min-width: 0;
  margin: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  background: var(--panel-strong);
  color: #25312d;
  font-size: 12px;
  line-height: 1.5;
}

.model-doc code {
  font-family: Consolas, "Cascadia Mono", "Yu Gothic UI", monospace;
  font-size: 0.95em;
}

.model-doc a {
  color: var(--blue);
  font-weight: 800;
  text-decoration: none;
}

.model-doc a:hover {
  text-decoration: underline;
}

.model-note {
  border: 1px solid rgba(15, 118, 110, 0.22);
  border-radius: 8px;
  padding: 10px 12px;
  background: #f3faf8;
  color: #244640;
}

.model-link-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.model-link-row a {
  display: inline-flex;
  align-items: center;
  min-height: 32px;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 6px 10px;
  background: var(--panel-strong);
}

.model-section {
  display: grid;
  gap: 10px;
  min-width: 0;
  border-top: 1px solid var(--line);
  padding-top: 14px;
}

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

.compact-equations {
  grid-template-columns: 1fr;
}

.equation {
  display: grid;
  grid-template-columns: minmax(110px, max-content) 18px minmax(0, 1fr);
  gap: 8px;
  align-items: baseline;
  min-width: 0;
  border: 1px solid #dce4df;
  border-radius: 8px;
  padding: 9px 10px;
  background: #fbfcfa;
  color: #20342f;
  font-family: "Cambria Math", "Times New Roman", "Yu Gothic UI", serif;
  font-size: 15px;
  line-height: 1.45;
}

.equation span {
  min-width: 0;
  overflow-wrap: anywhere;
}

.equation var {
  font-style: italic;
}

.model-source {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.model-source summary {
  cursor: pointer;
  overflow-wrap: anywhere;
  padding: 9px 10px;
  color: #25312d;
  font-size: 12px;
  font-weight: 900;
}

.model-source pre {
  border-width: 1px 0 0;
  border-radius: 0 0 8px 8px;
  background: #f6f8f5;
}

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

.metric-card {
  min-height: 92px;
  padding: 13px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 10px 32px rgba(31, 38, 35, 0.07);
}

.metric-card .label {
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.metric-card .value {
  font-size: 24px;
  font-weight: 900;
  line-height: 1.05;
  letter-spacing: 0;
}

.metric-card .sub {
  margin-top: 8px;
  color: var(--muted);
  font-size: 12px;
}

.metric-card.warn {
  border-color: rgba(183, 121, 31, 0.35);
  background: #fffaf0;
}

.metric-card.danger {
  border-color: rgba(180, 35, 24, 0.35);
  background: #fff5f4;
}

.visual-row {
  display: grid;
  grid-template-columns: minmax(280px, 0.8fr) minmax(260px, 1fr);
  gap: 14px;
}

.vector-panel,
.log-panel,
.chart-panel,
.table-panel {
  padding: 14px;
}

canvas {
  display: block;
  width: 100%;
  background: #fbfcfb;
  border: 1px solid #e2e7e3;
  border-radius: 7px;
}

#vectorCanvas {
  aspect-ratio: 19 / 15;
}

#chartCanvas {
  aspect-ratio: 92 / 33;
  cursor: crosshair;
  touch-action: none;
}

.warnings {
  display: grid;
  gap: 9px;
}

.warning-item {
  padding: 10px;
  border-left: 4px solid var(--line);
  border-radius: 7px;
  background: var(--panel-strong);
  color: #33413c;
  font-size: 13px;
  line-height: 1.45;
}

.warning-item.warn {
  border-left-color: var(--amber);
  background: #fffaf0;
}

.warning-item.danger {
  border-left-color: var(--red);
  background: #fff5f4;
}

.warning-item.ok {
  border-left-color: var(--green);
  background: #f1fbf4;
}

.table-scroll {
  max-height: 260px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 7px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}

th,
td {
  padding: 8px 9px;
  border-bottom: 1px solid #e7ebe7;
  text-align: right;
  white-space: nowrap;
}

th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: #f8faf8;
  color: var(--muted);
  font-size: 11px;
}

th:first-child,
td:first-child {
  text-align: left;
}

.driver-cards {
  display: grid;
  gap: 10px;
}

.driver-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 11px;
  background: #fff;
}

.driver-card.selected {
  border-color: rgba(15, 118, 110, 0.5);
  box-shadow: inset 0 0 0 1px rgba(15, 118, 110, 0.18);
}

.driver-card h3 {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
}

.driver-card dl {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 5px 10px;
  margin: 0;
  color: var(--muted);
  font-size: 12px;
}

.driver-card dd {
  margin: 0;
  color: var(--ink);
  font-weight: 800;
  text-align: right;
}

.score-bar {
  height: 7px;
  margin-top: 10px;
  overflow: hidden;
  border-radius: 999px;
  background: #e8ece8;
}

.score-fill {
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--red), var(--amber), var(--green));
}

.source-box {
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

.source-box a {
  display: block;
  margin: 7px 0;
  color: var(--blue);
  font-size: 12px;
  font-weight: 800;
  text-decoration: none;
}

.source-box a:hover {
  text-decoration: underline;
}

@media (max-width: 1240px) {
  .workspace {
    grid-template-columns: 320px minmax(0, 1fr);
  }

  .compat-layout {
    grid-template-columns: 320px minmax(0, 1fr);
  }

  .compare-panel {
    position: static;
    grid-column: 1 / -1;
    max-height: none;
  }

  .driver-cards {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 860px) {
  .app-shell {
    padding: 12px;
  }

  .topbar,
  .workspace,
  .compat-layout,
  .visual-row {
    display: grid;
    grid-template-columns: 1fr;
  }

  .top-actions {
    justify-content: start;
  }

  .control-panel,
  .compat-control-panel {
    position: static;
    max-height: none;
  }

  .metric-grid,
  .driver-cards,
  .equation-grid {
    grid-template-columns: 1fr;
  }

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

  .modal-backdrop {
    align-items: stretch;
    padding: 10px;
  }

  .modal-panel {
    max-height: calc(100vh - 20px);
    padding: 13px;
  }

  .equation {
    grid-template-columns: minmax(88px, max-content) 16px minmax(0, 1fr);
    font-size: 14px;
  }

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

  h1 {
    font-size: 23px;
  }
}
