:root {
  color-scheme: light dark;
  --bg: #f4f5f8;
  --panel: #ffffff;
  --text: #17181c;
  --muted: #6b7280;
  --line: #e3e5ec;
  --key: #ffffff;
  --key-text: #17181c;
  --key-shadow: 0 1px 2px rgba(17, 24, 39, 0.08);
  --function: #eceef4;
  --operator: #eef2ff;
  --operator-text: #3b4cca;
  --equals: #3b4cca;
  --equals-text: #ffffff;
  --danger: #b42318;
  --radius: 14px;
}

html[data-theme='dark'] {
  --bg: #14161c;
  --panel: #1c1f27;
  --text: #f2f3f7;
  --muted: #9aa1b1;
  --line: #2c3039;
  --key: #232732;
  --key-text: #f2f3f7;
  --key-shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  --function: #2b303c;
  --operator: #2c3350;
  --operator-text: #b9c4ff;
  --equals: #5b6cff;
  --equals-text: #ffffff;
  --danger: #ff8b7e;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
  -webkit-text-size-adjust: 100%;
}

.app {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 100%;
  padding: 14px 14px calc(14px + env(safe-area-inset-bottom));
  background: var(--bg);
}

.app__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.app__title {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.01em;
}

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

.badge {
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 11px;
  line-height: 18px;
}

.ghost {
  appearance: none;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--muted);
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 12px;
  cursor: pointer;
}

.ghost:hover { color: var(--text); }
.ghost:focus-visible { outline: 2px solid var(--operator-text); outline-offset: 2px; }

.display {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
  padding: 16px 14px;
  border-radius: var(--radius);
  background: var(--panel);
  border: 1px solid var(--line);
  min-height: 96px;
  overflow: hidden;
}

.display__expression {
  min-height: 18px;
  max-width: 100%;
  color: var(--muted);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
}

.display__expression::-webkit-scrollbar { display: none; }

.display__value {
  width: 100%;
  text-align: right;
  font-size: clamp(28px, 9vw, 44px);
  font-weight: 300;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
  white-space: nowrap;
  overflow-x: auto;
  scrollbar-width: none;
}

.display__value::-webkit-scrollbar { display: none; }
.display__value.is-error { color: var(--danger); font-size: clamp(18px, 6vw, 26px); }

.status {
  margin: 0;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.status__button {
  appearance: none;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--function);
  color: var(--text);
  font-size: 12px;
  padding: 3px 10px;
  cursor: pointer;
}

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

.key {
  appearance: none;
  -webkit-tap-highlight-color: transparent;
  border: 1px solid var(--line);
  background: var(--key);
  color: var(--key-text);
  box-shadow: var(--key-shadow);
  border-radius: var(--radius);
  font-size: clamp(17px, 5vw, 21px);
  font-weight: 500;
  padding: 0;
  min-height: 52px;
  cursor: pointer;
  transition: transform 80ms ease, filter 120ms ease;
}

.key:hover { filter: brightness(0.98); }
.key:active { transform: scale(0.97); }
.key:focus-visible { outline: 2px solid var(--operator-text); outline-offset: 2px; }

.key--function { background: var(--function); color: var(--muted); font-size: clamp(15px, 4.4vw, 18px); }
.key--operator { background: var(--operator); color: var(--operator-text); }
.key--equals { background: var(--equals); color: var(--equals-text); border-color: transparent; }

.history {
  border-top: 1px solid var(--line);
  padding-top: 10px;
  max-height: 180px;
  overflow-y: auto;
}

.history__list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.history__item {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.history__item span:last-child { color: var(--text); font-weight: 500; }

.history__empty { font-size: 12px; color: var(--muted); }

@media (max-width: 360px) {
  .app { padding: 10px 10px calc(10px + env(safe-area-inset-bottom)); gap: 10px; }
  .display { min-height: 84px; padding: 12px 10px; }
  .key { min-height: 46px; border-radius: 12px; }
  .keypad { gap: 6px; }
}

@media (prefers-reduced-motion: reduce) {
  .key { transition: none; }
}
