:root {
  --page-background: #f8f3eb;
  --panel-background: #fffaf3;
  --display-background: #fffcf7;
  --border-color: #dfd2c0;
  --brand-teal: #009e96;
  --brand-gray: #717070;
  --brand-charcoal: #4b4948;
  --deep-teal: #00676b;
  --text-color: var(--brand-charcoal);
  --muted-text: #655d55;
  --accent-text: var(--deep-teal);
  --neutral-control: #eee4d6;
  --number-background: color-mix(in srgb, var(--brand-teal) 13%, white);
  --function-background: var(--brand-charcoal);
  --panel-shadow: #4b392318;
  color-scheme: light;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  color: var(--text-color);
  background: var(--page-background);
}

:root.dark-mode {
  --page-background: #151d1d;
  --panel-background: #202b2b;
  --display-background: #111a1a;
  --border-color: #425451;
  --text-color: #edf7f4;
  --muted-text: #b4c7c3;
  --accent-text: #a0e4db;
  --neutral-control: #303f3e;
  --number-background: #2b4140;
  --function-background: #465653;
  --panel-shadow: #0004;
  color-scheme: dark;
}

* { box-sizing: border-box; }

body {
  --page-padding: clamp(12px, 4vw, 32px);
  min-height: 100vh;
  min-height: 100svh;
  margin: 0;
  padding: var(--page-padding);
  display: grid;
  place-items: center;
}

.workspace {
  --workspace-gap: 16px;
  display: grid;
  width: min(100%, 408px);
  gap: var(--workspace-gap);
}

.camera-panel {
  align-self: start;
  display: grid;
  min-width: 0;
  gap: 8px;
  padding: 10px;
  border: 1px solid var(--border-color);
  border-radius: 18px;
  background: var(--panel-background);
}

.camera-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 4px 8px;
}

.camera-header h2 { font-size: .875rem; }

#camera-toggle {
  min-height: 32px;
  padding-inline: 10px;
  border-radius: 8px;
  background: var(--deep-teal);
  color: #fff;
  font-size: .75rem;
  font-weight: 650;
}

.camera-preview {
  display: grid;
  place-items: center;
  overflow: hidden;
  aspect-ratio: 16 / 9;
  border-radius: 9px;
  background: var(--neutral-control);
  color: var(--muted-text);
  font-size: .75rem;
}

#camera-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transform: scaleX(-1);
}

#camera-status {
  min-height: 2.6em;
  margin: 0;
  color: var(--muted-text);
  font-size: .75rem;
  line-height: 1.3;
}

#camera-flash {
  position: fixed;
  z-index: 2;
  inset: 0;
  background: #fff;
  opacity: 0;
  pointer-events: none;
}

#camera-alert {
  position: fixed;
  z-index: 3;
  right: var(--page-padding);
  bottom: var(--page-padding);
  margin: 0;
  border-radius: 12px;
  background: var(--deep-teal);
  color: #fff;
  font-weight: 650;
}

#camera-alert:not(:empty) {
  padding: 12px 16px;
  box-shadow: 0 4px 24px #4b392340;
}

.calculator, .history {
  min-width: 0;
  padding: clamp(16px, 5vw, 28px);
  border: 1px solid var(--border-color);
  border-radius: 36px;
  background: var(--panel-background);
  box-shadow: 0 24px 80px var(--panel-shadow);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  margin-bottom: 16px;
}

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

.calculator-logo {
  width: 45px;
  height: auto;
}

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

#theme-toggle {
  min-height: 44px;
  padding-inline: 10px;
  border-radius: 14px;
  background: var(--neutral-control);
  font-size: .75rem;
  font-weight: 650;
  white-space: nowrap;
}

#theme-toggle[aria-pressed="true"] {
  background: var(--deep-teal);
  color: #fff;
}

h1, h2 { margin: 0; font-size: 1.05rem; font-weight: 650; }

button {
  border: 0;
  cursor: pointer;
  color: inherit;
  font: inherit;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: background-color 140ms ease, transform 100ms ease;
}

button:focus-visible { outline: 2px solid var(--accent-text); outline-offset: 3px; }
button:active, button.key-active { transform: scale(.94); }

.delete {
  display: grid;
  place-items: center;
  width: 46px;
  height: 44px;
  border-radius: 14px;
  background: var(--neutral-control);
  font-size: 1.25rem;
}

.screen {
  container-type: inline-size;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  min-height: clamp(110px, 32vw, 146px);
  padding: 14px 16px;
  margin-bottom: 20px;
  border: 1px solid var(--border-color);
  border-radius: 22px;
  background: var(--display-background);
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.expression {
  min-height: 1.35em;
  color: var(--muted-text);
  font-size: clamp(.75rem, 4.2cqi, .94rem);
  overflow-wrap: anywhere;
}

output {
  display: block;
  font-size: clamp(1.1rem, var(--result-size, 22cqi), 4.5rem);
  font-weight: 350;
  line-height: 1.1;
  letter-spacing: -.045em;
  white-space: nowrap;
}

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

.keypad button {
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  border-radius: 50%;
  background: var(--number-background);
  color: var(--accent-text);
  box-shadow: inset 0 1px #ffffff14;
  font-size: clamp(1.45rem, 6vw, 2rem);
  font-weight: 500;
}

.keypad .function { background: var(--function-background); color: #fff; font-size: clamp(1rem, 4.8vw, 1.4rem); }
.keypad .operator, .keypad .equals { background: var(--deep-teal); color: #fff; }
.keypad .operator[aria-pressed="true"] { box-shadow: inset 0 0 0 3px #ffffffa6; }
.keypad .zero { grid-column: span 2; aspect-ratio: auto; border-radius: 999px; }

@media (hover: hover) {
  button:hover { filter: brightness(.94); }
}

footer {
  margin-top: 20px;
  text-align: center;
  color: var(--muted-text);
  font-size: .75rem;
  line-height: 1.65;
}

footer span { margin-inline: .2em; }
kbd { color: var(--text-color); font: inherit; font-weight: 650; }

.history {
  display: flex;
  flex-direction: column;
  font-variant-numeric: tabular-nums;
}

.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 44px;
  gap: 12px;
  margin-bottom: 16px;
}

.history-count {
  color: var(--muted-text);
  font-size: .875rem;
  white-space: nowrap;
}

.history-empty {
  margin: 0 0 12px;
  padding: 16px;
  border: 1px solid var(--border-color);
  border-radius: 16px;
  color: var(--muted-text);
  font-size: .875rem;
  line-height: 1.5;
}

.history-list {
  max-height: 22rem;
  margin: 0 0 12px;
  padding: 0;
  list-style: none;
  overflow: auto;
  overscroll-behavior: contain;
  scrollbar-color: var(--brand-gray) var(--display-background);
  scrollbar-width: thin;
  border: 1px solid var(--border-color);
  border-radius: 16px;
  background: var(--display-background);
}

.history-list:focus-visible { outline: 2px solid var(--accent-text); outline-offset: 3px; }

.history-list li { padding: 13px 15px; overflow-wrap: anywhere; }
.history-list li + li { border-top: 1px solid var(--border-color); }
.history-list time { display: block; color: var(--muted-text); font-size: .8125rem; margin-bottom: 5px; }
.history-list p { margin: 0; color: var(--text-color); font-size: .9375rem; }
.history-list strong { display: block; color: var(--accent-text); font-size: 1.15rem; font-weight: 600; }

.history-mascot {
  flex: none;
  align-self: flex-end;
  width: 100px;
  height: auto;
  margin-block-start: auto;
}

@media (min-width: 680px) {
  .workspace {
    --workspace-gap: 20px;
    width: min(100%, 756px);
    grid-template-columns: minmax(0, 408fr) minmax(0, 328fr);
  }

  .history { contain: size; }
  .history-list { max-height: none; }
}

@media (680px <= width < 880px) {
  .workspace { grid-template-rows: minmax(160px, 1fr) auto; }
  .calculator { grid-row: span 2; }
  .camera-panel { grid-column: 2; }
}

@media (min-width: 880px) {
  .workspace {
    width: min(100%, 984px);
    grid-template-columns: minmax(0, 408fr) minmax(0, 328fr) clamp(164px, 20vw, 208px);
  }
}

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