:root {
  color-scheme: dark;
  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
  --bg: #0f172a;
  --panel: #111c33;
  --panel-border: #22314f;
  --surface: #16223d;
  --surface-strong: #223057;
  --text: #e5ecff;
  --muted: #b0bfdc;
  --accent: #6ea8fe;
  --accent-strong: #3b82f6;
  --good: #88f2b1;
  --warn: #ffd166;
  --bad: #ff8a8a;
  --calculator-width: 540px;
  --calculator-half-width: 270px;
  --drawer-tab-size: 58px;
  --drawer-gap: 18px;
  --drawer-width: 290px;
  --drawer-expanded-width: calc(var(--drawer-tab-size) + var(--drawer-width));
  --page-padding: 24px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(circle at top, rgba(59, 130, 246, 0.18), transparent 38%),
    linear-gradient(180deg, #091120 0%, var(--bg) 100%);
  color: var(--text);
}

button,
textarea {
  font: inherit;
}

.app-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: var(--page-padding);
}

.calculator-stage {
  position: relative;
  width: min(100%, var(--calculator-width));
}

.calculator {
  width: 100%;
  display: grid;
  gap: 20px;
  padding: 28px;
  border: 1px solid var(--panel-border);
  border-radius: 24px;
  background: rgba(17, 28, 51, 0.88);
  box-shadow:
    0 24px 70px rgba(2, 6, 23, 0.45),
    inset 0 1px 0 rgba(255, 255, 255, 0.04);
  backdrop-filter: blur(14px);
}

.calculator__header h1,
.calculator__header p {
  margin: 0;
}

.eyebrow {
  margin-bottom: 8px;
  color: var(--accent);
  font-size: 0.9rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.calculator__header h1 {
  font-size: clamp(1.85rem, 4vw, 2.5rem);
  line-height: 1.1;
}

.supporting-text {
  margin-top: 10px;
  color: var(--muted);
  line-height: 1.5;
}

.expression-panel,
.result-panel {
  display: grid;
  gap: 12px;
  padding: 18px;
  border-radius: 20px;
  background: rgba(9, 17, 32, 0.55);
  border: 1px solid rgba(110, 168, 254, 0.12);
}

.expression-panel__label,
.result-panel__label {
  color: var(--muted);
  font-size: 0.95rem;
  font-weight: 600;
}

.history-drawer {
  position: absolute;
  top: 0;
  left: calc(100% + var(--drawer-gap));
  width: var(--drawer-tab-size);
  display: grid;
  grid-template-columns: var(--drawer-tab-size) minmax(0, 1fr);
  align-self: start;
  min-height: 280px;
  border: 1px solid var(--panel-border);
  border-radius: 24px;
  background: rgba(17, 28, 51, 0.88);
  box-shadow:
    0 24px 70px rgba(2, 6, 23, 0.32),
    inset 0 1px 0 rgba(255, 255, 255, 0.04);
  backdrop-filter: blur(14px);
  overflow: hidden;
  transition: width 0.18s ease;
}

.history-drawer[data-open="true"] {
  width: clamp(
    var(--drawer-tab-size),
    calc(50vw - var(--calculator-half-width) - var(--drawer-gap) - var(--page-padding)),
    var(--drawer-expanded-width)
  );
}

.history-drawer__toggle {
  border: none;
  padding: 16px 10px;
  background: linear-gradient(180deg, rgba(59, 130, 246, 0.22), rgba(22, 34, 61, 0.72));
  border-right: 1px solid rgba(110, 168, 254, 0.12);
  color: var(--accent);
  cursor: pointer;
  display: grid;
  place-items: center;
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  transition: background 0.12s ease;
}

.history-drawer__toggle:hover {
  background: linear-gradient(180deg, rgba(74, 138, 247, 0.28), rgba(34, 48, 87, 0.76));
}

.history-drawer__toggle[aria-expanded="true"] {
  background: linear-gradient(180deg, rgba(59, 130, 246, 0.34), rgba(34, 48, 87, 0.78));
}

.history-panel {
  display: grid;
  gap: 12px;
  padding: 20px;
  min-width: 0;
}

.history-panel__header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}

.history-panel__title,
.history-panel__subtitle,
.history-panel__empty {
  margin: 0;
}

.history-panel__title {
  color: var(--muted);
  font-size: 0.95rem;
  font-weight: 600;
}

.history-panel__subtitle,
.history-panel__empty {
  color: var(--muted);
}

.history-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 10px;
  max-height: min(380px, calc(100vh - 220px));
  overflow: auto;
}

.history-list__item {
  padding: 14px;
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid rgba(110, 168, 254, 0.12);
}

.history-list__expression,
.history-list__result {
  margin: 0;
  font-family: Consolas, "Courier New", monospace;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.history-list__result {
  margin-top: 8px;
  color: var(--good);
  font-weight: 700;
}

.expression-panel__input {
  width: 100%;
  min-height: 108px;
  resize: vertical;
  border: none;
  border-radius: 16px;
  padding: 16px;
  background: var(--surface);
  color: var(--text);
  font-size: 1.5rem;
  line-height: 1.35;
  font-family: Consolas, "Courier New", monospace;
}

.expression-panel__input:focus-visible,
.key:focus-visible,
.history-drawer__toggle:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.result-panel__value {
  font-size: clamp(2rem, 6vw, 3rem);
  font-weight: 700;
  letter-spacing: -0.03em;
  overflow-wrap: anywhere;
}

.result-panel__status {
  margin: 0;
  color: var(--good);
  min-height: 1.5em;
}

.result-panel__status.is-warning {
  color: var(--warn);
}

.result-panel__status.is-error {
  color: var(--bad);
}

.keypad {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
}

.key {
  min-height: 64px;
  border: 1px solid rgba(110, 168, 254, 0.16);
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
  font-size: 1.2rem;
  font-weight: 700;
  cursor: pointer;
  transition:
    transform 0.12s ease,
    border-color 0.12s ease,
    background 0.12s ease;
}

.key:hover {
  transform: translateY(-1px);
  border-color: rgba(110, 168, 254, 0.4);
  background: var(--surface-strong);
}

.key:active {
  transform: translateY(1px);
}

.key--operator,
.key--action {
  color: var(--accent);
}

.key--action {
  font-size: 1rem;
}

.key--equals {
  background: linear-gradient(180deg, #3b82f6 0%, #2563eb 100%);
  border-color: rgba(147, 197, 253, 0.45);
}

.key--equals:hover {
  background: linear-gradient(180deg, #4a8af7 0%, #2d6cf0 100%);
}

.key:disabled,
.key:disabled:hover,
.key:disabled:active {
  transform: none;
  cursor: not-allowed;
  opacity: 0.55;
  border-color: rgba(110, 168, 254, 0.1);
  background: var(--surface);
}

.helper-text {
  margin: 0;
  color: var(--muted);
  line-height: 1.5;
}

code {
  padding: 0.1rem 0.35rem;
  border-radius: 6px;
  background: rgba(110, 168, 254, 0.12);
}

@media (max-width: 860px) {
  .app-shell {
    place-items: start center;
  }

  .calculator-stage {
    width: min(100%, var(--calculator-width));
    display: grid;
    gap: 14px;
  }

  .history-drawer {
    position: static;
    left: auto;
    top: auto;
    width: 100%;
    grid-template-columns: minmax(0, 1fr);
    min-height: 0;
  }

  .history-drawer[data-open="true"] {
    width: 100%;
  }

  .history-drawer__toggle {
    writing-mode: horizontal-tb;
    transform: none;
    min-height: 54px;
    padding: 0 18px;
    border-right: none;
    border-bottom: 1px solid rgba(110, 168, 254, 0.12);
    justify-content: start;
  }

  .history-panel {
    padding-top: 18px;
  }

  .history-list {
    max-height: 240px;
  }
}

@media (max-width: 540px) {
  .calculator {
    padding: 20px;
  }

  .key {
    min-height: 56px;
  }
}
