:root {
  color-scheme: light;
  --bg: #f7f4ee;
  --ink: #18212b;
  --muted: #66717d;
  --panel: #ffffff;
  --line: #d7dce0;
  --curve: #176b87;
  --accent: #d95f30;
  --sum: #2d8a55;
  --warn: #9f3f3f;
  --shadow: 0 18px 48px rgba(24, 33, 43, 0.12);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font-family: "Yu Gothic UI", "Hiragino Sans", system-ui, sans-serif;
}

button,
input {
  font: inherit;
}

button {
  border: 1px solid #bbc6ca;
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  min-height: 38px;
  padding: 0 12px;
  cursor: pointer;
}

button:hover {
  border-color: var(--curve);
}

button.active,
.button-row button:first-child {
  background: var(--ink);
  border-color: var(--ink);
  color: #fff;
}

input {
  width: 100%;
  border: 1px solid #bac5cb;
  border-radius: 8px;
  padding: 8px 10px;
  background: #fff;
  color: var(--ink);
}

.app {
  width: min(1480px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 22px 0 32px;
}

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

.eyebrow {
  margin: 0 0 4px;
  color: var(--accent);
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  letter-spacing: 0;
}

h1 {
  font-size: clamp(28px, 4vw, 46px);
  line-height: 1.08;
}

h2 {
  font-size: 17px;
}

.status {
  min-width: 180px;
  max-width: 42vw;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 10px 12px;
  color: var(--muted);
  text-align: right;
}

.layout {
  display: grid;
  grid-template-columns: minmax(280px, 360px) 1fr;
  gap: 18px;
  align-items: start;
}

.controls {
  display: grid;
  gap: 14px;
}

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

.panel {
  display: grid;
  gap: 13px;
  padding: 16px;
}

.equation {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 8px;
  align-items: center;
  font-size: 18px;
  font-weight: 700;
}

.two-col,
.button-row,
.mode-toggle {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.coefficient-grid {
  display: grid;
  grid-template-columns: minmax(82px, 0.58fr) 1fr;
  gap: 8px 10px;
  align-items: end;
}

.coefficient-grid input[type="range"] {
  align-self: center;
  padding: 0;
}

.toggle-row {
  display: flex;
}

.toggle {
  display: inline-flex;
  width: 100%;
  min-height: 38px;
  align-items: center;
  gap: 8px;
  border: 1px solid #bac5cb;
  border-radius: 8px;
  background: #fff;
  padding: 8px 10px;
  color: var(--ink);
}

.toggle input {
  width: auto;
  margin: 0;
}

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

.presets,
.prime-presets {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.prime-presets {
  grid-template-columns: repeat(4, 1fr);
}

.presets button,
.prime-presets button {
  min-width: 0;
  padding: 0 8px;
}

.hint {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.65;
}

.readout {
  display: grid;
  gap: 8px;
  font-size: 13px;
}

.readout div {
  display: grid;
  grid-template-columns: 76px 1fr;
  gap: 8px;
  align-items: start;
}

.readout span {
  color: var(--muted);
  font-weight: 700;
}

.readout strong {
  overflow-wrap: anywhere;
}

.calculation {
  border-left: 3px solid var(--sum);
  background: #f2faf4;
  border-radius: 0 8px 8px 0;
  color: #30533c;
  font-size: 13px;
  line-height: 1.65;
  padding: 9px 11px;
}

.formula-steps {
  border: 1px solid #d8dde0;
  border-radius: 8px;
  background: #fbfcfd;
  color: #26323c;
  font-family: "Cascadia Mono", "Consolas", monospace;
  font-size: 12px;
  line-height: 1.7;
  overflow-wrap: anywhere;
  padding: 10px 11px;
  white-space: pre-wrap;
}

.discriminant-card {
  display: grid;
  gap: 5px;
  border: 1px solid #d8dde0;
  border-left: 4px solid var(--curve);
  border-radius: 8px;
  background: #fbfcfd;
  padding: 10px 11px;
  font-size: 13px;
  line-height: 1.55;
}

.discriminant-card strong {
  color: var(--ink);
}

.discriminant-card span {
  color: var(--muted);
  overflow-wrap: anywhere;
}

.discriminant-card.smooth {
  border-left-color: var(--sum);
  background: #f2faf4;
}

.discriminant-card.near {
  border-left-color: #c58c21;
  background: #fff8e7;
}

.discriminant-card.singular {
  border-left-color: var(--warn);
  background: #fff1f0;
}

.canvas-panel {
  min-width: 0;
  overflow: hidden;
}

.canvas-head {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: start;
  padding: 16px 18px 12px;
  border-bottom: 1px solid var(--line);
}

.canvas-head p {
  margin: 6px 0 0;
  color: var(--muted);
  line-height: 1.55;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: end;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.legend span {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.legend i {
  width: 22px;
  height: 4px;
  border-radius: 99px;
  display: inline-block;
}

.legend .curve {
  background: var(--curve);
}

.legend .line {
  background: var(--accent);
}

.legend .sum {
  background: var(--sum);
}

.legend .orbit {
  background: #4d5ec4;
}

.legend .singular {
  background: var(--warn);
}

.multiples-head {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.multiples-list {
  display: grid;
  gap: 6px;
  max-height: 230px;
  overflow: auto;
  padding-right: 2px;
}

.multiples-list div {
  display: grid;
  grid-template-columns: 42px 1fr;
  gap: 8px;
  border: 1px solid #e0e5e8;
  border-radius: 8px;
  background: #fbfcfd;
  padding: 7px 8px;
  font-size: 12px;
}

.multiples-list span {
  color: #4d5ec4;
  font-weight: 800;
}

.multiples-list strong {
  font-weight: 650;
  overflow-wrap: anywhere;
}

.group-summary {
  border-left: 3px solid #4d5ec4;
  border-radius: 0 8px 8px 0;
  background: #f4f6ff;
  color: #33407f;
  font-size: 13px;
  font-weight: 700;
  line-height: 1.6;
  padding: 9px 11px;
}

.group-panel {
  display: grid;
  gap: 10px;
  border-top: 1px solid var(--line);
  background: #fff;
  padding: 14px 16px 16px;
}

.group-panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.group-panel-head button {
  min-height: 32px;
}

.cycle-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  max-height: 150px;
  overflow: auto;
}

.cycle-list button {
  display: grid;
  grid-template-columns: 70px 1fr;
  gap: 8px;
  border: 1px solid #e0e5e8;
  border-radius: 8px;
  background: #fbfcfd;
  padding: 7px 8px;
  font-size: 12px;
  min-height: 0;
  min-width: 150px;
  text-align: left;
}

.cycle-list button:hover {
  border-color: #4d5ec4;
}

.cycle-list button.active {
  border-color: #c58c21;
  background: #fff8e7;
  color: var(--ink);
}

.cycle-list span {
  color: #4d5ec4;
  font-weight: 800;
}

.cycle-list strong {
  font-weight: 650;
}

canvas {
  display: block;
  width: 100%;
  height: min(68vh, 760px);
  min-height: 500px;
  background: #fbfcfd;
  cursor: crosshair;
}

@media (max-width: 920px) {
  .app {
    width: min(100vw - 20px, 760px);
    padding-top: 14px;
  }

  .topbar,
  .canvas-head {
    display: grid;
  }

  .status {
    max-width: none;
    text-align: left;
  }

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

  canvas {
    height: 62vh;
    min-height: 430px;
  }
}

@media (max-width: 520px) {
  .two-col,
  .button-row,
  .mode-toggle,
  .coefficient-grid,
  .presets,
  .prime-presets {
    grid-template-columns: 1fr;
  }
}
