:root {
  --bg: #0a0b0e;
  --panel: #13151a;
  --key: #1c1f26;
  --fn: #2b303a;
  --txt: #f4f5f7;
  --dim: #8b92a1;
  --op: #ff9f0a;
  --eq: #3b82f6;
  --err: #ff6b6b;
  --ok: #30d158;
  --line: rgba(255, 255, 255, 0.08);
  --shadow: 0 20px 50px rgba(0, 0, 0, 0.5);
  --g: clamp(8px, 2.6vw, 14px);
  color-scheme: dark light;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #e9ecf1;
    --panel: #ffffff;
    --key: #ffffff;
    --fn: #dde1e9;
    --txt: #12141a;
    --dim: #6b7280;
    --err: #e5484d;
    --ok: #23a94a;
    --line: rgba(20, 30, 60, 0.1);
    --shadow: 0 20px 50px rgba(20, 30, 60, 0.14);
  }
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html {
  height: 100%;
}

body {
  height: 100%;
  height: 100dvh;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  background: var(--bg);
  color: var(--txt);
  font: 500 16px/1.2 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  padding:
    max(12px, env(safe-area-inset-top))
    max(12px, env(safe-area-inset-right))
    max(12px, env(safe-area-inset-bottom))
    max(12px, env(safe-area-inset-left));
}

/* ---------- Layout ---------- */
.calc {
  container-type: inline-size;
  width: min(100%, 400px);
  height: min(100%, 760px);
  display: flex;
  flex-direction: column;
  gap: var(--g);
}

.display {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: 6px;
  padding: clamp(16px, 5cqw, 24px);
  background: var(--panel);
  border-radius: clamp(20px, 7cqw, 30px);
  box-shadow: var(--shadow);
  font-variant-numeric: tabular-nums;
}

.bar {
  margin-bottom: auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--op);
  transition: background 0.3s ease;
}

.dot[data-on="1"] {
  background: var(--ok);
}

.ico {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px;
  margin: -8px;
  background: none;
  border: 0;
  border-radius: 12px;
  color: var(--dim);
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  touch-action: manipulation;
}

.ico:hover { color: var(--txt); }

.cnt:empty { display: none; }

.sub {
  min-height: 1.3em;
  font-size: clamp(0.9rem, 4.4cqw, 1.1rem);
  color: var(--dim);
  text-align: right;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sub.err {
  color: var(--err);
}

.out {
  display: block;
  font-size: clamp(2.4rem, 14cqw, 3.6rem);
  font-weight: 500;
  letter-spacing: -0.02em;
  text-align: right;
  white-space: nowrap;
  overflow: hidden;
}

.keys {
  flex: 0 0 58%;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(5, minmax(0, 1fr));
  gap: var(--g);
}

/* ---------- Keys ---------- */
.keys button {
  font: inherit;
  font-size: clamp(1.2rem, 6.4cqw, 1.7rem);
  color: var(--txt);
  background: var(--key);
  border: 0;
  border-radius: clamp(14px, 5cqw, 24px);
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  transition: transform 0.08s ease, filter 0.15s ease;
}

.keys .fn { background: var(--fn); }
.keys .clr { color: var(--err); }
.keys .op { background: var(--op); color: #fff; }
.keys .eq { grid-column: span 2; background: var(--eq); color: #fff; }

@media (hover: hover) {
  .keys button:hover { filter: brightness(1.12); }
}

.keys button:active,
.keys button.hit {
  transform: scale(0.94);
  filter: brightness(1.25);
}

button:focus-visible,
input:focus-visible {
  outline: 2px solid var(--eq);
  outline-offset: 2px;
}

/* ---------- History sheet ---------- */
dialog {
  margin: auto;
  width: min(92vw, 420px);
  max-height: min(86dvh, 680px);
  padding: 0;
  border: 0;
  border-radius: 26px;
  background: var(--panel);
  color: var(--txt);
  box-shadow: var(--shadow);
  overflow: hidden;
}

dialog[open] {
  display: flex;
  flex-direction: column;
  animation: rise 0.22s ease;
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(4px);
}

@keyframes rise {
  from { opacity: 0; transform: translateY(16px); }
}

.sheet {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 18px 18px max(18px, env(safe-area-inset-bottom));
  font-variant-numeric: tabular-nums;
}

.dh {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dh h2 {
  font-size: 1.15rem;
  font-weight: 600;
}

.x {
  width: 36px;
  height: 36px;
  border-radius: 50%;
}

.sheet button {
  font: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  padding: 12px 10px;
  color: var(--txt);
  background: var(--fn);
  border: 0;
  border-radius: 14px;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.08s ease, filter 0.15s ease;
}

.sheet button:active { transform: scale(0.96); }

@media (hover: hover) {
  .sheet button:hover { filter: brightness(1.12); }
}

#list {
  flex: 1;
  min-height: 120px;
  overflow-y: auto;
  overscroll-behavior: contain;
  list-style: none;
  margin: 0 -6px;
}

#list li {
  display: flex;
  align-items: center;
  gap: 4px;
  border-bottom: 1px solid var(--line);
}

#list li.empty {
  justify-content: center;
  padding: 40px 0;
  color: var(--dim);
  border: 0;
}

#list .it {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
  padding: 12px 8px;
  background: none;
  border-radius: 12px;
  text-align: right;
}

#list .it small {
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--dim);
  font-size: 0.85rem;
  font-weight: 500;
}

#list .it b {
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 1.35rem;
  font-weight: 500;
}

#list .del {
  flex: none;
  width: 34px;
  height: 34px;
  padding: 0;
  background: none;
  color: var(--dim);
  border-radius: 50%;
}

#list .del:hover { color: var(--err); }

.acts {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.join {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 8px;
}

.join .code {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  letter-spacing: 0.06em;
}

.join input {
  min-width: 0;
  padding: 0 12px;
  font: inherit;
  font-size: 0.95rem;
  text-transform: uppercase;
  color: var(--txt);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 14px;
}

.sheet .danger {
  color: var(--err);
  background: transparent;
  border: 1px solid var(--err);
}

/* ---------- Toast ---------- */
#toast {
  inset: auto auto max(28px, env(safe-area-inset-bottom)) 50%;
  transform: translateX(-50%);
  margin: 0;
  padding: 10px 18px;
  border: 0;
  border-radius: 999px;
  background: var(--txt);
  color: var(--bg);
  font-size: 0.9rem;
  font-weight: 600;
  box-shadow: var(--shadow);
}

/* ---------- Phones: sheet docks to the bottom ---------- */
@media (max-width: 520px) {
  dialog {
    margin: auto auto 0;
    width: 100%;
    max-width: 100%;
    max-height: 88dvh;
    border-radius: 26px 26px 0 0;
  }
}

/* ---------- Landscape phones ---------- */
@media (max-height: 500px) and (orientation: landscape) {
  .calc {
    width: min(100%, 760px);
    flex-direction: row;
  }
  .display { flex: 1; }
  .keys { flex: 1.15; }
  dialog {
    margin: auto;
    width: min(92vw, 560px);
    max-height: 94dvh;
    border-radius: 22px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .keys button, .sheet button { transition: none; }
  dialog[open] { animation: none; }
}