/* =========================================================
   Calculator keypad styles (standard calculator; reusable for scientific).
   Load after styles.css and tool.css.
   ========================================================= */

.calc-layout {
  display: grid;
  grid-template-columns: minmax(0, 400px) minmax(0, 1fr);
  gap: 20px;
  align-items: start;
}
@media (max-width: 760px) {
  .calc-layout { grid-template-columns: minmax(0, 1fr); }
}

.calc {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 16px;
  width: 100%;
  max-width: 400px;
}

/* ---------- Display ---------- */
.calc-display {
  background: var(--surface-2);
  border-radius: 14px;
  padding: 32px 16px 12px;   /* top row is kept free for the memory flag and Copy button */
  margin-bottom: 14px;
  text-align: right;
  min-height: 116px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  position: relative;
  overflow: hidden;
}
.calc-expr {
  color: var(--muted);
  font-size: 0.95rem;
  min-height: 1.4em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  direction: rtl;          /* keep the most recent part visible when long */
  unicode-bidi: plaintext;
  font-variant-numeric: tabular-nums;
}
.calc-value {
  font-size: 2.6rem;
  font-weight: 600;
  letter-spacing: -0.02em;
  line-height: 1.15;
  white-space: nowrap;
  overflow: hidden;
  font-variant-numeric: tabular-nums;
  transition: font-size 0.1s ease;
}
.calc-value.error { color: #e11d48; font-size: 1.5rem; }
.calc-flags {
  position: absolute;
  top: 10px; left: 14px;
  display: flex; gap: 8px;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  color: var(--accent);
}
.calc-copy {
  position: absolute;
  top: 6px; right: 8px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 0.75rem;
  padding: 4px 8px;
  border-radius: 6px;
  cursor: pointer;
}
.calc-copy:hover { color: var(--text); background: var(--surface); }

/* ---------- Keys ---------- */
.calc-keys {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}
.key {
  height: 60px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 1.35rem;
  font-weight: 500;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  transition: background 0.08s ease, transform 0.05s ease;
}
.key:hover { background: var(--surface-2); }
.key:active, .key.pressed { transform: scale(0.96); background: var(--surface-2); }
.key.fn { background: var(--surface-2); color: var(--text); font-size: 1.1rem; }
.key.fn:hover { filter: brightness(0.97); }
.key.op { background: var(--accent-soft); color: var(--accent); font-size: 1.5rem; border-color: transparent; }
.key.op.active { background: var(--accent); color: var(--on-accent); }
.key.eq { background: var(--accent); color: var(--on-accent); border-color: var(--accent); font-size: 1.6rem; }
.key.eq:hover { filter: brightness(1.08); background: var(--accent); }
.key.mem {
  height: 38px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
  background: transparent;
  border-color: transparent;
}
.key.mem:hover { color: var(--text); background: var(--surface-2); }

@media (max-width: 420px) {
  .key { height: 56px; }
  .calc-value { font-size: 2.2rem; }
}

/* ---------- History ---------- */
.history-list { list-style: none; margin: 0; padding: 0; max-height: 420px; overflow-y: auto; }
.history-list li + li { border-top: 1px solid var(--border); }
.history-list button {
  display: block;
  width: 100%;
  text-align: right;
  background: transparent;
  border: 0;
  color: var(--text);
  font: inherit;
  padding: 10px 8px;
  border-radius: 8px;
  cursor: pointer;
  font-variant-numeric: tabular-nums;
}
.history-list button:hover { background: var(--surface-2); }
.history-list .h-expr { display: block; color: var(--muted); font-size: 0.85rem; word-break: break-all; }
.history-list .h-result { display: block; font-size: 1.15rem; font-weight: 600; word-break: break-all; }
.history-empty { color: var(--muted); font-size: 0.92rem; margin: 0; }

/* Touch-only devices (phones, tablets) have no keyboard to use shortcuts with */
@media (hover: none) and (pointer: coarse) {
  .kbd-panel { display: none; }
}

.kbd-table { width: 100%; border-collapse: collapse; font-size: 0.95rem; }
.kbd-table td { padding: 7px 0; border-bottom: 1px solid var(--border); }
.kbd-table td:first-child { width: 45%; }
kbd {
  display: inline-block;
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 0.85em;
  padding: 2px 7px;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 6px;
  background: var(--surface-2);
  margin-right: 4px;
}

/* =========================================================
   Scientific calculator
   ========================================================= */
.calc-layout.wide { grid-template-columns: minmax(0, 760px) minmax(0, 1fr); }
@media (max-width: 1100px) {
  .calc-layout.wide { grid-template-columns: minmax(0, 1fr); }
  .calc-layout.wide .calc { margin: 0 auto; }
}
.calc.sci { max-width: 760px; }

.sci-input {
  width: 100%;
  border: 0;
  background: transparent;
  text-align: right;
  font: inherit;
  font-size: 1.3rem;
  color: var(--text);
  caret-color: var(--accent);
  padding: 0;
  margin-top: 14px;
  font-variant-numeric: tabular-nums;
}
.sci-input:focus { outline: none; }
.sci-input::placeholder { color: var(--muted); opacity: 0.6; }
.calc-value.preview { color: var(--muted); font-weight: 500; }
.calc-flags .off { color: var(--muted); opacity: 0.5; }

.sci-keys {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 14px;
}
.sci-keys .pad {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}
.sci-keys .key { height: 54px; }
.key.sm { font-size: 1.02rem; }
.key.toggle[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }

@media (max-width: 640px) {
  .sci-keys { grid-template-columns: minmax(0, 1fr); gap: 8px; }
  .sci-keys .key { height: 46px; }
  .sci-keys .pad.sci-fn .key { height: 40px; font-size: 0.95rem; }
}

.fn-table { width: 100%; border-collapse: collapse; font-size: 0.95rem; }
.fn-table th, .fn-table td { padding: 8px 10px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
.fn-table th { font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.fn-table td:first-child { white-space: nowrap; font-weight: 600; }
.fn-table-wrap { overflow-x: auto; }
