:root {
  --bg: #f6f1e8;
  --panel: rgba(255, 252, 245, 0.88);
  --panel-strong: #fffaf0;
  --border: rgba(89, 71, 47, 0.18);
  --text: #2c2418;
  --muted: #705f49;
  --accent: #8d5524;
  --accent-soft: rgba(141, 85, 36, 0.14);
  --danger: #9b2226;
  --success: #2f6f4f;
  --shadow: 0 18px 50px rgba(76, 52, 20, 0.12);
  --graph-bg: #fffdf9;
  --grid: rgba(96, 79, 58, 0.12);
  --axis: rgba(66, 49, 28, 0.3);
  --font-ui: "Avenir Next", "Gill Sans", "Trebuchet MS", sans-serif;
  --font-display: "Palatino", "Book Antiqua", Georgia, serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--font-ui);
  color: var(--text);
  background:
    radial-gradient(circle at top left, rgba(227, 183, 140, 0.28), transparent 35%),
    radial-gradient(circle at bottom right, rgba(166, 204, 176, 0.24), transparent 30%),
    linear-gradient(180deg, #fbf6ee 0%, #f2eadd 100%);
}

button,
input,
select {
  font: inherit;
}

button,
select {
  cursor: pointer;
}

.app-shell {
  max-width: 1600px;
  margin: 0 auto;
  padding: 24px;
}

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

.eyebrow {
  margin: 0 0 6px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-size: 0.75rem;
}

h1,
h2,
h3 {
  font-family: var(--font-display);
  margin: 0;
}

h1 {
  font-size: clamp(2rem, 4vw, 3.5rem);
}

.language-switch {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}

.workspace {
  display: grid;
  grid-template-columns: minmax(320px, 380px) minmax(0, 1fr);
  gap: 20px;
  align-items: stretch;
}

.sidebar,
.graph-area {
  min-height: 0;
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.function-panel,
.tool-panel,
.relation-panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 24px;
  box-shadow: var(--shadow);
}

.function-panel {
  padding: 16px;
  display: grid;
  gap: 12px;
}

.function-card {
  padding: 14px;
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.56);
  border: 1px solid rgba(76, 52, 20, 0.08);
  display: grid;
  gap: 10px;
}

.function-card.active {
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.92), rgba(247, 237, 221, 0.94));
  border-color: var(--card-color, var(--accent));
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--card-color, var(--accent)) 28%, white);
}

.card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.function-name {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  font-size: 1.2rem;
}

.color-dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--card-color, var(--accent));
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--card-color, var(--accent)) 18%, white);
}

.toggle-row {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
}

.toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
}

.card-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

button,
.secondary-button,
.mode-button {
  border: none;
  border-radius: 999px;
  padding: 11px 16px;
  background: var(--accent);
  color: white;
  transition: transform 140ms ease, opacity 140ms ease, background 140ms ease;
}

button:hover,
.secondary-button:hover,
.mode-button:hover {
  transform: translateY(-1px);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
  transform: none;
}

.secondary-button,
.mode-button {
  background: rgba(98, 74, 42, 0.12);
  color: var(--text);
}

.mode-button.active {
  background: var(--accent);
  color: white;
}

.tool-panel {
  padding: 16px;
  display: grid;
  gap: 12px;
}

.scale-panel {
  display: grid;
  gap: 10px;
  padding-top: 4px;
  border-top: 1px solid rgba(97, 79, 56, 0.12);
}

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

.mode-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 88px;
  padding: 8px 12px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 0.9rem;
}

.tool-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.graph-area {
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  gap: 20px;
}

.graph-frame {
  position: relative;
  min-height: 68vh;
  border-radius: 28px;
  overflow: hidden;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}

#graph-svg {
  width: 100%;
  height: 100%;
  display: block;
  background: var(--graph-bg);
  touch-action: none;
}

.graph-bg {
  fill: url(#graph-fill);
}

.graph-overlay {
  position: absolute;
  inset: 16px;
  pointer-events: none;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
}

.legend,
.status-box {
  display: grid;
  gap: 8px;
}

.legend-item,
.status-message {
  pointer-events: auto;
  border-radius: 999px;
  padding: 8px 12px;
  background: rgba(255, 252, 245, 0.92);
  border: 1px solid var(--border);
  backdrop-filter: blur(10px);
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.status-message.success {
  color: var(--success);
}

.status-message.error {
  color: var(--danger);
}

.relation-panel {
  padding: 16px;
  display: grid;
  gap: 12px;
}

.relation-label-row,
.relation-input-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.relation-input-row input,
#term-input {
  flex: 1 1 320px;
  min-width: 0;
  border-radius: 16px;
  border: 1px solid rgba(97, 79, 56, 0.16);
  background: rgba(255, 255, 255, 0.86);
  color: var(--text);
  padding: 14px 16px;
}

.help-text {
  margin: 0;
  color: var(--muted);
  line-height: 1.45;
}

#term-dialog {
  border: none;
  padding: 0;
  border-radius: 24px;
  background: transparent;
}

#term-dialog::backdrop {
  background: rgba(41, 31, 18, 0.35);
  backdrop-filter: blur(5px);
}

.dialog-card {
  min-width: min(90vw, 520px);
  padding: 22px;
  border-radius: 24px;
  background: var(--panel-strong);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  display: grid;
  gap: 14px;
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.dialog-close {
  width: 40px;
  height: 40px;
  padding: 0;
  font-size: 1.4rem;
  border-radius: 50%;
  background: rgba(98, 74, 42, 0.12);
  color: var(--text);
}

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

.grid-line {
  stroke: var(--grid);
  stroke-width: 1;
}

.axis-line {
  stroke: var(--axis);
  stroke-width: 2;
}

.axis-label {
  fill: var(--muted);
  font-size: 15px;
}

.tick-label {
  fill: rgba(78, 63, 43, 0.78);
  font-size: 13px;
}

.function-path {
  fill: none;
  stroke-width: 4;
  stroke-linejoin: round;
  stroke-linecap: round;
  cursor: pointer;
}

.function-path.inactive {
  opacity: 0.3;
}

.support-handle {
  fill: white;
  stroke-width: 3;
}

@media (max-width: 1080px) {
  .workspace {
    grid-template-columns: 1fr;
  }

  .graph-frame {
    min-height: 58vh;
  }
}

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

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

  .language-switch {
    justify-content: space-between;
  }

  .graph-overlay {
    inset: 12px;
    flex-direction: column;
    justify-content: space-between;
  }
}
