* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  background: var(--bg, #fdf6f5);
  color: var(--on-bg, #241c1e);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  position: relative;
}

#bg {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: -1;
  pointer-events: none;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
  padding: 10px 16px;
  border-bottom: 1px solid var(--outline-variant, #ddd);
  background: color-mix(in srgb, var(--bg, #fff) 72%, transparent);
}

.topbar .back {
  color: var(--primary, #d7263d);
  text-decoration: none;
  font-weight: 600;
  font-size: 14px;
}

.topbar .settings {
  margin-left: auto;
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.topbar label {
  font-size: 12px;
  color: var(--on-surface-variant, #666);
  display: flex;
  align-items: center;
  gap: 6px;
}

select,
input {
  font: inherit;
  color: var(--on-surface, #241c1e);
  background: var(--surface, #fff);
  border: 1px solid var(--outline, #ccc);
  border-radius: 8px;
  padding: 6px 8px;
  min-width: 0;
}

.tabs {
  display: flex;
  gap: 8px;
  padding: 10px 16px 0;
}

.tab {
  flex: 1;
  max-width: 170px;
  padding: 10px 12px;
  border: none;
  border-radius: 999px;
  background: var(--secondary-container, #eee);
  color: var(--on-secondary-container, #333);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.tab.active {
  background: var(--primary, #d7263d);
  color: var(--on-primary, #fff);
}

main {
  flex: 1;
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
  padding: 12px 14px 0;
  display: flex;
  flex-direction: column;
}

.panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.panel[hidden] {
  display: none;
}

.display {
  background: color-mix(in srgb, var(--surface-variant, #f3e6e7) 86%, transparent);
  color: var(--on-surface, #241c1e);
  border-radius: 18px;
  padding: 14px 16px;
  min-height: 118px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  overflow: hidden;
}

.display-expr {
  color: var(--on-surface-variant, #666);
  font-size: 14px;
  min-height: 18px;
  text-align: right;
  word-break: break-all;
}

.display-value {
  font-size: clamp(32px, 11vw, 46px);
  font-weight: 500;
  text-align: right;
  word-break: break-all;
  line-height: 1.15;
}

.display-value.error {
  color: #e5484d;
  font-size: clamp(22px, 7vw, 30px);
}

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

.display-actions button,
.graph-controls button,
.history-head button {
  font: inherit;
  font-size: 13px;
  border: 1px solid var(--outline, #ccc);
  background: var(--surface, #fff);
  color: var(--on-surface, #333);
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
}

.display-actions button.active {
  background: var(--primary, #d7263d);
  border-color: transparent;
  color: var(--on-primary, #fff);
  font-weight: 700;
}

.keypad {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-bottom: 10px;
  min-height: 340px;
}

.key-row {
  display: flex;
  gap: 8px;
  flex: 1;
}

.key {
  flex: 1;
  min-height: 46px;
  border: none;
  border-radius: 16px;
  font: inherit;
  font-size: clamp(15px, 4.4vw, 20px);
  font-weight: 500;
  cursor: pointer;
  transition: filter 0.12s;
}

.key:active {
  filter: brightness(0.94);
}

.key-digit {
  background: color-mix(in srgb, var(--surface, #fff) 82%, transparent);
  color: var(--on-surface, #241c1e);
}

.key-op {
  background: color-mix(in srgb, var(--primary-container, #ffd9dd) 82%, transparent);
  color: var(--on-primary-container, #40000a);
}

.key-func,
.key-const,
.key-angle {
  background: color-mix(in srgb, var(--secondary-container, #eee) 82%, transparent);
  color: var(--on-secondary-container, #222);
}

.key-clear {
  background: color-mix(in srgb, var(--tertiary-container, #c4ebc9) 82%, transparent);
  color: var(--on-tertiary-container, #033);
}

.key-back {
  background: color-mix(in srgb, var(--secondary-container, #eee) 82%, transparent);
  color: var(--on-secondary-container, #222);
}

.key-equals {
  background: color-mix(in srgb, var(--primary, #d7263d) 88%, transparent);
  color: var(--on-primary, #fff);
  font-weight: 700;
}

.field {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 15px;
}

.field input {
  flex: 1;
}

.graph-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--on-surface-variant, #666);
}

.graph-controls span {
  margin-right: auto;
}

#graph {
  width: 100%;
  flex: 1;
  min-height: 300px;
  background: var(--surface, #fff);
  border: 1px solid var(--outline-variant, #ddd);
  border-radius: 14px;
  touch-action: none;
  cursor: grab;
}

.card {
  background: color-mix(in srgb, var(--surface-variant, #f3e6e7) 90%, transparent);
  border-radius: 18px;
  padding: 16px;
}

.card h2 {
  margin: 0 0 2px;
  font-size: 18px;
}

.grid4 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 12px 0;
}

.grid4 label,
.field {
  color: var(--on-surface-variant, #666);
}

.grid4 label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
}

#slope-result,
#circle-result {
  margin-top: 6px;
}

.row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 5px 0;
  font-size: 14px;
  color: var(--on-surface-variant, #555);
}

.row b {
  color: var(--primary, #d7263d);
  font-weight: 600;
  word-break: break-all;
  text-align: right;
}

#slope-graph {
  width: 100%;
  height: 180px;
  margin-top: 12px;
  background: var(--surface, #fff);
  border-radius: 12px;
}

.muted {
  color: var(--on-surface-variant, #777);
  font-size: 13px;
}

.small {
  font-size: 12.5px;
}

.byline {
  text-align: center;
  color: var(--on-surface-variant, #888);
  font-size: 11px;
  padding: 10px;
}

#history {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: flex-end;
  z-index: 20;
}

#history[hidden] {
  display: none;
}

.history-sheet {
  background: var(--surface, #fff);
  color: var(--on-surface, #241c1e);
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
  border-radius: 18px 18px 0 0;
  padding: 16px;
  max-height: 70vh;
  overflow: auto;
}

.history-head {
  display: flex;
  align-items: center;
  gap: 8px;
}

.history-head h2 {
  margin: 0;
  flex: 1;
  font-size: 18px;
}

.history-item {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  border-bottom: 1px solid var(--outline-variant, #eee);
  background: none;
  color: inherit;
  font: inherit;
  padding: 10px 0;
  cursor: pointer;
}

.history-expr {
  display: block;
  color: var(--on-surface-variant, #666);
  font-size: 13px;
}

.history-result {
  display: block;
  color: var(--primary, #d7263d);
  font-weight: 600;
  font-size: 16px;
  margin-top: 2px;
}
