:root {
  --x: #ff5500;
  --y: #2f6fd6;
  --tan: #2e9e5b;
  --ink: #222;
  --muted: #888;
  --line: #e6e6e6;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
  background: #fff;
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

.app {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 28px;
  height: 100vh;
  height: 100svh;
  padding: 20px 28px;
}

.left {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.circle-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
}

.circle-wrap canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.circle-wrap canvas.grabbable {
  cursor: grab;
}

.circle-wrap canvas.dragging {
  cursor: grabbing;
}

.controls {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-bottom: 8px;
  font-size: 14px;
}

.control-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 10px 20px;
}

button,
input[type="text"] {
  font: inherit;
  border: 1px solid #d4d4d4;
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 6px 12px;
}

button {
  cursor: pointer;
}

button:hover {
  background: #f6f6f6;
}

.segmented {
  display: inline-flex;
}

.segmented button {
  border-radius: 0;
}

.segmented button:first-child {
  border-radius: 8px 0 0 8px;
}

.segmented button:last-child {
  border-radius: 0 8px 8px 0;
  border-left: none;
}

.segmented button.active {
  background: var(--ink);
  border-color: var(--ink);
  color: #fff;
}

.jump {
  display: inline-flex;
  gap: 6px;
}

.jump input {
  width: 190px;
}

.jump input.invalid {
  border-color: var(--x);
  outline: none;
}

.check {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  user-select: none;
}

.radius {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  user-select: none;
}

.radius input {
  width: 120px;
  accent-color: var(--ink);
}

#radius-value {
  min-width: 1.5em;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.playback {
  display: inline-flex;
  gap: 6px;
}

.home-link {
  position: absolute;
  top: 18px;
  left: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 8px;
  color: var(--muted);
}

.home-link:hover {
  background: #f6f6f6;
  color: var(--ink);
}

.home-link svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  padding: 6px 0;
}

.icon-button svg {
  width: 16px;
  height: 16px;
  fill: currentColor;
}

.icon-button .icon-stop,
.icon-button.playing .icon-play {
  display: none;
}

.icon-button.playing .icon-stop {
  display: block;
}

.right {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
}

.graph-toolbar {
  display: flex;
  justify-content: center;
  font-size: 14px;
}

.graph {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 10px 6px;
}

.graph header {
  display: flex;
  align-items: center;
  gap: 10px;
  height: 42px;
  flex: none;
  font-size: 15px;
}

.fn {
  font-weight: 700;
}

.fn.cos { color: var(--x); }
.fn.sin { color: var(--y); }
.fn.tan { color: var(--tan); }

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

.hint .frac {
  font-size: 1.05em;
  margin: 0 2px;
}

.val {
  margin-left: auto;
  font-size: 20px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.graph[data-fn="cos"] .val { color: var(--x); }
.graph[data-fn="sin"] .val { color: var(--y); }
.graph[data-fn="tan"] .val { color: var(--tan); }

.approx {
  color: var(--muted);
  font-size: 14px;
  font-weight: 400;
  margin-left: 6px;
}

.frac {
  display: inline-flex;
  flex-direction: column;
  vertical-align: middle;
  text-align: center;
  font-size: 0.8em;
  line-height: 1.1;
}

.frac > span:first-child {
  border-bottom: 1.5px solid currentColor;
  padding: 0 2px;
}

.plot {
  position: relative;
  flex: 1;
}

.plot canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  cursor: crosshair;
  touch-action: none;
}

/* Phones and other small screens: controls pack into three rows (units + playback, angle box, radius + symmetries) */
@media (max-width: 600px), (max-height: 500px) {
  .control-row {
    gap: 8px 12px;
  }

  .jump {
    order: 1;
    flex-basis: 100%;
    justify-content: center;
  }

  .radius input {
    width: 90px;
  }

  .home-link {
    top: 10px;
    left: 6px;
  }
}

@media (max-width: 900px) and (orientation: portrait) {
  .app {
    grid-template-columns: 1fr;
    height: auto;
  }

  .circle-wrap {
    flex: none;
    height: min(95vw, 560px);
  }

  .graph {
    flex: none;
    height: 210px;
  }
}

@media (max-width: 600px) and (orientation: portrait) {
  .app {
    gap: 8px;
    padding: 10px 16px 20px;
  }
}

/* Phone landscape: side by side and fitting the screen exactly, with smaller controls reflowed into as few rows as fit */
@media (orientation: landscape) and (max-height: 500px) {
  .app {
    gap: 16px;
    padding: 6px 16px 6px 44px;
  }

  .controls {
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    gap: 6px 10px;
    padding-bottom: 0;
    font-size: 13px;
  }

  .control-row {
    display: contents;
  }

  .segmented { order: 0; }
  .playback { order: 1; }
  .check { order: 2; }
  .radius { order: 4; }

  .jump {
    order: 3;
    flex-basis: auto;
  }

  button,
  input[type="text"] {
    padding: 4px 8px;
  }

  .icon-button {
    width: 32px;
    padding: 4px 0;
  }

  .jump input {
    width: 140px;
  }

  .radius input {
    width: 80px;
  }

  .home-link {
    top: 4px;
  }

  .graph-toolbar {
    font-size: 13px;
  }

  .right {
    gap: 6px;
  }

  .graph {
    min-height: 0;
    padding: 2px 8px 4px;
  }

  .graph header {
    height: 30px;
    font-size: 13px;
  }

  .val {
    font-size: 16px;
  }
}
